Skip to content

Commit

Permalink
Select new entry after cloning
Browse files Browse the repository at this point in the history
Also fixes re-selecting entries during a search refresh
  • Loading branch information
knu authored and droidmonkey committed Feb 14, 2023
1 parent b4be71d commit cc35bf2
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 41 deletions.
9 changes: 4 additions & 5 deletions src/gui/CloneDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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()));
Expand All @@ -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();
}

Expand Down
3 changes: 3 additions & 0 deletions src/gui/CloneDialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<Ui::CloneDialog> m_ui;

Expand Down
66 changes: 34 additions & 32 deletions src/gui/CloneDialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -6,54 +6,56 @@
<rect>
<x>0</x>
<y>0</y>
<width>347</width>
<height>136</height>
<width>319</width>
<height>132</height>
</rect>
</property>
<property name="windowTitle">
<string>Clone Entry Options</string>
</property>
<property name="modal">
<bool>true</bool>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QCheckBox" name="titleClone">
<property name="text">
<string>Append ' - Clone' to title</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="referencesClone">
<property name="text">
<string>Replace username and password with references</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="historyClone">
<property name="text">
<string>Copy history</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
<widget class="QCheckBox" name="titleClone">
<property name="text">
<string>Append ' - Clone' to title</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="referencesClone">
<property name="text">
<string>Replace username and password with references</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="historyClone">
<property name="text">
<string>Copy history</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeType">
<enum>QSizePolicy::Minimum</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
<height>6</height>
</size>
</property>
</spacer>
Expand Down
8 changes: 8 additions & 0 deletions src/gui/DatabaseWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -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);
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/gui/entry/EntryModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 5 additions & 2 deletions src/gui/entry/EntryView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions tests/gui/TestGui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit cc35bf2

Please sign in to comment.