OSDN Git Service

It's 2011 now.
[qt-creator-jp/qt-creator-jp.git] / share / qtcreator / qml / qmljsdebugger / editor / qmltoolbar.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 <QLabel>
35 #include <QIcon>
36 #include <QAction>
37 #include <QMenu>
38
39 #include "qmltoolbar.h"
40 #include "toolbarcolorbox.h"
41
42 #include <QDebug>
43
44 namespace QmlJSDebugger {
45
46 QmlToolbar::QmlToolbar(QWidget *parent)
47     : QToolBar(parent)
48     , m_emitSignals(true)
49     , m_isRunning(false)
50     , m_animationSpeed(1.0f)
51     , m_previousAnimationSpeed(0.0f)
52     , ui(new Ui)
53 {
54     ui->playIcon = QIcon(QLatin1String(":/qml/images/play-24.png"));
55     ui->pauseIcon = QIcon(QLatin1String(":/qml/images/pause-24.png"));
56
57     ui->designmode = new QAction(QIcon(QLatin1String(":/qml/images/observermode-24.png")),
58                                  tr("Observer Mode"), this);
59     ui->play = new QAction(ui->pauseIcon, tr("Play/Pause Animations"), this);
60     ui->select = new QAction(QIcon(QLatin1String(":/qml/images/select-24.png")), tr("Select"), this);
61     ui->selectMarquee = new QAction(QIcon(QLatin1String(":/qml/images/select-marquee-24.png")),
62                                     tr("Select (Marquee)"), this);
63     ui->zoom = new QAction(QIcon(QLatin1String(":/qml/images/zoom-24.png")), tr("Zoom"), this);
64     ui->colorPicker = new QAction(QIcon(QLatin1String(":/qml/images/color-picker-24.png")),
65                                   tr("Color Picker"), this);
66     ui->toQml = new QAction(QIcon(QLatin1String(":/qml/images/to-qml-24.png")),
67                             tr("Apply Changes to QML Viewer"), this);
68     ui->fromQml = new QAction(QIcon(QLatin1String(":/qml/images/from-qml-24.png")),
69                               tr("Apply Changes to Document"), this);
70     ui->designmode->setCheckable(true);
71     ui->designmode->setChecked(false);
72
73     ui->play->setCheckable(false);
74     ui->select->setCheckable(true);
75     ui->selectMarquee->setCheckable(true);
76     ui->zoom->setCheckable(true);
77     ui->colorPicker->setCheckable(true);
78
79     setWindowTitle(tr("Tools"));
80
81     addAction(ui->designmode);
82     addAction(ui->play);
83     addSeparator();
84
85     addAction(ui->select);
86     // disabled because multi selection does not do anything useful without design mode
87     //addAction(ui->selectMarquee);
88     addSeparator();
89     addAction(ui->zoom);
90     addAction(ui->colorPicker);
91     //addAction(ui->fromQml);
92
93     ui->colorBox = new ToolBarColorBox(this);
94     ui->colorBox->setMinimumSize(24, 24);
95     ui->colorBox->setMaximumSize(28, 28);
96     ui->colorBox->setColor(Qt::black);
97     addWidget(ui->colorBox);
98
99     setWindowFlags(Qt::Tool);
100
101     QMenu *playSpeedMenu = new QMenu(this);
102     QActionGroup *playSpeedMenuActions = new QActionGroup(this);
103     playSpeedMenuActions->setExclusive(true);
104     playSpeedMenu->addAction(tr("Animation Speed"));
105     playSpeedMenu->addSeparator();
106     ui->defaultAnimSpeedAction = playSpeedMenu->addAction(tr("1x"), this,
107                                                           SLOT(changeToDefaultAnimSpeed()));
108     ui->defaultAnimSpeedAction->setCheckable(true);
109     ui->defaultAnimSpeedAction->setChecked(true);
110     playSpeedMenuActions->addAction(ui->defaultAnimSpeedAction);
111
112     ui->halfAnimSpeedAction = playSpeedMenu->addAction(tr("0.5x"), this,
113                                                        SLOT(changeToHalfAnimSpeed()));
114     ui->halfAnimSpeedAction->setCheckable(true);
115     playSpeedMenuActions->addAction(ui->halfAnimSpeedAction);
116
117     ui->fourthAnimSpeedAction = playSpeedMenu->addAction(tr("0.25x"), this,
118                                                          SLOT(changeToFourthAnimSpeed()));
119     ui->fourthAnimSpeedAction->setCheckable(true);
120     playSpeedMenuActions->addAction(ui->fourthAnimSpeedAction);
121
122     ui->eighthAnimSpeedAction = playSpeedMenu->addAction(tr("0.125x"), this,
123                                                          SLOT(changeToEighthAnimSpeed()));
124     ui->eighthAnimSpeedAction->setCheckable(true);
125     playSpeedMenuActions->addAction(ui->eighthAnimSpeedAction);
126
127     ui->tenthAnimSpeedAction = playSpeedMenu->addAction(tr("0.1x"), this,
128                                                         SLOT(changeToTenthAnimSpeed()));
129     ui->tenthAnimSpeedAction->setCheckable(true);
130     playSpeedMenuActions->addAction(ui->tenthAnimSpeedAction);
131
132     ui->menuPauseAction = playSpeedMenu->addAction(tr("Pause"), this, SLOT(updatePauseAction()));
133     ui->menuPauseAction->setCheckable(true);
134     ui->menuPauseAction->setIcon(ui->pauseIcon);
135     playSpeedMenuActions->addAction(ui->menuPauseAction);
136     ui->play->setMenu(playSpeedMenu);
137
138     connect(ui->designmode, SIGNAL(toggled(bool)), SLOT(setDesignModeBehaviorOnClick(bool)));
139
140     connect(ui->colorPicker, SIGNAL(triggered()), SLOT(activateColorPickerOnClick()));
141
142     connect(ui->play, SIGNAL(triggered()), SLOT(activatePlayOnClick()));
143
144     connect(ui->zoom, SIGNAL(triggered()), SLOT(activateZoomOnClick()));
145     connect(ui->colorPicker, SIGNAL(triggered()), SLOT(activateColorPickerOnClick()));
146     connect(ui->select, SIGNAL(triggered()), SLOT(activateSelectToolOnClick()));
147     connect(ui->selectMarquee, SIGNAL(triggered()), SLOT(activateMarqueeSelectToolOnClick()));
148
149     connect(ui->toQml, SIGNAL(triggered()), SLOT(activateToQml()));
150     connect(ui->fromQml, SIGNAL(triggered()), SLOT(activateFromQml()));
151 }
152
153 QmlToolbar::~QmlToolbar()
154 {
155     delete ui;
156 }
157
158 void QmlToolbar::activateColorPicker()
159 {
160     m_emitSignals = false;
161     activateColorPickerOnClick();
162     m_emitSignals = true;
163 }
164
165 void QmlToolbar::activateSelectTool()
166 {
167     m_emitSignals = false;
168     activateSelectToolOnClick();
169     m_emitSignals = true;
170 }
171
172 void QmlToolbar::activateMarqueeSelectTool()
173 {
174     m_emitSignals = false;
175     activateMarqueeSelectToolOnClick();
176     m_emitSignals = true;
177 }
178
179 void QmlToolbar::activateZoom()
180 {
181     m_emitSignals = false;
182     activateZoomOnClick();
183     m_emitSignals = true;
184 }
185
186 void QmlToolbar::setAnimationSpeed(qreal slowdownFactor)
187 {
188     m_emitSignals = false;
189     if (slowdownFactor != 0) {
190         m_animationSpeed = slowdownFactor;
191
192         if (slowdownFactor == 1.0f) {
193             ui->defaultAnimSpeedAction->setChecked(true);
194         } else if (slowdownFactor == 2.0f) {
195             ui->halfAnimSpeedAction->setChecked(true);
196         } else if (slowdownFactor == 4.0f) {
197             ui->fourthAnimSpeedAction->setChecked(true);
198         } else if (slowdownFactor == 8.0f) {
199             ui->eighthAnimSpeedAction->setChecked(true);
200         } else if (slowdownFactor == 10.0f) {
201             ui->tenthAnimSpeedAction->setChecked(true);
202         }
203         updatePlayAction();
204     } else {
205         ui->menuPauseAction->setChecked(true);
206         updatePauseAction();
207     }
208
209     m_emitSignals = true;
210 }
211
212 void QmlToolbar::changeToDefaultAnimSpeed()
213 {
214     m_animationSpeed = 1.0f;
215     updatePlayAction();
216 }
217
218 void QmlToolbar::changeToHalfAnimSpeed()
219 {
220     m_animationSpeed = 2.0f;
221     updatePlayAction();
222 }
223
224 void QmlToolbar::changeToFourthAnimSpeed()
225 {
226     m_animationSpeed = 4.0f;
227     updatePlayAction();
228 }
229
230 void QmlToolbar::changeToEighthAnimSpeed()
231 {
232     m_animationSpeed = 8.0f;
233     updatePlayAction();
234 }
235
236 void QmlToolbar::changeToTenthAnimSpeed()
237 {
238     m_animationSpeed = 10.0f;
239     updatePlayAction();
240 }
241
242
243 void QmlToolbar::setDesignModeBehavior(bool inDesignMode)
244 {
245     m_emitSignals = false;
246     ui->designmode->setChecked(inDesignMode);
247     setDesignModeBehaviorOnClick(inDesignMode);
248     m_emitSignals = true;
249 }
250
251 void QmlToolbar::setDesignModeBehaviorOnClick(bool checked)
252 {
253     ui->play->setEnabled(checked);
254     ui->select->setEnabled(checked);
255     ui->selectMarquee->setEnabled(checked);
256     ui->zoom->setEnabled(checked);
257     ui->colorPicker->setEnabled(checked);
258     ui->toQml->setEnabled(checked);
259     ui->fromQml->setEnabled(checked);
260
261     if (m_emitSignals)
262         emit designModeBehaviorChanged(checked);
263 }
264
265 void QmlToolbar::setColorBoxColor(const QColor &color)
266 {
267     ui->colorBox->setColor(color);
268 }
269
270 void QmlToolbar::activatePlayOnClick()
271 {
272     if (m_isRunning) {
273         updatePauseAction();
274     } else {
275         updatePlayAction();
276     }
277 }
278
279 void QmlToolbar::updatePlayAction()
280 {
281     m_isRunning = true;
282     ui->play->setIcon(ui->pauseIcon);
283     if (m_animationSpeed != m_previousAnimationSpeed)
284         m_previousAnimationSpeed = m_animationSpeed;
285
286     if (m_emitSignals)
287         emit animationSpeedChanged(m_animationSpeed);
288 }
289
290 void QmlToolbar::updatePauseAction()
291 {
292     m_isRunning = false;
293     ui->play->setIcon(ui->playIcon);
294     if (m_emitSignals)
295         emit animationSpeedChanged(0.0f);
296 }
297
298 void QmlToolbar::activateColorPickerOnClick()
299 {
300     ui->zoom->setChecked(false);
301     ui->select->setChecked(false);
302     ui->selectMarquee->setChecked(false);
303
304     ui->colorPicker->setChecked(true);
305     if (m_activeTool != Constants::ColorPickerMode) {
306         m_activeTool = Constants::ColorPickerMode;
307         if (m_emitSignals)
308             emit colorPickerSelected();
309     }
310 }
311
312 void QmlToolbar::activateSelectToolOnClick()
313 {
314     ui->zoom->setChecked(false);
315     ui->selectMarquee->setChecked(false);
316     ui->colorPicker->setChecked(false);
317
318     ui->select->setChecked(true);
319     if (m_activeTool != Constants::SelectionToolMode) {
320         m_activeTool = Constants::SelectionToolMode;
321         if (m_emitSignals)
322             emit selectToolSelected();
323     }
324 }
325
326 void QmlToolbar::activateMarqueeSelectToolOnClick()
327 {
328     ui->zoom->setChecked(false);
329     ui->select->setChecked(false);
330     ui->colorPicker->setChecked(false);
331
332     ui->selectMarquee->setChecked(true);
333     if (m_activeTool != Constants::MarqueeSelectionToolMode) {
334         m_activeTool = Constants::MarqueeSelectionToolMode;
335         if (m_emitSignals)
336             emit marqueeSelectToolSelected();
337     }
338 }
339
340 void QmlToolbar::activateZoomOnClick()
341 {
342     ui->select->setChecked(false);
343     ui->selectMarquee->setChecked(false);
344     ui->colorPicker->setChecked(false);
345
346     ui->zoom->setChecked(true);
347     if (m_activeTool != Constants::ZoomMode) {
348         m_activeTool = Constants::ZoomMode;
349         if (m_emitSignals)
350             emit zoomToolSelected();
351     }
352 }
353
354 void QmlToolbar::activateFromQml()
355 {
356     if (m_emitSignals)
357         emit applyChangesFromQmlFileSelected();
358 }
359
360 void QmlToolbar::activateToQml()
361 {
362     if (m_emitSignals)
363         emit applyChangesToQmlFileSelected();
364 }
365
366 }