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 "behaviordialog.h"
36 #include <abstractview.h>
37 #include <nodeproperty.h>
38 #include <variantproperty.h>
39 #include <bindingproperty.h>
40 #include <nodeproperty.h>
46 namespace QmlDesigner {
48 void BehaviorDialog::registerDeclarativeType()
50 qmlRegisterType<QmlDesigner::BehaviorWidget>("Bauhaus",1,0,"BehaviorWidget");
53 BehaviorWidget::BehaviorWidget() : QPushButton(), m_BehaviorDialog(new BehaviorDialog(0))
56 connect(this, SIGNAL(toggled(bool)), this, SLOT(buttonPressed(bool)));
59 BehaviorWidget::BehaviorWidget(QWidget *parent) : QPushButton(parent), m_BehaviorDialog(new BehaviorDialog(0))
62 connect(this, SIGNAL(toggled(bool)), this, SLOT(buttonPressed(bool)));
65 PropertyEditorNodeWrapper* BehaviorWidget::complexNode() const
70 void BehaviorWidget::setComplexNode(PropertyEditorNodeWrapper* complexNode)
72 m_complexNode = complexNode;
73 m_propertyName = complexNode->propertyName();
74 m_modelNode = complexNode->parentModelNode();
76 if (!modelNode().isValid()) {
77 m_BehaviorDialog->hide();
80 m_BehaviorDialog->setup(modelNode(), propertyName());
83 void BehaviorWidget::buttonPressed(bool show)
86 if (m_BehaviorDialog->isVisible()) {
87 m_BehaviorDialog->reject();
89 m_BehaviorDialog->setup(modelNode(), propertyName());
90 m_BehaviorDialog->show();
96 BehaviorDialog::BehaviorDialog(QWidget *parent) : QDialog(parent), m_ui(new Ui_BehaviorDialog)
102 void BehaviorDialog::setup(const ModelNode &node, const QString propertyName)
105 m_ui->duration->setValue(100);
106 m_ui->velocity->setValue(2);
107 m_ui->spring->setValue(2);
108 m_ui->damping->setValue(2);
109 m_ui->stackedWidget->setCurrentIndex(0);
110 m_ui->curve->setCurrentIndex(0);
112 if (m_modelNode.isValid()) {
113 m_propertyName = propertyName;
114 m_ui->id->setText(m_modelNode.id());
115 m_ui->type->setText(m_modelNode.simplifiedTypeName());
116 m_ui->propertyName->setText(propertyName);
117 if (m_modelNode.hasProperty(m_propertyName) && m_modelNode.property(m_propertyName).isNodeProperty()) {
118 NodeProperty nodeProperty(m_modelNode.nodeProperty(m_propertyName));
119 if (nodeProperty.modelNode().type() == "Qt/SpringFollow") {
120 ModelNode springFollow = nodeProperty.modelNode();
121 m_ui->curve->setCurrentIndex(1);
122 m_ui->stackedWidget->setCurrentIndex(1);
123 if (springFollow.hasProperty("velocity") && springFollow.property("velocity").isVariantProperty())
124 m_ui->velocity->setValue(springFollow.variantProperty("velocity").value().toDouble());
125 if (springFollow.hasProperty("spring") && springFollow.property("spring").isVariantProperty())
126 m_ui->spring->setValue(springFollow.variantProperty("spring").value().toDouble());
127 if (springFollow.hasProperty("damping") && springFollow.property("damping").isVariantProperty())
128 m_ui->damping->setValue(springFollow.variantProperty("damping").value().toDouble());
129 if (springFollow.hasProperty("source") && springFollow.property("source").isVariantProperty())
130 m_ui->source->setText(springFollow.variantProperty("source").value().toString());
131 } else if (nodeProperty.modelNode().type() == "Qt/Behavior") {
132 if (nodeProperty.modelNode().hasProperty("animation") &&
133 nodeProperty.modelNode().property("animation").isNodeProperty() &&
134 nodeProperty.modelNode().nodeProperty("animation").modelNode().type() == "Qt/NumberAnimation") {
135 m_ui->curve->setCurrentIndex(0);
136 ModelNode animation = nodeProperty.modelNode().nodeProperty("animation").modelNode();
137 if (animation.hasProperty("duration") && animation.property("duration").isVariantProperty())
138 m_ui->duration->setValue(animation.variantProperty("duration").value().toInt());
139 if (animation.hasProperty("easing") && animation.property("easing").isVariantProperty()) {
140 QStringList easingItems;
141 for (int i = 0; i < m_ui->curve->count(); i++)
142 easingItems.append(m_ui->curve->itemText(i));
143 m_ui->curve->setCurrentIndex(easingItems.indexOf(animation.variantProperty("easing").value().toString()));
151 void BehaviorDialog::accept()
154 if (m_modelNode.hasProperty(m_propertyName))
155 m_modelNode.removeProperty(m_propertyName);
156 if (m_ui->comboBox->currentIndex() == 0) {
157 RewriterTransaction transaction(m_modelNode.view()->beginRewriterTransaction());
158 ModelNode Behavior = m_modelNode.view()->createModelNode("Qt/Behavior", 4, 7);
159 m_modelNode.nodeProperty(m_propertyName).reparentHere(Behavior);
160 ModelNode animation = m_modelNode.view()->createModelNode("Qt/NumberAnimation", 4, 7);
161 animation.variantProperty("duration") = m_ui->duration->value();
162 animation.variantProperty("easing") = m_ui->curve->currentText();
163 Behavior.nodeProperty("animation").reparentHere(animation);
165 RewriterTransaction transaction(m_modelNode.view()->beginRewriterTransaction());
166 ModelNode springFollow = m_modelNode.view()->createModelNode("Qt/SpringFollow", 4, 7);
167 m_modelNode.nodeProperty(m_propertyName).reparentHere(springFollow);
168 springFollow.variantProperty("velocity") = m_ui->velocity->value();
169 springFollow.variantProperty("spring") = m_ui->spring->value();
170 springFollow.variantProperty("damping") = m_ui->damping->value();
171 springFollow.bindingProperty("source") = m_ui->source->text();
175 void BehaviorDialog::reject()