KProperty

KPropertyWidgetsFactory.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2008-2017 Jarosław Staniek <staniek@kde.org>
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
29class Q_DECL_HIDDEN KPropertyLabel::Private
30{
31public:
32 Private()
33 {
34 }
35 ~Private()
36 {
37 }
38
39 const KProperty *property;
41 QVariant value;
42};
43
44KPropertyLabel::KPropertyLabel(QWidget *parent, const KProperty *property,
46 : QLabel(parent), d(new Private)
47{
48 d->property = property;
49 d->iface = iface;
50 setAutoFillBackground(true);
51
52 KPropertyEditorView* view = nullptr;
53 if (parent) {
54 view = qobject_cast<KPropertyEditorView *>(parent->parentWidget());
55 }
56 const QColor gridLineColor(view ? view->gridLineColor()
57 : KPropertyEditorView::defaultGridLineColor());
58 const int top = 1 + (gridLineColor.isValid() ? 1 : 0);
59
60 setContentsMargins(0, top, 0, 0);
61 setIndent(1);
62}
63
64KPropertyLabel::~KPropertyLabel()
65{
66 delete d;
67}
68
69QVariant KPropertyLabel::value() const
70{
71 return d->value;
72}
73
74void KPropertyLabel::setValue(const QVariant& value)
75{
76 d->value = value;
77 setText(d->iface->propertyValueToString(d->property, QLocale()));
78}
79
80void KPropertyLabel::paintEvent( QPaintEvent * event )
81{
83 KPropertyWidgetsFactory::paintTopGridLine(this);
84}
85
86//---------------
87
88//! @internal
89class Q_DECL_HIDDEN KPropertyWidgetsFactory::Private
90{
91public:
92 Private()
93 {
94 }
95 ~Private()
96 {
97 qDeleteAll(editorCreatorsSet);
98 qDeleteAll(valuePaintersSet);
99 }
100
103
106};
107
108KPropertyWidgetsFactory::KPropertyWidgetsFactory()
109 : d( new Private )
110{
111}
112
113KPropertyWidgetsFactory::~KPropertyWidgetsFactory()
114{
115 delete d;
116}
117
118QHash<int, KPropertyEditorCreatorInterface*> KPropertyWidgetsFactory::editorCreators() const
119{
120 return d->editorCreators;
121}
122
123QHash<int, KPropertyValuePainterInterface*> KPropertyWidgetsFactory::valuePainters() const
124{
125 return d->valuePainters;
126}
127
128void 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
143void 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
158void 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
165void 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
173void KPropertyWidgetsFactory::paintTopGridLine(QWidget *widget)
174{
175 // paint top grid line
176 KPropertyEditorView* view = nullptr;
177 if (widget->parentWidget()) {
178 view = qobject_cast<KPropertyEditorView*>(widget->parentWidget()->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
196void KPropertyWidgetsFactory::setTopAndBottomBordersUsingStyleSheet(QWidget *widget,
197 const QString& extraStyleSheet)
198{
199 KPropertyEditorView* view = nullptr;
200 if (widget->parentWidget()) {
201 view = qobject_cast<KPropertyEditorView*>(widget->parentWidget()->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
212class Q_DECL_HIDDEN KPropertyEditorCreatorOptions::Private
213{
214public:
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
231KPropertyEditorCreatorOptions::KPropertyEditorCreatorOptions()
232 : d(new Private)
233{
234}
235
236KPropertyEditorCreatorOptions::KPropertyEditorCreatorOptions(const KPropertyEditorCreatorOptions &other)
237 : d(new Private(*other.d))
238{
239}
240
241KPropertyEditorCreatorOptions::~KPropertyEditorCreatorOptions()
242{
243 delete d;
244}
245
247{
248 return d->bordersVisible;
249}
250
251void 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
271class Q_DECL_HIDDEN KPropertyEditorCreatorInterface::Private
272{
273public:
274 Private()
275 {
276 }
277
278 //! Options for altering the editor widget creation process
280};
281
282KPropertyEditorCreatorInterface::KPropertyEditorCreatorInterface()
283 : d(new Private)
284{
285}
286
287KPropertyEditorCreatorInterface::~KPropertyEditorCreatorInterface()
288{
289 delete d;
290}
291
292QWidget* 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
311
312//------------
313
314KPropertyValuePainterInterface::KPropertyValuePainterInterface()
315{
316}
317
318KPropertyValuePainterInterface::~KPropertyValuePainterInterface()
319{
320}
321
322//static
323void 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}
An interface for editor widget creators.
const KPropertyEditorCreatorOptions * options() const
Options for editor creating.
Options for altering the editor widget creation process.
bool operator==(const KPropertyEditorCreatorOptions &other) const
KPropertyEditorCreatorOptions & operator=(const KPropertyEditorCreatorOptions &other)
Assigns other to this KPropertyEditorCreatorOptions.
A widget for editing properties.
Label widget that can be used for displaying text-based read-only items Used in KPropertyLabelCreator...
Provides a specialized conversion of value to string depending on type.
The base class representing a single property.
Definition KProperty.h:96
const QList< QKeySequence > & copy()
QWidget * viewport() const const
QHash::iterator insert(const Key &key, const T &value)
virtual bool event(QEvent *e) override
virtual void paintEvent(QPaintEvent *) override
void setText(const QString &)
const char * className() const const
virtual const QMetaObject * metaObject() const const
QVariant property(const char *name) const const
void drawText(const QPointF &position, const QString &text)
QSet::iterator insert(const T &value)
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
QString fromLatin1(const char *str, int size)
bool operator==(const Qt3DRender::QGraphicsApiFilter &reference, const Qt3DRender::QGraphicsApiFilter &sample)
AlignLeft
QWidget * parentWidget() const const
void setStyleSheet(const QString &styleSheet)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sun Feb 25 2024 18:41:55 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.