modelnode.cpp 29 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
#include "modelnode.h"
31
32
33
#include <abstractproperty.h>
#include <abstractview.h>
#include <model.h>
34
#include <nodemetainfo.h>
35
36
37
38
39
40
41
42
43
#include "internalnode_p.h"
#include <QHash>
#include <QTextStream>
#include "invalidargumentexception.h"
#include "invalididexception.h"
#include "invalidmodelnodeexception.h"
#include "model_p.h"
#include "variantproperty.h"
#include "bindingproperty.h"
44
#include "signalhandlerproperty.h"
45
46
47
#include "nodeabstractproperty.h"
#include "nodelistproperty.h"
#include "nodeproperty.h"
48
#include <rewriterview.h>
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

namespace QmlDesigner {
using namespace QmlDesigner::Internal;

/*!
\class QmlDesigner::ModelNode
\ingroup CoreModel
\brief The central class to access the node which can represent a widget, layout
            or other items. A Node is a part of a tree and has properties.

Conceptually ModelNode is an opaque handle to the internal data structures.

There is always a root model node in every QmlDesigner::Model:
\code
QmlDesigner::Model *model = QmlDesigner::Model::create();
QmlDesigner::ModelNode rootNode = model->rootNode();
\endcode

You can add a property to a node:
\code
childNode.addProperty("pos", QPoint(2, 12));
\endcode

All the manipulation functions are generating undo commands internally.
*/



/*! \brief internal constructor

*/
80
ModelNode::ModelNode(const InternalNodePointer &internalNode, Model *model, const AbstractView *view):
81
82
        m_internalNode(internalNode),
        m_model(model),
83
        m_view(const_cast<AbstractView*>(view))
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
{
    Q_ASSERT(!m_model || m_view);
}

ModelNode::ModelNode(const ModelNode modelNode, AbstractView *view)
    : m_internalNode(modelNode.m_internalNode),
      m_model(modelNode.model()),
      m_view(view)
{

}

/*! \brief contructs a invalid model node
\return invalid node
\see invalid
*/
ModelNode::ModelNode():
        m_internalNode(new InternalNode)
{

}

ModelNode::ModelNode(const ModelNode &other):
        m_internalNode(other.m_internalNode),
        m_model(other.m_model),
        m_view(other.m_view)
{
}

ModelNode& ModelNode::operator=(const ModelNode &other)
{
    this->m_model = other.m_model;
    this->m_internalNode = other.m_internalNode;
    this->m_view = other.m_view;

    return *this;
}

/*! \brief does nothing
*/
ModelNode::~ModelNode()
{
}

QString ModelNode::generateNewId() const
{
    int counter = 1;
131
    QString newId = QString("%1%2").arg(QString::fromUtf8(simplifiedTypeName()).toLower()).arg(counter);
132

133
    while (view()->hasId(newId)) {
134
        counter += 1;
135
        newId = QString("%1%2").arg(QString::fromUtf8(simplifiedTypeName()).toLower()).arg(counter);
136
137
138
139
140
141
142
143
144
145
    }

    return newId;
}

/*! \brief returns the name of node which is a short cut to a property like objectName
\return name of the node
*/
QString ModelNode::id() const
{
146
    if (!isValid())
147
148
149
150
151
152
153
154
155
156
157
158
159
        throw InvalidModelNodeException(__LINE__, __FUNCTION__, __FILE__);

    return m_internalNode->id();
}

QString ModelNode::validId()
{
    if (id().isEmpty())
        setId(generateNewId());

    return id();
}

160
161
162
static bool idIsQmlKeyWord(const QString& id)
{
    QStringList keywords;
163
    keywords << "import" << "as";
164
165
166
167

    return keywords.contains(id);
}

168
169
static bool idContainsWrongLetter(const QString& id)
{
170
    static QRegExp idExpr(QLatin1String("[a-z_][a-zA-Z0-9_]*"));
171
172
173
174
175
    return !idExpr.exactMatch(id);
}

bool ModelNode::isValidId(const QString &id)
{
176
    return id.isEmpty() || (!idContainsWrongLetter(id) && !idIsQmlKeyWord(id));
177
178
179
180
}

void ModelNode::setId(const QString& id)
{
181
    Internal::WriteLocker locker(m_model.data());
182
183
184
185
186
187
    if (!isValid()) {
        Q_ASSERT_X(isValid(), Q_FUNC_INFO, "model node is invalid");
        throw InvalidModelNodeException(__LINE__, __FUNCTION__, __FILE__);
    }

    if (!isValidId(id))
188
        throw InvalidIdException(__LINE__, __FUNCTION__, __FILE__, id, InvalidIdException::InvalidCharacters);
189
190
191
192
193

    if (id == ModelNode::id())
        return;

    if (view()->hasId(id))
194
        throw InvalidIdException(__LINE__, __FUNCTION__, __FILE__, id, InvalidIdException::DuplicateId);
195

hjk's avatar
hjk committed
196
    m_model.data()->d->changeNodeId(internalNode(), id);
197
198
199
200
201
}

/*! \brief the fully-qualified type name of the node is represented as string
\return type of the node as a string
*/
202
TypeName ModelNode::type() const
203
204
205
206
207
208
209
210
{
    if (!isValid()) {
        Q_ASSERT_X(isValid(), Q_FUNC_INFO, "model node is invalid");
        throw InvalidModelNodeException(__LINE__, __FUNCTION__, __FILE__);
    }
    return m_internalNode->type();
}

211
/*! \brief minor number of the QML type
212
213
214
215
216
217
218
219
220
221
222
\return minor number
*/
int ModelNode::minorVersion() const
{
    if (!isValid()) {
        Q_ASSERT_X(isValid(), Q_FUNC_INFO, "model node is invalid");
        throw InvalidModelNodeException(__LINE__, __FUNCTION__, __FILE__);
    }
    return m_internalNode->minorVersion();
}

223
/*! \brief major number of the QML type
224
225
226
227
228
229
230
231
232
233
234
\return major number
*/
int ModelNode::majorVersion() const
{
    if (!isValid()) {
        Q_ASSERT_X(isValid(), Q_FUNC_INFO, "model node is invalid");
        throw InvalidModelNodeException(__LINE__, __FUNCTION__, __FILE__);
    }
    return m_internalNode->majorVersion();
}

235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
int getMajorVersionFromImport(Model *model)
{
    foreach (const Import &import, model->imports()) {
        if (import.isLibraryImport() && import.url() == QLatin1String("QtQuick")) {
            const QString versionString = import.version();
            if (versionString.contains(QLatin1String("."))) {
                const QString majorVersionString = versionString.split(QLatin1String(".")).first();
                return majorVersionString.toInt();
            }
        }
    }

    return -1;
}

int getMajorVersionFromNode(const ModelNode &modelNode)
{
    if (modelNode.metaInfo().isValid()) {
        if (modelNode.type() == "QtQuick.QtObject" || modelNode.type() == "QtQuick.Item")
            return modelNode.majorVersion();

        foreach (const NodeMetaInfo &superClass,  modelNode.metaInfo().superClasses()) {
            if (modelNode.type() == "QtQuick.QtObject" || modelNode.type() == "QtQuick.Item")
                return superClass.majorVersion();
        }
    }

    return 1; //default
}

265
266
267
268
269
270
271
272
273
274
/*! \brief major number of the QtQuick version used
\return major number of QtQuickVersion
*/
int ModelNode::majorQtQuickVersion() const
{
    if (!isValid()) {
        Q_ASSERT_X(isValid(), Q_FUNC_INFO, "model node is invalid");
        throw InvalidModelNodeException(__LINE__, __FUNCTION__, __FILE__);
    }

275
276
277
    int majorVersionFromImport = getMajorVersionFromImport(model());
    if (majorVersionFromImport >= 0)
        return majorVersionFromImport;
278

279
    return getMajorVersionFromNode(*this);
280
281
}

282
283

/*! \return the short-hand type name of the node. */
284
TypeName ModelNode::simplifiedTypeName() const
285
286
287
288
289
290
{
    if (!isValid()) {
        Q_ASSERT_X(isValid(), Q_FUNC_INFO, "model node is invalid");
        throw InvalidModelNodeException(__LINE__, __FUNCTION__, __FILE__);
    }

291
    return type().split('.').last();
292
293
294
295
296
297
298
299
300
301
302
303
304
}

/*! \brief Returns whether the node is valid

A node is valid if its model still exists, and contains this node.
Also, the current state must be a valid one.

A node might become invalid if e.g. it or one of its ancestors is deleted.

\return is a node valid(true) or invalid(false)
*/
bool ModelNode::isValid() const
{
Thomas Hartmann's avatar
Thomas Hartmann committed
305
    return !m_model.isNull() && !m_view.isNull() && m_internalNode && m_internalNode->isValid();
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
}

/*!
  \brief Returns whether the root node of the model is one of the anchestors of this node.

  Will return true also for the root node itself.
  */
bool ModelNode::isInHierarchy() const
{
    if (!isValid()) {
        Q_ASSERT_X(isValid(), Q_FUNC_INFO, "model node is invalid");
        throw InvalidModelNodeException(__LINE__, __FUNCTION__, __FILE__);
    }
    if (isRootNode())
        return true;
    if (!hasParentProperty())
        return false;
    return parentProperty().parentModelNode().isInHierarchy();
}

/*!
  \brief Returns the property containing this node

  The NodeAbstractProperty is invalid if this ModelNode has no parent.
  NodeAbstractProperty can be a NodeProperty containing a single ModelNode, or
  a NodeListProperty.

  \return the property containing this ModelNode
  */
NodeAbstractProperty ModelNode::parentProperty() const
{
    if (!isValid()) {
        Q_ASSERT_X(isValid(), Q_FUNC_INFO, "model node is invalid");
        throw InvalidModelNodeException(__LINE__, __FUNCTION__, __FILE__);
    }
    if (m_internalNode->parentProperty().isNull())
        return NodeAbstractProperty();

    return NodeAbstractProperty(m_internalNode->parentProperty()->name(), m_internalNode->parentProperty()->propertyOwner(), m_model.data(), view());
}


/*! \brief the command id is used to compress the some commands together.
\param newParentNode parent of this node will be set to this node
\param commandId integer which is used to descripe commands which should compressed together to one command

For example:
\code
node.setParentNode(parentNode1);
node.setParentNode(parentNode2, 212);
node.setParentNode(parentNode3, 212);
model->undoStack()->undo();
ModelNode parentNode4 = node.parentProperty().parentModelNode();
parentNode4 == parentNode1; -> true
\endcode

Marco Bubke's avatar
Marco Bubke committed
362
\see parentNode childNodes hasChildNodes Model::undo
363
364
365
366
367
368
369
370
371
372

*/

void ModelNode::setParentProperty(NodeAbstractProperty parent)
{
    if (!isValid()) {
        Q_ASSERT_X(isValid(), Q_FUNC_INFO, "model node is invalid");
        throw InvalidModelNodeException(__LINE__, __FUNCTION__, __FILE__);
    }

373
    if (!parent.parentModelNode().isValid())
374
375
376
377
378
379
380
381
382
383
384
385
386
        throw InvalidArgumentException(__LINE__, __FUNCTION__, __FILE__, "newParentNode");

    if (*this == parent.parentModelNode()) {
        Q_ASSERT_X(*this != parent.parentModelNode(), Q_FUNC_INFO, "cannot set parent to itself");
        throw InvalidModelNodeException(__LINE__, __FUNCTION__, __FILE__);
    }

    if (parent == parentProperty())
        return;

    parent.reparentHere(*this);
}

387
void ModelNode::setParentProperty(const ModelNode &newParentNode, const PropertyName &propertyName)
388
389
390
391
392
393
{
    setParentProperty(newParentNode.nodeAbstractProperty(propertyName));
}

/*! \brief test if there is a parent for this node
\return true is this node has a parent
Marco Bubke's avatar
Marco Bubke committed
394
\see childNodes parentNode setParentNode hasChildNodes Model::undo
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
*/
bool ModelNode::hasParentProperty() const
{
    if (!isValid()) {
        Q_ASSERT_X(isValid(), Q_FUNC_INFO, "model node is invalid");
        throw InvalidModelNodeException(__LINE__, __FUNCTION__, __FILE__);
    }

    if (m_internalNode->parentProperty().isNull())
        return false;

    return true;
}

/*!
  \brief Returns a BindingProperty

  Note that a valid BindingProperty is returned, if the ModelNode is valid,
  even if this property does not exist or is not a BindingProperty.
  Assigning an expression to this BindingProperty will create the property.

  \return BindingProperty named name
  */

419
BindingProperty ModelNode::bindingProperty(const PropertyName &name) const
420
421
422
423
424
425
426
{
    if (!isValid())
        throw InvalidModelNodeException(__LINE__, __FUNCTION__, __FILE__);

    return BindingProperty(name, m_internalNode, model(), view());
}

427
428
429
430
431
432
433
434
SignalHandlerProperty ModelNode::signalHandlerProperty(const PropertyName &name) const
{
    if (!isValid())
        throw InvalidModelNodeException(__LINE__, __FUNCTION__, __FILE__);

    return SignalHandlerProperty(name, m_internalNode, model(), view());
}

435
436
437
438
439
440
441
442
443
444
445

/*!
  \brief Returns a NodeProperty

  Note that a valid NodeProperty is returned, if the ModelNode is valid,
  even if this property does not exist or is not a NodeProperty.
  Assigning a ModelNode to this NodeProperty will create the property.

  \return NodeProperty named name
  */

446
NodeProperty ModelNode::nodeProperty(const PropertyName &name) const
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
{
      if (!isValid())
        throw InvalidModelNodeException(__LINE__, __FUNCTION__, __FILE__);

    return NodeProperty(name, m_internalNode, model(), view());
}


/*!
  \brief Returns a NodeListProperty

  Note that a valid NodeListProperty is returned, if the ModelNode is valid,
  even if this property does not exist or is not a NodeListProperty.
  Assigning a ModelNode to this NodeListProperty will create the property.

  \return NodeListProperty named name
  */

465
NodeListProperty ModelNode::nodeListProperty(const PropertyName &name) const
466
467
468
469
470
471
472
{
    if (!isValid())
        throw InvalidModelNodeException(__LINE__, __FUNCTION__, __FILE__);

    return NodeListProperty(name, m_internalNode, model(), view());
}

473
NodeAbstractProperty ModelNode::nodeAbstractProperty(const PropertyName &name) const
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
{
     if (!isValid())
        throw InvalidModelNodeException(__LINE__, __FUNCTION__, __FILE__);

    return NodeAbstractProperty(name, m_internalNode, model(), view());
}


/*!
  \brief Returns a VariantProperty

  Note that a valid VariantProperty is returned, if the ModelNode is valid,
  even if this property does not exist or is not a VariantProperty.
  Assigning a value to this VariantProperty will create the property.

  \return VariantProperty named name
  */

492
VariantProperty ModelNode::variantProperty(const PropertyName &name) const
493
494
495
496
497
498
499
{
    if (!isValid())
        throw InvalidModelNodeException(__LINE__, __FUNCTION__, __FILE__);

    return VariantProperty(name, m_internalNode, model(), view());
}

500
AbstractProperty ModelNode::property(const PropertyName &name) const
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
{
    if (!isValid())
        throw InvalidModelNodeException(__LINE__, __FUNCTION__, __FILE__);

    return AbstractProperty(name, m_internalNode, model(), view());
}

/*! \brief returns a property
\param name name of the property
\return returns a node property handle. If the property is not set yet, the node property is still valid (lazy reference).

It is searching only in the local Property.

\see addProperty changePropertyValue removeProperty properties hasProperties
*/

/*! \brief returns a list of all properties
\return list of all properties

The list of properties

*/
QList<AbstractProperty> ModelNode::properties() const
{
    if (!isValid())
        throw InvalidModelNodeException(__LINE__, __FUNCTION__, __FILE__);

    QList<AbstractProperty> propertyList;

530
    foreach (const PropertyName &propertyName, internalNode()->propertyNameList()) {
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
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
        AbstractProperty property(propertyName, internalNode(), model(), view());
        propertyList.append(property);
    }

    return propertyList;
}


/*! \brief returns a list of all VariantProperties
\return list of all VariantProperties

The list of all properties containing just an atomic value.

*/
QList<VariantProperty> ModelNode::variantProperties() const
{
    QList<VariantProperty> propertyList;

    foreach (const AbstractProperty &abstractProperty, properties())
        if (abstractProperty.isVariantProperty())
            propertyList.append(abstractProperty.toVariantProperty());
    return propertyList;
}

QList<NodeAbstractProperty> ModelNode::nodeAbstractProperties() const
{
    QList<NodeAbstractProperty> propertyList;

    foreach (const AbstractProperty &nodeAbstractProperty, properties())
        if (nodeAbstractProperty.isNodeAbstractProperty())
            propertyList.append(nodeAbstractProperty.toNodeAbstractProperty());
    return propertyList;
}

QList<NodeProperty> ModelNode::nodeProperties() const
{
    QList<NodeProperty> propertyList;

    foreach (const AbstractProperty &nodeProperty, properties())
        if (nodeProperty.isNodeProperty())
            propertyList.append(nodeProperty.toNodeProperty());
    return propertyList;
}

QList<NodeListProperty> ModelNode::nodeListProperties() const
{
    QList<NodeListProperty> propertyList;

    foreach (const AbstractProperty &nodeListProperty, properties())
        if (nodeListProperty.isNodeListProperty())
            propertyList.append(nodeListProperty.toNodeListProperty());
    return propertyList;
}


/*! \brief returns a list of all BindingProperties
\return list of all BindingProperties

The list of all properties containing an expression.

*/
QList<BindingProperty> ModelNode::bindingProperties() const
{
    QList<BindingProperty> propertyList;

    foreach (const AbstractProperty &bindingProperty, properties())
        if (bindingProperty.isBindingProperty())
            propertyList.append(bindingProperty.toBindingProperty());
    return propertyList;
}

/*!
\brief removes a property from this node
\param name name of the property

Does nothing if the node state does not set this property.

\see addProperty property  properties hasProperties
*/
610
void ModelNode::removeProperty(const PropertyName &name)
611
612
613
614
{
    if (!isValid())
        throw InvalidModelNodeException(__LINE__, __FUNCTION__, __FILE__);

hjk's avatar
hjk committed
615
    model()->d->checkPropertyName(name);
616
617

    if (internalNode()->hasProperty(name))
hjk's avatar
hjk committed
618
        model()->d->removeProperty(internalNode()->property(name));
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
}


/*! \brief removes this node from the node tree
*/

static QList<ModelNode> descendantNodes(const ModelNode &parent)
{
    QList<ModelNode> descendants(parent.allDirectSubModelNodes());
    foreach (const ModelNode &child, parent.allDirectSubModelNodes()) {
        descendants += descendantNodes(child);
    }
    return descendants;
}

static void removeModelNodeFromSelection(const ModelNode &node)
{
    { // remove nodes from the active selection:
        QList<ModelNode> selectedList = node.view()->selectedModelNodes();

        foreach (const ModelNode &childModelNode, descendantNodes(node))
            selectedList.removeAll(childModelNode);
        selectedList.removeAll(node);

        node.view()->setSelectedModelNodes(selectedList);
    }
}


/*! \brief complete removes this ModelNode from the Model

*/
void ModelNode::destroy()
{
    if (!isValid()) {
        Q_ASSERT_X(isValid(), Q_FUNC_INFO, "model node is invalid");
        throw InvalidModelNodeException(__LINE__, __FUNCTION__, __FILE__);
    }

658
    if (isRootNode())
659
660
661
        throw InvalidArgumentException(__LINE__, __FUNCTION__, __FILE__, "rootNode");

    removeModelNodeFromSelection(*this);
hjk's avatar
hjk committed
662
    model()->d->removeNode(internalNode());
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
}
//\}

/*! \name Property Manipulation
 *  This methodes interact with properties.
 */


/*!
  \brief Returns if the the two nodes reference the same entity in the same model
  */
bool operator ==(const ModelNode &firstNode, const ModelNode &secondNode)
{
    if (firstNode.m_internalNode.isNull() || secondNode.m_internalNode.isNull()) {
        Q_ASSERT_X(0, Q_FUNC_INFO, "model node is invalid");
        throw InvalidModelNodeException(__LINE__, __FUNCTION__, __FILE__);
    }

681
    return firstNode.internalId() == secondNode.internalId();
682
683
684
685
686
687
688
689
690
691
692
693
}

/*!
  \brief Returns if the the two nodes do not reference the same entity in the same model
  */
bool operator !=(const ModelNode &firstNode, const ModelNode &secondNode)
{
    if (firstNode.m_internalNode.isNull() || secondNode.m_internalNode.isNull()) {
        Q_ASSERT_X(0, Q_FUNC_INFO, "model node is invalid");
        throw InvalidModelNodeException(__LINE__, __FUNCTION__, __FILE__);
    }

694
    return firstNode.internalId() != secondNode.internalId();
695
696
697
698
699
700
701
702
703
}

bool operator <(const ModelNode &firstNode, const ModelNode &secondNode)
{
    if (firstNode.m_internalNode.isNull() || secondNode.m_internalNode.isNull()) {
        Q_ASSERT_X(0, Q_FUNC_INFO, "model node is invalid");
        throw InvalidModelNodeException(__LINE__, __FUNCTION__, __FILE__);
    }

704
    return firstNode.internalId() < secondNode.internalId();
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
}


Internal::InternalNodePointer ModelNode::internalNode() const
{
    if (!isValid()) {
        Q_ASSERT_X(isValid(), Q_FUNC_INFO, "model node is invalid");
        throw InvalidModelNodeException(__LINE__, __FUNCTION__, __FILE__);
    }
    return m_internalNode;
}


uint qHash(const ModelNode &node)
{
720
    return ::qHash(node.internalId());
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
}

/*!
\brief returns the model of the node
\return returns the model of the node
*/
Model *ModelNode::model() const
{
    return m_model.data();
}

/*!
\brief returns the view of the node
Each ModelNode belongs to one specific view.
\return view of the node
*/
AbstractView *ModelNode::view() const
{
    return m_view.data();
}


/*!
\brief returns all ModelNodes that are direct children of this ModelNode
The list contains every ModelNode that belongs to one of this ModelNodes
properties.
\return a list of all ModelNodes that are direct children
*/
const QList<ModelNode> ModelNode::allDirectSubModelNodes() const
{
    return toModelNodeList(internalNode()->allDirectSubNodes(), view());
}


/*!
\brief returns all ModelNodes that are direct or indirect children of this ModelNode
The list contains every ModelNode that is a direct or indirect child of this ModelNode.
All children in this list will be implicitly removed if this ModelNode is destroyed.
\return a list of all ModelNodes that are direct or indirect children
*/

const QList<ModelNode> ModelNode::allSubModelNodes() const
{
    return toModelNodeList(internalNode()->allSubNodes(), view());
}

/*!
\brief returns if this ModelNode has any child ModelNodes.

\return if this ModelNode has any child ModelNodes
*/

bool ModelNode::hasAnySubModelNodes() const
{
    return !nodeAbstractProperties().isEmpty();
}

const NodeMetaInfo ModelNode::metaInfo() const
{
    if (!isValid()) {
        Q_ASSERT_X(isValid(), Q_FUNC_INFO, "model node is invalid");
        throw InvalidModelNodeException(__LINE__, __FUNCTION__, __FILE__);
    }
784

785
    return NodeMetaInfo(model()->metaInfoProxyModel(), type(), majorVersion(), minorVersion());
786
787
}

788
789
790
791
792
793
794
795
796
797
bool ModelNode::hasMetaInfo() const
{
    if (!isValid()) {
        Q_ASSERT_X(isValid(), Q_FUNC_INFO, "model node is invalid");
        throw InvalidModelNodeException(__LINE__, __FUNCTION__, __FILE__);
    }

    return model()->hasNodeMetaInfo(type(), majorVersion(), minorVersion());
}

798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
/*! \brief has a node the selection of the model
\return true if the node his selection
*/
bool ModelNode::isSelected() const
{
    if (!isValid()) {
        Q_ASSERT_X(isValid(), Q_FUNC_INFO, "model node is invalid");
        throw InvalidModelNodeException(__LINE__, __FUNCTION__, __FILE__);
    }
    return view()->selectedModelNodes().contains(ModelNode(m_internalNode, m_model.data(), view()));
}

/*! \briefis this node the root node of the model
\return true if it is the root node
*/
bool ModelNode::isRootNode() const
{
    if (!isValid()) {
        Q_ASSERT_X(isValid(), Q_FUNC_INFO, "model node is invalid");
        throw InvalidModelNodeException(__LINE__, __FUNCTION__, __FILE__);
    }
    return view()->rootModelNode() == *this;
}

/*! \brief returns the list of all property names
\return list of all property names set in this state.

The list of properties set in this state.

\see addProperty property changePropertyValue removeProperty hasProperties
*/
829
PropertyNameList ModelNode::propertyNames() const
830
831
832
833
834
835
836
837
838
{
    if (!isValid())
        throw InvalidModelNodeException(__LINE__, __FUNCTION__, __FILE__);
    return internalNode()->propertyNameList();
}

/*! \brief test a if a property is set for this node
\return true if property a property ins this or a ancestor state exists
*/
839
bool ModelNode::hasProperty(const PropertyName &name) const
840
841
842
843
844
845
846
{
    if (!isValid())
        throw InvalidModelNodeException(__LINE__, __FUNCTION__, __FILE__);

    return internalNode()->hasProperty(name);
}

847
bool ModelNode::hasVariantProperty(const PropertyName &name) const
848
849
850
851
{
    return hasProperty(name) && internalNode()->property(name)->isVariantProperty();
}

852
bool ModelNode::hasBindingProperty(const PropertyName &name) const
853
854
855
856
{
    return hasProperty(name) && internalNode()->property(name)->isBindingProperty();
}

857
bool ModelNode::hasNodeAbstracProperty(const PropertyName &name) const
858
859
860
861
{
    return hasProperty(name) && internalNode()->property(name)->isNodeAbstractProperty();
}

862
bool ModelNode::hasNodeProperty(const PropertyName &name) const
863
864
865
866
{
    return hasProperty(name) && internalNode()->property(name)->isNodeProperty();
}

867
bool ModelNode::hasNodeListProperty(const PropertyName &name) const
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
{
    return hasProperty(name) && internalNode()->property(name)->isNodeListProperty();
}

static bool recursiveAncestor(const ModelNode &possibleAncestor, const ModelNode &node)
{
    if (node.hasParentProperty()) {
        if (node.parentProperty().parentModelNode() == possibleAncestor)
           return true;
        return recursiveAncestor(possibleAncestor, node.parentProperty().parentModelNode());
    }

    return false;
}

bool ModelNode::isAncestorOf(const ModelNode &node) const
{
    return recursiveAncestor(*this, node);
}

QDebug operator<<(QDebug debug, const ModelNode &modelNode)
{
    if (modelNode.isValid()) {
        debug.nospace() << "ModelNode("
892
                << modelNode.internalId() << ", "
893
                << modelNode.type() << ", "
894
                << modelNode.id() << ')';
895
896
897
898
899
900
901
902
903
904
905
906
    } else {
        debug.nospace() << "ModelNode(invalid)";
    }

    return debug.space();
}

QTextStream& operator<<(QTextStream &stream, const ModelNode &modelNode)
{
    if (modelNode.isValid()) {
        stream << "ModelNode("
                << "type: " << modelNode.type() << ", "
907
                << "id: " << modelNode.id() << ')';
908
909
910
911
912
913
914
915
916
    } else {
        stream << "ModelNode(invalid)";
    }

    return stream;
}

void ModelNode::selectNode()
{
917
    if (!isValid())
918
919
920
921
922
923
924
925
926
927
            throw InvalidModelNodeException(__LINE__, __FUNCTION__, __FILE__);

    QList<ModelNode> selectedNodeList;
    selectedNodeList.append(*this);

    view()->setSelectedModelNodes(selectedNodeList);
}

void ModelNode::deselectNode()
{
928
    if (!isValid())
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
            throw InvalidModelNodeException(__LINE__, __FUNCTION__, __FILE__);

    QList<ModelNode> selectedNodeList(view()->selectedModelNodes());
    selectedNodeList.removeAll(*this);

    view()->setSelectedModelNodes(selectedNodeList);
}

int ModelNode::variantUserType()
{
    return qMetaTypeId<ModelNode>();
}

QVariant ModelNode::toVariant() const
{
    return QVariant::fromValue(*this);
}

947
QVariant ModelNode::auxiliaryData(const PropertyName &name) const
948
{
949
    if (!isValid())
950
951
952
953
954
        throw InvalidModelNodeException(__LINE__, __FUNCTION__, __FILE__);

    return internalNode()->auxiliaryData(name);
}

955
void ModelNode::setAuxiliaryData(const PropertyName &name, const QVariant &data) const
956
{
957
    Internal::WriteLocker locker(m_model.data());
hjk's avatar
hjk committed
958
    m_model.data()->d->setAuxiliaryData(internalNode(), name, data);
959
960
}

961
bool ModelNode::hasAuxiliaryData(const PropertyName &name) const
962
{
963
    if (!isValid())
964
965
966
967
968
        throw InvalidModelNodeException(__LINE__, __FUNCTION__, __FILE__);

    return internalNode()->hasAuxiliaryData(name);
}

969
QHash<PropertyName, QVariant> ModelNode::auxiliaryData() const
970
{
971
    if (!isValid())
972
973
974
975
976
        throw InvalidModelNodeException(__LINE__, __FUNCTION__, __FILE__);

    return internalNode()->auxiliaryData();
}

Marco Bubke's avatar
Marco Bubke committed
977
978
void  ModelNode::setScriptFunctions(const QStringList &scriptFunctionList)
{
hjk's avatar
hjk committed
979
    model()->d->setScriptFunctions(internalNode(), scriptFunctionList);
Marco Bubke's avatar
Marco Bubke committed
980
981
982
983
984
985
986
}

QStringList  ModelNode::scriptFunctions() const
{
    return internalNode()->scriptFunctions();
}

987
988
989
990
991
992
993
994
qint32 ModelNode::internalId() const
{
    if (m_internalNode.isNull())
        return -1;

    return m_internalNode->internalId();
}

995
void ModelNode::setNodeSource(const QString &newNodeSource)
996
997
998
999
1000
{
    Internal::WriteLocker locker(m_model.data());

    if (!isValid()) {
        Q_ASSERT_X(isValid(), Q_FUNC_INFO, "model node is invalid");
For faster browsing, not all history is shown. View entire blame