OSDN Git Service

Update license.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / qmldesigner / components / propertyeditor / behaviordialog.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 (info@qt.nokia.com)
8 **
9 **
10 ** GNU Lesser General Public License Usage
11 **
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.
18 **
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.
22 **
23 ** Other Usage
24 **
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.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **************************************************************************/
32
33 #include "behaviordialog.h"
34
35 #include <abstractview.h>
36 #include <nodeproperty.h>
37 #include <variantproperty.h>
38 #include <bindingproperty.h>
39 #include <nodeproperty.h>
40
41 #include <QLineEdit>
42 #include <QSpinBox>
43
44
45 namespace QmlDesigner {
46
47 void BehaviorDialog::registerDeclarativeType()
48 {
49     qmlRegisterType<QmlDesigner::BehaviorWidget>("Bauhaus",1,0,"BehaviorWidget");
50 }
51
52 BehaviorWidget::BehaviorWidget() : QPushButton(), m_BehaviorDialog(new BehaviorDialog(0))
53 {
54     setCheckable(true);
55     connect(this, SIGNAL(toggled(bool)), this, SLOT(buttonPressed(bool)));
56
57 }
58 BehaviorWidget::BehaviorWidget(QWidget *parent) : QPushButton(parent), m_BehaviorDialog(new BehaviorDialog(0))
59 {
60     setCheckable(true);
61     connect(this, SIGNAL(toggled(bool)), this, SLOT(buttonPressed(bool)));
62 }
63
64 PropertyEditorNodeWrapper* BehaviorWidget::complexNode() const
65 {
66     return m_complexNode;
67 }
68
69 void BehaviorWidget::setComplexNode(PropertyEditorNodeWrapper* complexNode)
70 {
71     m_complexNode = complexNode;
72     m_propertyName = complexNode->propertyName();
73     m_modelNode = complexNode->parentModelNode();
74
75     if (!modelNode().isValid()) {
76         m_BehaviorDialog->hide();
77     }
78
79     m_BehaviorDialog->setup(modelNode(), propertyName());
80 }
81
82 void BehaviorWidget::buttonPressed(bool show)
83 {
84     if (show) {
85         if (m_BehaviorDialog->isVisible()) {
86             m_BehaviorDialog->reject();
87         } else {
88             m_BehaviorDialog->setup(modelNode(), propertyName());
89             m_BehaviorDialog->show();
90             setChecked(false);
91         }
92     }
93 }
94
95 BehaviorDialog::BehaviorDialog(QWidget *parent) : QDialog(parent), m_ui(new Ui_BehaviorDialog)
96 {
97     m_ui->setupUi(this);
98     setModal(true);
99 }
100
101  void BehaviorDialog::setup(const ModelNode &node, const QString propertyName)
102 {
103         m_modelNode = node;
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);
110
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()));
143                             }
144                     }
145                 }
146             }
147         }
148 }
149
150 void BehaviorDialog::accept()
151 {
152     QDialog::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);
163     } else {
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();
171     }
172 }
173
174 void BehaviorDialog::reject()
175 {
176     QDialog::reject();
177 }
178
179 }