Divide PreferencesDialog in two classes

This forces PreferencesDialog to serve only for UI needs.
Plus, this opens the door for adding another class for shortcut
management without pain.
This commit is contained in:
Vladislav Tronko
2016-09-03 22:28:28 +03:00
parent 11bff0ded8
commit 3a06395f88
16 changed files with 414 additions and 389 deletions

View File

@@ -2,6 +2,7 @@
#include "ui_PreferencesDialog.h"
#include "sqlitedb.h"
#include "FileDialog.h"
#include "Settings.h"
#include <QDir>
#include <QSettings>
@@ -9,8 +10,6 @@
#include <QMessageBox>
#include <QKeyEvent>
QHash<QString, QVariant> PreferencesDialog::m_hCache;
PreferencesDialog::PreferencesDialog(QWidget* parent)
: QDialog(parent),
ui(new Ui::PreferencesDialog)
@@ -56,18 +55,18 @@ void PreferencesDialog::chooseLocation()
void PreferencesDialog::loadSettings()
{
ui->encodingComboBox->setCurrentIndex(ui->encodingComboBox->findText(getSettingsValue("db", "defaultencoding").toString(), Qt::MatchFixedString));
ui->comboDefaultLocation->setCurrentIndex(getSettingsValue("db", "savedefaultlocation").toInt());
ui->locationEdit->setText(getSettingsValue("db", "defaultlocation").toString());
ui->checkUpdates->setChecked(getSettingsValue("checkversion", "enabled").toBool());
ui->checkHideSchemaLinebreaks->setChecked(getSettingsValue("db", "hideschemalinebreaks").toBool());
ui->foreignKeysCheckBox->setChecked(getSettingsValue("db", "foreignkeys").toBool());
ui->spinPrefetchSize->setValue(getSettingsValue("db", "prefetchsize").toInt());
ui->editDatabaseDefaultSqlText->setText(getSettingsValue("db", "defaultsqltext").toString());
ui->encodingComboBox->setCurrentIndex(ui->encodingComboBox->findText(Settings::getSettingsValue("db", "defaultencoding").toString(), Qt::MatchFixedString));
ui->comboDefaultLocation->setCurrentIndex(Settings::getSettingsValue("db", "savedefaultlocation").toInt());
ui->locationEdit->setText(Settings::getSettingsValue("db", "defaultlocation").toString());
ui->checkUpdates->setChecked(Settings::getSettingsValue("checkversion", "enabled").toBool());
ui->checkHideSchemaLinebreaks->setChecked(Settings::getSettingsValue("db", "hideschemalinebreaks").toBool());
ui->foreignKeysCheckBox->setChecked(Settings::getSettingsValue("db", "foreignkeys").toBool());
ui->spinPrefetchSize->setValue(Settings::getSettingsValue("db", "prefetchsize").toInt());
ui->editDatabaseDefaultSqlText->setText(Settings::getSettingsValue("db", "defaultsqltext").toString());
ui->defaultFieldTypeComboBox->addItems(sqlb::Field::Datatypes);
int defaultFieldTypeIndex = getSettingsValue("db", "defaultfieldtype").toInt();
int defaultFieldTypeIndex = Settings::getSettingsValue("db", "defaultfieldtype").toInt();
if (defaultFieldTypeIndex < sqlb::Field::Datatypes.count())
{
@@ -75,12 +74,12 @@ void PreferencesDialog::loadSettings()
}
// Gracefully handle the preferred Data Browser font not being available
int matchingFont = ui->comboDataBrowserFont->findText(getSettingsValue("databrowser", "font").toString(), Qt::MatchExactly);
int matchingFont = ui->comboDataBrowserFont->findText(Settings::getSettingsValue("databrowser", "font").toString(), Qt::MatchExactly);
if (matchingFont == -1)
matchingFont = ui->comboDataBrowserFont->findText(getSettingsDefaultValue("databrowser", "font").toString());
matchingFont = ui->comboDataBrowserFont->findText(Settings::getSettingsDefaultValue("databrowser", "font").toString());
ui->comboDataBrowserFont->setCurrentIndex(matchingFont);
ui->spinDataBrowserFontSize->setValue(getSettingsValue("databrowser", "fontsize").toInt());
ui->spinDataBrowserFontSize->setValue(Settings::getSettingsValue("databrowser", "fontsize").toInt());
loadColorSetting(ui->fr_null_fg, "null_fg");
loadColorSetting(ui->fr_null_bg, "null_bg");
loadColorSetting(ui->fr_reg_fg, "reg_fg");
@@ -88,333 +87,104 @@ void PreferencesDialog::loadSettings()
loadColorSetting(ui->fr_bin_fg, "bin_fg");
loadColorSetting(ui->fr_bin_bg, "bin_bg");
ui->spinSymbolLimit->setValue(getSettingsValue("databrowser", "symbol_limit").toInt());
ui->txtNull->setText(getSettingsValue("databrowser", "null_text").toString());
ui->editFilterEscape->setText(getSettingsValue("databrowser", "filter_escape").toString());
ui->spinFilterDelay->setValue(getSettingsValue("databrowser", "filter_delay").toInt());
ui->spinSymbolLimit->setValue(Settings::getSettingsValue("databrowser", "symbol_limit").toInt());
ui->txtNull->setText(Settings::getSettingsValue("databrowser", "null_text").toString());
ui->editFilterEscape->setText(Settings::getSettingsValue("databrowser", "filter_escape").toString());
ui->spinFilterDelay->setValue(Settings::getSettingsValue("databrowser", "filter_delay").toInt());
for(int i=0; i < ui->treeSyntaxHighlighting->topLevelItemCount(); ++i)
{
QString name = ui->treeSyntaxHighlighting->topLevelItem(i)->text(0);
QString colorname = getSettingsValue("syntaxhighlighter", name + "_colour").toString();
QString colorname = Settings::getSettingsValue("syntaxhighlighter", name + "_colour").toString();
QColor color = QColor(colorname);
ui->treeSyntaxHighlighting->topLevelItem(i)->setTextColor(2, color);
ui->treeSyntaxHighlighting->topLevelItem(i)->setBackgroundColor(2, color);
ui->treeSyntaxHighlighting->topLevelItem(i)->setText(2, colorname);
if (name != "null") {
ui->treeSyntaxHighlighting->topLevelItem(i)->setCheckState(3, getSettingsValue("syntaxhighlighter", name + "_bold").toBool() ? Qt::Checked : Qt::Unchecked);
ui->treeSyntaxHighlighting->topLevelItem(i)->setCheckState(4, getSettingsValue("syntaxhighlighter", name + "_italic").toBool() ? Qt::Checked : Qt::Unchecked);
ui->treeSyntaxHighlighting->topLevelItem(i)->setCheckState(5, getSettingsValue("syntaxhighlighter", name + "_underline").toBool() ? Qt::Checked : Qt::Unchecked);
ui->treeSyntaxHighlighting->topLevelItem(i)->setCheckState(3, Settings::getSettingsValue("syntaxhighlighter", name + "_bold").toBool() ? Qt::Checked : Qt::Unchecked);
ui->treeSyntaxHighlighting->topLevelItem(i)->setCheckState(4, Settings::getSettingsValue("syntaxhighlighter", name + "_italic").toBool() ? Qt::Checked : Qt::Unchecked);
ui->treeSyntaxHighlighting->topLevelItem(i)->setCheckState(5, Settings::getSettingsValue("syntaxhighlighter", name + "_underline").toBool() ? Qt::Checked : Qt::Unchecked);
}
}
// Gracefully handle the preferred Editor font not being available
matchingFont = ui->comboEditorFont->findText(getSettingsValue("editor", "font").toString(), Qt::MatchExactly);
matchingFont = ui->comboEditorFont->findText(Settings::getSettingsValue("editor", "font").toString(), Qt::MatchExactly);
if (matchingFont == -1)
matchingFont = ui->comboDataBrowserFont->findText(getSettingsDefaultValue("editor", "font").toString());
matchingFont = ui->comboDataBrowserFont->findText(Settings::getSettingsDefaultValue("editor", "font").toString());
ui->comboEditorFont->setCurrentIndex(matchingFont);
ui->spinEditorFontSize->setValue(getSettingsValue("editor", "fontsize").toInt());
ui->spinTabSize->setValue(getSettingsValue("editor", "tabsize").toInt());
ui->spinLogFontSize->setValue(getSettingsValue("log", "fontsize").toInt());
ui->checkAutoCompletion->setChecked(getSettingsValue("editor", "auto_completion").toBool());
ui->checkErrorIndicators->setChecked(getSettingsValue("editor", "error_indicators").toBool());
ui->checkHorizontalTiling->setChecked(getSettingsValue("editor", "horizontal_tiling").toBool());
ui->spinEditorFontSize->setValue(Settings::getSettingsValue("editor", "fontsize").toInt());
ui->spinTabSize->setValue(Settings::getSettingsValue("editor", "tabsize").toInt());
ui->spinLogFontSize->setValue(Settings::getSettingsValue("log", "fontsize").toInt());
ui->checkAutoCompletion->setChecked(Settings::getSettingsValue("editor", "auto_completion").toBool());
ui->checkErrorIndicators->setChecked(Settings::getSettingsValue("editor", "error_indicators").toBool());
ui->checkHorizontalTiling->setChecked(Settings::getSettingsValue("editor", "horizontal_tiling").toBool());
ui->listExtensions->addItems(getSettingsValue("extensions", "list").toStringList());
ui->checkRegexDisabled->setChecked(getSettingsValue("extensions", "disableregex").toBool());
ui->listExtensions->addItems(Settings::getSettingsValue("extensions", "list").toStringList());
ui->checkRegexDisabled->setChecked(Settings::getSettingsValue("extensions", "disableregex").toBool());
fillLanguageBox();
}
void PreferencesDialog::saveSettings()
{
setSettingsValue("db", "defaultencoding", ui->encodingComboBox->currentText());
setSettingsValue("db", "defaultlocation", ui->locationEdit->text());
setSettingsValue("db", "savedefaultlocation", ui->comboDefaultLocation->currentIndex());
setSettingsValue("db", "hideschemalinebreaks", ui->checkHideSchemaLinebreaks->isChecked());
setSettingsValue("db", "foreignkeys", ui->foreignKeysCheckBox->isChecked());
setSettingsValue("db", "prefetchsize", ui->spinPrefetchSize->value());
setSettingsValue("db", "defaultsqltext", ui->editDatabaseDefaultSqlText->text());
Settings::setSettingsValue("db", "defaultencoding", ui->encodingComboBox->currentText());
Settings::setSettingsValue("db", "defaultlocation", ui->locationEdit->text());
Settings::setSettingsValue("db", "savedefaultlocation", ui->comboDefaultLocation->currentIndex());
Settings::setSettingsValue("db", "hideschemalinebreaks", ui->checkHideSchemaLinebreaks->isChecked());
Settings::setSettingsValue("db", "foreignkeys", ui->foreignKeysCheckBox->isChecked());
Settings::setSettingsValue("db", "prefetchsize", ui->spinPrefetchSize->value());
Settings::setSettingsValue("db", "defaultsqltext", ui->editDatabaseDefaultSqlText->text());
setSettingsValue("db", "defaultfieldtype", ui->defaultFieldTypeComboBox->currentIndex());
Settings::setSettingsValue("db", "defaultfieldtype", ui->defaultFieldTypeComboBox->currentIndex());
setSettingsValue("checkversion", "enabled", ui->checkUpdates->isChecked());
Settings::setSettingsValue("checkversion", "enabled", ui->checkUpdates->isChecked());
setSettingsValue("databrowser", "font", ui->comboDataBrowserFont->currentText());
setSettingsValue("databrowser", "fontsize", ui->spinDataBrowserFontSize->value());
Settings::setSettingsValue("databrowser", "font", ui->comboDataBrowserFont->currentText());
Settings::setSettingsValue("databrowser", "fontsize", ui->spinDataBrowserFontSize->value());
saveColorSetting(ui->fr_null_fg, "null_fg");
saveColorSetting(ui->fr_null_bg, "null_bg");
saveColorSetting(ui->fr_reg_fg, "reg_fg");
saveColorSetting(ui->fr_reg_bg, "reg_bg");
saveColorSetting(ui->fr_bin_fg, "bin_fg");
saveColorSetting(ui->fr_bin_bg, "bin_bg");
setSettingsValue("databrowser", "symbol_limit", ui->spinSymbolLimit->value());
setSettingsValue("databrowser", "null_text", ui->txtNull->text());
setSettingsValue("databrowser", "filter_escape", ui->editFilterEscape->text());
setSettingsValue("databrowser", "filter_delay", ui->spinFilterDelay->value());
Settings::setSettingsValue("databrowser", "symbol_limit", ui->spinSymbolLimit->value());
Settings::setSettingsValue("databrowser", "null_text", ui->txtNull->text());
Settings::setSettingsValue("databrowser", "filter_escape", ui->editFilterEscape->text());
Settings::setSettingsValue("databrowser", "filter_delay", ui->spinFilterDelay->value());
for(int i=0; i < ui->treeSyntaxHighlighting->topLevelItemCount(); ++i)
{
QString name = ui->treeSyntaxHighlighting->topLevelItem(i)->text(0);
setSettingsValue("syntaxhighlighter", name + "_colour", ui->treeSyntaxHighlighting->topLevelItem(i)->text(2));
setSettingsValue("syntaxhighlighter", name + "_bold", ui->treeSyntaxHighlighting->topLevelItem(i)->checkState(3) == Qt::Checked);
setSettingsValue("syntaxhighlighter", name + "_italic", ui->treeSyntaxHighlighting->topLevelItem(i)->checkState(4) == Qt::Checked);
setSettingsValue("syntaxhighlighter", name + "_underline", ui->treeSyntaxHighlighting->topLevelItem(i)->checkState(5) == Qt::Checked);
Settings::setSettingsValue("syntaxhighlighter", name + "_colour", ui->treeSyntaxHighlighting->topLevelItem(i)->text(2));
Settings::setSettingsValue("syntaxhighlighter", name + "_bold", ui->treeSyntaxHighlighting->topLevelItem(i)->checkState(3) == Qt::Checked);
Settings::setSettingsValue("syntaxhighlighter", name + "_italic", ui->treeSyntaxHighlighting->topLevelItem(i)->checkState(4) == Qt::Checked);
Settings::setSettingsValue("syntaxhighlighter", name + "_underline", ui->treeSyntaxHighlighting->topLevelItem(i)->checkState(5) == Qt::Checked);
}
setSettingsValue("editor", "font", ui->comboEditorFont->currentText());
setSettingsValue("editor", "fontsize", ui->spinEditorFontSize->value());
setSettingsValue("editor", "tabsize", ui->spinTabSize->value());
setSettingsValue("log", "fontsize", ui->spinLogFontSize->value());
setSettingsValue("editor", "auto_completion", ui->checkAutoCompletion->isChecked());
setSettingsValue("editor", "error_indicators", ui->checkErrorIndicators->isChecked());
setSettingsValue("editor", "horizontal_tiling", ui->checkHorizontalTiling->isChecked());
Settings::setSettingsValue("editor", "font", ui->comboEditorFont->currentText());
Settings::setSettingsValue("editor", "fontsize", ui->spinEditorFontSize->value());
Settings::setSettingsValue("editor", "tabsize", ui->spinTabSize->value());
Settings::setSettingsValue("log", "fontsize", ui->spinLogFontSize->value());
Settings::setSettingsValue("editor", "auto_completion", ui->checkAutoCompletion->isChecked());
Settings::setSettingsValue("editor", "error_indicators", ui->checkErrorIndicators->isChecked());
Settings::setSettingsValue("editor", "horizontal_tiling", ui->checkHorizontalTiling->isChecked());
QStringList extList;
foreach(QListWidgetItem* item, ui->listExtensions->findItems(QString("*"), Qt::MatchWrap | Qt::MatchWildcard))
extList.append(item->text());
setSettingsValue("extensions", "list", extList);
setSettingsValue("extensions", "disableregex", ui->checkRegexDisabled->isChecked());
Settings::setSettingsValue("extensions", "list", extList);
Settings::setSettingsValue("extensions", "disableregex", ui->checkRegexDisabled->isChecked());
// Warn about restarting to change language
QVariant newLanguage = ui->languageComboBox->itemData(ui->languageComboBox->currentIndex());
if (newLanguage != getSettingsValue("General", "language"))
if (newLanguage != Settings::getSettingsValue("General", "language"))
QMessageBox::information(this, QApplication::applicationName(),
tr("The language will change after you restart the application."));
setSettingsValue("General", "language", newLanguage);
Settings::setSettingsValue("General", "language", newLanguage);
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, bool dont_save_to_disk)
{
// Sometime the value has to be saved for the current session only but get discarded when the application exits.
// In order to achieve this this flag can be set which disables the save to disk mechanism and only leaves the save to cache part active.
if(dont_save_to_disk == false)
{
// 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/savedefaultlocation?
if(group == "db" && name == "savedefaultlocation")
return 2;
// db/defaultlocation?
if(group == "db" && name == "defaultlocation")
return QDir::homePath();
// db/lastlocation?
if(group == "db" && name == "lastlocation")
return getSettingsValue("db", "defaultlocation");
// db/hideschemalinebreaks?
if(group == "db" && name == "hideschemalinebreaks")
return true;
// db/foreignkeys?
if(group == "db" && name == "foreignkeys")
return true;
// db/prefetchsize?
if(group == "db" && name == "prefetchsize")
return 50000;
// db/defaultsqltext?
if(group == "db" && name == "defaultsqltext")
return "";
// exportcsv/firstrowheader?
if(group == "exportcsv" && name == "firstrowheader")
return true;
// exportcsv/separator?
if(group == "exportcsv" && name == "separator")
return ',';
// exportcsv/quotecharacter?
if(group == "exportcsv" && name == "quotecharacter")
return '"';
// exportjson/prettyprint?
if(group == "exportjson" && name == "prettyprint")
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();
// General/language?
if(group == "General" && name == "language")
return QLocale::system().name();
// checkversion/enabled
if(group == "checkversion" && name == "enabled")
return true;
// Data Browser/NULL Fields
if(group == "databrowser")
{
if(name == "font")
return QFont().defaultFamily();
if(name == "fontsize")
return 10;
if(name == "symbol_limit")
return 5000;
if(name == "null_text")
return "NULL";
if(name == "filter_escape")
return "\\";
if(name == "filter_delay")
return 200;
if(name == "null_fg_colour")
return QColor(Qt::lightGray).name();
if(name == "null_bg_colour")
return QColor(Qt::white).name();
if(name == "reg_fg_colour")
return QColor(Qt::black).name();
if(name == "reg_bg_colour")
return QColor(Qt::white).name();
if(name == "bin_fg_colour")
return QColor(Qt::lightGray).name();
if(name == "bin_bg_colour")
return QColor(Qt::white).name();
}
// syntaxhighlighter?
if(group == "syntaxhighlighter")
{
// Bold? Only tables, functions and keywords are bold by default
if(name.right(4) == "bold")
return name == "keyword_bold" || name == "table_bold" || name == "function_bold";
// Italic? Nothing by default
if(name.right(6) == "italic")
return false;
// Underline? Nothing by default
if(name.right(9) == "underline")
return false;
// Colour?
if(name.right(6) == "colour")
{
if(name == "keyword_colour")
return QColor(Qt::darkBlue).name();
else if(name == "function_colour")
return QColor(Qt::blue).name();
else if(name == "table_colour")
return QColor(Qt::darkCyan).name();
else if(name == "comment_colour")
return QColor(Qt::darkGreen).name();
else if(name == "identifier_colour")
return QColor(Qt::darkMagenta).name();
else if(name == "string_colour")
return QColor(Qt::red).name();
else if(name == "currentline_colour")
return QColor(236, 236, 245).name();
}
}
// editor/font?
if(group == "editor" && name == "font")
{
QFont font("Monospace");
font.setStyleHint(QFont::TypeWriter);
return QFontInfo(font).family();
}
// editor/fontsize or log/fontsize?
if((group == "editor" || group == "log") && name == "fontsize")
return 9;
if(group == "editor")
{
if(name == "tabsize")
{
return 4;
}
}
// editor/auto_completion?
if(group == "editor" && name == "auto_completion")
return true;
// editor/error_indicators?
if(group == "editor" && name == "error_indicators")
return true;
// editor/horizontal_tiling?
if(group == "editor" && name == "horizontal_tiling")
return false;
// extensions/list?
if(group == "extensions" && name == "list")
return QStringList();
// extensions/disableregex?
if(group == "extension" && name == "disableregex")
return false;
// PlotDock/lineType or pointShape?
if(group == "PlotDock")
{
// QCPGraph::lsLine
if(name == "lineType")
return 1;
// QCPScatterStyle::ssDisk
if(name == "pointShape")
return 4;
}
// Unknown combination of group and name? Return an invalid QVariant!
return QVariant();
}
void PreferencesDialog::showColourDialog(QTreeWidgetItem* item, int column)
{
if(item->text(column).left(1) != "#")
@@ -534,7 +304,7 @@ void PreferencesDialog::fillLanguageBox()
ui->languageComboBox->model()->sort(0);
// Try to select the language for the stored locale
int index = ui->languageComboBox->findData(getSettingsValue("General", "language"),
int index = ui->languageComboBox->findData(Settings::getSettingsValue("General", "language"),
Qt::UserRole, Qt::MatchExactly);
// If there's no translation for the current locale, default to English
@@ -555,12 +325,12 @@ void PreferencesDialog::loadColorSetting(QFrame *frame, const QString & settingN
{
QPalette palette = frame->palette();
palette.setColor(frame->backgroundRole(),
QColor(getSettingsValue("databrowser", settingName + "_colour").toString()));
QColor(Settings::getSettingsValue("databrowser", settingName + "_colour").toString()));
frame->setPalette(palette);
}
void PreferencesDialog::saveColorSetting(QFrame *frame, const QString & settingName)
{
setSettingsValue("databrowser", settingName + "_colour",
Settings::setSettingsValue("databrowser", settingName + "_colour",
frame->palette().color(frame->backgroundRole()));
}