Multiple DatabaseTabWidget fixes.

Close databases on exit. Ask to save changes on database close.
Remove database from db list on database close.
Delete file pointer on database close.
Require to set master key at database creation time.
Append "*" to tabname if database is modified.
Handle database modified signals in database tab widget.
This commit is contained in:
Florian Geyer 2012-04-11 20:35:52 +02:00 committed by Felix Geyer
parent e39827d777
commit a058dcee5d
6 changed files with 97 additions and 11 deletions

View File

@ -19,6 +19,7 @@
#include <QtCore/QFileInfo> #include <QtCore/QFileInfo>
#include <QtGui/QTabWidget> #include <QtGui/QTabWidget>
#include <QtGui/QMessageBox>
#include "core/Database.h" #include "core/Database.h"
#include "core/Metadata.h" #include "core/Metadata.h"
@ -51,6 +52,8 @@ void DatabaseTabWidget::newDatabase()
dbStruct.dbWidget = new DatabaseWidget(db, this); dbStruct.dbWidget = new DatabaseWidget(db, this);
insertDatabase(db, dbStruct); insertDatabase(db, dbStruct);
dbStruct.dbWidget->switchToMasterKeyChange();
} }
void DatabaseTabWidget::openDatabase() void DatabaseTabWidget::openDatabase()
@ -127,22 +130,42 @@ void DatabaseTabWidget::emitEntrySelectionChanged()
Q_EMIT entrySelectionChanged(isSingleEntrySelected); Q_EMIT entrySelectionChanged(isSingleEntrySelected);
} }
void DatabaseTabWidget::closeDatabase(Database* db) bool DatabaseTabWidget::closeDatabase(Database* db)
{ {
Q_ASSERT(db); Q_ASSERT(db);
const DatabaseManagerStruct& dbStruct = m_dbList.value(db); const DatabaseManagerStruct& dbStruct = m_dbList.value(db);
if (dbStruct.modified) {
// TODO message box
}
int index = databaseIndex(db); int index = databaseIndex(db);
Q_ASSERT(index != -1); Q_ASSERT(index != -1);
if (dbStruct.modified) {
QMessageBox::StandardButton result =
QMessageBox::question(
this, tr("Save changes?"),
tr("\"%1\" was modified.\nSave changes?").arg(tabText(index)),
QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel, QMessageBox::Yes);
if (result == QMessageBox::Yes) {
saveDatabase(db);
}
else if (result == QMessageBox::Cancel) {
return false;
}
}
removeTab(index); removeTab(index);
m_dbList.remove(db);
delete dbStruct.file;
delete dbStruct.dbWidget; delete dbStruct.dbWidget;
delete db; delete db;
return true;
}
bool DatabaseTabWidget::closeAllDatabases() {
while (!m_dbList.isEmpty()) {
if (!closeDatabase()) {
return false;
}
}
return true;
} }
void DatabaseTabWidget::saveDatabase(Database* db) void DatabaseTabWidget::saveDatabase(Database* db)
@ -192,13 +215,21 @@ void DatabaseTabWidget::saveDatabaseAs(Database* db)
} }
} }
void DatabaseTabWidget::closeDatabase(int index) bool DatabaseTabWidget::closeDatabase(int index)
{ {
if (index == -1) { if (index == -1) {
index = currentIndex(); index = currentIndex();
} }
closeDatabase(indexDatabase(index)); return closeDatabase(indexDatabase(index));
}
void DatabaseTabWidget::closeDatabaseFromSender()
{
Q_ASSERT(sender());
DatabaseWidget* dbWidget = static_cast<DatabaseWidget*>(sender());
Database* db = databaseFromDatabaseWidget(dbWidget);
closeDatabase(db);
} }
void DatabaseTabWidget::saveDatabase(int index) void DatabaseTabWidget::saveDatabase(int index)
@ -272,7 +303,9 @@ void DatabaseTabWidget::updateTabName(Database* db)
tabName = QString("%1 [%2]").arg(db->metadata()->name(), tr("New database")); tabName = QString("%1 [%2]").arg(db->metadata()->name(), tr("New database"));
} }
} }
if (dbStruct.modified) {
tabName.append("*");
}
setTabText(index, tabName); setTabText(index, tabName);
} }
@ -297,6 +330,19 @@ Database* DatabaseTabWidget::indexDatabase(int index)
return 0; return 0;
} }
Database* DatabaseTabWidget::databaseFromDatabaseWidget(DatabaseWidget* dbWidget)
{
QHashIterator<Database*, DatabaseManagerStruct> i(m_dbList);
while (i.hasNext()) {
i.next();
if (i.value().dbWidget == dbWidget) {
return i.key();
}
}
return 0;
}
void DatabaseTabWidget::insertDatabase(Database* db, const DatabaseManagerStruct& dbStruct) void DatabaseTabWidget::insertDatabase(Database* db, const DatabaseManagerStruct& dbStruct)
{ {
m_dbList.insert(db, dbStruct); m_dbList.insert(db, dbStruct);
@ -308,6 +354,8 @@ void DatabaseTabWidget::insertDatabase(Database* db, const DatabaseManagerStruct
connect(db->metadata(), SIGNAL(nameTextChanged(Database*)), SLOT(updateTabName(Database*))); connect(db->metadata(), SIGNAL(nameTextChanged(Database*)), SLOT(updateTabName(Database*)));
connect(dbStruct.dbWidget->entryView(), SIGNAL(entrySelectionChanged()), SLOT(emitEntrySelectionChanged())); connect(dbStruct.dbWidget->entryView(), SIGNAL(entrySelectionChanged()), SLOT(emitEntrySelectionChanged()));
connect(dbStruct.dbWidget, SIGNAL(closeRequest()), SLOT(closeDatabase()));
connect(db, SIGNAL(modified()), SLOT(modified()));
} }
DatabaseWidget* DatabaseTabWidget::currentDatabaseWidget() DatabaseWidget* DatabaseTabWidget::currentDatabaseWidget()
@ -320,3 +368,14 @@ DatabaseWidget* DatabaseTabWidget::currentDatabaseWidget()
return 0; return 0;
} }
} }
void DatabaseTabWidget::modified()
{
Q_ASSERT(sender());
Database* db = static_cast<Database*>(sender());
DatabaseManagerStruct& dbStruct = m_dbList[db];
if (!dbStruct.modified) {
dbStruct.modified = true;
updateTabName(db);
}
}

View File

@ -54,7 +54,9 @@ public Q_SLOTS:
void openDatabase(); void openDatabase();
void saveDatabase(int index = -1); void saveDatabase(int index = -1);
void saveDatabaseAs(int index = -1); void saveDatabaseAs(int index = -1);
void closeDatabase(int index = -1); bool closeDatabase(int index = -1);
void closeDatabaseFromSender();
bool closeAllDatabases();
void changeMasterKey(); void changeMasterKey();
void createEntry(); void createEntry();
void editEntry(); void editEntry();
@ -70,13 +72,15 @@ private Q_SLOTS:
void openDatabaseRead(); void openDatabaseRead();
void openDatabaseCleanup(); void openDatabaseCleanup();
void emitEntrySelectionChanged(); void emitEntrySelectionChanged();
void modified();
private: private:
void saveDatabase(Database* db); void saveDatabase(Database* db);
void saveDatabaseAs(Database* db); void saveDatabaseAs(Database* db);
void closeDatabase(Database* db); bool closeDatabase(Database* db);
int databaseIndex(Database* db); int databaseIndex(Database* db);
Database* indexDatabase(int index); Database* indexDatabase(int index);
Database* databaseFromDatabaseWidget(DatabaseWidget* dbWidget);
void insertDatabase(Database* db, const DatabaseManagerStruct& dbStruct); void insertDatabase(Database* db, const DatabaseManagerStruct& dbStruct);
QWidget* m_window; QWidget* m_window;

View File

@ -155,6 +155,11 @@ void DatabaseWidget::updateMasterKey(bool accepted)
{ {
if (accepted) { if (accepted) {
m_db->setKey(m_changeMasterKeyWidget->newMasterKey()); m_db->setKey(m_changeMasterKeyWidget->newMasterKey());
}
else if (m_db->transformedMasterKey().isEmpty()) { // TODO other test?
Q_EMIT closeRequest();
return;
} }
setCurrentIndex(0); setCurrentIndex(0);

View File

@ -38,6 +38,9 @@ public:
GroupView* groupView(); GroupView* groupView();
EntryView* entryView(); EntryView* entryView();
Q_SIGNALS:
void closeRequest();
public Q_SLOTS: public Q_SLOTS:
void createEntry(); void createEntry();
void createGroup(); void createGroup();

View File

@ -18,6 +18,8 @@
#include "MainWindow.h" #include "MainWindow.h"
#include "ui_MainWindow.h" #include "ui_MainWindow.h"
#include <QtGui/QCloseEvent>
#include "core/Database.h" #include "core/Database.h"
#include "core/DataPath.h" #include "core/DataPath.h"
#include "core/Metadata.h" #include "core/Metadata.h"
@ -63,3 +65,13 @@ void MainWindow::currentTabChanged(int index)
m_ui->actionGroupEdit->setEnabled(hasTab); m_ui->actionGroupEdit->setEnabled(hasTab);
m_ui->actionChangeMasterKey->setEnabled(hasTab); m_ui->actionChangeMasterKey->setEnabled(hasTab);
} }
void MainWindow::closeEvent(QCloseEvent *event) {
if (!m_ui->tabWidget->closeAllDatabases()) {
event->ignore();
}
else {
event->accept();
}
}

View File

@ -32,6 +32,9 @@ public:
MainWindow(); MainWindow();
~MainWindow(); ~MainWindow();
protected:
void closeEvent(QCloseEvent *event);
private Q_SLOTS: private Q_SLOTS:
void currentTabChanged(int index); void currentTabChanged(int index);