OSDN Git Service

It's 2011 now.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / qmldesigner / designercore / instances / nodeinstance.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights.  These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include "nodeinstance.h"
35
36 #include <QPainter>
37 #include <modelnode.h>
38 #include "commondefines.h"
39
40 #include <QtDebug>
41
42 namespace QmlDesigner {
43
44 class ProxyNodeInstanceData
45 {
46 public:
47     ProxyNodeInstanceData()
48         : parentInstanceId(-1),
49           penWidth(1),
50           isAnchoredBySibling(false),
51           isAnchoredByChildren(false),
52           hasContent(false),
53           isMovable(false),
54           isResizable(false),
55           isInPositioner(false)
56     {}
57
58     qint32 parentInstanceId;
59     ModelNode modelNode;
60     QRectF boundingRect;
61     QPointF position;
62     QSizeF size;
63     QTransform transform;
64     QTransform sceneTransform;
65     int penWidth;
66     bool isAnchoredBySibling;
67     bool isAnchoredByChildren;
68     bool hasContent;
69     bool isMovable;
70     bool isResizable;
71     bool isInPositioner;
72
73
74     QHash<QString, QVariant> propertyValues;
75     QHash<QString, bool> hasBindingForProperty;
76     QHash<QString, bool> hasAnchors;
77     QHash<QString, QString> instanceTypes;
78
79     QImage renderImage;
80     QHash<QString, QPair<QString, qint32> > anchors;
81 };
82
83 NodeInstance::NodeInstance()
84 {
85 }
86
87 NodeInstance::NodeInstance(ProxyNodeInstanceData *dPointer)
88     : d(dPointer)
89 {
90 }
91
92 NodeInstance NodeInstance::create(const ModelNode &node)
93 {
94     ProxyNodeInstanceData *d = new ProxyNodeInstanceData;
95
96     d->modelNode = node;
97
98     return NodeInstance(d);
99 }
100
101 NodeInstance::~NodeInstance()
102 {
103 }
104
105 NodeInstance::NodeInstance(const NodeInstance &other)
106   : d(other.d)
107 {
108 }
109
110 NodeInstance &NodeInstance::operator=(const NodeInstance &other)
111 {
112     d = other.d;
113     return *this;
114 }
115
116 ModelNode NodeInstance::modelNode() const
117 {
118     if (d) {
119         return  d->modelNode;
120     } else {
121         return ModelNode();
122     }
123 }
124
125 qint32 NodeInstance::instanceId() const
126 {
127     if (d) {
128         return d->modelNode.internalId();
129     } else {
130         return -1;
131     }
132 }
133
134 bool NodeInstance::isValid() const
135 {
136     return instanceId() >= 0 && modelNode().isValid();
137 }
138
139 void NodeInstance::makeInvalid()
140 {
141     if (d)
142         d->modelNode = ModelNode();
143 }
144
145 QRectF NodeInstance::boundingRect() const
146 {
147     if (isValid()) {
148         return  d->boundingRect;
149     } else {
150         return QRectF();
151     }
152 }
153
154 bool NodeInstance::hasContent() const
155 {
156     if (isValid()) {
157         return d->hasContent;
158     } else {
159         return false;
160     }
161 }
162
163 bool NodeInstance::isAnchoredBySibling() const
164 {
165     if (isValid()) {
166         return d->isAnchoredBySibling;
167     } else {
168         return false;
169     }
170 }
171
172 bool NodeInstance::isAnchoredByChildren() const
173 {
174     if (isValid()) {
175         return d->isAnchoredByChildren;
176     } else {
177         return false;
178     }
179 }
180
181 bool NodeInstance::isMovable() const
182 {
183     if (isValid()) {
184         return d->isMovable;
185     } else {
186         return false;
187     }
188 }
189
190 bool NodeInstance::isResizable() const
191 {
192     if (isValid()) {
193         return d->isResizable;
194     } else {
195         return false;
196     }
197 }
198
199 QTransform NodeInstance::transform() const
200 {
201     if (isValid()) {
202         return d->transform;
203     } else {
204         return QTransform();
205     }
206 }
207 QTransform NodeInstance::sceneTransform() const
208 {
209     if (isValid()) {
210         return d->sceneTransform;
211     } else {
212         return QTransform();
213     }
214 }
215 bool NodeInstance::isInPositioner() const
216 {
217     if (isValid()) {
218         return d->isInPositioner;
219     } else {
220         return false;
221     }
222 }
223
224 QPointF NodeInstance::position() const
225 {
226     if (isValid()) {
227         return d->position;
228     } else {
229         return QPointF();
230     }
231 }
232
233 QSizeF NodeInstance::size() const
234 {
235     if (isValid()) {
236         return d->size;
237     } else {
238         return QSizeF();
239     }
240 }
241
242 int NodeInstance::penWidth() const
243 {
244     if (isValid()) {
245         return d->penWidth;
246     } else {
247         return 1;
248     }
249 }
250
251 void NodeInstance::paint(QPainter *painter)
252 {
253     if (isValid() && !d->renderImage.isNull())
254         painter->drawImage(boundingRect().topLeft(), d->renderImage);
255 }
256
257 QVariant NodeInstance::property(const QString &name) const
258 {
259     if (isValid())
260         return d->propertyValues.value(name);
261
262     return QVariant();
263 }
264
265 bool NodeInstance::hasBindingForProperty(const QString &name) const
266 {
267     if (isValid())
268         return d->hasBindingForProperty.value(name, false);
269
270     return false;
271 }
272
273 QString NodeInstance::instanceType(const QString &name) const
274 {
275     if (isValid())
276         return d->instanceTypes.value(name);
277
278     return QString();
279 }
280
281 qint32 NodeInstance::parentId() const
282 {
283     if (isValid()) {
284         return d->parentInstanceId;
285     } else {
286         return false;
287     }
288 }
289
290 bool NodeInstance::hasAnchor(const QString &name) const
291 {
292     if (isValid())
293         return d->hasAnchors.value(name, false);
294
295     return false;
296 }
297
298 QPair<QString, qint32> NodeInstance::anchor(const QString &name) const
299 {
300     if (isValid())
301         return d->anchors.value(name, QPair<QString, qint32>(QString(), qint32(-1)));
302
303     return QPair<QString, qint32>(QString(), -1);
304 }
305
306 void NodeInstance::setProperty(const QString &name, const QVariant &value)
307 {
308     d->propertyValues.insert(name, value);
309 }
310
311 void NodeInstance::setRenderImage(const QImage &image)
312 {
313     d->renderImage = image;
314 }
315
316 void NodeInstance::setParentId(qint32 instanceId)
317 {
318     d->parentInstanceId = instanceId;
319 }
320
321 void NodeInstance::setInformation(InformationName name, const QVariant &information, const QVariant &secondInformation, const QVariant &thirdInformation)
322 {
323     switch (name) {
324     case Size: d->size = information.toSizeF(); break;
325     case BoundingRect: d->boundingRect = information.toRectF(); break;
326     case Transform: d->transform = information.value<QTransform>(); break;
327     case PenWidth: d->penWidth = information.toInt(); break;
328     case Position: d->position = information.toPointF(); break;
329     case IsInPositioner: d->isInPositioner = information.toBool(); break;
330     case SceneTransform: d->sceneTransform = information.value<QTransform>(); break;
331     case IsResizable: d->isResizable = information.toBool(); break;
332     case IsMovable: d->isMovable = information.toBool(); break;
333     case IsAnchoredByChildren: d->isAnchoredByChildren  = information.toBool(); break;
334     case IsAnchoredBySibling: d->isAnchoredBySibling = information.toBool(); break;
335     case HasContent: d->hasContent = information.toBool(); break;
336     case HasAnchor: d->hasAnchors.insert(information.toString(), secondInformation.toBool());break;
337     case Anchor: d->anchors.insert(information.toString(), qMakePair(secondInformation.toString(), thirdInformation.value<qint32>())); break;
338     case InstanceTypeForProperty: d->instanceTypes.insert(information.toString(), secondInformation.toString()); break;
339     case HasBindingForProperty: d->hasBindingForProperty.insert(information.toString(), secondInformation.toBool()); break;
340     case NoName:
341     default: break;
342     }
343 }
344
345 }