• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • 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)
00041     : q(q), shown(false), manager(0) { }
00042 
00043   KPageWidgetItem* addPageInternal(QWidget *page, const QString &itemName,
00044                            const QString &pixmapName, const QString &header);
00045 
00046   void setupManagerConnections(KConfigDialogManager *manager);
00047 
00048   void _k_updateButtons();
00049   void _k_settingsChangedSlot();
00050 
00051   KConfigDialog *q;
00052   bool shown;
00053   KConfigDialogManager *manager;
00054   QMap<QWidget *, KConfigDialogManager *> managerForPage;
00055 
00059   static QHash<QString,KConfigDialog *> openDialogs;
00060 };
00061 
00062 QHash<QString,KConfigDialog *> KConfigDialog::KConfigDialogPrivate::openDialogs;
00063 
00064 KConfigDialog::KConfigDialog( QWidget *parent, const QString& name,
00065           KConfigSkeleton *config ) :
00066     KPageDialog( parent ),
00067     d(new KConfigDialogPrivate(this))
00068 {
00069   setCaption( i18n("Configure") );
00070   setFaceType( List );
00071   setButtons( Default|Ok|Apply|Cancel|Help );
00072   setHelp( QString(), KGlobal::mainComponent().componentName() );
00073   setDefaultButton( Ok );
00074   setObjectName( name );
00075   showButtonSeparator( true );
00076 
00077   if ( !name.isEmpty() ) {
00078     KConfigDialogPrivate::openDialogs.insert(name, this);
00079   } else {
00080     QString genericName;
00081     genericName.sprintf("SettingsDialog-%p", static_cast<void*>(this));
00082     KConfigDialogPrivate::openDialogs.insert(genericName, this);
00083     setObjectName(genericName);
00084   }
00085 
00086   connect(this, SIGNAL(okClicked()), this, SLOT(updateSettings()));
00087   connect(this, SIGNAL(applyClicked()), this, SLOT(updateSettings()));
00088   connect(this, SIGNAL(applyClicked()), this, SLOT(_k_updateButtons()));
00089   connect(this, SIGNAL(cancelClicked()), this, SLOT(updateWidgets()));
00090   connect(this, SIGNAL(defaultClicked()), this, SLOT(updateWidgetsDefault()));
00091   connect(this, SIGNAL(defaultClicked()), this, SLOT(_k_updateButtons()));
00092   connect(this, SIGNAL(pageRemoved(KPageWidgetItem*)), this, SLOT(onPageRemoved(KPageWidgetItem*)));
00093 
00094   d->manager = new KConfigDialogManager(this, config);
00095   d->setupManagerConnections(d->manager);
00096 
00097   enableButton(Apply, false);
00098 }
00099 
00100 KConfigDialog::~KConfigDialog()
00101 {
00102   KConfigDialogPrivate::openDialogs.remove(objectName());
00103   delete d;
00104 }
00105 
00106 KPageWidgetItem* KConfigDialog::addPage(QWidget *page,
00107                                 const QString &itemName,
00108                                 const QString &pixmapName,
00109                                 const QString &header,
00110                                 bool manage)
00111 {
00112   KPageWidgetItem* item = d->addPageInternal(page, itemName, pixmapName, header);
00113   if(manage)
00114     d->manager->addWidget(page);
00115   
00116   if (d->shown && manage)
00117   {
00118     // update the default button if the dialog is shown
00119     bool is_default = isButtonEnabled(Default) && d->manager->isDefault();
00120     enableButton(Default,!is_default);
00121   }
00122   return item;
00123 }
00124 
00125 KPageWidgetItem* KConfigDialog::addPage(QWidget *page,
00126                                 KConfigSkeleton *config,
00127                                 const QString &itemName,
00128                                 const QString &pixmapName,
00129                                 const QString &header)
00130 {
00131   KPageWidgetItem* item = d->addPageInternal(page, itemName, pixmapName, header);
00132   d->managerForPage[page] = new KConfigDialogManager(page, config);
00133   d->setupManagerConnections(d->managerForPage[page]);
00134   
00135   if (d->shown)
00136   {
00137     // update the default button if the dialog is shown
00138     bool is_default = isButtonEnabled(Default) && d->managerForPage[page]->isDefault();
00139     enableButton(Default,!is_default);
00140   }
00141   return item;
00142 }
00143 
00144 KPageWidgetItem* KConfigDialog::KConfigDialogPrivate::addPageInternal(QWidget *page,
00145                                         const QString &itemName,
00146                                         const QString &pixmapName,
00147                                         const QString &header)
00148 {
00149   KVBox *frame = new KVBox(q);
00150   frame->setSpacing( 0 );
00151   frame->setMargin( 0 );
00152   page->setParent(frame);
00153 
00154   KPageWidgetItem *item = new KPageWidgetItem( frame, itemName );
00155   item->setHeader( header );
00156   if ( !pixmapName.isEmpty() )
00157     item->setIcon( KIcon( pixmapName ) );
00158 
00159   q->KPageDialog::addPage( item );
00160   return item;
00161 }
00162 
00163 void KConfigDialog::KConfigDialogPrivate::setupManagerConnections(KConfigDialogManager *manager)
00164 {
00165     q->connect(manager, SIGNAL(settingsChanged()), q, SLOT(_k_settingsChangedSlot()));
00166     q->connect(manager, SIGNAL(widgetModified()), q, SLOT(_k_updateButtons()));
00167 
00168     q->connect(q, SIGNAL(okClicked()), manager, SLOT(updateSettings()));
00169     q->connect(q, SIGNAL(applyClicked()), manager, SLOT(updateSettings()));
00170     q->connect(q, SIGNAL(cancelClicked()), manager, SLOT(updateWidgets()));
00171     q->connect(q, SIGNAL(defaultClicked()), manager, SLOT(updateWidgetsDefault()));
00172 }
00173 
00174 void KConfigDialog::onPageRemoved( KPageWidgetItem *item )
00175 {   
00176     QMap<QWidget *, KConfigDialogManager *>::iterator j = d->managerForPage.begin();
00177     while (j != d->managerForPage.end())
00178     {
00179         // there is a manager for this page, so remove it
00180         if (item->widget()->isAncestorOf(j.key())) 
00181         {
00182             KConfigDialogManager* manager = j.value();
00183             d->managerForPage.erase(j);
00184             delete manager;
00185             d->_k_updateButtons();
00186             break;
00187         }
00188         j++;
00189     }
00190 }
00191 
00192 KConfigDialog* KConfigDialog::exists(const QString& name)
00193 {
00194   QHash<QString,KConfigDialog *>::const_iterator it = KConfigDialogPrivate::openDialogs.constFind( name );
00195   if ( it != KConfigDialogPrivate::openDialogs.constEnd() )
00196       return *it;
00197   return 0;
00198 }
00199 
00200 bool KConfigDialog::showDialog(const QString& name)
00201 {
00202   KConfigDialog *dialog = exists(name);
00203   if(dialog)
00204     dialog->show();
00205   return (dialog != NULL);
00206 }
00207 
00208 void KConfigDialog::KConfigDialogPrivate::_k_updateButtons()
00209 {
00210   static bool only_once = false;
00211   if (only_once) return;
00212   only_once = true;
00213 
00214   QMap<QWidget *, KConfigDialogManager *>::iterator it;
00215 
00216   bool has_changed = manager->hasChanged() || q->hasChanged();
00217   for (it = managerForPage.begin();
00218           it != managerForPage.end() && !has_changed;
00219           ++it)
00220   {
00221     has_changed |= (*it)->hasChanged();
00222   }
00223 
00224   q->enableButton(KDialog::Apply, has_changed);
00225 
00226   bool is_default = manager->isDefault() && q->isDefault();
00227   for (it = managerForPage.begin();
00228           it != managerForPage.end() && is_default;
00229           ++it)
00230   {
00231     is_default &= (*it)->isDefault();
00232   }
00233 
00234   q->enableButton(KDialog::Default, !is_default);
00235 
00236   emit q->widgetModified();
00237   only_once = false;
00238 }
00239 
00240 void KConfigDialog::KConfigDialogPrivate::_k_settingsChangedSlot()
00241 {
00242   // Update the buttons
00243   _k_updateButtons();
00244   emit q->settingsChanged(q->objectName());
00245 }
00246 
00247 void KConfigDialog::showEvent(QShowEvent *e)
00248 {
00249   if (!d->shown)
00250   {
00251     QMap<QWidget *, KConfigDialogManager *>::iterator it;
00252 
00253     updateWidgets();
00254     d->manager->updateWidgets();
00255     for (it = d->managerForPage.begin(); it != d->managerForPage.end(); ++it)
00256       (*it)->updateWidgets();
00257 
00258     bool has_changed = d->manager->hasChanged() || hasChanged();
00259     for (it = d->managerForPage.begin();
00260             it != d->managerForPage.end() && !has_changed;
00261             ++it)
00262     {
00263       has_changed |= (*it)->hasChanged();
00264     }
00265 
00266     enableButton(Apply, has_changed);
00267 
00268     bool is_default = d->manager->isDefault() && isDefault();
00269     for (it = d->managerForPage.begin();
00270             it != d->managerForPage.end() && is_default;
00271             ++it)
00272     {
00273       is_default &= (*it)->isDefault();
00274     }
00275 
00276     enableButton(Default, !is_default);
00277     d->shown = true;
00278   }
00279   KPageDialog::showEvent(e);
00280 }
00281 
00282 void KConfigDialog::updateSettings()
00283 {
00284 }
00285 
00286 void KConfigDialog::updateWidgets()
00287 {
00288 }
00289 
00290 void KConfigDialog::updateWidgetsDefault()
00291 {
00292 }
00293 
00294 bool KConfigDialog::hasChanged()
00295 {
00296     return false;
00297 }
00298 
00299 bool KConfigDialog::isDefault()
00300 {
00301     return true;
00302 }
00303 
00304 
00305 #include "kconfigdialog.moc"

KDEUI

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

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.7
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal