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