• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

KDEUI

  • sources
  • kde-4.12
  • kdelibs
  • kdeui
  • dialogs
kconfigdialog.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of the KDE libraries
3  * Copyright (C) 2003 Benjamin C Meyer (ben+kdelibs at meyerhome dot net)
4  * Copyright (C) 2003 Waldo Bastian <bastian@kde.org>
5  * Copyright (C) 2004 Michael Brade <brade@kde.org>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public License
18  * along with this library; see the file COPYING.LIB. If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  */
22 #include "kconfigdialog.h"
23 
24 #include <kcomponentdata.h>
25 #include <kconfigdialogmanager.h>
26 #include <kconfigskeleton.h>
27 #include <kdebug.h>
28 #include <kicon.h>
29 #include <kiconloader.h>
30 #include <klocale.h>
31 #include <kpagewidgetmodel.h>
32 #include <kvbox.h>
33 
34 #include <QtGui/QLayout>
35 #include <QtCore/QMap>
36 
37 class KConfigDialog::KConfigDialogPrivate
38 {
39 public:
40  KConfigDialogPrivate(KConfigDialog *q, const QString& name, KCoreConfigSkeleton *config)
41  : q(q), shown(false), manager(0)
42  {
43  q->setCaption( i18n("Configure") );
44  q->setFaceType( List );
45  q->setButtons( Default|Ok|Apply|Cancel|Help );
46  q->setHelp( QString(), KGlobal::mainComponent().componentName() );
47  q->setDefaultButton( Ok );
48  q->setObjectName( name );
49 
50  if ( !name.isEmpty() ) {
51  openDialogs.insert(name, q);
52  } else {
53  QString genericName;
54  genericName.sprintf("SettingsDialog-%p", static_cast<void*>(q));
55  openDialogs.insert(genericName, q);
56  q->setObjectName(genericName);
57  }
58 
59  connect(q, SIGNAL(okClicked()), q, SLOT(updateSettings()));
60  connect(q, SIGNAL(applyClicked()), q, SLOT(updateSettings()));
61  connect(q, SIGNAL(applyClicked()), q, SLOT(_k_updateButtons()));
62  connect(q, SIGNAL(cancelClicked()), q, SLOT(updateWidgets()));
63  connect(q, SIGNAL(defaultClicked()), q, SLOT(updateWidgetsDefault()));
64  connect(q, SIGNAL(defaultClicked()), q, SLOT(_k_updateButtons()));
65  connect(q, SIGNAL(pageRemoved(KPageWidgetItem*)), q, SLOT(onPageRemoved(KPageWidgetItem*)));
66 
67  manager = new KConfigDialogManager(q, config);
68  setupManagerConnections(manager);
69 
70  q->enableButton(Apply, false);
71  }
72 
73  KPageWidgetItem* addPageInternal(QWidget *page, const QString &itemName,
74  const QString &pixmapName, const QString &header);
75 
76  void setupManagerConnections(KConfigDialogManager *manager);
77 
78  void _k_updateButtons();
79  void _k_settingsChangedSlot();
80 
81  KConfigDialog *q;
82  bool shown;
83  KConfigDialogManager *manager;
84  QMap<QWidget *, KConfigDialogManager *> managerForPage;
85 
89  static QHash<QString,KConfigDialog *> openDialogs;
90 };
91 
92 QHash<QString,KConfigDialog *> KConfigDialog::KConfigDialogPrivate::openDialogs;
93 
94 KConfigDialog::KConfigDialog( QWidget *parent, const QString& name,
95  KConfigSkeleton *config ) :
96  KPageDialog( parent ),
97  d(new KConfigDialogPrivate(this, name, config))
98 {
99 }
100 
101 KConfigDialog::KConfigDialog( QWidget *parent, const QString& name,
102  KCoreConfigSkeleton *config ) :
103  KPageDialog( parent ),
104  d(new KConfigDialogPrivate(this, name, config))
105 {
106 }
107 
108 KConfigDialog::~KConfigDialog()
109 {
110  KConfigDialogPrivate::openDialogs.remove(objectName());
111  delete d;
112 }
113 
114 KPageWidgetItem* KConfigDialog::addPage(QWidget *page,
115  const QString &itemName,
116  const QString &pixmapName,
117  const QString &header,
118  bool manage)
119 {
120  Q_ASSERT(page);
121  if (!page) {
122  return 0;
123  }
124 
125  KPageWidgetItem* item = d->addPageInternal(page, itemName, pixmapName, header);
126  if (manage) {
127  d->manager->addWidget(page);
128  }
129 
130  if (d->shown && manage) {
131  // update the default button if the dialog is shown
132  bool is_default = isButtonEnabled(Default) && d->manager->isDefault();
133  enableButton(Default,!is_default);
134  }
135  return item;
136 }
137 
138 KPageWidgetItem* KConfigDialog::addPage(QWidget *page,
139  KConfigSkeleton *config,
140  const QString &itemName,
141  const QString &pixmapName,
142  const QString &header)
143 {
144  Q_ASSERT(page);
145  if (!page) {
146  return 0;
147  }
148 
149  KPageWidgetItem* item = d->addPageInternal(page, itemName, pixmapName, header);
150  d->managerForPage[page] = new KConfigDialogManager(page, config);
151  d->setupManagerConnections(d->managerForPage[page]);
152 
153  if (d->shown)
154  {
155  // update the default button if the dialog is shown
156  bool is_default = isButtonEnabled(Default) && d->managerForPage[page]->isDefault();
157  enableButton(Default,!is_default);
158  }
159  return item;
160 }
161 
162 KPageWidgetItem* KConfigDialog::KConfigDialogPrivate::addPageInternal(QWidget *page,
163  const QString &itemName,
164  const QString &pixmapName,
165  const QString &header)
166 {
167  KVBox *frame = new KVBox(q);
168  frame->setSpacing(-1);
169  page->setParent(frame);
170 
171  KPageWidgetItem *item = new KPageWidgetItem( frame, itemName );
172  item->setHeader( header );
173  if ( !pixmapName.isEmpty() )
174  item->setIcon( KIcon( pixmapName ) );
175 
176  q->KPageDialog::addPage( item );
177  return item;
178 }
179 
180 void KConfigDialog::KConfigDialogPrivate::setupManagerConnections(KConfigDialogManager *manager)
181 {
182  q->connect(manager, SIGNAL(settingsChanged()), q, SLOT(_k_settingsChangedSlot()));
183  q->connect(manager, SIGNAL(widgetModified()), q, SLOT(_k_updateButtons()));
184 
185  q->connect(q, SIGNAL(okClicked()), manager, SLOT(updateSettings()));
186  q->connect(q, SIGNAL(applyClicked()), manager, SLOT(updateSettings()));
187  q->connect(q, SIGNAL(cancelClicked()), manager, SLOT(updateWidgets()));
188  q->connect(q, SIGNAL(defaultClicked()), manager, SLOT(updateWidgetsDefault()));
189 }
190 
191 void KConfigDialog::onPageRemoved( KPageWidgetItem *item )
192 {
193  QMap<QWidget *, KConfigDialogManager *>::iterator j = d->managerForPage.begin();
194  while (j != d->managerForPage.end())
195  {
196  // there is a manager for this page, so remove it
197  if (item->widget()->isAncestorOf(j.key()))
198  {
199  KConfigDialogManager* manager = j.value();
200  d->managerForPage.erase(j);
201  delete manager;
202  d->_k_updateButtons();
203  break;
204  }
205  ++j;
206  }
207 }
208 
209 KConfigDialog* KConfigDialog::exists(const QString& name)
210 {
211  QHash<QString,KConfigDialog *>::const_iterator it = KConfigDialogPrivate::openDialogs.constFind( name );
212  if ( it != KConfigDialogPrivate::openDialogs.constEnd() )
213  return *it;
214  return 0;
215 }
216 
217 bool KConfigDialog::showDialog(const QString& name)
218 {
219  KConfigDialog *dialog = exists(name);
220  if(dialog)
221  dialog->show();
222  return (dialog != NULL);
223 }
224 
225 void KConfigDialog::KConfigDialogPrivate::_k_updateButtons()
226 {
227  static bool only_once = false;
228  if (only_once) return;
229  only_once = true;
230 
231  QMap<QWidget *, KConfigDialogManager *>::iterator it;
232 
233  bool has_changed = manager->hasChanged() || q->hasChanged();
234  for (it = managerForPage.begin();
235  it != managerForPage.end() && !has_changed;
236  ++it)
237  {
238  has_changed |= (*it)->hasChanged();
239  }
240 
241  q->enableButton(KDialog::Apply, has_changed);
242 
243  bool is_default = manager->isDefault() && q->isDefault();
244  for (it = managerForPage.begin();
245  it != managerForPage.end() && is_default;
246  ++it)
247  {
248  is_default &= (*it)->isDefault();
249  }
250 
251  q->enableButton(KDialog::Default, !is_default);
252 
253  emit q->widgetModified();
254  only_once = false;
255 }
256 
257 void KConfigDialog::KConfigDialogPrivate::_k_settingsChangedSlot()
258 {
259  // Update the buttons
260  _k_updateButtons();
261  emit q->settingsChanged(q->objectName());
262 }
263 
264 void KConfigDialog::showEvent(QShowEvent *e)
265 {
266  if (!d->shown)
267  {
268  QMap<QWidget *, KConfigDialogManager *>::iterator it;
269 
270  updateWidgets();
271  d->manager->updateWidgets();
272  for (it = d->managerForPage.begin(); it != d->managerForPage.end(); ++it)
273  (*it)->updateWidgets();
274 
275  bool has_changed = d->manager->hasChanged() || hasChanged();
276  for (it = d->managerForPage.begin();
277  it != d->managerForPage.end() && !has_changed;
278  ++it)
279  {
280  has_changed |= (*it)->hasChanged();
281  }
282 
283  enableButton(Apply, has_changed);
284 
285  bool is_default = d->manager->isDefault() && isDefault();
286  for (it = d->managerForPage.begin();
287  it != d->managerForPage.end() && is_default;
288  ++it)
289  {
290  is_default &= (*it)->isDefault();
291  }
292 
293  enableButton(Default, !is_default);
294  d->shown = true;
295  }
296  KPageDialog::showEvent(e);
297 }
298 
299 void KConfigDialog::updateSettings()
300 {
301 }
302 
303 void KConfigDialog::updateWidgets()
304 {
305 }
306 
307 void KConfigDialog::updateWidgetsDefault()
308 {
309 }
310 
311 bool KConfigDialog::hasChanged()
312 {
313  return false;
314 }
315 
316 bool KConfigDialog::isDefault()
317 {
318  return true;
319 }
320 
321 void KConfigDialog::updateButtons()
322 {
323  d->_k_updateButtons();
324 }
325 
326 void KConfigDialog::settingsChangedSlot()
327 {
328  d->_k_settingsChangedSlot();
329 }
330 
331 #include "kconfigdialog.moc"
KConfigDialog::updateWidgets
virtual void updateWidgets()
Update the dialog based on the settings.
Definition: kconfigdialog.cpp:303
KConfigDialog::showDialog
static bool showDialog(const QString &name)
Attempts to show the dialog with the name 'name'.
Definition: kconfigdialog.cpp:217
i18n
QString i18n(const char *text)
KConfigDialog::widgetModified
void widgetModified()
A widget in the dialog was modified.
KPageDialog
A dialog base class which can handle multiple pages.
Definition: kpagedialog.h:65
KVBox
A container widget which arranges its children vertically.
Definition: kvbox.h:36
KConfigDialog::settingsChangedSlot
void settingsChangedSlot()
Some setting was changed.
Definition: kconfigdialog.cpp:326
KDialog::okClicked
void okClicked()
The OK button was pressed.
KDialog::isButtonEnabled
bool isButtonEnabled(ButtonCode id) const
Returns whether any button is enabled.
Definition: kdialog.cpp:668
kdebug.h
KConfigDialog::showEvent
virtual void showEvent(QShowEvent *e)
Definition: kconfigdialog.cpp:264
KDialog::defaultClicked
void defaultClicked()
The Default button was pressed.
KConfigDialog::updateSettings
virtual void updateSettings()
Update the settings from the dialog.
Definition: kconfigdialog.cpp:299
KConfigDialog::exists
static KConfigDialog * exists(const QString &name)
See if a dialog with the name 'name' already exists.
Definition: kconfigdialog.cpp:209
KDialog::Cancel
Show Cancel-button. (this button reject()s the dialog; result set to QDialog::Rejected) ...
Definition: kdialog.h:144
KDialog::setHelp
void setHelp(const QString &anchor, const QString &appname=QString())
Sets the help path and topic.
Definition: kdialog.cpp:967
kpagewidgetmodel.h
QWidget
KConfigDialogManager::hasChanged
bool hasChanged() const
Returns whether the current state of the known widgets are different from the state in the config obj...
Definition: kconfigdialogmanager.cpp:517
KStandardAction::name
const char * name(StandardAction id)
This will return the internal name of a given standard action.
Definition: kstandardaction.cpp:223
KConfigDialogManager
Provides a means of automatically retrieving, saving and resetting KConfigSkeleton based settings in ...
Definition: kconfigdialogmanager.h:85
KConfigDialog::hasChanged
virtual bool hasChanged()
Returns whether the current state of the dialog is different from the current configuration.
Definition: kconfigdialog.cpp:311
KPageDialog::pageRemoved
void pageRemoved(KPageWidgetItem *page)
This signal is emitted whenever a page has been removed.
kiconloader.h
KConfigSkeleton
Class for handling preferences settings for an application.
Definition: kconfigskeleton.h:40
QString
KConfigDialog::addPage
KPageWidgetItem * addPage(QWidget *page, const QString &itemName, const QString &pixmapName=QString(), const QString &header=QString(), bool manage=true)
Adds page to the dialog and to KConfigDialogManager.
Definition: kconfigdialog.cpp:114
QHash< QString, KConfigDialog * >
klocale.h
kconfigdialog.h
KDialog::Default
Show Default button.
Definition: kdialog.h:140
KDialog::setCaption
virtual void setCaption(const QString &caption)
Make a KDE compliant caption.
Definition: kdialog.cpp:469
KConfigDialog::updateWidgetsDefault
virtual void updateWidgetsDefault()
Update the dialog based on the default settings.
Definition: kconfigdialog.cpp:307
KPageWidgetItem::setIcon
void setIcon(const KIcon &icon)
Sets the icon of the page widget item.
Definition: kpagewidgetmodel.cpp:130
KConfigDialogManager::isDefault
bool isDefault() const
Returns whether the current state of the known widgets are the same as the default state in the confi...
Definition: kconfigdialogmanager.cpp:539
KConfigDialog
Standard KDE configuration dialog class.
Definition: kconfigdialog.h:73
KPageDialog::setFaceType
void setFaceType(FaceType faceType)
Sets the face type of the dialog.
Definition: kpagedialog.cpp:68
KConfigDialog::KConfigDialogPrivate
friend class KConfigDialogPrivate
Definition: kconfigdialog.h:259
KDialog::cancelClicked
void cancelClicked()
The Cancel button was pressed.
KCoreConfigSkeleton
KIcon
A wrapper around QIcon that provides KDE icon features.
Definition: kicon.h:40
KDialog::applyClicked
void applyClicked()
The Apply button was pressed.
KDialog::Apply
Show Apply button.
Definition: kdialog.h:142
KConfigDialog::isDefault
virtual bool isDefault()
Returns whether the current state of the dialog is the same as the default configuration.
Definition: kconfigdialog.cpp:316
KDialog::setButtons
void setButtons(ButtonCodes buttonMask)
Creates (or recreates) the button box and all the buttons in it.
Definition: kdialog.cpp:206
kvbox.h
KDialog::Help
Show Help button. (this button will run the help set with setHelp)
Definition: kdialog.h:139
KConfigDialog::settingsChanged
void settingsChanged(const QString &dialogName)
One or more of the settings have been permanently changed such as if the user clicked on the Apply or...
KDialog::Ok
Show Ok button. (this button accept()s the dialog; result set to QDialog::Accepted) ...
Definition: kdialog.h:141
KPageWidgetItem::widget
QWidget * widget() const
Returns the widget of the page widget item.
Definition: kpagewidgetmodel.cpp:101
KDialog::setDefaultButton
void setDefaultButton(ButtonCode id)
Sets the button that will be activated when the Enter key is pressed.
Definition: kdialog.cpp:287
KConfigDialog::KConfigDialog
KConfigDialog(QWidget *parent, const QString &name, KConfigSkeleton *config)
Definition: kconfigdialog.cpp:94
KConfigDialog::updateButtons
void updateButtons()
Updates the Apply and Default buttons.
Definition: kconfigdialog.cpp:321
KPageWidgetItem
KPageWidgetItem is used by KPageWidget and represents a page.
Definition: kpagewidgetmodel.h:50
KDialog::enableButton
void enableButton(ButtonCode id, bool state)
Enable or disable (gray out) a general action button.
Definition: kdialog.cpp:661
KHBox::setSpacing
void setSpacing(int space)
Sets the spacing between the child widgets to space.
Definition: khbox.cpp:98
KGlobal::mainComponent
const KComponentData & mainComponent()
KConfigDialog::~KConfigDialog
~KConfigDialog()
Deconstructor, removes name from the list of open dialogs.
Definition: kconfigdialog.cpp:108
KPageWidgetItem::setHeader
void setHeader(const QString &header)
Sets the header of the page widget item.
Definition: kpagewidgetmodel.cpp:118
kcomponentdata.h
kicon.h
KPageDialog::List
Definition: kpagedialog.h:91
QMap< QWidget *, KConfigDialogManager * >
kconfigdialogmanager.h
kconfigskeleton.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:49:14 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDEUI

Skip menu "KDEUI"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal