KProperty

KPropertyWidgetsFactory.cpp
1 /* This file is part of the KDE project
2  Copyright (C) 2008-2017 Jarosław Staniek <[email protected]>
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License as published by the Free Software Foundation; either
7  version 2 of the License, or (at your option) any later version.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Library General Public License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to
16  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18 */
19 
20 
21 #include "KPropertyFactory.h"
22 
23 #include "KDefaultPropertyFactory.h"
24 #include "KPropertyEditorView.h"
25 #include "KPropertyStringEditor.h"
26 #include "KPropertyUtils.h"
27 #include "KPropertyUtils_p.h"
28 
29 class Q_DECL_HIDDEN KPropertyLabel::Private
30 {
31 public:
32  Private()
33  {
34  }
35  ~Private()
36  {
37  }
38 
39  const KProperty *property;
40  const KPropertyValueDisplayInterface *iface;
41  QVariant value;
42 };
43 
44 KPropertyLabel::KPropertyLabel(QWidget *parent, const KProperty *property,
45  const KPropertyValueDisplayInterface *iface)
46  : QLabel(parent), d(new Private)
47 {
48  d->property = property;
49  d->iface = iface;
51 
52  KPropertyEditorView* view = nullptr;
53  if (parent) {
54  view = qobject_cast<KPropertyEditorView *>(parent->parentWidget());
55  }
56  const QColor gridLineColor(view ? view->gridLineColor()
58  const int top = 1 + (gridLineColor.isValid() ? 1 : 0);
59 
60  setContentsMargins(0, top, 0, 0);
61  setIndent(1);
62 }
63 
64 KPropertyLabel::~KPropertyLabel()
65 {
66  delete d;
67 }
68 
69 QVariant KPropertyLabel::value() const
70 {
71  return d->value;
72 }
73 
74 void KPropertyLabel::setValue(const QVariant& value)
75 {
76  d->value = value;
77  setText(d->iface->propertyValueToString(d->property, QLocale()));
78 }
79 
80 void KPropertyLabel::paintEvent( QPaintEvent * event )
81 {
82  QLabel::paintEvent(event);
83  KPropertyWidgetsFactory::paintTopGridLine(this);
84 }
85 
86 //---------------
87 
89 class Q_DECL_HIDDEN KPropertyWidgetsFactory::Private
90 {
91 public:
92  Private()
93  {
94  }
95  ~Private()
96  {
97  qDeleteAll(editorCreatorsSet);
98  qDeleteAll(valuePaintersSet);
99  }
100 
103 
104  QSet<KPropertyEditorCreatorInterface*> editorCreatorsSet;
105  QSet<KPropertyValuePainterInterface*> valuePaintersSet;
106 };
107 
108 KPropertyWidgetsFactory::KPropertyWidgetsFactory()
109  : d( new Private )
110 {
111 }
112 
113 KPropertyWidgetsFactory::~KPropertyWidgetsFactory()
114 {
115  delete d;
116 }
117 
118 QHash<int, KPropertyEditorCreatorInterface*> KPropertyWidgetsFactory::editorCreators() const
119 {
120  return d->editorCreators;
121 }
122 
123 QHash<int, KPropertyValuePainterInterface*> KPropertyWidgetsFactory::valuePainters() const
124 {
125  return d->valuePainters;
126 }
127 
128 void KPropertyWidgetsFactory::addEditor(int type, KPropertyEditorCreatorInterface *creator)
129 {
130  addEditorInternal( type, creator, true );
131  if (dynamic_cast<KComposedPropertyCreatorInterface*>(creator)) {
132  addComposedPropertyCreatorInternal( type,
133  dynamic_cast<KComposedPropertyCreatorInterface*>(creator), false/* !own*/ );
134  }
135  if (dynamic_cast<KPropertyValuePainterInterface*>(creator)) {
136  addPainterInternal( type, dynamic_cast<KPropertyValuePainterInterface*>(creator), false/* !own*/ );
137  }
138  if (dynamic_cast<KPropertyValueDisplayInterface*>(creator)) {
139  addDisplayInternal( type, dynamic_cast<KPropertyValueDisplayInterface*>(creator), false/* !own*/ );
140  }
141 }
142 
143 void KPropertyWidgetsFactory::addPainter(int type, KPropertyValuePainterInterface *painter)
144 {
145  addPainterInternal(type, painter, true);
146  if (dynamic_cast<KComposedPropertyCreatorInterface*>(painter)) {
147  addComposedPropertyCreatorInternal( type,
148  dynamic_cast<KComposedPropertyCreatorInterface*>(painter), false/* !own*/ );
149  }
150  if (dynamic_cast<KPropertyEditorCreatorInterface*>(painter)) {
151  addEditorInternal( type, dynamic_cast<KPropertyEditorCreatorInterface*>(painter), false/* !own*/ );
152  }
153  if (dynamic_cast<KPropertyValueDisplayInterface*>(painter)) {
154  addDisplayInternal( type, dynamic_cast<KPropertyValueDisplayInterface*>(painter), false/* !own*/ );
155  }
156 }
157 
158 void KPropertyWidgetsFactory::addEditorInternal(int type, KPropertyEditorCreatorInterface *editor, bool own)
159 {
160  if (own)
161  d->editorCreatorsSet.insert(editor);
162  d->editorCreators.insert(type, editor);
163 }
164 
165 void KPropertyWidgetsFactory::addPainterInternal(int type, KPropertyValuePainterInterface *painter, bool own)
166 {
167  if (own)
168  d->valuePaintersSet.insert(painter);
169  d->valuePainters.insert(type, painter);
170 }
171 
172 //static
173 void KPropertyWidgetsFactory::paintTopGridLine(QWidget *widget)
174 {
175  // paint top grid line
176  KPropertyEditorView* view = nullptr;
177  if (widget->parentWidget()) {
179  }
180  const QColor gridLineColor(view ? view->gridLineColor() : KPropertyEditorView::defaultGridLineColor());
181  if (gridLineColor.isValid()) {
182  const QAbstractScrollArea *area = qobject_cast<const QAbstractScrollArea*>(widget);
183  QWidget *widgetToPaint;
184  if (area && area->viewport()) {
185  widgetToPaint = area->viewport();
186  } else {
187  widgetToPaint = widget;
188  }
189  QPainter p(widgetToPaint);
190  p.setPen(QPen( QBrush(gridLineColor), 1));
191  p.drawLine(0, 0, widget->width()-1, 0);
192  }
193 }
194 
195 //static
196 void KPropertyWidgetsFactory::setTopAndBottomBordersUsingStyleSheet(QWidget *widget,
197  const QString& extraStyleSheet)
198 {
199  KPropertyEditorView* view = nullptr;
200  if (widget->parentWidget()) {
202  }
203  const QColor gridLineColor(view ? view->gridLineColor() : KPropertyEditorView::defaultGridLineColor());
204  widget->setStyleSheet(
205  QString::fromLatin1("%1 { border-top: 1px solid %2;border-bottom: 1px solid %2; } %3")
206  .arg(QLatin1String(widget->metaObject()->className()))
207  .arg(gridLineColor.name()).arg(extraStyleSheet));
208 }
209 
210 //------------
211 
212 class Q_DECL_HIDDEN KPropertyEditorCreatorOptions::Private
213 {
214 public:
215  Private() : bordersVisible(false)
216  {
217  }
218  Private(const Private &other) {
219  copy(other);
220  }
221 #define KPropertyEditorCreatorOptionsPrivateArgs(o) std::tie(o.bordersVisible)
222  void copy(const Private &other) {
223  KPropertyEditorCreatorOptionsPrivateArgs((*this)) = KPropertyEditorCreatorOptionsPrivateArgs(other);
224  }
225  bool operator==(const Private &other) const {
226  return KPropertyEditorCreatorOptionsPrivateArgs((*this)) == KPropertyEditorCreatorOptionsPrivateArgs(other);
227  }
228  bool bordersVisible;
229 };
230 
231 KPropertyEditorCreatorOptions::KPropertyEditorCreatorOptions()
232  : d(new Private)
233 {
234 }
235 
236 KPropertyEditorCreatorOptions::KPropertyEditorCreatorOptions(const KPropertyEditorCreatorOptions &other)
237  : d(new Private(*other.d))
238 {
239 }
240 
241 KPropertyEditorCreatorOptions::~KPropertyEditorCreatorOptions()
242 {
243  delete d;
244 }
245 
247 {
248  return d->bordersVisible;
249 }
250 
251 void KPropertyEditorCreatorOptions::setBordersVisible(bool visible)
252 {
253  d->bordersVisible = visible;
254 }
255 
257 {
258  if (this != &other) {
259  d->copy(*other.d);
260  }
261  return *this;
262 }
263 
265 {
266  return *d == *other.d;
267 }
268 
269 //------------
270 
271 class Q_DECL_HIDDEN KPropertyEditorCreatorInterface::Private
272 {
273 public:
274  Private()
275  {
276  }
277 
280 };
281 
282 KPropertyEditorCreatorInterface::KPropertyEditorCreatorInterface()
283  : d(new Private)
284 {
285 }
286 
287 KPropertyEditorCreatorInterface::~KPropertyEditorCreatorInterface()
288 {
289  delete d;
290 }
291 
292 QWidget* KPropertyEditorCreatorInterface::createEditor(int type, QWidget *parent,
293  const QStyleOptionViewItem & option,
294  const QModelIndex & index) const
295 {
296  Q_UNUSED(type);
297  Q_UNUSED(option);
298  Q_UNUSED(index);
299  return new KPropertyStringEditor(parent);
300 }
301 
303 {
304  return &d->options;
305 }
306 
308 {
309  return &d->options;
310 }
311 
312 //------------
313 
314 KPropertyValuePainterInterface::KPropertyValuePainterInterface()
315 {
316 }
317 
318 KPropertyValuePainterInterface::~KPropertyValuePainterInterface()
319 {
320 }
321 
322 //static
323 void KPropertyValuePainterInterface::paint(const KPropertyValueDisplayInterface *iface,
324  QPainter *painter, const QStyleOptionViewItem &option,
325  const QModelIndex &index)
326 {
327  const KPropertyUtilsPrivate::PainterSaver saver(painter);
328  QRect r(option.rect);
329  r.setLeft(r.left() + 1);
330  KProperty *prop = KPropertyUtils::propertyForIndex(index);
332  iface->propertyValueToString(prop, QLocale()));
333 }
void setStyleSheet(const QString &styleSheet)
bool operator==(const KPropertyEditorCreatorOptions &other) const
virtual const QMetaObject * metaObject() const const
QWidget * viewport() const const
AlignLeft
An interface for editor widget creators.
bool operator==(const Qt3DRender::QGraphicsApiFilter &reference, const Qt3DRender::QGraphicsApiFilter &sample)
KIOCORE_EXPORT CopyJob * copy(const QUrl &src, const QUrl &dest, JobFlags flags=DefaultFlags)
QVariant property(const char *name) const const
Label widget that can be used for displaying text-based read-only items Used in KPropertyLabelCreator...
int left() const const
const KPropertyEditorCreatorOptions * options() const
Options for editor creating.
void drawText(const QPointF &position, const QString &text)
QColor gridLineColor() const
A widget for editing properties.
const char * className() const const
The base class representing a single property.
Definition: KProperty.h:95
virtual void paintEvent(QPaintEvent *) override
QWidget * parentWidget() const const
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
QString fromLatin1(const char *str, int size)
void setAutoFillBackground(bool enabled)
Options for altering the editor widget creation process.
KPropertyEditorCreatorOptions & operator=(const KPropertyEditorCreatorOptions &other)
Assigns other to this KPropertyEditorCreatorOptions.
T qobject_cast(QObject *object)
static QColor defaultGridLineColor()
void setLeft(int x)
void setContentsMargins(int left, int top, int right, int bottom)
Provides a specialized conversion of value to string depending on type.
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sun Aug 9 2020 23:10:57 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.