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