diff options
Diffstat (limited to 'src/gui/entry/EntryModel.cpp')
-rw-r--r-- | src/gui/entry/EntryModel.cpp | 169 |
1 files changed, 131 insertions, 38 deletions
diff --git a/src/gui/entry/EntryModel.cpp b/src/gui/entry/EntryModel.cpp index bf7eca0c7..0b766c854 100644 --- a/src/gui/entry/EntryModel.cpp +++ b/src/gui/entry/EntryModel.cpp @@ -19,7 +19,6 @@ #include <QDateTime> #include <QFont> -#include <QFontMetrics> #include <QMimeData> #include <QPainter> #include <QPalette> @@ -30,8 +29,9 @@ #include "core/Global.h" #include "core/Group.h" #include "core/Metadata.h" +#include "core/Resources.h" #ifdef Q_OS_MACOS -#include "gui/macutils/MacUtils.h" +#include "gui/osutils/macutils/MacUtils.h" #endif EntryModel::EntryModel(QObject* parent) @@ -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 @@ -174,7 +174,7 @@ QVariant EntryModel::data(const QModelIndex& index, int role) const if (attr->isReference(EntryAttributes::PasswordKey)) { result.prepend(tr("Ref: ", "Reference abbreviation")); } - if (entry->password().isEmpty() && config()->get("security/passwordemptynodots").toBool()) { + if (entry->password().isEmpty() && !config()->get(Config::Security_PasswordEmptyPlaceholder).toBool()) { result = ""; } return result; @@ -185,10 +185,16 @@ QVariant EntryModel::data(const QModelIndex& index, int role) const } return result; case Notes: - // Display only first line of notes in simplified format - result = entry->notes().section("\n", 0, 0).simplified(); - if (attr->isReference(EntryAttributes::NotesKey)) { - result.prepend(tr("Ref: ", "Reference abbreviation")); + if (!entry->notes().isEmpty()) { + if (config()->get(Config::Security_HideNotes).toBool()) { + result = EntryModel::HiddenContentDisplay; + } else { + // Display only first line of notes in simplified format if not hidden + result = entry->notes().section("\n", 0, 0).simplified(); + } + if (attr->isReference(EntryAttributes::NotesKey)) { + result.prepend(tr("Ref: ", "Reference abbreviation")); + } } return result; case Expires: @@ -218,10 +224,23 @@ QVariant EntryModel::data(const QModelIndex& index, int role) const } return result; } - case Totp: - result = entry->hasTotp() ? tr("Yes") : ""; + 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 = QStringLiteral("%1 %2").arg(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()) { case Username: @@ -240,7 +259,11 @@ QVariant EntryModel::data(const QModelIndex& index, int role) const case Paperclip: // Display entries with attachments above those without when // sorting ascendingly (and vice versa when sorting descendingly) - return entry->attachments()->isEmpty() ? 1 : 0; + 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 @@ -250,17 +273,19 @@ QVariant EntryModel::data(const QModelIndex& index, int role) const switch (index.column()) { case ParentGroup: if (entry->group()) { - return entry->group()->iconScaledPixmap(); + return entry->group()->iconPixmap(); } break; case Title: - if (entry->isExpired()) { - return databaseIcons()->iconPixmap(DatabaseIcons::ExpiredIconIndex); - } - return entry->iconScaledPixmap(); + return entry->iconPixmap(); case Paperclip: if (!entry->attachments()->isEmpty()) { - return m_paperClipPixmap; + return resources()->icon("paperclip"); + } + break; + case Totp: + if (entry->hasTotp()) { + return resources()->icon("chronometer"); } break; } @@ -271,20 +296,23 @@ QVariant EntryModel::data(const QModelIndex& index, int role) const } return font; } else if (role == Qt::ForegroundRole) { + QColor foregroundColor; + foregroundColor.setNamedColor(entry->foregroundColor()); if (entry->hasReferences()) { QPalette p; -#ifdef Q_OS_MACOS - if (macUtils()->isDarkMode()) { - return QVariant(p.color(QPalette::Inactive, QPalette::Dark)); - } -#endif - return QVariant(p.color(QPalette::Active, QPalette::Mid)); - } else if (entry->foregroundColor().isValid()) { - return QVariant(entry->foregroundColor()); + foregroundColor = p.color(QPalette::Current, QPalette::Text); + int lightness = + qMin(255, qMax(0, foregroundColor.lightness() + (foregroundColor.lightness() < 110 ? 85 : -51))); + foregroundColor.setHsl(foregroundColor.hue(), foregroundColor.saturation(), lightness); + return QVariant(foregroundColor); + } else if (foregroundColor.isValid()) { + return QVariant(foregroundColor); } } else if (role == Qt::BackgroundRole) { - if (entry->backgroundColor().isValid()) { - return QVariant(entry->backgroundColor()); + QColor backgroundColor; + backgroundColor.setNamedColor(entry->backgroundColor()); + if (backgroundColor.isValid()) { + return QVariant(backgroundColor); } } else if (role == Qt::TextAlignmentRole) { if (index.column() == Paperclip) { @@ -323,16 +351,51 @@ QVariant EntryModel::headerData(int section, Qt::Orientation orientation, int ro return tr("Accessed"); case Attachments: return tr("Attachments"); - case Totp: - return tr("TOTP"); + case Size: + return tr("Size"); } + } else if (role == Qt::DecorationRole) { - if (section == Paperclip) { - return m_paperClipPixmap; + switch (section) { + case Paperclip: + return resources()->icon("paperclip"); + case Totp: + return resources()->icon("chronometer"); + } + } else if (role == Qt::ToolTipRole) { + switch (section) { + case ParentGroup: + return tr("Group name"); + case Title: + return tr("Entry title"); + case Username: + return tr("Username"); + case Password: + return tr("Password"); + case Url: + return tr("URL"); + case Notes: + return tr("Entry notes"); + case Expires: + return tr("Entry expires at"); + case Created: + return tr("Creation date"); + case Modified: + return tr("Last modification date"); + case Accessed: + 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: + return tr("Has TOTP one-time password"); } } - return QVariant(); + return {}; } Qt::DropActions EntryModel::supportedDropActions() const @@ -433,10 +496,41 @@ void EntryModel::entryRemoved() if (m_group) { m_entries = m_group->entries(); } - endRemoveRows(); } +void EntryModel::entryAboutToMoveUp(int row) +{ + beginMoveRows(QModelIndex(), row, row, QModelIndex(), row - 1); + if (m_group) { + m_entries.move(row, row - 1); + } +} + +void EntryModel::entryMovedUp() +{ + if (m_group) { + m_entries = m_group->entries(); + } + endMoveRows(); +} + +void EntryModel::entryAboutToMoveDown(int row) +{ + beginMoveRows(QModelIndex(), row, row, QModelIndex(), row + 2); + if (m_group) { + m_entries.move(row, row + 1); + } +} + +void EntryModel::entryMovedDown() +{ + if (m_group) { + m_entries = m_group->entries(); + } + endMoveRows(); +} + void EntryModel::entryDataChanged(Entry* entry) { int row = m_entries.indexOf(entry); @@ -460,6 +554,10 @@ void EntryModel::makeConnections(const Group* group) connect(group, SIGNAL(entryAdded(Entry*)), SLOT(entryAdded(Entry*))); connect(group, SIGNAL(entryAboutToRemove(Entry*)), SLOT(entryAboutToRemove(Entry*))); connect(group, SIGNAL(entryRemoved(Entry*)), SLOT(entryRemoved())); + connect(group, SIGNAL(entryAboutToMoveUp(int)), SLOT(entryAboutToMoveUp(int))); + connect(group, SIGNAL(entryMovedUp()), SLOT(entryMovedUp())); + connect(group, SIGNAL(entryAboutToMoveDown(int)), SLOT(entryAboutToMoveDown(int))); + connect(group, SIGNAL(entryMovedDown()), SLOT(entryMovedDown())); connect(group, SIGNAL(entryDataChanged(Entry*)), SLOT(entryDataChanged(Entry*))); } @@ -498,8 +596,3 @@ void EntryModel::setPasswordsHidden(bool hide) emit dataChanged(index(0, 0), index(rowCount() - 1, columnCount() - 1)); emit passwordsHiddenChanged(); } - -void EntryModel::setPaperClipPixmap(const QPixmap& paperclip) -{ - m_paperClipPixmap = paperclip; -} |