graphicalnodeinstance.cpp 14.6 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "graphicalnodeinstance.h"

#include "qt5nodeinstanceserver.h"

#include <QQmlExpression>

#include <cmath>

#include <QQuickView>

#include <private/qquickitem_p.h>
#include <private/qquicktextinput_p.h>
#include <private/qquicktextedit_p.h>

15
16
#include <designersupport.h>

17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
namespace QmlDesigner {
namespace Internal {

bool GraphicalNodeInstance::s_createEffectItem = false;

GraphicalNodeInstance::GraphicalNodeInstance(QObject *object)
    : ObjectNodeInstance(object),
      m_hasHeight(false),
      m_hasWidth(false),
      m_hasContent(true),
      m_x(0.0),
      m_y(0.0),
      m_width(0.0),
      m_height(0.0)
{
}

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

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

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


bool GraphicalNodeInstance::isGraphical() const
{
    return true;
}

bool GraphicalNodeInstance::anyItemHasContent(QQuickItem *quickItem)
{
    if (quickItem->flags().testFlag(QQuickItem::ItemHasContents))
        return true;

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

    return false;
}

double GraphicalNodeInstance::x() const
{
    return m_x;
}

double GraphicalNodeInstance::y() const
{
    return m_y;
}

QQuickItem *GraphicalNodeInstance::quickItem() const
{
    return 0;
}

bool GraphicalNodeInstance::childItemsHaveContent(QQuickItem *quickItem)
{
    foreach (QQuickItem *childItem, quickItem->childItems()) {
        if (anyItemHasContent(childItem))
            return true;
    }

    return false;
}

bool GraphicalNodeInstance::hasContent() const
{
    if (m_hasContent)
        return true;

    return childItemsHaveContent(quickItem());
}

void GraphicalNodeInstance::createEffectItem(bool createEffectItem)
{
    s_createEffectItem = createEffectItem;
}

106
void GraphicalNodeInstance::updateAllDirtyNodesRecursive()
107
{
108
    updateAllDirtyNodesRecursive(quickItem());
109
110
111
112
113
114
115
116
}

GraphicalNodeInstance::~GraphicalNodeInstance()
{
    if (quickItem())
        designerSupport()->derefFromEffectItem(quickItem());
}

117
void GraphicalNodeInstance::updateDirtyNodesRecursive(QQuickItem *parentItem) const
118
119
120
{
    foreach (QQuickItem *childItem, parentItem->childItems()) {
        if (!nodeInstanceServer()->hasInstanceForObject(childItem))
121
            updateDirtyNodesRecursive(childItem);
122
123
124
125
126
    }

    DesignerSupport::updateDirtyNode(parentItem);
}

127
void GraphicalNodeInstance::updateAllDirtyNodesRecursive(QQuickItem *parentItem) const
128
129
{
    foreach (QQuickItem *childItem, parentItem->childItems())
130
            updateAllDirtyNodesRecursive(childItem);
131
132
133
134
135
136

    DesignerSupport::updateDirtyNode(parentItem);
}

QImage GraphicalNodeInstance::renderImage() const
{
137
    updateDirtyNodesRecursive(quickItem());
138

139
    QRectF renderBoundingRect = boundingRect();
140

141
    QImage renderImage = designerSupport()->renderImageForItem(quickItem(), renderBoundingRect, renderBoundingRect.size().toSize());
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

    return renderImage;
}

QImage GraphicalNodeInstance::renderPreviewImage(const QSize &previewImageSize) const
{
    QRectF previewItemBoundingRect = boundingRect();

    if (previewItemBoundingRect.isValid() && quickItem())
        return designerSupport()->renderImageForItem(quickItem(), previewItemBoundingRect, previewImageSize);

    return QImage();
}

void GraphicalNodeInstance::initialize(const ObjectNodeInstance::Pointer &objectNodeInstance)
{
    if (instanceId() == 0) {
        DesignerSupport::setRootItem(nodeInstanceServer()->quickView(), quickItem());
    } else {
        quickItem()->setParentItem(qobject_cast<QQuickItem*>(nodeInstanceServer()->quickView()->rootObject()));
    }

    if (s_createEffectItem || instanceId() == 0)
        designerSupport()->refFromEffectItem(quickItem());

    ObjectNodeInstance::initialize(objectNodeInstance);
    quickItem()->update();
}

QPointF GraphicalNodeInstance::position() const
{
    return quickItem()->position();
}

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

181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
static QTransform contentTransformForItem(QQuickItem *item, NodeInstanceServer *nodeInstanceServer)
{
    QTransform contentTransform;
    if (item->parentItem() && !nodeInstanceServer->hasInstanceForObject(item->parentItem())) {
        contentTransform = DesignerSupport::parentTransform(item->parentItem());
        return contentTransformForItem(item->parentItem(), nodeInstanceServer) * contentTransform;
    }

    return contentTransform;
}

QTransform GraphicalNodeInstance::contentTransform() const
{
    return contentTransformForItem(quickItem(), nodeInstanceServer());
}

197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
QTransform GraphicalNodeInstance::sceneTransform() const
{
    return DesignerSupport::windowTransform(quickItem());
}

double GraphicalNodeInstance::rotation() const
{
    return quickItem()->rotation();
}

double GraphicalNodeInstance::scale() const
{
    return quickItem()->scale();
}

QPointF GraphicalNodeInstance::transformOriginPoint() const
{
    return quickItem()->transformOriginPoint();
}

double GraphicalNodeInstance::zValue() const
{
    return quickItem()->z();
}

double GraphicalNodeInstance::opacity() const
{
    return quickItem()->opacity();
}

QSizeF GraphicalNodeInstance::size() const
{
    double width;

    if (DesignerSupport::isValidWidth(quickItem())) {
        width = quickItem()->width();
    } else {
        width = quickItem()->implicitWidth();
    }

    double height;

    if (DesignerSupport::isValidHeight(quickItem())) {
        height = quickItem()->height();
    } else {
        height = quickItem()->implicitHeight();
    }


    return QSizeF(width, height);
}

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

254
QRectF GraphicalNodeInstance::boundingRectWithStepChilds(QQuickItem *item) const
255
{
256
    QRectF boundingRect = item->boundingRect();
257

258
    foreach (QQuickItem *childItem, item->childItems()) {
259
        if (!nodeInstanceServer()->hasInstanceForObject(childItem)) {
260
            QRectF transformedRect = childItem->mapRectToItem(item, boundingRectWithStepChilds(childItem));
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
            if (isRectangleSane(transformedRect))
                boundingRect = boundingRect.united(transformedRect);
        }
    }

    return boundingRect;
}

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

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

int GraphicalNodeInstance::penWidth() const
{
    return DesignerSupport::borderWidth(quickItem());
}


295
QList<ServerNodeInstance> GraphicalNodeInstance::childItemsForChild(QQuickItem *item) const
296
297
298
{
    QList<ServerNodeInstance> instanceList;

299
300
    if (item) {
        foreach (QQuickItem *childItem, item->childItems())
301
302
303
304
305
306
307
308
309
310
311
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
        {
            if (childItem && nodeInstanceServer()->hasInstanceForObject(childItem)) {
                instanceList.append(nodeInstanceServer()->instanceForObject(childItem));
            } else {
                instanceList.append(childItemsForChild(childItem));
            }
        }
    }
    return instanceList;
}

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

    foreach (QQuickItem *childItem, quickItem()->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;
}


void GraphicalNodeInstance::setPropertyVariant(const PropertyName &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);

    quickItem()->update();

    refresh();

366
367
    if (isInLayoutable())
        parentInstance()->refreshLayoutable();
368
369
370
371
372
373
374
}

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

375
376
377
    if (name.startsWith("anchors.") && isRootNodeInstance())
        return;

378
379
380
381
382
383
    ObjectNodeInstance::setPropertyBinding(name, expression);

    quickItem()->update();

    refresh();

384
385
    if (isInLayoutable())
        parentInstance()->refreshLayoutable();
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
414
415
416
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
}

QVariant GraphicalNodeInstance::property(const PropertyName &name) const
{
    if (name == "visible")
        return quickItem()->isVisible();

    return ObjectNodeInstance::property(name);
}

void GraphicalNodeInstance::resetProperty(const PropertyName &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(quickItem(), 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);

    quickItem()->update();

442
443
    if (isInLayoutable())
        parentInstance()->refreshLayoutable();
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
}


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

    return anchorNameList.contains(name);
}

bool GraphicalNodeInstance::hasAnchor(const PropertyName &name) const
{
    return DesignerSupport::hasAnchor(quickItem(), name);
}

QPair<PropertyName, ServerNodeInstance> GraphicalNodeInstance::anchor(const PropertyName &name) const
{
    if (!isValidAnchorName(name) || !DesignerSupport::hasAnchor(quickItem(), name))
        return ObjectNodeInstance::anchor(name);

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

    QObject *targetObject = nameObjectPair.second;
    PropertyName targetName = nameObjectPair.first.toUtf8();

477
478
479
480
481
    while (targetObject) {
        if (nodeInstanceServer()->hasInstanceForObject(targetObject))
            return qMakePair(targetName, nodeInstanceServer()->instanceForObject(targetObject));
        else
            targetObject = parentObject(targetObject);
482
    }
483
484
485

    return ObjectNodeInstance::anchor(name);

486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
}

static void disableTextCursor(QQuickItem *item)
{
    foreach (QQuickItem *childItem, item->childItems())
        disableTextCursor(childItem);

    QQuickTextInput *textInput = qobject_cast<QQuickTextInput*>(item);
    if (textInput)
        textInput->setCursorVisible(false);

    QQuickTextEdit *textEdit = qobject_cast<QQuickTextEdit*>(item);
    if (textEdit)
        textEdit->setCursorVisible(false);
}

void GraphicalNodeInstance::doComponentComplete()
{
504
    doComponentCompleteRecursive(quickItem(), nodeInstanceServer());
505
506
507

    disableTextCursor(quickItem());

508
509
510
511
#if (QT_VERSION >= QT_VERSION_CHECK(5, 1, 0))
    DesignerSupport::emitComponentCompleteSignalForAttachedProperty(quickItem());
#endif

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
    quickItem()->update();
}

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

    return false;
}

QRectF GraphicalNodeInstance::boundingRect() const
{
    if (quickItem()) {
        if (quickItem()->clip()) {
            return quickItem()->boundingRect();
        } else {
            return boundingRectWithStepChilds(quickItem());
        }
    }

    return QRectF();
}

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

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

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

void GraphicalNodeInstance::refresh()
{
    repositioning(quickItem());
}

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

    return instanceList;
}

}
} // namespace QmlDesigner