diff --git a/src/EditDialog.cpp b/src/EditDialog.cpp index e1538cbe..732f3f07 100644 --- a/src/EditDialog.cpp +++ b/src/EditDialog.cpp @@ -5,6 +5,7 @@ #include #include "sqlitedb.h" #include +#include "PreferencesDialog.h" EditDialog::EditDialog(QWidget* parent) : QDialog(parent), @@ -70,7 +71,7 @@ void EditDialog::importData() QString fileName = QFileDialog::getOpenFileName( this, tr("Choose a file"), - defaultlocation, + PreferencesDialog::getSettingsValue("db", "defaultlocation").toString(), tr("Text files(*.txt);;Image files(%1);;All files(*)").arg(image_formats)); if(QFile::exists(fileName)) { @@ -91,7 +92,7 @@ void EditDialog::exportData() QString fileName = QFileDialog::getSaveFileName( this, tr("Choose a filename to export data"), - defaultlocation, + PreferencesDialog::getSettingsValue("db", "defaultlocation").toString(), tr("Text files(*.txt);;All files(*)")); if(fileName.size() > 0) diff --git a/src/EditDialog.h b/src/EditDialog.h index 5b95cffa..7dd7bfa8 100644 --- a/src/EditDialog.h +++ b/src/EditDialog.h @@ -16,8 +16,6 @@ public: explicit EditDialog(QWidget* parent = 0); ~EditDialog(); - QString defaultlocation; - public: int getCurrentCol() { return curCol; } int getCurrentRow() { return curRow; } diff --git a/src/ExportCsvDialog.cpp b/src/ExportCsvDialog.cpp index 30cb29ba..adeb053a 100644 --- a/src/ExportCsvDialog.cpp +++ b/src/ExportCsvDialog.cpp @@ -5,12 +5,12 @@ #include "ExportCsvDialog.h" #include "ui_ExportCsvDialog.h" #include "sqlitedb.h" +#include "PreferencesDialog.h" -ExportCsvDialog::ExportCsvDialog(DBBrowserDB* db, const QString& deflocation, QWidget* parent) +ExportCsvDialog::ExportCsvDialog(DBBrowserDB* db, QWidget* parent) : QDialog(parent), ui(new Ui::ExportCsvDialog), - pdb(db), - defaultLocation(deflocation) + pdb(db) { // Create UI ui->setupUi(this); @@ -32,7 +32,7 @@ void ExportCsvDialog::accept() QString fileName = QFileDialog::getSaveFileName( this, tr("Choose a filename to export data"), - defaultLocation, + PreferencesDialog::getSettingsValue("db", "defaultlocation").toString(), tr("Text files(*.csv *.txt)")); // Only if the user hasn't clicked the cancel button diff --git a/src/ExportCsvDialog.h b/src/ExportCsvDialog.h index aae69395..14cc7dab 100644 --- a/src/ExportCsvDialog.h +++ b/src/ExportCsvDialog.h @@ -13,7 +13,7 @@ class ExportCsvDialog : public QDialog Q_OBJECT public: - explicit ExportCsvDialog(DBBrowserDB* db, const QString& deflocation, QWidget* parent = 0); + explicit ExportCsvDialog(DBBrowserDB* db, QWidget* parent = 0); ~ExportCsvDialog(); private slots: @@ -22,7 +22,6 @@ private slots: private: Ui::ExportCsvDialog* ui; DBBrowserDB* pdb; - QString defaultLocation; }; #endif diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp index 9f227bd8..a6731373 100644 --- a/src/MainWindow.cpp +++ b/src/MainWindow.cpp @@ -1,7 +1,6 @@ #include "MainWindow.h" #include "ui_MainWindow.h" -#include -#include +#include #include #include #include @@ -35,7 +34,6 @@ MainWindow::MainWindow(QWidget* parent) init(); activateFields(false); - updatePreferences(); resetBrowser(); updateRecentFileActions(); } @@ -108,10 +106,9 @@ void MainWindow::init() connect(editWin, SIGNAL(updateRecordText(int, int, QByteArray)), this, SLOT(updateRecordText(int, int , QByteArray))); // Load window settings - QSettings settings(QApplication::organizationName(), QApplication::organizationName()); - restoreGeometry(settings.value("MainWindow/geometry").toByteArray()); - restoreState(settings.value("MainWindow/windowState").toByteArray()); - ui->comboLogSubmittedBy->setCurrentIndex(ui->comboLogSubmittedBy->findText(settings.value("SQLLogDock/Log", "Application").toString())); + restoreGeometry(PreferencesDialog::getSettingsValue("MainWindow", "geometry").toByteArray()); + restoreState(PreferencesDialog::getSettingsValue("MainWindow", "windowState").toByteArray()); + ui->comboLogSubmittedBy->setCurrentIndex(ui->comboLogSubmittedBy->findText(PreferencesDialog::getSettingsValue("SQLLogDock", "Log").toString())); // Set other window settings setAcceptDrops(true); @@ -136,7 +133,7 @@ void MainWindow::fileOpen(const QString & fileName) wFile = QFileDialog::getOpenFileName( this, tr("Choose a database file"), - defaultlocation); + PreferencesDialog::getSettingsValue("db", "defaultlocation").toString()); } if(QFile::exists(wFile) ) { @@ -163,7 +160,7 @@ void MainWindow::fileOpen() void MainWindow::fileNew() { - QString fileName = QFileDialog::getSaveFileName(this, tr("Choose a filename to save under"), defaultlocation); + QString fileName = QFileDialog::getSaveFileName(this, tr("Choose a filename to save under"), PreferencesDialog::getSettingsValue("db", "defaultlocation").toString()); if(!fileName.isEmpty()) { if(QFile::exists(fileName)) @@ -366,10 +363,9 @@ void MainWindow::fileExit() void MainWindow::closeEvent( QCloseEvent* event ) { - QSettings settings(QApplication::organizationName(), QApplication::organizationName()); - settings.setValue("MainWindow/geometry", saveGeometry()); - settings.setValue("MainWindow/windowState", saveState()); - settings.setValue("SQLLogDock/Log", ui->comboLogSubmittedBy->currentText()); + PreferencesDialog::setSettingsValue("MainWindow", "geometry", saveGeometry()); + PreferencesDialog::setSettingsValue("MainWindow", "windowState", saveState()); + PreferencesDialog::setSettingsValue("SQLLogDock", "Log", ui->comboLogSubmittedBy->currentText()); fileExit(); QMainWindow::closeEvent(event); } @@ -832,7 +828,7 @@ void MainWindow::importTableFromCSV() QString wFile = QFileDialog::getOpenFileName( this, tr("Choose a text file"), - defaultlocation, + PreferencesDialog::getSettingsValue("db", "defaultlocation").toString(), tr("Text files(*.csv *.txt);;All files(*)")); if (QFile::exists(wFile) ) @@ -849,7 +845,7 @@ void MainWindow::importTableFromCSV() void MainWindow::exportTableToCSV() { - ExportCsvDialog dialog(&db, defaultlocation, this); + ExportCsvDialog dialog(&db, this); dialog.exec(); } @@ -883,7 +879,7 @@ void MainWindow::exportDatabaseToSQL() QString fileName = QFileDialog::getSaveFileName( this, tr("Choose a filename to export"), - defaultlocation, + PreferencesDialog::getSettingsValue("db", "defaultlocation").toString(), tr("Text files(*.sql *.txt)")); if(fileName.size()) @@ -900,7 +896,7 @@ void MainWindow::importDatabaseFromSQL() QString fileName = QFileDialog::getOpenFileName( this, tr("Choose a file to import"), - defaultlocation, + PreferencesDialog::getSettingsValue("db", "defaultlocation").toString(), tr("Text files(*.sql *.txt);;All files(*)")); if (fileName.size() > 0) @@ -911,7 +907,7 @@ void MainWindow::importDatabaseFromSQL() QString newDBfile = QFileDialog::getSaveFileName( this, tr("Choose a filename to save under"), - defaultlocation); + PreferencesDialog::getSettingsValue("db", "defaultlocation").toString()); if (QFile::exists(newDBfile) ) { QString err = tr("File %1 already exists. Please choose a different name.").arg(newDBfile); @@ -936,20 +932,11 @@ void MainWindow::openPreferences() PreferencesDialog dialog(this); if(dialog.exec()) { - updatePreferences(); + db.setDefaultNewData(PreferencesDialog::getSettingsValue("db", "defaultnewdata").toString()); resetBrowser(); } } -void MainWindow::updatePreferences() -{ - PreferencesDialog prefs(this); - - db.setDefaultNewData(prefs.defaultnewdata); - defaultlocation= prefs.defaultlocation; - editWin->defaultlocation = defaultlocation; -} - //****************************************************************** //** Tree Events //****************************************************************** @@ -1046,10 +1033,7 @@ void MainWindow::openRecentFile() void MainWindow::updateRecentFileActions() { - QSettings settings(QApplication::organizationName(), QApplication::organizationName()); - QStringList files = settings.value("recentFileList").toStringList(); - - + QStringList files = PreferencesDialog::getSettingsValue("General", "recentFileList").toStringList(); int numRecentFiles = qMin(files.size(), (int)MaxRecentFiles); for (int i = 0; i < numRecentFiles; ++i) { @@ -1070,14 +1054,13 @@ void MainWindow::setCurrentFile(const QString &fileName) setWindowTitle( QApplication::applicationName() +" - "+fileName); activateFields(true); - QSettings settings(QApplication::organizationName(), QApplication::organizationName()); - QStringList files = settings.value("recentFileList").toStringList(); + QStringList files = PreferencesDialog::getSettingsValue("General", "recentFileList").toStringList(); files.removeAll(fileName); files.prepend(fileName); while (files.size() > MaxRecentFiles) files.removeLast(); - settings.setValue("recentFileList", files); + PreferencesDialog::setSettingsValue("General", "recentFileList", files); foreach (QWidget *widget, QApplication::topLevelWidgets()) { MainWindow *mainWin = qobject_cast(widget); diff --git a/src/MainWindow.h b/src/MainWindow.h index 5f8d616c..3a2eb283 100644 --- a/src/MainWindow.h +++ b/src/MainWindow.h @@ -74,7 +74,6 @@ private: EditDialog* editWin; FindDialog* findWin; QIntValidator* gotoValidator; - QString defaultlocation; DBBrowserDB db; @@ -139,7 +138,6 @@ private slots: virtual void exportDatabaseToSQL(); virtual void importDatabaseFromSQL(); virtual void openPreferences(); - virtual void updatePreferences(); virtual void openRecentFile(); virtual void loadPragmas(); virtual void updatePragmaUi(); diff --git a/src/PreferencesDialog.cpp b/src/PreferencesDialog.cpp index 64aa41e3..f1e06992 100644 --- a/src/PreferencesDialog.cpp +++ b/src/PreferencesDialog.cpp @@ -5,6 +5,8 @@ #include #include "sqlitedb.h" +QHash PreferencesDialog::m_hCache; + PreferencesDialog::PreferencesDialog(QWidget* parent) : QDialog(parent), ui(new Ui::PreferencesDialog) @@ -22,108 +24,105 @@ PreferencesDialog::~PreferencesDialog() delete ui; } -void PreferencesDialog::defaultDataChanged( int which ) -{ - if (which==2) { - defaultnewdata = QString("\'\'"); - } else if (which==1) { - defaultnewdata = QString("0"); - } else { - defaultnewdata = QString("NULL"); - } -} - -void PreferencesDialog::defaultTextChanged( int which ) -{ - if (which==1) { - defaulttext = QString("Auto"); - } else { - defaulttext = QString("Plain"); - } - -} - -void PreferencesDialog::encodingChanged( int which ) -{ - if (which == 0) { - defaultencoding = QString("UTF-8"); - } else { - defaultencoding = QString("UTF-16"); - } -} - -void PreferencesDialog::foreignkeysStateChanged(int state) -{ - foreignkeys = state > 0; -} - void PreferencesDialog::chooseLocation() { QString s = QFileDialog::getExistingDirectory( this, tr("Choose a directory"), - defaultlocation, + getSettingsValue("db", "defaultlocation").toString(), QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks); if(!s.isEmpty()) - { - defaultlocation = s; - ui->locationEdit->setText(defaultlocation); - } + ui->locationEdit->setText(s); } void PreferencesDialog::loadSettings() { - QSettings settings(QApplication::organizationName(), QApplication::organizationName()); - settings.sync(); - - defaultencoding = settings.value( "/db/defaultencoding", "UTF-8" ).toString(); - defaultnewdata = settings.value( "/db/defaultnewdata", "NULL" ).toString(); - defaultlocation = settings.value( "/db/defaultlocation", QDir::homePath() ).toString(); - defaulttext = settings.value( "/db/defaulttext", "Plain" ).toString(); - foreignkeys = settings.value( "/db/foreignkeys", true ).toBool(); - - if (defaultencoding=="Latin1" || defaultencoding == "UTF-8") - { - ui->encodingComboBox->setCurrentIndex(0); - } else { - ui->encodingComboBox->setCurrentIndex(1); - defaultencoding = QString("UTF-16"); - } - - if (defaultnewdata=="\'\'") - { - ui->defaultdataComboBox->setCurrentIndex(2) ; - } else if (defaultnewdata=="0") - { - ui->defaultdataComboBox->setCurrentIndex(1) ; - } else { - ui->defaultdataComboBox->setCurrentIndex(0) ; - defaultnewdata = QString("NULL"); - } - - if (defaulttext=="Auto") - { - ui->defaultTextComboBox->setCurrentIndex(1) ; - } else { - ui->defaultTextComboBox->setCurrentIndex(0) ; - defaulttext = QString("Plain"); - } - - ui->foreignKeysCheckBox->setChecked(foreignkeys); - - ui->locationEdit->setText(defaultlocation); + ui->encodingComboBox->setCurrentIndex(ui->encodingComboBox->findText(getSettingsValue("db", "defaultencoding").toString(), Qt::MatchFixedString)); + ui->defaultdataComboBox->setCurrentIndex(ui->defaultdataComboBox->findText(getSettingsValue("db", "defaultnewdata").toString(), Qt::MatchFixedString)); + ui->locationEdit->setText(getSettingsValue("db", "defaultlocation").toString()); + ui->defaultTextComboBox->setCurrentIndex(ui->defaultTextComboBox->findText(getSettingsValue("db", "defaulttext").toString(), Qt::MatchFixedString)); + ui->foreignKeysCheckBox->setChecked(getSettingsValue("db", "foreignkeys").toBool()); } void PreferencesDialog::saveSettings() { - QSettings settings(QApplication::organizationName(), QApplication::organizationName()); - - settings.setValue( "/db/defaultencoding", defaultencoding ); - settings.setValue( "/db/defaultnewdata", defaultnewdata ); - settings.setValue( "/db/defaultlocation", defaultlocation ); - settings.setValue( "/db/defaulttext", defaulttext ); - settings.setValue( "/db/foreignkeys", foreignkeys ); - settings.sync(); + setSettingsValue("db", "defaultencoding", ui->encodingComboBox->currentText()); + setSettingsValue("db", "defaultnewdata", ui->defaultdataComboBox->currentText()); + setSettingsValue("db", "defaultlocation", ui->locationEdit->text()); + setSettingsValue("db", "defaulttext", ui->defaultTextComboBox->currentText()); + setSettingsValue("db", "foreignkeys", ui->foreignKeysCheckBox->isChecked()); accept(); } + +QVariant PreferencesDialog::getSettingsValue(const QString& group, const QString& name) +{ + // Have a look in the cache first + QHash::iterator cacheIndex = m_hCache.find(group + name); + if(cacheIndex != m_hCache.end()) + { + return cacheIndex.value(); + } else { + // Nothing found in the cache, so get the value from the settings file or get the default value if there is no value set yet + QSettings settings(QApplication::organizationName(), QApplication::organizationName()); + QVariant value = settings.value(group + "/" + name, getSettingsDefaultValue(group, name)); + + // Store this value in the cache for further usage and return it afterwards + m_hCache.insert(group + name, value); + return value; + } +} + +void PreferencesDialog::setSettingsValue(const QString& group, const QString& name, const QVariant& value) +{ + // Set the group and save the given value + QSettings settings(QApplication::organizationName(), QApplication::organizationName()); + settings.beginGroup(group); + settings.setValue(name, value); + settings.endGroup(); + + // Also change it in the cache + m_hCache[group + name] = value; +} + +QVariant PreferencesDialog::getSettingsDefaultValue(const QString& group, const QString& name) +{ + // db/defaultencoding? + if(group == "db" && name == "defaultencoding") + return "UTF-8"; + + // db/defaultnewdata? + if(group == "db" && name == "defaultnewdata") + return "NULL"; + + // db/defaultlocation? + if(group == "db" && name == "defaultlocation") + return QDir::homePath(); + + // db/defaulttext? + if(group == "db" && name == "defaulttext") + return "Plain"; + + // db/foreignkeys? + if(group == "db" && name == "foreignkeys") + return true; + + // MainWindow/geometry? + if(group == "MainWindow" && name == "geometry") + return ""; + + // MainWindow/windowState? + if(group == "MainWindow" && name == "windowState") + return ""; + + // SQLLogDock/Log? + if(group == "SQLLogDock" && name == "Log") + return "Application"; + + // General/recentFileList? + if(group == "General" && name == "recentFileList") + return QStringList(); + + // Unknown combination of group and name? Return an invalid QVariant! + return QVariant(); +} diff --git a/src/PreferencesDialog.h b/src/PreferencesDialog.h index 264aed00..229643e1 100644 --- a/src/PreferencesDialog.h +++ b/src/PreferencesDialog.h @@ -2,6 +2,8 @@ #define __PREFERENCESDIALOG_H__ #include +#include +#include namespace Ui { class PreferencesDialog; @@ -15,23 +17,23 @@ public: explicit PreferencesDialog(QWidget* parent = 0); ~PreferencesDialog(); - QString defaulttext; - QString defaultlocation; - QString defaultnewdata; - QString defaultencoding; - bool foreignkeys; + // Use these methods to access the application settings. + static QVariant getSettingsValue(const QString& group, const QString& name); + static void setSettingsValue(const QString& group, const QString& name, const QVariant& value); private slots: - virtual void defaultDataChanged( int which ); - virtual void defaultTextChanged( int which ); - virtual void encodingChanged( int which ); - virtual void foreignkeysStateChanged( int state ); virtual void chooseLocation(); virtual void loadSettings(); virtual void saveSettings(); private: Ui::PreferencesDialog *ui; + + // This works similar to getSettingsValue but returns the default value instead of the value set by the user + static QVariant getSettingsDefaultValue(const QString& group, const QString& name); + + // Cache for storing the settings to avoid repeatedly reading the settings file all the time + static QHash m_hCache; }; #endif diff --git a/src/PreferencesDialog.ui b/src/PreferencesDialog.ui index 055e91b8..fa45879a 100644 --- a/src/PreferencesDialog.ui +++ b/src/PreferencesDialog.ui @@ -56,7 +56,7 @@ - Empty string + '' @@ -162,12 +162,13 @@ - buttonBox encodingComboBox defaultdataComboBox defaultTextComboBox + foreignKeysCheckBox locationEdit setLocationButton + buttonBox @@ -178,8 +179,8 @@ saveSettings() - 252 - 140 + 256 + 214 155 @@ -194,8 +195,8 @@ reject() - 320 - 140 + 324 + 214 286 @@ -203,38 +204,6 @@ - - encodingComboBox - activated(int) - PreferencesDialog - encodingChanged(int) - - - 265 - 15 - - - 339 - 8 - - - - - defaultdataComboBox - activated(int) - PreferencesDialog - defaultDataChanged(int) - - - 239 - 40 - - - 350 - 43 - - - setLocationButton clicked() @@ -242,8 +211,8 @@ chooseLocation() - 468 - 98 + 485 + 134 459 @@ -251,38 +220,6 @@ - - defaultTextComboBox - activated(int) - PreferencesDialog - defaultTextChanged(int) - - - 215 - 72 - - - 297 - 64 - - - - - foreignKeysCheckBox - stateChanged(int) - PreferencesDialog - foreignkeysStateChanged(int) - - - 213 - 113 - - - 245 - 109 - - - saveSettings()