nodeinstance.cpp 18.5 KB
Newer Older
1 2 3 4
/**************************************************************************
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
5
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
6 7 8 9 10 11 12 13 14 15 16 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
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
**
** 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.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/

#include "nodeinstance.h"

#include "objectnodeinstance.h"
#include "metainfo.h"
#include "graphicswidgetnodeinstance.h"
#include "widgetnodeinstance.h"
#include "qmlgraphicsitemnodeinstance.h"
#include "graphicsscenenodeinstance.h"
#include "graphicsviewnodeinstance.h"
#include "nodeinstanceview.h"
#include "qmlviewnodeinstance.h"
#include "dummynodeinstance.h"
#include "componentnodeinstance.h"
#include "qmltransitionnodeinstance.h"
#include "qmlpropertychangesnodeinstance.h"
45
#include "positionernodeinstance.h"
Marco Bubke's avatar
Marco Bubke committed
46
#include "behaviornodeinstance.h"
47 48 49 50 51 52 53 54 55
#include "qmlstatenodeinstance.h"
#include "nodeabstractproperty.h"
#include "variantproperty.h"

#include <invalidnodeinstanceexception.h>

#include <QHash>
#include <QSet>

56
#include <QtDeclarative/QDeclarativeEngine>
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

/*!
  \class QmlDesigner::NodeInstance
  \ingroup CoreInstance
  \brief NodeInstance is a common handle for the actual object representation of a ModelNode.

   NodeInstance abstracts away the differences e.g. in terms of position and size
   for QWidget, QGraphicsView, QLayout etc objects. Multiple NodeInstance objects can share
   the pointer to the same instance object. The actual instance will be deleted when
   the last NodeInstance object referencing to it is deleted. This can be disabled by
   setDeleteHeldInstance().

   \see QmlDesigner::NodeInstanceView
*/

namespace QmlDesigner {

/*!
\brief Constructor - creates a invalid NodeInstance


\see NodeInstanceView
*/
NodeInstance::NodeInstance()
{
}

/*!
\brief Destructor

*/
NodeInstance::~NodeInstance()
{
}

/*!
\brief Constructor - creates a valid NodeInstance

*/
NodeInstance::NodeInstance(const Internal::ObjectNodeInstance::Pointer &abstractInstance)
  : m_nodeInstance(abstractInstance)
{

}


NodeInstance::NodeInstance(const NodeInstance &other)
  : m_nodeInstance(other.m_nodeInstance)
{
}

NodeInstance &NodeInstance::operator=(const NodeInstance &other)
{
    m_nodeInstance = other.m_nodeInstance;
    return *this;
}

/*!
\brief Paints the NodeInstance with this painter.
\param painter used QPainter
*/
void NodeInstance::paint(QPainter *painter) const
{
    m_nodeInstance->paint(painter);
}

/*!
\brief Creates a new NodeInstace for this NodeMetaInfo

\param metaInfo MetaInfo for which a Instance should be created
127
\param context QDeclarativeContext which should be used
128 129 130 131
\returns Internal Pointer of a NodeInstance
\see NodeMetaInfo
*/
Internal::ObjectNodeInstance::Pointer NodeInstance::createInstance(const NodeMetaInfo &metaInfo,
132
                                                                     QDeclarativeContext *context, QObject *objectToBeWrapped)
133 134 135
{
    Internal::ObjectNodeInstance::Pointer instance;

136 137
    if (!metaInfo.isValid())
        instance = Internal::DummyNodeInstance::create();
138
    else if (metaInfo.isSubclassOf("QtWebKit/WebView", 1, 0))
139
        instance = Internal::DummyNodeInstance::create();
140
    else if (metaInfo.isSubclassOf("Qt/QWidget", 4, 7))
141
        instance = Internal::DummyNodeInstance::create();
142
    else if (metaInfo.isSubclassOf("Qt/QGraphicsView", 4, 7))
143
        instance = Internal::GraphicsViewNodeInstance::create(metaInfo, context, objectToBeWrapped);
144
    else if (metaInfo.isSubclassOf("Qt/QDeclarativeView", 4, 7))
145
        instance = Internal::QDeclarativeViewNodeInstance::create(metaInfo, context, objectToBeWrapped);
146
    else if (metaInfo.isSubclassOf("Qt/QGraphicsWidget", 4, 7))
147
        instance = Internal::GraphicsWidgetNodeInstance::create(metaInfo, context, objectToBeWrapped);
148 149
    else if (metaInfo.isSubclassOf("QDeclarativeBasePositioner", 4, 7))
        instance = Internal::PositionerNodeInstance::create(metaInfo, context, objectToBeWrapped);
150
    else if (metaInfo.isSubclassOf("Qt/Item", 4, 7))
151
        instance = Internal::QmlGraphicsItemNodeInstance::create(metaInfo, context, objectToBeWrapped);
152
    else if (metaInfo.isSubclassOf("Qt/QGraphicsScene", 4, 7))
153
        instance = Internal::GraphicsSceneNodeInstance::create(metaInfo, context, objectToBeWrapped);
154
    else if (metaInfo.isSubclassOf("Qt/Component", 4, 7))
155
        instance = Internal::ComponentNodeInstance::create(metaInfo, context, objectToBeWrapped);
156
    else if (metaInfo.isSubclassOf("Qt/PropertyChanges", 4, 7))
157
        instance = Internal::QmlPropertyChangesNodeInstance::create(metaInfo, context, objectToBeWrapped);
158
    else if (metaInfo.isSubclassOf("Qt/State", 4, 7))
159
        instance = Internal::QmlStateNodeInstance::create(metaInfo, context, objectToBeWrapped);
160
    else if (metaInfo.isSubclassOf("Qt/Transition", 4, 7))
161
        instance = Internal::QmlTransitionNodeInstance::create(metaInfo, context, objectToBeWrapped);
162
    else if (metaInfo.isSubclassOf("Qt/Behavior", 4, 7))
Marco Bubke's avatar
Marco Bubke committed
163
        instance = Internal::BehaviorNodeInstance::create(metaInfo, context, objectToBeWrapped);
164
    else if (metaInfo.isSubclassOf("Qt/QtObject", 4, 7))
165
        instance = Internal::ObjectNodeInstance::create(metaInfo, context, objectToBeWrapped);
166 167
    else
        instance = Internal::DummyNodeInstance::create();
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183


    return instance;
}



NodeInstance NodeInstance::create(NodeInstanceView *nodeInstanceView, const ModelNode &node, QObject *objectToBeWrapped)
{
    Q_ASSERT(node.isValid());
    Q_ASSERT(nodeInstanceView);

    // For the moment just use the root context of the engine
    // for all items. However, this is a hack ... ideally we should
    // rebuild the same context hierarchy as the qml compiler does

184
    QDeclarativeContext *context = nodeInstanceView->engine()->rootContext();
185 186 187 188 189

    NodeInstance instance(createInstance(node.metaInfo(), context, objectToBeWrapped));

    instance.m_nodeInstance->setModelNode(node);

190
    instance.m_nodeInstance->setNodeInstanceView(nodeInstanceView);
191

192 193
    instance.m_nodeInstance->initializePropertyWatcher(instance.m_nodeInstance);

194 195
    instance.setId(node.id());

196
    //QObject::connect(instance.internalObject(), SIGNAL(destroyed(QObject*)), nodeInstanceView, SLOT(removeIdFromContext(QObject*)));
Marco Bubke's avatar
Marco Bubke committed
197

198 199 200 201 202 203 204 205 206 207
    foreach (const VariantProperty &property, node.variantProperties()) {
        if (property.isDynamic())
            instance.setPropertyDynamicVariant(property.name(), property.dynamicTypeName(), property.value());
        else
            instance.setPropertyVariant(property.name(), property.value());
    }

    return instance;
}

208
NodeInstance NodeInstance::create(NodeInstanceView *nodeInstanceView, const NodeMetaInfo &metaInfo, QDeclarativeContext *context)
209 210
{
    NodeInstance instance(createInstance(metaInfo, context, 0));
211
    instance.m_nodeInstance->setNodeInstanceView(nodeInstanceView);
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 254 255 256 257 258 259 260 261 262 263 264 265

    return instance;
}

/*!
\brief Returns the ModelNode of this NodeInstance.
\returns ModelNode of this NodeState
*/
ModelNode NodeInstance::modelNode() const
{
    if (m_nodeInstance.isNull())
        return ModelNode();

    return m_nodeInstance->modelNode();
}


/*!
\brief Changes the NodeState of the ModelNode of this NodeInstance.
    All properties are updated.
\param state NodeState of this NodeInstance
*/
void NodeInstance::setModelNode(const ModelNode &node)
{
    Q_ASSERT(node.isValid());
    if (m_nodeInstance->modelNode() == node)
        return;

    m_nodeInstance->setModelNode(node);
}

/*!
\brief Returns if the NodeInstance is a top level item.
\returns true if this NodeInstance is a top level item
*/
bool NodeInstance::isTopLevel() const
{
    return m_nodeInstance->isTopLevel();
}

void NodeInstance::reparent(const NodeInstance &oldParentInstance, const QString &oldParentProperty, const NodeInstance &newParentInstance, const QString &newParentProperty)
{
    m_nodeInstance->reparent(oldParentInstance, oldParentProperty, newParentInstance, newParentProperty);
}

/*!
\brief Returns the parent NodeInstance of this NodeInstance.

    If there is not parent than the parent is invalid.

\returns Parent NodeInstance.
*/
NodeInstance NodeInstance::parent() const
{
266
    return m_nodeInstance->nodeInstanceParentForObject(m_nodeInstance->parent());
267 268 269 270
}

bool NodeInstance::hasParent() const
{
271
    return m_nodeInstance->parent();
272 273 274
}

/*!
275 276
\brief Returns if the NodeInstance is a QDeclarativeItem.
\returns true if this NodeInstance is a QDeclarativeItem
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 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
*/
bool NodeInstance::isQmlGraphicsItem() const
{
    return m_nodeInstance->isQmlGraphicsItem();
}

/*!
\brief Returns if the NodeInstance is a QGraphicsScene.
\returns true if this NodeInstance is a QGraphicsScene
*/
bool NodeInstance::isGraphicsScene() const
{
    return m_nodeInstance->isGraphicsScene();
}

/*!
\brief Returns if the NodeInstance is a QGraphicsView.
\returns true if this NodeInstance is a QGraphicsView
*/
bool NodeInstance::isGraphicsView() const
{
    return m_nodeInstance->isGraphicsView();
}

/*!
\brief Returns if the NodeInstance is a QGraphicsWidget.
\returns true if this NodeInstance is a QGraphicsWidget
*/
bool NodeInstance::isGraphicsWidget() const
{
    return m_nodeInstance->isGraphicsWidget();
}

/*!
\brief Returns if the NodeInstance is a QGraphicsProxyWidget.
\returns true if this NodeInstance is a QGraphicsProxyWidget
*/
bool NodeInstance::isProxyWidget() const
{
    return m_nodeInstance->isProxyWidget();
}

/*!
\brief Returns if the NodeInstance is a QWidget.
\returns true if this NodeInstance is a QWidget
*/
bool NodeInstance::isWidget() const
{
    return m_nodeInstance->isWidget();
}

/*!
329 330
\brief Returns if the NodeInstance is a QDeclarativeView.
\returns true if this NodeInstance is a QDeclarativeView
331
*/
332
bool NodeInstance::isQDeclarativeView() const
333
{
334
    return m_nodeInstance->isQDeclarativeView();
335 336 337 338 339 340 341 342 343 344 345 346
}

bool NodeInstance::isGraphicsObject() const
{
    return m_nodeInstance->isGraphicsObject();
}

bool NodeInstance::isTransition() const
{
    return m_nodeInstance->isTransition();
}

347 348 349 350 351
bool NodeInstance::isPositioner() const
{
    return m_nodeInstance->isPositioner();
}

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
/*!
\brief Returns if the NodeInstance is a QGraphicsItem.
\returns true if this NodeInstance is a QGraphicsItem
*/
bool NodeInstance::equalGraphicsItem(QGraphicsItem *item) const
{
    return m_nodeInstance->equalGraphicsItem(item);
}

/*!
\brief Returns the bounding rect of the NodeInstance.
\returns QRectF of the NodeInstance
*/
QRectF NodeInstance::boundingRect() const
{
    QRectF boundingRect(m_nodeInstance->boundingRect());

//
//    if (modelNode().isValid()) { // TODO implement recursiv stuff
//        if (qFuzzyIsNull(boundingRect.width()))
//            boundingRect.setWidth(nodeState().property("width").value().toDouble());
//
//        if (qFuzzyIsNull(boundingRect.height()))
//            boundingRect.setHeight(nodeState().property("height").value().toDouble());
//    }

    return boundingRect;
}

void NodeInstance::setPropertyVariant(const QString &name, const QVariant &value)
{
    m_nodeInstance->setPropertyVariant(name, value);
384

385 386
}

387
void NodeInstance::setPropertyDynamicVariant(const QString &name, const QString &typeName, const QVariant &value)
388
{
389 390
    m_nodeInstance->createDynamicProperty(name, typeName);
    m_nodeInstance->setPropertyVariant(name, value);
391 392 393 394 395 396 397
}

void NodeInstance::setPropertyBinding(const QString &name, const QString &expression)
{
    m_nodeInstance->setPropertyBinding(name, expression);
}

398
void NodeInstance::setPropertyDynamicBinding(const QString &name, const QString &typeName, const QString &expression)
399
{
400 401
    m_nodeInstance->createDynamicProperty(name, typeName);
    m_nodeInstance->setPropertyBinding(name, expression);
402 403 404 405 406 407 408
}

void NodeInstance::resetProperty(const QString &name)
{
    m_nodeInstance->resetProperty(name);
}

409 410 411 412 413
void NodeInstance::refreshProperty(const QString &name)
{
    m_nodeInstance->refreshProperty(name);
}

414 415 416 417 418 419 420 421 422 423 424 425 426 427
void NodeInstance::setId(const QString &id)
{
    m_nodeInstance->setId(id);
}

/*!
\brief Returns the property value of the property of this NodeInstance.
\returns QVariant value
*/
QVariant NodeInstance::property(const QString &name) const
{
    return m_nodeInstance->property(name);
}

Thomas Hartmann's avatar
Thomas Hartmann committed
428 429 430 431 432
bool NodeInstance::hasBindingForProperty(const QString &name) const
{
    return m_nodeInstance->hasBindingForProperty(name);
}

433 434 435 436 437 438 439 440 441
/*!
\brief Returns the property default value of the property of this NodeInstance.
\returns QVariant default value which is the reset value to
*/
QVariant NodeInstance::defaultValue(const QString &name) const
{
    return m_nodeInstance->resetValue(name);
}

442 443 444 445 446 447 448 449
/*!
\brief Returns the type of the property of this NodeInstance.
*/
QString NodeInstance::instanceType(const QString &name) const
{
    return m_nodeInstance->instanceType(name);
}

450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
/*!
\brief Returns if the NodeInstance is valid.
\returns true if the NodeInstance is valid
*/
bool NodeInstance::isValid() const
{
    return m_nodeInstance && internalObject();
}

void NodeInstance::makeInvalid()
{
    if (m_nodeInstance)
        m_nodeInstance->destroy();
    m_nodeInstance.clear();
}

bool NodeInstance::hasContent() const
{
    return m_nodeInstance->hasContent();
}

471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
bool NodeInstance::isResizable() const
{
    return m_nodeInstance->isResizable();
}

bool NodeInstance::isMovable() const
{
    return m_nodeInstance->isMovable();
}

bool NodeInstance::isInPositioner() const
{
    return m_nodeInstance->isInPositioner();
}

486 487 488 489 490
bool NodeInstance::hasAnchor(const QString &name) const
{
    return m_nodeInstance->hasAnchor(name);
}

Marco Bubke's avatar
Marco Bubke committed
491 492 493 494 495
int NodeInstance::penWidth() const
{
    return m_nodeInstance->penWidth();
}

496
bool NodeInstance::isAnchoredBySibling() const
497
{
498 499 500 501 502 503
    return m_nodeInstance->isAnchoredBySibling();
}

bool NodeInstance::isAnchoredByChildren() const
{
    return m_nodeInstance->isAnchoredByChildren();
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
}

QPair<QString, NodeInstance> NodeInstance::anchor(const QString &name) const
{
    return m_nodeInstance->anchor(name);
}

uint qHash(const NodeInstance &instance)
{
    return ::qHash(instance.m_nodeInstance.data());
}

bool operator==(const NodeInstance &first, const NodeInstance &second)
{
    return first.m_nodeInstance.data() == second.m_nodeInstance.data();
}

521
bool NodeInstance::isWrappingThisObject(QObject *object) const
522
{
523
    return internalObject() && internalObject() == object;
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 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631
}

/*!
\brief Returns the position in parent coordiantes.
\returns QPointF of the position of the instance.
*/
QPointF NodeInstance::position() const
{
    return m_nodeInstance->position();
}

/*!
\brief Returns the size in local coordiantes.
\returns QSizeF of the size of the instance.
*/
QSizeF NodeInstance::size() const
{
    QSizeF instanceSize = m_nodeInstance->size();

//    if (nodeState().isValid()) {
//        if (qFuzzyIsNull(instanceSize.width()))
//            instanceSize.setWidth(nodeState().property("width").value().toDouble());
//
//        if (qFuzzyIsNull(instanceSize.height()))
//            instanceSize.setHeight(nodeState().property("height").value().toDouble());
//    }
    return instanceSize;
}

QTransform NodeInstance::transform() const
{
    return m_nodeInstance->transform();
}

/*!
\brief Returns the transform matrix of the instance.
\returns QTransform of the instance.
*/
QTransform NodeInstance::customTransform() const
{
    return m_nodeInstance->customTransform();
}

QTransform NodeInstance::sceneTransform() const
{
    return m_nodeInstance->sceneTransform();
}

double NodeInstance::rotation() const
{
    return m_nodeInstance->rotation();
}

double NodeInstance::scale() const
{
    return m_nodeInstance->scale();
}

QList<QGraphicsTransform *> NodeInstance::transformations() const
{
    return m_nodeInstance->transformations();
}

QPointF NodeInstance::transformOriginPoint() const
{
    return m_nodeInstance->transformOriginPoint();
}

double NodeInstance::zValue() const
{
    return m_nodeInstance->zValue();
}

/*!
\brief Returns the opacity of the instance.
\returns 0.0 mean transparent and 1.0 opaque.
*/
double NodeInstance::opacity() const
{
    return m_nodeInstance->opacity();
}


void NodeInstance::setDeleteHeldInstance(bool deleteInstance)
{
    m_nodeInstance->setDeleteHeldInstance(deleteInstance);
}


void NodeInstance::paintUpdate()
{
    m_nodeInstance->paintUpdate();
}


Internal::QmlGraphicsItemNodeInstance::Pointer NodeInstance::qmlGraphicsItemNodeInstance() const
{
    return m_nodeInstance.dynamicCast<Internal::QmlGraphicsItemNodeInstance>();
}

QObject *NodeInstance::internalObject() const
{
    if (m_nodeInstance.isNull())
        return 0;

    return m_nodeInstance->object();
}

Marco Bubke's avatar
Marco Bubke committed
632 633 634 635 636 637 638 639 640 641
void NodeInstance::activateState()
{
    m_nodeInstance->activateState();
}

void NodeInstance::deactivateState()
{
    m_nodeInstance->deactivateState();
}

642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657
bool NodeInstance::updateStateVariant(const NodeInstance &target, const QString &propertyName, const QVariant &value)
{
    return m_nodeInstance->updateStateVariant(target, propertyName, value);
}

bool NodeInstance::updateStateBinding(const NodeInstance &target, const QString &propertyName, const QString &expression)
{
    return m_nodeInstance->updateStateBinding(target, propertyName, expression);
}

QVariant NodeInstance::resetVariant(const QString &propertyName) const
{
    return m_nodeInstance->resetValue(propertyName);
}

bool NodeInstance::resetStateProperty(const NodeInstance &target, const QString &propertyName, const QVariant &resetValue)
Marco Bubke's avatar
Marco Bubke committed
658
{
659
    return m_nodeInstance->resetStateProperty(target, propertyName, resetValue);
Marco Bubke's avatar
Marco Bubke committed
660
}
661 662 663 664 665 666

/*!
 Makes types used in node instances known to the Qml engine. To be called once at initialization time.
*/
void NodeInstance::registerDeclarativeTypes()
{
Marco Bubke's avatar
Marco Bubke committed
667
//    qmlRegisterType<QmlDesigner::Internal::QmlPropertyChangesObject>();
668
}
669

670 671
void NodeInstance::doComponentComplete()
{
672
    m_nodeInstance->doComponentComplete();
673 674
}

675 676 677 678 679
QString NodeInstance::id() const
{
    return m_nodeInstance->id();
}

680
#ifdef QTCREATOR_TEST
681 682 683 684
QObject* NodeInstance::testHandle() const
{
    return internalObject();
}
685 686 687 688 689
Internal::ObjectNodeInstance* NodeInstance::internalInstance() const
{
    return m_nodeInstance.data();
}

690 691
#endif

692
} // namespace QmlDesigner