diff --git a/src/Preferences.cc b/src/Preferences.cc index 7265d1a3..9969c91f 100644 --- a/src/Preferences.cc +++ b/src/Preferences.cc @@ -46,6 +46,29 @@ Preferences *Preferences::instance = NULL; const char * Preferences::featurePropertyName = "FeatureProperty"; Q_DECLARE_METATYPE(Feature *); +class SettingsReader : public Settings::Visitor +{ + QSettings settings; + virtual void handle(Settings::SettingsEntryBase * entry) const { + std::string key = entry->category() + "/" + entry->name(); + std::string value = settings.value(QString::fromStdString(key)).toString().toStdString(); + entry->from_string(value); + } +}; + +class SettingsWriter : public Settings::Visitor +{ + virtual void handle(Settings::SettingsEntryBase * entry) const { + QSettings settings; + QString key = QString::fromStdString(entry->category() + "/" + entry->name()); + if (entry->is_default()) { + settings.remove(key); + } else { + settings.setValue(key, QString::fromStdString(entry->to_string())); + } + } +}; + Preferences::Preferences(QWidget *parent) : QMainWindow(parent) { setupUi(this); @@ -141,6 +164,11 @@ void Preferences::init() { #endif this->polysetCacheSizeEdit->setValidator(validator); this->opencsgLimitEdit->setValidator(validator); + + Settings::Settings *s = Settings::Settings::inst(); + SettingsReader settingsReader; + s->visit(&settingsReader); + emit editorConfigChanged(); } Preferences::~Preferences() @@ -410,72 +438,79 @@ void Preferences::on_launcherBox_toggled(bool state) void Preferences::on_spinBoxIndentationWidth_valueChanged(int val) { Settings::Settings::inst()->set(Settings::Settings::indentationWidth, val); - emit editorConfigChanged(); + fireEditorConfigChanged(); } void Preferences::on_spinBoxTabWidth_valueChanged(int val) { Settings::Settings::inst()->set(Settings::Settings::tabWidth, val); - emit editorConfigChanged(); + fireEditorConfigChanged(); } void Preferences::on_comboBoxLineWrap_activated(int val) { Settings::Settings::inst()->set(Settings::Settings::lineWrap, (Settings::LineWrap)val); - emit editorConfigChanged(); + fireEditorConfigChanged(); } void Preferences::on_comboBoxLineWrapIndentation_activated(int val) { Settings::Settings::inst()->set(Settings::Settings::lineWrapIndentationStyle, (Settings::LineWrapIndentationStyle)val); - emit editorConfigChanged(); + fireEditorConfigChanged(); } void Preferences::on_spinBoxLineWrapIndentationIndent_valueChanged(int val) { Settings::Settings::inst()->set(Settings::Settings::lineWrapIndentation, val); - emit editorConfigChanged(); + fireEditorConfigChanged(); } void Preferences::on_comboBoxLineWrapVisualizationStart_activated(int val) { Settings::Settings::inst()->set(Settings::Settings::lineWrapVisualizationBegin, (Settings::LineWrapVisualization)val); - emit editorConfigChanged(); + fireEditorConfigChanged(); } void Preferences::on_comboBoxLineWrapVisualizationEnd_activated(int val) { Settings::Settings::inst()->set(Settings::Settings::lineWrapVisualizationEnd, (Settings::LineWrapVisualization)val); - emit editorConfigChanged(); + fireEditorConfigChanged(); } void Preferences::on_comboBoxShowWhitespaces_activated(int val) { Settings::Settings::inst()->set(Settings::Settings::showWhitespaces, (Settings::ShowWhitespaces)val); - emit editorConfigChanged(); + fireEditorConfigChanged(); } void Preferences::on_spinBoxShowWhitespacesSize_valueChanged(int val) { Settings::Settings::inst()->set(Settings::Settings::showWhitespacesSize, val); - emit editorConfigChanged(); + fireEditorConfigChanged(); } void Preferences::on_checkBoxAutoIndent_toggled(bool val) { Settings::Settings::inst()->set(Settings::Settings::autoIndent, val); - emit editorConfigChanged(); + fireEditorConfigChanged(); } void Preferences::on_checkBoxTabIndents_toggled(bool val) { Settings::Settings::inst()->set(Settings::Settings::tabIndents, val); - emit editorConfigChanged(); + fireEditorConfigChanged(); } void Preferences::on_checkBoxIndentationsUseTabs_toggled(bool val) { Settings::Settings::inst()->set(Settings::Settings::indentationsUseTabs, val); + fireEditorConfigChanged(); +} + +void Preferences::fireEditorConfigChanged() const +{ + SettingsWriter settingsWriter; + Settings::Settings::inst()->visit(&settingsWriter); emit editorConfigChanged(); } diff --git a/src/Preferences.h b/src/Preferences.h index f4122dff..e1db37d0 100644 --- a/src/Preferences.h +++ b/src/Preferences.h @@ -75,6 +75,7 @@ private: void updateGUI(); void removeDefaultSettings(); void setupFeaturesPage(); + void fireEditorConfigChanged() const; void addPrefPage(QActionGroup *group, QAction *action, QWidget *widget); QSettings::SettingsMap defaultmap; diff --git a/src/mainwin.cc b/src/mainwin.cc index 231eeb08..e4a01584 100644 --- a/src/mainwin.cc +++ b/src/mainwin.cc @@ -204,6 +204,7 @@ MainWindow::MainWindow(const QString &filename) #ifdef USE_SCINTILLA_EDITOR if (useScintilla) { connect(Preferences::inst(), SIGNAL(editorConfigChanged()), editor, SLOT(applySettings())); + Preferences::inst()->editorConfigChanged(); } #endif diff --git a/src/scintillaeditor.cpp b/src/scintillaeditor.cpp index 7836d6b1..c284349b 100644 --- a/src/scintillaeditor.cpp +++ b/src/scintillaeditor.cpp @@ -146,7 +146,7 @@ ScintillaEditor::ScintillaEditor(QWidget *parent) : EditorInterface(parent) qsci->indicatorDefine(QsciScintilla::RoundBoxIndicator, indicatorNumber); qsci->markerDefine(QsciScintilla::Circle, markerNumber); qsci->setUtf8(true); - applySettings(); + qsci->setFolding(QsciScintilla::BoxedTreeFoldStyle, 4); lexer = new ScadLexer(this); qsci->setLexer(lexer); @@ -179,7 +179,6 @@ void ScintillaEditor::applySettings() qsci->setIndentationsUseTabs(s->get(Settings::Settings::indentationsUseTabs)); qsci->setBraceMatching(QsciScintilla::SloppyBraceMatch); - qsci->setFolding(QsciScintilla::BoxedTreeFoldStyle, 4); qsci->setCaretLineVisible(true); } diff --git a/src/settings.cc b/src/settings.cc index 6c1fe2c8..f2137ce6 100644 --- a/src/settings.cc +++ b/src/settings.cc @@ -1,10 +1,32 @@ #include "settings.h" +#include "value.h" namespace Settings { +std::list SettingsEntryBase::entries; + +SettingsEntryBase::SettingsEntryBase(const std::string category, const std::string name) : _category(category), _name(name) +{ + entries.push_back(this); +} + +SettingsEntryBase::~SettingsEntryBase() +{ +} + +const std::string & SettingsEntryBase::category() const +{ + return _category; +} + +const std::string & SettingsEntryBase::name() const +{ + return _name; +} + template SettingsEntry::SettingsEntry(const std::string category, const std::string name, T defaultValue) - : category(category), name(name), value(defaultValue), defaultValue(defaultValue) + : SettingsEntryBase(category, name), value(defaultValue), defaultValue(defaultValue) { } @@ -13,6 +35,74 @@ SettingsEntry::~SettingsEntry() { } +template +bool SettingsEntry::is_default() const +{ + return defaultValue == value; +} + +template +std::string SettingsEntry::to_string() const +{ + return boost::lexical_cast(value); +} + +template <> +std::string SettingsEntry::to_string() const +{ + return boost::lexical_cast(value); +} + +template +void SettingsEntry::from_string(std::string val) +{ + try { + value = boost::lexical_cast(val); + } catch (const boost::bad_lexical_cast &e) { + value = defaultValue; + } +} + +template <> +void SettingsEntry::from_string(std::string val) +{ + try { + value = (LineWrap)boost::lexical_cast(val); + } catch (const boost::bad_lexical_cast &e) { + value = defaultValue; + } +} + +template <> +void SettingsEntry::from_string(std::string val) +{ + try { + value = (LineWrapIndentationStyle)boost::lexical_cast(val); + } catch (const boost::bad_lexical_cast &e) { + value = defaultValue; + } +} + +template <> +void SettingsEntry::from_string(std::string val) +{ + try { + value = (LineWrapVisualization)boost::lexical_cast(val); + } catch (const boost::bad_lexical_cast &e) { + value = defaultValue; + } +} + +template <> +void SettingsEntry::from_string(std::string val) +{ + try { + value = (ShowWhitespaces)boost::lexical_cast(val); + } catch (const boost::bad_lexical_cast &e) { + value = defaultValue; + } +} + SettingsEntry Settings::indentationWidth("editor", "indentationWidth", 4); SettingsEntry Settings::tabWidth("editor", "tabWidth", 8); SettingsEntry Settings::lineWrap("editor", "lineWrap", LINE_WRAP_WORD); @@ -46,6 +136,13 @@ Settings::~Settings() { } +void Settings::visit(class Visitor *visitor) +{ + for (std::list::iterator it = SettingsEntryBase::entries.begin();it != SettingsEntryBase::entries.end();it++) { + visitor->handle(*it); + } +} + template T Settings::defaultValue(const SettingsEntry &entry) { @@ -88,4 +185,12 @@ template ShowWhitespaces Settings::defaultValue(const SettingsEntry&); template void Settings::set(SettingsEntry&, ShowWhitespaces); -} \ No newline at end of file +Visitor::Visitor() +{ +} + +Visitor::~Visitor() +{ +} + +} diff --git a/src/settings.h b/src/settings.h index 4b0dc169..445a9a8a 100644 --- a/src/settings.h +++ b/src/settings.h @@ -1,6 +1,7 @@ #pragma once #include +#include #include namespace Settings { @@ -30,17 +31,42 @@ typedef enum { SHOW_WHITESPACES_AFTER_INDENTATION, } ShowWhitespaces; -template -class SettingsEntry +class SettingsEntryBase +{ +private: + std::string _category; + std::string _name; + +public: + const std::string & category() const; + const std::string & name() const; + + virtual bool is_default() const = 0; + virtual std::string to_string() const = 0; + virtual void from_string(std::string) = 0; + +protected: + SettingsEntryBase(const std::string category, const std::string name); + ~SettingsEntryBase(); + + static std::list entries; + + friend class Settings; +}; + +template +class SettingsEntry : public SettingsEntryBase { private: - std::string category; - std::string name; T value; T defaultValue; SettingsEntry(const std::string category, const std::string name, T defaultValue); virtual ~SettingsEntry(); + + virtual bool is_default() const; + virtual std::string to_string() const; + virtual void from_string(std::string); friend class Settings; }; @@ -62,7 +88,9 @@ public: static SettingsEntry indentationsUseTabs; static Settings *inst(bool erase = false); - + + void visit(class Visitor *visitor); + template T defaultValue(const SettingsEntry &entry); template T get(const SettingsEntry &entry); template void set(SettingsEntry &entry, T val); @@ -72,4 +100,13 @@ private: virtual ~Settings(); }; +class Visitor +{ +public: + Visitor(); + virtual ~Visitor(); + + virtual void handle(SettingsEntryBase * entry) const = 0; +}; + } \ No newline at end of file