From 9fb14504a7b84f1cff6f0fa02d64c143a01c4a7f Mon Sep 17 00:00:00 2001 From: Akinori MUSHA Date: Mon, 6 Feb 2023 04:37:26 +0900 Subject: [PATCH] Select new entry after cloning Also fixes re-selecting entries during a search refresh --- src/gui/CloneDialog.cpp | 9 +++-- src/gui/CloneDialog.h | 3 ++ src/gui/CloneDialog.ui | 66 +++++++++++++++++++----------------- src/gui/DatabaseWidget.cpp | 8 +++++ src/gui/entry/EntryModel.cpp | 6 ++-- src/gui/entry/EntryView.cpp | 7 ++-- tests/gui/TestGui.cpp | 1 + 7 files changed, 59 insertions(+), 41 deletions(-) diff --git a/src/gui/CloneDialog.cpp b/src/gui/CloneDialog.cpp index 0ce2c5f2ec..3e3c853470 100644 --- a/src/gui/CloneDialog.cpp +++ b/src/gui/CloneDialog.cpp @@ -18,8 +18,6 @@ #include "CloneDialog.h" #include "ui_CloneDialog.h" -#include "config-keepassx.h" - CloneDialog::CloneDialog(DatabaseWidget* parent, Database* db, Entry* entry) : QDialog(parent) , m_ui(new Ui::CloneDialog()) @@ -29,8 +27,9 @@ CloneDialog::CloneDialog(DatabaseWidget* parent, Database* db, Entry* entry) m_parent = parent; m_ui->setupUi(this); - this->setFixedSize(this->sizeHint()); + window()->layout()->setSizeConstraint(QLayout::SetFixedSize); + setWindowFlag(Qt::WindowContextHelpButtonHint, false); setAttribute(Qt::WA_DeleteOnClose); connect(m_ui->buttonBox, SIGNAL(rejected()), SLOT(close())); @@ -54,10 +53,10 @@ void CloneDialog::cloneEntry() flags |= Entry::CloneIncludeHistory; } - Entry* entry = m_entry->clone(flags); + auto entry = m_entry->clone(flags); entry->setGroup(m_entry->group()); - m_parent->refreshSearch(); + emit entryCloned(entry); close(); } diff --git a/src/gui/CloneDialog.h b/src/gui/CloneDialog.h index fe2787e3da..febf9d480d 100644 --- a/src/gui/CloneDialog.h +++ b/src/gui/CloneDialog.h @@ -34,6 +34,9 @@ class CloneDialog : public QDialog explicit CloneDialog(DatabaseWidget* parent = nullptr, Database* db = nullptr, Entry* entry = nullptr); ~CloneDialog() override; +signals: + void entryCloned(Entry* clone); + private: QScopedPointer m_ui; diff --git a/src/gui/CloneDialog.ui b/src/gui/CloneDialog.ui index db0614dfac..80da0ee583 100644 --- a/src/gui/CloneDialog.ui +++ b/src/gui/CloneDialog.ui @@ -6,54 +6,56 @@ 0 0 - 347 - 136 + 319 + 132 Clone Entry Options + + true + - - - - - Append ' - Clone' to title - - - true - - - - - - - Replace username and password with references - - - - - - - Copy history - - - true - - - - + + + Append ' - Clone' to title + + + true + + + + + + + Replace username and password with references + + + + + + + Copy history + + + true + + Qt::Vertical + + QSizePolicy::Minimum + 20 - 40 + 6 diff --git a/src/gui/DatabaseWidget.cpp b/src/gui/DatabaseWidget.cpp index 882a87f491..9bb0ac475f 100644 --- a/src/gui/DatabaseWidget.cpp +++ b/src/gui/DatabaseWidget.cpp @@ -447,6 +447,11 @@ void DatabaseWidget::cloneEntry() } auto cloneDialog = new CloneDialog(this, m_db.data(), currentEntry); + connect(cloneDialog, &CloneDialog::entryCloned, this, [this](auto entry) { + refreshSearch(); + m_entryView->setCurrentEntry(entry); + }); + cloneDialog->show(); } @@ -1399,7 +1404,10 @@ void DatabaseWidget::performUnlockDatabase(const QString& password, const QStrin void DatabaseWidget::refreshSearch() { if (isSearchActive()) { + auto selectedEntry = m_entryView->currentEntry(); search(m_lastSearchText); + // Re-select the previous entry if it is still in the search + m_entryView->setCurrentEntry(selectedEntry); } } diff --git a/src/gui/entry/EntryModel.cpp b/src/gui/entry/EntryModel.cpp index b25b002ef2..877efe9d2b 100644 --- a/src/gui/entry/EntryModel.cpp +++ b/src/gui/entry/EntryModel.cpp @@ -50,8 +50,10 @@ Entry* EntryModel::entryFromIndex(const QModelIndex& index) const QModelIndex EntryModel::indexFromEntry(Entry* entry) const { int row = m_entries.indexOf(entry); - Q_ASSERT(row != -1); - return index(row, 1); + if (row >= 0) { + return index(row, 1); + } + return {}; } void EntryModel::setGroup(Group* group) diff --git a/src/gui/entry/EntryView.cpp b/src/gui/entry/EntryView.cpp index 67a9698b7f..c992ead7b5 100644 --- a/src/gui/entry/EntryView.cpp +++ b/src/gui/entry/EntryView.cpp @@ -279,8 +279,11 @@ int EntryView::numberOfSelectedEntries() void EntryView::setCurrentEntry(Entry* entry) { - selectionModel()->setCurrentIndex(m_sortModel->mapFromSource(m_model->indexFromEntry(entry)), - QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows); + auto index = m_model->indexFromEntry(entry); + if (index.isValid()) { + selectionModel()->setCurrentIndex(m_sortModel->mapFromSource(index), + QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows); + } } Entry* EntryView::entryFromIndex(const QModelIndex& index) diff --git a/tests/gui/TestGui.cpp b/tests/gui/TestGui.cpp index 6daec5645f..71cb7d7056 100644 --- a/tests/gui/TestGui.cpp +++ b/tests/gui/TestGui.cpp @@ -1230,6 +1230,7 @@ void TestGui::testCloneEntry() Entry* entryClone = entryView->entryFromIndex(entryView->model()->index(1, 1)); QVERIFY(entryOrg->uuid() != entryClone->uuid()); QCOMPARE(entryClone->title(), entryOrg->title() + QString(" - Clone")); + QVERIFY(m_dbWidget->currentSelectedEntry()->uuid() == entryClone->uuid()); } void TestGui::testEntryPlaceholders()