2018-02-11 05:10:01 +05:30
|
|
|
/* Copyright 2015-2018 MultiMC Contributors
|
2016-04-07 02:39:30 +05:30
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2017-03-11 06:09:45 +05:30
|
|
|
#include "Index.h"
|
2016-04-07 02:39:30 +05:30
|
|
|
|
2017-03-11 06:09:45 +05:30
|
|
|
#include "VersionList.h"
|
2017-03-18 06:52:36 +05:30
|
|
|
#include "JsonFormat.h"
|
2016-04-07 02:39:30 +05:30
|
|
|
|
2017-03-11 06:09:45 +05:30
|
|
|
namespace Meta
|
|
|
|
{
|
|
|
|
Index::Index(QObject *parent)
|
2018-07-15 18:21:05 +05:30
|
|
|
: QAbstractListModel(parent)
|
2016-04-07 02:39:30 +05:30
|
|
|
{
|
|
|
|
}
|
2017-03-11 06:09:45 +05:30
|
|
|
Index::Index(const QVector<VersionListPtr> &lists, QObject *parent)
|
2018-07-15 18:21:05 +05:30
|
|
|
: QAbstractListModel(parent), m_lists(lists)
|
2016-04-07 02:39:30 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
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));
|
|
|
|
}
|
2016-04-07 02:39:30 +05:30
|
|
|
}
|
|
|
|
|
2017-03-11 06:09:45 +05:30
|
|
|
QVariant Index::data(const QModelIndex &index, int role) const
|
2016-04-07 02:39:30 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
if (index.parent().isValid() || index.row() < 0 || index.row() >= m_lists.size())
|
|
|
|
{
|
|
|
|
return QVariant();
|
|
|
|
}
|
2016-04-07 02:39:30 +05:30
|
|
|
|
2018-07-15 18:21:05 +05:30
|
|
|
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();
|
2016-04-07 02:39:30 +05:30
|
|
|
}
|
2017-03-11 06:09:45 +05:30
|
|
|
int Index::rowCount(const QModelIndex &parent) const
|
2016-04-07 02:39:30 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
return m_lists.size();
|
2016-04-07 02:39:30 +05:30
|
|
|
}
|
2017-03-11 06:09:45 +05:30
|
|
|
int Index::columnCount(const QModelIndex &parent) const
|
2016-04-07 02:39:30 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
return 1;
|
2016-04-07 02:39:30 +05:30
|
|
|
}
|
2017-03-11 06:09:45 +05:30
|
|
|
QVariant Index::headerData(int section, Qt::Orientation orientation, int role) const
|
2016-04-07 02:39:30 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
if (orientation == Qt::Horizontal && role == Qt::DisplayRole && section == 0)
|
|
|
|
{
|
|
|
|
return tr("Name");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return QVariant();
|
|
|
|
}
|
2016-04-07 02:39:30 +05:30
|
|
|
}
|
|
|
|
|
2017-03-11 06:09:45 +05:30
|
|
|
bool Index::hasUid(const QString &uid) const
|
2016-04-07 02:39:30 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
return m_uids.contains(uid);
|
2016-04-07 02:39:30 +05:30
|
|
|
}
|
2017-03-17 06:18:54 +05:30
|
|
|
|
|
|
|
VersionListPtr Index::get(const QString &uid)
|
2016-04-07 02:39:30 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
VersionListPtr out = m_uids.value(uid, nullptr);
|
|
|
|
if(!out)
|
|
|
|
{
|
|
|
|
out = std::make_shared<VersionList>(uid);
|
|
|
|
m_uids[uid] = out;
|
|
|
|
}
|
|
|
|
return out;
|
2016-04-07 02:39:30 +05:30
|
|
|
}
|
2017-03-17 06:18:54 +05:30
|
|
|
|
|
|
|
VersionPtr Index::get(const QString &uid, const QString &version)
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
auto list = get(uid);
|
|
|
|
return list->getVersion(version);
|
2017-03-17 06:18:54 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void Index::parse(const QJsonObject& obj)
|
2016-04-07 02:39:30 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
parseIndex(obj, this);
|
2016-04-07 02:39:30 +05:30
|
|
|
}
|
|
|
|
|
2017-12-14 06:52:20 +05:30
|
|
|
void Index::merge(const std::shared_ptr<Index> &other)
|
2016-04-07 02:39:30 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
const QVector<VersionListPtr> lists = std::dynamic_pointer_cast<Index>(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()]->mergeFromIndex(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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-04-07 02:39:30 +05:30
|
|
|
}
|
|
|
|
|
2017-03-11 06:09:45 +05:30
|
|
|
void Index::connectVersionList(const int row, const VersionListPtr &list)
|
2016-04-07 02:39:30 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
connect(list.get(), &VersionList::nameChanged, this, [this, row]()
|
|
|
|
{
|
|
|
|
emit dataChanged(index(row), index(row), QVector<int>() << Qt::DisplayRole);
|
|
|
|
});
|
2016-04-07 02:39:30 +05:30
|
|
|
}
|
2017-03-11 06:09:45 +05:30
|
|
|
}
|