OSDN Git Service

It's 2011 now.
[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 (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 "behaviordialog.h"
35
36 #include <abstractview.h>
37 #include <nodeproperty.h>
38 #include <variantproperty.h>
39 #include <bindingproperty.h>
40 #include <nodeproperty.h>
41
42 #include <QLineEdit>
43 #include <QSpinBox>
44
45
46 namespace QmlDesigner {
47
48 void BehaviorDialog::registerDeclarativeType()
49 {
50     qmlRegisterType<QmlDesigner::BehaviorWidget>("Bauhaus",1,0,"BehaviorWidget");
51 }
52
53 BehaviorWidget::BehaviorWidget() : QPushButton(), m_BehaviorDialog(new BehaviorDialog(0))
54 {
55     setCheckable(true);
56     connect(this, SIGNAL(toggled(bool)), this, SLOT(buttonPressed(bool)));
57
58 }
59 BehaviorWidget::BehaviorWidget(QWidget *parent) : QPushButton(parent), m_BehaviorDialog(new BehaviorDialog(0))
60 {
61     setCheckable(true);
62     connect(this, SIGNAL(toggled(bool)), this, SLOT(buttonPressed(bool)));
63 }
64
65 PropertyEditorNodeWrapper* BehaviorWidget::complexNode() const
66 {
67     return m_complexNode;
68 }
69
70 void BehaviorWidget::setComplexNode(PropertyEditorNodeWrapper* complexNode)
71 {
72     m_complexNode = complexNode;
73     m_propertyName = complexNode->propertyName();
74     m_modelNode = complexNode->parentModelNode();
75
76     if (!modelNode().isValid()) {
77         m_BehaviorDialog->hide();
78     }
79
80     m_BehaviorDialog->setup(modelNode(), propertyName());
81 }
82
83 void BehaviorWidget::buttonPressed(bool show)
84 {
85     if (show) {
86         if (m_BehaviorDialog->isVisible()) {
87             m_BehaviorDialog->reject();
88         } else {
89             m_BehaviorDialog->setup(modelNode(), propertyName());
90             m_BehaviorDialog->show();
91             setChecked(false);
92         }
93     }
94 }
95
96 BehaviorDialog::BehaviorDialog(QWidget *parent) : QDialog(parent), m_ui(new Ui_BehaviorDialog)
97 {
98     m_ui->setupUi(this);
99     setModal(true);
100 }
101
102  void BehaviorDialog::setup(const ModelNode &node, const QString propertyName)
103 {
104         m_modelNode = node;
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);
111
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()));
144                             }
145                     }
146                 }
147             }
148         }
149 }
150
151 void BehaviorDialog::accept()
152 {
153     QDialog::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);
164     } else {
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();
172     }
173 }
174
175 void BehaviorDialog::reject()
176 {
177     QDialog::reject();
178 }
179
180 }