From cdbf58b2c13186dc487f915a717639ec44b95d35 Mon Sep 17 00:00:00 2001 From: louib Date: Tue, 24 Jan 2017 22:17:16 -0500 Subject: [PATCH] Preserve group/entry focus when replacing db. (#176) --- src/gui/DatabaseWidget.cpp | 59 ++++++++++++++++++++++++++++++++------ src/gui/DatabaseWidget.h | 2 ++ 2 files changed, 53 insertions(+), 8 deletions(-) diff --git a/src/gui/DatabaseWidget.cpp b/src/gui/DatabaseWidget.cpp index 985374c49..f08b432fe 100644 --- a/src/gui/DatabaseWidget.cpp +++ b/src/gui/DatabaseWidget.cpp @@ -722,15 +722,10 @@ void DatabaseWidget::unlockDatabase(bool accepted) replaceDatabase(db); - const QList groups = m_db->rootGroup()->groupsRecursive(true); - for (Group* group : groups) { - if (group->uuid() == m_groupBeforeLock) { - m_groupView->setCurrentGroup(group); - break; - } - } - + restoreGroupEntryFocus(m_groupBeforeLock, m_entryBeforeLock); m_groupBeforeLock = Uuid(); + m_entryBeforeLock = Uuid(); + setCurrentWidget(m_mainWidget); m_unlockDatabaseWidget->clearForms(); Q_EMIT unlockedDatabase(); @@ -943,6 +938,10 @@ void DatabaseWidget::lock() m_groupBeforeLock = m_db->rootGroup()->uuid(); } + if (m_entryView->currentEntry()) { + m_entryBeforeLock = m_entryView->currentEntry()->uuid(); + } + clearAllWidgets(); m_unlockDatabaseWidget->load(m_filename); setCurrentWidget(m_unlockDatabaseWidget); @@ -1028,7 +1027,22 @@ void DatabaseWidget::reloadDatabaseFile() } } + Uuid groupBeforeReload; + if (m_groupView && m_groupView->currentGroup()) { + groupBeforeReload = m_groupView->currentGroup()->uuid(); + } + else { + groupBeforeReload = m_db->rootGroup()->uuid(); + } + + Uuid entryBeforeReload; + if (m_entryView && m_entryView->currentEntry()) { + entryBeforeReload = m_entryView->currentEntry()->uuid(); + } + replaceDatabase(db); + restoreGroupEntryFocus(groupBeforeReload, entryBeforeReload); + } else { MessageBox::critical(this, tr("Autoreload Failed"), @@ -1061,6 +1075,35 @@ QStringList DatabaseWidget::customEntryAttributes() const return entry->attributes()->customKeys(); } +/* + * Restores the focus on the group and entry that was focused + * before the database was locked or reloaded. + */ +void DatabaseWidget::restoreGroupEntryFocus(Uuid groupUuid, Uuid entryUuid) +{ + Group* restoredGroup = nullptr; + const QList groups = m_db->rootGroup()->groupsRecursive(true); + for (Group* group : groups) { + if (group->uuid() == groupUuid) { + restoredGroup = group; + break; + } + } + + if (restoredGroup != nullptr) { + m_groupView->setCurrentGroup(restoredGroup); + + const QList entries = restoredGroup->entries(); + for (Entry* entry : entries) { + if (entry->uuid() == entryUuid) { + m_entryView->setCurrentEntry(entry); + break; + } + } + } + +} + bool DatabaseWidget::isGroupSelected() const { return m_groupView->currentGroup() != nullptr; diff --git a/src/gui/DatabaseWidget.h b/src/gui/DatabaseWidget.h index f55fa2027..79e58cecf 100644 --- a/src/gui/DatabaseWidget.h +++ b/src/gui/DatabaseWidget.h @@ -163,6 +163,7 @@ private Q_SLOTS: // Database autoreload slots void onWatchedFileChanged(); void reloadDatabaseFile(); + void restoreGroupEntryFocus(Uuid groupUuid, Uuid EntryUuid); private: void setClipboardTextAndMinimize(const QString& text); @@ -190,6 +191,7 @@ private: Group* m_newParent; QString m_filename; Uuid m_groupBeforeLock; + Uuid m_entryBeforeLock; // Search state QString m_lastSearchText;