KCMUtils

kcmodulecontainer.cpp
1 /*
2  This file is part of the KDE libraries
3  SPDX-FileCopyrightText: 2004 Frans Englich <[email protected]>
4 
5  SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7 
8 #include "kcmodulecontainer.h"
9 #include <kcmutils_debug.h>
10 
11 #include <QTabWidget>
12 #include <QVBoxLayout>
13 
14 #include <KService>
15 #include <kcmoduleinfo.h>
16 #include <kcmoduleproxy.h>
17 
19 
20 #if KCMUTILS_BUILD_DEPRECATED_SINCE(5, 66)
21 
22 /***********************************************************************/
23 class Q_DECL_HIDDEN KCModuleContainer::KCModuleContainerPrivate
24 {
25 public:
26  KCModuleContainerPrivate(const QStringList &mods)
27  : modules(mods)
28  {
29  }
30 
31  QStringList modules;
32  QTabWidget *tabWidget = nullptr;
33  KCModule::Buttons buttons;
34  QVBoxLayout *topLayout = nullptr;
35 
36  /**
37  * A list containing KCModuleProxy objects which
38  * have changed and must be saved.
39  */
40  ModuleList changedModules;
41 
42  /**
43  * A list of all modules which are encapsulated.
44  */
45  ModuleList allModules;
46 };
47 /***********************************************************************/
48 
49 // The KCModuleContainer is only a wrapper around real KCModules.
50 /***********************************************************************/
52  : KCModule(parent)
53  , d(new KCModuleContainerPrivate(QString(mods).remove(QLatin1Char(' ')).split(QLatin1Char(','), Qt::SkipEmptyParts)))
54 {
55  init();
56 }
57 
59  : KCModule(parent)
60  , d(new KCModuleContainerPrivate(mods))
61 {
62  init();
63 }
64 
65 void KCModuleContainer::init()
66 {
67  d->topLayout = new QVBoxLayout(this);
68  d->topLayout->setContentsMargins(0, 0, 0, 0);
69  d->topLayout->setObjectName(QStringLiteral("topLayout"));
70  d->tabWidget = new QTabWidget(this);
71  d->tabWidget->setObjectName(QStringLiteral("tabWidget"));
72  connect(d->tabWidget, &QTabWidget::currentChanged, this, &KCModuleContainer::tabSwitched);
73  d->topLayout->addWidget(d->tabWidget);
74 
75  if (!d->modules.isEmpty()) {
76  /* Add our modules */
77  for (QStringList::const_iterator it = d->modules.constBegin(), total = d->modules.constEnd(); it != total; ++it) {
78  addModule((*it));
79  }
80  }
81 }
82 
84 {
85  /* In case it doesn't exist we just silently drop it.
86  * This allows people to easily extend containers.
87  * For example, KCM monitor gamma can be in kdegraphics.
88  */
90  if (!service) {
91  // qDebug() << "KCModuleContainer: module '" <<
92  // module << "' was not found and thus not loaded" << endl;
93  return;
94  }
95 
96  if (service->noDisplay()) {
97  return;
98  }
99 
100  KCModuleProxy *proxy = new KCModuleProxy(service, d->tabWidget);
101  d->allModules.append(proxy);
102 
103  proxy->setObjectName(module);
104 
105  d->tabWidget->addTab(proxy,
106  QIcon::fromTheme(proxy->moduleInfo().icon()),
107  /* Qt eats ampersands for dinner. But not this time. */
108  proxy->moduleInfo().moduleName().replace(QLatin1Char('&'), QStringLiteral("&&")));
109 
110  d->tabWidget->setTabToolTip(d->tabWidget->indexOf(proxy), proxy->moduleInfo().comment());
111 
112  connect(proxy, qOverload<KCModuleProxy *>(&KCModuleProxy::changed), this, &KCModuleContainer::moduleChanged);
113 
114  /* Collect our buttons - we go for the common deliminator */
115  setButtons(buttons() | proxy->realModule()->buttons());
116 }
117 
118 void KCModuleContainer::tabSwitched(int index)
119 {
120  KCModuleProxy *mod = static_cast<KCModuleProxy *>(d->tabWidget->widget(index));
121  setQuickHelp(mod->quickHelp());
122  setAboutData(mod->aboutData());
123 }
124 
126 {
127  ModuleList list = d->changedModules;
128  ModuleList::iterator it;
129  for (it = list.begin(); it != list.end(); ++it) {
130  (*it)->save();
131  }
132 
133  Q_EMIT changed(false);
134 }
135 
137 {
138  ModuleList list = d->allModules;
139  ModuleList::iterator it;
140  for (it = list.begin(); it != list.end(); ++it) {
141  (*it)->load();
142  }
143 
144  Q_EMIT changed(false);
145 }
146 
148 {
149  ModuleList list = d->allModules;
150  ModuleList::iterator it;
151  for (it = list.begin(); it != list.end(); ++it) {
152  (*it)->defaults();
153  }
154 
155  Q_EMIT changed(true);
156 }
157 
158 void KCModuleContainer::moduleChanged(KCModuleProxy *proxy)
159 {
160  d->changedModules.append(proxy);
161  if (d->changedModules.isEmpty()) {
162  return;
163  }
164 
165  Q_EMIT changed(true);
166 }
167 
169 {
170  delete d;
171 }
172 
173 #endif
174 
175 /***********************************************************************/
KCModuleContainer is a convenience class encapsulating several KCModules.
void setQuickHelp(const QString &help)
Encapsulates a KCModule for embedding.
Definition: kcmoduleproxy.h:55
void load() override
Q_EMITQ_EMIT
QString comment() const
QIcon fromTheme(const QString &name)
const KAboutData * aboutData() const
void save() override
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QString quickHelp() const
void setAboutData(const KAboutData *about)
QString icon() const
bool changed() const
KCModuleContainer(QWidget *parent, const QStringList &mods)
Creates a KCModuleContainer with tabs, each one containing one of the specified modules in mods.
void currentChanged(int index)
QString moduleName() const
void addModule(const QString &module)
Adds the specified module to the tab widget.
~KCModuleContainer() override
Default destructor.
void defaults() override
QString & replace(int position, int n, QChar after)
Buttons buttons() const
void setObjectName(const QString &name)
QList::iterator begin()
void setButtons(Buttons btn)
QList::iterator end()
static Ptr serviceByDesktopName(const QString &_name)
void changed()
KCModule * realModule() const
Access to the actual module.
KCModuleInfo moduleInfo() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sat Jun 25 2022 06:17:34 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.