Skip to content

Commit

Permalink
Move all settings logic to the preferences dialog
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
MKleusberg committed Mar 17, 2013
1 parent 066356e commit 2165e54
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 208 deletions.
5 changes: 3 additions & 2 deletions src/EditDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <QShortcut>
#include "sqlitedb.h"
#include <src/qhexedit.h>
#include "PreferencesDialog.h"

EditDialog::EditDialog(QWidget* parent)
: QDialog(parent),
Expand Down Expand Up @@ -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))
{
Expand All @@ -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)
Expand Down
2 changes: 0 additions & 2 deletions src/EditDialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ class EditDialog : public QDialog
explicit EditDialog(QWidget* parent = 0);
~EditDialog();

QString defaultlocation;

public:
int getCurrentCol() { return curCol; }
int getCurrentRow() { return curRow; }
Expand Down
8 changes: 4 additions & 4 deletions src/ExportCsvDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
Expand Down
3 changes: 1 addition & 2 deletions src/ExportCsvDialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -22,7 +22,6 @@ private slots:
private:
Ui::ExportCsvDialog* ui;
DBBrowserDB* pdb;
QString defaultLocation;
};

#endif
53 changes: 18 additions & 35 deletions src/MainWindow.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "MainWindow.h"
#include "ui_MainWindow.h"
#include <QtGui/QFileDialog>
#include <QSettings>
#include <QFileDialog>
#include <QFile>
#include <QApplication>
#include <QTextStream>
Expand Down Expand Up @@ -35,7 +34,6 @@ MainWindow::MainWindow(QWidget* parent)
init();

activateFields(false);
updatePreferences();
resetBrowser();
updateRecentFileActions();
}
Expand Down Expand Up @@ -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);
Expand All @@ -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) )
{
Expand All @@ -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))
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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) )
Expand All @@ -849,7 +845,7 @@ void MainWindow::importTableFromCSV()

void MainWindow::exportTableToCSV()
{
ExportCsvDialog dialog(&db, defaultlocation, this);
ExportCsvDialog dialog(&db, this);
dialog.exec();
}

Expand Down Expand Up @@ -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())
Expand All @@ -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)
Expand All @@ -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);
Expand All @@ -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
//******************************************************************
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
Expand Down
2 changes: 0 additions & 2 deletions src/MainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ class MainWindow : public QMainWindow
EditDialog* editWin;
FindDialog* findWin;
QIntValidator* gotoValidator;
QString defaultlocation;

DBBrowserDB db;

Expand Down Expand Up @@ -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();
Expand Down
Loading

0 comments on commit 2165e54

Please sign in to comment.