New actions: new, save and close database.

This commit is contained in:
Felix Geyer 2011-11-16 18:47:17 +01:00
parent 0ba03920df
commit 5df933be25
5 changed files with 113 additions and 16 deletions

View File

@ -42,6 +42,15 @@ DatabaseManager::DatabaseManager(QTabWidget* tabWidget)
connect(m_tabWidget, SIGNAL(tabCloseRequested(int)), SLOT(closeDatabase(int)));
}
void DatabaseManager::newDatabase()
{
DatabaseManagerStruct dbStruct;
Database* db = new Database();
dbStruct.dbWidget = new DatabaseWidget(db, m_tabWidget);
insertDatabase(db, dbStruct);
}
void DatabaseManager::openDatabase()
{
QString fileName = QFileDialog::getOpenFileName(m_window, tr("Open database"), QString(),
@ -82,12 +91,7 @@ void DatabaseManager::openDatabase(const QString& fileName)
dbStruct.file = file.take();
dbStruct.dbWidget = new DatabaseWidget(db, m_tabWidget);
m_dbList.insert(db, dbStruct);
m_tabWidget->addTab(dbStruct.dbWidget, "");
updateTabName(db);
connect(db->metadata(), SIGNAL(nameTextChanged(Database*)), SLOT(updateTabName(Database*)));
insertDatabase(db, dbStruct);
}
void DatabaseManager::closeDatabase(Database* db)
@ -108,11 +112,38 @@ void DatabaseManager::closeDatabase(Database* db)
delete db;
}
void DatabaseManager::saveDatabase(Database* db)
{
DatabaseManagerStruct& dbStruct = m_dbList[db];
// TODO ensure that the data is actually written to disk
dbStruct.file->reset();
m_writer.writeDatabase(dbStruct.file, db);
dbStruct.file->resize(dbStruct.file->pos());
dbStruct.file->flush();
dbStruct.modified = false;
updateTabName(db);
}
void DatabaseManager::closeDatabase(int index)
{
if (index == -1) {
index = m_tabWidget->currentIndex();
}
closeDatabase(indexDatabase(index));
}
void DatabaseManager::saveDatabase(int index)
{
if (index == -1) {
index = m_tabWidget->currentIndex();
}
saveDatabase(indexDatabase(index));
}
void DatabaseManager::updateTabName(Database* db)
{
int index = databaseIndex(db);
@ -120,15 +151,27 @@ void DatabaseManager::updateTabName(Database* db)
const DatabaseManagerStruct& dbStruct = m_dbList.value(db);
QString tabName;
if (dbStruct.file) {
QString filename = QFileInfo(*dbStruct.file).completeBaseName();
QString tabName;
if (db->metadata()->name().isEmpty()) {
tabName = filename;
}
else {
tabName = QString("%1 [%2]").arg(db->metadata()->name().arg(filename));
}
}
else {
if (db->metadata()->name().isEmpty()) {
tabName = tr("New database");
}
else {
tabName = QString("%1 [%2]").arg(db->metadata()->name().arg(tr("New database")));
}
}
m_tabWidget->setTabText(index, tabName);
}
@ -153,3 +196,13 @@ Database* DatabaseManager::indexDatabase(int index)
return 0;
}
void DatabaseManager::insertDatabase(Database* db, const DatabaseManagerStruct& dbStruct)
{
m_dbList.insert(db, dbStruct);
m_tabWidget->addTab(dbStruct.dbWidget, "");
updateTabName(db);
connect(db->metadata(), SIGNAL(nameTextChanged(Database*)), SLOT(updateTabName(Database*)));
}

View File

@ -44,11 +44,14 @@ class DatabaseManager : public QObject
public:
DatabaseManager(QTabWidget* tabWidget);
void openDatabase(const QString& fileName);
void saveDatabase(Database* db);
void closeDatabase(Database* db);
public Q_SLOTS:
void newDatabase();
void openDatabase();
void closeDatabase(int index);
void saveDatabase(int index = -1);
void closeDatabase(int index = -1);
private Q_SLOTS:
void updateTabName(Database* db);
@ -56,6 +59,7 @@ private Q_SLOTS:
private:
int databaseIndex(Database* db);
Database* indexDatabase(int index);
void insertDatabase(Database* db, const DatabaseManagerStruct& dbStruct);
QTabWidget* m_tabWidget;
QWidget* m_window;

View File

@ -28,6 +28,19 @@ MainWindow::MainWindow()
m_dbManager = new DatabaseManager(tabWidget);
connect(actionOpenDatabase, SIGNAL(triggered()), m_dbManager, SLOT(openDatabase()));
connect(tabWidget, SIGNAL(currentChanged(int)), SLOT(currentTabChanged(int)));
connect(actionDatabaseNew, SIGNAL(triggered()), m_dbManager, SLOT(newDatabase()));
connect(actionDatabaseOpen, SIGNAL(triggered()), m_dbManager, SLOT(openDatabase()));
connect(actionDatabaseSave, SIGNAL(triggered()), m_dbManager, SLOT(saveDatabase()));
connect(actionDatabaseClose, SIGNAL(triggered()), m_dbManager, SLOT(closeDatabase()));
connect(actionQuit, SIGNAL(triggered()), SLOT(close()));
}
void MainWindow::currentTabChanged(int index)
{
bool hasTab = (index != -1);
actionDatabaseSave->setEnabled(hasTab);
actionDatabaseClose->setEnabled(hasTab);
}

View File

@ -30,6 +30,9 @@ class MainWindow : public QMainWindow, private Ui::MainWindow
public:
MainWindow();
private Q_SLOTS:
void currentTabChanged(int index);
private:
DatabaseManager* m_dbManager;
};

View File

@ -41,9 +41,12 @@
</property>
<widget class="QMenu" name="menuFile">
<property name="title">
<string>File</string>
<string>Database</string>
</property>
<addaction name="actionOpenDatabase"/>
<addaction name="actionDatabaseNew"/>
<addaction name="actionDatabaseOpen"/>
<addaction name="actionDatabaseSave"/>
<addaction name="actionDatabaseClose"/>
<addaction name="separator"/>
<addaction name="actionQuit"/>
</widget>
@ -67,11 +70,32 @@
<string>About</string>
</property>
</action>
<action name="actionOpenDatabase">
<action name="actionDatabaseOpen">
<property name="text">
<string>Open database</string>
</property>
</action>
<action name="actionDatabaseSave">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>Save database</string>
</property>
</action>
<action name="actionDatabaseClose">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>Close database</string>
</property>
</action>
<action name="actionDatabaseNew">
<property name="text">
<string>New database</string>
</property>
</action>
</widget>
<resources/>
<connections/>