Integrate with QSettings.

master
Torsten Paul 2014-12-29 00:29:09 +01:00
parent 6c25d5ccb6
commit 6f9c4f7f09
6 changed files with 198 additions and 20 deletions

View File

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

View File

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

View File

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

View File

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

View File

@ -1,10 +1,32 @@
#include "settings.h"
#include "value.h"
namespace Settings {
std::list<SettingsEntryBase *> 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 <class T>
SettingsEntry<T>::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<T>::~SettingsEntry()
{
}
template <class T>
bool SettingsEntry<T>::is_default() const
{
return defaultValue == value;
}
template <class T>
std::string SettingsEntry<T>::to_string() const
{
return boost::lexical_cast<std::string>(value);
}
template <>
std::string SettingsEntry<int>::to_string() const
{
return boost::lexical_cast<std::string>(value);
}
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;
}
}
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;
}
}
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<ShowWhitespaces>::from_string(std::string val)
{
try {
value = (ShowWhitespaces)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);
@ -46,6 +136,13 @@ Settings::~Settings()
{
}
void Settings::visit(class Visitor *visitor)
{
for (std::list<SettingsEntryBase *>::iterator it = SettingsEntryBase::entries.begin();it != SettingsEntryBase::entries.end();it++) {
visitor->handle(*it);
}
}
template <typename T>
T Settings::defaultValue(const SettingsEntry<T> &entry)
{
@ -88,4 +185,12 @@ template ShowWhitespaces Settings::defaultValue(const SettingsEntry<ShowWhitespa
template ShowWhitespaces Settings::get(const SettingsEntry<ShowWhitespaces>&);
template void Settings::set(SettingsEntry<ShowWhitespaces>&, ShowWhitespaces);
}
Visitor::Visitor()
{
}
Visitor::~Visitor()
{
}
}

View File

@ -1,6 +1,7 @@
#pragma once
#include <map>
#include <list>
#include <string>
namespace Settings {
@ -30,17 +31,42 @@ typedef enum {
SHOW_WHITESPACES_AFTER_INDENTATION,
} ShowWhitespaces;
template <class T>
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<SettingsEntryBase *> entries;
friend class Settings;
};
template <class T>
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<bool> indentationsUseTabs;
static Settings *inst(bool erase = false);
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);
@ -72,4 +100,13 @@ private:
virtual ~Settings();
};
class Visitor
{
public:
Visitor();
virtual ~Visitor();
virtual void handle(SettingsEntryBase * entry) const = 0;
};
}