From 3545cfa00aacb4c487bc1c6a5c0ed48c9c6a99f6 Mon Sep 17 00:00:00 2001 From: PlutonicOverkill Date: Tue, 9 May 2017 19:50:16 +1200 Subject: [PATCH] Rewrite feature to reset options to default --- apps/opencs/CMakeLists.txt | 2 +- apps/opencs/model/prefs/boolsetting.cpp | 22 ++++++++---- apps/opencs/model/prefs/boolsetting.hpp | 5 +++ apps/opencs/model/prefs/coloursetting.cpp | 20 ++++++++--- apps/opencs/model/prefs/coloursetting.hpp | 8 +++++ apps/opencs/model/prefs/doublesetting.cpp | 24 ++++++++----- apps/opencs/model/prefs/doublesetting.hpp | 5 +++ apps/opencs/model/prefs/enumsetting.cpp | 26 ++++++++++---- apps/opencs/model/prefs/enumsetting.hpp | 5 +++ apps/opencs/model/prefs/intsetting.cpp | 22 ++++++++---- apps/opencs/model/prefs/intsetting.hpp | 5 +++ apps/opencs/model/prefs/modifiersetting.cpp | 13 +++++++ apps/opencs/model/prefs/modifiersetting.hpp | 2 ++ apps/opencs/model/prefs/setting.cpp | 4 +++ apps/opencs/model/prefs/setting.hpp | 5 +++ apps/opencs/model/prefs/shortcutsetting.cpp | 14 ++++++++ apps/opencs/model/prefs/shortcutsetting.hpp | 2 ++ apps/opencs/model/prefs/state.cpp | 35 +++++++++++++++++++ apps/opencs/model/prefs/state.hpp | 4 +++ apps/opencs/view/prefs/contextmenulist.cpp | 32 +++++++++++++++++ apps/opencs/view/prefs/contextmenulist.hpp | 30 ++++++++++++++++ apps/opencs/view/prefs/contextmenuwidget.cpp | 33 ++++++++++++++++++ apps/opencs/view/prefs/contextmenuwidget.hpp | 36 ++++++++++++++++++++ apps/opencs/view/prefs/dialogue.cpp | 17 ++++----- apps/opencs/view/prefs/dialogue.hpp | 1 - apps/opencs/view/prefs/page.cpp | 3 +- 26 files changed, 331 insertions(+), 44 deletions(-) create mode 100644 apps/opencs/view/prefs/contextmenulist.cpp create mode 100644 apps/opencs/view/prefs/contextmenulist.hpp create mode 100644 apps/opencs/view/prefs/contextmenuwidget.cpp create mode 100644 apps/opencs/view/prefs/contextmenuwidget.hpp diff --git a/apps/opencs/CMakeLists.txt b/apps/opencs/CMakeLists.txt index 8a3d3c1c85..d04259b383 100644 --- a/apps/opencs/CMakeLists.txt +++ b/apps/opencs/CMakeLists.txt @@ -110,7 +110,7 @@ opencs_units_noqt (view/tools ) opencs_units (view/prefs - dialogue pagebase page keybindingpage + dialogue pagebase page keybindingpage contextmenulist contextmenuwidget ) opencs_units (model/prefs diff --git a/apps/opencs/model/prefs/boolsetting.cpp b/apps/opencs/model/prefs/boolsetting.cpp index 6c0babaf7d..6431dc6af3 100644 --- a/apps/opencs/model/prefs/boolsetting.cpp +++ b/apps/opencs/model/prefs/boolsetting.cpp @@ -11,7 +11,7 @@ CSMPrefs::BoolSetting::BoolSetting (Category *parent, Settings::Manager *values, QMutex *mutex, const std::string& key, const std::string& label, bool default_) -: Setting (parent, values, mutex, key, label), mDefault (default_) +: Setting (parent, values, mutex, key, label), mDefault (default_), mWidget(0) {} CSMPrefs::BoolSetting& CSMPrefs::BoolSetting::setTooltip (const std::string& tooltip) @@ -22,18 +22,28 @@ CSMPrefs::BoolSetting& CSMPrefs::BoolSetting::setTooltip (const std::string& too std::pair CSMPrefs::BoolSetting::makeWidgets (QWidget *parent) { - QCheckBox *widget = new QCheckBox (QString::fromUtf8 (getLabel().c_str()), parent); - widget->setCheckState (mDefault ? Qt::Checked : Qt::Unchecked); + mWidget = new QCheckBox (QString::fromUtf8 (getLabel().c_str()), parent); + mWidget->setCheckState (mDefault ? Qt::Checked : Qt::Unchecked); if (!mTooltip.empty()) { QString tooltip = QString::fromUtf8 (mTooltip.c_str()); - widget->setToolTip (tooltip); + mWidget->setToolTip (tooltip); } - connect (widget, SIGNAL (stateChanged (int)), this, SLOT (valueChanged (int))); + connect (mWidget, SIGNAL (stateChanged (int)), this, SLOT (valueChanged (int))); - return std::make_pair (static_cast (0), widget); + return std::make_pair (static_cast (0), mWidget); +} + +void CSMPrefs::BoolSetting::updateWidget() +{ + if (mWidget) + { + mWidget->setCheckState(getValues().getBool(getKey(), getParent()->getKey()) + ? Qt::Checked + : Qt::Unchecked); + } } void CSMPrefs::BoolSetting::valueChanged (int value) diff --git a/apps/opencs/model/prefs/boolsetting.hpp b/apps/opencs/model/prefs/boolsetting.hpp index f8a321859f..37c018d140 100644 --- a/apps/opencs/model/prefs/boolsetting.hpp +++ b/apps/opencs/model/prefs/boolsetting.hpp @@ -3,6 +3,8 @@ #include "setting.hpp" +class QCheckBox; + namespace CSMPrefs { class BoolSetting : public Setting @@ -11,6 +13,7 @@ namespace CSMPrefs std::string mTooltip; bool mDefault; + QCheckBox* mWidget; public: @@ -22,6 +25,8 @@ namespace CSMPrefs /// Return label, input widget. virtual std::pair makeWidgets (QWidget *parent); + virtual void updateWidget(); + private slots: void valueChanged (int value); diff --git a/apps/opencs/model/prefs/coloursetting.cpp b/apps/opencs/model/prefs/coloursetting.cpp index d51bfad567..1a41621da2 100644 --- a/apps/opencs/model/prefs/coloursetting.cpp +++ b/apps/opencs/model/prefs/coloursetting.cpp @@ -3,6 +3,7 @@ #include #include +#include #include @@ -13,7 +14,7 @@ CSMPrefs::ColourSetting::ColourSetting (Category *parent, Settings::Manager *values, QMutex *mutex, const std::string& key, const std::string& label, QColor default_) -: Setting (parent, values, mutex, key, label), mDefault (default_) +: Setting (parent, values, mutex, key, label), mDefault (default_), mWidget(0) {} CSMPrefs::ColourSetting& CSMPrefs::ColourSetting::setTooltip (const std::string& tooltip) @@ -26,18 +27,27 @@ std::pair CSMPrefs::ColourSetting::makeWidgets (QWidget *p { QLabel *label = new QLabel (QString::fromUtf8 (getLabel().c_str()), parent); - CSVWidget::ColorEditor *widget = new CSVWidget::ColorEditor (mDefault, parent); + mWidget = new CSVWidget::ColorEditor (mDefault, parent); if (!mTooltip.empty()) { QString tooltip = QString::fromUtf8 (mTooltip.c_str()); label->setToolTip (tooltip); - widget->setToolTip (tooltip); + mWidget->setToolTip (tooltip); } - connect (widget, SIGNAL (pickingFinished()), this, SLOT (valueChanged())); + connect (mWidget, SIGNAL (pickingFinished()), this, SLOT (valueChanged())); - return std::make_pair (label, widget); + return std::make_pair (label, mWidget); +} + +void CSMPrefs::ColourSetting::updateWidget() +{ + if (mWidget) + { + mWidget->setColor(QString::fromStdString + (getValues().getString(getKey(), getParent()->getKey()))); + } } void CSMPrefs::ColourSetting::valueChanged() diff --git a/apps/opencs/model/prefs/coloursetting.hpp b/apps/opencs/model/prefs/coloursetting.hpp index be58426f2a..e892b21d65 100644 --- a/apps/opencs/model/prefs/coloursetting.hpp +++ b/apps/opencs/model/prefs/coloursetting.hpp @@ -5,6 +5,11 @@ #include +namespace CSVWidget +{ + class ColorEditor; +} + namespace CSMPrefs { class ColourSetting : public Setting @@ -13,6 +18,7 @@ namespace CSMPrefs std::string mTooltip; QColor mDefault; + CSVWidget::ColorEditor* mWidget; public: @@ -25,6 +31,8 @@ namespace CSMPrefs /// Return label, input widget. virtual std::pair makeWidgets (QWidget *parent); + virtual void updateWidget(); + private slots: void valueChanged(); diff --git a/apps/opencs/model/prefs/doublesetting.cpp b/apps/opencs/model/prefs/doublesetting.cpp index 531196174a..8ae6f4818c 100644 --- a/apps/opencs/model/prefs/doublesetting.cpp +++ b/apps/opencs/model/prefs/doublesetting.cpp @@ -16,7 +16,7 @@ CSMPrefs::DoubleSetting::DoubleSetting (Category *parent, Settings::Manager *val QMutex *mutex, const std::string& key, const std::string& label, double default_) : Setting (parent, values, mutex, key, label), mPrecision(2), mMin (0), mMax (std::numeric_limits::max()), - mDefault (default_) + mDefault (default_), mWidget(0) {} CSMPrefs::DoubleSetting& CSMPrefs::DoubleSetting::setPrecision(int precision) @@ -54,21 +54,29 @@ std::pair CSMPrefs::DoubleSetting::makeWidgets (QWidget *p { QLabel *label = new QLabel (QString::fromUtf8 (getLabel().c_str()), parent); - QDoubleSpinBox *widget = new QDoubleSpinBox (parent); - widget->setDecimals(mPrecision); - widget->setRange (mMin, mMax); - widget->setValue (mDefault); + mWidget = new QDoubleSpinBox (parent); + mWidget->setDecimals(mPrecision); + mWidget->setRange (mMin, mMax); + mWidget->setValue (mDefault); if (!mTooltip.empty()) { QString tooltip = QString::fromUtf8 (mTooltip.c_str()); label->setToolTip (tooltip); - widget->setToolTip (tooltip); + mWidget->setToolTip (tooltip); } - connect (widget, SIGNAL (valueChanged (double)), this, SLOT (valueChanged (double))); + connect (mWidget, SIGNAL (valueChanged (double)), this, SLOT (valueChanged (double))); - return std::make_pair (label, widget); + return std::make_pair (label, mWidget); +} + +void CSMPrefs::DoubleSetting::updateWidget() +{ + if (mWidget) + { + mWidget->setValue(getValues().getFloat(getKey(), getParent()->getKey())); + } } void CSMPrefs::DoubleSetting::valueChanged (double value) diff --git a/apps/opencs/model/prefs/doublesetting.hpp b/apps/opencs/model/prefs/doublesetting.hpp index 8fd345f4d0..4d297409b5 100644 --- a/apps/opencs/model/prefs/doublesetting.hpp +++ b/apps/opencs/model/prefs/doublesetting.hpp @@ -3,6 +3,8 @@ #include "setting.hpp" +class QDoubleSpinBox; + namespace CSMPrefs { class DoubleSetting : public Setting @@ -14,6 +16,7 @@ namespace CSMPrefs double mMax; std::string mTooltip; double mDefault; + QDoubleSpinBox* mWidget; public: @@ -35,6 +38,8 @@ namespace CSMPrefs /// Return label, input widget. virtual std::pair makeWidgets (QWidget *parent); + virtual void updateWidget(); + private slots: void valueChanged (double value); diff --git a/apps/opencs/model/prefs/enumsetting.cpp b/apps/opencs/model/prefs/enumsetting.cpp index e69f717ead..901bbf8e4e 100644 --- a/apps/opencs/model/prefs/enumsetting.cpp +++ b/apps/opencs/model/prefs/enumsetting.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include @@ -41,7 +42,7 @@ CSMPrefs::EnumValues& CSMPrefs::EnumValues::add (const std::string& value, const CSMPrefs::EnumSetting::EnumSetting (Category *parent, Settings::Manager *values, QMutex *mutex, const std::string& key, const std::string& label, const EnumValue& default_) -: Setting (parent, values, mutex, key, label), mDefault (default_) +: Setting (parent, values, mutex, key, label), mDefault (default_), mWidget() {} CSMPrefs::EnumSetting& CSMPrefs::EnumSetting::setTooltip (const std::string& tooltip) @@ -72,7 +73,7 @@ std::pair CSMPrefs::EnumSetting::makeWidgets (QWidget *par { QLabel *label = new QLabel (QString::fromUtf8 (getLabel().c_str()), parent); - QComboBox *widget = new QComboBox (parent); + mWidget = new QComboBox (parent); int index = 0; @@ -81,14 +82,14 @@ std::pair CSMPrefs::EnumSetting::makeWidgets (QWidget *par if (mDefault.mValue==mValues.mValues[i].mValue) index = i; - widget->addItem (QString::fromUtf8 (mValues.mValues[i].mValue.c_str())); + mWidget->addItem (QString::fromUtf8 (mValues.mValues[i].mValue.c_str())); if (!mValues.mValues[i].mTooltip.empty()) - widget->setItemData (i, QString::fromUtf8 (mValues.mValues[i].mTooltip.c_str()), + mWidget->setItemData (i, QString::fromUtf8 (mValues.mValues[i].mTooltip.c_str()), Qt::ToolTipRole); } - widget->setCurrentIndex (index); + mWidget->setCurrentIndex (index); if (!mTooltip.empty()) { @@ -96,9 +97,20 @@ std::pair CSMPrefs::EnumSetting::makeWidgets (QWidget *par label->setToolTip (tooltip); } - connect (widget, SIGNAL (currentIndexChanged (int)), this, SLOT (valueChanged (int))); + connect (mWidget, SIGNAL (currentIndexChanged (int)), this, SLOT (valueChanged (int))); - return std::make_pair (label, widget); + return std::make_pair (label, mWidget); +} + +void CSMPrefs::EnumSetting::updateWidget() +{ + if (mWidget) + { + int index = mWidget->findText(QString::fromStdString + (getValues().getString(getKey(), getParent()->getKey()))); + + mWidget->setCurrentIndex(index); + } } void CSMPrefs::EnumSetting::valueChanged (int value) diff --git a/apps/opencs/model/prefs/enumsetting.hpp b/apps/opencs/model/prefs/enumsetting.hpp index 728de3acd2..b01635cdf1 100644 --- a/apps/opencs/model/prefs/enumsetting.hpp +++ b/apps/opencs/model/prefs/enumsetting.hpp @@ -5,6 +5,8 @@ #include "setting.hpp" +class QComboBox; + namespace CSMPrefs { struct EnumValue @@ -35,6 +37,7 @@ namespace CSMPrefs std::string mTooltip; EnumValue mDefault; EnumValues mValues; + QComboBox* mWidget; public: @@ -53,6 +56,8 @@ namespace CSMPrefs /// Return label, input widget. virtual std::pair makeWidgets (QWidget *parent); + virtual void updateWidget(); + private slots: void valueChanged (int value); diff --git a/apps/opencs/model/prefs/intsetting.cpp b/apps/opencs/model/prefs/intsetting.cpp index 269a63af43..25dbf78c29 100644 --- a/apps/opencs/model/prefs/intsetting.cpp +++ b/apps/opencs/model/prefs/intsetting.cpp @@ -15,7 +15,7 @@ CSMPrefs::IntSetting::IntSetting (Category *parent, Settings::Manager *values, QMutex *mutex, const std::string& key, const std::string& label, int default_) : Setting (parent, values, mutex, key, label), mMin (0), mMax (std::numeric_limits::max()), - mDefault (default_) + mDefault (default_), mWidget(0) {} CSMPrefs::IntSetting& CSMPrefs::IntSetting::setRange (int min, int max) @@ -47,20 +47,28 @@ std::pair CSMPrefs::IntSetting::makeWidgets (QWidget *pare { QLabel *label = new QLabel (QString::fromUtf8 (getLabel().c_str()), parent); - QSpinBox *widget = new QSpinBox (parent); - widget->setRange (mMin, mMax); - widget->setValue (mDefault); + mWidget = new QSpinBox (parent); + mWidget->setRange (mMin, mMax); + mWidget->setValue (mDefault); if (!mTooltip.empty()) { QString tooltip = QString::fromUtf8 (mTooltip.c_str()); label->setToolTip (tooltip); - widget->setToolTip (tooltip); + mWidget->setToolTip (tooltip); } - connect (widget, SIGNAL (valueChanged (int)), this, SLOT (valueChanged (int))); + connect (mWidget, SIGNAL (valueChanged (int)), this, SLOT (valueChanged (int))); - return std::make_pair (label, widget); + return std::make_pair (label, mWidget); +} + +void CSMPrefs::IntSetting::updateWidget() +{ + if (mWidget) + { + mWidget->setValue(getValues().getInt(getKey(), getParent()->getKey())); + } } void CSMPrefs::IntSetting::valueChanged (int value) diff --git a/apps/opencs/model/prefs/intsetting.hpp b/apps/opencs/model/prefs/intsetting.hpp index 0ee6cf9e34..ee0989d835 100644 --- a/apps/opencs/model/prefs/intsetting.hpp +++ b/apps/opencs/model/prefs/intsetting.hpp @@ -3,6 +3,8 @@ #include "setting.hpp" +class QSpinBox; + namespace CSMPrefs { class IntSetting : public Setting @@ -13,6 +15,7 @@ namespace CSMPrefs int mMax; std::string mTooltip; int mDefault; + QSpinBox* mWidget; public: @@ -31,6 +34,8 @@ namespace CSMPrefs /// Return label, input widget. virtual std::pair makeWidgets (QWidget *parent); + virtual void updateWidget(); + private slots: void valueChanged (int value); diff --git a/apps/opencs/model/prefs/modifiersetting.cpp b/apps/opencs/model/prefs/modifiersetting.cpp index 4f4fac2484..b70d39714d 100644 --- a/apps/opencs/model/prefs/modifiersetting.cpp +++ b/apps/opencs/model/prefs/modifiersetting.cpp @@ -39,6 +39,19 @@ namespace CSMPrefs return std::make_pair(label, widget); } + void ModifierSetting::updateWidget() + { + if (mButton) + { + std::string shortcut = getValues().getString(getKey(), getParent()->getKey()); + + int modifier; + State::get().getShortcutManager().convertFromString(shortcut, modifier); + State::get().getShortcutManager().setModifier(getKey(), modifier); + resetState(); + } + } + bool ModifierSetting::eventFilter(QObject* target, QEvent* event) { if (event->type() == QEvent::KeyPress) diff --git a/apps/opencs/model/prefs/modifiersetting.hpp b/apps/opencs/model/prefs/modifiersetting.hpp index 95983f66d7..2c6a45b5a3 100644 --- a/apps/opencs/model/prefs/modifiersetting.hpp +++ b/apps/opencs/model/prefs/modifiersetting.hpp @@ -21,6 +21,8 @@ namespace CSMPrefs virtual std::pair makeWidgets(QWidget* parent); + virtual void updateWidget(); + protected: bool eventFilter(QObject* target, QEvent* event); diff --git a/apps/opencs/model/prefs/setting.cpp b/apps/opencs/model/prefs/setting.cpp index 75b58322d9..165062232a 100644 --- a/apps/opencs/model/prefs/setting.cpp +++ b/apps/opencs/model/prefs/setting.cpp @@ -30,6 +30,10 @@ std::pair CSMPrefs::Setting::makeWidgets (QWidget *parent) return std::pair (0, 0); } +void CSMPrefs::Setting::updateWidget() +{ +} + const CSMPrefs::Category *CSMPrefs::Setting::getParent() const { return mParent; diff --git a/apps/opencs/model/prefs/setting.hpp b/apps/opencs/model/prefs/setting.hpp index 00bcc638bb..7cb2d7acf6 100644 --- a/apps/opencs/model/prefs/setting.hpp +++ b/apps/opencs/model/prefs/setting.hpp @@ -47,6 +47,11 @@ namespace CSMPrefs /// widget. virtual std::pair makeWidgets (QWidget *parent); + /// Updates the widget returned by makeWidgets() to the current setting. + /// + /// \note If make_widgets() has not been called yet then nothing happens. + virtual void updateWidget(); + const Category *getParent() const; const std::string& getKey() const; diff --git a/apps/opencs/model/prefs/shortcutsetting.cpp b/apps/opencs/model/prefs/shortcutsetting.cpp index c56119deb9..418559475d 100644 --- a/apps/opencs/model/prefs/shortcutsetting.cpp +++ b/apps/opencs/model/prefs/shortcutsetting.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include "state.hpp" #include "shortcutmanager.hpp" @@ -46,6 +47,19 @@ namespace CSMPrefs return std::make_pair(label, widget); } + void ShortcutSetting::updateWidget() + { + if (mButton) + { + std::string shortcut = getValues().getString(getKey(), getParent()->getKey()); + + QKeySequence sequence; + State::get().getShortcutManager().convertFromString(shortcut, sequence); + State::get().getShortcutManager().setSequence(getKey(), sequence); + resetState(); + } + } + bool ShortcutSetting::eventFilter(QObject* target, QEvent* event) { if (event->type() == QEvent::KeyPress) diff --git a/apps/opencs/model/prefs/shortcutsetting.hpp b/apps/opencs/model/prefs/shortcutsetting.hpp index bb38b580a8..c388305a66 100644 --- a/apps/opencs/model/prefs/shortcutsetting.hpp +++ b/apps/opencs/model/prefs/shortcutsetting.hpp @@ -21,6 +21,8 @@ namespace CSMPrefs virtual std::pair makeWidgets(QWidget* parent); + virtual void updateWidget(); + protected: bool eventFilter(QObject* target, QEvent* event); diff --git a/apps/opencs/model/prefs/state.cpp b/apps/opencs/model/prefs/state.cpp index 3921e32f2b..618a4bfc95 100644 --- a/apps/opencs/model/prefs/state.cpp +++ b/apps/opencs/model/prefs/state.cpp @@ -587,6 +587,41 @@ CSMPrefs::State& CSMPrefs::State::get() return *sThis; } +void CSMPrefs::State::resetCategory(const std::string& category) +{ + for (Settings::CategorySettingValueMap::iterator i = mSettings.mUserSettings.begin(); + i != mSettings.mUserSettings.end(); ++i) + { + // if the category matches + if (i->first.first == category) + { + // mark the setting as changed + mSettings.mChangedSettings.insert(std::make_pair(i->first.first, i->first.second)); + // reset the value to the default + i->second = mSettings.mDefaultSettings[i->first]; + } + } + + Collection::iterator container = mCategories.find(category); + if (container != mCategories.end()) + { + Category settings = container->second; + for (Category::Iterator i = settings.begin(); i != settings.end(); ++i) + { + (*i)->updateWidget(); + update(**i); + } + } +} + +void CSMPrefs::State::resetAll() +{ + for (Collection::iterator iter = mCategories.begin(); iter != mCategories.end(); ++iter) + { + resetCategory(iter->first); + } +} + CSMPrefs::State& CSMPrefs::get() { diff --git a/apps/opencs/model/prefs/state.hpp b/apps/opencs/model/prefs/state.hpp index 1e46c68ee4..a32583dc51 100644 --- a/apps/opencs/model/prefs/state.hpp +++ b/apps/opencs/model/prefs/state.hpp @@ -106,6 +106,10 @@ namespace CSMPrefs static State& get(); + void resetCategory(const std::string& category); + + void resetAll(); + signals: void settingChanged (const CSMPrefs::Setting *setting); diff --git a/apps/opencs/view/prefs/contextmenulist.cpp b/apps/opencs/view/prefs/contextmenulist.cpp new file mode 100644 index 0000000000..71527d8c6a --- /dev/null +++ b/apps/opencs/view/prefs/contextmenulist.cpp @@ -0,0 +1,32 @@ +#include "contextmenulist.hpp" + +#include +#include + +#include "../../model/prefs/state.hpp" + +CSVPrefs::ContextMenuList::ContextMenuList(QWidget* parent) + :QListWidget(parent) +{ +} + +void CSVPrefs::ContextMenuList::contextMenuEvent(QContextMenuEvent* e) +{ + QMenu* menu = new QMenu(); + + menu->addAction("Reset category to default", this, SLOT(resetCategory())); + menu->addAction("Reset all to default", this, SLOT(resetAll())); + + menu->exec(e->globalPos()); + delete menu; +} + +void CSVPrefs::ContextMenuList::resetCategory() +{ + CSMPrefs::State::get().resetCategory(currentItem()->text().toStdString()); +} + +void CSVPrefs::ContextMenuList::resetAll() +{ + CSMPrefs::State::get().resetAll(); +} diff --git a/apps/opencs/view/prefs/contextmenulist.hpp b/apps/opencs/view/prefs/contextmenulist.hpp new file mode 100644 index 0000000000..222cfea6bb --- /dev/null +++ b/apps/opencs/view/prefs/contextmenulist.hpp @@ -0,0 +1,30 @@ +#ifndef CSV_PREFS_CONTEXTMENULIST_H +#define CSV_PREFS_CONTEXTMENULIST_H + +#include + +class QContextMenuEvent; + +namespace CSVPrefs +{ + class ContextMenuList : public QListWidget + { + Q_OBJECT + + public: + + ContextMenuList(QWidget* parent = 0); + + protected: + + void contextMenuEvent(QContextMenuEvent* e); + + private slots: + + void resetCategory(); + + void resetAll(); + }; +} + +#endif diff --git a/apps/opencs/view/prefs/contextmenuwidget.cpp b/apps/opencs/view/prefs/contextmenuwidget.cpp new file mode 100644 index 0000000000..abf3d6edce --- /dev/null +++ b/apps/opencs/view/prefs/contextmenuwidget.cpp @@ -0,0 +1,33 @@ +#include "contextmenuwidget.hpp" + +#include +#include + +#include "../../model/prefs/state.hpp" + +CSVPrefs::ContextMenuWidget::ContextMenuWidget(QWidget* parent, const std::string& category) + :QWidget(parent) + ,mCategory(category) +{ +} + +void CSVPrefs::ContextMenuWidget::contextMenuEvent(QContextMenuEvent* e) +{ + QMenu* menu = new QMenu(); + + menu->addAction("Reset category to default", this, SLOT(resetCategory())); + menu->addAction("Reset all to default", this, SLOT(resetAll())); + + menu->exec(e->globalPos()); + delete menu; +} + +void CSVPrefs::ContextMenuWidget::resetCategory() +{ + CSMPrefs::State::get().resetCategory(mCategory); +} + +void CSVPrefs::ContextMenuWidget::resetAll() +{ + CSMPrefs::State::get().resetAll(); +} diff --git a/apps/opencs/view/prefs/contextmenuwidget.hpp b/apps/opencs/view/prefs/contextmenuwidget.hpp new file mode 100644 index 0000000000..0d8d5006c4 --- /dev/null +++ b/apps/opencs/view/prefs/contextmenuwidget.hpp @@ -0,0 +1,36 @@ +#ifndef CSV_PREFS_CONTEXTMENUWIDGET_H +#define CSV_PREFS_CONTEXTMENUWIDGET_H + +#include + +#include + +class QContextMenuEvent; + +namespace CSVPrefs +{ + class ContextMenuWidget : public QWidget + { + Q_OBJECT + + public: + + ContextMenuWidget(QWidget* parent, const std::string& category); + + protected: + + void contextMenuEvent(QContextMenuEvent* e); + + private slots: + + void resetCategory(); + + void resetAll(); + + private: + + std::string mCategory; + }; +} + +#endif diff --git a/apps/opencs/view/prefs/dialogue.cpp b/apps/opencs/view/prefs/dialogue.cpp index 6848bcaba9..7f901c470d 100644 --- a/apps/opencs/view/prefs/dialogue.cpp +++ b/apps/opencs/view/prefs/dialogue.cpp @@ -12,16 +12,17 @@ #include "page.hpp" #include "keybindingpage.hpp" +#include "contextmenulist.hpp" void CSVPrefs::Dialogue::buildCategorySelector (QSplitter *main) { - mList = new QListWidget (main); - mList->setMinimumWidth (50); - mList->setSizePolicy (QSizePolicy::Expanding, QSizePolicy::Expanding); + CSVPrefs::ContextMenuList* list = new CSVPrefs::ContextMenuList (main); + list->setMinimumWidth (50); + list->setSizePolicy (QSizePolicy::Expanding, QSizePolicy::Expanding); - mList->setSelectionBehavior (QAbstractItemView::SelectItems); + list->setSelectionBehavior (QAbstractItemView::SelectItems); - main->addWidget (mList); + main->addWidget (list); QFontMetrics metrics (QApplication::font()); @@ -33,12 +34,12 @@ void CSVPrefs::Dialogue::buildCategorySelector (QSplitter *main) QString label = QString::fromUtf8 (iter->second.getKey().c_str()); maxWidth = std::max (maxWidth, metrics.width (label)); - mList->addItem (label); + list->addItem (label); } - mList->setMaximumWidth (maxWidth + 10); + list->setMaximumWidth (maxWidth + 10); - connect (mList, SIGNAL (currentItemChanged (QListWidgetItem *, QListWidgetItem *)), + connect (list, SIGNAL (currentItemChanged (QListWidgetItem *, QListWidgetItem *)), this, SLOT (selectionChanged (QListWidgetItem *, QListWidgetItem *))); } diff --git a/apps/opencs/view/prefs/dialogue.hpp b/apps/opencs/view/prefs/dialogue.hpp index fc66892c8b..ce017209a4 100644 --- a/apps/opencs/view/prefs/dialogue.hpp +++ b/apps/opencs/view/prefs/dialogue.hpp @@ -16,7 +16,6 @@ namespace CSVPrefs { Q_OBJECT - QListWidget *mList; QStackedWidget *mContent; private: diff --git a/apps/opencs/view/prefs/page.cpp b/apps/opencs/view/prefs/page.cpp index c23e9f64fe..c2098af443 100644 --- a/apps/opencs/view/prefs/page.cpp +++ b/apps/opencs/view/prefs/page.cpp @@ -5,11 +5,12 @@ #include "../../model/prefs/setting.hpp" #include "../../model/prefs/category.hpp" +#include "../../view/prefs/contextmenuwidget.hpp" CSVPrefs::Page::Page (CSMPrefs::Category& category, QWidget *parent) : PageBase (category, parent) { - QWidget *widget = new QWidget (parent); + CSVPrefs::ContextMenuWidget *widget = new CSVPrefs::ContextMenuWidget (parent, category.getKey()); mGrid = new QGridLayout (widget); for (CSMPrefs::Category::Iterator iter = category.begin(); iter!=category.end(); ++iter)