• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

KDEUI

kconfiggroupgui.cpp

Go to the documentation of this file.
00001 /*
00002    This file is part of the KDE libraries
00003    Copyright (c) 2007 Thiago Macieira <thiago@kde.org>
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License as published by the Free Software Foundation; either
00008    version 2 of the License, or (at your option) any later version.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this library; see the file COPYING.LIB.  If not, write to
00017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018    Boston, MA 02110-1301, USA.
00019 */
00020 
00021 #include <kconfiggroup.h>
00022 
00023 #include <QtCore/QMutableStringListIterator>
00024 #include <QtGui/QColor>
00025 #include <QtGui/QFont>
00026 
00027 #include <kconfiggroup_p.h>
00028 #include <kdebug.h>
00029 
00038 static bool readEntryGui(const QByteArray& data, const char* key, const QVariant &input,
00039                          QVariant &output)
00040 {
00041     const QString errString = QString::fromLatin1("\"%1\" - conversion from \"%3\" to %2 failed")
00042                               .arg(key)
00043                               .arg(QVariant::typeToName(input.type()))
00044                               .arg(data.constData());
00045     const QString formatError = QString::fromLatin1(" (wrong format: expected '%1' items, read '%2')");
00046 
00047     // set in case of failure
00048     output = input;
00049 
00050     switch (input.type()) {
00051     case QVariant::Color: {
00052         if (data.isEmpty() || data == "invalid") {
00053             output = QColor();  // return what was stored
00054             return true;
00055         } else if (data.at(0) == '#') {
00056             QColor col;
00057             col.setNamedColor(QString::fromUtf8(data.constData(), data.length()));
00058             output = col;
00059             return true;
00060         } else {
00061             const QList<QByteArray> list = data.split(',');
00062             const int count = list.count();
00063 
00064             if (count != 3 && count != 4) {
00065                 kError() << errString << formatError.arg("3' or '4").arg(count) << endl;
00066                 return true;    // return default
00067             }
00068 
00069             int temp[4];
00070             // bounds check components
00071             for(int i = 0; i < count; i++) {
00072                 bool ok;
00073                 const int j = temp[i] = list.at(i).toInt(&ok);
00074                 if (!ok) { // failed to convert to int
00075                     kError() << errString << " (integer conversion failed)" << endl;
00076                     return true; // return default
00077                 }
00078                 if (j < 0 || j > 255) {
00079                     static const char *const components[6] = {
00080                         "red", "green", "blue", "alpha"
00081                     };
00082                     const QString boundsError = QLatin1String(" (bounds error: %1 component %2)");
00083                     kError() << errString
00084                              << boundsError.arg(components[i]).arg(j < 0? "< 0": "> 255")
00085                              << endl;
00086                     return true; // return default
00087                 }
00088             }
00089             QColor aColor(temp[0], temp[1], temp[2]);
00090             if (count == 4)
00091                 aColor.setAlpha(temp[3]);
00092 
00093             if (aColor.isValid())
00094                 output = aColor;
00095             else
00096                 kError() << errString << endl;
00097             return true;
00098         }
00099     }
00100 
00101     case QVariant::Font: {
00102         QVariant tmp = QString::fromUtf8(data.constData(), data.length());
00103         if (tmp.convert(QVariant::Font))
00104             output = tmp;
00105         else
00106             kError() << errString << endl;
00107         return true;
00108     }
00109     case QVariant::Pixmap:
00110     case QVariant::Image:
00111     case QVariant::Brush:
00112     case QVariant::Palette:
00113     case QVariant::Icon:
00114     case QVariant::Region:
00115     case QVariant::Bitmap:
00116     case QVariant::Cursor:
00117     case QVariant::SizePolicy:
00118     case QVariant::Pen:
00119         // we may want to handle these in the future
00120 
00121     default:
00122         break;
00123     }
00124 
00125     return false;               // not handled
00126 }
00127 
00134 static bool writeEntryGui(KConfigGroup *cg, const char* key, const QVariant &prop,
00135                           KConfigGroup::WriteConfigFlags pFlags)
00136 {
00137     switch (prop.type()) {
00138     case QVariant::Color: {
00139         const QColor rColor = prop.value<QColor>();
00140 
00141         if (!rColor.isValid()) {
00142             cg->writeEntry(key, "invalid", pFlags);
00143             return true;
00144         }
00145 
00146         QList<int> list;
00147         list.insert(0, rColor.red());
00148         list.insert(1, rColor.green());
00149         list.insert(2, rColor.blue());
00150         if (rColor.alpha() != 255)
00151             list.insert(3, rColor.alpha());
00152 
00153         cg->writeEntry( key, list, pFlags );
00154         return true;
00155     }
00156     case QVariant::Font:
00157         cg->writeEntry( key, prop.toString().toUtf8(), pFlags );
00158         return true;
00159 
00160     case QVariant::Pixmap:
00161     case QVariant::Image:
00162     case QVariant::Brush:
00163     case QVariant::Palette:
00164     case QVariant::Icon:
00165     case QVariant::Region:
00166     case QVariant::Bitmap:
00167     case QVariant::Cursor:
00168     case QVariant::SizePolicy:
00169     case QVariant::Pen:
00170         // we may want to handle one of these in the future
00171         break;
00172 
00173     default:
00174         break;
00175     }
00176 
00177     return false;
00178 }
00179 
00180 static int initKConfigGroupGui()
00181 {
00182     _kde_internal_KConfigGroupGui.readEntryGui = readEntryGui;
00183     _kde_internal_KConfigGroupGui.writeEntryGui = writeEntryGui;
00184     return 42;                  // because 42 is nicer than 1 or 0
00185 }
00186 
00187 #ifdef Q_CONSTRUCTOR_FUNCTION
00188 Q_CONSTRUCTOR_FUNCTION(initKConfigGroupGui)
00189 #else
00190 static int dummyKConfigGroupGui = initKConfigGroupGui();
00191 #endif

KDEUI

Skip menu "KDEUI"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.7
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal