From e776de8eebed90af6b41c9df34e977795bcb4468 Mon Sep 17 00:00:00 2001 From: Felix Geyer Date: Sat, 17 May 2014 12:51:16 +0200 Subject: [PATCH] Remember and synchronize entry column sizes. Closes #159 --- src/gui/DatabaseWidget.cpp | 28 +++++++++ src/gui/DatabaseWidget.h | 3 + src/gui/DatabaseWidgetStateSync.cpp | 92 +++++++++++++++++++++++------ src/gui/DatabaseWidgetStateSync.h | 8 +++ 4 files changed, 113 insertions(+), 18 deletions(-) diff --git a/src/gui/DatabaseWidget.cpp b/src/gui/DatabaseWidget.cpp index 9a09814b7..5379e1a1f 100644 --- a/src/gui/DatabaseWidget.cpp +++ b/src/gui/DatabaseWidget.cpp @@ -138,6 +138,7 @@ DatabaseWidget::DatabaseWidget(Database* db, QWidget* parent) addWidget(m_unlockDatabaseWidget); connect(m_splitter, SIGNAL(splitterMoved(int,int)), SIGNAL(splitterSizesChanged())); + connect(m_entryView->header(), SIGNAL(sectionResized(int,int,int)), SIGNAL(entryColumnSizesChanged())); connect(m_groupView, SIGNAL(groupChanged(Group*)), this, SLOT(clearLastGroup(Group*))); connect(m_groupView, SIGNAL(groupChanged(Group*)), SIGNAL(groupChanged())); connect(m_groupView, SIGNAL(groupChanged(Group*)), m_entryView, SLOT(setGroup(Group*))); @@ -207,6 +208,29 @@ void DatabaseWidget::setSplitterSizes(const QList& sizes) m_splitter->setSizes(sizes); } +QList DatabaseWidget::entryHeaderViewSizes() const +{ + QList sizes; + + for (int i = 0; i < m_entryView->header()->count(); i++) { + sizes.append(m_entryView->header()->sectionSize(i)); + } + + return sizes; +} + +void DatabaseWidget::setEntryViewHeaderSizes(const QList& sizes) +{ + if (sizes.size() != m_entryView->header()->count()) { + Q_ASSERT(false); + return; + } + + for (int i = 0; i < sizes.size(); i++) { + m_entryView->header()->resizeSection(i, sizes[i]); + } +} + void DatabaseWidget::emitCurrentModeChanged() { Q_EMIT currentModeChanged(currentMode()); @@ -702,6 +726,8 @@ void DatabaseWidget::closeSearch() Q_ASSERT(m_lastGroup); m_groupView->setCurrentGroup(m_lastGroup); m_searchTimer->stop(); + + Q_EMIT listModeActivated(); } void DatabaseWidget::showSearch() @@ -739,6 +765,8 @@ void DatabaseWidget::showSearch() m_searchWidget->show(); search(); m_searchUi->searchEdit->setFocus(); + + Q_EMIT searchModeActivated(); } void DatabaseWidget::search() diff --git a/src/gui/DatabaseWidget.h b/src/gui/DatabaseWidget.h index 2e2077bcc..981784d48 100644 --- a/src/gui/DatabaseWidget.h +++ b/src/gui/DatabaseWidget.h @@ -76,6 +76,8 @@ public: bool isInEditMode() const; QList splitterSizes() const; void setSplitterSizes(const QList& sizes); + QList entryHeaderViewSizes() const; + void setEntryViewHeaderSizes(const QList& sizes); Q_SIGNALS: void closeRequest(); @@ -89,6 +91,7 @@ Q_SIGNALS: void listModeActivated(); void searchModeActivated(); void splitterSizesChanged(); + void entryColumnSizesChanged(); public Q_SLOTS: void createEntry(); diff --git a/src/gui/DatabaseWidgetStateSync.cpp b/src/gui/DatabaseWidgetStateSync.cpp index 2a071e306..c9e60bb93 100644 --- a/src/gui/DatabaseWidgetStateSync.cpp +++ b/src/gui/DatabaseWidgetStateSync.cpp @@ -24,28 +24,16 @@ DatabaseWidgetStateSync::DatabaseWidgetStateSync(QObject* parent) : QObject(parent) , m_activeDbWidget(Q_NULLPTR) { - QVariantList variantList = config()->get("GUI/SplitterState").toList(); - Q_FOREACH (const QVariant& var, variantList) { - bool ok; - int size = var.toInt(&ok); - if (ok) { - m_splitterSizes.append(size); - } - else { - m_splitterSizes.clear(); - break; - } - } + m_splitterSizes = variantToIntList(config()->get("GUI/SplitterState")); + m_columnSizesList = variantToIntList(config()->get("GUI/EntryListColumnSizes")); + m_columnSizesSearch = variantToIntList(config()->get("GUI/EntrySearchColumnSizes")); } DatabaseWidgetStateSync::~DatabaseWidgetStateSync() { - QVariantList variantList; - Q_FOREACH (int size, m_splitterSizes) { - variantList.append(size); - } - - config()->set("GUI/SplitterState", variantList); + config()->set("GUI/SplitterState", intListToVariant(m_splitterSizes)); + config()->set("GUI/EntryListColumnSizes", intListToVariant(m_columnSizesList)); + config()->set("GUI/EntrySearchColumnSizes", intListToVariant(m_columnSizesSearch)); } void DatabaseWidgetStateSync::setActive(DatabaseWidget* dbWidget) @@ -61,8 +49,35 @@ void DatabaseWidgetStateSync::setActive(DatabaseWidget* dbWidget) m_activeDbWidget->setSplitterSizes(m_splitterSizes); } + if (m_activeDbWidget->isGroupSelected()) { + restoreListView(); + } + else { + restoreSearchView(); + } + connect(m_activeDbWidget, SIGNAL(splitterSizesChanged()), SLOT(updateSplitterSizes())); + connect(m_activeDbWidget, SIGNAL(entryColumnSizesChanged()), + SLOT(updateColumnSizes())); + connect(m_activeDbWidget, SIGNAL(listModeActivated()), + SLOT(restoreListView())); + connect(m_activeDbWidget, SIGNAL(searchModeActivated()), + SLOT(restoreSearchView())); + } +} + +void DatabaseWidgetStateSync::restoreListView() +{ + if (!m_columnSizesList.isEmpty()) { + m_activeDbWidget->setEntryViewHeaderSizes(m_columnSizesList); + } +} + +void DatabaseWidgetStateSync::restoreSearchView() +{ + if (!m_columnSizesSearch.isEmpty()) { + m_activeDbWidget->setEntryViewHeaderSizes(m_columnSizesSearch); } } @@ -70,3 +85,44 @@ void DatabaseWidgetStateSync::updateSplitterSizes() { m_splitterSizes = m_activeDbWidget->splitterSizes(); } + +void DatabaseWidgetStateSync::updateColumnSizes() +{ + if (m_activeDbWidget->isGroupSelected()) { + m_columnSizesList = m_activeDbWidget->entryHeaderViewSizes(); + } + else { + m_columnSizesSearch = m_activeDbWidget->entryHeaderViewSizes(); + } +} + +QList DatabaseWidgetStateSync::variantToIntList(const QVariant& variant) +{ + QVariantList list = variant.toList(); + QList result; + + Q_FOREACH (const QVariant& var, list) { + bool ok; + int size = var.toInt(&ok); + if (ok) { + result.append(size); + } + else { + result.clear(); + break; + } + } + + return result; +} + +QVariant DatabaseWidgetStateSync::intListToVariant(const QList& list) +{ + QVariantList result; + + Q_FOREACH (int value, list) { + result.append(value); + } + + return result; +} diff --git a/src/gui/DatabaseWidgetStateSync.h b/src/gui/DatabaseWidgetStateSync.h index 574519bd4..26911837c 100644 --- a/src/gui/DatabaseWidgetStateSync.h +++ b/src/gui/DatabaseWidgetStateSync.h @@ -31,14 +31,22 @@ public: public Q_SLOTS: void setActive(DatabaseWidget* dbWidget); + void restoreListView(); + void restoreSearchView(); private Q_SLOTS: void updateSplitterSizes(); + void updateColumnSizes(); private: + static QList variantToIntList(const QVariant& variant); + static QVariant intListToVariant(const QList& list); + DatabaseWidget* m_activeDbWidget; QList m_splitterSizes; + QList m_columnSizesList; + QList m_columnSizesSearch; }; #endif // KEEPASSX_HEADERVIEWSYNC_H