Change settings handling to use Value objects.

master
Torsten Paul 2014-12-30 02:37:16 +01:00
parent 2c89f562a3
commit 9bcb38df48
7 changed files with 279 additions and 318 deletions

View File

@ -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<int>(value));
case Value::BOOL:
return Value(boost::lexical_cast<bool>(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

View File

@ -1,9 +1,11 @@
#pragma once
#include "qtgettext.h"
#include <QMainWindow>
#include <QSettings>
#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<const QAction *, QWidget *> prefPages;

View File

@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>607</width>
<height>432</height>
<width>621</width>
<height>413</height>
</rect>
</property>
<property name="sizePolicy">
@ -780,7 +780,7 @@
</widget>
</item>
<item>
<widget class="QComboBox" name="comboBoxLineWrapIndentation">
<widget class="QComboBox" name="comboBoxLineWrapIndentationStyle">
<item>
<property name="text">
<string>Fixed</string>
@ -1201,7 +1201,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>640</width>
<width>596</width>
<height>499</height>
</rect>
</property>

View File

@ -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)

View File

@ -1,132 +1,72 @@
#include "settings.h"
#include "value.h"
#include "printutils.h"
#include <boost/assign/std/vector.hpp>
using namespace boost::assign; // bring 'operator+=()' into scope
namespace Settings {
std::list<SettingsEntryBase *> SettingsEntryBase::entries;
static std::list<SettingsEntry *> 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 <class T>
SettingsEntry<T>::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 <class T>
SettingsEntry<T>::~SettingsEntry()
const Value & SettingsEntry::range() const
{
return _range;
}
template <class T>
bool SettingsEntry<T>::is_default() const
bool SettingsEntry::is_default() const
{
return defaultValue == value;
return _value == _default;
}
template <class T>
std::string SettingsEntry<T>::to_string() const
{
return boost::lexical_cast<std::string>(value);
static Value value(std::string s1, std::string s2) {
Value::VectorType v;
v += Value(s1), Value(s2);
return v;
}
template <>
std::string SettingsEntry<int>::to_string() const
{
return boost::lexical_cast<std::string>(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 <class T>
void SettingsEntry<T>::from_string(std::string val)
{
try {
value = boost::lexical_cast<T>(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<LineWrap>::from_string(std::string val)
{
try {
value = (LineWrap)boost::lexical_cast<int>(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<LineWrapIndentationStyle>::from_string(std::string val)
{
try {
value = (LineWrapIndentationStyle)boost::lexical_cast<int>(val);
} catch (const boost::bad_lexical_cast &e) {
value = defaultValue;
}
}
template <>
void SettingsEntry<LineWrapVisualization>::from_string(std::string val)
{
try {
value = (LineWrapVisualization)boost::lexical_cast<int>(val);
} catch (const boost::bad_lexical_cast &e) {
value = defaultValue;
}
}
template <>
void SettingsEntry<ShowWhitespace>::from_string(std::string val)
{
try {
value = (ShowWhitespace)boost::lexical_cast<int>(val);
} catch (const boost::bad_lexical_cast &e) {
value = defaultValue;
}
}
template <>
void SettingsEntry<IndentStyle>::from_string(std::string val)
{
try {
value = (IndentStyle)boost::lexical_cast<int>(val);
} catch (const boost::bad_lexical_cast &e) {
value = defaultValue;
}
}
SettingsEntry<int> Settings::indentationWidth("editor", "indentationWidth", 4);
SettingsEntry<int> Settings::tabWidth("editor", "tabWidth", 8);
SettingsEntry<LineWrap> Settings::lineWrap("editor", "lineWrap", LINE_WRAP_WORD);
SettingsEntry<LineWrapIndentationStyle> Settings::lineWrapIndentationStyle("editor", "lineWrapIndentationStyle", LINE_WRAP_INDENTATION_FIXED);
SettingsEntry<int> Settings::lineWrapIndentation("editor", "lineWrapIndentation", 4);
SettingsEntry<LineWrapVisualization> Settings::lineWrapVisualizationBegin("editor", "lineWrapVisualizationBegin", LINE_WRAP_VISUALIZATION_NONE);
SettingsEntry<LineWrapVisualization> Settings::lineWrapVisualizationEnd("editor", "lineWrapVisualizationEnd", LINE_WRAP_VISUALIZATION_BORDER);
SettingsEntry<ShowWhitespace> Settings::showWhitespace("editor", "showWhitespaces", SHOW_WHITESPACES_NEVER);
SettingsEntry<int> Settings::showWhitespaceSize("editor", "showWhitespacesSize", 2);
SettingsEntry<bool> Settings::autoIndent("editor", "autoIndent", true);
SettingsEntry<IndentStyle> Settings::indentStyle("editor", "indentStyle", INDENT_SPACES);
SettingsEntry<bool> Settings::highlightCurrentLine("editor", "highlightCurrentLine", true);
SettingsEntry<bool> 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<SettingsEntryBase *>::iterator it = SettingsEntryBase::entries.begin();it != SettingsEntryBase::entries.end();it++) {
visitor->handle(*it);
for (std::list<SettingsEntry *>::iterator it = entries.begin();it != entries.end();it++) {
visitor.handle(*(*it));
}
}
template <typename T>
T Settings::defaultValue(const SettingsEntry<T> &entry)
Value Settings::get(const SettingsEntry& entry)
{
return entry.defaultValue;
return entry._value;
}
template <typename T>
T Settings::get(const SettingsEntry<T> &entry)
void Settings::set(SettingsEntry& entry, const Value val)
{
return entry.value;
entry._value = val;
}
template <typename T>
void Settings::set(SettingsEntry<T> &entry, T val)
{
entry.value = val;
}
template bool Settings::defaultValue(const SettingsEntry<bool>&);
template bool Settings::get(const SettingsEntry<bool>&);
template void Settings::set(SettingsEntry<bool>&, bool);
template int Settings::defaultValue(const SettingsEntry<int>&);
template int Settings::get(const SettingsEntry<int>&);
template void Settings::set(SettingsEntry<int>&, int);
template LineWrap Settings::defaultValue(const SettingsEntry<LineWrap>&);
template LineWrap Settings::get(const SettingsEntry<LineWrap>&);
template void Settings::set(SettingsEntry<LineWrap>&, LineWrap);
template LineWrapVisualization Settings::defaultValue(const SettingsEntry<LineWrapVisualization>&);
template LineWrapVisualization Settings::get(const SettingsEntry<LineWrapVisualization>&);
template void Settings::set(SettingsEntry<LineWrapVisualization>&, LineWrapVisualization);
template LineWrapIndentationStyle Settings::defaultValue(const SettingsEntry<LineWrapIndentationStyle>&);
template LineWrapIndentationStyle Settings::get(const SettingsEntry<LineWrapIndentationStyle>&);
template void Settings::set(SettingsEntry<LineWrapIndentationStyle>&, LineWrapIndentationStyle);
template ShowWhitespace Settings::defaultValue(const SettingsEntry<ShowWhitespace>&);
template ShowWhitespace Settings::get(const SettingsEntry<ShowWhitespace>&);
template void Settings::set(SettingsEntry<ShowWhitespace>&, ShowWhitespace);
template IndentStyle Settings::defaultValue(const SettingsEntry<IndentStyle>&);
template IndentStyle Settings::get(const SettingsEntry<IndentStyle>&);
template void Settings::set(SettingsEntry<IndentStyle>&, 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));
}

View File

@ -4,102 +4,59 @@
#include <list>
#include <string>
#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<SettingsEntryBase *> entries;
friend class Settings;
};
template <class T>
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<int> indentationWidth;
static SettingsEntry<int> tabWidth;
static SettingsEntry<LineWrap> lineWrap;
static SettingsEntry<LineWrapIndentationStyle> lineWrapIndentationStyle;
static SettingsEntry<int> lineWrapIndentation;
static SettingsEntry<LineWrapVisualization> lineWrapVisualizationBegin;
static SettingsEntry<LineWrapVisualization> lineWrapVisualizationEnd;
static SettingsEntry<ShowWhitespace> showWhitespace;
static SettingsEntry<int> showWhitespaceSize;
static SettingsEntry<bool> autoIndent;
static SettingsEntry<IndentStyle> indentStyle;
static SettingsEntry<bool> highlightCurrentLine;
static SettingsEntry<bool> 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 <typename T> T defaultValue(const SettingsEntry<T> &entry);
template <typename T> T get(const SettingsEntry<T> &entry);
template <typename T> void set(SettingsEntry<T> &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;
};
}

View File

@ -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); }