From 3a5ee3a4c607c1e79ec4d7b792fb72b9c22fef22 Mon Sep 17 00:00:00 2001 From: Torsten Paul Date: Sat, 27 Dec 2014 00:12:17 +0100 Subject: [PATCH 1/8] Add GUI controls for editor settings. --- src/Preferences.ui | 255 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 252 insertions(+), 3 deletions(-) diff --git a/src/Preferences.ui b/src/Preferences.ui index eff8da96..cd3b88b7 100644 --- a/src/Preferences.ui +++ b/src/Preferences.ui @@ -7,7 +7,7 @@ 0 0 852 - 554 + 592 @@ -27,7 +27,7 @@ - 4 + 1 @@ -289,6 +289,255 @@ + + + + Editor settings + + + + 20 + + + + + + None + + + + + Wrap at character boundaries + + + + + Wrap at word boundaries + + + + + + + + Qt::Horizontal + + + QSizePolicy::Fixed + + + + 20 + 2 + + + + + + + + Show whitespaces + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + Never + + + + + Always + + + + + Only after indentation + + + + + + + + + 0 + 0 + + + + + None + + + + + Text + + + + + Border + + + + + Margin + + + + + + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + + Start + + + + + + + Line wrap visualization + + + + + + + Line wrap indentation + + + + + + + Tab width + + + + + + + Line wrap + + + + + + + End + + + + + + + + 0 + 0 + + + + + None + + + + + Text + + + + + Border + + + + + Margin + + + + + + + + Indent + + + + + + + + Fixed + + + + + Same + + + + + Indented + + + + + + + + Style + + + + + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + + Indentation width + + + + + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + @@ -646,7 +895,7 @@ - + Enable user interface localization (requires restart of OpenSCAD) From dde4575c882a79ac46c9906083d9bfc2a51140a3 Mon Sep 17 00:00:00 2001 From: Torsten Paul Date: Wed, 24 Sep 2014 22:18:14 +0200 Subject: [PATCH 2/8] Add class to handle settings. --- openscad.pro | 2 ++ src/mainwin.cc | 1 + src/settings.cc | 45 +++++++++++++++++++++++++++++++++++++++++++++ src/settings.h | 40 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 88 insertions(+) create mode 100644 src/settings.cc create mode 100644 src/settings.h diff --git a/openscad.pro b/openscad.pro index d9b99c73..80785f84 100644 --- a/openscad.pro +++ b/openscad.pro @@ -247,6 +247,7 @@ HEADERS += src/typedefs.h \ src/ProgressWidget.h \ src/parsersettings.h \ src/renderer.h \ + src/settings.h \ src/rendersettings.h \ src/colormap.h \ src/ThrownTogetherRenderer.h \ @@ -399,6 +400,7 @@ SOURCES += src/version_check.cc \ src/FreetypeRenderer.cc \ src/FontCache.cc \ \ + src/settings.cc \ src/rendersettings.cc \ src/highlighter.cc \ src/Preferences.cc \ diff --git a/src/mainwin.cc b/src/mainwin.cc index cfb44bda..0bcfb34f 100644 --- a/src/mainwin.cc +++ b/src/mainwin.cc @@ -43,6 +43,7 @@ #include "progress.h" #include "dxfdim.h" #include "legacyeditor.h" +#include "settings.h" #ifdef USE_SCINTILLA_EDITOR #include "scintillaeditor.h" #endif diff --git a/src/settings.cc b/src/settings.cc new file mode 100644 index 00000000..cf352fdd --- /dev/null +++ b/src/settings.cc @@ -0,0 +1,45 @@ +#include "settings.h" + +template +SettingsEntry::SettingsEntry(const std::string category, const std::string name, T defaultValue) + : category(category), name(name), defaultValue(defaultValue) +{ +} + +template +SettingsEntry::~SettingsEntry() +{ +} + +SettingsEntry Settings::indentationWidth("editor", "indentationWidth", 4); +SettingsEntry Settings::tabWidth("editor", "tabWidth", 8); +SettingsEntry Settings::lineWrap("editor", "lineWrap", LINE_WRAP_WORD); + +Settings *Settings::inst(bool erase) +{ + static Settings *instance = new Settings; + + if (erase) { + delete instance; + instance = NULL; + } + + return instance; +} + +Settings::Settings() +{ +} + +Settings::~Settings() +{ +} + +template +T Settings::defaultValue(const SettingsEntry &entry) +{ + return entry.defaultValue; +} + +template int Settings::defaultValue(const SettingsEntry&); +template SettingsLineWrap Settings::defaultValue(const SettingsEntry&); diff --git a/src/settings.h b/src/settings.h new file mode 100644 index 00000000..18e69c4d --- /dev/null +++ b/src/settings.h @@ -0,0 +1,40 @@ +#pragma once + +#include +#include + +typedef enum { + LINE_WRAP_NONE, + LINE_WRAP_CHARACTER, + LINE_WRAP_WORD, +} SettingsLineWrap; + +template +class SettingsEntry +{ + std::string category; + std::string name; + T value; + T defaultValue; + + SettingsEntry(const std::string category, const std::string name, T defaultValue); + virtual ~SettingsEntry(); + + friend class Settings; +}; + +class Settings +{ +public: + static SettingsEntry indentationWidth; + static SettingsEntry tabWidth; + static SettingsEntry lineWrap; + + static Settings *inst(bool erase = false); + + template T defaultValue(const SettingsEntry &entry); + +private: + Settings(); + virtual ~Settings(); +}; From 0cb6b3267a082a0f0ab6fdfd191d48202a4c8bc5 Mon Sep 17 00:00:00 2001 From: Torsten Paul Date: Sat, 27 Dec 2014 02:17:15 +0100 Subject: [PATCH 3/8] Add editor settings. --- src/Preferences.cc | 69 +++- src/Preferences.h | 12 + src/Preferences.ui | 72 +++-- src/mainwin.cc | 7 + src/scintillaeditor.cpp | 683 ++++++++++++++++++++++------------------ src/scintillaeditor.h | 1 + src/settings.cc | 45 ++- src/settings.h | 38 ++- 8 files changed, 599 insertions(+), 328 deletions(-) diff --git a/src/Preferences.cc b/src/Preferences.cc index 9c8f9b59..45afcba9 100644 --- a/src/Preferences.cc +++ b/src/Preferences.cc @@ -39,6 +39,7 @@ #endif #include "colormap.h" #include "rendersettings.h" +#include "settings.h" Preferences *Preferences::instance = NULL; @@ -400,13 +401,66 @@ void Preferences::on_mouseWheelZoomBox_toggled(bool state) settings.setValue("editor/ctrlmousewheelzoom", state); } -void -Preferences::on_launcherBox_toggled(bool state) +void Preferences::on_launcherBox_toggled(bool state) { QSettings settings; settings.setValue("launcher/showOnStartup", state); } +void Preferences::on_spinBoxIndentationWidth_valueChanged(int val) +{ + Settings::Settings::inst()->set(Settings::Settings::indentationWidth, val); + emit editorConfigChanged(); +} + +void Preferences::on_spinBoxTabWidth_valueChanged(int val) +{ + Settings::Settings::inst()->set(Settings::Settings::tabWidth, val); + emit editorConfigChanged(); +} + +void Preferences::on_comboBoxLineWrap_activated(int val) +{ + Settings::Settings::inst()->set(Settings::Settings::lineWrap, (Settings::LineWrap)val); + emit editorConfigChanged(); +} + +void Preferences::on_comboBoxLineWrapIndentation_activated(int val) +{ + Settings::Settings::inst()->set(Settings::Settings::lineWrapIndentationStyle, (Settings::LineWrapIndentationStyle)val); + emit editorConfigChanged(); +} + +void Preferences::on_spinBoxLineWrapIndentationIndent_valueChanged(int val) +{ + Settings::Settings::inst()->set(Settings::Settings::lineWrapIndentation, val); + emit editorConfigChanged(); +} + +void Preferences::on_comboBoxLineWrapVisualizationStart_activated(int val) +{ + Settings::Settings::inst()->set(Settings::Settings::lineWrapVisualizationBegin, (Settings::LineWrapVisualization)val); + emit editorConfigChanged(); +} + +void Preferences::on_comboBoxLineWrapVisualizationEnd_activated(int val) +{ + Settings::Settings::inst()->set(Settings::Settings::lineWrapVisualizationEnd, (Settings::LineWrapVisualization)val); + emit editorConfigChanged(); +} + +void Preferences::on_comboBoxShowWhitespaces_activated(int val) +{ + Settings::Settings::inst()->set(Settings::Settings::showWhitespaces, (Settings::ShowWhitespaces)val); + emit editorConfigChanged(); +} + +void Preferences::on_spinBoxShowWhitespacesSize_valueChanged(int val) +{ + Settings::Settings::inst()->set(Settings::Settings::showWhitespacesSize, val); + emit editorConfigChanged(); +} + void Preferences::keyPressEvent(QKeyEvent *e) { #ifdef Q_OS_MAC @@ -500,6 +554,17 @@ void Preferences::updateGUI() this->undockCheckBox->setChecked(getValue("advanced/undockableWindows").toBool()); this->undockCheckBox->setEnabled(this->reorderCheckBox->isChecked()); this->launcherBox->setChecked(getValue("launcher/showOnStartup").toBool()); + + Settings::Settings *s = Settings::Settings::inst(); + this->spinBoxIndentationWidth->setValue(s->get(Settings::Settings::indentationWidth)); + this->spinBoxTabWidth->setValue(s->get(Settings::Settings::tabWidth)); + this->comboBoxLineWrap->setCurrentIndex(s->get(Settings::Settings::lineWrap)); + this->comboBoxLineWrapIndentation->setCurrentIndex(s->get(Settings::Settings::lineWrapIndentationStyle)); + this->spinBoxLineWrapIndentationIndent->setValue(s->get(Settings::Settings::lineWrapIndentation)); + this->comboBoxLineWrapVisualizationStart->setCurrentIndex(s->get(Settings::Settings::lineWrapVisualizationBegin)); + this->comboBoxLineWrapVisualizationEnd->setCurrentIndex(s->get(Settings::Settings::lineWrapVisualizationEnd)); + this->comboBoxShowWhitespaces->setCurrentIndex(s->get(Settings::Settings::showWhitespaces)); + this->spinBoxShowWhitespacesSize->setValue(s->get(Settings::Settings::showWhitespacesSize)); } void Preferences::apply() const diff --git a/src/Preferences.h b/src/Preferences.h index cd89fa97..e55420f7 100644 --- a/src/Preferences.h +++ b/src/Preferences.h @@ -43,6 +43,17 @@ public slots: void on_launcherBox_toggled(bool); void on_editorType_editTextChanged(const QString &); + // editor settings + void on_spinBoxIndentationWidth_valueChanged(int); + void on_spinBoxTabWidth_valueChanged(int); + void on_comboBoxLineWrap_activated(int); + void on_comboBoxLineWrapIndentation_activated(int); + void on_spinBoxLineWrapIndentationIndent_valueChanged(int); + void on_comboBoxLineWrapVisualizationStart_activated(int); + void on_comboBoxLineWrapVisualizationEnd_activated(int); + void on_comboBoxShowWhitespaces_activated(int); + void on_spinBoxShowWhitespacesSize_valueChanged(int); + signals: void requestRedraw() const; void updateMdiMode(bool mdi) const; @@ -53,6 +64,7 @@ signals: void openCSGSettingsChanged() const; void syntaxHighlightChanged(const QString &s) const; void editorTypeChanged(const QString &type); + void editorConfigChanged() const; private: Preferences(QWidget *parent = NULL); diff --git a/src/Preferences.ui b/src/Preferences.ui index cd3b88b7..f4374141 100644 --- a/src/Preferences.ui +++ b/src/Preferences.ui @@ -334,9 +334,9 @@ - + - Show whitespaces + Whitespaces @@ -353,25 +353,6 @@ - - - - - Never - - - - - Always - - - - - Only after indentation - - - - @@ -533,6 +514,55 @@ Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + 99 + + + + + + + + Never + + + + + Always + + + + + Only after indentation + + + + + + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + 1 + + + 9 + + + + + + + Show + + + + + + + Size + diff --git a/src/mainwin.cc b/src/mainwin.cc index 0bcfb34f..231eeb08 100644 --- a/src/mainwin.cc +++ b/src/mainwin.cc @@ -193,6 +193,7 @@ MainWindow::MainWindow(const QString &filename) #ifdef USE_SCINTILLA_EDITOR if (useScintilla) { editor = new ScintillaEditor(editorDockContents); + } else #endif @@ -200,6 +201,12 @@ MainWindow::MainWindow(const QString &filename) Preferences::create(editor->colorSchemes()); +#ifdef USE_SCINTILLA_EDITOR + if (useScintilla) { + connect(Preferences::inst(), SIGNAL(editorConfigChanged()), editor, SLOT(applySettings())); + } +#endif + editorDockContents->layout()->addWidget(editor); setCorner(Qt::TopLeftCorner, Qt::LeftDockWidgetArea); diff --git a/src/scintillaeditor.cpp b/src/scintillaeditor.cpp index 2d0f07d0..309cda34 100644 --- a/src/scintillaeditor.cpp +++ b/src/scintillaeditor.cpp @@ -6,489 +6,574 @@ #include #include "Preferences.h" #include "PlatformUtils.h" +#include "settings.h" + +class SettingsConverter { +public: + QsciScintilla::WrapMode fromWrapMode(Settings::LineWrap val); + QsciScintilla::WrapVisualFlag fromLineWrapVisualization(Settings::LineWrapVisualization val); + QsciScintilla::WrapIndentMode fromLineWrapIndentationStyle(Settings::LineWrapIndentationStyle val); + QsciScintilla::WhitespaceVisibility fromShowWhitespaces(Settings::ShowWhitespaces val); +}; + +QsciScintilla::WrapMode SettingsConverter::fromWrapMode(Settings::LineWrap val) +{ + switch (val) { + case Settings::LINE_WRAP_NONE: + return QsciScintilla::WrapNone; + case Settings::LINE_WRAP_CHARACTER: + return QsciScintilla::WrapCharacter; + case Settings::LINE_WRAP_WORD: + return QsciScintilla::WrapWord; + default: + assert(false && "unknown wrap mode setting"); + } +} + +QsciScintilla::WrapVisualFlag SettingsConverter::fromLineWrapVisualization(Settings::LineWrapVisualization val) +{ + switch (val) { + case Settings::LINE_WRAP_VISUALIZATION_NONE: + return QsciScintilla::WrapFlagNone; + case Settings::LINE_WRAP_VISUALIZATION_TEXT: + return QsciScintilla::WrapFlagByText; + case Settings::LINE_WRAP_VISUALIZATION_BORDER: + return QsciScintilla::WrapFlagByBorder; + case Settings::LINE_WRAP_VISUALIZATION_MARGIN: + return QsciScintilla::WrapFlagInMargin; + default: + assert(false && "unknown wrap visualization setting"); + } +} + +QsciScintilla::WrapIndentMode SettingsConverter::fromLineWrapIndentationStyle(Settings::LineWrapIndentationStyle val) +{ + switch (val) { + case Settings::LINE_WRAP_INDENTATION_FIXED: + return QsciScintilla::WrapIndentFixed; + case Settings::LINE_WRAP_INDENTATION_SAME: + return QsciScintilla::WrapIndentSame; + case Settings::LINE_WRAP_INDENTATION_INDENTED: + return QsciScintilla::WrapIndentIndented; + default: + assert(false && "unknown wrap indentation style setting"); + } +} + +QsciScintilla::WhitespaceVisibility SettingsConverter::fromShowWhitespaces(Settings::ShowWhitespaces val) +{ + switch(val) { + case Settings::SHOW_WHITESPACES_NEVER: + return QsciScintilla::WsInvisible; + case Settings::SHOW_WHITESPACES_ALWAYS: + return QsciScintilla::WsVisible; + case Settings::SHOW_WHITESPACES_AFTER_INDENTATION: + return QsciScintilla::WsVisibleAfterIndent; + default: + assert(false && "unknown show whitespace setting"); + } +} EditorColorScheme::EditorColorScheme(fs::path path) : path(path) { - try { - boost::property_tree::read_json(boosty::stringy(path).c_str(), pt); - _name = QString(pt.get("name").c_str()); - _index = pt.get("index"); - } catch (const std::exception & e) { - PRINTB("Error reading color scheme file '%s': %s", path.c_str() % e.what()); - _name = ""; - _index = 0; - } + try { + boost::property_tree::read_json(boosty::stringy(path).c_str(), pt); + _name = QString(pt.get("name").c_str()); + _index = pt.get("index"); + } catch (const std::exception & e) { + PRINTB("Error reading color scheme file '%s': %s", path.c_str() % e.what()); + _name = ""; + _index = 0; + } } EditorColorScheme::~EditorColorScheme() { - + } bool EditorColorScheme::valid() const { - return !_name.isEmpty(); + return !_name.isEmpty(); } const QString & EditorColorScheme::name() const { - return _name; + return _name; } int EditorColorScheme::index() const { - return _index; + return _index; } const boost::property_tree::ptree & EditorColorScheme::propertyTree() const { - return pt; + return pt; } ScintillaEditor::ScintillaEditor(QWidget *parent) : EditorInterface(parent) { - scintillaLayout = new QVBoxLayout(this); - qsci = new QsciScintilla(this); + scintillaLayout = new QVBoxLayout(this); + qsci = new QsciScintilla(this); - // Force EOL mode to Unix, since QTextStream will manage local EOL modes. - qsci->setEolMode(QsciScintilla::EolUnix); + // Force EOL mode to Unix, since QTextStream will manage local EOL modes. + qsci->setEolMode(QsciScintilla::EolUnix); - // - // Remapping some scintilla key binding which conflict with OpenSCAD global - // key bindings, as well as some minor scintilla bugs - // - QsciCommand *c; + // + // Remapping some scintilla key binding which conflict with OpenSCAD global + // key bindings, as well as some minor scintilla bugs + // + QsciCommand *c; #ifdef Q_OS_MAC - // Alt-Backspace should delete left word (Alt-Delete already deletes right word) - c= qsci->standardCommands()->find(QsciCommand::DeleteWordLeft); - c->setKey(Qt::Key_Backspace | Qt::ALT); + // Alt-Backspace should delete left word (Alt-Delete already deletes right word) + c = qsci->standardCommands()->find(QsciCommand::DeleteWordLeft); + c->setKey(Qt::Key_Backspace | Qt::ALT); #endif - // Cmd/Ctrl-T is handled by the menu - c = qsci->standardCommands()->boundTo(Qt::Key_T | Qt::CTRL); - c->setKey(0); - // Cmd/Ctrl-D is handled by the menu - c = qsci->standardCommands()->boundTo(Qt::Key_D | Qt::CTRL); - c->setKey(0); - // Ctrl-Shift-Z should redo on all platforms - c= qsci->standardCommands()->find(QsciCommand::Redo); - c->setKey(Qt::Key_Z | Qt::CTRL | Qt::SHIFT); + // Cmd/Ctrl-T is handled by the menu + c = qsci->standardCommands()->boundTo(Qt::Key_T | Qt::CTRL); + c->setKey(0); + // Cmd/Ctrl-D is handled by the menu + c = qsci->standardCommands()->boundTo(Qt::Key_D | Qt::CTRL); + c->setKey(0); + // Ctrl-Shift-Z should redo on all platforms + c = qsci->standardCommands()->find(QsciCommand::Redo); + c->setKey(Qt::Key_Z | Qt::CTRL | Qt::SHIFT); - scintillaLayout->setContentsMargins(0, 0, 0, 0); - scintillaLayout->addWidget(qsci); + scintillaLayout->setContentsMargins(0, 0, 0, 0); + scintillaLayout->addWidget(qsci); - qsci->setBraceMatching (QsciScintilla::SloppyBraceMatch); - qsci->setWrapMode(QsciScintilla::WrapCharacter); - qsci->setWrapVisualFlags(QsciScintilla::WrapFlagByBorder, QsciScintilla::WrapFlagNone, 0); - qsci->setAutoIndent(true); - qsci->indicatorDefine(QsciScintilla::RoundBoxIndicator, indicatorNumber); - qsci->markerDefine(QsciScintilla::Circle, markerNumber); - qsci->setUtf8(true); - qsci->setTabIndents(true); - qsci->setTabWidth(8); - qsci->setIndentationWidth(4); - qsci->setIndentationsUseTabs(false); - - lexer = new ScadLexer(this); - qsci->setLexer(lexer); - initMargin(); - qsci->setFolding(QsciScintilla::BoxedTreeFoldStyle, 4); - qsci->setCaretLineVisible(true); + qsci->indicatorDefine(QsciScintilla::RoundBoxIndicator, indicatorNumber); + qsci->markerDefine(QsciScintilla::Circle, markerNumber); + qsci->setUtf8(true); + applySettings(); - connect(qsci, SIGNAL(textChanged()), this, SIGNAL(contentsChanged())); - connect(qsci, SIGNAL(modificationChanged(bool)), this, SIGNAL(modificationChanged(bool))); + lexer = new ScadLexer(this); + qsci->setLexer(lexer); + initMargin(); + + connect(qsci, SIGNAL(textChanged()), this, SIGNAL(contentsChanged())); + connect(qsci, SIGNAL(modificationChanged(bool)), this, SIGNAL(modificationChanged(bool))); +} + +/** + * Apply the settings that are changeable in the preferences. This is also + * called in the event handler from the preferences. + */ +void ScintillaEditor::applySettings() +{ + SettingsConverter conv; + Settings::Settings *s = Settings::Settings::inst(); + + qsci->setIndentationWidth(s->get(Settings::Settings::indentationWidth)); + qsci->setTabWidth(s->get(Settings::Settings::tabWidth)); + qsci->setWrapMode(conv.fromWrapMode(s->get(Settings::Settings::lineWrap))); + qsci->setWrapIndentMode(conv.fromLineWrapIndentationStyle(s->get(Settings::Settings::lineWrapIndentationStyle))); + qsci->setWrapVisualFlags(conv.fromLineWrapVisualization(s->get(Settings::Settings::lineWrapVisualizationEnd)), + conv.fromLineWrapVisualization(s->get(Settings::Settings::lineWrapVisualizationBegin)), + s->get(Settings::Settings::lineWrapIndentation)); + qsci->setWhitespaceVisibility(conv.fromShowWhitespaces(s->get(Settings::Settings::showWhitespaces))); + qsci->setWhitespaceSize(s->get(Settings::Settings::showWhitespacesSize)); + + qsci->setBraceMatching(QsciScintilla::SloppyBraceMatch); + qsci->setAutoIndent(true); + qsci->setTabIndents(true); + qsci->setIndentationsUseTabs(false); + qsci->setFolding(QsciScintilla::BoxedTreeFoldStyle, 4); + qsci->setCaretLineVisible(true); } void ScintillaEditor::setPlainText(const QString &text) -{ - qsci->setText(text); - setContentModified(false); +{ + qsci->setText(text); + setContentModified(false); } QString ScintillaEditor::toPlainText() { - return qsci->text(); + return qsci->text(); } void ScintillaEditor::setContentModified(bool modified) { - // FIXME: Due to an issue with QScintilla, we need to do this on the document itself. + // FIXME: Due to an issue with QScintilla, we need to do this on the document itself. #if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0) - qsci->SCN_SAVEPOINTLEFT(); + qsci->SCN_SAVEPOINTLEFT(); #endif - qsci->setModified(modified); + qsci->setModified(modified); } bool ScintillaEditor::isContentModified() { - return qsci->isModified(); + return qsci->isModified(); } -void ScintillaEditor::highlightError(int error_pos) +void ScintillaEditor::highlightError(int error_pos) { - int line, index; - qsci->lineIndexFromPosition(error_pos, &line, &index); - qsci->fillIndicatorRange(line, index, line, index+1, indicatorNumber); - qsci->markerAdd(line, markerNumber); + int line, index; + qsci->lineIndexFromPosition(error_pos, &line, &index); + qsci->fillIndicatorRange(line, index, line, index + 1, indicatorNumber); + qsci->markerAdd(line, markerNumber); } -void ScintillaEditor::unhighlightLastError() +void ScintillaEditor::unhighlightLastError() { - int totalLength = qsci->text().length(); - int line, index; - qsci->lineIndexFromPosition(totalLength, &line, &index); - qsci->clearIndicatorRange(0, 0, line, index, indicatorNumber); - qsci->markerDeleteAll(markerNumber); + int totalLength = qsci->text().length(); + int line, index; + qsci->lineIndexFromPosition(totalLength, &line, &index); + qsci->clearIndicatorRange(0, 0, line, index, indicatorNumber); + qsci->markerDeleteAll(markerNumber); } QColor ScintillaEditor::readColor(const boost::property_tree::ptree &pt, const std::string name, const QColor defaultColor) { - try { - const std::string val = pt.get(name); - return QColor(val.c_str()); - } catch (std::exception e) { - return defaultColor; - } + try { + const std::string val = pt.get(name); + return QColor(val.c_str()); + } catch (std::exception e) { + return defaultColor; + } } std::string ScintillaEditor::readString(const boost::property_tree::ptree &pt, const std::string name, const std::string defaultValue) { - try { - const std::string val = pt.get(name); - return val; - } catch (std::exception e) { - return defaultValue; - } + try { + const std::string val = pt.get(name); + return val; + } catch (std::exception e) { + return defaultValue; + } } int ScintillaEditor::readInt(const boost::property_tree::ptree &pt, const std::string name, const int defaultValue) { - try { - const int val = pt.get(name); - return val; - } catch (std::exception e) { - return defaultValue; - } + try { + const int val = pt.get(name); + return val; + } catch (std::exception e) { + return defaultValue; + } } void ScintillaEditor::setColormap(const EditorColorScheme *colorScheme) { - const boost::property_tree::ptree & pt = colorScheme->propertyTree(); + const boost::property_tree::ptree & pt = colorScheme->propertyTree(); - try { - QFont font = lexer->font(lexer->defaultStyle()); - const QColor textColor(pt.get("text").c_str()); - const QColor paperColor(pt.get("paper").c_str()); + try { + QFont font = lexer->font(lexer->defaultStyle()); + const QColor textColor(pt.get("text").c_str()); + const QColor paperColor(pt.get("paper").c_str()); - ScadLexer *l = new ScadLexer(this); + ScadLexer *l = new ScadLexer(this); - // Keywords must be set before the lexer is attached to QScintilla - // as they seem to be read and cached at attach time. - boost::optional keywords = pt.get_child_optional("keywords"); - if (keywords.is_initialized()) { - l->setKeywords(1, readString(keywords.get(), "keyword-set1", "")); - l->setKeywords(2, readString(keywords.get(), "keyword-set2", "")); - l->setKeywords(3, readString(keywords.get(), "keyword-set-doc", "")); - l->setKeywords(4, readString(keywords.get(), "keyword-set3", "")); + // Keywords must be set before the lexer is attached to QScintilla + // as they seem to be read and cached at attach time. + boost::optional keywords = pt.get_child_optional("keywords"); + if (keywords.is_initialized()) { + l->setKeywords(1, readString(keywords.get(), "keyword-set1", "")); + l->setKeywords(2, readString(keywords.get(), "keyword-set2", "")); + l->setKeywords(3, readString(keywords.get(), "keyword-set-doc", "")); + l->setKeywords(4, readString(keywords.get(), "keyword-set3", "")); + } + + qsci->setLexer(l); + delete lexer; + lexer = l; + + // All other properties must be set after attaching to QSCintilla so + // the editor gets the change events and updates itself to match + l->setFont(font); + l->setColor(textColor); + l->setPaper(paperColor); + + const boost::property_tree::ptree& colors = pt.get_child("colors"); + l->setColor(readColor(colors, "keyword1", textColor), QsciLexerCPP::Keyword); + l->setColor(readColor(colors, "keyword2", textColor), QsciLexerCPP::KeywordSet2); + l->setColor(readColor(colors, "keyword3", textColor), QsciLexerCPP::GlobalClass); + l->setColor(readColor(colors, "number", textColor), QsciLexerCPP::Number); + l->setColor(readColor(colors, "string", textColor), QsciLexerCPP::SingleQuotedString); + l->setColor(readColor(colors, "string", textColor), QsciLexerCPP::DoubleQuotedString); + l->setColor(readColor(colors, "operator", textColor), QsciLexerCPP::Operator); + l->setColor(readColor(colors, "comment", textColor), QsciLexerCPP::Comment); + l->setColor(readColor(colors, "commentline", textColor), QsciLexerCPP::CommentLine); + l->setColor(readColor(colors, "commentdoc", textColor), QsciLexerCPP::CommentDoc); + l->setColor(readColor(colors, "commentdoc", textColor), QsciLexerCPP::CommentLineDoc); + l->setColor(readColor(colors, "commentdockeyword", textColor), QsciLexerCPP::CommentDocKeyword); + + const boost::property_tree::ptree& caret = pt.get_child("caret"); + qsci->setCaretWidth(readInt(caret, "width", 1)); + qsci->setCaretForegroundColor(readColor(caret, "foreground", textColor)); + qsci->setCaretLineBackgroundColor(readColor(caret, "line-background", paperColor)); + + qsci->setMarkerBackgroundColor(readColor(colors, "error-marker", QColor(255, 0, 0, 100)), markerNumber); + qsci->setIndicatorForegroundColor(readColor(colors, "error-indicator", QColor(255, 0, 0, 100)), indicatorNumber); + qsci->setIndicatorOutlineColor(readColor(colors, "error-indicator-outline", QColor(255, 0, 0, 100)), indicatorNumber); + qsci->setWhitespaceBackgroundColor(readColor(colors, "whitespace-background", paperColor)); + qsci->setWhitespaceForegroundColor(readColor(colors, "whitespace-foreground", textColor)); + qsci->setMarginsBackgroundColor(readColor(colors, "margin-background", paperColor)); + qsci->setMarginsForegroundColor(readColor(colors, "margin-foreground", textColor)); + qsci->setMatchedBraceBackgroundColor(readColor(colors, "matched-brace-background", paperColor)); + qsci->setMatchedBraceForegroundColor(readColor(colors, "matched-brace-foreground", textColor)); + qsci->setUnmatchedBraceBackgroundColor(readColor(colors, "unmatched-brace-background", paperColor)); + qsci->setUnmatchedBraceForegroundColor(readColor(colors, "unmatched-brace-foreground", textColor)); + qsci->setSelectionForegroundColor(readColor(colors, "selection-foreground", paperColor)); + qsci->setSelectionBackgroundColor(readColor(colors, "selection-background", textColor)); + qsci->setFoldMarginColors(readColor(colors, "margin-foreground", textColor), + readColor(colors, "margin-background", paperColor)); + qsci->setEdgeColor(readColor(colors, "edge", textColor)); + } catch (std::exception e) { + noColor(); } - - qsci->setLexer(l); - delete lexer; - lexer = l; - - // All other properties must be set after attaching to QSCintilla so - // the editor gets the change events and updates itself to match - l->setFont(font); - l->setColor(textColor); - l->setPaper(paperColor); - - const boost::property_tree::ptree& colors = pt.get_child("colors"); - l->setColor(readColor(colors, "keyword1", textColor), QsciLexerCPP::Keyword); - l->setColor(readColor(colors, "keyword2", textColor), QsciLexerCPP::KeywordSet2); - l->setColor(readColor(colors, "keyword3", textColor), QsciLexerCPP::GlobalClass); - l->setColor(readColor(colors, "number", textColor), QsciLexerCPP::Number); - l->setColor(readColor(colors, "string", textColor), QsciLexerCPP::SingleQuotedString); - l->setColor(readColor(colors, "string", textColor), QsciLexerCPP::DoubleQuotedString); - l->setColor(readColor(colors, "operator", textColor), QsciLexerCPP::Operator); - l->setColor(readColor(colors, "comment", textColor), QsciLexerCPP::Comment); - l->setColor(readColor(colors, "commentline", textColor), QsciLexerCPP::CommentLine); - l->setColor(readColor(colors, "commentdoc", textColor), QsciLexerCPP::CommentDoc); - l->setColor(readColor(colors, "commentdoc", textColor), QsciLexerCPP::CommentLineDoc); - l->setColor(readColor(colors, "commentdockeyword", textColor), QsciLexerCPP::CommentDocKeyword); - - const boost::property_tree::ptree& caret = pt.get_child("caret"); - qsci->setCaretWidth(readInt(caret, "width", 1)); - qsci->setCaretForegroundColor(readColor(caret, "foreground", textColor)); - qsci->setCaretLineBackgroundColor(readColor(caret, "line-background", paperColor)); - - qsci->setMarkerBackgroundColor(readColor(colors, "error-marker", QColor(255, 0, 0, 100)), markerNumber); - qsci->setIndicatorForegroundColor(readColor(colors, "error-indicator", QColor(255, 0, 0, 100)), indicatorNumber); - qsci->setIndicatorOutlineColor(readColor(colors, "error-indicator-outline", QColor(255, 0, 0, 100)), indicatorNumber); - qsci->setWhitespaceBackgroundColor(readColor(colors, "whitespace-background", paperColor)); - qsci->setWhitespaceForegroundColor(readColor(colors, "whitespace-foreground", textColor)); - qsci->setMarginsBackgroundColor(readColor(colors, "margin-background", paperColor)); - qsci->setMarginsForegroundColor(readColor(colors, "margin-foreground", textColor)); - qsci->setMatchedBraceBackgroundColor(readColor(colors, "matched-brace-background", paperColor)); - qsci->setMatchedBraceForegroundColor(readColor(colors, "matched-brace-foreground", textColor)); - qsci->setUnmatchedBraceBackgroundColor(readColor(colors, "unmatched-brace-background", paperColor)); - qsci->setUnmatchedBraceForegroundColor(readColor(colors, "unmatched-brace-foreground", textColor)); - qsci->setSelectionForegroundColor(readColor(colors, "selection-foreground", paperColor)); - qsci->setSelectionBackgroundColor(readColor(colors, "selection-background", textColor)); - qsci->setFoldMarginColors(readColor(colors, "margin-foreground", textColor), - readColor(colors, "margin-background", paperColor)); - qsci->setEdgeColor(readColor(colors, "edge", textColor)); - } catch (std::exception e) { - noColor(); - } } void ScintillaEditor::noColor() { - lexer->setPaper(Qt::white); - lexer->setColor(Qt::black); - qsci->setCaretWidth(2); - qsci->setCaretForegroundColor(Qt::black); - qsci->setMarkerBackgroundColor(QColor(255, 0, 0, 100), markerNumber); - qsci->setIndicatorForegroundColor(QColor(255, 0, 0, 128), indicatorNumber); - qsci->setIndicatorOutlineColor(QColor(0, 0, 0, 255), indicatorNumber); // only alpha part is used - qsci->setCaretLineBackgroundColor(Qt::white); - qsci->setWhitespaceBackgroundColor(Qt::white); - qsci->setWhitespaceForegroundColor(Qt::black); - qsci->setMarginsBackgroundColor(Qt::white); - qsci->setMarginsForegroundColor(Qt::black); - qsci->setSelectionForegroundColor(Qt::white); - qsci->setSelectionBackgroundColor(Qt::black); - qsci->setMatchedBraceBackgroundColor(Qt::white); - qsci->setMatchedBraceForegroundColor(Qt::black); - qsci->setUnmatchedBraceBackgroundColor(Qt::white); - qsci->setUnmatchedBraceForegroundColor(Qt::black); - qsci->setMarginsBackgroundColor(Qt::lightGray); - qsci->setMarginsForegroundColor(Qt::black); - qsci->setFoldMarginColors(Qt::black, Qt::lightGray); - qsci->setEdgeColor(Qt::black); + lexer->setPaper(Qt::white); + lexer->setColor(Qt::black); + qsci->setCaretWidth(2); + qsci->setCaretForegroundColor(Qt::black); + qsci->setMarkerBackgroundColor(QColor(255, 0, 0, 100), markerNumber); + qsci->setIndicatorForegroundColor(QColor(255, 0, 0, 128), indicatorNumber); + qsci->setIndicatorOutlineColor(QColor(0, 0, 0, 255), indicatorNumber); // only alpha part is used + qsci->setCaretLineBackgroundColor(Qt::white); + qsci->setWhitespaceBackgroundColor(Qt::white); + qsci->setWhitespaceForegroundColor(Qt::black); + qsci->setMarginsBackgroundColor(Qt::white); + qsci->setMarginsForegroundColor(Qt::black); + qsci->setSelectionForegroundColor(Qt::white); + qsci->setSelectionBackgroundColor(Qt::black); + qsci->setMatchedBraceBackgroundColor(Qt::white); + qsci->setMatchedBraceForegroundColor(Qt::black); + qsci->setUnmatchedBraceBackgroundColor(Qt::white); + qsci->setUnmatchedBraceForegroundColor(Qt::black); + qsci->setMarginsBackgroundColor(Qt::lightGray); + qsci->setMarginsForegroundColor(Qt::black); + qsci->setFoldMarginColors(Qt::black, Qt::lightGray); + qsci->setEdgeColor(Qt::black); } void ScintillaEditor::enumerateColorSchemesInPath(ScintillaEditor::colorscheme_set_t &result_set, const fs::path path) { - const fs::path color_schemes = path / "color-schemes" / "editor"; + const fs::path color_schemes = path / "color-schemes" / "editor"; - fs::directory_iterator end_iter; - - if (fs::exists(color_schemes) && fs::is_directory(color_schemes)) { - for (fs::directory_iterator dir_iter(color_schemes); dir_iter != end_iter; ++dir_iter) { - if (!fs::is_regular_file(dir_iter->status())) { - continue; - } - - const fs::path path = (*dir_iter).path(); - if (!(path.extension().string() == ".json")) { - continue; - } - - EditorColorScheme *colorScheme = new EditorColorScheme(path); - if (colorScheme->valid()) { - result_set.insert(colorscheme_set_t::value_type(colorScheme->index(), boost::shared_ptr(colorScheme))); - } else { - delete colorScheme; - } + fs::directory_iterator end_iter; + + if (fs::exists(color_schemes) && fs::is_directory(color_schemes)) { + for (fs::directory_iterator dir_iter(color_schemes); dir_iter != end_iter; ++dir_iter) { + if (!fs::is_regular_file(dir_iter->status())) { + continue; + } + + const fs::path path = (*dir_iter).path(); + if (!(path.extension().string() == ".json")) { + continue; + } + + EditorColorScheme *colorScheme = new EditorColorScheme(path); + if (colorScheme->valid()) { + result_set.insert(colorscheme_set_t::value_type(colorScheme->index(), boost::shared_ptr(colorScheme))); + } else { + delete colorScheme; + } + } } - } } ScintillaEditor::colorscheme_set_t ScintillaEditor::enumerateColorSchemes() { - colorscheme_set_t result_set; + colorscheme_set_t result_set; - enumerateColorSchemesInPath(result_set, PlatformUtils::resourceBasePath()); - enumerateColorSchemesInPath(result_set, PlatformUtils::userConfigPath()); - - return result_set; + enumerateColorSchemesInPath(result_set, PlatformUtils::resourceBasePath()); + enumerateColorSchemesInPath(result_set, PlatformUtils::userConfigPath()); + + return result_set; } QStringList ScintillaEditor::colorSchemes() { - const colorscheme_set_t colorscheme_set = enumerateColorSchemes(); + const colorscheme_set_t colorscheme_set = enumerateColorSchemes(); - QStringList colorSchemes; - for (colorscheme_set_t::const_iterator it = colorscheme_set.begin();it != colorscheme_set.end();it++) { - colorSchemes << (*it).second.get()->name(); - } - colorSchemes << "Off"; - - return colorSchemes; + QStringList colorSchemes; + for (colorscheme_set_t::const_iterator it = colorscheme_set.begin(); it != colorscheme_set.end(); it++) { + colorSchemes << (*it).second.get()->name(); + } + colorSchemes << "Off"; + + return colorSchemes; } void ScintillaEditor::setHighlightScheme(const QString &name) { - const colorscheme_set_t colorscheme_set = enumerateColorSchemes(); + const colorscheme_set_t colorscheme_set = enumerateColorSchemes(); - for (colorscheme_set_t::const_iterator it = colorscheme_set.begin();it != colorscheme_set.end();it++) { - const EditorColorScheme *colorScheme = (*it).second.get(); - if (colorScheme->name() == name) { - setColormap(colorScheme); - return; + for (colorscheme_set_t::const_iterator it = colorscheme_set.begin(); it != colorscheme_set.end(); it++) { + const EditorColorScheme *colorScheme = (*it).second.get(); + if (colorScheme->name() == name) { + setColormap(colorScheme); + return; + } } - } - - noColor(); + + noColor(); } void ScintillaEditor::insert(const QString &text) { - qsci->insert(text); + qsci->insert(text); } void ScintillaEditor::replaceAll(const QString &text) { - qsci->selectAll(true); - qsci->replaceSelectedText(text); + qsci->selectAll(true); + qsci->replaceSelectedText(text); } void ScintillaEditor::undo() { - qsci->undo(); + qsci->undo(); } void ScintillaEditor::redo() { - qsci->redo(); + qsci->redo(); } void ScintillaEditor::cut() { - qsci->cut(); + qsci->cut(); } void ScintillaEditor::copy() { - qsci->copy(); + qsci->copy(); } void ScintillaEditor::paste() -{ - qsci->paste(); +{ + qsci->paste(); } void ScintillaEditor::zoomIn() { - qsci->zoomIn(); + qsci->zoomIn(); } -void ScintillaEditor::zoomOut() +void ScintillaEditor::zoomOut() { - qsci->zoomOut(); + qsci->zoomOut(); } void ScintillaEditor::initFont(const QString& fontName, uint size) { - QFont font(fontName, size); - font.setFixedPitch(true); - lexer->setFont(font); + QFont font(fontName, size); + font.setFixedPitch(true); + lexer->setFont(font); } void ScintillaEditor::initMargin() { - QFontMetrics fontmetrics = QFontMetrics(qsci->font()); - qsci->setMarginsFont(qsci->font()); - qsci->setMarginWidth(1, fontmetrics.width(QString::number(qsci->lines())) + 6); - qsci->setMarginLineNumbers(1, true); - - connect(qsci, SIGNAL(textChanged()), this, SLOT(onTextChanged())); + QFontMetrics fontmetrics = QFontMetrics(qsci->font()); + qsci->setMarginsFont(qsci->font()); + qsci->setMarginWidth(1, fontmetrics.width(QString::number(qsci->lines())) + 6); + qsci->setMarginLineNumbers(1, true); + + connect(qsci, SIGNAL(textChanged()), this, SLOT(onTextChanged())); } void ScintillaEditor::onTextChanged() { - QFontMetrics fontmetrics = qsci->fontMetrics(); - qsci->setMarginWidth(1, fontmetrics.width(QString::number(qsci->lines())) + 6); + QFontMetrics fontmetrics = qsci->fontMetrics(); + qsci->setMarginWidth(1, fontmetrics.width(QString::number(qsci->lines())) + 6); } bool ScintillaEditor::find(const QString &expr, bool findNext, bool findBackwards) { - int startline = -1, startindex = -1; + int startline = -1, startindex = -1; - // If findNext, start from the end of the current selection - if (qsci->hasSelectedText()) { - int lineFrom, indexFrom, lineTo, indexTo; - qsci->getSelection(&lineFrom, &indexFrom, &lineTo, &indexTo); - - startline = !(findBackwards xor findNext) ? std::min(lineFrom, lineTo) : std::max(lineFrom, lineTo); - startindex = !(findBackwards xor findNext) ? std::min(indexFrom, indexTo) : std::max(indexFrom, indexTo); - } + // If findNext, start from the end of the current selection + if (qsci->hasSelectedText()) { + int lineFrom, indexFrom, lineTo, indexTo; + qsci->getSelection(&lineFrom, &indexFrom, &lineTo, &indexTo); - return qsci->findFirst(expr, false, false, false, true, - !findBackwards, startline, startindex); + startline = !(findBackwards xor findNext) ? std::min(lineFrom, lineTo) : std::max(lineFrom, lineTo); + startindex = !(findBackwards xor findNext) ? std::min(indexFrom, indexTo) : std::max(indexFrom, indexTo); + } + + return qsci->findFirst(expr, false, false, false, true, + !findBackwards, startline, startindex); } void ScintillaEditor::replaceSelectedText(const QString &newText) { - if (qsci->selectedText() != newText) qsci->replaceSelectedText(newText); + if (qsci->selectedText() != newText) qsci->replaceSelectedText(newText); } void ScintillaEditor::getRange(int *lineFrom, int *lineTo) { - int indexFrom, indexTo; - if (qsci->hasSelectedText()) { - qsci->getSelection(lineFrom, &indexFrom, lineTo, &indexTo); - if (indexTo == 0) { - *lineTo = *lineTo - 1; + int indexFrom, indexTo; + if (qsci->hasSelectedText()) { + qsci->getSelection(lineFrom, &indexFrom, lineTo, &indexTo); + if (indexTo == 0) { + *lineTo = *lineTo - 1; + } + } else { + qsci->getCursorPosition(lineFrom, &indexFrom); + *lineTo = *lineFrom; } - } else { - qsci->getCursorPosition(lineFrom, &indexFrom); - *lineTo = *lineFrom; - } } void ScintillaEditor::indentSelection() { - int lineFrom, lineTo; - getRange(&lineFrom, &lineTo); - for (int line = lineFrom;line <= lineTo;line++) { - qsci->indent(line); - } + int lineFrom, lineTo; + getRange(&lineFrom, &lineTo); + for (int line = lineFrom; line <= lineTo; line++) { + qsci->indent(line); + } } void ScintillaEditor::unindentSelection() { - int lineFrom, lineTo; - getRange(&lineFrom, &lineTo); - for (int line = lineFrom;line <= lineTo;line++) { - qsci->unindent(line); - } + int lineFrom, lineTo; + getRange(&lineFrom, &lineTo); + for (int line = lineFrom; line <= lineTo; line++) { + qsci->unindent(line); + } } void ScintillaEditor::commentSelection() { - bool hasSelection = qsci->hasSelectedText(); - - int lineFrom, lineTo; - getRange(&lineFrom, &lineTo); - for (int line = lineFrom;line <= lineTo;line++) { - qsci->insertAt("//", line, 0); - } - - if (hasSelection) { - qsci->setSelection(lineFrom, 0, lineTo, std::max(0, qsci->lineLength(lineTo) - 1)); - } + bool hasSelection = qsci->hasSelectedText(); + + int lineFrom, lineTo; + getRange(&lineFrom, &lineTo); + for (int line = lineFrom; line <= lineTo; line++) { + qsci->insertAt("//", line, 0); + } + + if (hasSelection) { + qsci->setSelection(lineFrom, 0, lineTo, std::max(0, qsci->lineLength(lineTo) - 1)); + } } void ScintillaEditor::uncommentSelection() { - bool hasSelection = qsci->hasSelectedText(); + bool hasSelection = qsci->hasSelectedText(); - int lineFrom, lineTo; - getRange(&lineFrom, &lineTo); - for (int line = lineFrom;line <= lineTo;line++) { - QString lineText = qsci->text(line); - if (lineText.startsWith("//")) { - qsci->setSelection(line, 0, line, 2); - qsci->removeSelectedText(); + int lineFrom, lineTo; + getRange(&lineFrom, &lineTo); + for (int line = lineFrom; line <= lineTo; line++) { + QString lineText = qsci->text(line); + if (lineText.startsWith("//")) { + qsci->setSelection(line, 0, line, 2); + qsci->removeSelectedText(); + } + } + if (hasSelection) { + qsci->setSelection(lineFrom, 0, lineTo, std::max(0, qsci->lineLength(lineTo) - 1)); } - } - if (hasSelection) { - qsci->setSelection(lineFrom, 0, lineTo, std::max(0, qsci->lineLength(lineTo) - 1)); - } } QString ScintillaEditor::selectedText() { - return qsci->selectedText(); + return qsci->selectedText(); } diff --git a/src/scintillaeditor.h b/src/scintillaeditor.h index 42fcd274..4a4cdaa0 100644 --- a/src/scintillaeditor.h +++ b/src/scintillaeditor.h @@ -87,6 +87,7 @@ public slots: private slots: void onTextChanged(); + void applySettings(); private: QVBoxLayout *scintillaLayout; diff --git a/src/settings.cc b/src/settings.cc index cf352fdd..823df0e2 100644 --- a/src/settings.cc +++ b/src/settings.cc @@ -1,8 +1,10 @@ #include "settings.h" +namespace Settings { + template SettingsEntry::SettingsEntry(const std::string category, const std::string name, T defaultValue) - : category(category), name(name), defaultValue(defaultValue) + : category(category), name(name), value(defaultValue), defaultValue(defaultValue) { } @@ -13,7 +15,13 @@ SettingsEntry::~SettingsEntry() SettingsEntry Settings::indentationWidth("editor", "indentationWidth", 4); SettingsEntry Settings::tabWidth("editor", "tabWidth", 8); -SettingsEntry Settings::lineWrap("editor", "lineWrap", LINE_WRAP_WORD); +SettingsEntry Settings::lineWrap("editor", "lineWrap", LINE_WRAP_WORD); +SettingsEntry Settings::lineWrapIndentationStyle("editor", "lineWrapIndentationStyle", LINE_WRAP_INDENTATION_FIXED); +SettingsEntry Settings::lineWrapIndentation("editor", "lineWrapIndentation", 4); +SettingsEntry Settings::lineWrapVisualizationBegin("editor", "lineWrapVisualizationBegin", LINE_WRAP_VISUALIZATION_NONE); +SettingsEntry Settings::lineWrapVisualizationEnd("editor", "lineWrapVisualizationEnd", LINE_WRAP_VISUALIZATION_BORDER); +SettingsEntry Settings::showWhitespaces("editor", "showWhitespaces", SHOW_WHITESPACES_NEVER); +SettingsEntry Settings::showWhitespacesSize("editor", "showWhitespacesSize", 2); Settings *Settings::inst(bool erase) { @@ -41,5 +49,36 @@ T Settings::defaultValue(const SettingsEntry &entry) return entry.defaultValue; } +template +T Settings::get(const SettingsEntry &entry) +{ + return entry.value; +} + +template +void Settings::set(SettingsEntry &entry, T val) +{ + entry.value = val; +} + template int Settings::defaultValue(const SettingsEntry&); -template SettingsLineWrap Settings::defaultValue(const SettingsEntry&); +template int Settings::get(const SettingsEntry&); +template void Settings::set(SettingsEntry&, int); + +template LineWrap Settings::defaultValue(const SettingsEntry&); +template LineWrap Settings::get(const SettingsEntry&); +template void Settings::set(SettingsEntry&, LineWrap); + +template LineWrapVisualization Settings::defaultValue(const SettingsEntry&); +template LineWrapVisualization Settings::get(const SettingsEntry&); +template void Settings::set(SettingsEntry&, LineWrapVisualization); + +template LineWrapIndentationStyle Settings::defaultValue(const SettingsEntry&); +template LineWrapIndentationStyle Settings::get(const SettingsEntry&); +template void Settings::set(SettingsEntry&, LineWrapIndentationStyle); + +template ShowWhitespaces Settings::defaultValue(const SettingsEntry&); +template ShowWhitespaces Settings::get(const SettingsEntry&); +template void Settings::set(SettingsEntry&, ShowWhitespaces); + +} \ No newline at end of file diff --git a/src/settings.h b/src/settings.h index 18e69c4d..b854354e 100644 --- a/src/settings.h +++ b/src/settings.h @@ -3,15 +3,37 @@ #include #include +namespace Settings { + typedef enum { LINE_WRAP_NONE, LINE_WRAP_CHARACTER, LINE_WRAP_WORD, -} SettingsLineWrap; +} LineWrap; + +typedef enum { + LINE_WRAP_INDENTATION_FIXED, + LINE_WRAP_INDENTATION_SAME, + LINE_WRAP_INDENTATION_INDENTED, +} LineWrapIndentationStyle; + +typedef enum { + LINE_WRAP_VISUALIZATION_NONE, + LINE_WRAP_VISUALIZATION_TEXT, + LINE_WRAP_VISUALIZATION_BORDER, + LINE_WRAP_VISUALIZATION_MARGIN, +} LineWrapVisualization; + +typedef enum { + SHOW_WHITESPACES_NEVER, + SHOW_WHITESPACES_ALWAYS, + SHOW_WHITESPACES_AFTER_INDENTATION, +} ShowWhitespaces; template class SettingsEntry { +private: std::string category; std::string name; T value; @@ -28,13 +50,23 @@ class Settings public: static SettingsEntry indentationWidth; static SettingsEntry tabWidth; - static SettingsEntry lineWrap; - + static SettingsEntry lineWrap; + static SettingsEntry lineWrapIndentationStyle; + static SettingsEntry lineWrapIndentation; + static SettingsEntry lineWrapVisualizationBegin; + static SettingsEntry lineWrapVisualizationEnd; + static SettingsEntry showWhitespaces; + static SettingsEntry showWhitespacesSize; + static Settings *inst(bool erase = false); template T defaultValue(const SettingsEntry &entry); + template T get(const SettingsEntry &entry); + template void set(SettingsEntry &entry, T val); private: Settings(); virtual ~Settings(); }; + +} \ No newline at end of file From 6c25d5ccb6a0e263790b9bb8a3c4399abefa2f97 Mon Sep 17 00:00:00 2001 From: Torsten Paul Date: Sat, 27 Dec 2014 02:59:11 +0100 Subject: [PATCH 4/8] Add more settings (AutoIndent / TabIndents / IndentationsUseTabs). --- src/Preferences.cc | 21 + src/Preferences.h | 3 + src/Preferences.ui | 1372 +++++++++++++++++++++------------------ src/scintillaeditor.cpp | 6 +- src/settings.cc | 7 + src/settings.h | 3 + 6 files changed, 776 insertions(+), 636 deletions(-) diff --git a/src/Preferences.cc b/src/Preferences.cc index 45afcba9..7265d1a3 100644 --- a/src/Preferences.cc +++ b/src/Preferences.cc @@ -461,6 +461,24 @@ void Preferences::on_spinBoxShowWhitespacesSize_valueChanged(int val) emit editorConfigChanged(); } +void Preferences::on_checkBoxAutoIndent_toggled(bool val) +{ + Settings::Settings::inst()->set(Settings::Settings::autoIndent, val); + emit editorConfigChanged(); +} + +void Preferences::on_checkBoxTabIndents_toggled(bool val) +{ + Settings::Settings::inst()->set(Settings::Settings::tabIndents, val); + emit editorConfigChanged(); +} + +void Preferences::on_checkBoxIndentationsUseTabs_toggled(bool val) +{ + Settings::Settings::inst()->set(Settings::Settings::indentationsUseTabs, val); + emit editorConfigChanged(); +} + void Preferences::keyPressEvent(QKeyEvent *e) { #ifdef Q_OS_MAC @@ -565,6 +583,9 @@ void Preferences::updateGUI() this->comboBoxLineWrapVisualizationEnd->setCurrentIndex(s->get(Settings::Settings::lineWrapVisualizationEnd)); this->comboBoxShowWhitespaces->setCurrentIndex(s->get(Settings::Settings::showWhitespaces)); this->spinBoxShowWhitespacesSize->setValue(s->get(Settings::Settings::showWhitespacesSize)); + this->checkBoxAutoIndent->setChecked(s->get(Settings::Settings::autoIndent)); + this->checkBoxTabIndents->setChecked(s->get(Settings::Settings::tabIndents)); + this->checkBoxIndentationsUseTabs->setChecked(s->get(Settings::Settings::indentationsUseTabs)); } void Preferences::apply() const diff --git a/src/Preferences.h b/src/Preferences.h index e55420f7..f4122dff 100644 --- a/src/Preferences.h +++ b/src/Preferences.h @@ -53,6 +53,9 @@ public slots: void on_comboBoxLineWrapVisualizationEnd_activated(int); void on_comboBoxShowWhitespaces_activated(int); void on_spinBoxShowWhitespacesSize_valueChanged(int); + void on_checkBoxAutoIndent_toggled(bool); + void on_checkBoxTabIndents_toggled(bool); + void on_checkBoxIndentationsUseTabs_toggled(bool); signals: void requestRedraw() const; diff --git a/src/Preferences.ui b/src/Preferences.ui index f4374141..9c1e0275 100644 --- a/src/Preferences.ui +++ b/src/Preferences.ui @@ -6,8 +6,8 @@ 0 0 - 852 - 592 + 613 + 349 @@ -27,7 +27,7 @@ - 1 + 0 @@ -75,512 +75,578 @@ - - - - Qt::Vertical - - - - 20 - 645 - - - - + + 0 + + + 0 + - - - - - - 75 - true - - - - Editor Type - - - - - - - - Simple Editor - - - - - QScintilla Editor - - - - - - - - - 10 - 50 - false - - - - (requires restart) - - - - - - - Qt::Horizontal - - - QSizePolicy::Expanding - - - - 30 - 20 - - - - - - - - - - - - - 75 - true - - - - Font - - - false - - - - - - - - Nimbus Sans L - 12 - - - - - - - - true - - - - - - - Qt::Horizontal - - - - 40 - 20 - - - - - - - - - - 0 + + + QFrame::Plain - - - - - 75 - true - - - - Color syntax highlighting - - - - - - - - 0 - 0 - - - - - - - - Qt::Horizontal - - - QSizePolicy::Expanding - - - - 30 - 20 - - - - - - - - - - 5 + + Qt::ScrollBarAlwaysOn - - - - - 75 - true - - - - Use Ctrl/Cmd-Mouse-wheel to zoom text - - - - - - - - - - - - - - Qt::Horizontal - - - - 40 - 20 - - - - - - - - - - Editor settings + + true - - - 20 + + + + 0 + 0 + 685 + 609 + - - - - - None + + + 9 + + + 9 + + + + + + + + 75 + true + + + + Editor Type + + + + + + + + Simple Editor + + + + + QScintilla Editor + + + + + + + + + 10 + 50 + false + + + + (requires restart) + + + + + + + Qt::Horizontal + + + QSizePolicy::Expanding + + + + 30 + 20 + + + + + + + + + + + + + 75 + true + + + + Font + + + false + + + + + + + + Nimbus Sans L + 12 + + + + + + + + true + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + + 0 - - - - Wrap at character boundaries + + + + + 75 + true + + + + Color syntax highlighting + + + + + + + + 0 + 0 + + + + + + + + Qt::Horizontal + + + QSizePolicy::Expanding + + + + 30 + 20 + + + + + + + + + + 5 - - - - Wrap at word boundaries + + + + + 75 + true + + + + Use Ctrl/Cmd-Mouse-wheel to zoom text + + + + + + + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + + Editor settings - - - - - - - Qt::Horizontal - - - QSizePolicy::Fixed - - - - 20 - 2 - - - - - - - - Whitespaces - - - - - - - Qt::Horizontal - - - - 40 - 20 - - - - - - - - - 0 - 0 - - - - - None + + + 20 + + + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + + + None + + + + + Wrap at character boundaries + + + + + Wrap at word boundaries + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + Qt::Horizontal + + + QSizePolicy::Fixed + + + + 20 + 2 + + + + + + + + Whitespaces + + + + + + + + 0 + 0 + + + + + None + + + + + Text + + + + + Border + + + + + Margin + + + + + + + + Start + + + + + + + Line wrap visualization + + + + + + + Line wrap indentation + + + + + + + Tab width + + + + + + + Line wrap + + + + + + + End + + + + + + + + 0 + 0 + + + + + None + + + + + Text + + + + + Border + + + + + Margin + + + + + + + + Indent + + + + + + + + Fixed + + + + + Same + + + + + Indented + + + + + + + + Style + + + + + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + + Indentation width + + + + + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + 99 + + + + + + + + Never + + + + + Always + + + + + Only after indentation + + + + + + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + 1 + + + 9 + + + + + + + Show + + + + + + + Size + + + + + + + + + + + + + + Auto Indent + + + + + + + Tab Indents + + + + + + + Indentations Use Tabs + + + + + + + + + + + + + + + + + + + + + + + + Qt::Vertical - - - - Text + + + 20 + 40 + - - - - Border - - - - - Margin - - - - - - - - Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter - - - - - - - Start - - - - - - - Line wrap visualization - - - - - - - Line wrap indentation - - - - - - - Tab width - - - - - - - Line wrap - - - - - - - End - - - - - - - - 0 - 0 - - - - - None - - - - - Text - - - - - Border - - - - - Margin - - - - - - - - Indent - - - - - - - - Fixed - - - - - Same - - - - - Indented - - - - - - - - Style - - - - - - - Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter - - - - - - - Indentation width - - - - - - - Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter - - - 99 - - - - - - - - Never - - - - - Always - - - - - Only after indentation - - - - - - - - Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter - - - 1 - - - 9 - - - - - - - Show - - - - - - - Size - - - - + + + + - - - - Qt::Vertical - - - - 20 - 723 - - - - @@ -801,153 +867,193 @@ + + 0 + - - - OpenCSG + + + QFrame::Plain - - - - - Show capability warning - - - true - - - - - - - Enable for OpenGL 1.x - - - - - - - - - Turn off rendering at - - - - - - - - - - elements - - - - - - - - - Force Goldfeather - - - - - - - - - - - - CGAL Cache size - - - - - - - - - - bytes - - - - - - - - - - - PolySet Cache size - - - - - - - - - - bytes - - - - - - - - - Allow to open multiple documents + + Qt::ScrollBarAlwaysOn - - - - - - Enable docking of Editor and Console in different places - - - - - - - Enable undocking of Editor and Console to separate windows - - - - - - - Show Welcome Screen - - - - - - - Enable user interface localization (requires restart of OpenSCAD) - - + true + + + + 0 + 0 + 596 + 499 + + + + + 9 + + + 9 + + + 9 + + + + + OpenCSG + + + + + + Show capability warning + + + true + + + + + + + Enable for OpenGL 1.x + + + + + + + + + Turn off rendering at + + + + + + + + + + elements + + + + + + + + + Force Goldfeather + + + + + + + + + + + + CGAL Cache size + + + + + + + + + + bytes + + + + + + + + + 0 + + + + + PolySet Cache size + + + + + + + + + + bytes + + + + + + + + + Allow to open multiple documents + + + + + + + Enable docking of Editor and Console in different places + + + + + + + Enable undocking of Editor and Console to separate windows + + + + + + + Show Welcome Screen + + + + + + + Enable user interface localization (requires restart of OpenSCAD) + + + true + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + - - - - Qt::Vertical - - - - 20 - 11 - - - - diff --git a/src/scintillaeditor.cpp b/src/scintillaeditor.cpp index 309cda34..7836d6b1 100644 --- a/src/scintillaeditor.cpp +++ b/src/scintillaeditor.cpp @@ -174,11 +174,11 @@ void ScintillaEditor::applySettings() s->get(Settings::Settings::lineWrapIndentation)); qsci->setWhitespaceVisibility(conv.fromShowWhitespaces(s->get(Settings::Settings::showWhitespaces))); qsci->setWhitespaceSize(s->get(Settings::Settings::showWhitespacesSize)); + qsci->setAutoIndent(s->get(Settings::Settings::autoIndent)); + qsci->setTabIndents(s->get(Settings::Settings::tabIndents)); + qsci->setIndentationsUseTabs(s->get(Settings::Settings::indentationsUseTabs)); qsci->setBraceMatching(QsciScintilla::SloppyBraceMatch); - qsci->setAutoIndent(true); - qsci->setTabIndents(true); - qsci->setIndentationsUseTabs(false); qsci->setFolding(QsciScintilla::BoxedTreeFoldStyle, 4); qsci->setCaretLineVisible(true); } diff --git a/src/settings.cc b/src/settings.cc index 823df0e2..6c1fe2c8 100644 --- a/src/settings.cc +++ b/src/settings.cc @@ -22,6 +22,9 @@ SettingsEntry Settings::lineWrapVisualizationBegin("edito SettingsEntry Settings::lineWrapVisualizationEnd("editor", "lineWrapVisualizationEnd", LINE_WRAP_VISUALIZATION_BORDER); SettingsEntry Settings::showWhitespaces("editor", "showWhitespaces", SHOW_WHITESPACES_NEVER); SettingsEntry Settings::showWhitespacesSize("editor", "showWhitespacesSize", 2); +SettingsEntry Settings::autoIndent("editor", "autoIndent", true); +SettingsEntry Settings::tabIndents("editor", "tabIndents", true); +SettingsEntry Settings::indentationsUseTabs("editor", "indentationsUseTabs", false); Settings *Settings::inst(bool erase) { @@ -61,6 +64,10 @@ void Settings::set(SettingsEntry &entry, T val) entry.value = val; } +template bool Settings::defaultValue(const SettingsEntry&); +template bool Settings::get(const SettingsEntry&); +template void Settings::set(SettingsEntry&, bool); + template int Settings::defaultValue(const SettingsEntry&); template int Settings::get(const SettingsEntry&); template void Settings::set(SettingsEntry&, int); diff --git a/src/settings.h b/src/settings.h index b854354e..4b0dc169 100644 --- a/src/settings.h +++ b/src/settings.h @@ -57,6 +57,9 @@ public: static SettingsEntry lineWrapVisualizationEnd; static SettingsEntry showWhitespaces; static SettingsEntry showWhitespacesSize; + static SettingsEntry autoIndent; + static SettingsEntry tabIndents; + static SettingsEntry indentationsUseTabs; static Settings *inst(bool erase = false); From 6f9c4f7f09d6d4041a851b08a012a2467aa79b80 Mon Sep 17 00:00:00 2001 From: Torsten Paul Date: Mon, 29 Dec 2014 00:29:09 +0100 Subject: [PATCH 5/8] Integrate with QSettings. --- src/Preferences.cc | 57 +++++++++++++++++---- src/Preferences.h | 1 + src/mainwin.cc | 1 + src/scintillaeditor.cpp | 3 +- src/settings.cc | 109 +++++++++++++++++++++++++++++++++++++++- src/settings.h | 47 +++++++++++++++-- 6 files changed, 198 insertions(+), 20 deletions(-) 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 From 2c89f562a3eeaa9a166ce9ec3b2910d2a46cb734 Mon Sep 17 00:00:00 2001 From: Torsten Paul Date: Mon, 29 Dec 2014 03:49:45 +0100 Subject: [PATCH 6/8] Update GUI to make options a bit clearer for the user. --- src/Preferences.cc | 31 +- src/Preferences.h | 9 +- src/Preferences.ui | 1295 ++++++++++++++++++++++++--------------- src/scintillaeditor.cpp | 27 +- src/settings.cc | 33 +- src/settings.h | 18 +- 6 files changed, 884 insertions(+), 529 deletions(-) diff --git a/src/Preferences.cc b/src/Preferences.cc index 9969c91f..c6287a93 100644 --- a/src/Preferences.cc +++ b/src/Preferences.cc @@ -477,15 +477,15 @@ void Preferences::on_comboBoxLineWrapVisualizationEnd_activated(int val) fireEditorConfigChanged(); } -void Preferences::on_comboBoxShowWhitespaces_activated(int val) +void Preferences::on_comboBoxShowWhitespace_activated(int val) { - Settings::Settings::inst()->set(Settings::Settings::showWhitespaces, (Settings::ShowWhitespaces)val); + Settings::Settings::inst()->set(Settings::Settings::showWhitespace, (Settings::ShowWhitespace)val); fireEditorConfigChanged(); } -void Preferences::on_spinBoxShowWhitespacesSize_valueChanged(int val) +void Preferences::on_spinBoxShowWhitespaceSize_valueChanged(int val) { - Settings::Settings::inst()->set(Settings::Settings::showWhitespacesSize, val); + Settings::Settings::inst()->set(Settings::Settings::showWhitespaceSize, val); fireEditorConfigChanged(); } @@ -495,15 +495,21 @@ void Preferences::on_checkBoxAutoIndent_toggled(bool val) fireEditorConfigChanged(); } -void Preferences::on_checkBoxTabIndents_toggled(bool val) +void Preferences::on_comboBoxIndentUsing_activated(int val) { - Settings::Settings::inst()->set(Settings::Settings::tabIndents, val); + Settings::Settings::inst()->set(Settings::Settings::indentStyle, (Settings::IndentStyle)val); fireEditorConfigChanged(); } -void Preferences::on_checkBoxIndentationsUseTabs_toggled(bool val) +void Preferences::on_checkBoxHighlightCurrentLine_toggled(bool val) { - Settings::Settings::inst()->set(Settings::Settings::indentationsUseTabs, val); + Settings::Settings::inst()->set(Settings::Settings::highlightCurrentLine, val); + fireEditorConfigChanged(); +} + +void Preferences::on_checkBoxEnableBraceMatching_toggled(bool val) +{ + Settings::Settings::inst()->set(Settings::Settings::enableBraceMatching, val); fireEditorConfigChanged(); } @@ -616,11 +622,12 @@ void Preferences::updateGUI() this->spinBoxLineWrapIndentationIndent->setValue(s->get(Settings::Settings::lineWrapIndentation)); this->comboBoxLineWrapVisualizationStart->setCurrentIndex(s->get(Settings::Settings::lineWrapVisualizationBegin)); this->comboBoxLineWrapVisualizationEnd->setCurrentIndex(s->get(Settings::Settings::lineWrapVisualizationEnd)); - this->comboBoxShowWhitespaces->setCurrentIndex(s->get(Settings::Settings::showWhitespaces)); - this->spinBoxShowWhitespacesSize->setValue(s->get(Settings::Settings::showWhitespacesSize)); + this->comboBoxShowWhitespace->setCurrentIndex(s->get(Settings::Settings::showWhitespace)); + this->spinBoxShowWhitespaceSize->setValue(s->get(Settings::Settings::showWhitespaceSize)); this->checkBoxAutoIndent->setChecked(s->get(Settings::Settings::autoIndent)); - this->checkBoxTabIndents->setChecked(s->get(Settings::Settings::tabIndents)); - this->checkBoxIndentationsUseTabs->setChecked(s->get(Settings::Settings::indentationsUseTabs)); + this->comboBoxIndentUsing->setCurrentIndex(s->get(Settings::Settings::indentStyle)); + this->checkBoxHighlightCurrentLine->setChecked(s->get(Settings::Settings::highlightCurrentLine)); + this->checkBoxEnableBraceMatching->setChecked(s->get(Settings::Settings::enableBraceMatching)); } void Preferences::apply() const diff --git a/src/Preferences.h b/src/Preferences.h index e1db37d0..4432140f 100644 --- a/src/Preferences.h +++ b/src/Preferences.h @@ -51,11 +51,12 @@ public slots: void on_spinBoxLineWrapIndentationIndent_valueChanged(int); void on_comboBoxLineWrapVisualizationStart_activated(int); void on_comboBoxLineWrapVisualizationEnd_activated(int); - void on_comboBoxShowWhitespaces_activated(int); - void on_spinBoxShowWhitespacesSize_valueChanged(int); + void on_comboBoxShowWhitespace_activated(int); + void on_spinBoxShowWhitespaceSize_valueChanged(int); void on_checkBoxAutoIndent_toggled(bool); - void on_checkBoxTabIndents_toggled(bool); - void on_checkBoxIndentationsUseTabs_toggled(bool); + void on_comboBoxIndentUsing_activated(int); + void on_checkBoxHighlightCurrentLine_toggled(bool); + void on_checkBoxEnableBraceMatching_toggled(bool); signals: void requestRedraw() const; diff --git a/src/Preferences.ui b/src/Preferences.ui index 9c1e0275..f6d26e17 100644 --- a/src/Preferences.ui +++ b/src/Preferences.ui @@ -6,8 +6,8 @@ 0 0 - 613 - 349 + 607 + 432 @@ -23,7 +23,7 @@ true - + @@ -86,552 +86,589 @@ 0 - - - QFrame::Plain - - - Qt::ScrollBarAlwaysOn - + true - + 0 0 - 685 - 609 + 659 + 769 - - - 9 - - - 9 - - - - - - - - 75 - true - - - - Editor Type - - - - - + + + + + - - Simple Editor - + + + Qt::Horizontal + + + QSizePolicy::Expanding + + + + 30 + 20 + + + - - QScintilla Editor - + + + Editor Type + + - + - - - - - 10 - 50 - false - - - - (requires restart) - - + + + + + + + Simple Editor + + + + + QScintilla Editor + + + + + + + + + 10 + 50 + false + + + + (requires restart) + + + + + + + Qt::Horizontal + + + QSizePolicy::Expanding + + + + 30 + 20 + + + + + - - - - Qt::Horizontal - - - QSizePolicy::Expanding - - - - 30 - 20 - - - + + + + + + Qt::Horizontal + + + QSizePolicy::Expanding + + + + 30 + 20 + + + + + + + + Font + + + false + + + + - - - - - - - - - 75 - true - - - - Font - - - false - - + + + + + + + Liberation Sans + 12 + + + + + + + + true + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + - - - - - Nimbus Sans L - 12 - - - + + + + + + Qt::Horizontal + + + QSizePolicy::Expanding + + + + 30 + 20 + + + + + + + + Color syntax highlighting + + + + - - - - true - - - - - - - Qt::Horizontal - - - - 40 - 20 - - - - - - - - - - 0 - - - - - - 75 - true - - - - Color syntax highlighting - - - - - - - - 0 - 0 - - - - - - - - Qt::Horizontal - - - QSizePolicy::Expanding - - - - 30 - 20 - - - - - - - - - - 5 - - - - - - 75 - true - - - - Use Ctrl/Cmd-Mouse-wheel to zoom text - - - - - - - - - - - + Qt::Horizontal - 40 + 198 20 + + + + Ctrl/Cmd-Mouse-wheel zooms text + + + + + + + + + + 0 + 0 + + + + + + + + Qt::Horizontal + + + QSizePolicy::Expanding + + + + 30 + 20 + + + + + + - - + + - Editor settings + Indentation - - - 20 - - - - - Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter - - + + + + + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + 99 + + + 4 + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + - - - - - None - - - - - Wrap at character boundaries - - - - - Wrap at word boundaries - - - - - - + + Qt::Horizontal - 40 + 223 20 - - - - Qt::Horizontal - - - QSizePolicy::Fixed - - - - 20 - 2 - - - - - - - - Whitespaces - - - - - - - - 0 - 0 - - - - - None - - - - - Text - - - - - Border - - - - - Margin - - - - - - - - Start - - - - - - - Line wrap visualization - - - - - - - Line wrap indentation - - - - - - - Tab width - - - - - - - Line wrap - - - - - - - End - - - - - - - - 0 - 0 - - - - - None - - - - - Text - - - - - Border - - - - - Margin - - - - - - - - Indent - - - - - - - - Fixed - - - - - Same - - - - - Indented - - - - - - - - Style - - - - - - - Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter - - - - - - - Indentation width - - - - - - - Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter - - - 99 - - - - - - - - Never - - - - - Always - - - - - Only after indentation - - - - - - - - Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter - - - 1 - - - 9 - - - - - - - Show - - - - - - - Size - - - - + - - - - - - - Auto Indent - - + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + Indent using + + + + + + + + + + + + Spaces + + + + + Tabs + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + Indentation width + + + + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + Tab width + + + + + + + + + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + 4 + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + Tab key function + + + + + + + + + + + + Indent + + + + + Insert Tab + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + Show whitespace + + + + + + + + + + + + Never + + + + + Always + + + + + Only after indentation + + + + + + + + Size + + + + + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + 1 + + + 9 + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + + + + + Display + + + + - Tab Indents + Enable brace matching - - + + - Indentations Use Tabs - - - - - - - - - - - - - - + Highlight current line - - + + Qt::Vertical @@ -643,6 +680,284 @@ + + + + Line wrap + + + + + + + + + None + + + + + Wrap at character boundaries + + + + + Wrap at word boundaries + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + Line wrap indentation + + + + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + Line wrap visualization + + + + + + + + + + + Style + + + + + + + + Fixed + + + + + Same + + + + + Indented + + + + + + + + Indent + + + + + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + + + + Start + + + + + + + + 0 + 0 + + + + + None + + + + + Text + + + + + Border + + + + + Margin + + + + + + + + End + + + + + + + + 0 + 0 + + + + + None + + + + + Text + + + + + Border + + + + + Margin + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + Line wrap + + + + + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + @@ -650,7 +965,7 @@ - + @@ -886,7 +1201,7 @@ 0 0 - 596 + 640 499 diff --git a/src/scintillaeditor.cpp b/src/scintillaeditor.cpp index c284349b..46b54583 100644 --- a/src/scintillaeditor.cpp +++ b/src/scintillaeditor.cpp @@ -13,7 +13,7 @@ public: QsciScintilla::WrapMode fromWrapMode(Settings::LineWrap val); QsciScintilla::WrapVisualFlag fromLineWrapVisualization(Settings::LineWrapVisualization val); QsciScintilla::WrapIndentMode fromLineWrapIndentationStyle(Settings::LineWrapIndentationStyle val); - QsciScintilla::WhitespaceVisibility fromShowWhitespaces(Settings::ShowWhitespaces val); + QsciScintilla::WhitespaceVisibility fromShowWhitespaces(Settings::ShowWhitespace val); }; QsciScintilla::WrapMode SettingsConverter::fromWrapMode(Settings::LineWrap val) @@ -60,7 +60,7 @@ QsciScintilla::WrapIndentMode SettingsConverter::fromLineWrapIndentationStyle(Se } } -QsciScintilla::WhitespaceVisibility SettingsConverter::fromShowWhitespaces(Settings::ShowWhitespaces val) +QsciScintilla::WhitespaceVisibility SettingsConverter::fromShowWhitespaces(Settings::ShowWhitespace val) { switch(val) { case Settings::SHOW_WHITESPACES_NEVER: @@ -172,14 +172,25 @@ void ScintillaEditor::applySettings() qsci->setWrapVisualFlags(conv.fromLineWrapVisualization(s->get(Settings::Settings::lineWrapVisualizationEnd)), conv.fromLineWrapVisualization(s->get(Settings::Settings::lineWrapVisualizationBegin)), s->get(Settings::Settings::lineWrapIndentation)); - qsci->setWhitespaceVisibility(conv.fromShowWhitespaces(s->get(Settings::Settings::showWhitespaces))); - qsci->setWhitespaceSize(s->get(Settings::Settings::showWhitespacesSize)); + qsci->setWhitespaceVisibility(conv.fromShowWhitespaces(s->get(Settings::Settings::showWhitespace))); + qsci->setWhitespaceSize(s->get(Settings::Settings::showWhitespaceSize)); qsci->setAutoIndent(s->get(Settings::Settings::autoIndent)); - qsci->setTabIndents(s->get(Settings::Settings::tabIndents)); - qsci->setIndentationsUseTabs(s->get(Settings::Settings::indentationsUseTabs)); - qsci->setBraceMatching(QsciScintilla::SloppyBraceMatch); - qsci->setCaretLineVisible(true); + switch (s->get(Settings::Settings::indentStyle)) { + case Settings::INDENT_SPACES: + qsci->setTabIndents(true); + qsci->setIndentationsUseTabs(false); + break; + case Settings::INDENT_TABS: + qsci->setTabIndents(false); + qsci->setIndentationsUseTabs(true); + break; + default: + assert(false && "unknown indent style"); + } + + qsci->setBraceMatching(s->get(Settings::Settings::enableBraceMatching) ? QsciScintilla::SloppyBraceMatch : QsciScintilla::NoBraceMatch); + qsci->setCaretLineVisible(s->get(Settings::Settings::highlightCurrentLine)); } void ScintillaEditor::setPlainText(const QString &text) diff --git a/src/settings.cc b/src/settings.cc index f2137ce6..2888d96c 100644 --- a/src/settings.cc +++ b/src/settings.cc @@ -94,10 +94,20 @@ void SettingsEntry::from_string(std::string val) } template <> -void SettingsEntry::from_string(std::string val) +void SettingsEntry::from_string(std::string val) { try { - value = (ShowWhitespaces)boost::lexical_cast(val); + value = (ShowWhitespace)boost::lexical_cast(val); + } catch (const boost::bad_lexical_cast &e) { + value = defaultValue; + } +} + +template <> +void SettingsEntry::from_string(std::string val) +{ + try { + value = (IndentStyle)boost::lexical_cast(val); } catch (const boost::bad_lexical_cast &e) { value = defaultValue; } @@ -110,11 +120,12 @@ SettingsEntry Settings::lineWrapIndentationStyle("edit SettingsEntry Settings::lineWrapIndentation("editor", "lineWrapIndentation", 4); SettingsEntry Settings::lineWrapVisualizationBegin("editor", "lineWrapVisualizationBegin", LINE_WRAP_VISUALIZATION_NONE); SettingsEntry Settings::lineWrapVisualizationEnd("editor", "lineWrapVisualizationEnd", LINE_WRAP_VISUALIZATION_BORDER); -SettingsEntry Settings::showWhitespaces("editor", "showWhitespaces", SHOW_WHITESPACES_NEVER); -SettingsEntry Settings::showWhitespacesSize("editor", "showWhitespacesSize", 2); +SettingsEntry Settings::showWhitespace("editor", "showWhitespaces", SHOW_WHITESPACES_NEVER); +SettingsEntry Settings::showWhitespaceSize("editor", "showWhitespacesSize", 2); SettingsEntry Settings::autoIndent("editor", "autoIndent", true); -SettingsEntry Settings::tabIndents("editor", "tabIndents", true); -SettingsEntry Settings::indentationsUseTabs("editor", "indentationsUseTabs", false); +SettingsEntry Settings::indentStyle("editor", "indentStyle", INDENT_SPACES); +SettingsEntry Settings::highlightCurrentLine("editor", "highlightCurrentLine", true); +SettingsEntry Settings::enableBraceMatching("editor", "enableBraceMatching", true); Settings *Settings::inst(bool erase) { @@ -181,9 +192,13 @@ template LineWrapIndentationStyle Settings::defaultValue(const SettingsEntry&); template void Settings::set(SettingsEntry&, LineWrapIndentationStyle); -template ShowWhitespaces Settings::defaultValue(const SettingsEntry&); -template ShowWhitespaces Settings::get(const SettingsEntry&); -template void Settings::set(SettingsEntry&, ShowWhitespaces); +template ShowWhitespace Settings::defaultValue(const SettingsEntry&); +template ShowWhitespace Settings::get(const SettingsEntry&); +template void Settings::set(SettingsEntry&, ShowWhitespace); + +template IndentStyle Settings::defaultValue(const SettingsEntry&); +template IndentStyle Settings::get(const SettingsEntry&); +template void Settings::set(SettingsEntry&, IndentStyle); Visitor::Visitor() { diff --git a/src/settings.h b/src/settings.h index 445a9a8a..f52273ad 100644 --- a/src/settings.h +++ b/src/settings.h @@ -5,7 +5,12 @@ #include namespace Settings { - + +typedef enum { + INDENT_SPACES, + INDENT_TABS, +} IndentStyle; + typedef enum { LINE_WRAP_NONE, LINE_WRAP_CHARACTER, @@ -29,7 +34,7 @@ typedef enum { SHOW_WHITESPACES_NEVER, SHOW_WHITESPACES_ALWAYS, SHOW_WHITESPACES_AFTER_INDENTATION, -} ShowWhitespaces; +} ShowWhitespace; class SettingsEntryBase { @@ -81,11 +86,12 @@ public: static SettingsEntry lineWrapIndentation; static SettingsEntry lineWrapVisualizationBegin; static SettingsEntry lineWrapVisualizationEnd; - static SettingsEntry showWhitespaces; - static SettingsEntry showWhitespacesSize; + static SettingsEntry showWhitespace; + static SettingsEntry showWhitespaceSize; static SettingsEntry autoIndent; - static SettingsEntry tabIndents; - static SettingsEntry indentationsUseTabs; + static SettingsEntry indentStyle; + static SettingsEntry highlightCurrentLine; + static SettingsEntry enableBraceMatching; static Settings *inst(bool erase = false); From 9bcb38df484e238f55bddff84b8b53ad1e9337e4 Mon Sep 17 00:00:00 2001 From: Torsten Paul Date: Tue, 30 Dec 2014 02:37:16 +0100 Subject: [PATCH 7/8] Change settings handling to use Value objects. --- src/Preferences.cc | 165 ++++++++++++++++++++++++--------- src/Preferences.h | 15 ++- src/Preferences.ui | 8 +- src/scintillaeditor.cpp | 106 +++++++++------------ src/settings.cc | 198 +++++++++++++--------------------------- src/settings.h | 101 ++++++-------------- src/value.h | 4 + 7 files changed, 279 insertions(+), 318 deletions(-) diff --git a/src/Preferences.cc b/src/Preferences.cc index c6287a93..34ae2afd 100644 --- a/src/Preferences.cc +++ b/src/Preferences.cc @@ -39,7 +39,6 @@ #endif #include "colormap.h" #include "rendersettings.h" -#include "settings.h" Preferences *Preferences::instance = NULL; @@ -49,22 +48,48 @@ 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(); + const Value getValue(const Settings::SettingsEntry& entry, const std::string& value) const { + if (value.empty()) { + return entry.defaultValue(); + } + + try { + switch (entry.defaultValue().type()) { + case Value::STRING: + return Value(value); + case Value::NUMBER: + return Value(boost::lexical_cast(value)); + case Value::BOOL: + return Value(boost::lexical_cast(value)); + default: + assert(false && "invalid value type for settings"); + } + } catch (const boost::bad_lexical_cast& e) { + return entry.defaultValue(); + } + } + + virtual void handle(Settings::SettingsEntry& entry) const { + Settings::Settings *s = Settings::Settings::inst(); + + std::string key = entry.category() + "/" + entry.name(); std::string value = settings.value(QString::fromStdString(key)).toString().toStdString(); - entry->from_string(value); + s->set(entry, getValue(entry, value)); } }; class SettingsWriter : public Settings::Visitor { - virtual void handle(Settings::SettingsEntryBase * entry) const { + virtual void handle(Settings::SettingsEntry& entry) const { + Settings::Settings *s = Settings::Settings::inst(); + QSettings settings; - QString key = QString::fromStdString(entry->category() + "/" + entry->name()); - if (entry->is_default()) { + QString key = QString::fromStdString(entry.category() + "/" + entry.name()); + if (entry.is_default()) { settings.remove(key); } else { - settings.setValue(key, QString::fromStdString(entry->to_string())); + Value value = s->get(entry); + settings.setValue(key, QString::fromStdString(value.toString())); } } }; @@ -165,9 +190,20 @@ void Preferences::init() { this->polysetCacheSizeEdit->setValidator(validator); this->opencsgLimitEdit->setValidator(validator); - Settings::Settings *s = Settings::Settings::inst(); + initComboBox(this->comboBoxIndentUsing, Settings::Settings::indentStyle); + initComboBox(this->comboBoxLineWrap, Settings::Settings::lineWrap); + initComboBox(this->comboBoxLineWrapIndentationStyle, Settings::Settings::lineWrapIndentationStyle); + initComboBox(this->comboBoxLineWrapVisualizationEnd, Settings::Settings::lineWrapVisualizationEnd); + initComboBox(this->comboBoxLineWrapVisualizationStart, Settings::Settings::lineWrapVisualizationBegin); + initComboBox(this->comboBoxShowWhitespace, Settings::Settings::showWhitespace); + initComboBox(this->comboBoxTabKeyFunction, Settings::Settings::tabKeyFunction); + initSpinBox(this->spinBoxIndentationWidth, Settings::Settings::indentationWidth); + initSpinBox(this->spinBoxLineWrapIndentationIndent, Settings::Settings::lineWrapIndentation); + initSpinBox(this->spinBoxShowWhitespaceSize, Settings::Settings::showWhitespaceSize); + initSpinBox(this->spinBoxTabWidth, Settings::Settings::tabWidth); + SettingsReader settingsReader; - s->visit(&settingsReader); + Settings::Settings::inst()->visit(settingsReader); emit editorConfigChanged(); } @@ -437,86 +473,80 @@ void Preferences::on_launcherBox_toggled(bool state) void Preferences::on_spinBoxIndentationWidth_valueChanged(int val) { - Settings::Settings::inst()->set(Settings::Settings::indentationWidth, val); + Settings::Settings::inst()->set(Settings::Settings::indentationWidth, Value(val)); fireEditorConfigChanged(); } void Preferences::on_spinBoxTabWidth_valueChanged(int val) { - Settings::Settings::inst()->set(Settings::Settings::tabWidth, val); + Settings::Settings::inst()->set(Settings::Settings::tabWidth, Value(val)); fireEditorConfigChanged(); } void Preferences::on_comboBoxLineWrap_activated(int val) { - Settings::Settings::inst()->set(Settings::Settings::lineWrap, (Settings::LineWrap)val); - fireEditorConfigChanged(); + applyComboBox(comboBoxLineWrap, val, Settings::Settings::lineWrap); } -void Preferences::on_comboBoxLineWrapIndentation_activated(int val) +void Preferences::on_comboBoxLineWrapIndentationStyle_activated(int val) { - Settings::Settings::inst()->set(Settings::Settings::lineWrapIndentationStyle, (Settings::LineWrapIndentationStyle)val); - fireEditorConfigChanged(); + applyComboBox(comboBoxLineWrapIndentationStyle, val, Settings::Settings::lineWrapIndentationStyle); } void Preferences::on_spinBoxLineWrapIndentationIndent_valueChanged(int val) { - Settings::Settings::inst()->set(Settings::Settings::lineWrapIndentation, val); + Settings::Settings::inst()->set(Settings::Settings::lineWrapIndentation, Value(val)); fireEditorConfigChanged(); } void Preferences::on_comboBoxLineWrapVisualizationStart_activated(int val) { - Settings::Settings::inst()->set(Settings::Settings::lineWrapVisualizationBegin, (Settings::LineWrapVisualization)val); - fireEditorConfigChanged(); + applyComboBox(comboBoxLineWrapVisualizationStart, val, Settings::Settings::lineWrapVisualizationBegin); } void Preferences::on_comboBoxLineWrapVisualizationEnd_activated(int val) { - Settings::Settings::inst()->set(Settings::Settings::lineWrapVisualizationEnd, (Settings::LineWrapVisualization)val); - fireEditorConfigChanged(); + applyComboBox(comboBoxLineWrapVisualizationEnd, val, Settings::Settings::lineWrapVisualizationEnd); } void Preferences::on_comboBoxShowWhitespace_activated(int val) { - Settings::Settings::inst()->set(Settings::Settings::showWhitespace, (Settings::ShowWhitespace)val); - fireEditorConfigChanged(); + applyComboBox(comboBoxShowWhitespace, val, Settings::Settings::showWhitespace); } void Preferences::on_spinBoxShowWhitespaceSize_valueChanged(int val) { - Settings::Settings::inst()->set(Settings::Settings::showWhitespaceSize, val); + Settings::Settings::inst()->set(Settings::Settings::showWhitespaceSize, Value(val)); fireEditorConfigChanged(); } void Preferences::on_checkBoxAutoIndent_toggled(bool val) { - Settings::Settings::inst()->set(Settings::Settings::autoIndent, val); + Settings::Settings::inst()->set(Settings::Settings::autoIndent, Value(val)); fireEditorConfigChanged(); } void Preferences::on_comboBoxIndentUsing_activated(int val) { - Settings::Settings::inst()->set(Settings::Settings::indentStyle, (Settings::IndentStyle)val); - fireEditorConfigChanged(); + applyComboBox(comboBoxIndentUsing, val, Settings::Settings::indentStyle); } void Preferences::on_checkBoxHighlightCurrentLine_toggled(bool val) { - Settings::Settings::inst()->set(Settings::Settings::highlightCurrentLine, val); + Settings::Settings::inst()->set(Settings::Settings::highlightCurrentLine, Value(val)); fireEditorConfigChanged(); } void Preferences::on_checkBoxEnableBraceMatching_toggled(bool val) { - Settings::Settings::inst()->set(Settings::Settings::enableBraceMatching, val); + Settings::Settings::inst()->set(Settings::Settings::enableBraceMatching, Value(val)); fireEditorConfigChanged(); } void Preferences::fireEditorConfigChanged() const { SettingsWriter settingsWriter; - Settings::Settings::inst()->visit(&settingsWriter); + Settings::Settings::inst()->visit(settingsWriter); emit editorConfigChanged(); } @@ -615,19 +645,66 @@ void Preferences::updateGUI() this->launcherBox->setChecked(getValue("launcher/showOnStartup").toBool()); Settings::Settings *s = Settings::Settings::inst(); - this->spinBoxIndentationWidth->setValue(s->get(Settings::Settings::indentationWidth)); - this->spinBoxTabWidth->setValue(s->get(Settings::Settings::tabWidth)); - this->comboBoxLineWrap->setCurrentIndex(s->get(Settings::Settings::lineWrap)); - this->comboBoxLineWrapIndentation->setCurrentIndex(s->get(Settings::Settings::lineWrapIndentationStyle)); - this->spinBoxLineWrapIndentationIndent->setValue(s->get(Settings::Settings::lineWrapIndentation)); - this->comboBoxLineWrapVisualizationStart->setCurrentIndex(s->get(Settings::Settings::lineWrapVisualizationBegin)); - this->comboBoxLineWrapVisualizationEnd->setCurrentIndex(s->get(Settings::Settings::lineWrapVisualizationEnd)); - this->comboBoxShowWhitespace->setCurrentIndex(s->get(Settings::Settings::showWhitespace)); - this->spinBoxShowWhitespaceSize->setValue(s->get(Settings::Settings::showWhitespaceSize)); - this->checkBoxAutoIndent->setChecked(s->get(Settings::Settings::autoIndent)); - this->comboBoxIndentUsing->setCurrentIndex(s->get(Settings::Settings::indentStyle)); - this->checkBoxHighlightCurrentLine->setChecked(s->get(Settings::Settings::highlightCurrentLine)); - this->checkBoxEnableBraceMatching->setChecked(s->get(Settings::Settings::enableBraceMatching)); + updateComboBox(this->comboBoxLineWrap, Settings::Settings::lineWrap); + updateComboBox(this->comboBoxLineWrapIndentationStyle, Settings::Settings::lineWrapIndentationStyle); + updateComboBox(this->comboBoxLineWrapVisualizationStart, Settings::Settings::lineWrapVisualizationBegin); + updateComboBox(this->comboBoxLineWrapVisualizationEnd, Settings::Settings::lineWrapVisualizationEnd); + updateComboBox(this->comboBoxShowWhitespace, Settings::Settings::showWhitespace); + updateComboBox(this->comboBoxIndentUsing, Settings::Settings::indentStyle); + updateComboBox(this->comboBoxTabKeyFunction, Settings::Settings::tabKeyFunction); + this->spinBoxIndentationWidth->setValue(s->get(Settings::Settings::indentationWidth).toDouble()); + this->spinBoxTabWidth->setValue(s->get(Settings::Settings::tabWidth).toDouble()); + this->spinBoxLineWrapIndentationIndent->setValue(s->get(Settings::Settings::lineWrapIndentation).toDouble()); + this->spinBoxShowWhitespaceSize->setValue(s->get(Settings::Settings::showWhitespaceSize).toDouble()); + this->checkBoxAutoIndent->setChecked(s->get(Settings::Settings::autoIndent).toBool()); + this->checkBoxHighlightCurrentLine->setChecked(s->get(Settings::Settings::highlightCurrentLine).toBool()); + this->checkBoxEnableBraceMatching->setChecked(s->get(Settings::Settings::enableBraceMatching).toBool()); +} + +void Preferences::initComboBox(QComboBox *comboBox, const Settings::SettingsEntry& entry) +{ + comboBox->clear(); + Value::VectorType vector = entry.range().toVector(); + for (Value::VectorType::iterator it = vector.begin();it != vector.end();it++) { + QString val = QString::fromStdString((*it)[0].toString()); + QString text = QString::fromStdString((*it)[1].toString()); + comboBox->addItem(text, val); + } +} + +void Preferences::initSpinBox(QSpinBox *spinBox, const Settings::SettingsEntry& entry) +{ + Value::RangeType range = entry.range().toRange(); + spinBox->setMinimum(range.begin_value()); + spinBox->setMaximum(range.end_value()); +} + +void Preferences::updateComboBox(QComboBox *comboBox, const Settings::SettingsEntry& entry) +{ + Settings::Settings *s = Settings::Settings::inst(); + + Value value = s->get(entry); + QString text = QString::fromStdString(value.toString()); + int idx = comboBox->findData(text); + if (idx >= 0) { + comboBox->setCurrentIndex(idx); + } else { + Value defaultValue = entry.defaultValue(); + QString defaultText = QString::fromStdString(defaultValue.toString()); + int defIdx = comboBox->findData(defaultText); + if (defIdx >= 0) { + comboBox->setCurrentIndex(defIdx); + } else { + comboBox->setCurrentIndex(0); + } + } +} + +void Preferences::applyComboBox(QComboBox *comboBox, int val, Settings::SettingsEntry& entry) +{ + QString s = comboBox->itemData(val).toString(); + Settings::Settings::inst()->set(entry, Value(s.toStdString())); + fireEditorConfigChanged(); } void Preferences::apply() const diff --git a/src/Preferences.h b/src/Preferences.h index 4432140f..a5604dad 100644 --- a/src/Preferences.h +++ b/src/Preferences.h @@ -1,9 +1,11 @@ #pragma once -#include "qtgettext.h" #include #include + +#include "qtgettext.h" #include "ui_Preferences.h" +#include "settings.h" class Preferences : public QMainWindow, public Ui::Preferences { @@ -47,7 +49,7 @@ public slots: void on_spinBoxIndentationWidth_valueChanged(int); void on_spinBoxTabWidth_valueChanged(int); void on_comboBoxLineWrap_activated(int); - void on_comboBoxLineWrapIndentation_activated(int); + void on_comboBoxLineWrapIndentationStyle_activated(int); void on_spinBoxLineWrapIndentationIndent_valueChanged(int); void on_comboBoxLineWrapVisualizationStart_activated(int); void on_comboBoxLineWrapVisualizationEnd_activated(int); @@ -79,6 +81,15 @@ private: void fireEditorConfigChanged() const; void addPrefPage(QActionGroup *group, QAction *action, QWidget *widget); + /** Initialize combobox list values from the settings range values */ + void initComboBox(QComboBox *comboBox, const Settings::SettingsEntry& entry); + /** Initialize spinbox min/max values from the settings range values */ + void initSpinBox(QSpinBox *spinBox, const Settings::SettingsEntry& entry); + /** Update combobox from current settings */ + void updateComboBox(QComboBox *comboBox, const Settings::SettingsEntry& entry); + /** Set value from combobox to settings */ + void applyComboBox(QComboBox *comboBox, int val, Settings::SettingsEntry& entry); + QSettings::SettingsMap defaultmap; QHash prefPages; diff --git a/src/Preferences.ui b/src/Preferences.ui index f6d26e17..7c411229 100644 --- a/src/Preferences.ui +++ b/src/Preferences.ui @@ -6,8 +6,8 @@ 0 0 - 607 - 432 + 621 + 413 @@ -780,7 +780,7 @@ - + Fixed @@ -1201,7 +1201,7 @@ 0 0 - 640 + 596 499 diff --git a/src/scintillaeditor.cpp b/src/scintillaeditor.cpp index 46b54583..a240d24c 100644 --- a/src/scintillaeditor.cpp +++ b/src/scintillaeditor.cpp @@ -10,67 +10,59 @@ class SettingsConverter { public: - QsciScintilla::WrapMode fromWrapMode(Settings::LineWrap val); - QsciScintilla::WrapVisualFlag fromLineWrapVisualization(Settings::LineWrapVisualization val); - QsciScintilla::WrapIndentMode fromLineWrapIndentationStyle(Settings::LineWrapIndentationStyle val); - QsciScintilla::WhitespaceVisibility fromShowWhitespaces(Settings::ShowWhitespace val); + QsciScintilla::WrapMode toWrapMode(Value val); + QsciScintilla::WrapVisualFlag toLineWrapVisualization(Value val); + QsciScintilla::WrapIndentMode toLineWrapIndentationStyle(Value val); + QsciScintilla::WhitespaceVisibility toShowWhitespaces(Value val); }; -QsciScintilla::WrapMode SettingsConverter::fromWrapMode(Settings::LineWrap val) +QsciScintilla::WrapMode SettingsConverter::toWrapMode(Value val) { - switch (val) { - case Settings::LINE_WRAP_NONE: - return QsciScintilla::WrapNone; - case Settings::LINE_WRAP_CHARACTER: + std::string v = val.toString(); + if (v == "Char") { return QsciScintilla::WrapCharacter; - case Settings::LINE_WRAP_WORD: + } else if (v == "Word") { return QsciScintilla::WrapWord; - default: - assert(false && "unknown wrap mode setting"); + } else { + return QsciScintilla::WrapNone; } } -QsciScintilla::WrapVisualFlag SettingsConverter::fromLineWrapVisualization(Settings::LineWrapVisualization val) +QsciScintilla::WrapVisualFlag SettingsConverter::toLineWrapVisualization(Value val) { - switch (val) { - case Settings::LINE_WRAP_VISUALIZATION_NONE: - return QsciScintilla::WrapFlagNone; - case Settings::LINE_WRAP_VISUALIZATION_TEXT: + std::string v = val.toString(); + if (v == "Text") { return QsciScintilla::WrapFlagByText; - case Settings::LINE_WRAP_VISUALIZATION_BORDER: + } else if (v == "Border") { return QsciScintilla::WrapFlagByBorder; - case Settings::LINE_WRAP_VISUALIZATION_MARGIN: + } else if (v == "Margin") { return QsciScintilla::WrapFlagInMargin; - default: - assert(false && "unknown wrap visualization setting"); + } else { + return QsciScintilla::WrapFlagNone; } } -QsciScintilla::WrapIndentMode SettingsConverter::fromLineWrapIndentationStyle(Settings::LineWrapIndentationStyle val) +QsciScintilla::WrapIndentMode SettingsConverter::toLineWrapIndentationStyle(Value val) { - switch (val) { - case Settings::LINE_WRAP_INDENTATION_FIXED: - return QsciScintilla::WrapIndentFixed; - case Settings::LINE_WRAP_INDENTATION_SAME: + std::string v = val.toString(); + if (v == "Same") { return QsciScintilla::WrapIndentSame; - case Settings::LINE_WRAP_INDENTATION_INDENTED: + } else if (v == "Indented") { return QsciScintilla::WrapIndentIndented; - default: - assert(false && "unknown wrap indentation style setting"); + } else { + return QsciScintilla::WrapIndentFixed; } } -QsciScintilla::WhitespaceVisibility SettingsConverter::fromShowWhitespaces(Settings::ShowWhitespace val) +QsciScintilla::WhitespaceVisibility SettingsConverter::toShowWhitespaces(Value val) { - switch(val) { - case Settings::SHOW_WHITESPACES_NEVER: - return QsciScintilla::WsInvisible; - case Settings::SHOW_WHITESPACES_ALWAYS: + std::string v = val.toString(); + if (v == "Always") { return QsciScintilla::WsVisible; - case Settings::SHOW_WHITESPACES_AFTER_INDENTATION: + } else if (v == "AfterIndentation") { return QsciScintilla::WsVisibleAfterIndent; - default: - assert(false && "unknown show whitespace setting"); + } else { + return QsciScintilla::WsInvisible; } } @@ -165,32 +157,24 @@ void ScintillaEditor::applySettings() SettingsConverter conv; Settings::Settings *s = Settings::Settings::inst(); - qsci->setIndentationWidth(s->get(Settings::Settings::indentationWidth)); - qsci->setTabWidth(s->get(Settings::Settings::tabWidth)); - qsci->setWrapMode(conv.fromWrapMode(s->get(Settings::Settings::lineWrap))); - qsci->setWrapIndentMode(conv.fromLineWrapIndentationStyle(s->get(Settings::Settings::lineWrapIndentationStyle))); - qsci->setWrapVisualFlags(conv.fromLineWrapVisualization(s->get(Settings::Settings::lineWrapVisualizationEnd)), - conv.fromLineWrapVisualization(s->get(Settings::Settings::lineWrapVisualizationBegin)), - s->get(Settings::Settings::lineWrapIndentation)); - qsci->setWhitespaceVisibility(conv.fromShowWhitespaces(s->get(Settings::Settings::showWhitespace))); - qsci->setWhitespaceSize(s->get(Settings::Settings::showWhitespaceSize)); - qsci->setAutoIndent(s->get(Settings::Settings::autoIndent)); + qsci->setIndentationWidth(s->get(Settings::Settings::indentationWidth).toDouble()); + qsci->setTabWidth(s->get(Settings::Settings::tabWidth).toDouble()); + qsci->setWrapMode(conv.toWrapMode(s->get(Settings::Settings::lineWrap))); + qsci->setWrapIndentMode(conv.toLineWrapIndentationStyle(s->get(Settings::Settings::lineWrapIndentationStyle))); + qsci->setWrapVisualFlags(conv.toLineWrapVisualization(s->get(Settings::Settings::lineWrapVisualizationEnd)), + conv.toLineWrapVisualization(s->get(Settings::Settings::lineWrapVisualizationBegin)), + s->get(Settings::Settings::lineWrapIndentation).toDouble()); + qsci->setWhitespaceVisibility(conv.toShowWhitespaces(s->get(Settings::Settings::showWhitespace))); + qsci->setWhitespaceSize(s->get(Settings::Settings::showWhitespaceSize).toDouble()); + qsci->setAutoIndent(s->get(Settings::Settings::autoIndent).toBool()); - switch (s->get(Settings::Settings::indentStyle)) { - case Settings::INDENT_SPACES: - qsci->setTabIndents(true); - qsci->setIndentationsUseTabs(false); - break; - case Settings::INDENT_TABS: - qsci->setTabIndents(false); - qsci->setIndentationsUseTabs(true); - break; - default: - assert(false && "unknown indent style"); - } + std::string indentStyle = s->get(Settings::Settings::indentStyle).toString(); + qsci->setIndentationsUseTabs(indentStyle == "Tabs"); + std::string tabKeyFunction = s->get(Settings::Settings::tabKeyFunction).toString(); + qsci->setTabIndents(tabKeyFunction == "Indent"); - qsci->setBraceMatching(s->get(Settings::Settings::enableBraceMatching) ? QsciScintilla::SloppyBraceMatch : QsciScintilla::NoBraceMatch); - qsci->setCaretLineVisible(s->get(Settings::Settings::highlightCurrentLine)); + qsci->setBraceMatching(s->get(Settings::Settings::enableBraceMatching).toBool() ? QsciScintilla::SloppyBraceMatch : QsciScintilla::NoBraceMatch); + qsci->setCaretLineVisible(s->get(Settings::Settings::highlightCurrentLine).toBool()); } void ScintillaEditor::setPlainText(const QString &text) diff --git a/src/settings.cc b/src/settings.cc index 2888d96c..8af5fea3 100644 --- a/src/settings.cc +++ b/src/settings.cc @@ -1,132 +1,72 @@ #include "settings.h" -#include "value.h" +#include "printutils.h" + +#include +using namespace boost::assign; // bring 'operator+=()' into scope namespace Settings { -std::list SettingsEntryBase::entries; +static std::list entries; -SettingsEntryBase::SettingsEntryBase(const std::string category, const std::string name) : _category(category), _name(name) +SettingsEntry::SettingsEntry(const std::string category, const std::string name, const Value range, const Value def) + : _category(category), _name(name), _value(def), _range(range), _default(def) { entries.push_back(this); } -SettingsEntryBase::~SettingsEntryBase() +SettingsEntry::~SettingsEntry() { } -const std::string & SettingsEntryBase::category() const +const std::string & SettingsEntry::category() const { return _category; } -const std::string & SettingsEntryBase::name() const +const std::string & SettingsEntry::name() const { return _name; } -template -SettingsEntry::SettingsEntry(const std::string category, const std::string name, T defaultValue) - : SettingsEntryBase(category, name), value(defaultValue), defaultValue(defaultValue) +const Value & SettingsEntry::defaultValue() const { + return _default; } -template -SettingsEntry::~SettingsEntry() +const Value & SettingsEntry::range() const { + return _range; } -template -bool SettingsEntry::is_default() const +bool SettingsEntry::is_default() const { - return defaultValue == value; + return _value == _default; } -template -std::string SettingsEntry::to_string() const -{ - return boost::lexical_cast(value); +static Value value(std::string s1, std::string s2) { + Value::VectorType v; + v += Value(s1), Value(s2); + return v; } -template <> -std::string SettingsEntry::to_string() const -{ - return boost::lexical_cast(value); +static Value values(std::string s1, std::string s1disp, std::string s2, std::string s2disp) { + Value::VectorType v; + v += value(s1, s1disp), value(s2, s2disp); + return v; } -template -void SettingsEntry::from_string(std::string val) -{ - try { - value = boost::lexical_cast(val); - } catch (const boost::bad_lexical_cast &e) { - value = defaultValue; - } +static Value values(std::string s1, std::string s1disp, std::string s2, std::string s2disp, std::string s3, std::string s3disp) { + Value::VectorType v; + v += value(s1, s1disp), value(s2, s2disp), value(s3, s3disp); + return v; } -template <> -void SettingsEntry::from_string(std::string val) -{ - try { - value = (LineWrap)boost::lexical_cast(val); - } catch (const boost::bad_lexical_cast &e) { - value = defaultValue; - } +static Value values(std::string s1, std::string s1disp, std::string s2, std::string s2disp, std::string s3, std::string s3disp, std::string s4, std::string s4disp) { + Value::VectorType v; + v += value(s1, s1disp), value(s2, s2disp), value(s3, s3disp), value(s4, s4disp); + return v; } -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 = (ShowWhitespace)boost::lexical_cast(val); - } catch (const boost::bad_lexical_cast &e) { - value = defaultValue; - } -} - -template <> -void SettingsEntry::from_string(std::string val) -{ - try { - value = (IndentStyle)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); -SettingsEntry Settings::lineWrapIndentationStyle("editor", "lineWrapIndentationStyle", LINE_WRAP_INDENTATION_FIXED); -SettingsEntry Settings::lineWrapIndentation("editor", "lineWrapIndentation", 4); -SettingsEntry Settings::lineWrapVisualizationBegin("editor", "lineWrapVisualizationBegin", LINE_WRAP_VISUALIZATION_NONE); -SettingsEntry Settings::lineWrapVisualizationEnd("editor", "lineWrapVisualizationEnd", LINE_WRAP_VISUALIZATION_BORDER); -SettingsEntry Settings::showWhitespace("editor", "showWhitespaces", SHOW_WHITESPACES_NEVER); -SettingsEntry Settings::showWhitespaceSize("editor", "showWhitespacesSize", 2); -SettingsEntry Settings::autoIndent("editor", "autoIndent", true); -SettingsEntry Settings::indentStyle("editor", "indentStyle", INDENT_SPACES); -SettingsEntry Settings::highlightCurrentLine("editor", "highlightCurrentLine", true); -SettingsEntry Settings::enableBraceMatching("editor", "enableBraceMatching", true); - Settings *Settings::inst(bool erase) { static Settings *instance = new Settings; @@ -147,59 +87,23 @@ Settings::~Settings() { } -void Settings::visit(class Visitor *visitor) +void Settings::visit(Visitor& visitor) { - for (std::list::iterator it = SettingsEntryBase::entries.begin();it != SettingsEntryBase::entries.end();it++) { - visitor->handle(*it); + for (std::list::iterator it = entries.begin();it != entries.end();it++) { + visitor.handle(*(*it)); } } -template -T Settings::defaultValue(const SettingsEntry &entry) +Value Settings::get(const SettingsEntry& entry) { - return entry.defaultValue; + return entry._value; } -template -T Settings::get(const SettingsEntry &entry) +void Settings::set(SettingsEntry& entry, const Value val) { - return entry.value; + entry._value = val; } -template -void Settings::set(SettingsEntry &entry, T val) -{ - entry.value = val; -} - -template bool Settings::defaultValue(const SettingsEntry&); -template bool Settings::get(const SettingsEntry&); -template void Settings::set(SettingsEntry&, bool); - -template int Settings::defaultValue(const SettingsEntry&); -template int Settings::get(const SettingsEntry&); -template void Settings::set(SettingsEntry&, int); - -template LineWrap Settings::defaultValue(const SettingsEntry&); -template LineWrap Settings::get(const SettingsEntry&); -template void Settings::set(SettingsEntry&, LineWrap); - -template LineWrapVisualization Settings::defaultValue(const SettingsEntry&); -template LineWrapVisualization Settings::get(const SettingsEntry&); -template void Settings::set(SettingsEntry&, LineWrapVisualization); - -template LineWrapIndentationStyle Settings::defaultValue(const SettingsEntry&); -template LineWrapIndentationStyle Settings::get(const SettingsEntry&); -template void Settings::set(SettingsEntry&, LineWrapIndentationStyle); - -template ShowWhitespace Settings::defaultValue(const SettingsEntry&); -template ShowWhitespace Settings::get(const SettingsEntry&); -template void Settings::set(SettingsEntry&, ShowWhitespace); - -template IndentStyle Settings::defaultValue(const SettingsEntry&); -template IndentStyle Settings::get(const SettingsEntry&); -template void Settings::set(SettingsEntry&, IndentStyle); - Visitor::Visitor() { } @@ -208,4 +112,28 @@ Visitor::~Visitor() { } +/* + * Supported settings entry types are: bool / int and string selection + * + * String selection is used to handle comboboxes and has two values + * per config selection. The first value is used internally for both + * finding the combobox selection and for storing the value in the + * external settings file. The second value is the display value that + * can be translated. + */ +SettingsEntry Settings::indentationWidth("editor", "indentationWidth", Value(Value::RangeType(1, 16)), Value(4)); +SettingsEntry Settings::tabWidth("editor", "tabWidth", Value(Value::RangeType(1, 16)), Value(8)); +SettingsEntry Settings::lineWrap("editor", "lineWrap", values("None", _("None"), "Char", _("Wrap at character boundaries"), "Word", _("Wrap at word boundaries")), Value("Word")); +SettingsEntry Settings::lineWrapIndentationStyle("editor", "lineWrapIndentationStyle", values("Fixed", _("Fixed"), "Same", _("Same"), "Indented", _("Indented")), Value("Fixed")); +SettingsEntry Settings::lineWrapIndentation("editor", "lineWrapIndentation", Value(Value::RangeType(0, 999)), Value(4)); +SettingsEntry Settings::lineWrapVisualizationBegin("editor", "lineWrapVisualizationBegin", values("None", _("None"), "Text", _("Text"), "Border", _("Border"), "Margin", _("Margin")), Value("None")); +SettingsEntry Settings::lineWrapVisualizationEnd("editor", "lineWrapVisualizationEnd", values("None", _("None"), "Text", _("Text"), "Border", _("Border"), "Margin", _("Margin")), Value("Border")); +SettingsEntry Settings::showWhitespace("editor", "showWhitespaces", values("Never", _("Never"), "Always", _("Always"), "AfterIndentation", _("After indentation")), Value("Never")); +SettingsEntry Settings::showWhitespaceSize("editor", "showWhitespacesSize", Value(Value::RangeType(1, 16)), Value(2)); +SettingsEntry Settings::autoIndent("editor", "autoIndent", Value(true), Value(true)); +SettingsEntry Settings::indentStyle("editor", "indentStyle", values("Spaces", _("Spaces"), "Tabs", _("Tabs")), Value("Spaces")); +SettingsEntry Settings::tabKeyFunction("editor", "tabKeyFunction", values("Indent", _("Indent"), "InsertTab", _("Insert Tab")), Value("Indent")); +SettingsEntry Settings::highlightCurrentLine("editor", "highlightCurrentLine", Value(true), Value(true)); +SettingsEntry Settings::enableBraceMatching("editor", "enableBraceMatching", Value(true), Value(true)); + } diff --git a/src/settings.h b/src/settings.h index f52273ad..a3099fe8 100644 --- a/src/settings.h +++ b/src/settings.h @@ -4,102 +4,59 @@ #include #include +#include "value.h" + namespace Settings { -typedef enum { - INDENT_SPACES, - INDENT_TABS, -} IndentStyle; - -typedef enum { - LINE_WRAP_NONE, - LINE_WRAP_CHARACTER, - LINE_WRAP_WORD, -} LineWrap; - -typedef enum { - LINE_WRAP_INDENTATION_FIXED, - LINE_WRAP_INDENTATION_SAME, - LINE_WRAP_INDENTATION_INDENTED, -} LineWrapIndentationStyle; - -typedef enum { - LINE_WRAP_VISUALIZATION_NONE, - LINE_WRAP_VISUALIZATION_TEXT, - LINE_WRAP_VISUALIZATION_BORDER, - LINE_WRAP_VISUALIZATION_MARGIN, -} LineWrapVisualization; - -typedef enum { - SHOW_WHITESPACES_NEVER, - SHOW_WHITESPACES_ALWAYS, - SHOW_WHITESPACES_AFTER_INDENTATION, -} ShowWhitespace; - -class SettingsEntryBase +class SettingsEntry { private: std::string _category; std::string _name; + Value _value; + Value _range; + Value _default; 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; + virtual const Value & defaultValue() const; + virtual const Value & range() const; + virtual bool is_default() const; protected: - SettingsEntryBase(const std::string category, const std::string name); - ~SettingsEntryBase(); - - static std::list entries; - - friend class Settings; -}; - -template -class SettingsEntry : public SettingsEntryBase -{ -private: - T value; - T defaultValue; - - SettingsEntry(const std::string category, const std::string name, T defaultValue); + SettingsEntry(const std::string category, const std::string name, const Value range, const Value def); virtual ~SettingsEntry(); - virtual bool is_default() const; - virtual std::string to_string() const; - virtual void from_string(std::string); - friend class Settings; }; class Settings { public: - static SettingsEntry indentationWidth; - static SettingsEntry tabWidth; - static SettingsEntry lineWrap; - static SettingsEntry lineWrapIndentationStyle; - static SettingsEntry lineWrapIndentation; - static SettingsEntry lineWrapVisualizationBegin; - static SettingsEntry lineWrapVisualizationEnd; - static SettingsEntry showWhitespace; - static SettingsEntry showWhitespaceSize; - static SettingsEntry autoIndent; - static SettingsEntry indentStyle; - static SettingsEntry highlightCurrentLine; - static SettingsEntry enableBraceMatching; + static SettingsEntry indentationWidth; + static SettingsEntry tabWidth; + static SettingsEntry lineWrap; + static SettingsEntry lineWrapIndentationStyle; + static SettingsEntry lineWrapIndentation; + static SettingsEntry lineWrapVisualizationBegin; + static SettingsEntry lineWrapVisualizationEnd; + static SettingsEntry showWhitespace; + static SettingsEntry showWhitespaceSize; + static SettingsEntry autoIndent; + static SettingsEntry indentStyle; + static SettingsEntry tabKeyFunction; + static SettingsEntry highlightCurrentLine; + static SettingsEntry enableBraceMatching; static Settings *inst(bool erase = false); - void visit(class Visitor *visitor); + void visit(class Visitor& visitor); - template T defaultValue(const SettingsEntry &entry); - template T get(const SettingsEntry &entry); - template void set(SettingsEntry &entry, T val); + Value defaultValue(const SettingsEntry& entry); + Value get(const SettingsEntry& entry); + void set(SettingsEntry& entry, const Value val); private: Settings(); @@ -112,7 +69,7 @@ public: Visitor(); virtual ~Visitor(); - virtual void handle(SettingsEntryBase * entry) const = 0; + virtual void handle(SettingsEntry& entry) const = 0; }; } \ No newline at end of file diff --git a/src/value.h b/src/value.h index 63b57ce6..f646681f 100644 --- a/src/value.h +++ b/src/value.h @@ -82,6 +82,10 @@ public: this->end_val == other.end_val; } + double begin_value() { return begin_val; } + double step_value() { return step_val; } + double end_value() { return end_val; } + iterator begin() { return iterator(*this, RANGE_TYPE_BEGIN); } iterator end() { return iterator(*this, RANGE_TYPE_END); } From e69513fa2e6a88d05ef0e58eb7cd6aabe402e5c6 Mon Sep 17 00:00:00 2001 From: Marius Kintel Date: Tue, 30 Dec 2014 02:49:47 -0500 Subject: [PATCH 8/8] #1119 Added missing slot to change tab key function --- src/Preferences.cc | 5 +++++ src/Preferences.h | 37 +++++++++++++++++++++++-------------- 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/src/Preferences.cc b/src/Preferences.cc index 34ae2afd..11d64502 100644 --- a/src/Preferences.cc +++ b/src/Preferences.cc @@ -531,6 +531,11 @@ void Preferences::on_comboBoxIndentUsing_activated(int val) applyComboBox(comboBoxIndentUsing, val, Settings::Settings::indentStyle); } +void Preferences::on_comboBoxTabKeyFunction_activated(int val) +{ + applyComboBox(comboBoxTabKeyFunction, val, Settings::Settings::tabKeyFunction); +} + void Preferences::on_checkBoxHighlightCurrentLine_toggled(bool val) { Settings::Settings::inst()->set(Settings::Settings::highlightCurrentLine, Value(val)); diff --git a/src/Preferences.h b/src/Preferences.h index a5604dad..35c7d625 100644 --- a/src/Preferences.h +++ b/src/Preferences.h @@ -45,20 +45,29 @@ public slots: void on_launcherBox_toggled(bool); void on_editorType_editTextChanged(const QString &); - // editor settings - void on_spinBoxIndentationWidth_valueChanged(int); - void on_spinBoxTabWidth_valueChanged(int); - void on_comboBoxLineWrap_activated(int); - void on_comboBoxLineWrapIndentationStyle_activated(int); - void on_spinBoxLineWrapIndentationIndent_valueChanged(int); - void on_comboBoxLineWrapVisualizationStart_activated(int); - void on_comboBoxLineWrapVisualizationEnd_activated(int); - void on_comboBoxShowWhitespace_activated(int); - void on_spinBoxShowWhitespaceSize_valueChanged(int); - void on_checkBoxAutoIndent_toggled(bool); - void on_comboBoxIndentUsing_activated(int); - void on_checkBoxHighlightCurrentLine_toggled(bool); - void on_checkBoxEnableBraceMatching_toggled(bool); + // + // editor settings + // + + // Indentation + void on_checkBoxAutoIndent_toggled(bool); + void on_comboBoxIndentUsing_activated(int); + void on_spinBoxIndentationWidth_valueChanged(int); + void on_spinBoxTabWidth_valueChanged(int); + void on_comboBoxTabKeyFunction_activated(int); + void on_comboBoxShowWhitespace_activated(int); + void on_spinBoxShowWhitespaceSize_valueChanged(int); + + // Line wrap + void on_comboBoxLineWrap_activated(int); + void on_comboBoxLineWrapIndentationStyle_activated(int); + void on_spinBoxLineWrapIndentationIndent_valueChanged(int); + void on_comboBoxLineWrapVisualizationStart_activated(int); + void on_comboBoxLineWrapVisualizationEnd_activated(int); + + // Display + void on_checkBoxHighlightCurrentLine_toggled(bool); + void on_checkBoxEnableBraceMatching_toggled(bool); signals: void requestRedraw() const;