File indexing completed on 2024-04-28 04:36:29

0001 /*
0002     SPDX-FileCopyrightText: 2014 Alex Richardson <arichardson.kde@gmail.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 
0007 #include "configpage.h"
0008 
0009 #include <KConfigDialogManager>
0010 #include <KCoreConfigSkeleton>
0011 
0012 namespace KDevelop {
0013 
0014 class ConfigPagePrivate
0015 {
0016 public:
0017     explicit ConfigPagePrivate(IPlugin* plugin)
0018         : plugin(plugin)
0019     {}
0020     QScopedPointer<KConfigDialogManager> configManager;
0021     KCoreConfigSkeleton* configSkeleton = nullptr;
0022     IPlugin* plugin;
0023 };
0024 
0025 ConfigPage::ConfigPage(IPlugin* plugin, KCoreConfigSkeleton* config, QWidget* parent)
0026     : KTextEditor::ConfigPage(parent)
0027     , d_ptr(new ConfigPagePrivate(plugin))
0028 {
0029     setConfigSkeleton(config);
0030 }
0031 
0032 ConfigPage::~ConfigPage()
0033 {
0034 }
0035 
0036 void ConfigPage::apply()
0037 {
0038     Q_D(ConfigPage);
0039 
0040     // if d->configManager is null, this method must be overridden
0041     Q_ASSERT_X(d->configManager, metaObject()->className(),
0042                "Config page does not use a KConfigSkeleton, but doesn't override apply()");
0043 
0044     QSignalBlocker blockSigs(this); // we don't want to emit changed() while calling apply()
0045     d->configManager->updateSettings();
0046     d->configSkeleton->load();
0047     d->configManager->updateWidgets();
0048 }
0049 
0050 void ConfigPage::defaults()
0051 {
0052     Q_D(ConfigPage);
0053 
0054     // if d->configManager is null, this method must be overridden
0055     Q_ASSERT_X(d->configManager, metaObject()->className(),
0056                "Config page does not use a KConfigSkeleton, but doesn't override defaults()");
0057     d->configManager->updateWidgetsDefault();
0058 }
0059 
0060 void ConfigPage::reset()
0061 {
0062     Q_D(ConfigPage);
0063 
0064     // if d->configManager is null, this method must be overridden
0065     Q_ASSERT_X(d->configManager, metaObject()->className(),
0066                "Config page does not use a KConfigSkeleton, but doesn't override reset()");
0067     d->configManager->updateWidgets();
0068 }
0069 
0070 void ConfigPage::initConfigManager()
0071 {
0072     Q_D(ConfigPage);
0073 
0074     if (d->configManager) {
0075         d->configManager->addWidget(this);
0076     }
0077 
0078     if (needsResetDuringInitialization()) {
0079         reset();
0080     }
0081 }
0082 
0083 bool ConfigPage::needsResetDuringInitialization() const
0084 {
0085     // Potentially slow but safe default: makes sure all widgets are in the correct state.
0086     // Many Kate classes derived from KTextEditor::ConfigPage call reset() in their constructors.
0087     // TODO: update KDevelop classes derived from KTextEditor::ConfigPage one by one as follows:
0088     // 1) override needsResetDuringInitialization() to return false;
0089     // 2) call reset() in constructor(s) if needed.
0090     // Once all KDevelop and KDevelop plugin ConfigPage-derived classes return false from
0091     // needsResetDuringInitialization() (verify by making this function pure virtual), remove
0092     // needsResetDuringInitialization() and its overrides, never call reset() in initConfigManager().
0093     return true;
0094 }
0095 
0096 KCoreConfigSkeleton* ConfigPage::configSkeleton() const
0097 {
0098     Q_D(const ConfigPage);
0099 
0100     return d->configSkeleton;
0101 }
0102 
0103 void ConfigPage::setConfigSkeleton(KCoreConfigSkeleton* skel)
0104 {
0105     Q_D(ConfigPage);
0106 
0107     if (d->configSkeleton == skel) {
0108         return;
0109     }
0110     d->configSkeleton = skel;
0111     if (!skel) {
0112         d->configManager.reset();
0113         return;
0114     }
0115     // create the config dialog manager if it didn't exist or recreate it.
0116     // This is needed because the used config skeleton has changed
0117     // and no setter for that exists in KConfigDialogManager
0118     d->configManager.reset(new KConfigDialogManager(this, d->configSkeleton));
0119     connect(d->configManager.data(), &KConfigDialogManager::widgetModified, this, &ConfigPage::changed);
0120     // d->configManager->addWidget(this) must be called from the config dialog,
0121     // since the widget tree is probably not complete when calling this function
0122 }
0123 
0124 
0125 int ConfigPage::childPages() const
0126 {
0127     return 0;
0128 }
0129 
0130 ConfigPage* ConfigPage::childPage(int number)
0131 {
0132     Q_UNUSED(number)
0133     return nullptr;
0134 }
0135 
0136 IPlugin* ConfigPage::plugin() const
0137 {
0138     Q_D(const ConfigPage);
0139 
0140     return d->plugin;
0141 }
0142 
0143 ConfigPage::ConfigPageType ConfigPage::configPageType() const
0144 {
0145     return DefaultConfigPage;
0146 }
0147 
0148 } // namespace KDevelop
0149 
0150 #include "moc_configpage.cpp"