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"
45#include "qtvariantproperty.h"
46
48{
49 Q_OBJECT
50public:
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
60public slots:
61 virtual void setValue(QtProperty* property, const QVariant& val);
62
63protected:
64 virtual void initializeProperty(QtProperty* property);
65 virtual void uninitializeProperty(QtProperty* property);
66private slots:
67 void slotValueChanged(QtProperty* property, const QVariant& value);
68 void slotPropertyDestroyed(QtProperty* property);
69
70private:
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,
91 this,
92 SLOT(slotPropertyDestroyed(QtProperty*)));
93}
94
98
99void
100VariantManager::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
120void
121VariantManager::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
137bool
139{
140 if (propertyType == QVariant::PointF)
141 {
142 return true;
143 }
144
146}
147
148int
150{
151 if (propertyType == QVariant::PointF)
152 {
153 return QVariant::PointF;
154 }
155
157}
158
159QVariant
160VariantManager::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
170QString
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
181}
182
183void
184VariantManager::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
214}
215
216void
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
243void
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
266int
267main(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"
QtBrowserItem * addProperty(QtProperty *property)
void setFactoryForManager(PropertyManager *manager, QtAbstractEditorFactory< PropertyManager > *factory)
void propertyDestroyed(QtProperty *property)
void propertyChanged(QtProperty *property)
The QtProperty class encapsulates an instance of a property.
void setPropertyName(const QString &text)
The QtTreePropertyBrowser class provides QTreeWidget based property browser.
The QtVariantEditorFactory class provides widgets for properties created by QtVariantPropertyManager ...
The QtVariantPropertyManager class provides and manages QVariant based properties.
void initializeProperty(QtProperty *property) override
virtual QtVariantProperty * addProperty(int propertyType, const QString &name=QString())
virtual QVariant value(const QtProperty *property) const
QtVariantPropertyManager(QObject *parent=0)
virtual void setValue(QtProperty *property, const QVariant &val)
void valueChanged(QtProperty *property, const QVariant &val)
void uninitializeProperty(QtProperty *property) override
virtual bool isPropertyTypeSupported(int propertyType) const
int propertyType(const QtProperty *property) const
QString valueText(const QtProperty *property) const override
int valueType(const QtProperty *property) const
The QtVariantProperty class is a convenience class handling QVariant based properties.
void setValue(const QVariant &value)
virtual int valueType(int propertyType) const
Definition main.cpp:149
virtual QVariant value(const QtProperty *property) const
Definition main.cpp:160
QString valueText(const QtProperty *property) const
Definition main.cpp:171
virtual void setValue(QtProperty *property, const QVariant &val)
Definition main.cpp:184
~VariantManager()
Definition main.cpp:95
virtual void uninitializeProperty(QtProperty *property)
Definition main.cpp:244
VariantManager(QObject *parent=0)
Definition main.cpp:83
virtual void initializeProperty(QtProperty *property)
Definition main.cpp:217
virtual bool isPropertyTypeSupported(int propertyType) const
Definition main.cpp:138
double v(double t, double v0, double a0, double j)
Definition CtrlUtil.h:39
This file offers overloads of toIce() and fromIce() functions for STL container types.