kdecore Library API Documentation

kconfigskeleton.h

00001 /* 00002 * This file is part of KDE. 00003 * 00004 * Copyright (c) 2001,2002,2003 Cornelius Schumacher <schumacher@kde.org> 00005 * Copyright (c) 2003 Waldo Bastian <bastian@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., 59 Temple Place - Suite 330, 00020 * Boston, MA 02111-1307, USA. 00021 */ 00022 00023 #ifndef _KCONFIGSKELETON_H 00024 #define _KCONFIGSKELETON_H 00025 00026 #include <qcolor.h> 00027 #include <qdatetime.h> 00028 #include <qfont.h> 00029 #include <qpoint.h> 00030 #include <qptrlist.h> 00031 #include <qdict.h> 00032 #include <qrect.h> 00033 #include <qsize.h> 00034 #include <qstringlist.h> 00035 #include <qvariant.h> 00036 #include <kconfig.h> 00037 #include <kglobalsettings.h> 00038 00050 class KConfigSkeletonItem 00051 { 00052 public: 00053 typedef QValueList < KConfigSkeletonItem * >List; 00054 typedef QDict < KConfigSkeletonItem > Dict; 00055 typedef QDictIterator < KConfigSkeletonItem > DictIterator; 00056 00063 KConfigSkeletonItem(const QString & group, const QString & key) 00064 :mGroup(group),mKey(key), mIsImmutable(true) 00065 { 00066 } 00067 00071 virtual ~KConfigSkeletonItem() 00072 { 00073 } 00074 00078 void setGroup( const QString &group ) 00079 { 00080 mGroup = group; 00081 } 00082 00086 QString group() const 00087 { 00088 return mGroup; 00089 } 00090 00094 void setKey( const QString &key ) 00095 { 00096 mKey = key; 00097 } 00098 00102 QString key() const 00103 { 00104 return mKey; 00105 } 00106 00110 void setName(const QString &name) 00111 { 00112 mName = name; 00113 } 00114 00118 QString name() const 00119 { 00120 return mName; 00121 } 00122 00126 void setLabel( const QString &l ) 00127 { 00128 mLabel = l; 00129 } 00130 00134 QString label() const 00135 { 00136 return mLabel; 00137 } 00138 00142 void setWhatsThis( const QString &w ) 00143 { 00144 mWhatsThis = w; 00145 } 00146 00150 QString whatsThis() const 00151 { 00152 return mWhatsThis; 00153 } 00154 00160 virtual void readConfig(KConfig *) = 0; 00161 00166 virtual void writeConfig(KConfig *) = 0; 00167 00171 virtual void readDefault(KConfig *) = 0; 00172 00176 virtual void setProperty(const QVariant &p) = 0; 00177 00181 virtual QVariant property() const = 0; 00182 00186 virtual QVariant minValue() const { return QVariant(); } 00187 00191 virtual QVariant maxValue() const { return QVariant(); } 00192 00196 virtual void setDefault() = 0; 00197 00202 virtual void swapDefault() = 0; 00203 00207 bool isImmutable() const 00208 { 00209 return mIsImmutable; 00210 } 00211 00212 protected: 00213 void readImmutability(KConfig *); 00214 00215 QString mGroup; 00216 QString mKey; 00217 QString mName; 00218 00219 private: 00220 bool mIsImmutable; 00221 00222 QString mLabel; 00223 QString mWhatsThis; 00224 }; 00225 00226 00227 template < typename T > class KConfigSkeletonGenericItem:public KConfigSkeletonItem 00228 { 00229 public: 00230 KConfigSkeletonGenericItem(const QString & group, const QString & key, T & reference, 00231 T defaultValue) 00232 : KConfigSkeletonItem(group, key), mReference(reference), 00233 mDefault(defaultValue), mLoadedValue(defaultValue) 00234 { 00235 } 00236 00240 void setValue(const T & v) 00241 { 00242 mReference = v; 00243 } 00244 00248 T & value() 00249 { 00250 return mReference; 00251 } 00252 00256 const T & value() const 00257 { 00258 return mReference; 00259 } 00260 00264 virtual void setDefaultValue( const T &v ) 00265 { 00266 mDefault = v; 00267 } 00268 00269 virtual void setDefault() 00270 { 00271 mReference = mDefault; 00272 } 00273 00274 virtual void writeConfig(KConfig * config) 00275 { 00276 if ( mReference != mLoadedValue ) // Is this needed? 00277 { 00278 config->setGroup(mGroup); 00279 if ((mDefault == mReference) && !config->hasDefault( mKey)) 00280 config->revertToDefault( mKey ); 00281 else 00282 config->writeEntry(mKey, mReference); 00283 } 00284 } 00285 00286 void readDefault(KConfig * config) 00287 { 00288 config->setReadDefaults(true); 00289 readConfig(config); 00290 config->setReadDefaults(false); 00291 mDefault = mReference; 00292 } 00293 00294 void swapDefault() 00295 { 00296 T tmp = mReference; 00297 mReference = mDefault; 00298 mDefault = tmp; 00299 } 00300 00301 protected: 00302 T & mReference; 00303 T mDefault; 00304 T mLoadedValue; 00305 }; 00306 00361 class KConfigSkeleton 00362 { 00363 public: 00364 00368 class ItemString:public KConfigSkeletonGenericItem < QString > 00369 { 00370 public: 00371 enum Type { Normal, Password, Path }; 00372 00373 ItemString(const QString & group, const QString & key, 00374 QString & reference, 00375 const QString & defaultValue = QString::fromLatin1(""), // NOT QString::null !! 00376 Type type = Normal); 00377 00378 void writeConfig(KConfig * config); 00379 void readConfig(KConfig * config); 00380 void setProperty(const QVariant & p); 00381 QVariant property() const; 00382 00383 private: 00384 Type mType; 00385 }; 00386 00390 class ItemPassword:public ItemString 00391 { 00392 public: 00393 ItemPassword(const QString & group, const QString & key, 00394 QString & reference, 00395 const QString & defaultValue = QString::fromLatin1("")); // NOT QString::null !! 00396 }; 00397 00401 class ItemPath:public ItemString 00402 { 00403 public: 00404 ItemPath(const QString & group, const QString & key, 00405 QString & reference, 00406 const QString & defaultValue = QString::null); 00407 }; 00408 00409 00413 class ItemProperty:public KConfigSkeletonGenericItem < QVariant > 00414 { 00415 public: 00416 ItemProperty(const QString & group, const QString & key, 00417 QVariant & reference, QVariant defaultValue = 0); 00418 00419 void readConfig(KConfig * config); 00420 void setProperty(const QVariant & p); 00421 QVariant property() const; 00422 }; 00423 00424 00428 class ItemBool:public KConfigSkeletonGenericItem < bool > 00429 { 00430 public: 00431 ItemBool(const QString & group, const QString & key, bool & reference, 00432 bool defaultValue = true); 00433 00434 void readConfig(KConfig * config); 00435 void setProperty(const QVariant & p); 00436 QVariant property() const; 00437 }; 00438 00439 00443 class ItemInt:public KConfigSkeletonGenericItem < int > 00444 { 00445 public: 00446 ItemInt(const QString & group, const QString & key, int &reference, 00447 int defaultValue = 0); 00448 00449 void readConfig(KConfig * config); 00450 void setProperty(const QVariant & p); 00451 QVariant property() const; 00452 QVariant minValue() const; 00453 QVariant maxValue() const; 00454 00455 void setMinValue(int); 00456 void setMaxValue(int); 00457 00458 private: 00459 bool mHasMin : 1; 00460 bool mHasMax : 1; 00461 int mMin; 00462 int mMax; 00463 }; 00464 00468 class ItemInt64:public KConfigSkeletonGenericItem < Q_INT64 > 00469 { 00470 public: 00471 ItemInt64(const QString & group, const QString & key, Q_INT64 &reference, 00472 Q_INT64 defaultValue = 0); 00473 00474 void readConfig(KConfig * config); 00475 void setProperty(const QVariant & p); 00476 QVariant property() const; 00477 00478 QVariant minValue() const; 00479 QVariant maxValue() const; 00480 00481 void setMinValue(Q_INT64); 00482 void setMaxValue(Q_INT64); 00483 00484 private: 00485 bool mHasMin : 1; 00486 bool mHasMax : 1; 00487 Q_INT64 mMin; 00488 Q_INT64 mMax; 00489 }; 00490 00494 class ItemEnum:public ItemInt 00495 { 00496 public: 00497 struct Choice 00498 { 00499 QString name; 00500 QString label; 00501 QString whatsThis; 00502 }; 00503 00504 ItemEnum(const QString & group, const QString & key, int &reference, 00505 const QValueList<Choice> &choices, int defaultValue = 0); 00506 00507 QValueList<Choice> choices() const; 00508 00509 void readConfig(KConfig * config); 00510 void writeConfig(KConfig * config); 00511 00512 private: 00513 QValueList<Choice> mChoices; 00514 }; 00515 00516 00520 class ItemUInt:public KConfigSkeletonGenericItem < unsigned int > 00521 { 00522 public: 00523 ItemUInt(const QString & group, const QString & key, 00524 unsigned int &reference, unsigned int defaultValue = 0); 00525 00526 void readConfig(KConfig * config); 00527 void setProperty(const QVariant & p); 00528 QVariant property() const; 00529 QVariant minValue() const; 00530 QVariant maxValue() const; 00531 00532 void setMinValue(unsigned int); 00533 void setMaxValue(unsigned int); 00534 00535 private: 00536 bool mHasMin : 1; 00537 bool mHasMax : 1; 00538 unsigned int mMin; 00539 unsigned int mMax; 00540 }; 00541 00542 00546 class ItemLong:public KConfigSkeletonGenericItem < long > 00547 { 00548 public: 00549 ItemLong(const QString & group, const QString & key, long &reference, 00550 long defaultValue = 0); 00551 00552 void readConfig(KConfig * config); 00553 void setProperty(const QVariant & p); 00554 QVariant property() const; 00555 QVariant minValue() const; 00556 QVariant maxValue() const; 00557 00558 void setMinValue(long); 00559 void setMaxValue(long); 00560 00561 private: 00562 bool mHasMin : 1; 00563 bool mHasMax : 1; 00564 long mMin; 00565 long mMax; 00566 }; 00567 00568 00572 class ItemULong:public KConfigSkeletonGenericItem < unsigned long > 00573 { 00574 public: 00575 ItemULong(const QString & group, const QString & key, 00576 unsigned long &reference, unsigned long defaultValue = 0); 00577 00578 void readConfig(KConfig * config); 00579 void setProperty(const QVariant & p); 00580 QVariant property() const; 00581 QVariant minValue() const; 00582 QVariant maxValue() const; 00583 00584 void setMinValue(unsigned long); 00585 void setMaxValue(unsigned long); 00586 00587 private: 00588 bool mHasMin : 1; 00589 bool mHasMax : 1; 00590 unsigned long mMin; 00591 unsigned long mMax; 00592 }; 00593 00597 class ItemUInt64:public KConfigSkeletonGenericItem < Q_UINT64 > 00598 { 00599 public: 00600 ItemUInt64(const QString & group, const QString & key, Q_UINT64 &reference, 00601 Q_UINT64 defaultValue = 0); 00602 00603 void readConfig(KConfig * config); 00604 void setProperty(const QVariant & p); 00605 QVariant property() const; 00606 00607 QVariant minValue() const; 00608 QVariant maxValue() const; 00609 00610 void setMinValue(Q_UINT64); 00611 void setMaxValue(Q_UINT64); 00612 00613 private: 00614 bool mHasMin : 1; 00615 bool mHasMax : 1; 00616 Q_UINT64 mMin; 00617 Q_UINT64 mMax; 00618 }; 00619 00623 class ItemDouble:public KConfigSkeletonGenericItem < double > 00624 { 00625 public: 00626 ItemDouble(const QString & group, const QString & key, 00627 double &reference, double defaultValue = 0); 00628 00629 void readConfig(KConfig * config); 00630 void setProperty(const QVariant & p); 00631 QVariant property() const; 00632 QVariant minValue() const; 00633 QVariant maxValue() const; 00634 00635 void setMinValue(double); 00636 void setMaxValue(double); 00637 00638 private: 00639 bool mHasMin : 1; 00640 bool mHasMax : 1; 00641 double mMin; 00642 double mMax; 00643 }; 00644 00645 00649 class ItemColor:public KConfigSkeletonGenericItem < QColor > 00650 { 00651 public: 00652 ItemColor(const QString & group, const QString & key, 00653 QColor & reference, 00654 const QColor & defaultValue = QColor(128, 128, 128)); 00655 00656 void readConfig(KConfig * config); 00657 void setProperty(const QVariant & p); 00658 QVariant property() const; 00659 }; 00660 00661 00665 class ItemFont:public KConfigSkeletonGenericItem < QFont > 00666 { 00667 public: 00668 ItemFont(const QString & group, const QString & key, QFont & reference, 00669 const QFont & defaultValue = KGlobalSettings::generalFont()); 00670 00671 void readConfig(KConfig * config); 00672 void setProperty(const QVariant & p); 00673 QVariant property() const; 00674 }; 00675 00676 00680 class ItemRect:public KConfigSkeletonGenericItem < QRect > 00681 { 00682 public: 00683 ItemRect(const QString & group, const QString & key, QRect & reference, 00684 const QRect & defaultValue = QRect()); 00685 00686 void readConfig(KConfig * config); 00687 void setProperty(const QVariant & p); 00688 QVariant property() const; 00689 }; 00690 00691 00695 class ItemPoint:public KConfigSkeletonGenericItem < QPoint > 00696 { 00697 public: 00698 ItemPoint(const QString & group, const QString & key, QPoint & reference, 00699 const QPoint & defaultValue = QPoint()); 00700 00701 void readConfig(KConfig * config); 00702 void setProperty(const QVariant & p); 00703 QVariant property() const; 00704 }; 00705 00706 00710 class ItemSize:public KConfigSkeletonGenericItem < QSize > 00711 { 00712 public: 00713 ItemSize(const QString & group, const QString & key, QSize & reference, 00714 const QSize & defaultValue = QSize()); 00715 00716 void readConfig(KConfig * config); 00717 void setProperty(const QVariant & p); 00718 QVariant property() const; 00719 }; 00720 00721 00725 class ItemDateTime:public KConfigSkeletonGenericItem < QDateTime > 00726 { 00727 public: 00728 ItemDateTime(const QString & group, const QString & key, 00729 QDateTime & reference, 00730 const QDateTime & defaultValue = QDateTime()); 00731 00732 void readConfig(KConfig * config); 00733 void setProperty(const QVariant & p); 00734 QVariant property() const; 00735 }; 00736 00737 00741 class ItemStringList:public KConfigSkeletonGenericItem < QStringList > 00742 { 00743 public: 00744 ItemStringList(const QString & group, const QString & key, 00745 QStringList & reference, 00746 const QStringList & defaultValue = QStringList()); 00747 00748 void readConfig(KConfig * config); 00749 void setProperty(const QVariant & p); 00750 QVariant property() const; 00751 }; 00752 00753 00757 class ItemIntList:public KConfigSkeletonGenericItem < QValueList < int > > 00758 { 00759 public: 00760 ItemIntList(const QString & group, const QString & key, 00761 QValueList < int >&reference, 00762 const QValueList < int >&defaultValue = QValueList < int >()); 00763 00764 void readConfig(KConfig * config); 00765 void setProperty(const QVariant & p); 00766 QVariant property() const; 00767 }; 00768 00769 00770 public: 00777 KConfigSkeleton(const QString & configname = QString::null); 00778 00784 KConfigSkeleton(KSharedConfig::Ptr config); 00785 00789 virtual ~ KConfigSkeleton(); 00790 00794 void setDefaults(); 00795 00800 void readConfig(); 00801 00806 void writeConfig(); 00807 00813 void setCurrentGroup(const QString & group); 00814 00818 QString currentGroup() 00819 { 00820 return mCurrentGroup; 00821 } 00822 00829 void addItem(KConfigSkeletonItem *, const QString & name = QString::null ); 00830 00842 ItemString *addItemString(const QString & name, QString & reference, 00843 const QString & defaultValue = QString::fromLatin1(""), // NOT QString::null !! 00844 const QString & key = QString::null); 00845 00859 ItemPassword *addItemPassword(const QString & name, QString & reference, 00860 const QString & defaultValue = QString::fromLatin1(""), 00861 const QString & key = QString::null); 00862 00876 ItemPath *addItemPath(const QString & name, QString & reference, 00877 const QString & defaultValue = QString::fromLatin1(""), 00878 const QString & key = QString::null); 00879 00893 ItemProperty *addItemProperty(const QString & name, QVariant & reference, 00894 const QVariant & defaultValue = QVariant(), 00895 const QString & key = QString::null); 00907 ItemBool *addItemBool(const QString & name, bool & reference, 00908 bool defaultValue = false, 00909 const QString & key = QString::null); 00910 00922 ItemInt *addItemInt(const QString & name, int &reference, int defaultValue = 0, 00923 const QString & key = QString::null); 00924 00936 ItemUInt *addItemUInt(const QString & name, unsigned int &reference, 00937 unsigned int defaultValue = 0, 00938 const QString & key = QString::null); 00939 00951 ItemLong *addItemLong(const QString & name, long &reference, 00952 long defaultValue = 0, 00953 const QString & key = QString::null); 00954 00966 ItemULong *addItemULong(const QString & name, unsigned long &reference, 00967 unsigned long defaultValue = 0, 00968 const QString & key = QString::null); 00969 00981 ItemInt64 *addItemInt64(const QString & name, Q_INT64 &reference, 00982 Q_INT64 defaultValue = 0, 00983 const QString & key = QString::null); 00984 00996 ItemUInt64 *addItemUInt64(const QString & name, Q_UINT64 &reference, 00997 Q_UINT64 defaultValue = 0, 00998 const QString & key = QString::null); 00999 01011 ItemDouble *addItemDouble(const QString & name, double &reference, 01012 double defaultValue = 0.0, 01013 const QString & key = QString::null); 01014 01026 ItemColor *addItemColor(const QString & name, QColor & reference, 01027 const QColor & defaultValue = QColor(128, 128, 128), 01028 const QString & key = QString::null); 01029 01041 ItemFont *addItemFont(const QString & name, QFont & reference, 01042 const QFont & defaultValue = 01043 KGlobalSettings::generalFont(), 01044 const QString & key = QString::null); 01045 01057 ItemRect *addItemRect(const QString & name, QRect & reference, 01058 const QRect & defaultValue = QRect(), 01059 const QString & key = QString::null); 01060 01072 ItemPoint *addItemPoint(const QString & name, QPoint & reference, 01073 const QPoint & defaultValue = QPoint(), 01074 const QString & key = QString::null); 01075 01087 ItemSize *addItemSize(const QString & name, QSize & reference, 01088 const QSize & defaultValue = QSize(), 01089 const QString & key = QString::null); 01090 01102 ItemDateTime *addItemDateTime(const QString & name, QDateTime & reference, 01103 const QDateTime & defaultValue = QDateTime(), 01104 const QString & key = QString::null); 01105 01117 ItemStringList *addItemStringList(const QString & name, QStringList & reference, 01118 const QStringList & defaultValue = QStringList(), 01119 const QString & key = QString::null); 01120 01132 ItemIntList *addItemIntList(const QString & name, QValueList < int >&reference, 01133 const QValueList < int >&defaultValue = 01134 QValueList < int >(), 01135 const QString & key = QString::null); 01136 01140 KConfig *config() const; 01141 01145 KConfigSkeletonItem::List items() const 01146 { 01147 return mItems; 01148 } 01149 01153 bool isImmutable(const QString & name); 01154 01158 KConfigSkeletonItem * findItem(const QString & name); 01159 01166 bool useDefaults(bool b); 01167 01168 protected: 01174 virtual void usrUseDefaults(bool) 01175 { 01176 } 01177 01178 virtual void usrSetDefaults() 01179 { 01180 } 01181 01185 virtual void usrReadConfig() 01186 { 01187 } 01188 01192 virtual void usrWriteConfig() 01193 { 01194 } 01195 01196 private: 01197 QString mCurrentGroup; 01198 01199 KSharedConfig::Ptr mConfig; // pointer to KConfig object 01200 01201 KConfigSkeletonItem::List mItems; 01202 KConfigSkeletonItem::Dict mItemDict; 01203 01204 bool mUseDefaults; 01205 01206 class Private; 01207 Private *d; 01208 01209 }; 01210 01211 #endif
KDE Logo
This file is part of the documentation for kdecore Library Version 3.2.3.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sun Oct 10 18:54:55 2004 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003