libkdepim

kprefsdialog.cpp

00001 /*
00002     This file is part of libkdepim.
00003 
00004     Copyright (c) 2001,2003 Cornelius Schumacher <schumacher@kde.org>
00005     Copyright (C) 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com>
00006     Copyright (C) 2005 Allen Winter <winter@kde.org>
00007 
00008     This library is free software; you can redistribute it and/or
00009     modify it under the terms of the GNU Library General Public
00010     License as published by the Free Software Foundation; either
00011     version 2 of the License, or (at your option) any later version.
00012 
00013     This library is distributed in the hope that it will be useful,
00014     but WITHOUT ANY WARRANTY; without even the implied warranty of
00015     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016     Library General Public License for more details.
00017 
00018     You should have received a copy of the GNU Library General Public License
00019     along with this library; see the file COPYING.LIB.  If not, write to
00020     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00021     Boston, MA 02110-1301, USA.
00022 */
00023 
00024 #include <qlayout.h>
00025 #include <qlabel.h>
00026 #include <qbuttongroup.h>
00027 #include <qlineedit.h>
00028 #include <qfont.h>
00029 #include <qspinbox.h>
00030 #include <qframe.h>
00031 #include <qcombobox.h>
00032 #include <qcheckbox.h>
00033 #include <qradiobutton.h>
00034 #include <qpushbutton.h>
00035 #include <qdatetimeedit.h>
00036 #include <qwhatsthis.h>
00037 
00038 #include <kcolorbutton.h>
00039 #include <kdebug.h>
00040 #include <klocale.h>
00041 #include <kfontdialog.h>
00042 #include <kmessagebox.h>
00043 #include <kconfigskeleton.h>
00044 #include <kurlrequester.h>
00045 #include "ktimeedit.h"
00046 #include "kdateedit.h"
00047 
00048 #include "kprefsdialog.h"
00049 #include "kprefsdialog.moc"
00050 
00051 namespace KPrefsWidFactory {
00052 
00053 KPrefsWid *create( KConfigSkeletonItem *item, QWidget *parent )
00054 {
00055   KConfigSkeleton::ItemBool *boolItem =
00056       dynamic_cast<KConfigSkeleton::ItemBool *>( item );
00057   if ( boolItem ) {
00058     return new KPrefsWidBool( boolItem, parent );
00059   }
00060 
00061   KConfigSkeleton::ItemString *stringItem =
00062       dynamic_cast<KConfigSkeleton::ItemString *>( item );
00063   if ( stringItem ) {
00064     return new KPrefsWidString( stringItem, parent );
00065   }
00066 
00067   KConfigSkeleton::ItemEnum *enumItem =
00068       dynamic_cast<KConfigSkeleton::ItemEnum *>( item );
00069   if ( enumItem ) {
00070     QValueList<KConfigSkeleton::ItemEnum::Choice> choices = enumItem->choices();
00071     if ( choices.isEmpty() ) {
00072       kdError() << "KPrefsWidFactory::create(): Enum has no choices." << endl;
00073       return 0;
00074     } else {
00075       KPrefsWidRadios *radios = new KPrefsWidRadios( enumItem, parent );
00076       QValueList<KConfigSkeleton::ItemEnum::Choice>::ConstIterator it;
00077       for( it = choices.begin(); it != choices.end(); ++it ) {
00078         radios->addRadio( (*it).label );
00079       }
00080       return radios;
00081     }
00082   }
00083 
00084   KConfigSkeleton::ItemInt *intItem =
00085       dynamic_cast<KConfigSkeleton::ItemInt *>( item );
00086   if ( intItem ) {
00087     return new KPrefsWidInt( intItem, parent );
00088   }
00089 
00090   return 0;
00091 }
00092 
00093 }
00094 
00095 
00096 QValueList<QWidget *> KPrefsWid::widgets() const
00097 {
00098   return QValueList<QWidget *>();
00099 }
00100 
00101 
00102 KPrefsWidBool::KPrefsWidBool( KConfigSkeleton::ItemBool *item, QWidget *parent )
00103   : mItem( item )
00104 {
00105   mCheck = new QCheckBox( item->label(), parent);
00106   connect( mCheck, SIGNAL( clicked() ), SIGNAL( changed() ) );
00107   if ( !item->whatsThis().isNull() ) {
00108     QWhatsThis::add( mCheck, item->whatsThis() );
00109   }
00110 }
00111 
00112 void KPrefsWidBool::readConfig()
00113 {
00114   mCheck->setChecked( mItem->value() );
00115 }
00116 
00117 void KPrefsWidBool::writeConfig()
00118 {
00119   mItem->setValue( mCheck->isChecked() );
00120 }
00121 
00122 QCheckBox *KPrefsWidBool::checkBox()
00123 {
00124   return mCheck;
00125 }
00126 
00127 QValueList<QWidget *> KPrefsWidBool::widgets() const
00128 {
00129   QValueList<QWidget *> widgets;
00130   widgets.append( mCheck );
00131   return widgets;
00132 }
00133 
00134 
00135 KPrefsWidInt::KPrefsWidInt( KConfigSkeleton::ItemInt *item,
00136                             QWidget *parent )
00137   : mItem( item )
00138 {
00139   mLabel = new QLabel( mItem->label()+':', parent );
00140   mSpin = new QSpinBox( parent );
00141   if ( !item->minValue().isNull() ) {
00142     mSpin->setMinValue( item->minValue().toInt() );
00143   }
00144   if ( !item->maxValue().isNull() ) {
00145     mSpin->setMaxValue( item->maxValue().toInt() );
00146   }
00147   connect( mSpin, SIGNAL( valueChanged( int ) ), SIGNAL( changed() ) );
00148   mLabel->setBuddy( mSpin );
00149   QString whatsThis = mItem->whatsThis();
00150   if ( !whatsThis.isEmpty() ) {
00151     QWhatsThis::add( mLabel, whatsThis );
00152     QWhatsThis::add( mSpin, whatsThis );
00153   }
00154 }
00155 
00156 void KPrefsWidInt::readConfig()
00157 {
00158   mSpin->setValue( mItem->value() );
00159 }
00160 
00161 void KPrefsWidInt::writeConfig()
00162 {
00163   mItem->setValue( mSpin->value() );
00164 }
00165 
00166 QLabel *KPrefsWidInt::label()
00167 {
00168   return mLabel;
00169 }
00170 
00171 QSpinBox *KPrefsWidInt::spinBox()
00172 {
00173   return mSpin;
00174 }
00175 
00176 QValueList<QWidget *> KPrefsWidInt::widgets() const
00177 {
00178   QValueList<QWidget *> widgets;
00179   widgets.append( mLabel );
00180   widgets.append( mSpin );
00181   return widgets;
00182 }
00183 
00184 
00185 KPrefsWidColor::KPrefsWidColor( KConfigSkeleton::ItemColor *item,
00186                                 QWidget *parent )
00187   : mItem( item )
00188 {
00189   mButton = new KColorButton( parent );
00190   connect( mButton, SIGNAL( changed( const QColor & ) ), SIGNAL( changed() ) );
00191   mLabel = new QLabel( mButton, mItem->label()+':', parent );
00192   mLabel->setBuddy( mButton );
00193   QString whatsThis = mItem->whatsThis();
00194   if (!whatsThis.isNull()) {
00195     QWhatsThis::add(mButton, whatsThis);
00196   }
00197 }
00198 
00199 KPrefsWidColor::~KPrefsWidColor()
00200 {
00201 //  kdDebug(5300) << "KPrefsWidColor::~KPrefsWidColor()" << endl;
00202 }
00203 
00204 void KPrefsWidColor::readConfig()
00205 {
00206   mButton->setColor( mItem->value() );
00207 }
00208 
00209 void KPrefsWidColor::writeConfig()
00210 {
00211   mItem->setValue( mButton->color() );
00212 }
00213 
00214 QLabel *KPrefsWidColor::label()
00215 {
00216   return mLabel;
00217 }
00218 
00219 KColorButton *KPrefsWidColor::button()
00220 {
00221   return mButton;
00222 }
00223 
00224 
00225 KPrefsWidFont::KPrefsWidFont( KConfigSkeleton::ItemFont *item,
00226                               QWidget *parent, const QString &sampleText )
00227   : mItem( item )
00228 {
00229   mLabel = new QLabel( mItem->label()+':', parent );
00230 
00231   mPreview = new QLabel( sampleText, parent );
00232   mPreview->setFrameStyle( QFrame::Panel | QFrame::Sunken );
00233 
00234   mButton = new QPushButton( i18n("Choose..."), parent );
00235   connect( mButton, SIGNAL( clicked() ), SLOT( selectFont() ) );
00236   QString whatsThis = mItem->whatsThis();
00237   if (!whatsThis.isNull()) {
00238     QWhatsThis::add(mPreview, whatsThis);
00239     QWhatsThis::add(mButton, whatsThis);
00240   }
00241 }
00242 
00243 KPrefsWidFont::~KPrefsWidFont()
00244 {
00245 }
00246 
00247 void KPrefsWidFont::readConfig()
00248 {
00249   mPreview->setFont( mItem->value() );
00250 }
00251 
00252 void KPrefsWidFont::writeConfig()
00253 {
00254   mItem->setValue( mPreview->font() );
00255 }
00256 
00257 QLabel *KPrefsWidFont::label()
00258 {
00259   return mLabel;
00260 }
00261 
00262 QFrame *KPrefsWidFont::preview()
00263 {
00264   return mPreview;
00265 }
00266 
00267 QPushButton *KPrefsWidFont::button()
00268 {
00269   return mButton;
00270 }
00271 
00272 void KPrefsWidFont::selectFont()
00273 {
00274   QFont myFont(mPreview->font());
00275   int result = KFontDialog::getFont(myFont);
00276   if (result == KFontDialog::Accepted) {
00277     mPreview->setFont(myFont);
00278     emit changed();
00279   }
00280 }
00281 
00282 
00283 KPrefsWidTime::KPrefsWidTime( KConfigSkeleton::ItemDateTime *item,
00284                               QWidget *parent )
00285   : mItem( item )
00286 {
00287   mLabel = new QLabel( mItem->label()+':', parent );
00288   mTimeEdit = new KTimeEdit( parent );
00289   mLabel->setBuddy( mTimeEdit );
00290   connect( mTimeEdit, SIGNAL( timeChanged( QTime ) ), SIGNAL( changed() ) );
00291   QString whatsThis = mItem->whatsThis();
00292   if ( !whatsThis.isNull() ) {
00293     QWhatsThis::add( mTimeEdit, whatsThis );
00294   }
00295 }
00296 
00297 void KPrefsWidTime::readConfig()
00298 {
00299   mTimeEdit->setTime( mItem->value().time() );
00300 }
00301 
00302 void KPrefsWidTime::writeConfig()
00303 {
00304   // Don't overwrite the date value of the QDateTime, so we can use a
00305   // KPrefsWidTime and a KPrefsWidDate on the same config entry!
00306   QDateTime dt( mItem->value() );
00307   dt.setTime( mTimeEdit->getTime() );
00308   mItem->setValue( dt );
00309 }
00310 
00311 QLabel *KPrefsWidTime::label()
00312 {
00313   return mLabel;
00314 }
00315 
00316 KTimeEdit *KPrefsWidTime::timeEdit()
00317 {
00318   return mTimeEdit;
00319 }
00320 
00321 
00322 KPrefsWidDuration::KPrefsWidDuration( KConfigSkeleton::ItemDateTime *item,
00323                                       QWidget *parent )
00324   : mItem( item )
00325 {
00326   mLabel = new QLabel( mItem->label()+':', parent );
00327   mTimeEdit = new QTimeEdit( parent );
00328   mLabel->setBuddy( mTimeEdit );
00329   mTimeEdit->setAutoAdvance( true );
00330   mTimeEdit->setDisplay( QTimeEdit::Hours | QTimeEdit::Minutes );
00331   mTimeEdit->setRange( QTime( 0, 1 ), QTime( 24, 0 ) ); // [1min, 24hr]
00332   connect( mTimeEdit,
00333            SIGNAL( valueChanged( const QTime & ) ), SIGNAL( changed() ) );
00334   QString whatsThis = mItem->whatsThis();
00335   if ( !whatsThis.isNull() ) {
00336     QWhatsThis::add( mTimeEdit, whatsThis );
00337   }
00338 }
00339 
00340 void KPrefsWidDuration::readConfig()
00341 {
00342   mTimeEdit->setTime( mItem->value().time() );
00343 }
00344 
00345 void KPrefsWidDuration::writeConfig()
00346 {
00347   QDateTime dt( mItem->value() );
00348   dt.setTime( mTimeEdit->time() );
00349   mItem->setValue( dt );
00350 }
00351 
00352 QLabel *KPrefsWidDuration::label()
00353 {
00354   return mLabel;
00355 }
00356 
00357 QTimeEdit *KPrefsWidDuration::timeEdit()
00358 {
00359   return mTimeEdit;
00360 }
00361 
00362 
00363 KPrefsWidDate::KPrefsWidDate( KConfigSkeleton::ItemDateTime *item,
00364                               QWidget *parent )
00365   : mItem( item )
00366 {
00367   mLabel = new QLabel( mItem->label()+':', parent );
00368   mDateEdit = new KDateEdit( parent );
00369   mLabel->setBuddy( mDateEdit );
00370   connect( mDateEdit, SIGNAL( dateChanged( const QDate& ) ), SIGNAL( changed() ) );
00371   QString whatsThis = mItem->whatsThis();
00372   if ( !whatsThis.isNull() ) {
00373     QWhatsThis::add( mDateEdit, whatsThis );
00374   }
00375 }
00376 
00377 void KPrefsWidDate::readConfig()
00378 {
00379   mDateEdit->setDate( mItem->value().date().isValid() ? mItem->value().date() : QDate::currentDate()  );
00380 }
00381 
00382 void KPrefsWidDate::writeConfig()
00383 {
00384   QDateTime dt( mItem->value() );
00385   dt.setDate( mDateEdit->date() );
00386   mItem->setValue( dt );
00387 }
00388 
00389 QLabel *KPrefsWidDate::label()
00390 {
00391   return mLabel;
00392 }
00393 
00394 KDateEdit *KPrefsWidDate::dateEdit()
00395 {
00396   return mDateEdit;
00397 }
00398 
00399 
00400 KPrefsWidRadios::KPrefsWidRadios( KConfigSkeleton::ItemEnum *item,
00401                                   QWidget *parent )
00402   : mItem( item )
00403 {
00404   mBox = new QButtonGroup( 1, Qt::Horizontal, mItem->label(), parent );
00405   connect( mBox, SIGNAL( clicked( int ) ), SIGNAL( changed() ) );
00406 }
00407 
00408 KPrefsWidRadios::~KPrefsWidRadios()
00409 {
00410 }
00411 
00412 void KPrefsWidRadios::addRadio(const QString &text, const QString &whatsThis)
00413 {
00414   QRadioButton *r = new QRadioButton(text,mBox);
00415   if (!whatsThis.isNull()) {
00416     QWhatsThis::add(r, whatsThis);
00417   }
00418 }
00419 
00420 QButtonGroup *KPrefsWidRadios::groupBox()
00421 {
00422   return mBox;
00423 }
00424 
00425 void KPrefsWidRadios::readConfig()
00426 {
00427   mBox->setButton( mItem->value() );
00428 }
00429 
00430 void KPrefsWidRadios::writeConfig()
00431 {
00432   mItem->setValue( mBox->id( mBox->selected() ) );
00433 }
00434 
00435 QValueList<QWidget *> KPrefsWidRadios::widgets() const
00436 {
00437   QValueList<QWidget *> w;
00438   w.append( mBox );
00439   return w;
00440 }
00441 
00442 KPrefsWidCombo::KPrefsWidCombo( KConfigSkeleton::ItemEnum *item,
00443                                   QWidget *parent )
00444   : mItem( item )
00445 {
00446   QHBox *hbox = new QHBox(parent);
00447   new QLabel( mItem->label(), hbox );
00448   mCombo = new QComboBox( hbox );
00449   connect( mCombo, SIGNAL( activated( int ) ), SIGNAL( changed() ) );
00450 }
00451 
00452 KPrefsWidCombo::~KPrefsWidCombo()
00453 {
00454 }
00455 
00456 void KPrefsWidCombo::readConfig()
00457 {
00458   mCombo->setCurrentItem( mItem->value() );
00459 }
00460 
00461 void KPrefsWidCombo::writeConfig()
00462 {
00463   mItem->setValue( mCombo->currentItem() );
00464 }
00465 
00466 QValueList<QWidget *> KPrefsWidCombo::widgets() const
00467 {
00468   QValueList<QWidget *> w;
00469   w.append( mCombo );
00470   return w;
00471 }
00472 
00473 QComboBox* KPrefsWidCombo::comboBox()
00474 {
00475   return mCombo;
00476 }
00477 
00478 KPrefsWidString::KPrefsWidString( KConfigSkeleton::ItemString *item,
00479                                   QWidget *parent,
00480                                   QLineEdit::EchoMode echomode )
00481   : mItem( item )
00482 {
00483   mLabel = new QLabel( mItem->label()+':', parent );
00484   mEdit = new QLineEdit( parent );
00485   mLabel->setBuddy( mEdit );
00486   connect( mEdit, SIGNAL( textChanged( const QString & ) ),
00487            SIGNAL( changed() ) );
00488   mEdit->setEchoMode( echomode );
00489   QString whatsThis = mItem->whatsThis();
00490   if ( !whatsThis.isNull() ) {
00491     QWhatsThis::add( mEdit, whatsThis );
00492   }
00493 }
00494 
00495 KPrefsWidString::~KPrefsWidString()
00496 {
00497 }
00498 
00499 void KPrefsWidString::readConfig()
00500 {
00501   mEdit->setText( mItem->value() );
00502 }
00503 
00504 void KPrefsWidString::writeConfig()
00505 {
00506   mItem->setValue( mEdit->text() );
00507 }
00508 
00509 QLabel *KPrefsWidString::label()
00510 {
00511   return mLabel;
00512 }
00513 
00514 QLineEdit *KPrefsWidString::lineEdit()
00515 {
00516   return mEdit;
00517 }
00518 
00519 QValueList<QWidget *> KPrefsWidString::widgets() const
00520 {
00521   QValueList<QWidget *> widgets;
00522   widgets.append( mLabel );
00523   widgets.append( mEdit );
00524   return widgets;
00525 }
00526 
00527 
00528 KPrefsWidPath::KPrefsWidPath( KConfigSkeleton::ItemPath *item, QWidget *parent,
00529                               const QString &filter, uint mode )
00530   : mItem( item )
00531 {
00532   mLabel = new QLabel( mItem->label()+':', parent );
00533   mURLRequester = new KURLRequester( parent );
00534   mLabel->setBuddy( mURLRequester );
00535   mURLRequester->setMode( mode );
00536   mURLRequester->setFilter( filter );
00537   connect( mURLRequester, SIGNAL( textChanged( const QString & ) ),
00538            SIGNAL( changed() ) );
00539   QString whatsThis = mItem->whatsThis();
00540   if ( !whatsThis.isNull() ) {
00541     QWhatsThis::add( mURLRequester, whatsThis );
00542   }
00543 }
00544 
00545 KPrefsWidPath::~KPrefsWidPath()
00546 {
00547 }
00548 
00549 void KPrefsWidPath::readConfig()
00550 {
00551   mURLRequester->setURL( mItem->value() );
00552 }
00553 
00554 void KPrefsWidPath::writeConfig()
00555 {
00556   mItem->setValue( mURLRequester->url() );
00557 }
00558 
00559 QLabel *KPrefsWidPath::label()
00560 {
00561   return mLabel;
00562 }
00563 
00564 KURLRequester *KPrefsWidPath::urlRequester()
00565 {
00566   return mURLRequester;
00567 }
00568 
00569 QValueList<QWidget *> KPrefsWidPath::widgets() const
00570 {
00571   QValueList<QWidget *> widgets;
00572   widgets.append( mLabel );
00573   widgets.append( mURLRequester );
00574   return widgets;
00575 }
00576 
00577 
00578 KPrefsWidManager::KPrefsWidManager( KConfigSkeleton *prefs )
00579   : mPrefs( prefs )
00580 {
00581 }
00582 
00583 KPrefsWidManager::~KPrefsWidManager()
00584 {
00585 }
00586 
00587 void KPrefsWidManager::addWid( KPrefsWid *wid )
00588 {
00589   mPrefsWids.append( wid );
00590 }
00591 
00592 KPrefsWidBool *KPrefsWidManager::addWidBool( KConfigSkeleton::ItemBool *item,
00593                                              QWidget *parent )
00594 {
00595   KPrefsWidBool *w = new KPrefsWidBool( item, parent );
00596   addWid( w );
00597   return w;
00598 }
00599 
00600 KPrefsWidTime *KPrefsWidManager::addWidTime( KConfigSkeleton::ItemDateTime *item,
00601                                              QWidget *parent )
00602 {
00603   KPrefsWidTime *w = new KPrefsWidTime( item, parent );
00604   addWid( w );
00605   return w;
00606 }
00607 
00608 KPrefsWidDuration *KPrefsWidManager::addWidDuration( KConfigSkeleton::ItemDateTime *item,
00609                                                      QWidget *parent )
00610 {
00611   KPrefsWidDuration *w = new KPrefsWidDuration( item, parent );
00612   addWid( w );
00613   return w;
00614 }
00615 
00616 KPrefsWidDate *KPrefsWidManager::addWidDate( KConfigSkeleton::ItemDateTime *item,
00617                                              QWidget *parent )
00618 {
00619   KPrefsWidDate *w = new KPrefsWidDate( item, parent );
00620   addWid( w );
00621   return w;
00622 }
00623 
00624 KPrefsWidColor *KPrefsWidManager::addWidColor( KConfigSkeleton::ItemColor *item,
00625                                                QWidget *parent )
00626 {
00627   KPrefsWidColor *w = new KPrefsWidColor( item, parent );
00628   addWid( w );
00629   return w;
00630 }
00631 
00632 KPrefsWidRadios *KPrefsWidManager::addWidRadios( KConfigSkeleton::ItemEnum *item,
00633                                                  QWidget *parent )
00634 {
00635   KPrefsWidRadios *w = new KPrefsWidRadios( item, parent );
00636   QValueList<KConfigSkeleton::ItemEnum::Choice> choices;
00637   choices = item->choices();
00638   QValueList<KConfigSkeleton::ItemEnum::Choice>::ConstIterator it;
00639   for( it = choices.begin(); it != choices.end(); ++it ) {
00640     w->addRadio( (*it).label, (*it).whatsThis );
00641   }
00642   addWid( w );
00643   return w;
00644 }
00645 
00646 KPrefsWidCombo *KPrefsWidManager::addWidCombo( KConfigSkeleton::ItemEnum *item,
00647                                                QWidget* parent )
00648 {
00649   KPrefsWidCombo *w = new KPrefsWidCombo( item, parent );
00650   QValueList<KConfigSkeleton::ItemEnum::Choice> choices;
00651   choices = item->choices();
00652   QValueList<KConfigSkeleton::ItemEnum::Choice>::ConstIterator it;
00653   for( it = choices.begin(); it != choices.end(); ++it ) {
00654     w->comboBox()->insertItem( (*it).label, -1 );
00655   }
00656   addWid( w );
00657   return w;
00658 }
00659 
00660 KPrefsWidString *KPrefsWidManager::addWidString( KConfigSkeleton::ItemString *item,
00661                                                  QWidget *parent )
00662 {
00663   KPrefsWidString *w = new KPrefsWidString( item, parent,
00664                                             QLineEdit::Normal );
00665   addWid( w );
00666   return w;
00667 }
00668 
00669 KPrefsWidPath *KPrefsWidManager::addWidPath( KConfigSkeleton::ItemPath *item,
00670                                              QWidget *parent, const QString &filter, uint mode )
00671 {
00672   KPrefsWidPath *w = new KPrefsWidPath( item, parent, filter, mode );
00673   addWid( w );
00674   return w;
00675 }
00676 
00677 KPrefsWidString *KPrefsWidManager::addWidPassword( KConfigSkeleton::ItemString *item,
00678                                                    QWidget *parent )
00679 {
00680   KPrefsWidString *w = new KPrefsWidString( item, parent, QLineEdit::Password );
00681   addWid( w );
00682   return w;
00683 }
00684 
00685 KPrefsWidFont *KPrefsWidManager::addWidFont( KConfigSkeleton::ItemFont *item,
00686                                              QWidget *parent,
00687                                              const QString &sampleText )
00688 {
00689   KPrefsWidFont *w = new KPrefsWidFont( item, parent, sampleText );
00690   addWid( w );
00691   return w;
00692 }
00693 
00694 KPrefsWidInt *KPrefsWidManager::addWidInt( KConfigSkeleton::ItemInt *item,
00695                                            QWidget *parent )
00696 {
00697   KPrefsWidInt *w = new KPrefsWidInt( item, parent );
00698   addWid( w );
00699   return w;
00700 }
00701 
00702 void KPrefsWidManager::setWidDefaults()
00703 {
00704   kdDebug() << "KPrefsWidManager::setWidDefaults()" << endl;
00705 
00706   bool tmp = mPrefs->useDefaults( true );
00707 
00708   readWidConfig();
00709 
00710   mPrefs->useDefaults( tmp );
00711 }
00712 
00713 void KPrefsWidManager::readWidConfig()
00714 {
00715   kdDebug(5310) << "KPrefsWidManager::readWidConfig()" << endl;
00716 
00717   KPrefsWid *wid;
00718   for( wid = mPrefsWids.first(); wid; wid = mPrefsWids.next() ) {
00719     wid->readConfig();
00720   }
00721 }
00722 
00723 void KPrefsWidManager::writeWidConfig()
00724 {
00725   kdDebug(5310) << "KPrefsWidManager::writeWidConfig()" << endl;
00726 
00727   KPrefsWid *wid;
00728   for( wid = mPrefsWids.first(); wid; wid = mPrefsWids.next() ) {
00729     wid->writeConfig();
00730   }
00731 
00732   mPrefs->writeConfig();
00733 }
00734 
00735 
00736 KPrefsDialog::KPrefsDialog( KConfigSkeleton *prefs, QWidget *parent, char *name,
00737                             bool modal )
00738   : KDialogBase(IconList,i18n("Preferences"),Ok|Apply|Cancel|Default,Ok,parent,
00739                 name,modal,true),
00740     KPrefsWidManager( prefs )
00741 {
00742 // TODO: This seems to cause a crash on exit. Investigate later.
00743 //  mPrefsWids.setAutoDelete(true);
00744 
00745 //  connect(this,SIGNAL(defaultClicked()),SLOT(setDefaults()));
00746   connect(this,SIGNAL(cancelClicked()),SLOT(reject()));
00747 }
00748 
00749 KPrefsDialog::~KPrefsDialog()
00750 {
00751 }
00752 
00753 void KPrefsDialog::autoCreate()
00754 {
00755   KConfigSkeletonItem::List items = prefs()->items();
00756 
00757   QMap<QString,QWidget *> mGroupPages;
00758   QMap<QString,QGridLayout *> mGroupLayouts;
00759   QMap<QString,int> mCurrentRows;
00760 
00761   KConfigSkeletonItem::List::ConstIterator it;
00762   for( it = items.begin(); it != items.end(); ++it ) {
00763     QString group = (*it)->group();
00764     QString name = (*it)->name();
00765 
00766     kdDebug() << "ITEMS: " << (*it)->name() << endl;
00767 
00768     QWidget *page;
00769     QGridLayout *layout;
00770     int currentRow;
00771     if ( !mGroupPages.contains( group ) ) {
00772       page = addPage( group );
00773       layout = new QGridLayout( page );
00774       mGroupPages.insert( group, page );
00775       mGroupLayouts.insert( group, layout );
00776       currentRow = 0;
00777       mCurrentRows.insert( group, currentRow );
00778     } else {
00779       page = mGroupPages[ group ];
00780       layout = mGroupLayouts[ group ];
00781       currentRow = mCurrentRows[ group ];
00782     }
00783 
00784     KPrefsWid *wid = KPrefsWidFactory::create( *it, page );
00785 
00786     if ( wid ) {
00787       QValueList<QWidget *> widgets = wid->widgets();
00788       if ( widgets.count() == 1 ) {
00789         layout->addMultiCellWidget( widgets[ 0 ],
00790                                     currentRow, currentRow, 0, 1 );
00791       } else if ( widgets.count() == 2 ) {
00792         layout->addWidget( widgets[ 0 ], currentRow, 0 );
00793         layout->addWidget( widgets[ 1 ], currentRow, 1 );
00794       } else {
00795         kdError() << "More widgets than expected: " << widgets.count() << endl;
00796       }
00797 
00798       if ( (*it)->isImmutable() ) {
00799         QValueList<QWidget *>::Iterator it2;
00800         for( it2 = widgets.begin(); it2 != widgets.end(); ++it2 ) {
00801           (*it2)->setEnabled( false );
00802         }
00803       }
00804 
00805       addWid( wid );
00806 
00807       mCurrentRows.replace( group, ++currentRow );
00808     }
00809   }
00810 
00811   readConfig();
00812 }
00813 
00814 void KPrefsDialog::setDefaults()
00815 {
00816   setWidDefaults();
00817 }
00818 
00819 void KPrefsDialog::readConfig()
00820 {
00821   readWidConfig();
00822 
00823   usrReadConfig();
00824 }
00825 
00826 void KPrefsDialog::writeConfig()
00827 {
00828   writeWidConfig();
00829 
00830   usrWriteConfig();
00831 
00832   readConfig();
00833 }
00834 
00835 
00836 void KPrefsDialog::slotApply()
00837 {
00838   writeConfig();
00839   emit configChanged();
00840 }
00841 
00842 void KPrefsDialog::slotOk()
00843 {
00844   slotApply();
00845   accept();
00846 }
00847 
00848 void KPrefsDialog::slotDefault()
00849 {
00850   kdDebug() << "KPrefsDialog::slotDefault()" << endl;
00851 
00852   if (KMessageBox::warningContinueCancel(this,
00853       i18n("You are about to set all preferences to default values. All "
00854       "custom modifications will be lost."),i18n("Setting Default Preferences"),
00855       i18n("Reset to Defaults"))
00856     == KMessageBox::Continue) setDefaults();
00857 }
00858 
00859 
00860 KPrefsModule::KPrefsModule( KConfigSkeleton *prefs, QWidget *parent,
00861                             const char *name )
00862   : KCModule( parent, name ),
00863     KPrefsWidManager( prefs )
00864 {
00865   emit changed( false );
00866 }
00867 
00868 void KPrefsModule::addWid( KPrefsWid *wid )
00869 {
00870   KPrefsWidManager::addWid( wid );
00871 
00872   connect( wid, SIGNAL( changed() ), SLOT( slotWidChanged() ) );
00873 }
00874 
00875 void KPrefsModule::slotWidChanged()
00876 {
00877   kdDebug(5310) << "KPrefsModule::slotWidChanged()" << endl;
00878 
00879   emit changed( true );
00880 }
00881 
00882 void KPrefsModule::load()
00883 {
00884   kdDebug(5310) << "KPrefsModule::load()" << endl;
00885 
00886   readWidConfig();
00887 
00888   usrReadConfig();
00889 
00890   emit changed( false );
00891 }
00892 
00893 void KPrefsModule::save()
00894 {
00895   kdDebug(5310) << "KPrefsModule::save()" << endl;
00896 
00897   writeWidConfig();
00898 
00899   usrWriteConfig();
00900 }
00901 
00902 void KPrefsModule::defaults()
00903 {
00904   setWidDefaults();
00905 
00906   emit changed( true );
00907 }
KDE Home | KDE Accessibility Home | Description of Access Keys