From 45c186bba891e7c03b559e8b07e00743e38efead Mon Sep 17 00:00:00 2001 From: Ivan Savenko Date: Thu, 30 Jan 2014 12:35:17 +0000 Subject: [PATCH] some code cleanup in launcher, tweaked appearence of mod list --- launcher/modManager/cmodlist.cpp | 15 +++ launcher/modManager/cmodlist.h | 2 + launcher/modManager/cmodlistmodel_moc.cpp | 115 +++++++++++----------- launcher/modManager/cmodlistmodel_moc.h | 7 +- launcher/modManager/cmodlistview_moc.cpp | 25 +---- 5 files changed, 85 insertions(+), 79 deletions(-) diff --git a/launcher/modManager/cmodlist.cpp b/launcher/modManager/cmodlist.cpp index fd598069a..838fa034b 100644 --- a/launcher/modManager/cmodlist.cpp +++ b/launcher/modManager/cmodlist.cpp @@ -28,6 +28,21 @@ bool CModEntry::compareVersions(QString lesser, QString greater) return false; } +QString CModEntry::sizeToString(double size) +{ + static const QString sizes[] = + { + /*"%1 B", */"%1 KiB", "%1 MiB", "%1 GiB", "%1 TiB" + }; + size_t index = 0; + while (size > 1024 && index < 4) + { + size /= 1024; + index++; + } + return sizes[index].arg(QString::number(size, 'f', 1)); +} + CModEntry::CModEntry(QVariantMap repository, QVariantMap localData, QVariantMap modSettings, QString modname): repository(repository), localData(localData), diff --git a/launcher/modManager/cmodlist.h b/launcher/modManager/cmodlist.h index 9739eb40a..b468bb886 100644 --- a/launcher/modManager/cmodlist.h +++ b/launcher/modManager/cmodlist.h @@ -49,6 +49,8 @@ public: // returns true if less < greater comparing versions section by section static bool compareVersions(QString lesser, QString greater); + + static QString sizeToString(double size); }; class CModList diff --git a/launcher/modManager/cmodlistmodel_moc.cpp b/launcher/modManager/cmodlistmodel_moc.cpp index 858e2b537..8a2cfdfb0 100644 --- a/launcher/modManager/cmodlistmodel_moc.cpp +++ b/launcher/modManager/cmodlistmodel_moc.cpp @@ -23,7 +23,7 @@ namespace ModFields "Type", "Name", "Version", - "Size (KB)", + "Size", "Author" }; } @@ -47,42 +47,70 @@ QString CModListModel::modIndexToName(int index) const return indexToName[index]; } +QVariant CModListModel::getValue(const CModEntry &mod, int field) const +{ + switch(field) + { + case ModFields::STATUS_ENABLED: + return mod.getModStatus() & (ModStatus::ENABLED | ModStatus::INSTALLED); + + case ModFields::STATUS_UPDATE: + return mod.getModStatus() & (ModStatus::UPDATEABLE | ModStatus::INSTALLED); + + default: + return mod.getValue(ModFields::names[field]); + } +} + +QVariant CModListModel::getText(const CModEntry & mod, int field) const +{ + switch(field) + { + case ModFields::STATUS_ENABLED: + case ModFields::STATUS_UPDATE: + return ""; + case ModFields::SIZE: + return CModEntry::sizeToString(getValue(mod, field).toDouble()); + default: + return getValue(mod, field); + } +} + +QVariant CModListModel::getIcon(const CModEntry & mod, int field) const +{ + if (field == ModFields::STATUS_ENABLED && mod.isEnabled()) + return QIcon(ModStatus::iconEnabled); + if (field == ModFields::STATUS_ENABLED && mod.isDisabled()) + return QIcon(ModStatus::iconDisabled); + + if (field == ModFields::STATUS_UPDATE && mod.isUpdateable()) + return QIcon(ModStatus::iconUpdate); + if (field == ModFields::STATUS_UPDATE && !mod.isInstalled()) + return QIcon(ModStatus::iconDownload); + + return QVariant(); +} + +QVariant CModListModel::getTextAlign(int field) const +{ + if (field == ModFields::SIZE) + return QVariant(Qt::AlignRight | Qt::AlignVCenter); + else + return QVariant(Qt::AlignLeft | Qt::AlignVCenter); +} + QVariant CModListModel::data(const QModelIndex &index, int role) const { if (index.isValid()) { auto mod = getMod(modIndexToName(index.row())); - if (index.column() == ModFields::STATUS_ENABLED) + switch (role) { - if (role == Qt::DecorationRole) - { - if (mod.isEnabled()) - return QIcon(ModStatus::iconEnabled); - - if (mod.isDisabled()) - return QIcon(ModStatus::iconDisabled); - - return QVariant(); - } - } - if (index.column() == ModFields::STATUS_UPDATE) - { - if (role == Qt::DecorationRole) - { - if (mod.isUpdateable()) - return QIcon(ModStatus::iconUpdate); - - if (!mod.isInstalled()) - return QIcon(ModStatus::iconDownload); - - return QVariant(); - } - } - - if (role == Qt::DisplayRole) - { - return mod.getValue(ModFields::names[index.column()]); + case Qt::DecorationRole: return getIcon(mod, index.column()); + case Qt::DisplayRole: return getText(mod, index.column()); + case Qt::UserRole: return getValue(mod, index.column()); + case Qt::TextAlignmentRole: return getTextAlign(index.column()); } } return QVariant(); @@ -165,32 +193,6 @@ bool CModFilterModel::filterAcceptsRow(int source_row, const QModelIndex &source return false; } -bool CModFilterModel::lessThan(const QModelIndex &left, const QModelIndex &right) const -{ - assert(left.column() == right.column()); - - CModEntry modLeft = base->getMod(base->modIndexToName(left.row())); - CModEntry modRight = base->getMod(base->modIndexToName(left.row())); - - switch (left.column()) - { - case ModFields::STATUS_ENABLED: - { - return (modLeft.getModStatus() & (ModStatus::ENABLED | ModStatus::INSTALLED)) - < (modRight.getModStatus() & (ModStatus::ENABLED | ModStatus::INSTALLED)); - } - case ModFields::STATUS_UPDATE: - { - return (modLeft.getModStatus() & (ModStatus::UPDATEABLE | ModStatus::INSTALLED)) - < (modRight.getModStatus() & (ModStatus::UPDATEABLE | ModStatus::INSTALLED)); - } - default: - { - return QSortFilterProxyModel::lessThan(left, right); - } - } -} - CModFilterModel::CModFilterModel(CModListModel * model, QObject * parent): QSortFilterProxyModel(parent), base(model), @@ -198,4 +200,5 @@ CModFilterModel::CModFilterModel(CModListModel * model, QObject * parent): filterMask(ModStatus::MASK_NONE) { setSourceModel(model); + setSortRole(Qt::UserRole); } diff --git a/launcher/modManager/cmodlistmodel_moc.h b/launcher/modManager/cmodlistmodel_moc.h index bb1e0ab74..53331cb3c 100644 --- a/launcher/modManager/cmodlistmodel_moc.h +++ b/launcher/modManager/cmodlistmodel_moc.h @@ -27,6 +27,11 @@ class CModListModel : public QAbstractTableModel, public CModList QVector indexToName; void endResetModel(); + + QVariant getTextAlign(int field) const; + QVariant getValue(const CModEntry & mod, int field) const; + QVariant getText(const CModEntry & mod, int field) const; + QVariant getIcon(const CModEntry & mod, int field) const; public: /// CModListContainer overrides void resetRepositories(); @@ -60,8 +65,6 @@ class CModFilterModel : public QSortFilterProxyModel bool filterMatches(int modIndex) const; bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const; - - bool lessThan(const QModelIndex &left, const QModelIndex &right) const; public: void setTypeFilter(int filteredType, int filterMask); diff --git a/launcher/modManager/cmodlistview_moc.cpp b/launcher/modManager/cmodlistview_moc.cpp index 432f8c85e..1206dbe62 100644 --- a/launcher/modManager/cmodlistview_moc.cpp +++ b/launcher/modManager/cmodlistview_moc.cpp @@ -33,8 +33,9 @@ void CModListView::setupModsView() ui->allModsView->sortByColumn(ModFields::TYPE, Qt::AscendingOrder); ui->allModsView->setColumnWidth(ModFields::STATUS_ENABLED, 30); ui->allModsView->setColumnWidth(ModFields::STATUS_UPDATE, 30); - ui->allModsView->setColumnWidth(ModFields::NAME, 120); - ui->allModsView->setColumnWidth(ModFields::SIZE, 60); + ui->allModsView->setColumnWidth(ModFields::TYPE, 80); + ui->allModsView->setColumnWidth(ModFields::NAME, 180); + ui->allModsView->setColumnWidth(ModFields::SIZE, 80); ui->allModsView->setColumnWidth(ModFields::VERSION, 60); connect( ui->allModsView->selectionModel(), SIGNAL( currentRowChanged( const QModelIndex &, const QModelIndex & )), @@ -119,24 +120,6 @@ static QString replaceIfNotEmpty(QVariant value, QString pattern) return ""; } -static QVariant sizeToString(QVariant value) -{ - if (value.canConvert()) - { - static QString symbols = "kMGTPE"; - auto number = value.toUInt(); - size_t i=0; - - while (number >= 1000) - { - number /= 1000; - i++; - } - return QVariant(QString("%1 %2B").arg(number).arg(symbols.at(i))); - } - return value; -} - static QString replaceIfNotEmpty(QStringList value, QString pattern) { if (!value.empty()) @@ -158,7 +141,7 @@ QString CModListView::genModInfoText(CModEntry &mod) result += replaceIfNotEmpty(mod.getValue("name"), lineTemplate.arg("Mod name")); result += replaceIfNotEmpty(mod.getValue("installedVersion"), lineTemplate.arg("Installed version")); result += replaceIfNotEmpty(mod.getValue("latestVersion"), lineTemplate.arg("Latest version")); - result += replaceIfNotEmpty(sizeToString(mod.getValue("size")), lineTemplate.arg("Download size")); + result += replaceIfNotEmpty(CModEntry::sizeToString(mod.getValue("size").toDouble()), lineTemplate.arg("Download size")); result += replaceIfNotEmpty(mod.getValue("author"), lineTemplate.arg("Authors")); result += replaceIfNotEmpty(mod.getValue("contact"), urlTemplate.arg("Home")); result += replaceIfNotEmpty(mod.getValue("depends"), lineTemplate.arg("Required mods"));