main.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the Qt Solutions component.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
10 **
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
13 ** met:
14 ** * Redistributions of source code must retain the above copyright
15 ** notice, this list of conditions and the following disclaimer.
16 ** * Redistributions in binary form must reproduce the above copyright
17 ** notice, this list of conditions and the following disclaimer in
18 ** the documentation and/or other materials provided with the
19 ** distribution.
20 ** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
21 ** of its contributors may be used to endorse or promote products derived
22 ** from this software without specific prior written permission.
23 **
24 **
25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
36 **
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40 
41 #include <QApplication>
42 #include <QMap>
43 #include <QDoubleSpinBox>
44 #include "qtpropertybrowser.h"
45 #include "qteditorfactory.h"
46 #include "qttreepropertybrowser.h"
47 
49 {
50  Q_OBJECT
51 public:
52  DecoratedDoublePropertyManager(QObject* parent = 0);
54 
55  QString prefix(const QtProperty* property) const;
56  QString suffix(const QtProperty* property) const;
57 public Q_SLOTS:
58  void setPrefix(QtProperty* property, const QString& prefix);
59  void setSuffix(QtProperty* property, const QString& suffix);
60 Q_SIGNALS:
61  void prefixChanged(QtProperty* property, const QString& prefix);
62  void suffixChanged(QtProperty* property, const QString& suffix);
63 protected:
64  QString valueText(const QtProperty* property) const;
65  virtual void initializeProperty(QtProperty* property);
66  virtual void uninitializeProperty(QtProperty* property);
67 private:
68  struct Data
69  {
70  QString prefix;
71  QString suffix;
72  };
73  QMap<const QtProperty*, Data> propertyToData;
74 };
75 
77  : QtDoublePropertyManager(parent)
78 {
79 }
80 
82 {
83 }
84 
85 QString DecoratedDoublePropertyManager::prefix(const QtProperty* property) const
86 {
87  if (!propertyToData.contains(property))
88  {
89  return QString();
90  }
91 
92  return propertyToData[property].prefix;
93 }
94 
95 QString DecoratedDoublePropertyManager::suffix(const QtProperty* property) const
96 {
97  if (!propertyToData.contains(property))
98  {
99  return QString();
100  }
101 
102  return propertyToData[property].suffix;
103 }
104 
105 void DecoratedDoublePropertyManager::setPrefix(QtProperty* property, const QString& prefix)
106 {
107  if (!propertyToData.contains(property))
108  {
109  return;
110  }
111 
112  DecoratedDoublePropertyManager::Data data = propertyToData[property];
113 
114  if (data.prefix == prefix)
115  {
116  return;
117  }
118 
119  data.prefix = prefix;
120  propertyToData[property] = data;
121 
122  emit propertyChanged(property);
123  emit prefixChanged(property, prefix);
124 }
125 
126 void DecoratedDoublePropertyManager::setSuffix(QtProperty* property, const QString& suffix)
127 {
128  if (!propertyToData.contains(property))
129  {
130  return;
131  }
132 
133  DecoratedDoublePropertyManager::Data data = propertyToData[property];
134 
135  if (data.suffix == suffix)
136  {
137  return;
138  }
139 
140  data.suffix = suffix;
141  propertyToData[property] = data;
142 
143  emit propertyChanged(property);
144  emit suffixChanged(property, suffix);
145 }
146 
148 {
149  QString text = QtDoublePropertyManager::valueText(property);
150 
151  if (!propertyToData.contains(property))
152  {
153  return text;
154  }
155 
156  DecoratedDoublePropertyManager::Data data = propertyToData[property];
157  text = data.prefix + text + data.suffix;
158 
159  return text;
160 }
161 
163 {
164  propertyToData[property] = DecoratedDoublePropertyManager::Data();
166 }
167 
169 {
170  propertyToData.remove(property);
172 }
173 
174 
175 class DecoratedDoubleSpinBoxFactory : public QtAbstractEditorFactory<DecoratedDoublePropertyManager>
176 {
177  Q_OBJECT
178 public:
179  DecoratedDoubleSpinBoxFactory(QObject* parent = 0);
181 protected:
183  QWidget* createEditor(DecoratedDoublePropertyManager* manager, QtProperty* property,
184  QWidget* parent);
186 private slots:
187 
188  void slotPrefixChanged(QtProperty* property, const QString& prefix);
189  void slotSuffixChanged(QtProperty* property, const QString& prefix);
190  void slotEditorDestroyed(QObject* object);
191 private:
192  /* We delegate responsibilities for QtDoublePropertyManager, which is a base class
193  of DecoratedDoublePropertyManager to appropriate QtDoubleSpinBoxFactory */
194  QtDoubleSpinBoxFactory* originalFactory;
195  QMap<QtProperty*, QList<QDoubleSpinBox*> > createdEditors;
196  QMap<QDoubleSpinBox*, QtProperty*> editorToProperty;
197 };
198 
201 {
202  originalFactory = new QtDoubleSpinBoxFactory(this);
203 }
204 
206 {
207  // not need to delete editors because they will be deleted by originalFactory in its destructor
208 }
209 
211 {
212  originalFactory->addPropertyManager(manager);
213  connect(manager, SIGNAL(prefixChanged(QtProperty*, const QString&)), this, SLOT(slotPrefixChanged(QtProperty*, const QString&)));
214  connect(manager, SIGNAL(suffixChanged(QtProperty*, const QString&)), this, SLOT(slotSuffixChanged(QtProperty*, const QString&)));
215 }
216 
218  QWidget* parent)
219 {
220  QtAbstractEditorFactoryBase* base = originalFactory;
221  QWidget* w = base->createEditor(property, parent);
222 
223  if (!w)
224  {
225  return 0;
226  }
227 
228  QDoubleSpinBox* spinBox = qobject_cast<QDoubleSpinBox*>(w);
229 
230  if (!spinBox)
231  {
232  return 0;
233  }
234 
235  spinBox->setPrefix(manager->prefix(property));
236  spinBox->setSuffix(manager->suffix(property));
237 
238  createdEditors[property].append(spinBox);
239  editorToProperty[spinBox] = property;
240 
241  return spinBox;
242 }
243 
245 {
246  originalFactory->removePropertyManager(manager);
247  disconnect(manager, SIGNAL(prefixChanged(QtProperty*, const QString&)), this, SLOT(slotPrefixChanged(QtProperty*, const QString&)));
248  disconnect(manager, SIGNAL(suffixChanged(QtProperty*, const QString&)), this, SLOT(slotSuffixChanged(QtProperty*, const QString&)));
249 }
250 
251 void DecoratedDoubleSpinBoxFactory::slotPrefixChanged(QtProperty* property, const QString& prefix)
252 {
253  if (!createdEditors.contains(property))
254  {
255  return;
256  }
257 
258  DecoratedDoublePropertyManager* manager = propertyManager(property);
259 
260  if (!manager)
261  {
262  return;
263  }
264 
265  QList<QDoubleSpinBox*> editors = createdEditors[property];
266  QListIterator<QDoubleSpinBox*> itEditor(editors);
267 
268  while (itEditor.hasNext())
269  {
270  QDoubleSpinBox* editor = itEditor.next();
271  editor->setPrefix(prefix);
272  }
273 }
274 
275 void DecoratedDoubleSpinBoxFactory::slotSuffixChanged(QtProperty* property, const QString& prefix)
276 {
277  if (!createdEditors.contains(property))
278  {
279  return;
280  }
281 
282  DecoratedDoublePropertyManager* manager = propertyManager(property);
283 
284  if (!manager)
285  {
286  return;
287  }
288 
289  QList<QDoubleSpinBox*> editors = createdEditors[property];
290  QListIterator<QDoubleSpinBox*> itEditor(editors);
291 
292  while (itEditor.hasNext())
293  {
294  QDoubleSpinBox* editor = itEditor.next();
295  editor->setSuffix(prefix);
296  }
297 }
298 
299 void DecoratedDoubleSpinBoxFactory::slotEditorDestroyed(QObject* object)
300 {
301  QMap<QDoubleSpinBox*, QtProperty*>::ConstIterator itEditor =
302  editorToProperty.constBegin();
303 
304  while (itEditor != editorToProperty.constEnd())
305  {
306  if (itEditor.key() == object)
307  {
308  QDoubleSpinBox* editor = itEditor.key();
309  QtProperty* property = itEditor.value();
310  editorToProperty.remove(editor);
311  createdEditors[property].removeAll(editor);
312 
313  if (createdEditors[property].isEmpty())
314  {
315  createdEditors.remove(property);
316  }
317 
318  return;
319  }
320 
321  itEditor++;
322  }
323 }
324 
325 
326 int main(int argc, char** argv)
327 {
328  QApplication app(argc, argv);
329 
330  QtDoublePropertyManager* undecoratedManager = new QtDoublePropertyManager();
331  QtProperty* undecoratedProperty = undecoratedManager->addProperty("Undecorated");
332  undecoratedManager->setValue(undecoratedProperty, 123.45);
333 
335  QtProperty* decoratedProperty = decoratedManager->addProperty("Decorated");
336  decoratedManager->setPrefix(decoratedProperty, "speed: ");
337  decoratedManager->setSuffix(decoratedProperty, " km/h");
338  decoratedManager->setValue(decoratedProperty, 123.45);
339 
340  QtDoubleSpinBoxFactory* undecoratedFactory = new QtDoubleSpinBoxFactory();
342 
344  editor->setFactoryForManager(undecoratedManager, undecoratedFactory);
345  editor->setFactoryForManager(decoratedManager, decoratedFactory);
346  editor->addProperty(undecoratedProperty);
347  editor->addProperty(decoratedProperty);
348  editor->show();
349 
350  int ret = app.exec();
351 
352  delete decoratedFactory;
353  delete decoratedManager;
354  delete undecoratedFactory;
355  delete undecoratedManager;
356  delete editor;
357 
358  return ret;
359 }
360 
361 #include "main.moc"
DecoratedDoublePropertyManager::~DecoratedDoublePropertyManager
~DecoratedDoublePropertyManager()
Definition: main.cpp:81
QtAbstractPropertyBrowser::addProperty
QtBrowserItem * addProperty(QtProperty *property)
Definition: qtpropertybrowser.cpp:1969
armarx::aron::ret
ReaderT::InputType T & ret
Definition: rw.h:21
DecoratedDoubleSpinBoxFactory::disconnectPropertyManager
void disconnectPropertyManager(DecoratedDoublePropertyManager *manager)
Definition: main.cpp:244
QtAbstractPropertyManager::addProperty
QtProperty * addProperty(const QString &name=QString())
Definition: qtpropertybrowser.cpp:826
QtTreePropertyBrowser
The QtTreePropertyBrowser class provides QTreeWidget based property browser.
Definition: qttreepropertybrowser.h:51
QtDoublePropertyManager::initializeProperty
void initializeProperty(QtProperty *property) override
Definition: qtpropertymanager.cpp:1469
DecoratedDoublePropertyManager::initializeProperty
virtual void initializeProperty(QtProperty *property)
Definition: main.cpp:162
qteditorfactory.h
QtDoublePropertyManager::uninitializeProperty
void uninitializeProperty(QtProperty *property) override
Definition: qtpropertymanager.cpp:1477
QtDoublePropertyManager::valueText
QString valueText(const QtProperty *property) const override
Definition: qtpropertymanager.cpp:1270
QtDoubleSpinBoxFactory
The QtDoubleSpinBoxFactory class provides QDoubleSpinBox widgets for properties created by QtDoublePr...
Definition: qteditorfactory.h:146
DecoratedDoublePropertyManager::prefix
QString prefix(const QtProperty *property) const
Definition: main.cpp:85
QtDoublePropertyManager::setValue
void setValue(QtProperty *property, double val)
Definition: qtpropertymanager.cpp:1293
DecoratedDoublePropertyManager::uninitializeProperty
virtual void uninitializeProperty(QtProperty *property)
Definition: main.cpp:168
QtProperty
The QtProperty class encapsulates an instance of a property.
Definition: qtpropertybrowser.h:71
DecoratedDoublePropertyManager::suffix
QString suffix(const QtProperty *property) const
Definition: main.cpp:95
qtpropertybrowser.h
QtAbstractEditorFactoryBase
The QtAbstractEditorFactoryBase provides an interface for editor factories.
Definition: qtpropertybrowser.h:147
DecoratedDoubleSpinBoxFactory::DecoratedDoubleSpinBoxFactory
DecoratedDoubleSpinBoxFactory(QObject *parent=0)
Definition: main.cpp:199
QtAbstractEditorFactoryBase::createEditor
virtual QWidget * createEditor(QtProperty *property, QWidget *parent)=0
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
QtDoublePropertyManager
The QtDoublePropertyManager provides and manages double properties.
Definition: qtpropertymanager.h:136
QtAbstractEditorFactory
The QtAbstractEditorFactory is the base template class for editor factories.
Definition: qtpropertybrowser.h:164
DecoratedDoubleSpinBoxFactory
Definition: main.cpp:175
main
int main(int argc, char *argv[])
Definition: main.cpp:31
QtAbstractEditorFactory< DecoratedDoublePropertyManager >::propertyManager
DecoratedDoublePropertyManager * propertyManager(QtProperty *property) const
Definition: qtpropertybrowser.h:212
QtAbstractEditorFactory::removePropertyManager
void removePropertyManager(PropertyManager *manager)
Definition: qtpropertybrowser.h:196
DecoratedDoublePropertyManager::setPrefix
void setPrefix(QtProperty *property, const QString &prefix)
Definition: main.cpp:105
QtAbstractEditorFactory::addPropertyManager
void addPropertyManager(PropertyManager *manager)
Definition: qtpropertybrowser.h:184
DecoratedDoublePropertyManager::setSuffix
void setSuffix(QtProperty *property, const QString &suffix)
Definition: main.cpp:126
DecoratedDoubleSpinBoxFactory::createEditor
QWidget * createEditor(DecoratedDoublePropertyManager *manager, QtProperty *property, QWidget *parent)
Definition: main.cpp:217
qttreepropertybrowser.h
DecoratedDoubleSpinBoxFactory::connectPropertyManager
void connectPropertyManager(DecoratedDoublePropertyManager *manager)
Definition: main.cpp:210
DecoratedDoublePropertyManager::DecoratedDoublePropertyManager
DecoratedDoublePropertyManager(QObject *parent=0)
Definition: main.cpp:76
DecoratedDoublePropertyManager::valueText
QString valueText(const QtProperty *property) const
Definition: main.cpp:147
QtAbstractPropertyManager::propertyChanged
void propertyChanged(QtProperty *property)
DecoratedDoubleSpinBoxFactory::~DecoratedDoubleSpinBoxFactory
~DecoratedDoubleSpinBoxFactory()
Definition: main.cpp:205
DecoratedDoublePropertyManager::suffixChanged
void suffixChanged(QtProperty *property, const QString &suffix)
DecoratedDoublePropertyManager::prefixChanged
void prefixChanged(QtProperty *property, const QString &prefix)
QtAbstractPropertyBrowser::setFactoryForManager
void setFactoryForManager(PropertyManager *manager, QtAbstractEditorFactory< PropertyManager > *factory)
Definition: qtpropertybrowser.h:304
DecoratedDoublePropertyManager
Definition: main.cpp:48