2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
// Copyright (C) 2016 Falko Arps
|
|
|
|
|
// Copyright (C) 2016 Sven Klein
|
|
|
|
|
// Copyright (C) 2016 Giuliano Schneider
|
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0
|
2009-04-24 16:47:39 +02:00
|
|
|
|
|
|
|
|
#include "ioptionspage.h"
|
|
|
|
|
|
2021-03-24 05:44:52 +01:00
|
|
|
#include <coreplugin/icore.h>
|
|
|
|
|
|
|
|
|
|
#include <utils/aspects.h>
|
2017-01-17 18:47:58 +01:00
|
|
|
#include <utils/qtcassert.h>
|
2021-03-25 17:38:12 +01:00
|
|
|
#include <utils/stringutils.h>
|
2017-07-07 14:05:06 +03:00
|
|
|
|
2013-12-03 14:17:03 +01:00
|
|
|
#include <QCheckBox>
|
|
|
|
|
#include <QGroupBox>
|
2015-02-26 13:38:54 +01:00
|
|
|
#include <QIcon>
|
2013-12-03 14:17:03 +01:00
|
|
|
#include <QLabel>
|
|
|
|
|
#include <QPushButton>
|
2020-03-27 11:56:25 +01:00
|
|
|
#include <QRegularExpression>
|
2013-12-03 14:17:03 +01:00
|
|
|
|
2020-01-10 09:33:24 +01:00
|
|
|
using namespace Utils;
|
|
|
|
|
|
2021-03-25 17:38:12 +01:00
|
|
|
namespace Core {
|
|
|
|
|
|
2020-02-14 16:55:29 +01:00
|
|
|
/*!
|
|
|
|
|
\class Core::IOptionsPageProvider
|
|
|
|
|
\inmodule QtCreator
|
|
|
|
|
\internal
|
|
|
|
|
*/
|
|
|
|
|
/*!
|
|
|
|
|
\class Core::IOptionsPageWidget
|
|
|
|
|
\inmodule QtCreator
|
|
|
|
|
\internal
|
|
|
|
|
*/
|
|
|
|
|
|
2009-04-24 16:47:39 +02:00
|
|
|
/*!
|
2013-12-11 15:37:48 +01:00
|
|
|
\class Core::IOptionsPage
|
2020-06-12 16:04:30 +02:00
|
|
|
\inheaderfile coreplugin/dialogs/ioptionspage.h
|
2020-02-14 16:55:29 +01:00
|
|
|
\ingroup mainclasses
|
|
|
|
|
\inmodule QtCreator
|
2020-06-12 16:04:30 +02:00
|
|
|
|
2013-12-11 15:37:48 +01:00
|
|
|
\brief The IOptionsPage class is an interface for providing pages for the
|
2020-02-14 16:55:29 +01:00
|
|
|
\uicontrol Options dialog (called \uicontrol Preferences on \macos).
|
|
|
|
|
|
|
|
|
|
\image qtcreator-options-dialog.png
|
2009-04-24 16:47:39 +02:00
|
|
|
*/
|
2013-12-03 14:17:03 +01:00
|
|
|
|
2013-12-11 15:37:48 +01:00
|
|
|
/*!
|
|
|
|
|
|
2020-06-26 13:59:38 +02:00
|
|
|
\fn Utils::Id Core::IOptionsPage::id() const
|
2013-12-11 15:37:48 +01:00
|
|
|
|
|
|
|
|
Returns a unique identifier for referencing the options page.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/*!
|
2020-02-21 14:47:59 +01:00
|
|
|
\fn QString Core::IOptionsPage::displayName() const
|
2013-12-11 15:37:48 +01:00
|
|
|
|
|
|
|
|
Returns the translated display name of the options page.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/*!
|
2020-06-26 13:59:38 +02:00
|
|
|
\fn Utils::Id Core::IOptionsPage::category() const
|
2013-12-11 15:37:48 +01:00
|
|
|
|
|
|
|
|
Returns the unique id for the category that the options page should be displayed in. This id is
|
2020-02-14 16:55:29 +01:00
|
|
|
used for sorting the list on the left side of the \uicontrol Options dialog.
|
2013-12-11 15:37:48 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/*!
|
2020-02-21 14:47:59 +01:00
|
|
|
\fn QString Core::IOptionsPage::displayCategory() const
|
2013-12-11 15:37:48 +01:00
|
|
|
|
|
|
|
|
Returns the translated category name of the options page. This name is displayed in the list on
|
2020-02-14 16:55:29 +01:00
|
|
|
the left side of the \uicontrol Options dialog.
|
2013-12-11 15:37:48 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
Returns the category icon of the options page. This icon is displayed in the list on the left
|
2020-02-14 16:55:29 +01:00
|
|
|
side of the \uicontrol Options dialog.
|
2013-12-11 15:37:48 +01:00
|
|
|
*/
|
2021-03-25 17:38:12 +01:00
|
|
|
QIcon IOptionsPage::categoryIcon() const
|
2015-02-26 13:38:54 +01:00
|
|
|
{
|
2016-09-20 10:47:09 +02:00
|
|
|
return m_categoryIcon.icon();
|
2015-02-26 13:38:54 +01:00
|
|
|
}
|
|
|
|
|
|
2017-01-17 18:47:58 +01:00
|
|
|
/*!
|
2020-02-14 16:55:29 +01:00
|
|
|
Sets the \a widgetCreator callback to create page widgets on demand. The
|
|
|
|
|
widget will be destroyed on finish().
|
2017-01-17 18:47:58 +01:00
|
|
|
*/
|
2021-03-25 17:38:12 +01:00
|
|
|
void IOptionsPage::setWidgetCreator(const WidgetCreator &widgetCreator)
|
2017-01-17 18:47:58 +01:00
|
|
|
{
|
|
|
|
|
m_widgetCreator = widgetCreator;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-05 07:00:08 +02:00
|
|
|
/*!
|
|
|
|
|
\fn QStringList Core::IOptionsPage::keywords() const
|
|
|
|
|
|
|
|
|
|
Returns a list of ui strings that are used inside the widget.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
QStringList IOptionsPage::keywords() const
|
|
|
|
|
{
|
|
|
|
|
if (!m_keywordsInitialized) {
|
|
|
|
|
auto that = const_cast<IOptionsPage *>(this);
|
|
|
|
|
QWidget *widget = that->widget();
|
|
|
|
|
if (!widget)
|
|
|
|
|
return {};
|
|
|
|
|
// find common subwidgets
|
|
|
|
|
for (const QLabel *label : widget->findChildren<QLabel *>())
|
|
|
|
|
m_keywords << Utils::stripAccelerator(label->text());
|
|
|
|
|
for (const QCheckBox *checkbox : widget->findChildren<QCheckBox *>())
|
|
|
|
|
m_keywords << Utils::stripAccelerator(checkbox->text());
|
|
|
|
|
for (const QPushButton *pushButton : widget->findChildren<QPushButton *>())
|
|
|
|
|
m_keywords << Utils::stripAccelerator(pushButton->text());
|
|
|
|
|
for (const QGroupBox *groupBox : widget->findChildren<QGroupBox *>())
|
|
|
|
|
m_keywords << Utils::stripAccelerator(groupBox->title());
|
|
|
|
|
|
|
|
|
|
m_keywordsInitialized = true;
|
|
|
|
|
}
|
|
|
|
|
return m_keywords;
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-11 15:37:48 +01:00
|
|
|
/*!
|
2020-02-14 16:55:29 +01:00
|
|
|
Returns the widget to show in the \uicontrol Options dialog. You should create a widget lazily here,
|
2013-12-11 15:37:48 +01:00
|
|
|
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.
|
2017-01-17 18:47:58 +01:00
|
|
|
|
2020-02-14 16:55:29 +01:00
|
|
|
Alternatively, use setWidgetCreator() to set a callback function that is used to
|
2017-01-17 18:47:58 +01:00
|
|
|
lazily create a widget in time.
|
|
|
|
|
|
2020-02-14 16:55:29 +01:00
|
|
|
Either override this function in a derived class, or set a widget creator.
|
2013-12-11 15:37:48 +01:00
|
|
|
*/
|
|
|
|
|
|
2021-03-25 17:38:12 +01:00
|
|
|
QWidget *IOptionsPage::widget()
|
2017-01-17 18:47:58 +01:00
|
|
|
{
|
2021-03-24 05:44:52 +01:00
|
|
|
if (!m_widget) {
|
|
|
|
|
if (m_widgetCreator) {
|
|
|
|
|
m_widget = m_widgetCreator();
|
|
|
|
|
} else if (m_layouter) {
|
|
|
|
|
m_widget = new QWidget;
|
|
|
|
|
m_layouter(m_widget);
|
|
|
|
|
} else {
|
|
|
|
|
QTC_CHECK(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-01-17 18:47:58 +01:00
|
|
|
return m_widget;
|
|
|
|
|
}
|
2013-12-11 15:37:48 +01:00
|
|
|
|
2017-01-17 18:47:58 +01:00
|
|
|
/*!
|
2020-02-14 16:55:29 +01:00
|
|
|
Called when selecting the \uicontrol Apply button on the options page dialog.
|
|
|
|
|
Should detect whether any changes were made and store those.
|
|
|
|
|
|
|
|
|
|
Either override this function in a derived class, or set a widget creator.
|
2017-01-17 18:47:58 +01:00
|
|
|
|
2020-02-14 16:55:29 +01:00
|
|
|
\sa setWidgetCreator()
|
2013-12-11 15:37:48 +01:00
|
|
|
*/
|
|
|
|
|
|
2021-03-25 17:38:12 +01:00
|
|
|
void IOptionsPage::apply()
|
2017-01-17 18:47:58 +01:00
|
|
|
{
|
2021-03-24 05:44:52 +01:00
|
|
|
if (auto widget = qobject_cast<IOptionsPageWidget *>(m_widget)) {
|
|
|
|
|
widget->apply();
|
|
|
|
|
} else if (m_settings) {
|
|
|
|
|
if (m_settings->isDirty()) {
|
|
|
|
|
m_settings->apply();
|
2021-03-25 17:38:12 +01:00
|
|
|
m_settings->writeSettings(ICore::settings());
|
2021-03-24 05:44:52 +01:00
|
|
|
}
|
|
|
|
|
}
|
2017-01-17 18:47:58 +01:00
|
|
|
}
|
2013-12-11 15:37:48 +01:00
|
|
|
|
2017-01-17 18:47:58 +01:00
|
|
|
/*!
|
2020-02-14 16:55:29 +01:00
|
|
|
Called directly before the \uicontrol Options dialog closes. Here you should
|
|
|
|
|
delete the widget that was created in widget() to free resources.
|
2017-01-17 18:47:58 +01:00
|
|
|
|
2020-02-14 16:55:29 +01:00
|
|
|
Either override this function in a derived class, or set a widget creator.
|
|
|
|
|
|
|
|
|
|
\sa setWidgetCreator()
|
2013-12-11 15:37:48 +01:00
|
|
|
*/
|
|
|
|
|
|
2021-03-25 17:38:12 +01:00
|
|
|
void IOptionsPage::finish()
|
2017-01-17 18:47:58 +01:00
|
|
|
{
|
2021-03-24 05:44:52 +01:00
|
|
|
if (auto widget = qobject_cast<IOptionsPageWidget *>(m_widget))
|
|
|
|
|
widget->finish();
|
|
|
|
|
else if (m_settings)
|
|
|
|
|
m_settings->finish();
|
|
|
|
|
|
|
|
|
|
delete m_widget;
|
2017-01-17 18:47:58 +01:00
|
|
|
}
|
|
|
|
|
|
2020-02-14 16:55:29 +01:00
|
|
|
/*!
|
|
|
|
|
Sets \a categoryIconPath as the path to the category icon of the options
|
|
|
|
|
page.
|
|
|
|
|
*/
|
2021-08-11 08:44:00 +02:00
|
|
|
void IOptionsPage::setCategoryIconPath(const FilePath &categoryIconPath)
|
2020-01-10 09:33:24 +01:00
|
|
|
{
|
2021-08-11 08:44:00 +02:00
|
|
|
m_categoryIcon = Icon({{categoryIconPath, Theme::PanelTextColorDark}}, Icon::Tint);
|
2020-01-10 09:33:24 +01:00
|
|
|
}
|
|
|
|
|
|
2021-03-25 17:38:12 +01:00
|
|
|
void IOptionsPage::setSettings(AspectContainer *settings)
|
2021-03-24 05:44:52 +01:00
|
|
|
{
|
|
|
|
|
m_settings = settings;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-25 17:38:12 +01:00
|
|
|
void IOptionsPage::setLayouter(const std::function<void(QWidget *w)> &layouter)
|
2021-03-24 05:44:52 +01:00
|
|
|
{
|
|
|
|
|
m_layouter = layouter;
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-11 15:37:48 +01:00
|
|
|
/*!
|
2020-06-26 13:59:38 +02:00
|
|
|
\fn void Core::IOptionsPage::setId(Utils::Id id)
|
2013-12-11 15:37:48 +01:00
|
|
|
|
|
|
|
|
Sets the \a id of the options page.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/*!
|
2020-02-21 14:47:59 +01:00
|
|
|
\fn void Core::IOptionsPage::setDisplayName(const QString &displayName)
|
2013-12-11 15:37:48 +01:00
|
|
|
|
|
|
|
|
Sets \a displayName as the display name of the options page.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/*!
|
2020-06-26 13:59:38 +02:00
|
|
|
\fn void Core::IOptionsPage::setCategory(Utils::Id category)
|
2013-12-11 15:37:48 +01:00
|
|
|
|
|
|
|
|
Uses \a category to sort the options pages.
|
|
|
|
|
*/
|
2013-12-03 14:17:03 +01:00
|
|
|
|
2013-12-11 15:37:48 +01:00
|
|
|
/*!
|
2020-02-21 14:47:59 +01:00
|
|
|
\fn void Core::IOptionsPage::setDisplayCategory(const QString &displayCategory)
|
2013-12-11 15:37:48 +01:00
|
|
|
|
|
|
|
|
Sets \a displayCategory as the display category of the options page.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/*!
|
2020-02-21 14:47:59 +01:00
|
|
|
\fn void Core::IOptionsPage::setCategoryIcon(const Utils::Icon &categoryIcon)
|
2013-12-11 15:37:48 +01:00
|
|
|
|
|
|
|
|
Sets \a categoryIcon as the category icon of the options page.
|
|
|
|
|
*/
|
|
|
|
|
|
2021-03-25 17:38:12 +01:00
|
|
|
static QList<IOptionsPage *> g_optionsPages;
|
2013-12-11 15:37:48 +01:00
|
|
|
|
|
|
|
|
/*!
|
2018-04-10 12:28:18 +02:00
|
|
|
Constructs an options page with the given \a parent and registers it
|
2020-02-14 16:55:29 +01:00
|
|
|
at the global options page pool if \a registerGlobally is \c true.
|
2013-12-11 15:37:48 +01:00
|
|
|
*/
|
2021-03-25 17:38:12 +01:00
|
|
|
IOptionsPage::IOptionsPage(QObject *parent, bool registerGlobally)
|
2018-04-10 12:28:18 +02:00
|
|
|
: QObject(parent)
|
2013-12-03 14:17:03 +01:00
|
|
|
{
|
2018-04-10 12:28:18 +02:00
|
|
|
if (registerGlobally)
|
|
|
|
|
g_optionsPages.append(this);
|
2013-12-03 14:17:03 +01:00
|
|
|
}
|
|
|
|
|
|
2013-12-11 15:37:48 +01:00
|
|
|
/*!
|
2020-02-14 16:55:29 +01:00
|
|
|
\internal
|
2013-12-11 15:37:48 +01:00
|
|
|
*/
|
2021-03-25 17:38:12 +01:00
|
|
|
IOptionsPage::~IOptionsPage()
|
2013-12-03 14:17:03 +01:00
|
|
|
{
|
2017-12-08 17:20:48 +01:00
|
|
|
g_optionsPages.removeOne(this);
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-14 16:55:29 +01:00
|
|
|
/*!
|
|
|
|
|
Returns a list of all options pages.
|
|
|
|
|
*/
|
2021-03-25 17:38:12 +01:00
|
|
|
const QList<IOptionsPage *> IOptionsPage::allOptionsPages()
|
2017-12-08 17:20:48 +01:00
|
|
|
{
|
|
|
|
|
return g_optionsPages;
|
2013-12-03 14:17:03 +01:00
|
|
|
}
|
|
|
|
|
|
2013-12-11 15:37:48 +01:00
|
|
|
/*!
|
2020-03-27 11:56:25 +01:00
|
|
|
Is used by the \uicontrol Options dialog search filter to match \a regexp to this options
|
2013-12-11 15:37:48 +01:00
|
|
|
page. This defaults to take the widget and then looks for all child labels, check boxes, push
|
|
|
|
|
buttons, and group boxes. Should return \c true when a match is found.
|
|
|
|
|
*/
|
2021-03-25 17:38:12 +01:00
|
|
|
bool IOptionsPage::matches(const QRegularExpression ®exp) const
|
2013-12-03 14:17:03 +01:00
|
|
|
{
|
2022-10-05 07:00:08 +02:00
|
|
|
for (const QString &keyword : keywords())
|
2020-03-27 11:56:25 +01:00
|
|
|
if (keyword.contains(regexp))
|
2013-12-03 14:17:03 +01:00
|
|
|
return true;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2015-02-26 13:38:54 +01:00
|
|
|
|
2021-03-25 17:38:12 +01:00
|
|
|
static QList<IOptionsPageProvider *> g_optionsPagesProviders;
|
2017-12-08 17:20:48 +01:00
|
|
|
|
2021-03-25 17:38:12 +01:00
|
|
|
IOptionsPageProvider::IOptionsPageProvider(QObject *parent)
|
2017-12-08 17:20:48 +01:00
|
|
|
: QObject(parent)
|
|
|
|
|
{
|
|
|
|
|
g_optionsPagesProviders.append(this);
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-25 17:38:12 +01:00
|
|
|
IOptionsPageProvider::~IOptionsPageProvider()
|
2017-12-08 17:20:48 +01:00
|
|
|
{
|
|
|
|
|
g_optionsPagesProviders.removeOne(this);
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-25 17:38:12 +01:00
|
|
|
const QList<IOptionsPageProvider *> IOptionsPageProvider::allOptionsPagesProviders()
|
2017-12-08 17:20:48 +01:00
|
|
|
{
|
|
|
|
|
return g_optionsPagesProviders;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-25 17:38:12 +01:00
|
|
|
QIcon IOptionsPageProvider::categoryIcon() const
|
2015-02-26 13:38:54 +01:00
|
|
|
{
|
2018-04-23 18:02:55 +02:00
|
|
|
return m_categoryIcon.icon();
|
2015-02-26 13:38:54 +01:00
|
|
|
}
|
2021-03-25 17:38:12 +01:00
|
|
|
|
|
|
|
|
} // Core
|