libkdepim Library API Documentation

kprefsdialog.cpp

00001 /*
00002     This file is part of libkdepim.
00003 
00004     Copyright (c) 2001,2003 Cornelius Schumacher <schumacher@kde.org>
00005 
00006     This library is free software; you can redistribute it and/or
00007     modify it under the terms of the GNU Library General Public
00008     License as published by the Free Software Foundation; either
00009     version 2 of the License, or (at your option) any later version.
00010 
00011     This library is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014     Library General Public License for more details.
00015 
00016     You should have received a copy of the GNU Library General Public License
00017     along with this library; see the file COPYING.LIB.  If not, write to
00018     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00019     Boston, MA 02111-1307, USA.
00020 */
00021 
00022 #include <qlayout.h>
00023 #include <qlabel.h>
00024 #include <qbuttongroup.h>
00025 #include <qlineedit.h>
00026 #include <qfont.h>
00027 #include <qspinbox.h>
00028 #include <qframe.h>
00029 #include <qcombobox.h>
00030 #include <qcheckbox.h>
00031 #include <qradiobutton.h>
00032 #include <qpushbutton.h>
00033 #include <qwhatsthis.h>
00034 
00035 #include <kcolorbutton.h>
00036 #include <kdebug.h>
00037 #include <klocale.h>
00038 #include <kfontdialog.h>
00039 #include <kmessagebox.h>
00040 #include <kconfigskeleton.h>
00041 
00042 #include "kprefsdialog.h"
00043 #include "kprefsdialog.moc"
00044 
00045 namespace KPrefsWidFactory {
00046 
00047 KPrefsWid *create( KConfigSkeletonItem *item, QWidget *parent )
00048 {
00049   KConfigSkeleton::ItemBool *boolItem =
00050       dynamic_cast<KConfigSkeleton::ItemBool *>( item );
00051   if ( boolItem ) {
00052     return new KPrefsWidBool( boolItem, parent );
00053   }
00054 
00055   KConfigSkeleton::ItemString *stringItem =
00056       dynamic_cast<KConfigSkeleton::ItemString *>( item );
00057   if ( stringItem ) {
00058     return new KPrefsWidString( stringItem, parent );
00059   }
00060   
00061   KConfigSkeleton::ItemEnum *enumItem =
00062       dynamic_cast<KConfigSkeleton::ItemEnum *>( item );
00063   if ( enumItem ) {
00064     QValueList<KConfigSkeleton::ItemEnum::Choice> choices = enumItem->choices();
00065     if ( choices.isEmpty() ) {
00066       kdError() << "KPrefsWidFactory::create(): Enum has no choices." << endl;
00067       return 0;
00068     } else {
00069       KPrefsWidRadios *radios = new KPrefsWidRadios( enumItem, parent );
00070       QValueList<KConfigSkeleton::ItemEnum::Choice>::ConstIterator it;
00071       for( it = choices.begin(); it != choices.end(); ++it ) {
00072         radios->addRadio( (*it).label );
00073       }
00074       return radios;
00075     }
00076   }
00077   
00078   KConfigSkeleton::ItemInt *intItem =
00079       dynamic_cast<KConfigSkeleton::ItemInt *>( item );
00080   if ( intItem ) {
00081     return new KPrefsWidInt( intItem, parent );
00082   }
00083   
00084   return 0;
00085 }
00086 
00087 }
00088 
00089 
00090 QValueList<QWidget *> KPrefsWid::widgets() const
00091 {
00092   return QValueList<QWidget *>();
00093 }
00094 
00095 
00096 KPrefsWidBool::KPrefsWidBool( KConfigSkeleton::ItemBool *item, QWidget *parent )
00097   : mItem( item )
00098 {
00099   mCheck = new QCheckBox( item->label(), parent);
00100   connect( mCheck, SIGNAL( clicked() ), SIGNAL( changed() ) );
00101   if ( !item->whatsThis().isNull() ) {
00102     QWhatsThis::add( mCheck, item->whatsThis() );
00103   }
00104 }
00105 
00106 void KPrefsWidBool::readConfig()
00107 {
00108   mCheck->setChecked( mItem->value() );
00109 }
00110 
00111 void KPrefsWidBool::writeConfig()
00112 {
00113   mItem->setValue( mCheck->isChecked() );
00114 }
00115 
00116 QCheckBox *KPrefsWidBool::checkBox()
00117 {
00118   return mCheck;
00119 }
00120 
00121 QValueList<QWidget *> KPrefsWidBool::widgets() const
00122 {
00123   QValueList<QWidget *> widgets;
00124   widgets.append( mCheck );
00125   return widgets;
00126 }
00127 
00128 
00129 KPrefsWidInt::KPrefsWidInt( KConfigSkeleton::ItemInt *item,
00130                             QWidget *parent )
00131   : mItem( item )
00132 {
00133   mLabel = new QLabel( mItem->label(), parent );
00134   mSpin = new QSpinBox( parent );
00135   connect( mSpin, SIGNAL( valueChanged( int ) ), SIGNAL( changed() ) );
00136   mLabel->setBuddy( mSpin );
00137   QString whatsThis = mItem->whatsThis();
00138   if ( !whatsThis.isEmpty() ) {
00139     QWhatsThis::add( mLabel, whatsThis );
00140     QWhatsThis::add( mSpin, whatsThis );
00141   }
00142 }
00143 
00144 void KPrefsWidInt::readConfig()
00145 {
00146   mSpin->setValue( mItem->value() );
00147 }
00148 
00149 void KPrefsWidInt::writeConfig()
00150 {
00151   mItem->setValue( mSpin->value() );
00152 }
00153 
00154 QLabel *KPrefsWidInt::label()
00155 {
00156   return mLabel;
00157 }
00158 
00159 QSpinBox *KPrefsWidInt::spinBox()
00160 {
00161   return mSpin;
00162 }
00163 
00164 QValueList<QWidget *> KPrefsWidInt::widgets() const
00165 {
00166   QValueList<QWidget *> widgets;
00167   widgets.append( mLabel );
00168   widgets.append( mSpin );
00169   return widgets;
00170 }
00171 
00172 
00173 KPrefsWidColor::KPrefsWidColor( KConfigSkeleton::ItemColor *item,
00174                                 QWidget *parent )
00175   : mItem( item )
00176 {
00177   mButton = new KColorButton( parent );
00178   connect( mButton, SIGNAL( changed( const QColor & ) ), SIGNAL( changed() ) );
00179   mLabel = new QLabel( mButton, mItem->label(), parent );
00180   QString whatsThis = mItem->whatsThis();
00181   if (!whatsThis.isNull()) {
00182     QWhatsThis::add(mButton, whatsThis);
00183   }
00184 }
00185 
00186 KPrefsWidColor::~KPrefsWidColor()
00187 {
00188 //  kdDebug(5300) << "KPrefsWidColor::~KPrefsWidColor()" << endl;
00189 }
00190 
00191 void KPrefsWidColor::readConfig()
00192 {
00193   mButton->setColor( mItem->value() );
00194 }
00195 
00196 void KPrefsWidColor::writeConfig()
00197 {
00198   mItem->setValue( mButton->color() );
00199 }
00200 
00201 QLabel *KPrefsWidColor::label()
00202 {
00203   return mLabel;
00204 }
00205 
00206 KColorButton *KPrefsWidColor::button()
00207 {
00208   return mButton;
00209 }
00210 
00211 
00212 KPrefsWidFont::KPrefsWidFont( KConfigSkeleton::ItemFont *item,
00213                               QWidget *parent, const QString &sampleText )
00214   : mItem( item )
00215 {
00216   mLabel = new QLabel( mItem->label(), parent );
00217 
00218   mPreview = new QLabel( sampleText, parent );
00219   mPreview->setFrameStyle( QFrame::Panel | QFrame::Sunken );
00220 
00221   mButton = new QPushButton( i18n("Choose..."), parent );
00222   connect( mButton, SIGNAL( clicked() ), SLOT( selectFont() ) );
00223   QString whatsThis = mItem->whatsThis();
00224   if (!whatsThis.isNull()) {
00225     QWhatsThis::add(mPreview, whatsThis);
00226     QWhatsThis::add(mButton, whatsThis);
00227   }
00228 }
00229 
00230 KPrefsWidFont::~KPrefsWidFont()
00231 {
00232 }
00233 
00234 void KPrefsWidFont::readConfig()
00235 {
00236   mPreview->setFont( mItem->value() );
00237 }
00238 
00239 void KPrefsWidFont::writeConfig()
00240 {
00241   mItem->setValue( mPreview->font() );
00242 }
00243 
00244 QLabel *KPrefsWidFont::label()
00245 {
00246   return mLabel;
00247 }
00248 
00249 QFrame *KPrefsWidFont::preview()
00250 {
00251   return mPreview;
00252 }
00253 
00254 QPushButton *KPrefsWidFont::button()
00255 {
00256   return mButton;
00257 }
00258 
00259 void KPrefsWidFont::selectFont()
00260 {
00261   QFont myFont(mPreview->font());
00262   int result = KFontDialog::getFont(myFont);
00263   if (result == KFontDialog::Accepted) {
00264     mPreview->setFont(myFont);
00265     emit changed();
00266   }
00267 }
00268 
00269 
00270 KPrefsWidTime::KPrefsWidTime( KConfigSkeleton::ItemInt *item,
00271                               QWidget *parent )
00272   : mItem( item )
00273 {
00274   mLabel = new QLabel( mItem->label(), parent );
00275   mSpin = new QSpinBox( 0, 23, 1, parent );
00276   connect( mSpin, SIGNAL( valueChanged( int ) ), SIGNAL( changed() ) );
00277   mSpin->setSuffix( ":00" );
00278   QString whatsThis = mItem->whatsThis();
00279   if ( !whatsThis.isNull() ) {
00280     QWhatsThis::add( mSpin, whatsThis );
00281   }
00282 }
00283 
00284 void KPrefsWidTime::readConfig()
00285 {
00286   mSpin->setValue( mItem->value() );
00287 }
00288 
00289 void KPrefsWidTime::writeConfig()
00290 {
00291   mItem->setValue( mSpin->value() );
00292 }
00293 
00294 QLabel *KPrefsWidTime::label()
00295 {
00296   return mLabel;
00297 }
00298 
00299 QSpinBox *KPrefsWidTime::spinBox()
00300 {
00301   return mSpin;
00302 }
00303 
00304 
00305 KPrefsWidRadios::KPrefsWidRadios( KConfigSkeleton::ItemEnum *item,
00306                                   QWidget *parent )
00307   : mItem( item )
00308 {
00309   mBox = new QButtonGroup( 1, Qt::Horizontal, mItem->label(), parent );
00310   connect( mBox, SIGNAL( clicked( int ) ), SIGNAL( changed() ) );
00311 }
00312 
00313 KPrefsWidRadios::~KPrefsWidRadios()
00314 {
00315 }
00316 
00317 void KPrefsWidRadios::addRadio(const QString &text, const QString &whatsThis)
00318 {
00319   QRadioButton *r = new QRadioButton(text,mBox);
00320   if (!whatsThis.isNull()) {
00321     QWhatsThis::add(r, whatsThis);
00322   }
00323 }
00324 
00325 QButtonGroup *KPrefsWidRadios::groupBox()
00326 {
00327   return mBox;
00328 }
00329 
00330 void KPrefsWidRadios::readConfig()
00331 {
00332   mBox->setButton( mItem->value() );
00333 }
00334 
00335 void KPrefsWidRadios::writeConfig()
00336 {
00337   mItem->setValue( mBox->id( mBox->selected() ) );
00338 }
00339 
00340 QValueList<QWidget *> KPrefsWidRadios::widgets() const
00341 {
00342   QValueList<QWidget *> w;
00343   w.append( mBox );
00344   return w;
00345 }
00346 
00347 
00348 KPrefsWidString::KPrefsWidString( KConfigSkeleton::ItemString *item,
00349                                   QWidget *parent,
00350                                   QLineEdit::EchoMode echomode )
00351   : mItem( item )
00352 {
00353   mLabel = new QLabel( mItem->label(), parent );
00354   mEdit = new QLineEdit( parent );
00355   connect( mEdit, SIGNAL( textChanged( const QString & ) ),
00356            SIGNAL( changed() ) );
00357   mEdit->setEchoMode( echomode );
00358   QString whatsThis = mItem->whatsThis();
00359   if ( !whatsThis.isNull() ) {
00360     QWhatsThis::add( mEdit, whatsThis );
00361   }
00362 }
00363 
00364 KPrefsWidString::~KPrefsWidString()
00365 {
00366 }
00367 
00368 void KPrefsWidString::readConfig()
00369 {
00370   mEdit->setText( mItem->value() );
00371 }
00372 
00373 void KPrefsWidString::writeConfig()
00374 {
00375   mItem->setValue( mEdit->text() );
00376 }
00377 
00378 QLabel *KPrefsWidString::label()
00379 {
00380   return mLabel;
00381 }
00382 
00383 QLineEdit *KPrefsWidString::lineEdit()
00384 {
00385   return mEdit;
00386 }
00387 
00388 QValueList<QWidget *> KPrefsWidString::widgets() const
00389 {
00390   QValueList<QWidget *> widgets;
00391   widgets.append( mLabel );
00392   widgets.append( mEdit );
00393   return widgets;
00394 }
00395 
00396 
00397 KPrefsWidManager::KPrefsWidManager( KConfigSkeleton *prefs )
00398   : mPrefs( prefs )
00399 {
00400 }
00401 
00402 KPrefsWidManager::~KPrefsWidManager()
00403 {
00404 }
00405 
00406 void KPrefsWidManager::addWid( KPrefsWid *wid )
00407 {
00408   mPrefsWids.append( wid );
00409 }
00410 
00411 KPrefsWidBool *KPrefsWidManager::addWidBool( KConfigSkeleton::ItemBool *item,
00412                                              QWidget *parent )
00413 {
00414   KPrefsWidBool *w = new KPrefsWidBool( item, parent );
00415   addWid( w );
00416   return w;
00417 }
00418 
00419 KPrefsWidTime *KPrefsWidManager::addWidTime( KConfigSkeleton::ItemInt *item,
00420                                              QWidget *parent )
00421 {
00422   KPrefsWidTime *w = new KPrefsWidTime( item, parent );
00423   addWid( w );
00424   return w;
00425 }
00426 
00427 KPrefsWidColor *KPrefsWidManager::addWidColor( KConfigSkeleton::ItemColor *item,
00428                                                QWidget *parent )
00429 {
00430   KPrefsWidColor *w = new KPrefsWidColor( item, parent );
00431   addWid( w );
00432   return w;
00433 }
00434 
00435 KPrefsWidRadios *KPrefsWidManager::addWidRadios( KConfigSkeleton::ItemEnum *item,
00436                                                  QWidget *parent )
00437 {
00438   KPrefsWidRadios *w = new KPrefsWidRadios( item, parent );
00439   QValueList<KConfigSkeleton::ItemEnum::Choice> choices;
00440   choices = item->choices();
00441   QValueList<KConfigSkeleton::ItemEnum::Choice>::ConstIterator it;
00442   for( it = choices.begin(); it != choices.end(); ++it ) {
00443     w->addRadio( (*it).label, (*it).whatsThis );
00444   }
00445   addWid( w );
00446   return w;
00447 }
00448 
00449 KPrefsWidString *KPrefsWidManager::addWidString( KConfigSkeleton::ItemString *item,
00450                                                  QWidget *parent )
00451 {
00452   KPrefsWidString *w = new KPrefsWidString( item, parent,
00453                                             QLineEdit::Normal );
00454   addWid( w );
00455   return w;
00456 }
00457 
00458 KPrefsWidString *KPrefsWidManager::addWidPassword( KConfigSkeleton::ItemString *item,
00459                                                    QWidget *parent )
00460 {
00461   KPrefsWidString *w = new KPrefsWidString( item, parent, QLineEdit::Password );
00462   addWid( w );
00463   return w;
00464 }
00465 
00466 KPrefsWidFont *KPrefsWidManager::addWidFont( KConfigSkeleton::ItemFont *item,
00467                                              QWidget *parent,
00468                                              const QString &sampleText )
00469 {
00470   KPrefsWidFont *w = new KPrefsWidFont( item, parent, sampleText );
00471   addWid( w );
00472   return w;
00473 }
00474 
00475 void KPrefsWidManager::setWidDefaults()
00476 {
00477   kdDebug() << "KPrefsWidManager::setWidDefaults()" << endl;
00478 
00479   bool tmp = mPrefs->useDefaults( true );
00480 
00481   readWidConfig();
00482 
00483   mPrefs->useDefaults( tmp );
00484 }
00485 
00486 void KPrefsWidManager::readWidConfig()
00487 {
00488   kdDebug(5310) << "KPrefsWidManager::readWidConfig()" << endl;
00489 
00490   KPrefsWid *wid;
00491   for( wid = mPrefsWids.first(); wid; wid = mPrefsWids.next() ) {
00492     wid->readConfig();
00493   }
00494 }
00495 
00496 void KPrefsWidManager::writeWidConfig()
00497 {
00498   kdDebug(5310) << "KPrefsWidManager::writeWidConfig()" << endl;
00499 
00500   KPrefsWid *wid;
00501   for( wid = mPrefsWids.first(); wid; wid = mPrefsWids.next() ) {
00502     wid->writeConfig();
00503   }
00504 
00505   mPrefs->writeConfig();
00506 }
00507 
00508 
00509 KPrefsDialog::KPrefsDialog( KConfigSkeleton *prefs, QWidget *parent, char *name,
00510                             bool modal )
00511   : KDialogBase(IconList,i18n("Preferences"),Ok|Apply|Cancel|Default,Ok,parent,
00512                 name,modal,true),
00513     KPrefsWidManager( prefs )
00514 {
00515 // TODO: This seems to cause a crash on exit. Investigate later.
00516 //  mPrefsWids.setAutoDelete(true);
00517 
00518 //  connect(this,SIGNAL(defaultClicked()),SLOT(setDefaults()));
00519   connect(this,SIGNAL(cancelClicked()),SLOT(reject()));
00520 }
00521 
00522 KPrefsDialog::~KPrefsDialog()
00523 {
00524 }
00525 
00526 void KPrefsDialog::autoCreate()
00527 {
00528   KConfigSkeletonItem::List items = prefs()->items();
00529   
00530   QMap<QString,QWidget *> mGroupPages;
00531   QMap<QString,QGridLayout *> mGroupLayouts;
00532   QMap<QString,int> mCurrentRows;
00533   
00534   KConfigSkeletonItem::List::ConstIterator it;
00535   for( it = items.begin(); it != items.end(); ++it ) {
00536     QString group = (*it)->group();
00537     QString name = (*it)->name();
00538 
00539     kdDebug() << "ITEMS: " << (*it)->name() << endl;
00540 
00541     QWidget *page;
00542     QGridLayout *layout;
00543     int currentRow;
00544     if ( !mGroupPages.contains( group ) ) {
00545       page = addPage( group );
00546       layout = new QGridLayout( page );
00547       mGroupPages.insert( group, page );
00548       mGroupLayouts.insert( group, layout );
00549       currentRow = 0;
00550       mCurrentRows.insert( group, currentRow );
00551     } else {
00552       page = mGroupPages[ group ];
00553       layout = mGroupLayouts[ group ];
00554       currentRow = mCurrentRows[ group ];
00555     }
00556 
00557     KPrefsWid *wid = KPrefsWidFactory::create( *it, page );
00558     
00559     if ( wid ) {
00560       QValueList<QWidget *> widgets = wid->widgets();
00561       if ( widgets.count() == 1 ) {
00562         layout->addMultiCellWidget( widgets[ 0 ],
00563                                     currentRow, currentRow, 0, 1 );
00564       } else if ( widgets.count() == 2 ) {
00565         layout->addWidget( widgets[ 0 ], currentRow, 0 );
00566         layout->addWidget( widgets[ 1 ], currentRow, 1 );      
00567       } else {
00568         kdError() << "More widgets than expected: " << widgets.count() << endl;
00569       }
00570   
00571       if ( (*it)->isImmutable() ) {
00572         QValueList<QWidget *>::Iterator it2;
00573         for( it2 = widgets.begin(); it2 != widgets.end(); ++it2 ) {
00574           (*it2)->setEnabled( false );
00575         }
00576       }
00577   
00578       addWid( wid );
00579   
00580       mCurrentRows.replace( group, ++currentRow );
00581     }
00582   }
00583   
00584   readConfig();
00585 }
00586 
00587 void KPrefsDialog::setDefaults()
00588 {
00589   setWidDefaults();
00590 }
00591 
00592 void KPrefsDialog::readConfig()
00593 {
00594   readWidConfig();
00595 
00596   usrReadConfig();
00597 }
00598 
00599 void KPrefsDialog::writeConfig()
00600 {
00601   writeWidConfig();
00602 
00603   usrWriteConfig();
00604 
00605   readConfig();
00606 }
00607 
00608 
00609 void KPrefsDialog::slotApply()
00610 {
00611   writeConfig();
00612   emit configChanged();
00613 }
00614 
00615 void KPrefsDialog::slotOk()
00616 {
00617   slotApply();
00618   accept();
00619 }
00620 
00621 void KPrefsDialog::slotDefault()
00622 {
00623   kdDebug() << "KPrefsDialog::slotDefault()" << endl;
00624 
00625   if (KMessageBox::warningContinueCancel(this,
00626       i18n("You are about to set all preferences to default values. All "
00627       "custom modifications will be lost."),i18n("Setting Default Preferences"),
00628       i18n("Continue"))
00629     == KMessageBox::Continue) setDefaults();
00630 }
00631 
00632 
00633 KPrefsModule::KPrefsModule( KConfigSkeleton *prefs, QWidget *parent,
00634                             const char *name )
00635   : KCModule( parent, name ),
00636     KPrefsWidManager( prefs )
00637 {
00638   emit changed( false );
00639 }
00640 
00641 void KPrefsModule::addWid( KPrefsWid *wid )
00642 {
00643   KPrefsWidManager::addWid( wid );
00644 
00645   connect( wid, SIGNAL( changed() ), SLOT( slotWidChanged() ) );
00646 }
00647 
00648 void KPrefsModule::slotWidChanged()
00649 {
00650   kdDebug(5310) << "KPrefsModule::slotWidChanged()" << endl;
00651 
00652   emit changed( true );
00653 }
00654 
00655 void KPrefsModule::load()
00656 {
00657   kdDebug(5310) << "KPrefsModule::load()" << endl;
00658 
00659   readWidConfig();
00660 
00661   usrReadConfig();
00662 
00663   emit changed( false );
00664 }
00665 
00666 void KPrefsModule::save()
00667 {
00668   kdDebug(5310) << "KPrefsModule::save()" << endl;
00669 
00670   writeWidConfig();
00671 
00672   usrWriteConfig();
00673 }
00674 
00675 void KPrefsModule::defaults()
00676 {
00677   setWidDefaults();
00678 
00679   emit changed( true );
00680 }
KDE Logo
This file is part of the documentation for libkdepim Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat May 1 11:36:31 2004 by doxygen 1.2.15 written by Dimitri van Heesch, © 1997-2003