From 73ffdd5ac53676835d46f65be04436f97839c9c3 Mon Sep 17 00:00:00 2001 From: Marc Zinnschlag Date: Fri, 11 Dec 2015 11:15:14 +0100 Subject: [PATCH] added colour settings --- apps/opencs/CMakeLists.txt | 2 +- apps/opencs/model/prefs/coloursetting.cpp | 49 +++++++++++++++++++++++ apps/opencs/model/prefs/coloursetting.hpp | 33 +++++++++++++++ apps/opencs/model/prefs/state.cpp | 28 ++++++++++++- apps/opencs/model/prefs/state.hpp | 5 +++ 5 files changed, 114 insertions(+), 3 deletions(-) create mode 100644 apps/opencs/model/prefs/coloursetting.cpp create mode 100644 apps/opencs/model/prefs/coloursetting.hpp diff --git a/apps/opencs/CMakeLists.txt b/apps/opencs/CMakeLists.txt index b51cfb52f..e76b5ce77 100644 --- a/apps/opencs/CMakeLists.txt +++ b/apps/opencs/CMakeLists.txt @@ -138,7 +138,7 @@ opencs_hdrs_noqt (model/settings ) opencs_units (model/prefs - state setting intsetting doublesetting boolsetting enumsetting + state setting intsetting doublesetting boolsetting enumsetting coloursetting ) opencs_units_noqt (model/prefs diff --git a/apps/opencs/model/prefs/coloursetting.cpp b/apps/opencs/model/prefs/coloursetting.cpp new file mode 100644 index 000000000..a56485292 --- /dev/null +++ b/apps/opencs/model/prefs/coloursetting.cpp @@ -0,0 +1,49 @@ + +#include "coloursetting.hpp" + +#include + +#include + +#include + +#include "../../view/widget/coloreditor.hpp" + +#include "category.hpp" +#include "state.hpp" + +CSMPrefs::ColourSetting::ColourSetting (Category *parent, Settings::Manager *values, + const std::string& key, const std::string& label, QColor default_) +: Setting (parent, values, key, label), mDefault (default_) +{} + +CSMPrefs::ColourSetting& CSMPrefs::ColourSetting::setTooltip (const std::string& tooltip) +{ + mTooltip = tooltip; + return *this; +} + +std::pair CSMPrefs::ColourSetting::makeWidgets (QWidget *parent) +{ + QLabel *label = new QLabel (QString::fromUtf8 (getLabel().c_str()), parent); + + CSVWidget::ColorEditor *widget = new CSVWidget::ColorEditor (mDefault, parent); + + if (!mTooltip.empty()) + { + QString tooltip = QString::fromUtf8 (mTooltip.c_str()); + label->setToolTip (tooltip); + widget->setToolTip (tooltip); + } + + connect (widget, SIGNAL (pickingFinished()), this, SLOT (valueChanged())); + + return std::make_pair (label, widget); +} + +void CSMPrefs::ColourSetting::valueChanged() +{ + CSVWidget::ColorEditor& widget = dynamic_cast (*sender()); + getValues().setString (getKey(), getParent()->getKey(), widget.color().name().toUtf8().data()); + getParent()->getState()->update (*this); +} diff --git a/apps/opencs/model/prefs/coloursetting.hpp b/apps/opencs/model/prefs/coloursetting.hpp new file mode 100644 index 000000000..fed2adc0a --- /dev/null +++ b/apps/opencs/model/prefs/coloursetting.hpp @@ -0,0 +1,33 @@ +#ifndef CSM_PREFS_COLOURSETTING_H +#define CSM_PREFS_COLOURSETTING_H + +#include "setting.hpp" + +#include + +namespace CSMPrefs +{ + class ColourSetting : public Setting + { + Q_OBJECT + + std::string mTooltip; + QColor mDefault; + + public: + + ColourSetting (Category *parent, Settings::Manager *values, + const std::string& key, const std::string& label, QColor default_); + + ColourSetting& setTooltip (const std::string& tooltip); + + /// Return label, input widget. + virtual std::pair makeWidgets (QWidget *parent); + + private slots: + + void valueChanged(); + }; +} + +#endif diff --git a/apps/opencs/model/prefs/state.cpp b/apps/opencs/model/prefs/state.cpp index 99449309c..41956a5a9 100644 --- a/apps/opencs/model/prefs/state.cpp +++ b/apps/opencs/model/prefs/state.cpp @@ -8,6 +8,7 @@ #include "intsetting.hpp" #include "doublesetting.hpp" #include "boolsetting.hpp" +#include "coloursetting.hpp" CSMPrefs::State *CSMPrefs::State::sThis = 0; @@ -141,8 +142,13 @@ void CSMPrefs::State::declare() setRange (0, 10000); declareInt ("error-height", "Initial height of the error panel", 100). setRange (100, 10000); - // syntax-colouring - + declareColour ("colour-int", "Highlight Colour: Integer Literals", QColor ("darkmagenta")); + declareColour ("colour-float", "Highlight Colour: Float Literals", QColor ("magenta")); + declareColour ("colour-name", "Highlight Colour: Names", QColor ("grey")); + declareColour ("colour-keyword", "Highlight Colour: Keywords", QColor ("red")); + declareColour ("colour-special", "Highlight Colour: Special Characters", QColor ("darkorange")); + declareColour ("colour-comment", "Highlight Colour: Comments", QColor ("green")); + declareColour ("colour-id", "Highlight Colour: IDs", QColor ("blue")); declareCategory ("General Input"); declareBool ("cycle", "Cyclic next/previous", false). setTooltip ("When using next/previous functions at the last/first item of a " @@ -271,6 +277,24 @@ CSMPrefs::EnumSetting& CSMPrefs::State::declareEnum (const std::string& key, return *setting; } +CSMPrefs::ColourSetting& CSMPrefs::State::declareColour (const std::string& key, + const std::string& label, QColor default_) +{ + if (mCurrentCategory==mCategories.end()) + throw std::logic_error ("no category for setting"); + + setDefault (key, default_.name().toUtf8().data()); + + default_.setNamedColor (QString::fromUtf8 (mSettings.getString (key, mCurrentCategory->second.getKey()).c_str())); + + CSMPrefs::ColourSetting *setting = + new CSMPrefs::ColourSetting (&mCurrentCategory->second, &mSettings, key, label, default_); + + mCurrentCategory->second.addSetting (setting); + + return *setting; +} + void CSMPrefs::State::setDefault (const std::string& key, const std::string& default_) { Settings::CategorySetting fullKey (mCurrentCategory->second.getKey(), key); diff --git a/apps/opencs/model/prefs/state.hpp b/apps/opencs/model/prefs/state.hpp index e4de7fcff..1e4ada5b4 100644 --- a/apps/opencs/model/prefs/state.hpp +++ b/apps/opencs/model/prefs/state.hpp @@ -16,11 +16,14 @@ #include "setting.hpp" #include "enumsetting.hpp" +class QColor; + namespace CSMPrefs { class IntSetting; class DoubleSetting; class BoolSetting; + class ColourSetting; class State : public QObject { @@ -60,6 +63,8 @@ namespace CSMPrefs EnumSetting& declareEnum (const std::string& key, const std::string& label, EnumValue default_); + ColourSetting& declareColour (const std::string& key, const std::string& label, QColor default_); + void setDefault (const std::string& key, const std::string& default_); public: