KProperty

KPropertyWidgetsPluginManager.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2008-2015 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 "KPropertyWidgetsPluginManager.h"
22#include "KDefaultPropertyFactory.h"
23#include "KPropertyEditorView.h"
24#include "KPropertyUtils.h"
25
26//! Class for access to KPropertyWidgetsPluginManager constructor
27class KPropertyWidgetsPluginManagerSingleton
28{
29public:
30 KPropertyWidgetsPluginManager object;
31};
32
33//! @internal
34class Q_DECL_HIDDEN KPropertyWidgetsPluginManager::Private
35{
36public:
37 Private()
38 {
39 }
40 ~Private()
41 {
42 }
43
46};
47
48Q_GLOBAL_STATIC(KPropertyWidgetsPluginManagerSingleton, _self)
49
50//! @internal Make sure sure the KPropertyWidgetsPluginManager is created after
51//! KPropertyFactoryManager (delayed). Unless KPropertyFactoryManager is created,
52//! KPropertyWidgetsPluginManager isn't created.
53//! @todo is this worth putting in a reusable macro?
54struct KPropertyWidgetsPluginManagerInitializer {
55 KPropertyWidgetsPluginManagerInitializer() {
56 KPropertyFactoryManager::addInitFunction(&initMe);
57 }
58 static void initMe() { KPropertyWidgetsPluginManager::self(); }
59};
60namespace {
61KPropertyWidgetsPluginManagerInitializer init;
62}
63
64//------------
65
66KPropertyWidgetsPluginManager::KPropertyWidgetsPluginManager()
67 : d(new Private)
68{
69 registerFactory(new KDefaultPropertyFactory);
70}
71
72KPropertyWidgetsPluginManager::~KPropertyWidgetsPluginManager()
73{
74 delete d;
75}
76
77KPropertyWidgetsPluginManager* KPropertyWidgetsPluginManager::self()
78{
79 KPropertyFactoryManager::self(); // make sure KPropertyFactoryManager instance exists
80 // before KPropertyWidgetsPluginManager ctor is called
81 return &_self->object;
82}
83
84void KPropertyWidgetsPluginManager::registerFactory(KPropertyWidgetsFactory *factory)
85{
86 KPropertyFactoryManager::self()->registerFactory(factory);
87
89 = factory->editorCreators().constEnd();
91 it != editorCreatorsItEnd; ++it)
92 {
93 d->editorCreators.insert(it.key(), it.value());
94 }
96 = factory->valuePainters().constEnd();
98 it != valuePaintersItEnd; ++it)
99 {
100 d->valuePainters.insert(it.key(), it.value());
101 }
102}
103
104bool KPropertyWidgetsPluginManager::isEditorForTypeAvailable( int type ) const
105{
106 return d->editorCreators.value(type);
107}
108
109QWidget * KPropertyWidgetsPluginManager::createEditor(
110 int type, QWidget *parent,
111 const QStyleOptionViewItem & option, const QModelIndex & index ) const
112{
113 const KPropertyEditorCreatorInterface *creator = d->editorCreators.value(type);
114 if (!creator)
115 return nullptr;
116 QWidget *w = creator->createEditor(type, parent, option, index);
117 KProperty *property = KPropertyUtils::propertyForIndex(index);
118 if (w && property) {
120 if (!creator->options()->bordersVisible()) {
121//! @todo get real border color from the palette
122 QColor gridLineColor(qobject_cast<KPropertyEditorView*>(parent->parentWidget()) ?
123 qobject_cast<KPropertyEditorView*>(parent->parentWidget())->gridLineColor()
125 QString cssClassName = QLatin1String(w->metaObject()->className());
126 cssClassName.replace(QLatin1String("KProperty"), QString()); //!< @todo
127 QString css =
128 QString::fromLatin1("%1 { border-top: 1px solid %2; } ")
129 .arg(cssClassName).arg(gridLineColor.name());
130// kprDebug() << css;
131 w->setStyleSheet(css);
132 }
133 }
134 return w;
135}
136
137bool KPropertyWidgetsPluginManager::paint( int type, QPainter * painter,
138 const QStyleOptionViewItem & option, const QModelIndex & index ) const
139{
140 const KPropertyValuePainterInterface *_painter = d->valuePainters.value(type);
141 if (!_painter)
142 return false;
143 QStyleOptionViewItem realOption(option);
144 if (option.state & QStyle::State_Selected) {
145 // paint background because there may be editor widget with no autoFillBackground set
146 realOption.palette.setBrush(QPalette::Text, realOption.palette.highlightedText());
147 painter->fillRect(realOption.rect, realOption.palette.highlight());
148 }
149 painter->setPen(realOption.palette.text().color());
150 _painter->paint(painter, realOption, index);
151 return true;
152}
153
154//! @todo
155#if 0
156 const int type = parent->type();
157/*
158 CustomPropertyFactory *factory = d->registeredWidgets[type];
159 if (factory)
160 return factory->createCustomProperty(parent);
161*/
162 switch (type) {
163 case Size:
164 case Size_Width:
165 case Size_Height:
166 return new SizeCustomProperty(parent);
167 case Point:
168 case Point_X:
169 case Point_Y:
170 return new PointCustomProperty(parent);
171 case Rect:
172 case Rect_X:
173 case Rect_Y:
174 case Rect_Width:
175 case Rect_Height:
176 return new RectCustomProperty(parent);
177 case SizePolicy:
178/* case SizePolicy_HorizontalStretch:
179 case SizePolicy_VerticalStretch:
180 case SizePolicy_HorizontalPolicy:
181 case SizePolicy_VerticalPolicy:*/
182 return new SizePolicyCustomProperty(parent);
183 default:;
184 }
185 return 0;
186#endif
An interface for editor widget creators.
const KPropertyEditorCreatorOptions * options() const
Options for editor creating.
static QColor defaultGridLineColor()
The base class representing a single property.
Definition KProperty.h:96
Type type(const QSqlDatabase &db)
QCA_EXPORT void init()
const_iterator constBegin() const const
const_iterator constEnd() const const
iterator insert(const Key &key, const T &value)
T value(const Key &key) const const
const char * className() const const
virtual const QMetaObject * metaObject() const const
QObject * parent() const const
QVariant property(const char *name) const const
T qobject_cast(QObject *object)
void setObjectName(QAnyStringView name)
void fillRect(const QRect &rectangle, QGradient::Preset preset)
void setPen(Qt::PenStyle style)
QString arg(Args &&... args) const const
QString fromLatin1(QByteArrayView str)
QString & replace(QChar before, QChar after, Qt::CaseSensitivity cs)
void setStyleSheet(const QString &styleSheet)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:04:09 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.