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