1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-24 22:14:36 +02:00

some code cleanup in launcher, tweaked appearence of mod list

This commit is contained in:
Ivan Savenko 2014-01-30 12:35:17 +00:00
parent 3779a54ddd
commit 45c186bba8
5 changed files with 85 additions and 79 deletions

View File

@ -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),

View File

@ -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

View File

@ -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);
}

View File

@ -27,6 +27,11 @@ class CModListModel : public QAbstractTableModel, public CModList
QVector<QString> 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);

View File

@ -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<QString>())
{
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"));