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

KDEUI

kconfigdialog.cpp
Go to the documentation of this file.
00001 /*
00002  *  This file is part of the KDE libraries
00003  *  Copyright (C) 2003 Benjamin C Meyer (ben+kdelibs at meyerhome dot net)
00004  *  Copyright (C) 2003 Waldo Bastian <bastian@kde.org>
00005  *  Copyright (C) 2004 Michael Brade <brade@kde.org>
00006  *
00007  *  This library is free software; you can redistribute it and/or
00008  *  modify it under the terms of the GNU Library General Public
00009  *  License as published by the Free Software Foundation; either
00010  *  version 2 of the License, or (at your option) any later version.
00011  *
00012  *  This library is distributed in the hope that it will be useful,
00013  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  *  Library General Public License for more details.
00016  *
00017  *  You should have received a copy of the GNU Library General Public License
00018  *  along with this library; see the file COPYING.LIB.  If not, write to
00019  *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00020  *  Boston, MA 02110-1301, USA.
00021  */
00022 #include "kconfigdialog.h"
00023 
00024 #include <kcomponentdata.h>
00025 #include <kconfigdialogmanager.h>
00026 #include <kconfigskeleton.h>
00027 #include <kdebug.h>
00028 #include <kicon.h>
00029 #include <kiconloader.h>
00030 #include <klocale.h>
00031 #include <kpagewidgetmodel.h>
00032 #include <kvbox.h>
00033 
00034 #include <QtGui/QLayout>
00035 #include <QtCore/QMap>
00036 
00037 class KConfigDialog::KConfigDialogPrivate
00038 {
00039 public:
00040   KConfigDialogPrivate(KConfigDialog *q, const QString& name, KCoreConfigSkeleton *config)
00041     : q(q), shown(false), manager(0)
00042   {
00043     q->setCaption( i18n("Configure") );
00044     q->setFaceType( List );
00045     q->setButtons( Default|Ok|Apply|Cancel|Help );
00046     q->setHelp( QString(), KGlobal::mainComponent().componentName() );
00047     q->setDefaultButton( Ok );
00048     q->setObjectName( name );
00049 
00050     if ( !name.isEmpty() ) {
00051       openDialogs.insert(name, q);
00052     } else {
00053       QString genericName;
00054       genericName.sprintf("SettingsDialog-%p", static_cast<void*>(q));
00055       openDialogs.insert(genericName, q);
00056       q->setObjectName(genericName);
00057     }
00058 
00059     connect(q, SIGNAL(okClicked()), q, SLOT(updateSettings()));
00060     connect(q, SIGNAL(applyClicked()), q, SLOT(updateSettings()));
00061     connect(q, SIGNAL(applyClicked()), q, SLOT(_k_updateButtons()));
00062     connect(q, SIGNAL(cancelClicked()), q, SLOT(updateWidgets()));
00063     connect(q, SIGNAL(defaultClicked()), q, SLOT(updateWidgetsDefault()));
00064     connect(q, SIGNAL(defaultClicked()), q, SLOT(_k_updateButtons()));
00065     connect(q, SIGNAL(pageRemoved(KPageWidgetItem*)), q, SLOT(onPageRemoved(KPageWidgetItem*)));
00066 
00067     manager = new KConfigDialogManager(q, config);
00068     setupManagerConnections(manager);
00069 
00070     q->enableButton(Apply, false);
00071   }
00072 
00073   KPageWidgetItem* addPageInternal(QWidget *page, const QString &itemName,
00074                            const QString &pixmapName, const QString &header);
00075 
00076   void setupManagerConnections(KConfigDialogManager *manager);
00077 
00078   void _k_updateButtons();
00079   void _k_settingsChangedSlot();
00080 
00081   KConfigDialog *q;
00082   bool shown;
00083   KConfigDialogManager *manager;
00084   QMap<QWidget *, KConfigDialogManager *> managerForPage;
00085 
00089   static QHash<QString,KConfigDialog *> openDialogs;
00090 };
00091 
00092 QHash<QString,KConfigDialog *> KConfigDialog::KConfigDialogPrivate::openDialogs;
00093 
00094 KConfigDialog::KConfigDialog( QWidget *parent, const QString& name,
00095           KConfigSkeleton *config ) :
00096     KPageDialog( parent ),
00097     d(new KConfigDialogPrivate(this, name, config))
00098 {
00099 }
00100 
00101 KConfigDialog::KConfigDialog( QWidget *parent, const QString& name,
00102           KCoreConfigSkeleton *config ) :
00103     KPageDialog( parent ),
00104     d(new KConfigDialogPrivate(this, name, config))
00105 {
00106 }
00107 
00108 KConfigDialog::~KConfigDialog()
00109 {
00110   KConfigDialogPrivate::openDialogs.remove(objectName());
00111   delete d;
00112 }
00113 
00114 KPageWidgetItem* KConfigDialog::addPage(QWidget *page,
00115                                 const QString &itemName,
00116                                 const QString &pixmapName,
00117                                 const QString &header,
00118                                 bool manage)
00119 {
00120   Q_ASSERT(page);
00121   if (!page) {
00122       return 0;
00123   }
00124 
00125   KPageWidgetItem* item = d->addPageInternal(page, itemName, pixmapName, header);
00126   if (manage) {
00127     d->manager->addWidget(page);
00128   }
00129 
00130   if (d->shown && manage) {
00131     // update the default button if the dialog is shown
00132     bool is_default = isButtonEnabled(Default) && d->manager->isDefault();
00133     enableButton(Default,!is_default);
00134   }
00135   return item;
00136 }
00137 
00138 KPageWidgetItem* KConfigDialog::addPage(QWidget *page,
00139                                 KConfigSkeleton *config,
00140                                 const QString &itemName,
00141                                 const QString &pixmapName,
00142                                 const QString &header)
00143 {
00144   Q_ASSERT(page);
00145   if (!page) {
00146       return 0;
00147   }
00148 
00149   KPageWidgetItem* item = d->addPageInternal(page, itemName, pixmapName, header);
00150   d->managerForPage[page] = new KConfigDialogManager(page, config);
00151   d->setupManagerConnections(d->managerForPage[page]);
00152 
00153   if (d->shown)
00154   {
00155     // update the default button if the dialog is shown
00156     bool is_default = isButtonEnabled(Default) && d->managerForPage[page]->isDefault();
00157     enableButton(Default,!is_default);
00158   }
00159   return item;
00160 }
00161 
00162 KPageWidgetItem* KConfigDialog::KConfigDialogPrivate::addPageInternal(QWidget *page,
00163                                         const QString &itemName,
00164                                         const QString &pixmapName,
00165                                         const QString &header)
00166 {
00167   KVBox *frame = new KVBox(q);
00168   frame->setSpacing(-1);
00169   page->setParent(frame);
00170 
00171   KPageWidgetItem *item = new KPageWidgetItem( frame, itemName );
00172   item->setHeader( header );
00173   if ( !pixmapName.isEmpty() )
00174     item->setIcon( KIcon( pixmapName ) );
00175 
00176   q->KPageDialog::addPage( item );
00177   return item;
00178 }
00179 
00180 void KConfigDialog::KConfigDialogPrivate::setupManagerConnections(KConfigDialogManager *manager)
00181 {
00182     q->connect(manager, SIGNAL(settingsChanged()), q, SLOT(_k_settingsChangedSlot()));
00183     q->connect(manager, SIGNAL(widgetModified()), q, SLOT(_k_updateButtons()));
00184 
00185     q->connect(q, SIGNAL(okClicked()), manager, SLOT(updateSettings()));
00186     q->connect(q, SIGNAL(applyClicked()), manager, SLOT(updateSettings()));
00187     q->connect(q, SIGNAL(cancelClicked()), manager, SLOT(updateWidgets()));
00188     q->connect(q, SIGNAL(defaultClicked()), manager, SLOT(updateWidgetsDefault()));
00189 }
00190 
00191 void KConfigDialog::onPageRemoved( KPageWidgetItem *item )
00192 {
00193     QMap<QWidget *, KConfigDialogManager *>::iterator j = d->managerForPage.begin();
00194     while (j != d->managerForPage.end())
00195     {
00196         // there is a manager for this page, so remove it
00197         if (item->widget()->isAncestorOf(j.key()))
00198         {
00199             KConfigDialogManager* manager = j.value();
00200             d->managerForPage.erase(j);
00201             delete manager;
00202             d->_k_updateButtons();
00203             break;
00204         }
00205         j++;
00206     }
00207 }
00208 
00209 KConfigDialog* KConfigDialog::exists(const QString& name)
00210 {
00211   QHash<QString,KConfigDialog *>::const_iterator it = KConfigDialogPrivate::openDialogs.constFind( name );
00212   if ( it != KConfigDialogPrivate::openDialogs.constEnd() )
00213       return *it;
00214   return 0;
00215 }
00216 
00217 bool KConfigDialog::showDialog(const QString& name)
00218 {
00219   KConfigDialog *dialog = exists(name);
00220   if(dialog)
00221     dialog->show();
00222   return (dialog != NULL);
00223 }
00224 
00225 void KConfigDialog::KConfigDialogPrivate::_k_updateButtons()
00226 {
00227   static bool only_once = false;
00228   if (only_once) return;
00229   only_once = true;
00230 
00231   QMap<QWidget *, KConfigDialogManager *>::iterator it;
00232 
00233   bool has_changed = manager->hasChanged() || q->hasChanged();
00234   for (it = managerForPage.begin();
00235           it != managerForPage.end() && !has_changed;
00236           ++it)
00237   {
00238     has_changed |= (*it)->hasChanged();
00239   }
00240 
00241   q->enableButton(KDialog::Apply, has_changed);
00242 
00243   bool is_default = manager->isDefault() && q->isDefault();
00244   for (it = managerForPage.begin();
00245           it != managerForPage.end() && is_default;
00246           ++it)
00247   {
00248     is_default &= (*it)->isDefault();
00249   }
00250 
00251   q->enableButton(KDialog::Default, !is_default);
00252 
00253   emit q->widgetModified();
00254   only_once = false;
00255 }
00256 
00257 void KConfigDialog::KConfigDialogPrivate::_k_settingsChangedSlot()
00258 {
00259   // Update the buttons
00260   _k_updateButtons();
00261   emit q->settingsChanged(q->objectName());
00262 }
00263 
00264 void KConfigDialog::showEvent(QShowEvent *e)
00265 {
00266   if (!d->shown)
00267   {
00268     QMap<QWidget *, KConfigDialogManager *>::iterator it;
00269 
00270     updateWidgets();
00271     d->manager->updateWidgets();
00272     for (it = d->managerForPage.begin(); it != d->managerForPage.end(); ++it)
00273       (*it)->updateWidgets();
00274 
00275     bool has_changed = d->manager->hasChanged() || hasChanged();
00276     for (it = d->managerForPage.begin();
00277             it != d->managerForPage.end() && !has_changed;
00278             ++it)
00279     {
00280       has_changed |= (*it)->hasChanged();
00281     }
00282 
00283     enableButton(Apply, has_changed);
00284 
00285     bool is_default = d->manager->isDefault() && isDefault();
00286     for (it = d->managerForPage.begin();
00287             it != d->managerForPage.end() && is_default;
00288             ++it)
00289     {
00290       is_default &= (*it)->isDefault();
00291     }
00292 
00293     enableButton(Default, !is_default);
00294     d->shown = true;
00295   }
00296   KPageDialog::showEvent(e);
00297 }
00298 
00299 void KConfigDialog::updateSettings()
00300 {
00301 }
00302 
00303 void KConfigDialog::updateWidgets()
00304 {
00305 }
00306 
00307 void KConfigDialog::updateWidgetsDefault()
00308 {
00309 }
00310 
00311 bool KConfigDialog::hasChanged()
00312 {
00313     return false;
00314 }
00315 
00316 bool KConfigDialog::isDefault()
00317 {
00318     return true;
00319 }
00320 
00321 void KConfigDialog::updateButtons()
00322 {
00323     d->_k_updateButtons();
00324 }
00325 
00326 void KConfigDialog::settingsChangedSlot()
00327 {
00328     d->_k_settingsChangedSlot();
00329 }
00330 
00331 #include "kconfigdialog.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Wed May 2 2012 17:56:56 by doxygen 1.8.0 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.8.3 API Reference

Skip menu "kdelibs-4.8.3 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