Skip to content

Commit

Permalink
Merge branch 'develop' into feat/switch-to-XDG_STATE_HOME-9738
Browse files Browse the repository at this point in the history
  • Loading branch information
jNullj authored Oct 22, 2023
2 parents 74c0f3d + cd7a53a commit bb9700d
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 22 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ desktop.ini
# MSVC Files
CMakeSettings.json
CMakePresets.json
CMakeUserPresets.json
.vs/
out/
out/
4 changes: 4 additions & 0 deletions share/translations/keepassxc_en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3840,6 +3840,10 @@ Error: %1</source>
<source>Has TOTP</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Background Color</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>EntryPreviewWidget</name>
Expand Down
34 changes: 29 additions & 5 deletions src/gui/entry/EntryModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ int EntryModel::columnCount(const QModelIndex& parent) const
return 0;
}

return 15;
return 16;
}

QVariant EntryModel::data(const QModelIndex& index, int role) const
Expand Down Expand Up @@ -230,6 +230,13 @@ QVariant EntryModel::data(const QModelIndex& index, int role) const

return result;
}
case Color:
QColor backgroundColor;
backgroundColor.setNamedColor(entry->backgroundColor());
if (backgroundColor.isValid()) {
result = "";
return result;
}
}
} else if (role == Qt::UserRole) { // Qt::UserRole is used as sort role, see EntryView::EntryView()
switch (index.column()) {
Expand Down Expand Up @@ -314,6 +321,15 @@ QVariant EntryModel::data(const QModelIndex& index, int role) const
}
return font;
} else if (role == Qt::ForegroundRole) {

if (index.column() == Color) {
QColor backgroundColor;
backgroundColor.setNamedColor(entry->backgroundColor());
if (backgroundColor.isValid()) {
return backgroundColor;
}
}

QColor foregroundColor;
foregroundColor.setNamedColor(entry->foregroundColor());
if (entry->hasReferences()) {
Expand All @@ -327,10 +343,12 @@ QVariant EntryModel::data(const QModelIndex& index, int role) const
return QVariant(foregroundColor);
}
} else if (role == Qt::BackgroundRole) {
QColor backgroundColor;
backgroundColor.setNamedColor(entry->backgroundColor());
if (backgroundColor.isValid()) {
return QVariant(backgroundColor);
if (m_backgroundColorVisible) {
QColor backgroundColor;
backgroundColor.setNamedColor(entry->backgroundColor());
if (backgroundColor.isValid()) {
return QVariant(backgroundColor);
}
}
} else if (role == Qt::ToolTipRole) {
if (index.column() == PasswordStrength && !entry->password().isEmpty() && !entry->excludeFromReports()) {
Expand Down Expand Up @@ -414,6 +432,8 @@ QVariant EntryModel::headerData(int section, Qt::Orientation orientation, int ro
return tr("Has attachments");
case Totp:
return tr("Has TOTP");
case Color:
return tr("Background Color");
}
}

Expand Down Expand Up @@ -596,3 +616,7 @@ void EntryModel::makeConnections(const Group* group)
connect(group, SIGNAL(entryMovedDown()), SLOT(entryMovedDown()));
connect(group, SIGNAL(entryDataChanged(Entry*)), SLOT(entryDataChanged(Entry*)));
}
void EntryModel::setBackgroundColorVisible(bool visible)
{
m_backgroundColorVisible = visible;
}
5 changes: 4 additions & 1 deletion src/gui/entry/EntryModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ class EntryModel : public QAbstractTableModel
Attachments = 11,
Totp = 12,
Size = 13,
PasswordStrength = 14
PasswordStrength = 14,
Color = 15
};

explicit EntryModel(QObject* parent = nullptr);
Expand All @@ -67,6 +68,7 @@ class EntryModel : public QAbstractTableModel

void setGroup(Group* group);
void setEntries(const QList<Entry*>& entries);
void setBackgroundColorVisible(bool visible);

private slots:
void entryAboutToAdd(Entry* entry);
Expand All @@ -85,6 +87,7 @@ private slots:
void severConnections();
void makeConnections(const Group* group);

bool m_backgroundColorVisible = true;
Group* m_group;
QList<Entry*> m_entries;
QList<Entry*> m_orgEntries;
Expand Down
16 changes: 15 additions & 1 deletion src/gui/entry/EntryView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,11 +219,12 @@ void EntryView::displaySearch(const QList<Entry*>& entries)
m_model->setEntries(entries);
header()->showSection(EntryModel::ParentGroup);

setFirstEntryActive();

// Reset sort column to 'Group', overrides DatabaseWidgetStateSync
m_sortModel->sort(EntryModel::ParentGroup, Qt::AscendingOrder);
sortByColumn(EntryModel::ParentGroup, Qt::AscendingOrder);

setFirstEntryActive();
m_inSearchMode = true;
}

Expand Down Expand Up @@ -335,6 +336,7 @@ bool EntryView::setViewState(const QByteArray& state)
bool status = header()->restoreState(state);
resetFixedColumns();
m_columnsNeedRelayout = state.isEmpty();
onHeaderChanged();
return status;
}

Expand Down Expand Up @@ -375,6 +377,9 @@ void EntryView::toggleColumnVisibility(QAction* action)
// least one visible column remains, as the table header will disappear
// entirely when all columns are hidden
int columnIndex = action->data().toInt();
if (columnIndex == EntryModel::Color) {
m_model->setBackgroundColorVisible(!action->isChecked());
}
if (action->isChecked()) {
header()->showSection(columnIndex);
if (header()->sectionSize(columnIndex) == 0) {
Expand Down Expand Up @@ -446,6 +451,8 @@ void EntryView::resetFixedColumns()
header()->resizeSection(col, width);
}
}
header()->setMinimumSectionSize(1);
header()->resizeSection(EntryModel::Color, ICON_ONLY_SECTION_SIZE);
}

/**
Expand Down Expand Up @@ -474,6 +481,8 @@ void EntryView::resetViewToDefaults()
header()->hideSection(EntryModel::Attachments);
header()->hideSection(EntryModel::Size);
header()->hideSection(EntryModel::PasswordStrength);
header()->hideSection(EntryModel::Color);
onHeaderChanged();

// Reset column order to logical indices
for (int i = 0; i < header()->count(); ++i) {
Expand Down Expand Up @@ -501,6 +510,11 @@ void EntryView::resetViewToDefaults()
}
}

void EntryView::onHeaderChanged()
{
m_model->setBackgroundColorVisible(isColumnHidden(EntryModel::Color));
}

void EntryView::showEvent(QShowEvent* event)
{
QTreeView::showEvent(event);
Expand Down
1 change: 1 addition & 0 deletions src/gui/entry/EntryView.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ private slots:
private:
void resetFixedColumns();
bool isColumnHidden(int logicalIndex);
void onHeaderChanged();

EntryModel* const m_model;
SortFilterHideProxyModel* const m_sortModel;
Expand Down
18 changes: 4 additions & 14 deletions tests/TestEntryModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -313,15 +313,11 @@ void TestEntryModel::testProxyModel()

modelSource->setGroup(db->rootGroup());

/**
* @author Fonic <https://github.com/fonic>
* Update comparison value of modelProxy->columnCount() to account for
* additional columns 'Password', 'Notes', 'Expires', 'Created', 'Modified',
* 'Accessed', 'Paperclip', 'Attachments', and TOTP
*/
// Test hiding and showing a column
auto columnCount = modelProxy->columnCount();
QSignalSpy spyColumnRemove(modelProxy, SIGNAL(columnsAboutToBeRemoved(QModelIndex, int, int)));
modelProxy->hideColumn(0, true);
QCOMPARE(modelProxy->columnCount(), 14);
QCOMPARE(modelProxy->columnCount(), columnCount - 1);
QVERIFY(!spyColumnRemove.isEmpty());

int oldSpyColumnRemoveSize = spyColumnRemove.size();
Expand All @@ -335,15 +331,9 @@ void TestEntryModel::testProxyModel()
entryList << entry;
modelSource->setEntries(entryList);

/**
* @author Fonic <https://github.com/fonic>
* Update comparison value of modelProxy->columnCount() to account for
* additional columns 'Password', 'Notes', 'Expires', 'Created', 'Modified',
* 'Accessed', 'Paperclip', 'Attachments', and TOTP
*/
QSignalSpy spyColumnInsert(modelProxy, SIGNAL(columnsAboutToBeInserted(QModelIndex, int, int)));
modelProxy->hideColumn(0, false);
QCOMPARE(modelProxy->columnCount(), 15);
QCOMPARE(modelProxy->columnCount(), columnCount);
QVERIFY(!spyColumnInsert.isEmpty());

int oldSpyColumnInsertSize = spyColumnInsert.size();
Expand Down
7 changes: 7 additions & 0 deletions tests/gui/TestGui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1082,6 +1082,13 @@ void TestGui::testSearch()
QCOMPARE(groupView->currentGroup(), m_db->rootGroup());
QVERIFY(!m_dbWidget->isSearchActive());

// check if first entry is selected after search
QTest::keyClicks(searchTextEdit, "some");
QTRY_VERIFY(m_dbWidget->isSearchActive());
QTRY_COMPARE(entryView->selectedEntries().length(), 1);
QModelIndex index_current = entryView->indexFromEntry(entryView->currentEntry());
QTRY_COMPARE(index_current.row(), 0);

// Try to edit the first entry from the search view
// Refocus back to search edit
QTest::mouseClick(searchTextEdit, Qt::LeftButton);
Expand Down

0 comments on commit bb9700d

Please sign in to comment.