• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

KDEUI

  • kdeui
  • config
kconfiggroupgui.cpp
Go to the documentation of this file.
1/*
2 This file is part of the KDE libraries
3 Copyright (c) 2007 Thiago Macieira <thiago@kde.org>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
19*/
20
21#include <kconfiggroup.h>
22
23#include <QtCore/QMutableStringListIterator>
24#include <QtGui/QColor>
25#include <QtGui/QFont>
26
27#include <kconfiggroup_p.h>
28#include <kdebug.h>
29
38static bool readEntryGui(const QByteArray& data, const char* key, const QVariant &input,
39 QVariant &output)
40{
41 const QString errString = QString::fromLatin1("\"%1\" - conversion from \"%3\" to %2 failed")
42 .arg(key)
43 .arg(QVariant::typeToName(input.type()))
44 .arg(data.constData());
45 const QString formatError = QString::fromLatin1(" (wrong format: expected '%1' items, read '%2')");
46
47 // set in case of failure
48 output = input;
49
50 switch (input.type()) {
51 case QVariant::Color: {
52 if (data.isEmpty() || data == "invalid") {
53 output = QColor(); // return what was stored
54 return true;
55 } else if (data.at(0) == '#') {
56 QColor col;
57 col.setNamedColor(QString::fromUtf8(data.constData(), data.length()));
58 output = col;
59 return true;
60 } else if (!data.contains(',')) {
61 QColor col;
62 col.setNamedColor(QString::fromUtf8(data.constData(), data.length()));
63 if (!col.isValid())
64 kError() << qPrintable(errString);
65 output = col;
66 return true;
67 } else {
68 const QList<QByteArray> list = data.split(',');
69 const int count = list.count();
70
71 if (count != 3 && count != 4) {
72 kError() << qPrintable(errString) << qPrintable(formatError.arg("3' or '4").arg(count));
73 return true; // return default
74 }
75
76 int temp[4];
77 // bounds check components
78 for(int i = 0; i < count; i++) {
79 bool ok;
80 const int j = temp[i] = list.at(i).toInt(&ok);
81 if (!ok) { // failed to convert to int
82 kError() << qPrintable(errString) << " (integer conversion failed)";
83 return true; // return default
84 }
85 if (j < 0 || j > 255) {
86 static const char *const components[6] = {
87 "red", "green", "blue", "alpha"
88 };
89 const QString boundsError = QLatin1String(" (bounds error: %1 component %2)");
90 kError() << qPrintable(errString)
91 << qPrintable(boundsError.arg(components[i]).arg(j < 0? "< 0": "> 255"));
92 return true; // return default
93 }
94 }
95 QColor aColor(temp[0], temp[1], temp[2]);
96 if (count == 4)
97 aColor.setAlpha(temp[3]);
98
99 if (aColor.isValid())
100 output = aColor;
101 else
102 kError() << qPrintable(errString);
103 return true;
104 }
105 }
106
107 case QVariant::Font: {
108 QVariant tmp = QString::fromUtf8(data.constData(), data.length());
109 if (tmp.convert(QVariant::Font))
110 output = tmp;
111 else
112 kError() << qPrintable(errString);
113 return true;
114 }
115 case QVariant::Pixmap:
116 case QVariant::Image:
117 case QVariant::Brush:
118 case QVariant::Palette:
119 case QVariant::Icon:
120 case QVariant::Region:
121 case QVariant::Bitmap:
122 case QVariant::Cursor:
123 case QVariant::SizePolicy:
124 case QVariant::Pen:
125 // we may want to handle these in the future
126
127 default:
128 break;
129 }
130
131 return false; // not handled
132}
133
140static bool writeEntryGui(KConfigGroup *cg, const char* key, const QVariant &prop,
141 KConfigGroup::WriteConfigFlags pFlags)
142{
143 switch (prop.type()) {
144 case QVariant::Color: {
145 const QColor rColor = prop.value<QColor>();
146
147 if (!rColor.isValid()) {
148 cg->writeEntry(key, "invalid", pFlags);
149 return true;
150 }
151
152 QList<int> list;
153 list.insert(0, rColor.red());
154 list.insert(1, rColor.green());
155 list.insert(2, rColor.blue());
156 if (rColor.alpha() != 255)
157 list.insert(3, rColor.alpha());
158
159 cg->writeEntry( key, list, pFlags );
160 return true;
161 }
162 case QVariant::Font:
163 cg->writeEntry( key, prop.toString().toUtf8(), pFlags );
164 return true;
165
166 case QVariant::Pixmap:
167 case QVariant::Image:
168 case QVariant::Brush:
169 case QVariant::Palette:
170 case QVariant::Icon:
171 case QVariant::Region:
172 case QVariant::Bitmap:
173 case QVariant::Cursor:
174 case QVariant::SizePolicy:
175 case QVariant::Pen:
176 // we may want to handle one of these in the future
177 break;
178
179 default:
180 break;
181 }
182
183 return false;
184}
185
186static int initKConfigGroupGui()
187{
188 _kde_internal_KConfigGroupGui.readEntryGui = readEntryGui;
189 _kde_internal_KConfigGroupGui.writeEntryGui = writeEntryGui;
190 return 42; // because 42 is nicer than 1 or 0
191}
192
193#ifdef Q_CONSTRUCTOR_FUNCTION
194Q_CONSTRUCTOR_FUNCTION(initKConfigGroupGui)
195#else
196static int dummyKConfigGroupGui = initKConfigGroupGui();
197#endif
KConfigGroup
KConfigGroup::writeEntry
void writeEntry(const char *key, const char *value, WriteConfigFlags pFlags=Normal)
QList
output
void output(QList< Action > actions, QHash< QString, QString > domain)
_kde_internal_KConfigGroupGui
KConfigGroupGui _kde_internal_KConfigGroupGui
kconfiggroup.h
kconfiggroup_p.h
dummyKConfigGroupGui
static int dummyKConfigGroupGui
Definition: kconfiggroupgui.cpp:196
initKConfigGroupGui
static int initKConfigGroupGui()
Definition: kconfiggroupgui.cpp:186
readEntryGui
static bool readEntryGui(const QByteArray &data, const char *key, const QVariant &input, QVariant &output)
Try to read a GUI type from config group cg at key key.
Definition: kconfiggroupgui.cpp:38
writeEntryGui
static bool writeEntryGui(KConfigGroup *cg, const char *key, const QVariant &prop, KConfigGroup::WriteConfigFlags pFlags)
Try to write a GUI type prop to config group cg at key key.
Definition: kconfiggroupgui.cpp:140
kdebug.h
KConfigGroupGui::writeEntryGui
kWriteEntryGui writeEntryGui
KConfigGroupGui::readEntryGui
kReadEntryGui readEntryGui
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDEUI

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

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal