From 9363d23e09a9981de4c8f799a91ae02eb9747aa8 Mon Sep 17 00:00:00 2001 From: Florian Geyer Date: Thu, 15 May 2014 18:05:58 +0200 Subject: [PATCH] Remove dependency to Group- and EntryView from MainWindow. --- src/gui/DatabaseWidget.cpp | 20 ++++++++++++++++++++ src/gui/DatabaseWidget.h | 3 +++ src/gui/MainWindow.cpp | 19 ++++++++----------- src/gui/MainWindow.h | 1 + src/gui/entry/EntryView.cpp | 7 ++++++- src/gui/entry/EntryView.h | 1 + 6 files changed, 39 insertions(+), 12 deletions(-) diff --git a/src/gui/DatabaseWidget.cpp b/src/gui/DatabaseWidget.cpp index 419028ab8..d6d48c5e0 100644 --- a/src/gui/DatabaseWidget.cpp +++ b/src/gui/DatabaseWidget.cpp @@ -811,3 +811,23 @@ void DatabaseWidget::updateFilename(const QString& fileName) { m_filename = fileName; } + +int DatabaseWidget::numberOfSelectedEntries() +{ + return m_entryView->numberOfSelectedEntries(); +} + +QStringList DatabaseWidget::customEntryAttributes() +{ + Entry* entry = m_entryView->currentEntry(); + if (!entry) { + return QStringList(); + } + + return entry->attributes()->customKeys(); +} + +bool DatabaseWidget::isGroupSelected() +{ + return m_groupView->currentGroup() != Q_NULLPTR; +} diff --git a/src/gui/DatabaseWidget.h b/src/gui/DatabaseWidget.h index b535a9b8f..d2b4e9e05 100644 --- a/src/gui/DatabaseWidget.h +++ b/src/gui/DatabaseWidget.h @@ -71,6 +71,9 @@ public: DatabaseWidget::Mode currentMode(); void lock(); void updateFilename(const QString& filename); + int numberOfSelectedEntries(); + QStringList customEntryAttributes(); + bool isGroupSelected(); Q_SIGNALS: void closeRequest(); diff --git a/src/gui/MainWindow.cpp b/src/gui/MainWindow.cpp index e0c6582e0..98bd5aee1 100644 --- a/src/gui/MainWindow.cpp +++ b/src/gui/MainWindow.cpp @@ -23,15 +23,11 @@ #include "autotype/AutoType.h" #include "core/Config.h" -#include "core/Database.h" -#include "core/Entry.h" #include "core/FilePath.h" #include "core/InactivityTimer.h" #include "core/Metadata.h" #include "gui/AboutDialog.h" #include "gui/DatabaseWidget.h" -#include "gui/entry/EntryView.h" -#include "gui/group/GroupView.h" const QString MainWindow::BaseWindowTitle = "KeePassX"; @@ -40,6 +36,8 @@ MainWindow::MainWindow() { m_ui->setupUi(this); + m_countDefaultAttributes = m_ui->menuEntryCopyAttribute->actions().size(); + restoreGeometry(config()->get("window/Geometry").toByteArray()); setWindowIcon(filePath()->applicationIcon()); @@ -229,17 +227,16 @@ void MainWindow::updateCopyAttributesMenu() return; } - Entry* entry = dbWidget->entryView()->currentEntry(); - if (!entry || !dbWidget->entryView()->isSingleEntrySelected()) { + if (!dbWidget->numberOfSelectedEntries() == 1) { return; } QList actions = m_ui->menuEntryCopyAttribute->actions(); - for (int i = EntryAttributes::DefaultAttributes.size() + 1; i < actions.size(); i++) { + for (int i = m_countDefaultAttributes + 1; i < actions.size(); i++) { delete actions[i]; } - Q_FOREACH (const QString& key, entry->attributes()->customKeys()) { + Q_FOREACH (const QString& key, dbWidget->customEntryAttributes()) { QAction* action = m_ui->menuEntryCopyAttribute->addAction(key); m_copyAdditionalAttributeActions->addAction(action); } @@ -276,9 +273,9 @@ void MainWindow::setMenuActionState(DatabaseWidget::Mode mode) switch (mode) { case DatabaseWidget::ViewMode: { bool inSearch = dbWidget->isInSearchMode(); - bool singleEntrySelected = dbWidget->entryView()->isSingleEntrySelected(); - bool entriesSelected = !dbWidget->entryView()->selectionModel()->selectedRows().isEmpty(); - bool groupSelected = dbWidget->groupView()->currentGroup(); + bool singleEntrySelected = dbWidget->numberOfSelectedEntries() == 1; + bool entriesSelected = dbWidget->numberOfSelectedEntries() > 0; + bool groupSelected = dbWidget->isGroupSelected(); m_ui->actionEntryNew->setEnabled(!inSearch); m_ui->actionEntryClone->setEnabled(singleEntrySelected && !inSearch); diff --git a/src/gui/MainWindow.h b/src/gui/MainWindow.h index 706fd2d50..e904426ae 100644 --- a/src/gui/MainWindow.h +++ b/src/gui/MainWindow.h @@ -77,6 +77,7 @@ private: QActionGroup* m_copyAdditionalAttributeActions; QStringList m_openDatabases; InactivityTimer* m_inactivityTimer; + int m_countDefaultAttributes; Q_DISABLE_COPY(MainWindow) }; diff --git a/src/gui/entry/EntryView.cpp b/src/gui/entry/EntryView.cpp index f71f80bc6..eb79ec966 100644 --- a/src/gui/entry/EntryView.cpp +++ b/src/gui/entry/EntryView.cpp @@ -100,9 +100,14 @@ Entry* EntryView::currentEntry() } } +int EntryView::numberOfSelectedEntries() +{ + return selectionModel()->selectedRows().size(); +} + bool EntryView::isSingleEntrySelected() { - return (selectionModel()->selectedRows().size() == 1); + return (numberOfSelectedEntries() == 1); } void EntryView::setCurrentEntry(Entry* entry) diff --git a/src/gui/entry/EntryView.h b/src/gui/entry/EntryView.h index b5f056aa0..2a1df439c 100644 --- a/src/gui/entry/EntryView.h +++ b/src/gui/entry/EntryView.h @@ -42,6 +42,7 @@ public: Entry* entryFromIndex(const QModelIndex& index); void setEntryList(const QList& entries); bool inEntryListMode(); + int numberOfSelectedEntries(); public Q_SLOTS: void setGroup(Group* group);