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 Q_ASSERT(page); 00112 if (!page) { 00113 return 0; 00114 } 00115 00116 KPageWidgetItem* item = d->addPageInternal(page, itemName, pixmapName, header); 00117 if (manage) { 00118 d->manager->addWidget(page); 00119 } 00120 00121 if (d->shown && manage) { 00122 // update the default button if the dialog is shown 00123 bool is_default = isButtonEnabled(Default) && d->manager->isDefault(); 00124 enableButton(Default,!is_default); 00125 } 00126 return item; 00127 } 00128 00129 KPageWidgetItem* KConfigDialog::addPage(QWidget *page, 00130 KConfigSkeleton *config, 00131 const QString &itemName, 00132 const QString &pixmapName, 00133 const QString &header) 00134 { 00135 Q_ASSERT(page); 00136 if (!page) { 00137 return 0; 00138 } 00139 00140 KPageWidgetItem* item = d->addPageInternal(page, itemName, pixmapName, header); 00141 d->managerForPage[page] = new KConfigDialogManager(page, config); 00142 d->setupManagerConnections(d->managerForPage[page]); 00143 00144 if (d->shown) 00145 { 00146 // update the default button if the dialog is shown 00147 bool is_default = isButtonEnabled(Default) && d->managerForPage[page]->isDefault(); 00148 enableButton(Default,!is_default); 00149 } 00150 return item; 00151 } 00152 00153 KPageWidgetItem* KConfigDialog::KConfigDialogPrivate::addPageInternal(QWidget *page, 00154 const QString &itemName, 00155 const QString &pixmapName, 00156 const QString &header) 00157 { 00158 KVBox *frame = new KVBox(q); 00159 frame->setSpacing(-1); 00160 page->setParent(frame); 00161 00162 KPageWidgetItem *item = new KPageWidgetItem( frame, itemName ); 00163 item->setHeader( header ); 00164 if ( !pixmapName.isEmpty() ) 00165 item->setIcon( KIcon( pixmapName ) ); 00166 00167 q->KPageDialog::addPage( item ); 00168 return item; 00169 } 00170 00171 void KConfigDialog::KConfigDialogPrivate::setupManagerConnections(KConfigDialogManager *manager) 00172 { 00173 q->connect(manager, SIGNAL(settingsChanged()), q, SLOT(_k_settingsChangedSlot())); 00174 q->connect(manager, SIGNAL(widgetModified()), q, SLOT(_k_updateButtons())); 00175 00176 q->connect(q, SIGNAL(okClicked()), manager, SLOT(updateSettings())); 00177 q->connect(q, SIGNAL(applyClicked()), manager, SLOT(updateSettings())); 00178 q->connect(q, SIGNAL(cancelClicked()), manager, SLOT(updateWidgets())); 00179 q->connect(q, SIGNAL(defaultClicked()), manager, SLOT(updateWidgetsDefault())); 00180 } 00181 00182 void KConfigDialog::onPageRemoved( KPageWidgetItem *item ) 00183 { 00184 QMap<QWidget *, KConfigDialogManager *>::iterator j = d->managerForPage.begin(); 00185 while (j != d->managerForPage.end()) 00186 { 00187 // there is a manager for this page, so remove it 00188 if (item->widget()->isAncestorOf(j.key())) 00189 { 00190 KConfigDialogManager* manager = j.value(); 00191 d->managerForPage.erase(j); 00192 delete manager; 00193 d->_k_updateButtons(); 00194 break; 00195 } 00196 j++; 00197 } 00198 } 00199 00200 KConfigDialog* KConfigDialog::exists(const QString& name) 00201 { 00202 QHash<QString,KConfigDialog *>::const_iterator it = KConfigDialogPrivate::openDialogs.constFind( name ); 00203 if ( it != KConfigDialogPrivate::openDialogs.constEnd() ) 00204 return *it; 00205 return 0; 00206 } 00207 00208 bool KConfigDialog::showDialog(const QString& name) 00209 { 00210 KConfigDialog *dialog = exists(name); 00211 if(dialog) 00212 dialog->show(); 00213 return (dialog != NULL); 00214 } 00215 00216 void KConfigDialog::KConfigDialogPrivate::_k_updateButtons() 00217 { 00218 static bool only_once = false; 00219 if (only_once) return; 00220 only_once = true; 00221 00222 QMap<QWidget *, KConfigDialogManager *>::iterator it; 00223 00224 bool has_changed = manager->hasChanged() || q->hasChanged(); 00225 for (it = managerForPage.begin(); 00226 it != managerForPage.end() && !has_changed; 00227 ++it) 00228 { 00229 has_changed |= (*it)->hasChanged(); 00230 } 00231 00232 q->enableButton(KDialog::Apply, has_changed); 00233 00234 bool is_default = manager->isDefault() && q->isDefault(); 00235 for (it = managerForPage.begin(); 00236 it != managerForPage.end() && is_default; 00237 ++it) 00238 { 00239 is_default &= (*it)->isDefault(); 00240 } 00241 00242 q->enableButton(KDialog::Default, !is_default); 00243 00244 emit q->widgetModified(); 00245 only_once = false; 00246 } 00247 00248 void KConfigDialog::KConfigDialogPrivate::_k_settingsChangedSlot() 00249 { 00250 // Update the buttons 00251 _k_updateButtons(); 00252 emit q->settingsChanged(q->objectName()); 00253 } 00254 00255 void KConfigDialog::showEvent(QShowEvent *e) 00256 { 00257 if (!d->shown) 00258 { 00259 QMap<QWidget *, KConfigDialogManager *>::iterator it; 00260 00261 updateWidgets(); 00262 d->manager->updateWidgets(); 00263 for (it = d->managerForPage.begin(); it != d->managerForPage.end(); ++it) 00264 (*it)->updateWidgets(); 00265 00266 bool has_changed = d->manager->hasChanged() || hasChanged(); 00267 for (it = d->managerForPage.begin(); 00268 it != d->managerForPage.end() && !has_changed; 00269 ++it) 00270 { 00271 has_changed |= (*it)->hasChanged(); 00272 } 00273 00274 enableButton(Apply, has_changed); 00275 00276 bool is_default = d->manager->isDefault() && isDefault(); 00277 for (it = d->managerForPage.begin(); 00278 it != d->managerForPage.end() && is_default; 00279 ++it) 00280 { 00281 is_default &= (*it)->isDefault(); 00282 } 00283 00284 enableButton(Default, !is_default); 00285 d->shown = true; 00286 } 00287 KPageDialog::showEvent(e); 00288 } 00289 00290 void KConfigDialog::updateSettings() 00291 { 00292 } 00293 00294 void KConfigDialog::updateWidgets() 00295 { 00296 } 00297 00298 void KConfigDialog::updateWidgetsDefault() 00299 { 00300 } 00301 00302 bool KConfigDialog::hasChanged() 00303 { 00304 return false; 00305 } 00306 00307 bool KConfigDialog::isDefault() 00308 { 00309 return true; 00310 } 00311 00312 void KConfigDialog::updateButtons() 00313 { 00314 d->_k_updateButtons(); 00315 } 00316 00317 void KConfigDialog::settingsChangedSlot() 00318 { 00319 d->_k_settingsChangedSlot(); 00320 } 00321 00322 #include "kconfigdialog.moc"
KDE 4.6 API Reference