qmlitemnode.cpp 19.2 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
2
**
3
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
7
**
hjk's avatar
hjk committed
8
9
10
11
12
13
14
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
15
16
**
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17
18
19
20
21
22
23
24
25
** Alternatively, 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.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
con's avatar
con committed
26
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
29
30
31
32
33

#include "qmlitemnode.h"
#include <metainfo.h>
#include "qmlchangeset.h"
#include "nodelistproperty.h"
34
#include "variantproperty.h"
35
36
#include "qmlanchors.h"
#include "invalidmodelnodeexception.h"
37
38
39
40
41
42
43
44
45
46
47
48
#include "itemlibraryinfo.h"

#include "plaintexteditmodifier.h"
#include "rewriterview.h"
#include "modelmerger.h"
#include "rewritingexception.h"

#include <QMessageBox>
#include <QUrl>
#include <QPlainTextEdit>
#include <QFileInfo>
#include <QDir>
49
50
51

namespace QmlDesigner {

52
bool QmlItemNode::isItemOrWindow(const ModelNode &modelNode)
53
54
55
56
57
58
59
60
61
62
{
    if (modelNode.metaInfo().isSubclassOf("QtQuick.Item", -1, -1))
        return true;

    if (modelNode.metaInfo().isSubclassOf("QtQuick.Window.Window", -1, -1) && modelNode.isRootNode())
        return true;

    return false;
}

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
static QmlItemNode createQmlItemNodeFromSource(AbstractView *view, const QString &source, const QPointF &position)
{
    QScopedPointer<Model> inputModel(Model::create("QtQuick.Item", 1, 0, view->model()));
    inputModel->setFileUrl(view->model()->fileUrl());
    QPlainTextEdit textEdit;

    textEdit.setPlainText(source);
    NotIndentingTextEditModifier modifier(&textEdit);

    QScopedPointer<RewriterView> rewriterView(new RewriterView(RewriterView::Amend, 0));
    rewriterView->setCheckSemanticErrors(false);
    rewriterView->setTextModifier(&modifier);
    inputModel->setRewriterView(rewriterView.data());

    if (rewriterView->errors().isEmpty() && rewriterView->rootModelNode().isValid()) {
        ModelNode rootModelNode = rewriterView->rootModelNode();
        inputModel->detachView(rewriterView.data());

        rootModelNode.variantProperty("x").setValue(qRound(position.x()));
        rootModelNode.variantProperty("y").setValue(qRound(position.y()));

        ModelMerger merger(view);
        return merger.insertModel(rootModelNode);
    }

    return QmlItemNode();
}


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
QmlItemNode QmlItemNode::createQmlItemNode(AbstractView *view, const ItemLibraryEntry &itemLibraryEntry, const QPointF &position, QmlItemNode parentQmlItemNode)
{
    if (!parentQmlItemNode.isValid())
        parentQmlItemNode = QmlItemNode(view->rootModelNode());

    Q_ASSERT(parentQmlItemNode.isValid());

    QmlItemNode newQmlItemNode;

    try {
        RewriterTransaction transaction = view->beginRewriterTransaction();

        NodeMetaInfo metaInfo = view->model()->metaInfo(itemLibraryEntry.typeName());

        int minorVersion = metaInfo.minorVersion();
        int majorVersion = metaInfo.majorVersion();

        if (itemLibraryEntry.typeName().contains('.')) {

            const QString newImportUrl = itemLibraryEntry.requiredImport();

            if (!itemLibraryEntry.requiredImport().isEmpty()) {
                const QString newImportVersion = QString("%1.%2").arg(QString::number(itemLibraryEntry.majorVersion()), QString::number(itemLibraryEntry.minorVersion()));

                Import newImport = Import::createLibraryImport(newImportUrl, newImportVersion);
                if (itemLibraryEntry.majorVersion() == -1 && itemLibraryEntry.minorVersion() == -1)
                    newImport = Import::createFileImport(newImportUrl, QString());
                else
                    newImport = Import::createLibraryImport(newImportUrl, newImportVersion);

                foreach (const Import &import, view->model()->imports()) {
                    if (import.isLibraryImport()
                            && import.url() == newImport.url()
                            && import.version() == newImport.version()) {
                        // reuse this import
                        newImport = import;
                        break;
                    }
                }

                if (!view->model()->hasImport(newImport, true, true))
                    view->model()->changeImports(QList<Import>() << newImport, QList<Import>());
            }
        }

Thomas Hartmann's avatar
Thomas Hartmann committed
137
        if (itemLibraryEntry.qmlSource().isEmpty()) {
138
139
140
141
            QList<QPair<PropertyName, QVariant> > propertyPairList;
            propertyPairList.append(qMakePair(PropertyName("x"), QVariant(qRound(position.x()))));
            propertyPairList.append(qMakePair(PropertyName("y"), QVariant(qRound(position.y()))));

142
143
144
145
146
            foreach (const PropertyContainer &property, itemLibraryEntry.properties())
                propertyPairList.append(qMakePair(property.name(), property.value()));

            newQmlItemNode = QmlItemNode(view->createModelNode(itemLibraryEntry.typeName(), majorVersion, minorVersion, propertyPairList));
        } else {
Thomas Hartmann's avatar
Thomas Hartmann committed
147
            newQmlItemNode = createQmlItemNodeFromSource(view, itemLibraryEntry.qmlSource(), position);
148
149
        }

150
        if (parentQmlItemNode.hasDefaultPropertyName())
151
152
153
154
155
            parentQmlItemNode.nodeAbstractProperty(parentQmlItemNode.defaultPropertyName()).reparentHere(newQmlItemNode);

        if (!newQmlItemNode.isValid())
            return newQmlItemNode;

Thomas Hartmann's avatar
Thomas Hartmann committed
156
        newQmlItemNode.setId(view->generateNewId(itemLibraryEntry.name()));
157

158
        if (!view->currentState().isBaseState()) {
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
            newQmlItemNode.modelNode().variantProperty("opacity").setValue(0);
            newQmlItemNode.setVariantProperty("opacity", 1);
        }

        Q_ASSERT(newQmlItemNode.isValid());
    }
    catch (RewritingException &e) {
        QMessageBox::warning(0, "Error", e.description());
    }

    Q_ASSERT(newQmlItemNode.isValid());

    return newQmlItemNode;
}

174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
static void checkImageImport(AbstractView *view)
{
    const QString newImportUrl = QLatin1String("QtQuick");
    const QString newImportVersion = QLatin1String("1.1");
    Import newImport = Import::createLibraryImport(newImportUrl, newImportVersion);

    foreach (const Import &import, view->model()->imports()) {
        if (import.isLibraryImport()
            && import.url() == newImport.url()) {
            // reuse this import
            newImport = import;
            break;
        }
    }

    if (!view->model()->hasImport(newImport, true, true))
        view->model()->changeImports(QList<Import>() << newImport, QList<Import>());
}

193
194
QmlItemNode QmlItemNode::createQmlItemNodeFromImage(AbstractView *view, const QString &imageName, const QPointF &position, QmlItemNode parentQmlItemNode)
{
195
196
    QmlItemNode newQmlItemNode;
    if (!parentQmlItemNode.isValid())
197
198
        parentQmlItemNode = QmlItemNode(view->rootModelNode());

199
200
    if (parentQmlItemNode.isValid()) {
        RewriterTransaction transaction = view->beginRewriterTransaction();
201

202
        checkImageImport(view);
203

204
205
206
207
208
        if (view->model()->hasNodeMetaInfo("QtQuick.Image")) {
            NodeMetaInfo metaInfo = view->model()->metaInfo("QtQuick.Image");
            QList<QPair<PropertyName, QVariant> > propertyPairList;
            propertyPairList.append(qMakePair(PropertyName("x"), QVariant(qRound(position.x()))));
            propertyPairList.append(qMakePair(PropertyName("y"), QVariant(qRound(position.y()))));
209

210
            QString relativeImageName = imageName;
211

212
213
214
215
216
217
            //use relative path
            if (QFileInfo(view->model()->fileUrl().toLocalFile()).exists()) {
                QDir fileDir(QFileInfo(view->model()->fileUrl().toLocalFile()).absolutePath());
                relativeImageName = fileDir.relativeFilePath(imageName);
                propertyPairList.append(qMakePair(PropertyName("source"), QVariant(relativeImageName)));
            }
218

219
220
            newQmlItemNode = QmlItemNode(view->createModelNode("QtQuick.Image", metaInfo.majorVersion(), metaInfo.minorVersion(), propertyPairList));
            parentQmlItemNode.defaultNodeAbstractProperty().reparentHere(newQmlItemNode);
221

222
            newQmlItemNode.setId(view->generateNewId("image"));
223

224
            if (!view->currentState().isBaseState()) {
225
226
227
                newQmlItemNode.modelNode().variantProperty("opacity").setValue(0);
                newQmlItemNode.setVariantProperty("opacity", 1);
            }
228

229
            Q_ASSERT(newQmlItemNode.isValid());
230
231
232
233
234
235
236
237
        }
        Q_ASSERT(newQmlItemNode.isValid());
    }


    return newQmlItemNode;
}

238
239
bool QmlItemNode::isValid() const
{
240
241
242
243
244
245
    return isValidQmlItemNode(modelNode());
}

bool QmlItemNode::isValidQmlItemNode(const ModelNode &modelNode)
{
    return isValidQmlObjectNode(modelNode) && modelNode.metaInfo().isValid() && isItemOrWindow(modelNode);
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
}

bool QmlItemNode::isRootNode() const
{
    return modelNode().isValid() && modelNode().isRootNode();
}

QStringList QmlModelStateGroup::names() const
{
    QStringList returnList;

    if (!modelNode().isValid())
        throw new InvalidModelNodeException(__LINE__, __FUNCTION__, __FILE__);

    if (modelNode().property("states").isNodeListProperty()) {
        foreach (const ModelNode &node, modelNode().nodeListProperty("states").toModelNodeList()) {
262
            if (QmlModelState::isValidQmlModelState(node))
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
                returnList.append(QmlModelState(node).name());
        }
    }
    return returnList;
}

/**
  \brief Returns list of states (without 'base state').
  The list contains all states defined by this item.
  */
QmlModelStateGroup QmlItemNode::states() const
{
    if (isValid())
        return QmlModelStateGroup(modelNode());
    else
        return QmlModelStateGroup();
}

QList<QmlItemNode> QmlItemNode::children() const
{
283
    QList<ModelNode> childrenList;
284
285
286

    if (isValid()) {

287
288
        if (modelNode().hasNodeListProperty("children"))
                childrenList.append(modelNode().nodeListProperty("children").toModelNodeList());
289

290
291
292
293
294
        if (modelNode().hasNodeListProperty("data")) {
            foreach (const ModelNode &node, modelNode().nodeListProperty("data").toModelNodeList()) {
                if (QmlItemNode::isValidQmlItemNode(node))
                    childrenList.append(node);
            }
295
296
        }
    }
297
298

    return toQmlItemNodeList(childrenList);
299
300
301
302
}

QList<QmlObjectNode> QmlItemNode::resources() const
{
303
    QList<ModelNode> resourcesList;
304
305
306

    if (isValid()) {

307
308
        if (modelNode().hasNodeListProperty("resources"))
                resourcesList.append(modelNode().nodeListProperty("resources").toModelNodeList());
309

310
311
312
313
314
        if (modelNode().hasNodeListProperty("data")) {
            foreach (const ModelNode &node, modelNode().nodeListProperty("data").toModelNodeList()) {
                if (!QmlItemNode::isValidQmlItemNode(node))
                    resourcesList.append(node);
            }
315
316
        }
    }
317
318

    return toQmlObjectNodeList(resourcesList);
319
320
321
322
}

QList<QmlObjectNode> QmlItemNode::allDirectSubNodes() const
{
323
    return toQmlObjectNodeList(modelNode().allDirectSubModelNodes());
324
325
326
327
328
329
330
331
332
}

QmlAnchors QmlItemNode::anchors() const
{
    return QmlAnchors(*this);
}

bool QmlItemNode::hasChildren() const
{
333
334
335
    if (modelNode().hasNodeListProperty("children"))
        return true;

336
337
338
339
340
    return !children().isEmpty();
}

bool QmlItemNode::hasResources() const
{
341
342
343
    if (modelNode().hasNodeListProperty("resources"))
        return true;

344
345
346
347
348
349
350
351
    return !resources().isEmpty();
}

bool QmlItemNode::instanceHasAnchors() const
{
    return anchors().instanceHasAnchors();
}

352
bool QmlItemNode::instanceHasShowContent() const
353
354
355
356
{
    return nodeInstance().hasContent();
}

357
bool QmlItemNode::instanceCanReparent() const
358
{
359
    return QmlObjectNode::instanceCanReparent() && !anchors().instanceHasAnchors() && !instanceIsAnchoredBySibling();
360
361
}

362
bool QmlItemNode::instanceIsAnchoredBySibling() const
363
{
364
365
366
367
368
369
    return nodeInstance().isAnchoredBySibling();
}

bool QmlItemNode::instanceIsAnchoredByChildren() const
{
    return nodeInstance().isAnchoredByChildren();
370
371
}

372
373
374
375
376
377
378
379
380
381
bool QmlItemNode::instanceIsMovable() const
{
    return nodeInstance().isMovable();
}

bool QmlItemNode::instanceIsResizable() const
{
    return nodeInstance().isResizable();
}

382
bool QmlItemNode::instanceIsInLayoutable() const
383
{
384
     return nodeInstance().isInLayoutable();
385
386
}

387
388
389
390
391
bool QmlItemNode::instanceHasRotationTransform() const
{
    return nodeInstance().transform().type() > QTransform::TxScale;
}

392
393
394
395
396
397
398
399
400
bool itemIsMovable(const ModelNode &modelNode)
{
    if (modelNode.metaInfo().isSubclassOf("QtQuick.Controls.Tab", -1, -1))
        return false;

    return true;
}


401
402
403
bool QmlItemNode::modelIsMovable() const
{
    return !modelNode().hasBindingProperty("x")
404
405
            && !modelNode().hasBindingProperty("y")
            && itemIsMovable(modelNode());
406
407
}

408
409
410
bool QmlItemNode::modelIsResizable() const
{
    return !modelNode().hasBindingProperty("width")
411
412
            && !modelNode().hasBindingProperty("height")
            && itemIsMovable(modelNode());
413
414
}

415
QRectF  QmlItemNode::instanceBoundingRect() const
416
417
418
419
420
{
    return QRectF(QPointF(0, 0), nodeInstance().size());
}

QRectF QmlItemNode::instancePaintedBoundingRect() const
421
422
423
424
{
    return nodeInstance().boundingRect();
}

425
426
427
428
429
QRectF QmlItemNode::instanceContentItemBoundingRect() const
{
    return nodeInstance().contentItemBoundingRect();
}

430
431
432
433
434
QTransform  QmlItemNode::instanceTransform() const
{
    return nodeInstance().transform();
}

435
436
437
438
439
440
441
442
443
444
QTransform QmlItemNode::instanceTransformWithContentTransform() const
{
    return nodeInstance().transform() * nodeInstance().contentTransform();
}

QTransform QmlItemNode::instanceTransformWithContentItemTransform() const
{
    return nodeInstance().transform() * nodeInstance().contentItemTransform();
}

445
446
447
448
449
QTransform QmlItemNode::instanceSceneTransform() const
{
    return nodeInstance().sceneTransform();
}

450
451
452
453
454
QTransform QmlItemNode::instanceSceneContentItemTransform() const
{
    return nodeInstance().sceneTransform() * nodeInstance().contentItemTransform();
}

455
456
QPointF QmlItemNode::instanceScenePosition() const
{
457
458
459
460
    if (hasInstanceParentItem())
        return instanceParentItem().instanceSceneTransform().map(nodeInstance().position());
     else if (modelNode().hasParentProperty() && QmlItemNode::isValidQmlItemNode(modelNode().parentProperty().parentModelNode()))
        return QmlItemNode(modelNode().parentProperty().parentModelNode()).instanceSceneTransform().map(nodeInstance().position());
461
462
463
464

    return QPointF();
}

465
466
467
468
469
470
471
472
473
474
QPointF QmlItemNode::instancePosition() const
{
    return nodeInstance().position();
}

QSizeF QmlItemNode::instanceSize() const
{
    return nodeInstance().size();
}

Marco Bubke's avatar
Marco Bubke committed
475
int QmlItemNode::instancePenWidth() const
476
{
Marco Bubke's avatar
Marco Bubke committed
477
    return nodeInstance().penWidth();
478
479
}

480
481
482
483
484
bool QmlItemNode::instanceIsRenderPixmapNull() const
{
    return nodeInstance().renderPixmap().isNull();
}

485
QPixmap QmlItemNode::instanceRenderPixmap() const
Marco Bubke's avatar
Marco Bubke committed
486
{
487
488
489
490
491
492
    return nodeInstance().renderPixmap();
}

QPixmap QmlItemNode::instanceBlurredRenderPixmap() const
{
    return nodeInstance().blurredRenderPixmap();
Marco Bubke's avatar
Marco Bubke committed
493
494
}

495
496
497
498
499
500
501
502
503
QList<QmlModelState> QmlModelStateGroup::allStates() const
{
    QList<QmlModelState> returnList;

    if (!modelNode().isValid())
        throw new InvalidModelNodeException(__LINE__, __FUNCTION__, __FILE__);

    if (modelNode().property("states").isNodeListProperty()) {
        foreach (const ModelNode &node, modelNode().nodeListProperty("states").toModelNodeList()) {
504
505
            if (QmlModelState::isValidQmlModelState(node))
                returnList.append(node);
506
507
508
509
510
        }
    }
    return returnList;
}

511
TypeName QmlItemNode::simplifiedTypeName() const
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
{
    return modelNode().simplifiedTypeName();
}

uint qHash(const QmlItemNode &node)
{
    return qHash(node.modelNode());
}

QmlModelState QmlModelStateGroup::addState(const QString &name)
{
    if (!modelNode().isValid())
        throw new InvalidModelNodeException(__LINE__, __FUNCTION__, __FILE__);


    PropertyListType propertyList;
528
    propertyList.append(qMakePair(PropertyName("name"), QVariant(name)));
529

530
    ModelNode newState = QmlModelState::createQmlState(modelNode().view(), propertyList);
Marco Bubke's avatar
Marco Bubke committed
531
    modelNode().nodeListProperty("states").reparentHere(newState);
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

    return newState;
}

void QmlModelStateGroup::removeState(const QString &name)
{
    if (!modelNode().isValid())
        throw new InvalidModelNodeException(__LINE__, __FUNCTION__, __FILE__);

    if (state(name).isValid())
        state(name).modelNode().destroy();
}

QmlModelState QmlModelStateGroup::state(const QString &name) const
{
    if (!modelNode().isValid())
        throw new InvalidModelNodeException(__LINE__, __FUNCTION__, __FILE__);

    if (modelNode().property("states").isNodeListProperty()) {
        foreach (const ModelNode &node, modelNode().nodeListProperty("states").toModelNodeList()) {
            if (QmlModelState(node).name() == name)
                return node;
        }
    }
    return QmlModelState();
}

QList<ModelNode> toModelNodeList(const QList<QmlItemNode> &qmlItemNodeList)
{
    QList<ModelNode> modelNodeList;

    foreach (const QmlItemNode &qmlItemNode, qmlItemNodeList)
        modelNodeList.append(qmlItemNode.modelNode());

    return modelNodeList;
}

QList<QmlItemNode> toQmlItemNodeList(const QList<ModelNode> &modelNodeList)
{
    QList<QmlItemNode> qmlItemNodeList;

    foreach (const ModelNode &modelNode, modelNodeList) {
574
575
        if (QmlItemNode::isValidQmlItemNode(modelNode))
            qmlItemNodeList.append(modelNode);
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
    }

    return qmlItemNodeList;
}

const QList<QmlItemNode> QmlItemNode::allDirectSubModelNodes() const
{
    return toQmlItemNodeList(modelNode().allDirectSubModelNodes());
}

const QList<QmlItemNode> QmlItemNode::allSubModelNodes() const
{
    return toQmlItemNodeList(modelNode().allSubModelNodes());
}

bool QmlItemNode::hasAnySubModelNodes() const
{
    return modelNode().hasAnySubModelNodes();
}

596
597
void QmlItemNode::setPosition(const QPointF &position)
{
598
    if (!hasBindingProperty("x") && !anchors().instanceHasAnchor(AnchorLine::Left))
599
600
        setVariantProperty("x", qRound(position.x()));

601
    if (!hasBindingProperty("y") && !anchors().instanceHasAnchor(AnchorLine::Top))
602
603
604
605
606
607
608
609
610
611
612
        setVariantProperty("y", qRound(position.y()));
}

void QmlItemNode::setPostionInBaseState(const QPointF &position)
{
    modelNode().variantProperty("x").setValue(qRound(position.x()));
    modelNode().variantProperty("y").setValue(qRound(position.y()));
}

void QmlItemNode::setSize(const QSizeF &size)
{
613
    if (!hasBindingProperty("width") && !anchors().instanceHasAnchor(AnchorLine::Right))
614
615
        setVariantProperty("width", qRound(size.width()));

616
    if (!hasBindingProperty("height") && !anchors().instanceHasAnchor(AnchorLine::Bottom))
617
618
619
        setVariantProperty("height", qRound(size.height()));
}

620
} //QmlDesigner