qmlstatenodeinstance.cpp 4.69 KB
Newer Older
1 2 3 4
/**************************************************************************
**
** This file is part of Qt Creator
**
con's avatar
con committed
5
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 7 8
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
con's avatar
con committed
9
** No Commercial Usage
10
**
con's avatar
con committed
11 12 13 14
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
15 16 17 18 19 20 21 22 23 24
**
** 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.
**
con's avatar
con committed
25 26 27 28 29 30
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
31 32 33 34
**
**************************************************************************/

#include "qmlstatenodeinstance.h"
35 36
#include "nodeabstractproperty.h"

37
#include <private/qdeclarativestategroup_p.h>
38

39 40 41
#include "qmlpropertychangesnodeinstance.h"
#include <private/qdeclarativestateoperations_p.h>

42 43
#include <invalidnodeinstanceexception.h>

44 45 46 47 48 49
namespace QmlDesigner {
namespace Internal {

/**
  \class QmlStateNodeInstance

50
  QmlStateNodeInstance manages a QDeclarativeState object.
51 52
  */

53
QmlStateNodeInstance::QmlStateNodeInstance(QDeclarativeState *object) :
54 55 56 57 58
        ObjectNodeInstance(object)
{
}

QmlStateNodeInstance::Pointer
59
        QmlStateNodeInstance::create(QObject *object)
60
{
61
    QDeclarativeState *stateObject = qobject_cast<QDeclarativeState*>(object);
62 63 64

    if (stateObject == 0)
        throw InvalidNodeInstanceException(__LINE__, __FUNCTION__, __FILE__);
65 66 67 68 69 70 71 72

    Pointer instance(new QmlStateNodeInstance(stateObject));

    instance->populateResetValueHash();

    return instance;
}

Marco Bubke's avatar
Marco Bubke committed
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
}

Marco Bubke's avatar
Marco Bubke committed
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 97
    Q_ASSERT(qobject_cast<QDeclarativeState*>(object()));
    return static_cast<QDeclarativeState*>(object());
98 99
}

100
QDeclarativeStateGroup *QmlStateNodeInstance::stateGroup() const
101 102 103 104 105 106
{
    return stateObject()->stateGroup();
}

bool QmlStateNodeInstance::isStateActive() const
{
Thomas Hartmann's avatar
Thomas Hartmann committed
107
    return stateObject() && stateGroup() && stateGroup()->state() == property("name");
108 109
}

110 111
void QmlStateNodeInstance::setPropertyVariant(const QString &name, const QVariant &value)
{
112 113 114
    bool hasParent = parent();
    bool isStateOfTheRootModelNode = parentInstance() && parentInstance()->isRootNodeInstance();
    if (name == "when" && (!hasParent || isStateOfTheRootModelNode))
115 116 117 118 119 120 121
        return;

    ObjectNodeInstance::setPropertyVariant(name, value);
}

void QmlStateNodeInstance::setPropertyBinding(const QString &name, const QString &expression)
{
122 123 124
    bool hasParent = parent();
    bool isStateOfTheRootModelNode = parentInstance() && parentInstance()->isRootNodeInstance();
    if (name == "when" && (!hasParent || isStateOfTheRootModelNode))
125 126 127 128 129
        return;

    ObjectNodeInstance::setPropertyBinding(name, expression);
}

130
bool QmlStateNodeInstance::updateStateVariant(const ObjectNodeInstance::Pointer &target, const QString &propertyName, const QVariant &value)
131
{
132
    return stateObject()->changeValueInRevertList(target->object(), propertyName.toLatin1(), value);
133 134
}

135
bool QmlStateNodeInstance::updateStateBinding(const ObjectNodeInstance::Pointer &target, const QString &propertyName, const QString &expression)
136
{
137
    return stateObject()->changeValueInRevertList(target->object(), propertyName.toLatin1(), expression);
138 139
}

140
bool QmlStateNodeInstance::resetStateProperty(const ObjectNodeInstance::Pointer &target, const QString &propertyName, const QVariant & /* resetValue */)
141
{
142
    return stateObject()->removeEntryFromRevertList(target->object(), propertyName.toLatin1());
143 144
}

145 146
} // namespace Internal
} // namespace QmlDesigner