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 
43 #include "qteditorfactory.h"
44 #include "qttreepropertybrowser.h"
45 #include "qtvariantproperty.h"
46 
48 {
49  Q_OBJECT
50 public:
51  VariantManager(QObject* parent = 0);
53 
54  virtual QVariant value(const QtProperty* property) const;
55  virtual int valueType(int propertyType) const;
56  virtual bool isPropertyTypeSupported(int propertyType) const;
57 
58  QString valueText(const QtProperty* property) const;
59 
60 public slots:
61  virtual void setValue(QtProperty* property, const QVariant& val);
62 
63 protected:
64  virtual void initializeProperty(QtProperty* property);
65  virtual void uninitializeProperty(QtProperty* property);
66 private slots:
67  void slotValueChanged(QtProperty* property, const QVariant& value);
68  void slotPropertyDestroyed(QtProperty* property);
69 
70 private:
71  struct Data
72  {
73  QVariant value;
76  };
77 
78  QMap<const QtProperty*, Data> propertyToData;
79  QMap<const QtProperty*, QtProperty*> xToProperty;
80  QMap<const QtProperty*, QtProperty*> yToProperty;
81 };
82 
84 {
85  connect(this,
86  SIGNAL(valueChanged(QtProperty*, const QVariant&)),
87  this,
88  SLOT(slotValueChanged(QtProperty*, const QVariant&)));
89  connect(this,
90  SIGNAL(propertyDestroyed(QtProperty*)),
91  this,
92  SLOT(slotPropertyDestroyed(QtProperty*)));
93 }
94 
96 {
97 }
98 
99 void
100 VariantManager::slotValueChanged(QtProperty* property, const QVariant& value)
101 {
102  if (xToProperty.contains(property))
103  {
104  QtProperty* pointProperty = xToProperty[property];
105  QVariant v = this->value(pointProperty);
106  QPointF p = v.value<QPointF>();
107  p.setX(value.value<double>());
108  setValue(pointProperty, p);
109  }
110  else if (yToProperty.contains(property))
111  {
112  QtProperty* pointProperty = yToProperty[property];
113  QVariant v = this->value(pointProperty);
114  QPointF p = v.value<QPointF>();
115  p.setY(value.value<double>());
116  setValue(pointProperty, p);
117  }
118 }
119 
120 void
121 VariantManager::slotPropertyDestroyed(QtProperty* property)
122 {
123  if (xToProperty.contains(property))
124  {
125  QtProperty* pointProperty = xToProperty[property];
126  propertyToData[pointProperty].x = 0;
127  xToProperty.remove(property);
128  }
129  else if (yToProperty.contains(property))
130  {
131  QtProperty* pointProperty = yToProperty[property];
132  propertyToData[pointProperty].y = 0;
133  yToProperty.remove(property);
134  }
135 }
136 
137 bool
139 {
140  if (propertyType == QVariant::PointF)
141  {
142  return true;
143  }
144 
146 }
147 
148 int
149 VariantManager::valueType(int propertyType) const
150 {
151  if (propertyType == QVariant::PointF)
152  {
153  return QVariant::PointF;
154  }
155 
157 }
158 
159 QVariant
160 VariantManager::value(const QtProperty* property) const
161 {
162  if (propertyToData.contains(property))
163  {
164  return propertyToData[property].value;
165  }
166 
167  return QtVariantPropertyManager::value(property);
168 }
169 
170 QString
171 VariantManager::valueText(const QtProperty* property) const
172 {
173  if (propertyToData.contains(property))
174  {
175  QVariant v = propertyToData[property].value;
176  QPointF p = v.value<QPointF>();
177  return QString(tr("(%1, %2)").arg(QString::number(p.x())).arg(QString::number(p.y())));
178  }
179 
180  return QtVariantPropertyManager::valueText(property);
181 }
182 
183 void
184 VariantManager::setValue(QtProperty* property, const QVariant& val)
185 {
186  if (propertyToData.contains(property))
187  {
188  if (val.type() != QVariant::PointF && !val.canConvert(QVariant::PointF))
189  {
190  return;
191  }
192 
193  QPointF p = val.value<QPointF>();
194  Data d = propertyToData[property];
195  d.value = p;
196 
197  if (d.x)
198  {
199  d.x->setValue(p.x());
200  }
201 
202  if (d.y)
203  {
204  d.y->setValue(p.y());
205  }
206 
207  propertyToData[property] = d;
208  emit propertyChanged(property);
209  emit valueChanged(property, p);
210  return;
211  }
212 
213  QtVariantPropertyManager::setValue(property, val);
214 }
215 
216 void
218 {
219  if (propertyType(property) == QVariant::PointF)
220  {
221  Data d;
222 
223  d.value = QPointF(0, 0);
224 
225  VariantManager* that = (VariantManager*)this;
226 
227  d.x = that->addProperty(QVariant::Double);
228  d.x->setPropertyName(tr("Position X"));
229  property->addSubProperty(d.x);
230  xToProperty[d.x] = property;
231 
232  d.y = that->addProperty(QVariant::Double);
233  d.y->setPropertyName(tr("Position Y"));
234  property->addSubProperty(d.y);
235  yToProperty[d.y] = property;
236 
237  propertyToData[property] = d;
238  }
239 
241 }
242 
243 void
245 {
246  if (propertyToData.contains(property))
247  {
248  Data d = propertyToData[property];
249 
250  if (d.x)
251  {
252  xToProperty.remove(d.x);
253  }
254 
255  if (d.y)
256  {
257  yToProperty.remove(d.y);
258  }
259 
260  propertyToData.remove(property);
261  }
262 
264 }
265 
266 int
267 main(int argc, char** argv)
268 {
269  QApplication app(argc, argv);
270 
271  VariantManager* variantManager = new VariantManager();
272 
273  QtVariantProperty* item = variantManager->addProperty(QVariant::PointF, "PointF Property");
274  item->setValue(QPointF(2.5, 13.13));
275 
276  QtVariantEditorFactory* variantFactory = new QtVariantEditorFactory();
277 
279  QtVariantPropertyManager* varMan = variantManager;
280  ed1.setFactoryForManager(varMan, variantFactory);
281  ed1.addProperty(item);
282 
283 
284  ed1.show();
285 
286  int ret = app.exec();
287 
288  delete variantFactory;
289  delete variantManager;
290 
291  return ret;
292 }
293 
294 #include "main.moc"
QtVariantPropertyManager::addProperty
virtual QtVariantProperty * addProperty(int propertyType, const QString &name=QString())
Definition: qtvariantproperty.cpp:1651
QtVariantPropertyManager::uninitializeProperty
void uninitializeProperty(QtProperty *property) override
Definition: qtvariantproperty.cpp:2606
QtAbstractPropertyBrowser::addProperty
QtBrowserItem * addProperty(QtProperty *property)
Definition: qtpropertybrowser.cpp:2050
QtVariantPropertyManager::initializeProperty
void initializeProperty(QtProperty *property) override
Definition: qtvariantproperty.cpp:2561
armarx::aron::ret
ReaderT::InputType T & ret
Definition: rw.h:13
QtTreePropertyBrowser
The QtTreePropertyBrowser class provides QTreeWidget based property browser.
Definition: qttreepropertybrowser.h:51
QtVariantPropertyManager::valueChanged
void valueChanged(QtProperty *property, const QVariant &val)
qteditorfactory.h
QtVariantPropertyManager
The QtVariantPropertyManager class provides and manages QVariant based properties.
Definition: qtvariantproperty.h:78
QtProperty
The QtProperty class encapsulates an instance of a property.
Definition: qtpropertybrowser.h:71
VariantManager::isPropertyTypeSupported
virtual bool isPropertyTypeSupported(int propertyType) const
Definition: main.cpp:138
QtVariantPropertyManager::setValue
virtual void setValue(QtProperty *property, const QVariant &val)
Definition: qtvariantproperty.cpp:2143
armarx::VariantType::Double
const VariantTypeId Double
Definition: Variant.h:920
QtVariantProperty
The QtVariantProperty class is a convenience class handling QVariant based properties.
Definition: qtvariantproperty.h:56
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:855
QtVariantPropertyManager::value
virtual QVariant value(const QtProperty *property) const
Definition: qtvariantproperty.cpp:1682
VariantManager::uninitializeProperty
virtual void uninitializeProperty(QtProperty *property)
Definition: main.cpp:244
QtVariantPropertyManager::isPropertyTypeSupported
virtual bool isPropertyTypeSupported(int propertyType) const
Definition: qtvariantproperty.cpp:1626
qtvariantproperty.h
QtVariantPropertyManager::valueText
QString valueText(const QtProperty *property) const override
Definition: qtvariantproperty.cpp:2539
main
int main(int argc, char *argv[])
Definition: main.cpp:32
VariantManager::valueText
QString valueText(const QtProperty *property) const
Definition: main.cpp:171
QtVariantEditorFactory
The QtVariantEditorFactory class provides widgets for properties created by QtVariantPropertyManager ...
Definition: qtvariantproperty.h:166
QtVariantProperty::setValue
void setValue(const QVariant &value)
Definition: qtvariantproperty.cpp:284
QtAbstractPropertyManager::propertyDestroyed
void propertyDestroyed(QtProperty *property)
VariantManager::setValue
virtual void setValue(QtProperty *property, const QVariant &val)
Definition: main.cpp:184
VariantManager::VariantManager
VariantManager(QObject *parent=0)
Definition: main.cpp:83
armarx::ctrlutil::v
double v(double t, double v0, double a0, double j)
Definition: CtrlUtil.h:39
QtVariantPropertyManager::propertyType
int propertyType(const QtProperty *property) const
Definition: qtvariantproperty.cpp:1829
qttreepropertybrowser.h
VariantManager::value
virtual QVariant value(const QtProperty *property) const
Definition: main.cpp:160
QtAbstractPropertyManager::propertyChanged
void propertyChanged(QtProperty *property)
QtVariantPropertyManager::valueType
int valueType(const QtProperty *property) const
Definition: qtvariantproperty.cpp:1801
QtAbstractPropertyBrowser::setFactoryForManager
void setFactoryForManager(PropertyManager *manager, QtAbstractEditorFactory< PropertyManager > *factory)
Definition: qtpropertybrowser.h:327
VariantManager
Definition: main.cpp:47
VariantManager::valueType
virtual int valueType(int propertyType) const
Definition: main.cpp:149
VariantManager::initializeProperty
virtual void initializeProperty(QtProperty *property)
Definition: main.cpp:217
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
VariantManager::~VariantManager
~VariantManager()
Definition: main.cpp:95