KDEUI
kconfiggroupgui.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
00048 output = input;
00049
00050 switch (input.type()) {
00051 case QVariant::Color: {
00052 if (data.isEmpty() || data == "invalid") {
00053 output = QColor();
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;
00067 }
00068
00069 int temp[4];
00070
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) {
00075 kError() << errString << " (integer conversion failed)" << endl;
00076 return true;
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;
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
00120
00121 default:
00122 break;
00123 }
00124
00125 return false;
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
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;
00185 }
00186
00187 #ifdef Q_CONSTRUCTOR_FUNCTION
00188 Q_CONSTRUCTOR_FUNCTION(initKConfigGroupGui)
00189 #else
00190 static int dummyKConfigGroupGui = initKConfigGroupGui();
00191 #endif