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 if (!data.contains(',')) { 00061 QColor col; 00062 col.setNamedColor(QString::fromUtf8(data.constData(), data.length())); 00063 if (!col.isValid()) 00064 kError() << qPrintable(errString); 00065 output = col; 00066 return true; 00067 } else { 00068 const QList<QByteArray> list = data.split(','); 00069 const int count = list.count(); 00070 00071 if (count != 3 && count != 4) { 00072 kError() << qPrintable(errString) << qPrintable(formatError.arg("3' or '4").arg(count)); 00073 return true; // return default 00074 } 00075 00076 int temp[4]; 00077 // bounds check components 00078 for(int i = 0; i < count; i++) { 00079 bool ok; 00080 const int j = temp[i] = list.at(i).toInt(&ok); 00081 if (!ok) { // failed to convert to int 00082 kError() << qPrintable(errString) << " (integer conversion failed)"; 00083 return true; // return default 00084 } 00085 if (j < 0 || j > 255) { 00086 static const char *const components[6] = { 00087 "red", "green", "blue", "alpha" 00088 }; 00089 const QString boundsError = QLatin1String(" (bounds error: %1 component %2)"); 00090 kError() << qPrintable(errString) 00091 << qPrintable(boundsError.arg(components[i]).arg(j < 0? "< 0": "> 255")); 00092 return true; // return default 00093 } 00094 } 00095 QColor aColor(temp[0], temp[1], temp[2]); 00096 if (count == 4) 00097 aColor.setAlpha(temp[3]); 00098 00099 if (aColor.isValid()) 00100 output = aColor; 00101 else 00102 kError() << qPrintable(errString); 00103 return true; 00104 } 00105 } 00106 00107 case QVariant::Font: { 00108 QVariant tmp = QString::fromUtf8(data.constData(), data.length()); 00109 if (tmp.convert(QVariant::Font)) 00110 output = tmp; 00111 else 00112 kError() << qPrintable(errString); 00113 return true; 00114 } 00115 case QVariant::Pixmap: 00116 case QVariant::Image: 00117 case QVariant::Brush: 00118 case QVariant::Palette: 00119 case QVariant::Icon: 00120 case QVariant::Region: 00121 case QVariant::Bitmap: 00122 case QVariant::Cursor: 00123 case QVariant::SizePolicy: 00124 case QVariant::Pen: 00125 // we may want to handle these in the future 00126 00127 default: 00128 break; 00129 } 00130 00131 return false; // not handled 00132 } 00133 00140 static bool writeEntryGui(KConfigGroup *cg, const char* key, const QVariant &prop, 00141 KConfigGroup::WriteConfigFlags pFlags) 00142 { 00143 switch (prop.type()) { 00144 case QVariant::Color: { 00145 const QColor rColor = prop.value<QColor>(); 00146 00147 if (!rColor.isValid()) { 00148 cg->writeEntry(key, "invalid", pFlags); 00149 return true; 00150 } 00151 00152 QList<int> list; 00153 list.insert(0, rColor.red()); 00154 list.insert(1, rColor.green()); 00155 list.insert(2, rColor.blue()); 00156 if (rColor.alpha() != 255) 00157 list.insert(3, rColor.alpha()); 00158 00159 cg->writeEntry( key, list, pFlags ); 00160 return true; 00161 } 00162 case QVariant::Font: 00163 cg->writeEntry( key, prop.toString().toUtf8(), pFlags ); 00164 return true; 00165 00166 case QVariant::Pixmap: 00167 case QVariant::Image: 00168 case QVariant::Brush: 00169 case QVariant::Palette: 00170 case QVariant::Icon: 00171 case QVariant::Region: 00172 case QVariant::Bitmap: 00173 case QVariant::Cursor: 00174 case QVariant::SizePolicy: 00175 case QVariant::Pen: 00176 // we may want to handle one of these in the future 00177 break; 00178 00179 default: 00180 break; 00181 } 00182 00183 return false; 00184 } 00185 00186 static int initKConfigGroupGui() 00187 { 00188 _kde_internal_KConfigGroupGui.readEntryGui = readEntryGui; 00189 _kde_internal_KConfigGroupGui.writeEntryGui = writeEntryGui; 00190 return 42; // because 42 is nicer than 1 or 0 00191 } 00192 00193 #ifdef Q_CONSTRUCTOR_FUNCTION 00194 Q_CONSTRUCTOR_FUNCTION(initKConfigGroupGui) 00195 #else 00196 static int dummyKConfigGroupGui = initKConfigGroupGui(); 00197 #endif
KDE 4.6 API Reference