sgitemnodeinstance.cpp 15.6 KB
Newer Older
1
2
/**************************************************************************
**
Thomas Murach's avatar
Thomas Murach committed
3
** This file is part of Qt Creator
4
**
Thomas Murach's avatar
Thomas Murach committed
5
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6
**
Thomas Murach's avatar
Thomas Murach committed
7
** Contact: Nokia Corporation (info@qt.nokia.com)
8
9
**
**
Thomas Murach's avatar
Thomas Murach committed
10
** GNU Lesser General Public License Usage
11
**
Thomas Murach's avatar
Thomas Murach committed
12
13
14
15
16
17
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this file.
** Please review the following information to ensure the GNU Lesser General
** Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18
**
Thomas Murach's avatar
Thomas Murach committed
19
20
21
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22
**
Thomas Murach's avatar
Thomas Murach committed
23
** Other Usage
24
**
Thomas Murach's avatar
Thomas Murach committed
25
26
27
28
29
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
** If you have questions regarding the use of this file, please contact
** Nokia at info@qt.nokia.com.
30
31
32
33
34
35
36
37
**
**************************************************************************/

#include "sgitemnodeinstance.h"

#include "qt5nodeinstanceserver.h"

#include <QDeclarativeExpression>
38
#include <QSGView>
39
40
41
42
#include <cmath>

#include <QHash>

43
44
#include <QtDebug>

45
46
47
namespace QmlDesigner {
namespace Internal {

48
49
bool SGItemNodeInstance::s_createEffectItem = false;

50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
SGItemNodeInstance::SGItemNodeInstance(QSGItem *item)
   : ObjectNodeInstance(item),
     m_hasHeight(false),
     m_hasWidth(false),
     m_isResizable(true),
     m_hasContent(true),
     m_isMovable(true),
     m_x(0.0),
     m_y(0.0),
     m_width(0.0),
     m_height(0.0)
{
}

SGItemNodeInstance::~SGItemNodeInstance()
{
    if (sgItem())
        designerSupport()->derefFromEffectItem(sgItem());
}

bool SGItemNodeInstance::hasContent() const
{
    return m_hasContent;
}

QList<ServerNodeInstance> SGItemNodeInstance::childItems() const
{
    QList<ServerNodeInstance> instanceList;

    foreach (QSGItem *childItem, sgItem()->childItems())
    {
        if (childItem && nodeInstanceServer()->hasInstanceForObject(childItem)) {
            instanceList.append(nodeInstanceServer()->instanceForObject(childItem));
        } else { //there might be an item in between the parent instance
                 //and the child instance.
                 //Popular example is flickable which has a viewport item between
                 //the flickable item and the flickable children
            instanceList.append(childItemsForChild(childItem)); //In such a case we go deeper inside the item and
                                                           //search for child items with instances.
        }
    }

    return instanceList;
}

QList<ServerNodeInstance> SGItemNodeInstance::childItemsForChild(QSGItem *childItem) const
{
    QList<ServerNodeInstance> instanceList;

    if (childItem) {
        foreach (QSGItem *childItem, childItem->childItems())
        {
            if (childItem && nodeInstanceServer()->hasInstanceForObject(childItem)) {
                instanceList.append(nodeInstanceServer()->instanceForObject(childItem));
            } else {
                instanceList.append(childItemsForChild(childItem));
            }
        }
    }
    return instanceList;
}

void SGItemNodeInstance::setHasContent(bool hasContent)
{
    m_hasContent = hasContent;
}


bool anyItemHasContent(QSGItem *graphicsItem)
{
    if (graphicsItem->flags().testFlag(QSGItem::ItemHasContents))
        return true;

    foreach (QSGItem *childItem, graphicsItem->childItems()) {
        if (anyItemHasContent(childItem))
            return true;
    }

    return false;
}

QPointF SGItemNodeInstance::position() const
{
    return sgItem()->pos();
}

QTransform SGItemNodeInstance::transform() const
{
    return DesignerSupport::parentTransform(sgItem());
}

QTransform SGItemNodeInstance::customTransform() const
{
    return QTransform();
}

QTransform SGItemNodeInstance::sceneTransform() const
{
    return DesignerSupport::canvasTransform(sgItem());
}

double SGItemNodeInstance::rotation() const
{
    return sgItem()->rotation();
}

double SGItemNodeInstance::scale() const
{
    return sgItem()->scale();
}

QPointF SGItemNodeInstance::transformOriginPoint() const
{
    return sgItem()->transformOriginPoint();
}

double SGItemNodeInstance::zValue() const
{
    return sgItem()->z();
}

double SGItemNodeInstance::opacity() const
{
    return sgItem()->opacity();
}

QObject *SGItemNodeInstance::parent() const
{
    if (!sgItem() || !sgItem()->parentItem())
        return 0;

    return sgItem()->parentItem();
}

bool SGItemNodeInstance::equalSGItem(QSGItem *item) const
{
    return item == sgItem();
}

189
190
191
192
193
194
void SGItemNodeInstance::updateDirtyNodeRecursive(QSGItem *parentItem) const
{
    foreach (QSGItem *childItem, parentItem->childItems()) {
        if (!nodeInstanceServer()->hasInstanceForObject(childItem))
            updateDirtyNodeRecursive(childItem);
    }
195
196

    DesignerSupport::updateDirtyNode(parentItem);
197
198
}

199
200
QImage SGItemNodeInstance::renderImage() const
{
201
202
    updateDirtyNodeRecursive(sgItem());

203
204
205
    QRectF boundingRect = boundingRectWithStepChilds(sgItem());

    QImage renderImage = designerSupport()->renderImageForItem(sgItem(), boundingRect, boundingRect.size().toSize());
206

207
208
209
    renderImage = renderImage.convertToFormat(QImage::Format_ARGB32_Premultiplied);

    return renderImage;
210
211
212
213
}

bool SGItemNodeInstance::isMovable() const
{
214
215
216
    if (isRootNodeInstance())
        return false;

217
218
219
220
221
222
223
224
225
226
    return m_isMovable && sgItem() && sgItem()->parentItem();
}

void SGItemNodeInstance::setMovable(bool movable)
{
    m_isMovable = movable;
}

SGItemNodeInstance::Pointer SGItemNodeInstance::create(QObject *object)
{
227
    QSGItem *sgItem = qobject_cast<QSGItem*>(object);
228
229
230
231
232
233
234
235
236
237

    Q_ASSERT(sgItem);

    Pointer instance(new SGItemNodeInstance(sgItem));

    instance->setHasContent(anyItemHasContent(sgItem));
    sgItem->setFlag(QSGItem::ItemHasContents, true);

    static_cast<QDeclarativeParserStatus*>(sgItem)->classBegin();

238
    instance->populateResetHashes();
239
240
241
242

    return instance;
}

243
void SGItemNodeInstance::initialize(const ObjectNodeInstance::Pointer &objectNodeInstance)
244
{
245
246
247
248
249
250
    if (instanceId() == 0) {
        DesignerSupport::setRootItem(nodeInstanceServer()->sgView(), sgItem());
    } else {
        sgItem()->setParentItem(qobject_cast<QSGItem*>(nodeInstanceServer()->sgView()->rootObject()));
    }

251
252
253
    if (s_createEffectItem || instanceId() == 0)
        designerSupport()->refFromEffectItem(sgItem());

254
    ObjectNodeInstance::initialize(objectNodeInstance);
255
    sgItem()->update();
256
257
258
259
260
261
262
263
264
}

bool SGItemNodeInstance::isSGItem() const
{
    return true;
}

QSizeF SGItemNodeInstance::size() const
{
265
266
    double width;

267
    if (DesignerSupport::isValidWidth(sgItem())) {
268
269
270
271
272
273
274
        width = sgItem()->width();
    } else {
        width = sgItem()->implicitWidth();
    }

    double height;

275
    if (DesignerSupport::isValidHeight(sgItem())) {
276
277
278
279
280
281
282
        height = sgItem()->height();
    } else {
        height = sgItem()->implicitHeight();
    }


    return QSizeF(width, height);
283
284
}

285
286
287
288
289
static inline bool isRectangleSane(const QRectF &rect)
{
    return rect.isValid() && (rect.width() < 10000) && (rect.height() < 10000);
}

290
QRectF SGItemNodeInstance::boundingRectWithStepChilds(QSGItem *parentItem) const
291
{
292
    QRectF boundingRect = parentItem->boundingRect();
293

294
    foreach (QSGItem *childItem, parentItem->childItems()) {
295
        if (!nodeInstanceServer()->hasInstanceForObject(childItem)) {
296
297
298
            QRectF transformedRect = childItem->mapRectToItem(parentItem, boundingRectWithStepChilds(childItem));
            if (isRectangleSane(transformedRect))
                boundingRect = boundingRect.united(transformedRect);
299
300
301
        }
    }

302
303
    return boundingRect;
}
304

305
306
307
308
309
310
QRectF SGItemNodeInstance::boundingRect() const
{
    if (sgItem()) {
        if (sgItem()->clip()) {
            return sgItem()->boundingRect();
        } else {
311
            return boundingRectWithStepChilds(sgItem());
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
        }
    }

    return QRectF();
}

void SGItemNodeInstance::setPropertyVariant(const QString &name, const QVariant &value)
{
    if (name == "state")
        return; // states are only set by us

    if (name == "height") {
        m_height = value.toDouble();
       if (value.isValid())
           m_hasHeight = true;
       else
           m_hasHeight = false;
    }

    if (name == "width") {
       m_width = value.toDouble();
       if (value.isValid())
           m_hasWidth = true;
       else
           m_hasWidth = false;
    }

    if (name == "x")
        m_x = value.toDouble();

    if (name == "y")
        m_y = value.toDouble();

    ObjectNodeInstance::setPropertyVariant(name, value);

    refresh();
}

void SGItemNodeInstance::setPropertyBinding(const QString &name, const QString &expression)
{
    if (name == "state")
        return; // states are only set by us

    ObjectNodeInstance::setPropertyBinding(name, expression);
}

QVariant SGItemNodeInstance::property(const QString &name) const
{
    return ObjectNodeInstance::property(name);
}

void SGItemNodeInstance::resetHorizontal()
 {
    setPropertyVariant("x", m_x);
    if (m_width > 0.0) {
        setPropertyVariant("width", m_width);
    } else {
        setPropertyVariant("width", sgItem()->implicitWidth());
    }
}

void SGItemNodeInstance::resetVertical()
 {
    setPropertyVariant("y", m_y);
    if (m_height > 0.0) {
        setPropertyVariant("height", m_height);
    } else {
        setPropertyVariant("height", sgItem()->implicitWidth());
    }
}

static void repositioning(QSGItem *item)
{
    if (!item)
        return;

//    QDeclarativeBasePositioner *positioner = qobject_cast<QDeclarativeBasePositioner*>(item);
//    if (positioner)
//        positioner->rePositioning();

    if (item->parentItem())
        repositioning(item->parentItem());
}

void SGItemNodeInstance::refresh()
{
    repositioning(sgItem());
}

void SGItemNodeInstance::doComponentComplete()
{
    if (sgItem()) {
        if (DesignerSupport::isComponentComplete(sgItem()))
            return;
        static_cast<QDeclarativeParserStatus*>(sgItem())->componentComplete();
    }

    sgItem()->update();
}

bool SGItemNodeInstance::isResizable() const
{
414
415
416
    if (isRootNodeInstance())
        return false;

417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
    return m_isResizable && sgItem() && sgItem()->parentItem();
}

void SGItemNodeInstance::setResizable(bool resizeable)
{
    m_isResizable = resizeable;
}

int SGItemNodeInstance::penWidth() const
{
    return DesignerSupport::borderWidth(sgItem());
}

void SGItemNodeInstance::resetProperty(const QString &name)
{
    if (name == "height") {
        m_hasHeight = false;
        m_height = 0.0;
    }

    if (name == "width") {
        m_hasWidth = false;
        m_width = 0.0;
    }

    if (name == "x")
        m_x = 0.0;

    if (name == "y")
        m_y = 0.0;

    DesignerSupport::resetAnchor(sgItem(), name);

    if (name == "anchors.fill") {
        resetHorizontal();
        resetVertical();
    } else if (name == "anchors.centerIn") {
        resetHorizontal();
        resetVertical();
    } else if (name == "anchors.top") {
        resetVertical();
    } else if (name == "anchors.left") {
        resetHorizontal();
    } else if (name == "anchors.right") {
        resetHorizontal();
    } else if (name == "anchors.bottom") {
        resetVertical();
    } else if (name == "anchors.horizontalCenter") {
        resetHorizontal();
    } else if (name == "anchors.verticalCenter") {
        resetVertical();
    } else if (name == "anchors.baseline") {
        resetVertical();
    }

    ObjectNodeInstance::resetProperty(name);
}

void SGItemNodeInstance::reparent(const ObjectNodeInstance::Pointer &oldParentInstance, const QString &oldParentProperty, const ObjectNodeInstance::Pointer &newParentInstance, const QString &newParentProperty)
{
    if (oldParentInstance && oldParentInstance->isPositioner()) {
        setInPositioner(false);
        setMovable(true);
    }

    ObjectNodeInstance::reparent(oldParentInstance, oldParentProperty, newParentInstance, newParentProperty);

    if (newParentInstance && newParentInstance->isPositioner()) {
        setInPositioner(true);
        setMovable(false);
    }

    if (oldParentInstance && oldParentInstance->isPositioner() && !(newParentInstance && newParentInstance->isPositioner())) {
        if (!hasBindingForProperty("x"))
            setPropertyVariant("x", m_x);

        if (!hasBindingForProperty("y"))
            setPropertyVariant("y", m_y);
    }

    refresh();
498
    DesignerSupport::updateDirtyNode(sgItem());
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
}

static bool isValidAnchorName(const QString &name)
{
    static QStringList anchorNameList(QStringList() << "anchors.top"
                                                    << "anchors.left"
                                                    << "anchors.right"
                                                    << "anchors.bottom"
                                                    << "anchors.verticalCenter"
                                                    << "anchors.horizontalCenter"
                                                    << "anchors.fill"
                                                    << "anchors.centerIn"
                                                    << "anchors.baseline");

    return anchorNameList.contains(name);
}

bool SGItemNodeInstance::hasAnchor(const QString &name) const
{
    return DesignerSupport::hasAnchor(sgItem(), name);
}

QPair<QString, ServerNodeInstance> SGItemNodeInstance::anchor(const QString &name) const
{
    if (!isValidAnchorName(name) || !DesignerSupport::hasAnchor(sgItem(), name))
        return ObjectNodeInstance::anchor(name);

    QPair<QString, QObject*> nameObjectPair = DesignerSupport::anchorLineTarget(sgItem(), name, context());

    QObject *targetObject = nameObjectPair.second;
    QString targetName = nameObjectPair.first;

    if (targetObject && nodeInstanceServer()->hasInstanceForObject(targetObject)) {
        return qMakePair(targetName, nodeInstanceServer()->instanceForObject(targetObject));
    } else {
        return ObjectNodeInstance::anchor(name);
    }
}

QList<ServerNodeInstance> SGItemNodeInstance::stateInstances() const
{
    QList<ServerNodeInstance> instanceList;
    QList<QObject*> stateList = DesignerSupport::statesForItem(sgItem());
    foreach (QObject *state, stateList)
    {
        if (state && nodeInstanceServer()->hasInstanceForObject(state))
            instanceList.append(nodeInstanceServer()->instanceForObject(state));
    }

    return instanceList;
}

bool SGItemNodeInstance::isAnchoredBySibling() const
{
    if (sgItem()->parentItem()) {
        foreach (QSGItem *siblingItem, sgItem()->parentItem()->childItems()) { // search in siblings for a anchor to this item
            if (siblingItem) {
                if (DesignerSupport::isAnchoredTo(siblingItem, sgItem()))
                    return true;
            }
        }
    }

    return false;
}

bool SGItemNodeInstance::isAnchoredByChildren() const
{
    if (DesignerSupport::areChildrenAnchoredTo(sgItem(), sgItem())) // search in children for a anchor to this item
        return true;

    return false;
}

QSGItem *SGItemNodeInstance::sgItem() const
{
    if (object() == 0)
        return 0;

    Q_ASSERT(qobject_cast<QSGItem*>(object()));
    return static_cast<QSGItem*>(object());
}

DesignerSupport *SGItemNodeInstance::designerSupport() const
{
    return qt5NodeInstanceServer()->designerSupport();
}

Qt5NodeInstanceServer *SGItemNodeInstance::qt5NodeInstanceServer() const
{
    return qobject_cast<Qt5NodeInstanceServer*>(nodeInstanceServer());
}

592
593
594
595
596
void SGItemNodeInstance::createEffectItem(bool createEffectItem)
{
    s_createEffectItem = createEffectItem;
}

597
598
599
} // namespace Internal
} // namespace QmlDesigner