Rename DatabaseManager to DatabaseTabWidget and turn it into a QTabWidget.

This commit is contained in:
Felix Geyer 2011-12-30 18:43:24 +01:00
parent 752c720226
commit 4cb8cffe3f
6 changed files with 67 additions and 65 deletions

View File

@ -46,7 +46,7 @@ set(keepassx_SOURCES
format/KeePass2Writer.cpp format/KeePass2Writer.cpp
format/KeePass2XmlReader.cpp format/KeePass2XmlReader.cpp
format/KeePass2XmlWriter.cpp format/KeePass2XmlWriter.cpp
gui/DatabaseManager.cpp gui/DatabaseTabWidget.cpp
gui/DatabaseWidget.cpp gui/DatabaseWidget.cpp
gui/EditEntryWidget.cpp gui/EditEntryWidget.cpp
gui/EditGroupWidget.cpp gui/EditGroupWidget.cpp
@ -72,7 +72,7 @@ set(keepassx_MOC
core/Entry.h core/Entry.h
core/Group.h core/Group.h
core/Metadata.h core/Metadata.h
gui/DatabaseManager.h gui/DatabaseTabWidget.h
gui/DatabaseWidget.h gui/DatabaseWidget.h
gui/EditEntryWidget.h gui/EditEntryWidget.h
gui/EditGroupWidget.h gui/EditGroupWidget.h

View File

@ -15,7 +15,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "DatabaseManager.h" #include "DatabaseTabWidget.h"
#include <QtCore/QFileInfo> #include <QtCore/QFileInfo>
#include <QtGui/QTabWidget> #include <QtGui/QTabWidget>
@ -37,24 +37,24 @@ DatabaseManagerStruct::DatabaseManagerStruct()
{ {
} }
DatabaseManager::DatabaseManager(QTabWidget* tabWidget) DatabaseTabWidget::DatabaseTabWidget(QWidget* parent)
: m_tabWidget(tabWidget) : QTabWidget(parent)
, m_window(tabWidget->window()) , m_window(parent->window())
{ {
connect(m_tabWidget, SIGNAL(tabCloseRequested(int)), SLOT(closeDatabase(int))); connect(this, SIGNAL(tabCloseRequested(int)), SLOT(closeDatabase(int)));
connect(m_tabWidget, SIGNAL(currentChanged(int)), SLOT(emitEntrySelectionChanged())); connect(this, SIGNAL(currentChanged(int)), SLOT(emitEntrySelectionChanged()));
} }
void DatabaseManager::newDatabase() void DatabaseTabWidget::newDatabase()
{ {
DatabaseManagerStruct dbStruct; DatabaseManagerStruct dbStruct;
Database* db = new Database(); Database* db = new Database();
dbStruct.dbWidget = new DatabaseWidget(db, m_tabWidget); dbStruct.dbWidget = new DatabaseWidget(db, this);
insertDatabase(db, dbStruct); insertDatabase(db, dbStruct);
} }
void DatabaseManager::openDatabase() void DatabaseTabWidget::openDatabase()
{ {
QString fileName = fileDialog()->getOpenFileName(m_window, tr("Open database"), QString(), QString fileName = fileDialog()->getOpenFileName(m_window, tr("Open database"), QString(),
tr("KeePass 2 Database").append(" (*.kdbx)")); tr("KeePass 2 Database").append(" (*.kdbx)"));
@ -63,7 +63,7 @@ void DatabaseManager::openDatabase()
} }
} }
void DatabaseManager::openDatabase(const QString& fileName) void DatabaseTabWidget::openDatabase(const QString& fileName)
{ {
QScopedPointer<QFile> file(new QFile(fileName)); QScopedPointer<QFile> file(new QFile(fileName));
// TODO error handling // TODO error handling
@ -84,7 +84,7 @@ void DatabaseManager::openDatabase(const QString& fileName)
openDatabaseDialog(); openDatabaseDialog();
} }
void DatabaseManager::openDatabaseDialog() void DatabaseTabWidget::openDatabaseDialog()
{ {
m_curKeyDialog = new KeyOpenDialog(m_curDbStruct.fileName, m_window); m_curKeyDialog = new KeyOpenDialog(m_curDbStruct.fileName, m_window);
connect(m_curKeyDialog, SIGNAL(accepted()), SLOT(openDatabaseRead())); connect(m_curKeyDialog, SIGNAL(accepted()), SLOT(openDatabaseRead()));
@ -93,7 +93,7 @@ void DatabaseManager::openDatabaseDialog()
m_curKeyDialog->show(); m_curKeyDialog->show();
} }
void DatabaseManager::openDatabaseRead() void DatabaseTabWidget::openDatabaseRead()
{ {
m_curDbStruct.file->reset(); m_curDbStruct.file->reset();
Database* db = m_reader.readDatabase(m_curDbStruct.file, m_curKeyDialog->key()); Database* db = m_reader.readDatabase(m_curDbStruct.file, m_curKeyDialog->key());
@ -104,13 +104,13 @@ void DatabaseManager::openDatabaseRead()
openDatabaseDialog(); openDatabaseDialog();
} }
else { else {
m_curDbStruct.dbWidget = new DatabaseWidget(db, m_tabWidget); m_curDbStruct.dbWidget = new DatabaseWidget(db, this);
insertDatabase(db, m_curDbStruct); insertDatabase(db, m_curDbStruct);
m_curDbStruct = DatabaseManagerStruct(); m_curDbStruct = DatabaseManagerStruct();
} }
} }
void DatabaseManager::openDatabaseCleanup() void DatabaseTabWidget::openDatabaseCleanup()
{ {
delete m_curKeyDialog; delete m_curKeyDialog;
m_curKeyDialog = 0; m_curKeyDialog = 0;
@ -121,7 +121,7 @@ void DatabaseManager::openDatabaseCleanup()
m_curDbStruct = DatabaseManagerStruct(); m_curDbStruct = DatabaseManagerStruct();
} }
void DatabaseManager::emitEntrySelectionChanged() void DatabaseTabWidget::emitEntrySelectionChanged()
{ {
DatabaseWidget* dbWidget = currentDatabaseWidget(); DatabaseWidget* dbWidget = currentDatabaseWidget();
@ -133,7 +133,7 @@ void DatabaseManager::emitEntrySelectionChanged()
Q_EMIT entrySelectionChanged(isSingleEntrySelected); Q_EMIT entrySelectionChanged(isSingleEntrySelected);
} }
void DatabaseManager::closeDatabase(Database* db) void DatabaseTabWidget::closeDatabase(Database* db)
{ {
Q_ASSERT(db); Q_ASSERT(db);
@ -146,12 +146,12 @@ void DatabaseManager::closeDatabase(Database* db)
int index = databaseIndex(db); int index = databaseIndex(db);
Q_ASSERT(index != -1); Q_ASSERT(index != -1);
m_tabWidget->removeTab(index); removeTab(index);
delete dbStruct.dbWidget; delete dbStruct.dbWidget;
delete db; delete db;
} }
void DatabaseManager::saveDatabase(Database* db) void DatabaseTabWidget::saveDatabase(Database* db)
{ {
DatabaseManagerStruct& dbStruct = m_dbList[db]; DatabaseManagerStruct& dbStruct = m_dbList[db];
@ -170,7 +170,7 @@ void DatabaseManager::saveDatabase(Database* db)
} }
} }
void DatabaseManager::saveDatabaseAs(Database* db) void DatabaseTabWidget::saveDatabaseAs(Database* db)
{ {
DatabaseManagerStruct& dbStruct = m_dbList[db]; DatabaseManagerStruct& dbStruct = m_dbList[db];
QString oldFileName; QString oldFileName;
@ -197,53 +197,53 @@ void DatabaseManager::saveDatabaseAs(Database* db)
} }
} }
void DatabaseManager::closeDatabase(int index) void DatabaseTabWidget::closeDatabase(int index)
{ {
if (index == -1) { if (index == -1) {
index = m_tabWidget->currentIndex(); index = currentIndex();
} }
closeDatabase(indexDatabase(index)); closeDatabase(indexDatabase(index));
} }
void DatabaseManager::saveDatabase(int index) void DatabaseTabWidget::saveDatabase(int index)
{ {
if (index == -1) { if (index == -1) {
index = m_tabWidget->currentIndex(); index = currentIndex();
} }
saveDatabase(indexDatabase(index)); saveDatabase(indexDatabase(index));
} }
void DatabaseManager::saveDatabaseAs(int index) void DatabaseTabWidget::saveDatabaseAs(int index)
{ {
if (index == -1) { if (index == -1) {
index = m_tabWidget->currentIndex(); index = currentIndex();
} }
saveDatabaseAs(indexDatabase(index)); saveDatabaseAs(indexDatabase(index));
} }
void DatabaseManager::createEntry() void DatabaseTabWidget::createEntry()
{ {
currentDatabaseWidget()->createEntry(); currentDatabaseWidget()->createEntry();
} }
void DatabaseManager::editEntry() void DatabaseTabWidget::editEntry()
{ {
currentDatabaseWidget()->switchToEntryEdit(); currentDatabaseWidget()->switchToEntryEdit();
} }
void DatabaseManager::createGroup() void DatabaseTabWidget::createGroup()
{ {
currentDatabaseWidget()->createGroup(); currentDatabaseWidget()->createGroup();
} }
void DatabaseManager::editGroup() void DatabaseTabWidget::editGroup()
{ {
currentDatabaseWidget()->switchToGroupEdit(); currentDatabaseWidget()->switchToGroupEdit();
} }
void DatabaseManager::updateTabName(Database* db) void DatabaseTabWidget::updateTabName(Database* db)
{ {
int index = databaseIndex(db); int index = databaseIndex(db);
Q_ASSERT(index != -1); Q_ASSERT(index != -1);
@ -262,7 +262,7 @@ void DatabaseManager::updateTabName(Database* db)
tabName = db->metadata()->name(); tabName = db->metadata()->name();
} }
m_tabWidget->setTabToolTip(index, dbStruct.fileName); setTabToolTip(index, dbStruct.fileName);
} }
else { else {
if (db->metadata()->name().isEmpty()) { if (db->metadata()->name().isEmpty()) {
@ -273,18 +273,18 @@ void DatabaseManager::updateTabName(Database* db)
} }
} }
m_tabWidget->setTabText(index, tabName); setTabText(index, tabName);
} }
int DatabaseManager::databaseIndex(Database* db) int DatabaseTabWidget::databaseIndex(Database* db)
{ {
QWidget* dbWidget = m_dbList.value(db).dbWidget; QWidget* dbWidget = m_dbList.value(db).dbWidget;
return m_tabWidget->indexOf(dbWidget); return indexOf(dbWidget);
} }
Database* DatabaseManager::indexDatabase(int index) Database* DatabaseTabWidget::indexDatabase(int index)
{ {
QWidget* dbWidget = m_tabWidget->widget(index); QWidget* dbWidget = widget(index);
QHashIterator<Database*, DatabaseManagerStruct> i(m_dbList); QHashIterator<Database*, DatabaseManagerStruct> i(m_dbList);
while (i.hasNext()) { while (i.hasNext()) {
@ -297,22 +297,22 @@ Database* DatabaseManager::indexDatabase(int index)
return 0; return 0;
} }
void DatabaseManager::insertDatabase(Database* db, const DatabaseManagerStruct& dbStruct) void DatabaseTabWidget::insertDatabase(Database* db, const DatabaseManagerStruct& dbStruct)
{ {
m_dbList.insert(db, dbStruct); m_dbList.insert(db, dbStruct);
m_tabWidget->addTab(dbStruct.dbWidget, ""); addTab(dbStruct.dbWidget, "");
updateTabName(db); updateTabName(db);
int index = databaseIndex(db); int index = databaseIndex(db);
m_tabWidget->setCurrentIndex(index); setCurrentIndex(index);
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()));
} }
DatabaseWidget* DatabaseManager::currentDatabaseWidget() DatabaseWidget* DatabaseTabWidget::currentDatabaseWidget()
{ {
Database* db = indexDatabase(m_tabWidget->currentIndex()); Database* db = indexDatabase(currentIndex());
if (db) { if (db) {
return m_dbList[db].dbWidget; return m_dbList[db].dbWidget;
} }

View File

@ -20,6 +20,7 @@
#include <QtCore/QHash> #include <QtCore/QHash>
#include <QtCore/QObject> #include <QtCore/QObject>
#include <QtGui/QTabWidget>
#include "format/KeePass2Reader.h" #include "format/KeePass2Reader.h"
#include "format/KeePass2Writer.h" #include "format/KeePass2Writer.h"
@ -40,12 +41,12 @@ struct DatabaseManagerStruct
bool readOnly; bool readOnly;
}; };
class DatabaseManager : public QObject class DatabaseTabWidget : public QTabWidget
{ {
Q_OBJECT Q_OBJECT
public: public:
DatabaseManager(QTabWidget* tabWidget); DatabaseTabWidget(QWidget* parent);
void openDatabase(const QString& fileName); void openDatabase(const QString& fileName);
void saveDatabase(Database* db); void saveDatabase(Database* db);
void saveDatabaseAs(Database* db); void saveDatabaseAs(Database* db);
@ -78,7 +79,6 @@ private:
Database* indexDatabase(int index); Database* indexDatabase(int index);
void insertDatabase(Database* db, const DatabaseManagerStruct& dbStruct); void insertDatabase(Database* db, const DatabaseManagerStruct& dbStruct);
QTabWidget* m_tabWidget;
QWidget* m_window; QWidget* m_window;
KeePass2Reader m_reader; KeePass2Reader m_reader;
KeePass2Writer m_writer; KeePass2Writer m_writer;

View File

@ -20,7 +20,6 @@
#include "core/Database.h" #include "core/Database.h"
#include "core/Metadata.h" #include "core/Metadata.h"
#include "gui/DatabaseManager.h"
#include "gui/DatabaseWidget.h" #include "gui/DatabaseWidget.h"
MainWindow::MainWindow() MainWindow::MainWindow()
@ -28,21 +27,19 @@ MainWindow::MainWindow()
{ {
m_ui->setupUi(this); m_ui->setupUi(this);
m_dbManager = new DatabaseManager(m_ui->tabWidget);
connect(m_ui->tabWidget, SIGNAL(currentChanged(int)), SLOT(currentTabChanged(int))); connect(m_ui->tabWidget, SIGNAL(currentChanged(int)), SLOT(currentTabChanged(int)));
connect(m_dbManager, SIGNAL(entrySelectionChanged(bool)), m_ui->actionEntryEdit, SLOT(setEnabled(bool))); connect(m_ui->tabWidget, SIGNAL(entrySelectionChanged(bool)), m_ui->actionEntryEdit, SLOT(setEnabled(bool)));
connect(m_dbManager, SIGNAL(entrySelectionChanged(bool)), m_ui->actionEntryDelete, SLOT(setEnabled(bool))); connect(m_ui->tabWidget, SIGNAL(entrySelectionChanged(bool)), m_ui->actionEntryDelete, SLOT(setEnabled(bool)));
connect(m_ui->actionDatabaseNew, SIGNAL(triggered()), m_dbManager, SLOT(newDatabase())); connect(m_ui->actionDatabaseNew, SIGNAL(triggered()), m_ui->tabWidget, SLOT(newDatabase()));
connect(m_ui->actionDatabaseOpen, SIGNAL(triggered()), m_dbManager, SLOT(openDatabase())); connect(m_ui->actionDatabaseOpen, SIGNAL(triggered()), m_ui->tabWidget, SLOT(openDatabase()));
connect(m_ui->actionDatabaseSave, SIGNAL(triggered()), m_dbManager, SLOT(saveDatabase())); connect(m_ui->actionDatabaseSave, SIGNAL(triggered()), m_ui->tabWidget, SLOT(saveDatabase()));
connect(m_ui->actionDatabaseSaveAs, SIGNAL(triggered()), m_dbManager, SLOT(saveDatabaseAs())); connect(m_ui->actionDatabaseSaveAs, SIGNAL(triggered()), m_ui->tabWidget, SLOT(saveDatabaseAs()));
connect(m_ui->actionDatabaseClose, SIGNAL(triggered()), m_dbManager, SLOT(closeDatabase())); connect(m_ui->actionDatabaseClose, SIGNAL(triggered()), m_ui->tabWidget, SLOT(closeDatabase()));
connect(m_ui->actionEntryNew, SIGNAL(triggered()), m_dbManager, SLOT(createEntry())); connect(m_ui->actionEntryNew, SIGNAL(triggered()), m_ui->tabWidget, SLOT(createEntry()));
connect(m_ui->actionEntryEdit, SIGNAL(triggered()), m_dbManager, SLOT(editEntry())); connect(m_ui->actionEntryEdit, SIGNAL(triggered()), m_ui->tabWidget, SLOT(editEntry()));
connect(m_ui->actionGroupNew, SIGNAL(triggered()), m_dbManager, SLOT(createGroup())); connect(m_ui->actionGroupNew, SIGNAL(triggered()), m_ui->tabWidget, SLOT(createGroup()));
connect(m_ui->actionGroupEdit, SIGNAL(triggered()), m_dbManager, SLOT(editGroup())); connect(m_ui->actionGroupEdit, SIGNAL(triggered()), m_ui->tabWidget, SLOT(editGroup()));
connect(m_ui->actionQuit, SIGNAL(triggered()), SLOT(close())); connect(m_ui->actionQuit, SIGNAL(triggered()), SLOT(close()));
} }

View File

@ -20,8 +20,6 @@
#include <QtGui/QMainWindow> #include <QtGui/QMainWindow>
class Database;
class DatabaseManager;
namespace Ui { namespace Ui {
class MainWindow; class MainWindow;
} }
@ -39,7 +37,6 @@ private Q_SLOTS:
private: private:
QScopedPointer<Ui::MainWindow> m_ui; QScopedPointer<Ui::MainWindow> m_ui;
DatabaseManager* m_dbManager;
Q_DISABLE_COPY(MainWindow) Q_DISABLE_COPY(MainWindow)
}; };

View File

@ -16,7 +16,7 @@
<widget class="QWidget" name="centralwidget"> <widget class="QWidget" name="centralwidget">
<layout class="QVBoxLayout" name="verticalLayout"> <layout class="QVBoxLayout" name="verticalLayout">
<item> <item>
<widget class="QTabWidget" name="tabWidget"> <widget class="DatabaseTabWidget" name="tabWidget">
<property name="currentIndex"> <property name="currentIndex">
<number>-1</number> <number>-1</number>
</property> </property>
@ -36,7 +36,7 @@
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>800</width> <width>800</width>
<height>21</height> <height>20</height>
</rect> </rect>
</property> </property>
<widget class="QMenu" name="menuFile"> <widget class="QMenu" name="menuFile">
@ -188,6 +188,14 @@
</property> </property>
</action> </action>
</widget> </widget>
<customwidgets>
<customwidget>
<class>DatabaseTabWidget</class>
<extends>QTabWidget</extends>
<header>gui/DatabaseTabWidget.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<resources/> <resources/>
<connections/> <connections/>
</ui> </ui>