KConfig

kconfiggroupgui.cpp
1 /*
2  This file is part of the KDE libraries
3  SPDX-FileCopyrightText: 2007 Thiago Macieira <[email protected]>
4 
5  SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7 
8 #include <kconfiggroup.h>
9 
10 #include <QMutableStringListIterator>
11 #include <QColor>
12 #include <QDebug>
13 #include <QFont>
14 
15 #include <kconfiggroup_p.h>
16 
25 static bool readEntryGui(const QByteArray &data, const char *key, const QVariant &input,
26  QVariant &output)
27 {
28  const auto errString = [&]() {
29  return QStringLiteral("\"%1\" - conversion from \"%3\" to %2 failed")
30  .arg(QLatin1String(key),
32  QLatin1String(data.constData()) );
33  };
34 
35  // set in case of failure
36  output = input;
37 
38  switch (static_cast<QMetaType::Type>(input.type())) {
39  case QMetaType::QColor: {
40  if (data.isEmpty() || data == "invalid") {
41  output = QColor(); // return what was stored
42  return true;
43  } else if (data.at(0) == '#') {
44  QColor col;
45  col.setNamedColor(QString::fromUtf8(data.constData(), data.length()));
46  output = col;
47  return true;
48  } else if (!data.contains(',')) {
49  QColor col;
50  col.setNamedColor(QString::fromUtf8(data.constData(), data.length()));
51  if (!col.isValid()) {
52  qCritical() << qPrintable(errString());
53  }
54  output = col;
55  return true;
56  } else {
57  const QList<QByteArray> list = data.split(',');
58  const int count = list.count();
59 
60  if (count != 3 && count != 4) {
61  const QString formatError = QStringLiteral(" (wrong format: expected '%1' items, read '%2')");
62  qCritical() << qPrintable(errString()) << qPrintable(formatError.arg(QStringLiteral("3' or '4")).arg(count));
63  return true; // return default
64  }
65 
66  int temp[4];
67  // bounds check components
68  for (int i = 0; i < count; i++) {
69  bool ok;
70  const int j = temp[i] = list.at(i).toInt(&ok);
71  if (!ok) { // failed to convert to int
72  qCritical() << qPrintable(errString()) << " (integer conversion failed)";
73  return true; // return default
74  }
75  if (j < 0 || j > 255) {
76  static const char *const components[] = {
77  "red", "green", "blue", "alpha"
78  };
79  const QString boundsError = QStringLiteral(" (bounds error: %1 component %2)");
80  qCritical() << qPrintable(errString())
81  << qPrintable(boundsError.arg(QLatin1String(components[i])).arg(j < 0 ? QStringLiteral("< 0") : QStringLiteral("> 255")));
82  return true; // return default
83  }
84  }
85  QColor aColor(temp[0], temp[1], temp[2]);
86  if (count == 4) {
87  aColor.setAlpha(temp[3]);
88  }
89 
90  if (aColor.isValid()) {
91  output = aColor;
92  } else {
93  qCritical() << qPrintable(errString());
94  }
95  return true;
96  }
97  }
98 
99  case QMetaType::QFont: {
100  QVariant tmp = QString::fromUtf8(data.constData(), data.length());
101  if (tmp.convert(QMetaType::QFont)) {
102  output = tmp;
103  } else {
104  qCritical() << qPrintable(errString());
105  }
106  return true;
107  }
108  case QMetaType::QPixmap:
109  case QMetaType::QImage:
110  case QMetaType::QBrush:
111  case QMetaType::QPalette:
112  case QMetaType::QIcon:
113  case QMetaType::QRegion:
114  case QMetaType::QBitmap:
115  case QMetaType::QCursor:
117  case QMetaType::QPen:
118  // we may want to handle these in the future
119 
120  default:
121  break;
122  }
123 
124  return false; // not handled
125 }
126 
133 static bool writeEntryGui(KConfigGroup *cg, const char *key, const QVariant &prop,
135 {
136  switch (static_cast<QMetaType::Type>(prop.type())) {
137  case QMetaType::QColor: {
138  const QColor rColor = prop.value<QColor>();
139 
140  if (!rColor.isValid()) {
141  cg->writeEntry(key, "invalid", pFlags);
142  return true;
143  }
144 
145  QList<int> list;
146  list.insert(0, rColor.red());
147  list.insert(1, rColor.green());
148  list.insert(2, rColor.blue());
149  if (rColor.alpha() != 255) {
150  list.insert(3, rColor.alpha());
151  }
152 
153  cg->writeEntry(key, list, pFlags);
154  return true;
155  }
156  case QMetaType::QFont: {
157  QFont f = prop.value<QFont>();
158  // If the styleName property is set for a QFont, using setBold(true) would
159  // lead to Qt using an "emboldended"/synthetic font style instead of using
160  // the bold style provided by the font itself; the latter looks much better
161  // than the former. For more details see:
162  // https://bugreports.qt.io/browse/QTBUG-63792
163  // https://bugs.kde.org/show_bug.cgi?id=378523
164  if (f.weight() == QFont::Normal
165  && (f.styleName() == QLatin1String("Regular")
166  || f.styleName() == QLatin1String("Normal")
167  || f.styleName() == QLatin1String("Book")
168  || f.styleName() == QLatin1String("Roman"))) {
169  f.setStyleName(QString());
170  }
171  cg->writeEntry(key, f.toString().toUtf8(), pFlags);
172  return true;
173  }
174  case QMetaType::QPixmap:
175  case QMetaType::QImage:
176  case QMetaType::QBrush:
177  case QMetaType::QPalette:
178  case QMetaType::QIcon:
179  case QMetaType::QRegion:
180  case QMetaType::QBitmap:
181  case QMetaType::QCursor:
183  case QMetaType::QPen:
184  // we may want to handle one of these in the future
185  break;
186 
187  default:
188  break;
189  }
190 
191  return false;
192 }
193 
194 static int initKConfigGroupGui()
195 {
196  _kde_internal_KConfigGroupGui.readEntryGui = readEntryGui;
197  _kde_internal_KConfigGroupGui.writeEntryGui = writeEntryGui;
198  return 42; // because 42 is nicer than 1 or 0
199 }
200 
201 #ifdef Q_CONSTRUCTOR_FUNCTION
202 Q_CONSTRUCTOR_FUNCTION(initKConfigGroupGui)
203 #else
204 static int dummyKConfigGroupGui = initKConfigGroupGui();
205 #endif
QList< QByteArray > split(char sep) const const
char at(int i) const const
int weight() const const
const T & at(int i) const const
bool isEmpty() const const
T value() const const
void writeEntry(const QString &key, const QVariant &value, WriteConfigFlags pFlags=Normal)
Writes a value to the configuration object.
int length() const const
void setNamedColor(const QString &name)
void setStyleName(const QString &styleName)
QString styleName() const const
int count(const T &value) const const
QString fromUtf8(const char *str, int size)
int red() const const
const char * constData() const const
int alpha() const const
int green() const const
const char * typeToName(int typeId)
A class for one specific group in a KConfig object.
Definition: kconfiggroup.h:38
int blue() const const
QString toString() const const
void insert(int i, const T &value)
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
bool contains(char ch) const const
QVariant::Type type() const const
bool convert(int targetTypeId)
bool isValid() const const
KIOFILEWIDGETS_EXPORT QStringList list(const QString &fileClass)
QByteArray toUtf8() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sat Jul 11 2020 22:47:24 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.