Qt: Improve size column display

This commit is contained in:
Vicki Pfau 2017-01-27 16:56:13 -08:00
parent 46782f16a0
commit 849f80e7a1
2 changed files with 18 additions and 3 deletions

View File

@ -40,16 +40,17 @@ LibraryModel::LibraryModel(const QString& path, QObject* parent)
[](const mLibraryEntry& e) -> QString { [](const mLibraryEntry& e) -> QString {
double size = e.filesize; double size = e.filesize;
QString unit = "B"; QString unit = "B";
if (size > 1024.0) { if (size >= 1024.0) {
size /= 1024.0; size /= 1024.0;
unit = "kiB"; unit = "kiB";
} }
if (size > 1024.0) { if (size >= 1024.0) {
size /= 1024.0; size /= 1024.0;
unit = "MiB"; unit = "MiB";
} }
return QString("%0 %1").arg(size, 0, 'f', 1).arg(unit); return QString("%0 %1").arg(size, 0, 'f', 1).arg(unit);
} },
Qt::AlignRight
}; };
s_columns["platform"] = { s_columns["platform"] = {
tr("Platform"), tr("Platform"),
@ -174,6 +175,8 @@ QVariant LibraryModel::data(const QModelIndex& index, int role) const {
QFontMetrics fm((QFont())); QFontMetrics fm((QFont()));
return fm.size(Qt::TextSingleLine, m_columns[index.column()].value(entry)); return fm.size(Qt::TextSingleLine, m_columns[index.column()].value(entry));
} }
case Qt::TextAlignmentRole:
return m_columns[index.column()].alignment;
default: default:
return QVariant(); return QVariant();
} }
@ -250,6 +253,15 @@ void LibraryModel::directoryLoaded(const QString& path) {
} }
} }
LibraryModel::LibraryColumn::LibraryColumn() {
}
LibraryModel::LibraryColumn::LibraryColumn(const QString& name, std::function<QString(const mLibraryEntry&)> value, int alignment)
: name(name)
, value(value)
, alignment(alignment)
{
}
LibraryModel::LibraryHandle::LibraryHandle(mLibrary* lib, const QString& p) LibraryModel::LibraryHandle::LibraryHandle(mLibrary* lib, const QString& p)
: library(lib) : library(lib)

View File

@ -58,8 +58,11 @@ private slots:
private: private:
struct LibraryColumn { struct LibraryColumn {
LibraryColumn();
LibraryColumn(const QString&, std::function<QString(const mLibraryEntry&)>, int = Qt::AlignLeft);
QString name; QString name;
std::function<QString(const mLibraryEntry&)> value; std::function<QString(const mLibraryEntry&)> value;
int alignment;
}; };
class LibraryHandle { class LibraryHandle {