Add "Size" column

- Sizes are displayed in B, KiB, MiB, and GiB with 2 significant
  digits after the decimal
- Column is hidden by default
- TestEntryModel updated for testing proxy model
This commit is contained in:
Ojas Anand 2020-04-17 09:51:36 -04:00 committed by Janek Bevendorff
parent d9214db404
commit 1b18c5d51d
6 changed files with 48 additions and 14 deletions

View file

@ -129,7 +129,7 @@ int EntryModel::columnCount(const QModelIndex& parent) const
return 0;
}
return 13;
return 14;
}
QVariant EntryModel::data(const QModelIndex& index, int role) const
@ -222,6 +222,22 @@ QVariant EntryModel::data(const QModelIndex& index, int role) const
}
return result;
}
case Size: {
const int unitsSize = 4;
QString units[unitsSize] = {"B", "KiB", "MiB", "GiB"};
float resultInt = entry->size();
for (int i = 0; i < unitsSize; i++) {
if (resultInt < 1024 || i == unitsSize - 1) {
resultInt = qRound(resultInt * 100) / 100.0;
result = QString::number(resultInt) + " " + units[i];
break;
}
resultInt /= 1024.0;
}
return result;
}
}
} else if (role == Qt::UserRole) { // Qt::UserRole is used as sort role, see EntryView::EntryView()
switch (index.column()) {
@ -244,6 +260,8 @@ QVariant EntryModel::data(const QModelIndex& index, int role) const
return !entry->attachments()->isEmpty();
case Totp:
return entry->hasTotp();
case Size:
return entry->size();
default:
// For all other columns, simply use data provided by Qt::Display-
// Role for sorting
@ -334,6 +352,8 @@ QVariant EntryModel::headerData(int section, Qt::Orientation orientation, int ro
return tr("Accessed");
case Attachments:
return tr("Attachments");
case Size:
return tr("Size");
}
} else if (role == Qt::DecorationRole) {
@ -367,6 +387,8 @@ QVariant EntryModel::headerData(int section, Qt::Orientation orientation, int ro
return tr("Last access date");
case Attachments:
return tr("Attached files");
case Size:
return tr("Entry size");
case Paperclip:
return tr("Has attachments");
case Totp:

View file

@ -43,7 +43,8 @@ public:
Accessed = 9,
Paperclip = 10,
Attachments = 11,
Totp = 12
Totp = 12,
Size = 13
};
explicit EntryModel(QObject* parent = nullptr);

View file

@ -72,7 +72,7 @@ EntryView::EntryView(QWidget* parent)
resetViewToDefaults();
// Actions to toggle column visibility, each carrying the corresponding
// colummn index as data
// column index as data
m_columnActions = new QActionGroup(this);
m_columnActions->setExclusive(false);
for (int visualIndex = 1; visualIndex < header()->count(); ++visualIndex) {
@ -451,6 +451,7 @@ void EntryView::resetViewToDefaults()
header()->hideSection(EntryModel::Created);
header()->hideSection(EntryModel::Accessed);
header()->hideSection(EntryModel::Attachments);
header()->hideSection(EntryModel::Size);
// Reset column order to logical indices
for (int i = 0; i < header()->count(); ++i) {