From c29c423ec825100f656406db27143a001afcf7f1 Mon Sep 17 00:00:00 2001 From: Florian Geyer Date: Wed, 25 Apr 2012 01:32:05 +0200 Subject: [PATCH] Add enum to describe current mode of database widget. --- src/gui/DatabaseTabWidget.cpp | 4 ++-- src/gui/DatabaseTabWidget.h | 3 ++- src/gui/DatabaseWidget.cpp | 27 ++++++++++++++++++++++++++- src/gui/DatabaseWidget.h | 10 ++++++++++ src/gui/MainWindow.cpp | 17 +++++++---------- src/gui/MainWindow.h | 4 +++- 6 files changed, 50 insertions(+), 15 deletions(-) diff --git a/src/gui/DatabaseTabWidget.cpp b/src/gui/DatabaseTabWidget.cpp index b8eae0a0d..09c7a592e 100644 --- a/src/gui/DatabaseTabWidget.cpp +++ b/src/gui/DatabaseTabWidget.cpp @@ -160,7 +160,7 @@ bool DatabaseTabWidget::closeDatabase(Database* db) if (dbName.right(1) == "*") { dbName.chop(1); } - if (dbStruct.dbWidget->currentIndex() != 0) { + if (dbStruct.dbWidget->currentMode() == DatabaseWidget::EditMode){ QMessageBox::StandardButton result = QMessageBox::question( this, tr("Close?"), @@ -407,7 +407,7 @@ void DatabaseTabWidget::insertDatabase(Database* db, const DatabaseManagerStruct connect(dbStruct.dbWidget->entryView(), SIGNAL(entrySelectionChanged()), SLOT(emitEntrySelectionChanged())); connect(dbStruct.dbWidget, SIGNAL(closeRequest()), SLOT(closeDatabase())); connect(db, SIGNAL(modified()), SLOT(modified())); - connect(dbStruct.dbWidget, SIGNAL(currentChanged(int)), this, SIGNAL(currentWidgetIndexChanged(int))); + connect(dbStruct.dbWidget, SIGNAL(currentModeChanged(DatabaseWidget::Mode)), this, SIGNAL(currentWidgetModeChanged(DatabaseWidget::Mode))); } DatabaseWidget* DatabaseTabWidget::currentDatabaseWidget() diff --git a/src/gui/DatabaseTabWidget.h b/src/gui/DatabaseTabWidget.h index 788bc6b6e..c9819d48d 100644 --- a/src/gui/DatabaseTabWidget.h +++ b/src/gui/DatabaseTabWidget.h @@ -23,6 +23,7 @@ #include #include "format/KeePass2Writer.h" +#include "gui/DatabaseWidget.h" class DatabaseWidget; class DatabaseOpenDialog; @@ -71,7 +72,7 @@ public Q_SLOTS: Q_SIGNALS: void entrySelectionChanged(bool singleEntrySelected); - void currentWidgetIndexChanged(int index); + void currentWidgetModeChanged(DatabaseWidget::Mode); void tabNameChanged(); private Q_SLOTS: diff --git a/src/gui/DatabaseWidget.cpp b/src/gui/DatabaseWidget.cpp index a1ea521b9..1c2c88398 100644 --- a/src/gui/DatabaseWidget.cpp +++ b/src/gui/DatabaseWidget.cpp @@ -31,7 +31,6 @@ #include "gui/EntryView.h" #include "gui/GroupView.h" - DatabaseWidget::DatabaseWidget(Database* db, QWidget* parent) : QStackedWidget(parent) , m_db(db) @@ -87,10 +86,36 @@ DatabaseWidget::DatabaseWidget(Database* db, QWidget* parent) connect(m_editGroupWidget, SIGNAL(editFinished(bool)), SLOT(switchToView(bool))); connect(m_changeMasterKeyWidget, SIGNAL(editFinished(bool)), SLOT(updateMasterKey(bool))); connect(m_databaseSettingsWidget, SIGNAL(editFinished(bool)), SLOT(updateSettings(bool))); + connect(this, SIGNAL(currentChanged(int)), this, SLOT(emitCurrentModeChanged())); setCurrentIndex(0); } +DatabaseWidget::Mode DatabaseWidget::currentMode() +{ + switch (currentIndex()) { + case -1: + return DatabaseWidget::None; + break; + case 0: + return DatabaseWidget::ViewMode; + break; + case 1: // entry edit + case 2: // group edit + case 3: // change master key + case 4: // database settings + return DatabaseWidget::EditMode; + break; + } + + return DatabaseWidget::None; +} + +void DatabaseWidget::emitCurrentModeChanged() +{ + Q_EMIT currentModeChanged(currentMode()); +} + GroupView* DatabaseWidget::groupView() { return m_groupView; diff --git a/src/gui/DatabaseWidget.h b/src/gui/DatabaseWidget.h index a9f19fc85..e4c526ab5 100644 --- a/src/gui/DatabaseWidget.h +++ b/src/gui/DatabaseWidget.h @@ -35,6 +35,13 @@ class DatabaseWidget : public QStackedWidget Q_OBJECT public: + enum Mode + { + None = 0, + ViewMode = 1, + EditMode = 2 + }; + explicit DatabaseWidget(Database* db, QWidget* parent = 0); GroupView* groupView(); EntryView* entryView(); @@ -44,9 +51,11 @@ public: bool canDeleteCurrentGoup(); int addWidget(QWidget* w); void setCurrentIndex(int index); + DatabaseWidget::Mode currentMode(); Q_SIGNALS: void closeRequest(); + void currentModeChanged(DatabaseWidget::Mode); public Q_SLOTS: void createEntry(); @@ -63,6 +72,7 @@ private Q_SLOTS: void switchToGroupEdit(Group* entry, bool create); void updateMasterKey(bool accepted); void updateSettings(bool accepted); + void emitCurrentModeChanged(); private: Database* const m_db; diff --git a/src/gui/MainWindow.cpp b/src/gui/MainWindow.cpp index 9417eff8e..8a543b706 100644 --- a/src/gui/MainWindow.cpp +++ b/src/gui/MainWindow.cpp @@ -34,7 +34,7 @@ MainWindow::MainWindow() setWindowIcon(dataPath()->applicationIcon()); connect(m_ui->tabWidget, SIGNAL(entrySelectionChanged(bool)), SLOT(setMenuActionState())); - connect(m_ui->tabWidget, SIGNAL(currentWidgetIndexChanged(int)), SLOT(setMenuActionState(int))); + connect(m_ui->tabWidget, SIGNAL(currentWidgetModeChanged(DatabaseWidget::Mode)), SLOT(setMenuActionState(DatabaseWidget::Mode))); connect(m_ui->tabWidget, SIGNAL(tabNameChanged()), SLOT(updateWindowTitle())); connect(m_ui->tabWidget, SIGNAL(currentChanged(int)), SLOT(updateWindowTitle())); @@ -65,19 +65,19 @@ void MainWindow::openDatabase(const QString& fileName, const QString& pw, const const QString MainWindow::m_baseWindowTitle = "KeePassX"; -void MainWindow::setMenuActionState(int index) +void MainWindow::setMenuActionState(DatabaseWidget::Mode mode) { if (m_ui->tabWidget->currentIndex() != -1) { m_ui->actionDatabaseClose->setEnabled(true); DatabaseWidget* dbWidget = m_ui->tabWidget->currentDatabaseWidget(); Q_ASSERT(dbWidget); - if (index == -1) { - index = dbWidget->currentIndex(); + if (mode == DatabaseWidget::None) { + mode = dbWidget->currentMode(); } - switch (index) { - case 0: // view + switch (mode) { + case DatabaseWidget::ViewMode: m_ui->actionEntryNew->setEnabled(true); m_ui->actionGroupNew->setEnabled(true); if (dbWidget->entryView()->currentIndex().isValid()) { @@ -101,10 +101,7 @@ void MainWindow::setMenuActionState(int index) m_ui->actionDatabaseSave->setEnabled(true); m_ui->actionDatabaseSaveAs->setEnabled(true); break; - case 1: // entry edit - case 2: // group edit - case 3: // change master key - case 4: // database settings + case DatabaseWidget::EditMode: m_ui->actionEntryNew->setEnabled(false); m_ui->actionGroupNew->setEnabled(false); m_ui->actionEntryEdit->setEnabled(false); diff --git a/src/gui/MainWindow.h b/src/gui/MainWindow.h index 000a95fd7..784e0f955 100644 --- a/src/gui/MainWindow.h +++ b/src/gui/MainWindow.h @@ -20,6 +20,8 @@ #include +#include "gui/DatabaseWidget.h" + namespace Ui { class MainWindow; } @@ -37,7 +39,7 @@ protected: void closeEvent(QCloseEvent *event); private Q_SLOTS: - void setMenuActionState(int index = -1); + void setMenuActionState(DatabaseWidget::Mode mode = DatabaseWidget::None); void updateWindowTitle(); private: