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

KDEUI

  • 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"
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Wed Jun 5 2013 18:37:30 by doxygen 1.8.3.1 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-4.10.4 API Reference

Skip menu "kdelibs-4.10.4 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
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
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