1 /*
2     SPDX-FileCopyrightText: 2014 Alex Richardson <arichardson.kde@gmail.com>
3 
4     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5 */
6 
7 #include "configpage.h"
8 
9 #include <KConfigDialogManager>
10 #include <KCoreConfigSkeleton>
11 
12 namespace KDevelop {
13 
14 class ConfigPagePrivate
15 {
16 public:
ConfigPagePrivate(IPlugin * plugin)17     explicit ConfigPagePrivate(IPlugin* plugin)
18         : plugin(plugin)
19     {}
20     QScopedPointer<KConfigDialogManager> configManager;
21     KCoreConfigSkeleton* configSkeleton = nullptr;
22     IPlugin* plugin;
23 };
24 
ConfigPage(IPlugin * plugin,KCoreConfigSkeleton * config,QWidget * parent)25 ConfigPage::ConfigPage(IPlugin* plugin, KCoreConfigSkeleton* config, QWidget* parent)
26     : KTextEditor::ConfigPage(parent)
27     , d_ptr(new ConfigPagePrivate(plugin))
28 {
29     setConfigSkeleton(config);
30 }
31 
~ConfigPage()32 ConfigPage::~ConfigPage()
33 {
34 }
35 
apply()36 void ConfigPage::apply()
37 {
38     Q_D(ConfigPage);
39 
40     // if d->configManager is null, this method must be overridden
41     Q_ASSERT_X(d->configManager, metaObject()->className(),
42                "Config page does not use a KConfigSkeleton, but doesn't override apply()");
43 
44     QSignalBlocker blockSigs(this); // we don't want to emit changed() while calling apply()
45     d->configManager->updateSettings();
46     d->configSkeleton->load();
47     d->configManager->updateWidgets();
48 }
49 
defaults()50 void ConfigPage::defaults()
51 {
52     Q_D(ConfigPage);
53 
54     // if d->configManager is null, this method must be overridden
55     Q_ASSERT_X(d->configManager, metaObject()->className(),
56                "Config page does not use a KConfigSkeleton, but doesn't override defaults()");
57     d->configManager->updateWidgetsDefault();
58 }
59 
reset()60 void ConfigPage::reset()
61 {
62     Q_D(ConfigPage);
63 
64     // if d->configManager is null, this method must be overridden
65     Q_ASSERT_X(d->configManager, metaObject()->className(),
66                "Config page does not use a KConfigSkeleton, but doesn't override reset()");
67     d->configManager->updateWidgets();
68 }
69 
initConfigManager()70 void ConfigPage::initConfigManager()
71 {
72     Q_D(ConfigPage);
73 
74     if (d->configManager) {
75         d->configManager->addWidget(this);
76     }
77 }
78 
configSkeleton() const79 KCoreConfigSkeleton* ConfigPage::configSkeleton() const
80 {
81     Q_D(const ConfigPage);
82 
83     return d->configSkeleton;
84 }
85 
setConfigSkeleton(KCoreConfigSkeleton * skel)86 void ConfigPage::setConfigSkeleton(KCoreConfigSkeleton* skel)
87 {
88     Q_D(ConfigPage);
89 
90     if (d->configSkeleton == skel) {
91         return;
92     }
93     d->configSkeleton = skel;
94     if (!skel) {
95         d->configManager.reset();
96         return;
97     }
98     // create the config dialog manager if it didn't exist or recreate it.
99     // This is needed because the used config skeleton has changed
100     // and no setter for that exists in KConfigDialogManager
101     d->configManager.reset(new KConfigDialogManager(this, d->configSkeleton));
102     connect(d->configManager.data(), &KConfigDialogManager::widgetModified, this, &ConfigPage::changed);
103     // d->configManager->addWidget(this) must be called from the config dialog,
104     // since the widget tree is probably not complete when calling this function
105 }
106 
107 
childPages() const108 int ConfigPage::childPages() const
109 {
110     return 0;
111 }
112 
childPage(int number)113 ConfigPage* ConfigPage::childPage(int number)
114 {
115     Q_UNUSED(number)
116     return nullptr;
117 }
118 
plugin() const119 IPlugin* ConfigPage::plugin() const
120 {
121     Q_D(const ConfigPage);
122 
123     return d->plugin;
124 }
125 
configPageType() const126 ConfigPage::ConfigPageType ConfigPage::configPageType() const
127 {
128     return DefaultConfigPage;
129 }
130 
131 } // namespace KDevelop
132