From 809e62e373eae6ea6c65993ddcd1e84ed10342c8 Mon Sep 17 00:00:00 2001 From: hjk Date: Tue, 17 Jan 2017 18:47:58 +0100 Subject: [PATCH] Core: Add a IOptionPage::setWidgetCreator convenience method ... to simplify the typical apply()/finish() implementation. Use the scheme for some of the debugger option pages. Change-Id: I1bcb12116d2f79ed886b5f21aafa62c2c99a3db4 Reviewed-by: Eike Ziller --- .../coreplugin/dialogs/ioptionspage.cpp | 49 +++++++++- src/plugins/coreplugin/dialogs/ioptionspage.h | 25 +++-- src/plugins/debugger/commonoptionspage.cpp | 59 ++++++------ src/plugins/debugger/commonoptionspage.h | 9 -- src/plugins/debugger/gdb/gdboptionspage.cpp | 95 +++++-------------- 5 files changed, 113 insertions(+), 124 deletions(-) diff --git a/src/plugins/coreplugin/dialogs/ioptionspage.cpp b/src/plugins/coreplugin/dialogs/ioptionspage.cpp index a8dbfbffdec..636ca3ed4ed 100644 --- a/src/plugins/coreplugin/dialogs/ioptionspage.cpp +++ b/src/plugins/coreplugin/dialogs/ioptionspage.cpp @@ -29,6 +29,7 @@ #include "ioptionspage.h" #include +#include #include #include @@ -83,28 +84,66 @@ QIcon Core::IOptionsPage::categoryIcon() const return m_categoryIcon.icon(); } +/*! + This sets a callback to create page widgets on demand. The widget will + be destroyed on \c finish. + */ +void Core::IOptionsPage::setWidgetCreator(const WidgetCreator &widgetCreator) +{ + m_widgetCreator = widgetCreator; +} + /*! \fn QWidget *IOptionsPage::widget() Returns the widget to show in the \gui Options dialog. You should create a widget lazily here, and delete it again in the finish() method. This method can be called multiple times, so you should only create a new widget if the old one was deleted. + + Alternatively, use \c setWidgetCreator to set a callback function that is used to + lazily create a widget in time. + + Either override this function in a derived class, or set a \c widgetCreator. */ -/*! - \fn void IOptionsPage::apply() +QWidget *Core::IOptionsPage::widget() +{ + QTC_ASSERT(m_widgetCreator, return nullptr); + if (!m_widget) + m_widget = m_widgetCreator(); + return m_widget; +} +/*! This is called when selecting the \gui Apply button on the options page dialog. It should detect whether any changes were made and store those. + + Either override this function in a derived class, or set a \c widgetCreator. */ +void Core::IOptionsPage::apply() +{ + QTC_ASSERT(m_widgetCreator, return); + if (m_widget) + m_widget->apply(); +} + /*! - \fn void IOptionsPage::finish() - - Is called directly before the \gui Options dialog closes. Here you should delete the widget that + This is called directly before the \gui Options dialog closes. Here you should delete the widget that was created in widget() to free resources. + + Either override this function in a derived class, or set a \c widgetCreator. */ +void Core::IOptionsPage::finish() +{ + QTC_ASSERT(m_widgetCreator, return); + if (m_widget) { + m_widget->finish(); + delete m_widget; + } +} + /*! \fn void IOptionsPage::setId(Id id) diff --git a/src/plugins/coreplugin/dialogs/ioptionspage.h b/src/plugins/coreplugin/dialogs/ioptionspage.h index 9d7b831078d..a1fb5e43f59 100644 --- a/src/plugins/coreplugin/dialogs/ioptionspage.h +++ b/src/plugins/coreplugin/dialogs/ioptionspage.h @@ -30,15 +30,21 @@ #include #include +#include #include +#include -QT_BEGIN_NAMESPACE -class QIcon; -class QWidget; -QT_END_NAMESPACE +#include namespace Core { +class CORE_EXPORT IOptionsPageWidget : public QWidget +{ +public: + virtual void apply() = 0; + virtual void finish() = 0; +}; + class CORE_EXPORT IOptionsPage : public QObject { Q_OBJECT @@ -55,10 +61,13 @@ public: QString displayCategory() const { return m_displayCategory; } QIcon categoryIcon() const; + using WidgetCreator = std::function; + void setWidgetCreator(const WidgetCreator &widgetCreator); + virtual bool matches(const QString &searchKeyWord) const; - virtual QWidget *widget() = 0; - virtual void apply() = 0; - virtual void finish() = 0; + virtual QWidget *widget(); + virtual void apply(); + virtual void finish(); protected: void setId(Id id) { m_id = id; } @@ -72,6 +81,8 @@ protected: QString m_displayName; QString m_displayCategory; Utils::Icon m_categoryIcon; + WidgetCreator m_widgetCreator; + QPointer m_widget; // Used in conjunction with m_widgetCreator mutable bool m_keywordsInitialized = false; mutable QStringList m_keywords; diff --git a/src/plugins/debugger/commonoptionspage.cpp b/src/plugins/debugger/commonoptionspage.cpp index 52007a36365..8b4ea5c7acc 100644 --- a/src/plugins/debugger/commonoptionspage.cpp +++ b/src/plugins/debugger/commonoptionspage.cpp @@ -278,31 +278,14 @@ QString CommonOptionsPage::msgSetBreakpointAtFunctionToolTip(const char *functio // /////////////////////////////////////////////////////////////////////// -LocalsAndExpressionsOptionsPage::LocalsAndExpressionsOptionsPage() +class LocalsAndExpressionsOptionsPageWidget : public IOptionsPageWidget { - setId("Z.Debugger.LocalsAndExpressions"); - //: '&&' will appear as one (one is marking keyboard shortcut) - setDisplayName(QCoreApplication::translate("Debugger", "Locals && Expressions")); - setCategory(DEBUGGER_SETTINGS_CATEGORY); -} + Q_DECLARE_TR_FUNCTIONS(Debugger::Internal::LocalsAndExpressionsOptionsPage) -void LocalsAndExpressionsOptionsPage::apply() -{ - m_group.apply(ICore::settings()); -} - -void LocalsAndExpressionsOptionsPage::finish() -{ - m_group.finish(); - delete m_widget; -} - -QWidget *LocalsAndExpressionsOptionsPage::widget() -{ - if (!m_widget) { - m_widget = new QWidget; - - auto debuggingHelperGroupBox = new QGroupBox(m_widget); +public: + LocalsAndExpressionsOptionsPageWidget() + { + auto debuggingHelperGroupBox = new QGroupBox(this); debuggingHelperGroupBox->setTitle(tr("Use Debugging Helper")); debuggingHelperGroupBox->setCheckable(true); @@ -337,23 +320,23 @@ QWidget *LocalsAndExpressionsOptionsPage::widget() auto checkBoxUseCodeModel = new QCheckBox(debuggingHelperGroupBox); auto checkBoxShowThreadNames = new QCheckBox(debuggingHelperGroupBox); - auto checkBoxShowStdNamespace = new QCheckBox(m_widget); - auto checkBoxShowQtNamespace = new QCheckBox(m_widget); - auto checkBoxShowQObjectNames = new QCheckBox(m_widget); + auto checkBoxShowStdNamespace = new QCheckBox(this); + auto checkBoxShowQtNamespace = new QCheckBox(this); + auto checkBoxShowQObjectNames = new QCheckBox(this); - auto spinBoxMaximalStringLength = new QSpinBox(m_widget); + auto spinBoxMaximalStringLength = new QSpinBox(this); spinBoxMaximalStringLength->setSpecialValueText(tr("")); spinBoxMaximalStringLength->setMaximum(10000000); spinBoxMaximalStringLength->setSingleStep(1000); spinBoxMaximalStringLength->setValue(10000); - auto spinBoxDisplayStringLimit = new QSpinBox(m_widget); + auto spinBoxDisplayStringLimit = new QSpinBox(this); spinBoxDisplayStringLimit->setSpecialValueText(tr("")); spinBoxDisplayStringLimit->setMaximum(10000); spinBoxDisplayStringLimit->setSingleStep(10); spinBoxDisplayStringLimit->setValue(100); - auto chooser = new VariableChooser(m_widget); + auto chooser = new VariableChooser(this); chooser->addSupportedWidget(textEditCustomDumperCommands); chooser->addSupportedWidget(pathChooserExtraDumperFile->lineEdit()); @@ -377,7 +360,7 @@ QWidget *LocalsAndExpressionsOptionsPage::widget() lowerLayout->addLayout(layout1); lowerLayout->addStretch(); - auto layout = new QVBoxLayout(m_widget); + auto layout = new QVBoxLayout(this); layout->addWidget(debuggingHelperGroupBox); layout->addLayout(lowerLayout); layout->addStretch(); @@ -400,7 +383,21 @@ QWidget *LocalsAndExpressionsOptionsPage::widget() m_group.insert(action(DisplayStringLimit), spinBoxDisplayStringLimit); m_group.insert(action(MaximalStringLength), spinBoxMaximalStringLength); } - return m_widget; + + void apply() { m_group.apply(ICore::settings()); } + void finish() { m_group.finish(); } + +private: + Utils::SavedActionSet m_group; +}; + +LocalsAndExpressionsOptionsPage::LocalsAndExpressionsOptionsPage() +{ + setId("Z.Debugger.LocalsAndExpressions"); + //: '&&' will appear as one (one is marking keyboard shortcut) + setDisplayName(QCoreApplication::translate("Debugger", "Locals && Expressions")); + setCategory(DEBUGGER_SETTINGS_CATEGORY); + setWidgetCreator([] { return new LocalsAndExpressionsOptionsPageWidget; }); } } // namespace Internal diff --git a/src/plugins/debugger/commonoptionspage.h b/src/plugins/debugger/commonoptionspage.h index ea2383f54cf..c1e776448d3 100644 --- a/src/plugins/debugger/commonoptionspage.h +++ b/src/plugins/debugger/commonoptionspage.h @@ -81,15 +81,6 @@ class LocalsAndExpressionsOptionsPage : public Core::IOptionsPage public: LocalsAndExpressionsOptionsPage(); - - // IOptionsPage - QWidget *widget() final; - void apply() final; - void finish() final; - -private: - QPointer m_widget; - Utils::SavedActionSet m_group; }; } // namespace Internal diff --git a/src/plugins/debugger/gdb/gdboptionspage.cpp b/src/plugins/debugger/gdb/gdboptionspage.cpp index 8bf697c2e17..164dcb5f6b8 100644 --- a/src/plugins/debugger/gdb/gdboptionspage.cpp +++ b/src/plugins/debugger/gdb/gdboptionspage.cpp @@ -27,6 +27,7 @@ #include #include #include +#include #include #include @@ -46,6 +47,7 @@ #include using namespace Core; +using namespace Utils; namespace Debugger { namespace Internal { @@ -56,26 +58,22 @@ namespace Internal { // ///////////////////////////////////////////////////////////////////////// -class GdbOptionsPageWidget : public QWidget -{ - Q_OBJECT -public: - GdbOptionsPageWidget(); - Utils::SavedActionSet group; -}; - class GdbOptionsPage : public Core::IOptionsPage { Q_OBJECT public: GdbOptionsPage(); +}; - QWidget *widget() override; - void apply() override; - void finish() override; +class GdbOptionsPageWidget : public IOptionsPageWidget +{ +public: + GdbOptionsPageWidget(); -private: - QPointer m_widget; + void apply() final { group.apply(ICore::settings()); } + void finish() final { group.finish(); } + + Utils::SavedActionSet group; }; GdbOptionsPageWidget::GdbOptionsPageWidget() @@ -269,27 +267,7 @@ GdbOptionsPage::GdbOptionsPage() setId("M.Gdb"); setDisplayName(tr("GDB")); setCategory(Constants::DEBUGGER_SETTINGS_CATEGORY); -} - -QWidget *GdbOptionsPage::widget() -{ - if (!m_widget) - m_widget = new GdbOptionsPageWidget; - return m_widget; -} - -void GdbOptionsPage::apply() -{ - if (m_widget) - m_widget->group.apply(ICore::settings()); -} - -void GdbOptionsPage::finish() -{ - if (m_widget) { - m_widget->group.finish(); - delete m_widget; - } + setWidgetCreator([] { return new GdbOptionsPageWidget; }); } ///////////////////////////////////////////////////////////////////////// @@ -298,12 +276,15 @@ void GdbOptionsPage::finish() // ///////////////////////////////////////////////////////////////////////// -class GdbOptionsPageWidget2 : public QWidget +class GdbOptionsPageWidget2 : public IOptionsPageWidget { Q_OBJECT public: GdbOptionsPageWidget2(); + void apply() final { group.apply(ICore::settings()); } + void finish() final { group.finish(); } + Utils::SavedActionSet group; }; @@ -389,45 +370,15 @@ GdbOptionsPageWidget2::GdbOptionsPageWidget2() // The "Dangerous" options. class GdbOptionsPage2 : public Core::IOptionsPage { - Q_OBJECT public: - GdbOptionsPage2(); - - QWidget *widget() override; - void apply() override; - void finish() override; - -private: - QPointer m_widget; -}; - -GdbOptionsPage2::GdbOptionsPage2() -{ - setId("M.Gdb2"); - setDisplayName(tr("GDB Extended")); - setCategory(Constants::DEBUGGER_SETTINGS_CATEGORY); -} - -QWidget *GdbOptionsPage2::widget() -{ - if (!m_widget) - m_widget = new GdbOptionsPageWidget2; - return m_widget; -} - -void GdbOptionsPage2::apply() -{ - if (m_widget) - m_widget->group.apply(ICore::settings()); -} - -void GdbOptionsPage2::finish() -{ - if (m_widget) { - m_widget->group.finish(); - delete m_widget; + GdbOptionsPage2() + { + setId("M.Gdb2"); + setDisplayName(GdbOptionsPage::tr("GDB Extended")); + setCategory(Constants::DEBUGGER_SETTINGS_CATEGORY); + setWidgetCreator([] { return new GdbOptionsPageWidget2; }); } -} +}; // Registration