From ab868df50eb6f9f3958bdc0a7ab9199dcdf46b3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Mr=C3=A1zek?= Date: Sat, 11 Mar 2017 01:39:45 +0100 Subject: NOISSUE Wonko is the new Meta And then Wonko was the Meta. --- api/logic/meta/Index.cpp | 150 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 150 insertions(+) create mode 100644 api/logic/meta/Index.cpp (limited to 'api/logic/meta/Index.cpp') diff --git a/api/logic/meta/Index.cpp b/api/logic/meta/Index.cpp new file mode 100644 index 00000000..8a6b1355 --- /dev/null +++ b/api/logic/meta/Index.cpp @@ -0,0 +1,150 @@ +/* Copyright 2015-2017 MultiMC Contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "Index.h" + +#include "VersionList.h" +#include "tasks/LocalLoadTask.h" +#include "tasks/RemoteLoadTask.h" +#include "format/Format.h" + +namespace Meta +{ +Index::Index(QObject *parent) + : QAbstractListModel(parent) +{ +} +Index::Index(const QVector &lists, QObject *parent) + : QAbstractListModel(parent), m_lists(lists) +{ + for (int i = 0; i < m_lists.size(); ++i) + { + m_uids.insert(m_lists.at(i)->uid(), m_lists.at(i)); + connectVersionList(i, m_lists.at(i)); + } +} + +QVariant Index::data(const QModelIndex &index, int role) const +{ + if (index.parent().isValid() || index.row() < 0 || index.row() >= m_lists.size()) + { + return QVariant(); + } + + VersionListPtr list = m_lists.at(index.row()); + switch (role) + { + case Qt::DisplayRole: + switch (index.column()) + { + case 0: return list->humanReadable(); + default: break; + } + case UidRole: return list->uid(); + case NameRole: return list->name(); + case ListPtrRole: return QVariant::fromValue(list); + } + return QVariant(); +} +int Index::rowCount(const QModelIndex &parent) const +{ + return m_lists.size(); +} +int Index::columnCount(const QModelIndex &parent) const +{ + return 1; +} +QVariant Index::headerData(int section, Qt::Orientation orientation, int role) const +{ + if (orientation == Qt::Horizontal && role == Qt::DisplayRole && section == 0) + { + return tr("Name"); + } + else + { + return QVariant(); + } +} + +std::unique_ptr Index::remoteUpdateTask() +{ + return std::unique_ptr(new IndexRemoteLoadTask(this, this)); +} +std::unique_ptr Index::localUpdateTask() +{ + return std::unique_ptr(new IndexLocalLoadTask(this, this)); +} + +QJsonObject Index::serialized() const +{ + return Format::serializeIndex(this); +} + +bool Index::hasUid(const QString &uid) const +{ + return m_uids.contains(uid); +} +VersionListPtr Index::getList(const QString &uid) const +{ + return m_uids.value(uid, nullptr); +} +VersionListPtr Index::getListGuaranteed(const QString &uid) const +{ + return m_uids.value(uid, std::make_shared(uid)); +} + +void Index::merge(const Ptr &other) +{ + const QVector lists = std::dynamic_pointer_cast(other)->m_lists; + // initial load, no need to merge + if (m_lists.isEmpty()) + { + beginResetModel(); + m_lists = lists; + for (int i = 0; i < lists.size(); ++i) + { + m_uids.insert(lists.at(i)->uid(), lists.at(i)); + connectVersionList(i, lists.at(i)); + } + endResetModel(); + } + else + { + for (const VersionListPtr &list : lists) + { + if (m_uids.contains(list->uid())) + { + m_uids[list->uid()]->merge(list); + } + else + { + beginInsertRows(QModelIndex(), m_lists.size(), m_lists.size()); + connectVersionList(m_lists.size(), list); + m_lists.append(list); + m_uids.insert(list->uid(), list); + endInsertRows(); + } + } + } +} + +void Index::connectVersionList(const int row, const VersionListPtr &list) +{ + connect(list.get(), &VersionList::nameChanged, this, [this, row]() + { + emit dataChanged(index(row), index(row), QVector() << Qt::DisplayRole); + }); +} +} -- cgit v1.2.3