Preserve group/entry focus when replacing db. (#176)

This commit is contained in:
louib 2017-01-24 22:17:16 -05:00 committed by Jonathan White
parent 42c0815cda
commit cdbf58b2c1
2 changed files with 53 additions and 8 deletions

View File

@ -722,15 +722,10 @@ void DatabaseWidget::unlockDatabase(bool accepted)
replaceDatabase(db); replaceDatabase(db);
const QList<Group*> groups = m_db->rootGroup()->groupsRecursive(true); restoreGroupEntryFocus(m_groupBeforeLock, m_entryBeforeLock);
for (Group* group : groups) {
if (group->uuid() == m_groupBeforeLock) {
m_groupView->setCurrentGroup(group);
break;
}
}
m_groupBeforeLock = Uuid(); m_groupBeforeLock = Uuid();
m_entryBeforeLock = Uuid();
setCurrentWidget(m_mainWidget); setCurrentWidget(m_mainWidget);
m_unlockDatabaseWidget->clearForms(); m_unlockDatabaseWidget->clearForms();
Q_EMIT unlockedDatabase(); Q_EMIT unlockedDatabase();
@ -943,6 +938,10 @@ void DatabaseWidget::lock()
m_groupBeforeLock = m_db->rootGroup()->uuid(); m_groupBeforeLock = m_db->rootGroup()->uuid();
} }
if (m_entryView->currentEntry()) {
m_entryBeforeLock = m_entryView->currentEntry()->uuid();
}
clearAllWidgets(); clearAllWidgets();
m_unlockDatabaseWidget->load(m_filename); m_unlockDatabaseWidget->load(m_filename);
setCurrentWidget(m_unlockDatabaseWidget); 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); replaceDatabase(db);
restoreGroupEntryFocus(groupBeforeReload, entryBeforeReload);
} }
else { else {
MessageBox::critical(this, tr("Autoreload Failed"), MessageBox::critical(this, tr("Autoreload Failed"),
@ -1061,6 +1075,35 @@ QStringList DatabaseWidget::customEntryAttributes() const
return entry->attributes()->customKeys(); 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<Group*> 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<Entry*> entries = restoredGroup->entries();
for (Entry* entry : entries) {
if (entry->uuid() == entryUuid) {
m_entryView->setCurrentEntry(entry);
break;
}
}
}
}
bool DatabaseWidget::isGroupSelected() const bool DatabaseWidget::isGroupSelected() const
{ {
return m_groupView->currentGroup() != nullptr; return m_groupView->currentGroup() != nullptr;

View File

@ -163,6 +163,7 @@ private Q_SLOTS:
// Database autoreload slots // Database autoreload slots
void onWatchedFileChanged(); void onWatchedFileChanged();
void reloadDatabaseFile(); void reloadDatabaseFile();
void restoreGroupEntryFocus(Uuid groupUuid, Uuid EntryUuid);
private: private:
void setClipboardTextAndMinimize(const QString& text); void setClipboardTextAndMinimize(const QString& text);
@ -190,6 +191,7 @@ private:
Group* m_newParent; Group* m_newParent;
QString m_filename; QString m_filename;
Uuid m_groupBeforeLock; Uuid m_groupBeforeLock;
Uuid m_entryBeforeLock;
// Search state // Search state
QString m_lastSearchText; QString m_lastSearchText;