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 <eike.ziller@qt.io>
This commit is contained in:
hjk
2017-01-17 18:47:58 +01:00
parent 804aec1417
commit 809e62e373
5 changed files with 113 additions and 124 deletions

View File

@@ -29,6 +29,7 @@
#include "ioptionspage.h" #include "ioptionspage.h"
#include <utils/stringutils.h> #include <utils/stringutils.h>
#include <utils/qtcassert.h>
#include <QCheckBox> #include <QCheckBox>
#include <QGroupBox> #include <QGroupBox>
@@ -83,28 +84,66 @@ QIcon Core::IOptionsPage::categoryIcon() const
return m_categoryIcon.icon(); 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() \fn QWidget *IOptionsPage::widget()
Returns the widget to show in the \gui Options dialog. You should create a widget lazily here, 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 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. 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.
*/ */
/*! QWidget *Core::IOptionsPage::widget()
\fn void IOptionsPage::apply() {
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 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. 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() This is called directly before the \gui Options dialog closes. Here you should delete the widget that
Is called directly before the \gui Options dialog closes. Here you should delete the widget that
was created in widget() to free resources. 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) \fn void IOptionsPage::setId(Id id)

View File

@@ -30,15 +30,21 @@
#include <utils/icon.h> #include <utils/icon.h>
#include <QObject> #include <QObject>
#include <QPointer>
#include <QStringList> #include <QStringList>
#include <QWidget>
QT_BEGIN_NAMESPACE #include <functional>
class QIcon;
class QWidget;
QT_END_NAMESPACE
namespace Core { namespace Core {
class CORE_EXPORT IOptionsPageWidget : public QWidget
{
public:
virtual void apply() = 0;
virtual void finish() = 0;
};
class CORE_EXPORT IOptionsPage : public QObject class CORE_EXPORT IOptionsPage : public QObject
{ {
Q_OBJECT Q_OBJECT
@@ -55,10 +61,13 @@ public:
QString displayCategory() const { return m_displayCategory; } QString displayCategory() const { return m_displayCategory; }
QIcon categoryIcon() const; QIcon categoryIcon() const;
using WidgetCreator = std::function<IOptionsPageWidget *()>;
void setWidgetCreator(const WidgetCreator &widgetCreator);
virtual bool matches(const QString &searchKeyWord) const; virtual bool matches(const QString &searchKeyWord) const;
virtual QWidget *widget() = 0; virtual QWidget *widget();
virtual void apply() = 0; virtual void apply();
virtual void finish() = 0; virtual void finish();
protected: protected:
void setId(Id id) { m_id = id; } void setId(Id id) { m_id = id; }
@@ -72,6 +81,8 @@ protected:
QString m_displayName; QString m_displayName;
QString m_displayCategory; QString m_displayCategory;
Utils::Icon m_categoryIcon; Utils::Icon m_categoryIcon;
WidgetCreator m_widgetCreator;
QPointer<IOptionsPageWidget> m_widget; // Used in conjunction with m_widgetCreator
mutable bool m_keywordsInitialized = false; mutable bool m_keywordsInitialized = false;
mutable QStringList m_keywords; mutable QStringList m_keywords;

View File

@@ -278,31 +278,14 @@ QString CommonOptionsPage::msgSetBreakpointAtFunctionToolTip(const char *functio
// //
/////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////
LocalsAndExpressionsOptionsPage::LocalsAndExpressionsOptionsPage() class LocalsAndExpressionsOptionsPageWidget : public IOptionsPageWidget
{ {
setId("Z.Debugger.LocalsAndExpressions"); Q_DECLARE_TR_FUNCTIONS(Debugger::Internal::LocalsAndExpressionsOptionsPage)
//: '&&' will appear as one (one is marking keyboard shortcut)
setDisplayName(QCoreApplication::translate("Debugger", "Locals && Expressions"));
setCategory(DEBUGGER_SETTINGS_CATEGORY);
}
void LocalsAndExpressionsOptionsPage::apply() public:
LocalsAndExpressionsOptionsPageWidget()
{ {
m_group.apply(ICore::settings()); auto debuggingHelperGroupBox = new QGroupBox(this);
}
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);
debuggingHelperGroupBox->setTitle(tr("Use Debugging Helper")); debuggingHelperGroupBox->setTitle(tr("Use Debugging Helper"));
debuggingHelperGroupBox->setCheckable(true); debuggingHelperGroupBox->setCheckable(true);
@@ -337,23 +320,23 @@ QWidget *LocalsAndExpressionsOptionsPage::widget()
auto checkBoxUseCodeModel = new QCheckBox(debuggingHelperGroupBox); auto checkBoxUseCodeModel = new QCheckBox(debuggingHelperGroupBox);
auto checkBoxShowThreadNames = new QCheckBox(debuggingHelperGroupBox); auto checkBoxShowThreadNames = new QCheckBox(debuggingHelperGroupBox);
auto checkBoxShowStdNamespace = new QCheckBox(m_widget); auto checkBoxShowStdNamespace = new QCheckBox(this);
auto checkBoxShowQtNamespace = new QCheckBox(m_widget); auto checkBoxShowQtNamespace = new QCheckBox(this);
auto checkBoxShowQObjectNames = new QCheckBox(m_widget); auto checkBoxShowQObjectNames = new QCheckBox(this);
auto spinBoxMaximalStringLength = new QSpinBox(m_widget); auto spinBoxMaximalStringLength = new QSpinBox(this);
spinBoxMaximalStringLength->setSpecialValueText(tr("<unlimited>")); spinBoxMaximalStringLength->setSpecialValueText(tr("<unlimited>"));
spinBoxMaximalStringLength->setMaximum(10000000); spinBoxMaximalStringLength->setMaximum(10000000);
spinBoxMaximalStringLength->setSingleStep(1000); spinBoxMaximalStringLength->setSingleStep(1000);
spinBoxMaximalStringLength->setValue(10000); spinBoxMaximalStringLength->setValue(10000);
auto spinBoxDisplayStringLimit = new QSpinBox(m_widget); auto spinBoxDisplayStringLimit = new QSpinBox(this);
spinBoxDisplayStringLimit->setSpecialValueText(tr("<unlimited>")); spinBoxDisplayStringLimit->setSpecialValueText(tr("<unlimited>"));
spinBoxDisplayStringLimit->setMaximum(10000); spinBoxDisplayStringLimit->setMaximum(10000);
spinBoxDisplayStringLimit->setSingleStep(10); spinBoxDisplayStringLimit->setSingleStep(10);
spinBoxDisplayStringLimit->setValue(100); spinBoxDisplayStringLimit->setValue(100);
auto chooser = new VariableChooser(m_widget); auto chooser = new VariableChooser(this);
chooser->addSupportedWidget(textEditCustomDumperCommands); chooser->addSupportedWidget(textEditCustomDumperCommands);
chooser->addSupportedWidget(pathChooserExtraDumperFile->lineEdit()); chooser->addSupportedWidget(pathChooserExtraDumperFile->lineEdit());
@@ -377,7 +360,7 @@ QWidget *LocalsAndExpressionsOptionsPage::widget()
lowerLayout->addLayout(layout1); lowerLayout->addLayout(layout1);
lowerLayout->addStretch(); lowerLayout->addStretch();
auto layout = new QVBoxLayout(m_widget); auto layout = new QVBoxLayout(this);
layout->addWidget(debuggingHelperGroupBox); layout->addWidget(debuggingHelperGroupBox);
layout->addLayout(lowerLayout); layout->addLayout(lowerLayout);
layout->addStretch(); layout->addStretch();
@@ -400,7 +383,21 @@ QWidget *LocalsAndExpressionsOptionsPage::widget()
m_group.insert(action(DisplayStringLimit), spinBoxDisplayStringLimit); m_group.insert(action(DisplayStringLimit), spinBoxDisplayStringLimit);
m_group.insert(action(MaximalStringLength), spinBoxMaximalStringLength); 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 } // namespace Internal

View File

@@ -81,15 +81,6 @@ class LocalsAndExpressionsOptionsPage : public Core::IOptionsPage
public: public:
LocalsAndExpressionsOptionsPage(); LocalsAndExpressionsOptionsPage();
// IOptionsPage
QWidget *widget() final;
void apply() final;
void finish() final;
private:
QPointer<QWidget> m_widget;
Utils::SavedActionSet m_group;
}; };
} // namespace Internal } // namespace Internal

View File

@@ -27,6 +27,7 @@
#include <debugger/debuggeractions.h> #include <debugger/debuggeractions.h>
#include <debugger/debuggercore.h> #include <debugger/debuggercore.h>
#include <debugger/debuggerinternalconstants.h> #include <debugger/debuggerinternalconstants.h>
#include <debugger/debuggerconstants.h>
#include <coreplugin/dialogs/ioptionspage.h> #include <coreplugin/dialogs/ioptionspage.h>
#include <coreplugin/icore.h> #include <coreplugin/icore.h>
@@ -46,6 +47,7 @@
#include <QTextEdit> #include <QTextEdit>
using namespace Core; using namespace Core;
using namespace Utils;
namespace Debugger { namespace Debugger {
namespace Internal { namespace Internal {
@@ -56,26 +58,22 @@ namespace Internal {
// //
///////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////
class GdbOptionsPageWidget : public QWidget
{
Q_OBJECT
public:
GdbOptionsPageWidget();
Utils::SavedActionSet group;
};
class GdbOptionsPage : public Core::IOptionsPage class GdbOptionsPage : public Core::IOptionsPage
{ {
Q_OBJECT Q_OBJECT
public: public:
GdbOptionsPage(); GdbOptionsPage();
};
QWidget *widget() override; class GdbOptionsPageWidget : public IOptionsPageWidget
void apply() override; {
void finish() override; public:
GdbOptionsPageWidget();
private: void apply() final { group.apply(ICore::settings()); }
QPointer<GdbOptionsPageWidget> m_widget; void finish() final { group.finish(); }
Utils::SavedActionSet group;
}; };
GdbOptionsPageWidget::GdbOptionsPageWidget() GdbOptionsPageWidget::GdbOptionsPageWidget()
@@ -269,27 +267,7 @@ GdbOptionsPage::GdbOptionsPage()
setId("M.Gdb"); setId("M.Gdb");
setDisplayName(tr("GDB")); setDisplayName(tr("GDB"));
setCategory(Constants::DEBUGGER_SETTINGS_CATEGORY); setCategory(Constants::DEBUGGER_SETTINGS_CATEGORY);
} setWidgetCreator([] { return new GdbOptionsPageWidget; });
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;
}
} }
///////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////
@@ -298,12 +276,15 @@ void GdbOptionsPage::finish()
// //
///////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////
class GdbOptionsPageWidget2 : public QWidget class GdbOptionsPageWidget2 : public IOptionsPageWidget
{ {
Q_OBJECT Q_OBJECT
public: public:
GdbOptionsPageWidget2(); GdbOptionsPageWidget2();
void apply() final { group.apply(ICore::settings()); }
void finish() final { group.finish(); }
Utils::SavedActionSet group; Utils::SavedActionSet group;
}; };
@@ -389,45 +370,15 @@ GdbOptionsPageWidget2::GdbOptionsPageWidget2()
// The "Dangerous" options. // The "Dangerous" options.
class GdbOptionsPage2 : public Core::IOptionsPage class GdbOptionsPage2 : public Core::IOptionsPage
{ {
Q_OBJECT
public: public:
GdbOptionsPage2(); GdbOptionsPage2()
QWidget *widget() override;
void apply() override;
void finish() override;
private:
QPointer<GdbOptionsPageWidget2> m_widget;
};
GdbOptionsPage2::GdbOptionsPage2()
{ {
setId("M.Gdb2"); setId("M.Gdb2");
setDisplayName(tr("GDB Extended")); setDisplayName(GdbOptionsPage::tr("GDB Extended"));
setCategory(Constants::DEBUGGER_SETTINGS_CATEGORY); setCategory(Constants::DEBUGGER_SETTINGS_CATEGORY);
setWidgetCreator([] { return new GdbOptionsPageWidget2; });
} }
};
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;
}
}
// Registration // Registration