KProperty

KPropertyWidgetsPluginManager.cpp
1 /* This file is part of the KDE project
2  Copyright (C) 2008-2015 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 "KPropertyWidgetsPluginManager.h"
22 #include "KDefaultPropertyFactory.h"
23 #include "KPropertyEditorView.h"
24 #include "KPropertyUtils.h"
25 
27 class KPropertyWidgetsPluginManagerSingleton
28 {
29 public:
30  KPropertyWidgetsPluginManager object;
31 };
32 
34 class Q_DECL_HIDDEN KPropertyWidgetsPluginManager::Private
35 {
36 public:
37  Private()
38  {
39  }
40  ~Private()
41  {
42  }
43 
46 };
47 
48 Q_GLOBAL_STATIC(KPropertyWidgetsPluginManagerSingleton, _self)
49 
50 struct KPropertyWidgetsPluginManagerInitializer {
55  KPropertyWidgetsPluginManagerInitializer() {
56  KPropertyFactoryManager::addInitFunction(&initMe);
57  }
58  static void initMe() { KPropertyWidgetsPluginManager::self(); }
59 };
60 namespace {
61 KPropertyWidgetsPluginManagerInitializer init;
62 }
63 
64 //------------
65 
66 KPropertyWidgetsPluginManager::KPropertyWidgetsPluginManager()
67  : d(new Private)
68 {
69  registerFactory(new KDefaultPropertyFactory);
70 }
71 
72 KPropertyWidgetsPluginManager::~KPropertyWidgetsPluginManager()
73 {
74  delete d;
75 }
76 
77 KPropertyWidgetsPluginManager* KPropertyWidgetsPluginManager::self()
78 {
79  KPropertyFactoryManager::self(); // make sure KPropertyFactoryManager instance exists
80  // before KPropertyWidgetsPluginManager ctor is called
81  return &_self->object;
82 }
83 
84 void KPropertyWidgetsPluginManager::registerFactory(KPropertyWidgetsFactory *factory)
85 {
86  KPropertyFactoryManager::self()->registerFactory(factory);
87 
89  = factory->editorCreators().constEnd();
90  for (QHash<int, KPropertyEditorCreatorInterface*>::ConstIterator it( factory->editorCreators().constBegin() );
91  it != editorCreatorsItEnd; ++it)
92  {
93  d->editorCreators.insert(it.key(), it.value());
94  }
96  = factory->valuePainters().constEnd();
97  for (QHash<int, KPropertyValuePainterInterface*>::ConstIterator it( factory->valuePainters().constBegin() );
98  it != valuePaintersItEnd; ++it)
99  {
100  d->valuePainters.insert(it.key(), it.value());
101  }
102 }
103 
104 bool KPropertyWidgetsPluginManager::isEditorForTypeAvailable( int type ) const
105 {
106  return d->editorCreators.value(type);
107 }
108 
109 QWidget * 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) {
119  w->setObjectName(QLatin1String(property->name()));
120  if (!creator->options()->bordersVisible()) {
122  QColor gridLineColor(qobject_cast<KPropertyEditorView*>(parent->parentWidget()) ?
123  qobject_cast<KPropertyEditorView*>(parent->parentWidget())->gridLineColor()
124  : KPropertyEditorView::defaultGridLineColor());
125  QString cssClassName = QLatin1String(w->metaObject()->className());
126  cssClassName.replace(QLatin1String("KProperty"), QString());
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 
137 bool 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 
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
void setStyleSheet(const QString &styleSheet)
void fillRect(const QRectF &rectangle, const QBrush &brush)
QString name() const const
virtual const QMetaObject * metaObject() const const
An interface for editor widget creators.
QHash::const_iterator constEnd() const const
void setPen(const QColor &color)
void setObjectName(const QString &name)
const KPropertyEditorCreatorOptions * options() const
Options for editor creating.
A widget for editing properties.
const char * className() const const
QCA_EXPORT void init()
The base class representing a single property.
Definition: KProperty.h:95
QString & replace(int position, int n, QChar after)
QWidget * parentWidget() const const
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
QString fromLatin1(const char *str, int size)
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sun Aug 2 2020 23:02:00 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.