1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights. These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
34 #include "qmlstatenodeinstance.h"
35 #include "nodeabstractproperty.h"
37 #include <private/qdeclarativestategroup_p.h>
39 #include "qmlpropertychangesnodeinstance.h"
40 #include <private/qdeclarativestateoperations_p.h>
42 #include <invalidnodeinstanceexception.h>
44 namespace QmlDesigner {
48 \class QmlStateNodeInstance
50 QmlStateNodeInstance manages a QDeclarativeState object.
53 QmlStateNodeInstance::QmlStateNodeInstance(QDeclarativeState *object) :
54 ObjectNodeInstance(object)
58 QmlStateNodeInstance::Pointer
59 QmlStateNodeInstance::create(QObject *object)
61 QDeclarativeState *stateObject = qobject_cast<QDeclarativeState*>(object);
64 throw InvalidNodeInstanceException(__LINE__, __FUNCTION__, __FILE__);
66 Pointer instance(new QmlStateNodeInstance(stateObject));
68 instance->populateResetValueHash();
73 void QmlStateNodeInstance::activateState()
76 if (!isStateActive()) {
77 nodeInstanceServer()->setStateInstance(nodeInstanceServer()->instanceForObject(object()));
78 stateGroup()->setState(property("name").toString());
83 void QmlStateNodeInstance::deactivateState()
86 if (isStateActive()) {
87 nodeInstanceServer()->clearStateInstance();
88 stateGroup()->setState(QString());
93 QDeclarativeState *QmlStateNodeInstance::stateObject() const
96 Q_ASSERT(qobject_cast<QDeclarativeState*>(object()));
97 return static_cast<QDeclarativeState*>(object());
100 QDeclarativeStateGroup *QmlStateNodeInstance::stateGroup() const
102 return stateObject()->stateGroup();
105 bool QmlStateNodeInstance::isStateActive() const
107 qDebug() << stateObject();
108 qDebug() << stateGroup();
110 qDebug() << "state name" << stateGroup()->state() << property("name");
111 return stateObject() && stateGroup() && stateGroup()->state() == property("name");
114 void QmlStateNodeInstance::setPropertyVariant(const QString &name, const QVariant &value)
116 qDebug() << __FUNCTION__ << stateObject() << name << value;
117 bool hasParent = parent();
118 bool isStateOfTheRootModelNode = parentInstance() && parentInstance()->isRootNodeInstance();
119 if (name == "when" && (!hasParent || isStateOfTheRootModelNode))
122 ObjectNodeInstance::setPropertyVariant(name, value);
125 void QmlStateNodeInstance::setPropertyBinding(const QString &name, const QString &expression)
127 bool hasParent = parent();
128 bool isStateOfTheRootModelNode = parentInstance() && parentInstance()->isRootNodeInstance();
129 if (name == "when" && (!hasParent || isStateOfTheRootModelNode))
132 ObjectNodeInstance::setPropertyBinding(name, expression);
135 bool QmlStateNodeInstance::updateStateVariant(const ObjectNodeInstance::Pointer &target, const QString &propertyName, const QVariant &value)
137 return stateObject()->changeValueInRevertList(target->object(), propertyName.toLatin1(), value);
140 bool QmlStateNodeInstance::updateStateBinding(const ObjectNodeInstance::Pointer &target, const QString &propertyName, const QString &expression)
142 return stateObject()->changeValueInRevertList(target->object(), propertyName.toLatin1(), expression);
145 bool QmlStateNodeInstance::resetStateProperty(const ObjectNodeInstance::Pointer &target, const QString &propertyName, const QVariant & /* resetValue */)
147 return stateObject()->removeEntryFromRevertList(target->object(), propertyName.toLatin1());
150 } // namespace Internal
151 } // namespace QmlDesigner