2015-04-28 12:31:37 +05:30
|
|
|
#include "VersionProxyModel.h"
|
|
|
|
#include "MultiMC.h"
|
|
|
|
#include <QSortFilterProxyModel>
|
|
|
|
#include <QPixmapCache>
|
2015-10-05 05:17:27 +05:30
|
|
|
#include <Version.h>
|
2018-04-08 23:19:02 +05:30
|
|
|
#include <meta/VersionList.h>
|
2015-04-28 12:31:37 +05:30
|
|
|
|
|
|
|
class VersionFilterModel : public QSortFilterProxyModel
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
Q_OBJECT
|
2015-04-28 12:31:37 +05:30
|
|
|
public:
|
2018-07-15 18:21:05 +05:30
|
|
|
VersionFilterModel(VersionProxyModel *parent) : QSortFilterProxyModel(parent)
|
|
|
|
{
|
|
|
|
m_parent = parent;
|
|
|
|
setSortRole(BaseVersionList::SortRole);
|
|
|
|
sort(0, Qt::DescendingOrder);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
|
|
|
|
{
|
|
|
|
const auto &filters = m_parent->filters();
|
|
|
|
for (auto it = filters.begin(); it != filters.end(); ++it)
|
|
|
|
{
|
|
|
|
auto idx = sourceModel()->index(source_row, 0, source_parent);
|
|
|
|
auto data = sourceModel()->data(idx, it.key());
|
|
|
|
auto match = data.toString();
|
|
|
|
if(!it.value()->accepts(match))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void filterChanged()
|
|
|
|
{
|
|
|
|
invalidateFilter();
|
|
|
|
}
|
2015-04-28 12:31:37 +05:30
|
|
|
private:
|
2018-07-15 18:21:05 +05:30
|
|
|
VersionProxyModel *m_parent;
|
2015-04-28 12:31:37 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
VersionProxyModel::VersionProxyModel(QObject *parent) : QAbstractProxyModel(parent)
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
filterModel = new VersionFilterModel(this);
|
|
|
|
connect(filterModel, &QAbstractItemModel::dataChanged, this, &VersionProxyModel::sourceDataChanged);
|
|
|
|
connect(filterModel, &QAbstractItemModel::rowsAboutToBeInserted, this, &VersionProxyModel::sourceRowsAboutToBeInserted);
|
|
|
|
connect(filterModel, &QAbstractItemModel::rowsInserted, this, &VersionProxyModel::sourceRowsInserted);
|
|
|
|
connect(filterModel, &QAbstractItemModel::rowsAboutToBeRemoved, this, &VersionProxyModel::sourceRowsAboutToBeRemoved);
|
|
|
|
connect(filterModel, &QAbstractItemModel::rowsRemoved, this, &VersionProxyModel::sourceRowsRemoved);
|
|
|
|
// FIXME: implement when needed
|
|
|
|
/*
|
|
|
|
connect(replacing, &QAbstractItemModel::rowsAboutToBeMoved, this, &VersionProxyModel::sourceRowsAboutToBeMoved);
|
|
|
|
connect(replacing, &QAbstractItemModel::rowsMoved, this, &VersionProxyModel::sourceRowsMoved);
|
|
|
|
connect(replacing, &QAbstractItemModel::layoutAboutToBeChanged, this, &VersionProxyModel::sourceLayoutAboutToBeChanged);
|
|
|
|
connect(replacing, &QAbstractItemModel::layoutChanged, this, &VersionProxyModel::sourceLayoutChanged);
|
|
|
|
*/
|
|
|
|
connect(filterModel, &QAbstractItemModel::modelAboutToBeReset, this, &VersionProxyModel::sourceAboutToBeReset);
|
|
|
|
connect(filterModel, &QAbstractItemModel::modelReset, this, &VersionProxyModel::sourceReset);
|
|
|
|
|
|
|
|
QAbstractProxyModel::setSourceModel(filterModel);
|
2015-04-28 12:31:37 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
QVariant VersionProxyModel::headerData(int section, Qt::Orientation orientation, int role) const
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
if(section < 0 || section >= m_columns.size())
|
|
|
|
return QVariant();
|
|
|
|
if(orientation != Qt::Horizontal)
|
|
|
|
return QVariant();
|
|
|
|
auto column = m_columns[section];
|
|
|
|
if(role == Qt::DisplayRole)
|
|
|
|
{
|
|
|
|
switch(column)
|
|
|
|
{
|
|
|
|
case Name:
|
|
|
|
return tr("Version");
|
|
|
|
case ParentVersion:
|
|
|
|
return tr("Minecraft"); //FIXME: this should come from metadata
|
|
|
|
case Branch:
|
|
|
|
return tr("Branch");
|
|
|
|
case Type:
|
|
|
|
return tr("Type");
|
|
|
|
case Architecture:
|
|
|
|
return tr("Architecture");
|
|
|
|
case Path:
|
|
|
|
return tr("Path");
|
|
|
|
case Time:
|
|
|
|
return tr("Released");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(role == Qt::ToolTipRole)
|
|
|
|
{
|
|
|
|
switch(column)
|
|
|
|
{
|
|
|
|
case Name:
|
|
|
|
return tr("The name of the version.");
|
|
|
|
case ParentVersion:
|
|
|
|
return tr("Minecraft version"); //FIXME: this should come from metadata
|
|
|
|
case Branch:
|
|
|
|
return tr("The version's branch");
|
|
|
|
case Type:
|
|
|
|
return tr("The version's type");
|
|
|
|
case Architecture:
|
|
|
|
return tr("CPU Architecture");
|
|
|
|
case Path:
|
|
|
|
return tr("Filesystem path to this version");
|
|
|
|
case Time:
|
|
|
|
return tr("Release date of this version");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return QVariant();
|
2015-04-28 12:31:37 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
QVariant VersionProxyModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
if(!index.isValid())
|
|
|
|
{
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
auto column = m_columns[index.column()];
|
|
|
|
auto parentIndex = mapToSource(index);
|
|
|
|
switch(role)
|
|
|
|
{
|
|
|
|
case Qt::DisplayRole:
|
|
|
|
{
|
|
|
|
switch(column)
|
|
|
|
{
|
|
|
|
case Name:
|
2018-11-10 01:14:39 +05:30
|
|
|
{
|
|
|
|
QString version = sourceModel()->data(parentIndex, BaseVersionList::VersionRole).toString();
|
|
|
|
if(version == m_currentVersion)
|
|
|
|
{
|
2018-11-12 06:27:26 +05:30
|
|
|
return tr("%1 (installed)").arg(version);
|
2018-11-10 01:14:39 +05:30
|
|
|
}
|
|
|
|
return version;
|
|
|
|
}
|
2018-07-15 18:21:05 +05:30
|
|
|
case ParentVersion:
|
|
|
|
return sourceModel()->data(parentIndex, BaseVersionList::ParentVersionRole);
|
|
|
|
case Branch:
|
|
|
|
return sourceModel()->data(parentIndex, BaseVersionList::BranchRole);
|
|
|
|
case Type:
|
|
|
|
return sourceModel()->data(parentIndex, BaseVersionList::TypeRole);
|
|
|
|
case Architecture:
|
|
|
|
return sourceModel()->data(parentIndex, BaseVersionList::ArchitectureRole);
|
|
|
|
case Path:
|
|
|
|
return sourceModel()->data(parentIndex, BaseVersionList::PathRole);
|
|
|
|
case Time:
|
|
|
|
return sourceModel()->data(parentIndex, Meta::VersionList::TimeRole).toDate();
|
|
|
|
default:
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case Qt::ToolTipRole:
|
|
|
|
{
|
|
|
|
switch(column)
|
|
|
|
{
|
|
|
|
case Name:
|
|
|
|
{
|
|
|
|
if(hasRecommended)
|
|
|
|
{
|
|
|
|
auto value = sourceModel()->data(parentIndex, BaseVersionList::RecommendedRole);
|
|
|
|
if(value.toBool())
|
|
|
|
{
|
|
|
|
return tr("Recommended");
|
|
|
|
}
|
|
|
|
else if(hasLatest)
|
|
|
|
{
|
|
|
|
auto value = sourceModel()->data(parentIndex, BaseVersionList::LatestRole);
|
|
|
|
if(value.toBool())
|
|
|
|
{
|
|
|
|
return tr("Latest");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(index.row() == 0)
|
|
|
|
{
|
|
|
|
return tr("Latest");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
return sourceModel()->data(parentIndex, BaseVersionList::VersionIdRole);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case Qt::DecorationRole:
|
|
|
|
{
|
|
|
|
switch(column)
|
|
|
|
{
|
|
|
|
case Name:
|
|
|
|
{
|
|
|
|
if(hasRecommended)
|
|
|
|
{
|
|
|
|
auto value = sourceModel()->data(parentIndex, BaseVersionList::RecommendedRole);
|
|
|
|
if(value.toBool())
|
|
|
|
{
|
|
|
|
return MMC->getThemedIcon("star");
|
|
|
|
}
|
|
|
|
else if(hasLatest)
|
|
|
|
{
|
|
|
|
auto value = sourceModel()->data(parentIndex, BaseVersionList::LatestRole);
|
|
|
|
if(value.toBool())
|
|
|
|
{
|
|
|
|
return MMC->getThemedIcon("bug");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(index.row() == 0)
|
|
|
|
{
|
|
|
|
return MMC->getThemedIcon("bug");
|
|
|
|
}
|
|
|
|
auto pixmap = QPixmapCache::find("placeholder");
|
|
|
|
if(!pixmap)
|
|
|
|
{
|
|
|
|
QPixmap px(16,16);
|
|
|
|
px.fill(Qt::transparent);
|
|
|
|
QPixmapCache::insert("placeholder", px);
|
|
|
|
return px;
|
|
|
|
}
|
|
|
|
return *pixmap;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
if(roles.contains((BaseVersionList::ModelRoles)role))
|
|
|
|
{
|
|
|
|
return sourceModel()->data(parentIndex, role);
|
|
|
|
}
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
}
|
2018-06-29 02:48:45 +05:30
|
|
|
}
|
2015-04-28 12:31:37 +05:30
|
|
|
|
|
|
|
QModelIndex VersionProxyModel::parent(const QModelIndex &child) const
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
return QModelIndex();
|
2015-04-28 12:31:37 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
QModelIndex VersionProxyModel::mapFromSource(const QModelIndex &sourceIndex) const
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
if(sourceIndex.isValid())
|
|
|
|
{
|
|
|
|
return index(sourceIndex.row(), 0);
|
|
|
|
}
|
|
|
|
return QModelIndex();
|
2015-04-28 12:31:37 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
QModelIndex VersionProxyModel::mapToSource(const QModelIndex &proxyIndex) const
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
if(proxyIndex.isValid())
|
|
|
|
{
|
|
|
|
return sourceModel()->index(proxyIndex.row(), 0);
|
|
|
|
}
|
|
|
|
return QModelIndex();
|
2015-04-28 12:31:37 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
QModelIndex VersionProxyModel::index(int row, int column, const QModelIndex &parent) const
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
// no trees here... shoo
|
|
|
|
if(parent.isValid())
|
|
|
|
{
|
|
|
|
return QModelIndex();
|
|
|
|
}
|
|
|
|
if(row < 0 || row >= sourceModel()->rowCount())
|
|
|
|
return QModelIndex();
|
|
|
|
if(column < 0 || column >= columnCount())
|
|
|
|
return QModelIndex();
|
|
|
|
return QAbstractItemModel::createIndex(row, column);
|
2015-04-28 12:31:37 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
int VersionProxyModel::columnCount(const QModelIndex &parent) const
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
return m_columns.size();
|
2015-04-28 12:31:37 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
int VersionProxyModel::rowCount(const QModelIndex &parent) const
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
if(sourceModel())
|
|
|
|
{
|
|
|
|
return sourceModel()->rowCount();
|
|
|
|
}
|
|
|
|
return 0;
|
2015-04-28 12:31:37 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void VersionProxyModel::sourceDataChanged(const QModelIndex &source_top_left,
|
2018-07-15 18:21:05 +05:30
|
|
|
const QModelIndex &source_bottom_right)
|
2015-04-28 12:31:37 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
if(source_top_left.parent() != source_bottom_right.parent())
|
|
|
|
return;
|
2015-04-28 12:31:37 +05:30
|
|
|
|
2018-07-15 18:21:05 +05:30
|
|
|
// whole row is getting changed
|
|
|
|
auto topLeft = createIndex(source_top_left.row(), 0);
|
|
|
|
auto bottomRight = createIndex(source_bottom_right.row(), columnCount() - 1);
|
|
|
|
emit dataChanged(topLeft, bottomRight);
|
2015-04-28 12:31:37 +05:30
|
|
|
}
|
|
|
|
|
2015-09-26 07:34:09 +05:30
|
|
|
void VersionProxyModel::setSourceModel(QAbstractItemModel *replacingRaw)
|
2015-04-28 12:31:37 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
auto replacing = dynamic_cast<BaseVersionList *>(replacingRaw);
|
|
|
|
beginResetModel();
|
|
|
|
|
|
|
|
m_columns.clear();
|
|
|
|
if(!replacing)
|
|
|
|
{
|
|
|
|
roles.clear();
|
|
|
|
filterModel->setSourceModel(replacing);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
roles = replacing->providesRoles();
|
|
|
|
if(roles.contains(BaseVersionList::VersionRole))
|
|
|
|
{
|
|
|
|
m_columns.push_back(Name);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
if(roles.contains(BaseVersionList::ParentVersionRole))
|
|
|
|
{
|
|
|
|
m_columns.push_back(ParentVersion);
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
if(roles.contains(BaseVersionList::ArchitectureRole))
|
|
|
|
{
|
|
|
|
m_columns.push_back(Architecture);
|
|
|
|
}
|
|
|
|
if(roles.contains(BaseVersionList::PathRole))
|
|
|
|
{
|
|
|
|
m_columns.push_back(Path);
|
|
|
|
}
|
|
|
|
if(roles.contains(Meta::VersionList::TimeRole))
|
|
|
|
{
|
|
|
|
m_columns.push_back(Time);
|
|
|
|
}
|
|
|
|
if(roles.contains(BaseVersionList::BranchRole))
|
|
|
|
{
|
|
|
|
m_columns.push_back(Branch);
|
|
|
|
}
|
|
|
|
if(roles.contains(BaseVersionList::TypeRole))
|
|
|
|
{
|
|
|
|
m_columns.push_back(Type);
|
|
|
|
}
|
|
|
|
if(roles.contains(BaseVersionList::RecommendedRole))
|
|
|
|
{
|
|
|
|
hasRecommended = true;
|
|
|
|
}
|
|
|
|
if(roles.contains(BaseVersionList::LatestRole))
|
|
|
|
{
|
|
|
|
hasLatest = true;
|
|
|
|
}
|
|
|
|
filterModel->setSourceModel(replacing);
|
|
|
|
|
|
|
|
endResetModel();
|
2015-04-28 12:31:37 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
QModelIndex VersionProxyModel::getRecommended() const
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
if(!roles.contains(BaseVersionList::RecommendedRole))
|
|
|
|
{
|
|
|
|
return index(0, 0);
|
|
|
|
}
|
|
|
|
int recommended = 0;
|
|
|
|
for (int i = 0; i < rowCount(); i++)
|
|
|
|
{
|
|
|
|
auto value = sourceModel()->data(mapToSource(index(i, 0)), BaseVersionList::RecommendedRole);
|
|
|
|
if (value.toBool())
|
|
|
|
{
|
|
|
|
recommended = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return index(recommended, 0);
|
2015-04-28 12:31:37 +05:30
|
|
|
}
|
|
|
|
|
2017-12-09 06:00:23 +05:30
|
|
|
QModelIndex VersionProxyModel::getVersion(const QString& version) const
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
int found = -1;
|
|
|
|
for (int i = 0; i < rowCount(); i++)
|
|
|
|
{
|
|
|
|
auto value = sourceModel()->data(mapToSource(index(i, 0)), BaseVersionList::VersionRole);
|
|
|
|
if (value.toString() == version)
|
|
|
|
{
|
|
|
|
found = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(found == -1)
|
|
|
|
{
|
|
|
|
return QModelIndex();
|
|
|
|
}
|
|
|
|
return index(found, 0);
|
2017-12-09 06:00:23 +05:30
|
|
|
}
|
|
|
|
|
2015-04-28 12:31:37 +05:30
|
|
|
void VersionProxyModel::clearFilters()
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
m_filters.clear();
|
|
|
|
filterModel->filterChanged();
|
2015-04-28 12:31:37 +05:30
|
|
|
}
|
|
|
|
|
2018-03-19 07:06:12 +05:30
|
|
|
void VersionProxyModel::setFilter(const BaseVersionList::ModelRoles column, Filter * f)
|
2015-04-28 12:31:37 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
m_filters[column].reset(f);
|
|
|
|
filterModel->filterChanged();
|
2015-04-28 12:31:37 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
const VersionProxyModel::FilterMap &VersionProxyModel::filters() const
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
return m_filters;
|
2015-04-28 12:31:37 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void VersionProxyModel::sourceAboutToBeReset()
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
beginResetModel();
|
2015-04-28 12:31:37 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void VersionProxyModel::sourceReset()
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
endResetModel();
|
2015-04-28 12:31:37 +05:30
|
|
|
}
|
|
|
|
|
2018-03-19 07:06:12 +05:30
|
|
|
void VersionProxyModel::sourceRowsAboutToBeInserted(const QModelIndex& parent, int first, int last)
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
beginInsertRows(parent, first, last);
|
2018-03-19 07:06:12 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void VersionProxyModel::sourceRowsInserted(const QModelIndex& parent, int first, int last)
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
endInsertRows();
|
2018-03-19 07:06:12 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void VersionProxyModel::sourceRowsAboutToBeRemoved(const QModelIndex& parent, int first, int last)
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
beginRemoveRows(parent, first, last);
|
2018-03-19 07:06:12 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void VersionProxyModel::sourceRowsRemoved(const QModelIndex& parent, int first, int last)
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
endRemoveRows();
|
2018-03-19 07:06:12 +05:30
|
|
|
}
|
|
|
|
|
2018-11-10 01:14:39 +05:30
|
|
|
void VersionProxyModel::setCurrentVersion(const QString &version)
|
|
|
|
{
|
|
|
|
m_currentVersion = version;
|
|
|
|
}
|
2018-03-19 07:06:12 +05:30
|
|
|
|
2015-04-28 12:31:37 +05:30
|
|
|
#include "VersionProxyModel.moc"
|