From fec19b475e1236a6aa2125c5a25f8fc2958500a9 Mon Sep 17 00:00:00 2001 From: Florian Geyer Date: Mon, 16 Apr 2012 21:28:49 +0200 Subject: [PATCH] Disable/enable menu actions depending on what currently can be done. --- src/gui/DatabaseTabWidget.cpp | 1 + src/gui/DatabaseTabWidget.h | 1 + src/gui/DatabaseWidget.cpp | 5 +++ src/gui/DatabaseWidget.h | 2 + src/gui/EntryView.cpp | 1 + src/gui/MainWindow.cpp | 85 ++++++++++++++++++++++++++++++----- src/gui/MainWindow.h | 3 +- 7 files changed, 85 insertions(+), 13 deletions(-) diff --git a/src/gui/DatabaseTabWidget.cpp b/src/gui/DatabaseTabWidget.cpp index da1264346..f607c3a93 100644 --- a/src/gui/DatabaseTabWidget.cpp +++ b/src/gui/DatabaseTabWidget.cpp @@ -360,6 +360,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(currentIndexChanged(int)), this, SIGNAL(currentWidgetIndexChanged(int))); } DatabaseWidget* DatabaseTabWidget::currentDatabaseWidget() diff --git a/src/gui/DatabaseTabWidget.h b/src/gui/DatabaseTabWidget.h index 4f2dab161..143356537 100644 --- a/src/gui/DatabaseTabWidget.h +++ b/src/gui/DatabaseTabWidget.h @@ -65,6 +65,7 @@ public Q_SLOTS: Q_SIGNALS: void entrySelectionChanged(bool singleEntrySelected); + void currentWidgetIndexChanged(int index); private Q_SLOTS: void updateTabName(Database* db); diff --git a/src/gui/DatabaseWidget.cpp b/src/gui/DatabaseWidget.cpp index ef263270e..f9893bc15 100644 --- a/src/gui/DatabaseWidget.cpp +++ b/src/gui/DatabaseWidget.cpp @@ -185,6 +185,11 @@ void DatabaseWidget::switchToMasterKeyChange() setCurrentIndex(3); } +void DatabaseWidget::setCurrentIndex(int index) { + QStackedWidget::setCurrentIndex(index); + Q_EMIT currentIndexChanged(index); +} + bool DatabaseWidget::dbHasKey() { return m_db->hasKey(); diff --git a/src/gui/DatabaseWidget.h b/src/gui/DatabaseWidget.h index 64e54f9b0..563e3e1c9 100644 --- a/src/gui/DatabaseWidget.h +++ b/src/gui/DatabaseWidget.h @@ -41,6 +41,7 @@ public: Q_SIGNALS: void closeRequest(); + void currentIndexChanged(int index); public Q_SLOTS: void createEntry(); @@ -48,6 +49,7 @@ public Q_SLOTS: void switchToEntryEdit(); void switchToGroupEdit(); void switchToMasterKeyChange(); + void setCurrentIndex(int index); private Q_SLOTS: void switchToView(bool accepted); diff --git a/src/gui/EntryView.cpp b/src/gui/EntryView.cpp index 2c4695e29..2e202dbd3 100644 --- a/src/gui/EntryView.cpp +++ b/src/gui/EntryView.cpp @@ -35,6 +35,7 @@ EntryView::EntryView(QWidget* parent) void EntryView::setGroup(Group* group) { m_model->setGroup(group); + Q_EMIT entrySelectionChanged(); } void EntryView::emitEntryActivated(const QModelIndex& index) diff --git a/src/gui/MainWindow.cpp b/src/gui/MainWindow.cpp index f9375b078..8273317c9 100644 --- a/src/gui/MainWindow.cpp +++ b/src/gui/MainWindow.cpp @@ -24,6 +24,7 @@ #include "core/DataPath.h" #include "core/Metadata.h" #include "gui/DatabaseWidget.h" +#include "gui/EntryView.h" MainWindow::MainWindow() : m_ui(new Ui::MainWindow()) @@ -32,9 +33,9 @@ MainWindow::MainWindow() setWindowIcon(dataPath()->applicationIcon()); - connect(m_ui->tabWidget, SIGNAL(currentChanged(int)), SLOT(currentTabChanged(int))); - connect(m_ui->tabWidget, SIGNAL(entrySelectionChanged(bool)), m_ui->actionEntryEdit, SLOT(setEnabled(bool))); - connect(m_ui->tabWidget, SIGNAL(entrySelectionChanged(bool)), m_ui->actionEntryDelete, SLOT(setEnabled(bool))); + connect(m_ui->tabWidget, SIGNAL(currentChanged(int)), SLOT(setMenuActionState())); + connect(m_ui->tabWidget, SIGNAL(entrySelectionChanged(bool)), SLOT(setMenuActionState())); + connect(m_ui->tabWidget, SIGNAL(currentWidgetIndexChanged(int)), SLOT(setMenuActionState(int))); connect(m_ui->actionDatabaseNew, SIGNAL(triggered()), m_ui->tabWidget, SLOT(newDatabase())); connect(m_ui->actionDatabaseOpen, SIGNAL(triggered()), m_ui->tabWidget, SLOT(openDatabase())); @@ -53,17 +54,75 @@ MainWindow::~MainWindow() { } -void MainWindow::currentTabChanged(int index) +void MainWindow::setMenuActionState(int index) { - bool hasTab = (index != -1); + if (m_ui->tabWidget->currentIndex() != -1) + { + m_ui->actionDatabaseClose->setEnabled(true); + DatabaseWidget* dbWidget = m_ui->tabWidget->currentDatabaseWidget(); + Q_ASSERT(dbWidget); - m_ui->actionDatabaseSave->setEnabled(hasTab); - m_ui->actionDatabaseSaveAs->setEnabled(hasTab); - m_ui->actionDatabaseClose->setEnabled(hasTab); - m_ui->actionEntryNew->setEnabled(hasTab); - m_ui->actionGroupNew->setEnabled(hasTab); - m_ui->actionGroupEdit->setEnabled(hasTab); - m_ui->actionChangeMasterKey->setEnabled(hasTab); + if (index == -1) { + index = dbWidget->currentIndex(); + } + + switch(index) { + case 0: // view + m_ui->actionEntryNew->setEnabled(true); + m_ui->actionGroupNew->setEnabled(true); + if (dbWidget->entryView()->currentIndex().isValid()) { + m_ui->actionEntryEdit->setEnabled(true); + m_ui->actionEntryDelete->setEnabled(true); + } + else { + m_ui->actionEntryEdit->setEnabled(false); + m_ui->actionEntryDelete->setEnabled(false); + } + m_ui->actionGroupEdit->setEnabled(true); + // TODO + /* + if () { //check if root group selected + m_ui->actiocGroupDelete->setEnabled(true); + } + else { + m_ui->actiocGroupDelete->setEnabled(false); + } + */ + m_ui->actionChangeMasterKey->setEnabled(true); + m_ui->actionDatabaseSave->setEnabled(true); + m_ui->actionDatabaseSaveAs->setEnabled(true); + break; + case 1: // entry edit + case 2: // group edit + case 3: // change master key + m_ui->actionEntryNew->setEnabled(false); + m_ui->actionGroupNew->setEnabled(false); + m_ui->actionEntryEdit->setEnabled(false); + m_ui->actionGroupEdit->setEnabled(false); + m_ui->actionEntryDelete->setEnabled(false); + m_ui->actiocGroupDelete->setEnabled(false); + m_ui->actionChangeMasterKey->setEnabled(false); + m_ui->actionDatabaseSave->setEnabled(false); + m_ui->actionDatabaseSaveAs->setEnabled(false); + break; + default: + Q_ASSERT(false); + } + m_ui->actionDatabaseClose->setEnabled(true); + } + else { + m_ui->actionEntryNew->setEnabled(false); + m_ui->actionGroupNew->setEnabled(false); + m_ui->actionEntryEdit->setEnabled(false); + m_ui->actionGroupEdit->setEnabled(false); + m_ui->actionEntryDelete->setEnabled(false); + m_ui->actiocGroupDelete->setEnabled(false); + m_ui->actionChangeMasterKey->setEnabled(false); + m_ui->actionDatabaseSave->setEnabled(false); + m_ui->actionDatabaseSaveAs->setEnabled(false); + + m_ui->actionDatabaseClose->setEnabled(false); + } } void MainWindow::closeEvent(QCloseEvent *event) { @@ -75,3 +134,5 @@ void MainWindow::closeEvent(QCloseEvent *event) { } } + + diff --git a/src/gui/MainWindow.h b/src/gui/MainWindow.h index 42f4ba3c0..b19067c36 100644 --- a/src/gui/MainWindow.h +++ b/src/gui/MainWindow.h @@ -36,7 +36,8 @@ protected: void closeEvent(QCloseEvent *event); private Q_SLOTS: - void currentTabChanged(int index); + void setMenuActionState(int index = -1); + private: QScopedPointer m_ui;