From b629bd329247aa47b99afa6c6f9a2a28803af09b Mon Sep 17 00:00:00 2001 From: kintel Date: Mon, 25 Jan 2010 20:52:43 +0000 Subject: [PATCH] Save preferences to disk git-svn-id: http://svn.clifford.at/openscad/trunk@352 b57f626f-c46c-0410-a088-ec61d464b74c --- Preferences.cc | 89 ++++++++++++++++++++++++++++++++++++++++++-------- Preferences.h | 13 +++++--- mainwin.cc | 2 ++ 3 files changed, 85 insertions(+), 19 deletions(-) diff --git a/Preferences.cc b/Preferences.cc index 2634c0b9..fba265d1 100644 --- a/Preferences.cc +++ b/Preferences.cc @@ -22,6 +22,7 @@ #include #include +#include Preferences *Preferences::instance = NULL; @@ -29,6 +30,11 @@ Preferences::Preferences(QWidget *parent) : QMainWindow(parent) { setupUi(this); + // Setup default settings + this->defaultmap["3dview/colorscheme"] = this->colorSchemeChooser->currentItem()->text(); + this->defaultmap["editor/fontfamily"] = this->fontChooser->currentText(); + this->defaultmap["editor/fontsize"] = this->fontSize->currentText().toUInt(); + // Toolbar QActionGroup *group = new QActionGroup(this); group->addAction(prefsAction3DView); @@ -40,7 +46,6 @@ Preferences::Preferences(QWidget *parent) : QMainWindow(parent) this->actionTriggered(this->prefsAction3DView); // 3D View pane - this->colorscheme = this->colorSchemeChooser->item(0)->text(); this->colorschemes["Cornfield"][BACKGROUND_COLOR] = QColor(0xff, 0xff, 0xe5); this->colorschemes["Cornfield"][OPENCSG_FACE_FRONT_COLOR] = QColor(0xf9, 0xd7, 0x2c); this->colorschemes["Cornfield"][OPENCSG_FACE_BACK_COLOR] = QColor(0x9d, 0xcb, 0x51); @@ -74,26 +79,25 @@ Preferences::Preferences(QWidget *parent) : QMainWindow(parent) this->colorschemes["Sunset"][CGAL_EDGE_2D_COLOR] = QColor(0xff, 0x00, 0x00); this->colorschemes["Sunset"][CROSSHAIR_COLOR] = QColor(0x80, 0x00, 0x00); - connect(this->colorSchemeChooser, SIGNAL(itemSelectionChanged()), - this, SLOT(colorSchemeChanged())); - // Editor pane QFontDatabase db; foreach(int size, db.standardSizes()) { this->fontSize->addItem(QString::number(size)); } - this->fontSize->setCurrentIndex(this->fontSize->findText(QString::number(12))); - fontFamilyChanged(this->fontChooser->currentText()); - fontSizeChanged(this->fontSize->currentText()); + connect(this->colorSchemeChooser, SIGNAL(itemSelectionChanged()), + this, SLOT(colorSchemeChanged())); connect(this->fontChooser, SIGNAL(activated(const QString &)), this, SLOT(fontFamilyChanged(const QString &))); - connect(this->fontSize, SIGNAL(activated(const QString &)), + connect(this->fontSize, SIGNAL(editTextChanged(const QString &)), this, SLOT(fontSizeChanged(const QString &))); + + updateGUI(); } Preferences::~Preferences() { + removeDefaultSettings(); } void @@ -112,25 +116,30 @@ Preferences::actionTriggered(QAction *action) void Preferences::colorSchemeChanged() { - this->colorscheme = this->colorSchemeChooser->currentItem()->text(); + QSettings settings; + settings.setValue("3dview/colorscheme", this->colorSchemeChooser->currentItem()->text()); + emit requestRedraw(); } const QColor &Preferences::color(RenderColor idx) { - return this->colorschemes[this->colorscheme][idx]; + return this->colorschemes[getValue("3dview/colorscheme").toString()][idx]; } void Preferences::fontFamilyChanged(const QString &family) { - this->fontfamily = family; - emit fontChanged(this->fontfamily, this->fontsize); + QSettings settings; + settings.setValue("editor/fontfamily", family); + emit fontChanged(family, getValue("editor/fontsize").toUInt()); } void Preferences::fontSizeChanged(const QString &size) { - this->fontsize = size.toUInt(); - emit fontChanged(this->fontfamily, this->fontsize); + uint intsize = size.toUInt(); + QSettings settings; + settings.setValue("editor/fontsize", intsize); + emit fontChanged(getValue("editor/fontfamily").toString(), intsize); } void Preferences::keyPressEvent(QKeyEvent *e) @@ -145,3 +154,55 @@ void Preferences::keyPressEvent(QKeyEvent *e) close(); } } + +/*! + Removes settings that are the same as the default settings to avoid + overwriting future changes to default settings. + */ +void Preferences::removeDefaultSettings() +{ + QSettings settings; + for (QSettings::SettingsMap::const_iterator iter = this->defaultmap.begin(); + iter != this->defaultmap.end(); + iter++) { + if (settings.value(iter.key()) == iter.value()) { + settings.remove(iter.key()); + } + } +} + +QVariant Preferences::getValue(const QString &key) const +{ + QSettings settings; + return settings.value(key, this->defaultmap[key]); +} + +void Preferences::updateGUI() +{ + QList found = + this->colorSchemeChooser->findItems(getValue("3dview/colorscheme").toString(), + Qt::MatchExactly); + if (!found.isEmpty()) this->colorSchemeChooser->setCurrentItem(found.first()); + + QString fontfamily = getValue("editor/fontfamily").toString(); + int fidx = this->fontChooser->findText(fontfamily); + if (fidx >= 0) { + this->fontChooser->setCurrentIndex(fidx); + } + + QString fontsize = getValue("editor/fontsize").toString(); + int sidx = this->fontSize->findText(fontsize); + if (sidx >= 0) { + this->fontSize->setCurrentIndex(sidx); + } + else { + this->fontSize->setEditText(fontsize); + } +} + +void Preferences::apply() const +{ + emit fontChanged(getValue("editor/fontfamily").toString(), getValue("editor/fontsize").toUInt()); + emit requestRedraw(); +} + diff --git a/Preferences.h b/Preferences.h index 24b734eb..39599fd6 100644 --- a/Preferences.h +++ b/Preferences.h @@ -2,6 +2,7 @@ #define PREFERENCES_H_ #include +#include #include "ui_Preferences.h" class Preferences : public QMainWindow, public Ui::Preferences @@ -25,6 +26,8 @@ public: CROSSHAIR_COLOR }; const QColor &color(RenderColor idx); + QVariant getValue(const QString &key) const; + void apply() const; public slots: void actionTriggered(class QAction *); @@ -33,17 +36,17 @@ public slots: void fontSizeChanged(const QString &); signals: - void requestRedraw(); - void fontChanged(const QString &family, uint size); + void requestRedraw() const; + void fontChanged(const QString &family, uint size) const; private: Preferences(QWidget *parent = NULL); void keyPressEvent(QKeyEvent *e); + void updateGUI(); + void removeDefaultSettings(); + QSettings::SettingsMap defaultmap; QHash > colorschemes; - QString colorscheme; - QString fontfamily; - uint fontsize; static Preferences *instance; }; diff --git a/mainwin.cc b/mainwin.cc index 0797a469..5ad510bf 100644 --- a/mainwin.cc +++ b/mainwin.cc @@ -288,6 +288,8 @@ MainWindow::MainWindow(const char *filename) connect(Preferences::inst(), SIGNAL(requestRedraw()), this->screen, SLOT(updateGL())); connect(Preferences::inst(), SIGNAL(fontChanged(const QString&,uint)), this, SLOT(setFont(const QString&,uint))); + Preferences::inst()->apply(); + // display this window and check for OpenGL 2.0 (OpenCSG) support viewModeThrownTogether();