Move all settings logic to the preferences dialog

Read and write the settings only from the preferences dialog.

Remove all the copies of some settings which were stored in nearly every
dialog class individually.

Simplify the settings dialog code by removing all those not really
needed slots.
This commit is contained in:
Martin Kleusberg
2013-03-17 16:09:28 +01:00
parent 066356e5eb
commit 2165e544a2
9 changed files with 132 additions and 215 deletions

View File

@@ -5,6 +5,7 @@
#include <QShortcut>
#include "sqlitedb.h"
#include <src/qhexedit.h>
#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)

View File

@@ -16,8 +16,6 @@ public:
explicit EditDialog(QWidget* parent = 0);
~EditDialog();
QString defaultlocation;
public:
int getCurrentCol() { return curCol; }
int getCurrentRow() { return curRow; }

View File

@@ -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

View File

@@ -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

View File

@@ -1,7 +1,6 @@
#include "MainWindow.h"
#include "ui_MainWindow.h"
#include <QtGui/QFileDialog>
#include <QSettings>
#include <QFileDialog>
#include <QFile>
#include <QApplication>
#include <QTextStream>
@@ -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<MainWindow *>(widget);

View File

@@ -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();

View File

@@ -5,6 +5,8 @@
#include <QFileDialog>
#include "sqlitedb.h"
QHash<QString, QVariant> 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<QString, QVariant>::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();
}

View File

@@ -2,6 +2,8 @@
#define __PREFERENCESDIALOG_H__
#include <QDialog>
#include <QVariant>
#include <QHash>
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<QString, QVariant> m_hCache;
};
#endif

View File

@@ -56,7 +56,7 @@
</item>
<item>
<property name="text">
<string>Empty string</string>
<string>''</string>
</property>
</item>
</widget>
@@ -162,12 +162,13 @@
</layout>
</widget>
<tabstops>
<tabstop>buttonBox</tabstop>
<tabstop>encodingComboBox</tabstop>
<tabstop>defaultdataComboBox</tabstop>
<tabstop>defaultTextComboBox</tabstop>
<tabstop>foreignKeysCheckBox</tabstop>
<tabstop>locationEdit</tabstop>
<tabstop>setLocationButton</tabstop>
<tabstop>buttonBox</tabstop>
</tabstops>
<resources/>
<connections>
@@ -178,8 +179,8 @@
<slot>saveSettings()</slot>
<hints>
<hint type="sourcelabel">
<x>252</x>
<y>140</y>
<x>256</x>
<y>214</y>
</hint>
<hint type="destinationlabel">
<x>155</x>
@@ -194,8 +195,8 @@
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>320</x>
<y>140</y>
<x>324</x>
<y>214</y>
</hint>
<hint type="destinationlabel">
<x>286</x>
@@ -203,38 +204,6 @@
</hint>
</hints>
</connection>
<connection>
<sender>encodingComboBox</sender>
<signal>activated(int)</signal>
<receiver>PreferencesDialog</receiver>
<slot>encodingChanged(int)</slot>
<hints>
<hint type="sourcelabel">
<x>265</x>
<y>15</y>
</hint>
<hint type="destinationlabel">
<x>339</x>
<y>8</y>
</hint>
</hints>
</connection>
<connection>
<sender>defaultdataComboBox</sender>
<signal>activated(int)</signal>
<receiver>PreferencesDialog</receiver>
<slot>defaultDataChanged(int)</slot>
<hints>
<hint type="sourcelabel">
<x>239</x>
<y>40</y>
</hint>
<hint type="destinationlabel">
<x>350</x>
<y>43</y>
</hint>
</hints>
</connection>
<connection>
<sender>setLocationButton</sender>
<signal>clicked()</signal>
@@ -242,8 +211,8 @@
<slot>chooseLocation()</slot>
<hints>
<hint type="sourcelabel">
<x>468</x>
<y>98</y>
<x>485</x>
<y>134</y>
</hint>
<hint type="destinationlabel">
<x>459</x>
@@ -251,38 +220,6 @@
</hint>
</hints>
</connection>
<connection>
<sender>defaultTextComboBox</sender>
<signal>activated(int)</signal>
<receiver>PreferencesDialog</receiver>
<slot>defaultTextChanged(int)</slot>
<hints>
<hint type="sourcelabel">
<x>215</x>
<y>72</y>
</hint>
<hint type="destinationlabel">
<x>297</x>
<y>64</y>
</hint>
</hints>
</connection>
<connection>
<sender>foreignKeysCheckBox</sender>
<signal>stateChanged(int)</signal>
<receiver>PreferencesDialog</receiver>
<slot>foreignkeysStateChanged(int)</slot>
<hints>
<hint type="sourcelabel">
<x>213</x>
<y>113</y>
</hint>
<hint type="destinationlabel">
<x>245</x>
<y>109</y>
</hint>
</hints>
</connection>
</connections>
<slots>
<slot>saveSettings()</slot>