OSDN Git Service

It's 2011 now.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / qmldesigner / designercore / instances / qmlstatenodeinstance.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
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
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
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.
24 **
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.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include "qmlstatenodeinstance.h"
35 #include "nodeabstractproperty.h"
36
37 #include <private/qdeclarativestategroup_p.h>
38
39 #include "qmlpropertychangesnodeinstance.h"
40 #include <private/qdeclarativestateoperations_p.h>
41
42 #include <invalidnodeinstanceexception.h>
43
44 namespace QmlDesigner {
45 namespace Internal {
46
47 /**
48   \class QmlStateNodeInstance
49
50   QmlStateNodeInstance manages a QDeclarativeState object.
51   */
52
53 QmlStateNodeInstance::QmlStateNodeInstance(QDeclarativeState *object) :
54         ObjectNodeInstance(object)
55 {
56 }
57
58 QmlStateNodeInstance::Pointer
59         QmlStateNodeInstance::create(QObject *object)
60 {
61     QDeclarativeState *stateObject = qobject_cast<QDeclarativeState*>(object);
62
63     if (stateObject == 0)
64         throw InvalidNodeInstanceException(__LINE__, __FUNCTION__, __FILE__);
65
66     Pointer instance(new QmlStateNodeInstance(stateObject));
67
68     instance->populateResetValueHash();
69
70     return instance;
71 }
72
73 void QmlStateNodeInstance::activateState()
74 {
75     if (stateGroup()) {
76         if (!isStateActive()) {
77             nodeInstanceServer()->setStateInstance(nodeInstanceServer()->instanceForObject(object()));
78             stateGroup()->setState(property("name").toString());
79         }
80     }
81 }
82
83 void QmlStateNodeInstance::deactivateState()
84 {
85     if (stateGroup()) {
86         if (isStateActive()) {
87             nodeInstanceServer()->clearStateInstance();
88             stateGroup()->setState(QString());
89         }
90     }
91 }
92
93 QDeclarativeState *QmlStateNodeInstance::stateObject() const
94 {
95     Q_ASSERT(object());
96     Q_ASSERT(qobject_cast<QDeclarativeState*>(object()));
97     return static_cast<QDeclarativeState*>(object());
98 }
99
100 QDeclarativeStateGroup *QmlStateNodeInstance::stateGroup() const
101 {
102     return stateObject()->stateGroup();
103 }
104
105 bool QmlStateNodeInstance::isStateActive() const
106 {
107     qDebug() << stateObject();
108     qDebug() << stateGroup();
109     if (stateGroup())
110         qDebug() << "state name" << stateGroup()->state() << property("name");
111     return stateObject() && stateGroup() && stateGroup()->state() == property("name");
112 }
113
114 void QmlStateNodeInstance::setPropertyVariant(const QString &name, const QVariant &value)
115 {
116     qDebug() << __FUNCTION__ << stateObject() << name << value;
117     bool hasParent = parent();
118     bool isStateOfTheRootModelNode = parentInstance() && parentInstance()->isRootNodeInstance();
119     if (name == "when" && (!hasParent || isStateOfTheRootModelNode))
120         return;
121
122     ObjectNodeInstance::setPropertyVariant(name, value);
123 }
124
125 void QmlStateNodeInstance::setPropertyBinding(const QString &name, const QString &expression)
126 {
127     bool hasParent = parent();
128     bool isStateOfTheRootModelNode = parentInstance() && parentInstance()->isRootNodeInstance();
129     if (name == "when" && (!hasParent || isStateOfTheRootModelNode))
130         return;
131
132     ObjectNodeInstance::setPropertyBinding(name, expression);
133 }
134
135 bool QmlStateNodeInstance::updateStateVariant(const ObjectNodeInstance::Pointer &target, const QString &propertyName, const QVariant &value)
136 {
137     return stateObject()->changeValueInRevertList(target->object(), propertyName.toLatin1(), value);
138 }
139
140 bool QmlStateNodeInstance::updateStateBinding(const ObjectNodeInstance::Pointer &target, const QString &propertyName, const QString &expression)
141 {
142     return stateObject()->changeValueInRevertList(target->object(), propertyName.toLatin1(), expression);
143 }
144
145 bool QmlStateNodeInstance::resetStateProperty(const ObjectNodeInstance::Pointer &target, const QString &propertyName, const QVariant & /* resetValue */)
146 {
147     return stateObject()->removeEntryFromRevertList(target->object(), propertyName.toLatin1());
148 }
149
150 } // namespace Internal
151 } // namespace QmlDesigner