1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
10 ** GNU Lesser General Public License Usage
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
31 **************************************************************************/
33 #include "behaviordialog.h"
35 #include <abstractview.h>
36 #include <nodeproperty.h>
37 #include <variantproperty.h>
38 #include <bindingproperty.h>
39 #include <nodeproperty.h>
45 namespace QmlDesigner {
47 void BehaviorDialog::registerDeclarativeType()
49 qmlRegisterType<QmlDesigner::BehaviorWidget>("Bauhaus",1,0,"BehaviorWidget");
52 BehaviorWidget::BehaviorWidget() : QPushButton(), m_BehaviorDialog(new BehaviorDialog(0))
55 connect(this, SIGNAL(toggled(bool)), this, SLOT(buttonPressed(bool)));
58 BehaviorWidget::BehaviorWidget(QWidget *parent) : QPushButton(parent), m_BehaviorDialog(new BehaviorDialog(0))
61 connect(this, SIGNAL(toggled(bool)), this, SLOT(buttonPressed(bool)));
64 PropertyEditorNodeWrapper* BehaviorWidget::complexNode() const
69 void BehaviorWidget::setComplexNode(PropertyEditorNodeWrapper* complexNode)
71 m_complexNode = complexNode;
72 m_propertyName = complexNode->propertyName();
73 m_modelNode = complexNode->parentModelNode();
75 if (!modelNode().isValid()) {
76 m_BehaviorDialog->hide();
79 m_BehaviorDialog->setup(modelNode(), propertyName());
82 void BehaviorWidget::buttonPressed(bool show)
85 if (m_BehaviorDialog->isVisible()) {
86 m_BehaviorDialog->reject();
88 m_BehaviorDialog->setup(modelNode(), propertyName());
89 m_BehaviorDialog->show();
95 BehaviorDialog::BehaviorDialog(QWidget *parent) : QDialog(parent), m_ui(new Ui_BehaviorDialog)
101 void BehaviorDialog::setup(const ModelNode &node, const QString propertyName)
104 m_ui->duration->setValue(100);
105 m_ui->velocity->setValue(2);
106 m_ui->spring->setValue(2);
107 m_ui->damping->setValue(2);
108 m_ui->stackedWidget->setCurrentIndex(0);
109 m_ui->curve->setCurrentIndex(0);
111 if (m_modelNode.isValid()) {
112 m_propertyName = propertyName;
113 m_ui->id->setText(m_modelNode.id());
114 m_ui->type->setText(m_modelNode.simplifiedTypeName());
115 m_ui->propertyName->setText(propertyName);
116 if (m_modelNode.hasProperty(m_propertyName) && m_modelNode.property(m_propertyName).isNodeProperty()) {
117 NodeProperty nodeProperty(m_modelNode.nodeProperty(m_propertyName));
118 if (nodeProperty.modelNode().type() == "Qt/SpringFollow") {
119 ModelNode springFollow = nodeProperty.modelNode();
120 m_ui->curve->setCurrentIndex(1);
121 m_ui->stackedWidget->setCurrentIndex(1);
122 if (springFollow.hasProperty("velocity") && springFollow.property("velocity").isVariantProperty())
123 m_ui->velocity->setValue(springFollow.variantProperty("velocity").value().toDouble());
124 if (springFollow.hasProperty("spring") && springFollow.property("spring").isVariantProperty())
125 m_ui->spring->setValue(springFollow.variantProperty("spring").value().toDouble());
126 if (springFollow.hasProperty("damping") && springFollow.property("damping").isVariantProperty())
127 m_ui->damping->setValue(springFollow.variantProperty("damping").value().toDouble());
128 if (springFollow.hasProperty("source") && springFollow.property("source").isVariantProperty())
129 m_ui->source->setText(springFollow.variantProperty("source").value().toString());
130 } else if (nodeProperty.modelNode().type() == "Qt/Behavior") {
131 if (nodeProperty.modelNode().hasProperty("animation") &&
132 nodeProperty.modelNode().property("animation").isNodeProperty() &&
133 nodeProperty.modelNode().nodeProperty("animation").modelNode().type() == "Qt/NumberAnimation") {
134 m_ui->curve->setCurrentIndex(0);
135 ModelNode animation = nodeProperty.modelNode().nodeProperty("animation").modelNode();
136 if (animation.hasProperty("duration") && animation.property("duration").isVariantProperty())
137 m_ui->duration->setValue(animation.variantProperty("duration").value().toInt());
138 if (animation.hasProperty("easing") && animation.property("easing").isVariantProperty()) {
139 QStringList easingItems;
140 for (int i = 0; i < m_ui->curve->count(); i++)
141 easingItems.append(m_ui->curve->itemText(i));
142 m_ui->curve->setCurrentIndex(easingItems.indexOf(animation.variantProperty("easing").value().toString()));
150 void BehaviorDialog::accept()
153 if (m_modelNode.hasProperty(m_propertyName))
154 m_modelNode.removeProperty(m_propertyName);
155 if (m_ui->comboBox->currentIndex() == 0) {
156 RewriterTransaction transaction(m_modelNode.view()->beginRewriterTransaction());
157 ModelNode Behavior = m_modelNode.view()->createModelNode("Qt/Behavior", 4, 7);
158 m_modelNode.nodeProperty(m_propertyName).reparentHere(Behavior);
159 ModelNode animation = m_modelNode.view()->createModelNode("Qt/NumberAnimation", 4, 7);
160 animation.variantProperty("duration") = m_ui->duration->value();
161 animation.variantProperty("easing") = m_ui->curve->currentText();
162 Behavior.nodeProperty("animation").reparentHere(animation);
164 RewriterTransaction transaction(m_modelNode.view()->beginRewriterTransaction());
165 ModelNode springFollow = m_modelNode.view()->createModelNode("Qt/SpringFollow", 4, 7);
166 m_modelNode.nodeProperty(m_propertyName).reparentHere(springFollow);
167 springFollow.variantProperty("velocity") = m_ui->velocity->value();
168 springFollow.variantProperty("spring") = m_ui->spring->value();
169 springFollow.variantProperty("damping") = m_ui->damping->value();
170 springFollow.bindingProperty("source") = m_ui->source->text();
174 void BehaviorDialog::reject()