qmlinspectortoolbar.cpp 14.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/

30
31
#include "qmlinspectortoolbar.h"
#include "qmljsinspectorconstants.h"
32
#include "qmljstoolbarcolorbox.h"
33
34
35
36
37
38
39

#include <extensionsystem/pluginmanager.h>
#include <coreplugin/icore.h>
#include <projectexplorer/projectexplorerconstants.h>
#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/actionmanager/command.h>

40
41
42
#include <utils/styledbar.h>
#include <utils/filterlineedit.h>

43
44
45
#include <QHBoxLayout>
#include <QAction>
#include <QToolButton>
46
47
#include <QMenu>
#include <QActionGroup>
48
49
50
51
52
53
54
55
56
57
58
59
60

namespace QmlJSInspector {
namespace Internal {

static QToolButton *createToolButton(QAction *action)
{
    QToolButton *button = new QToolButton;
    button->setDefaultAction(action);
    return button;
}

QmlInspectorToolbar::QmlInspectorToolbar(QObject *parent) :
    QObject(parent),
61
    m_observerModeAction(0),
62
//    m_reloadAction(0),
63
64
65
66
67
68
    m_playAction(0),
    m_selectAction(0),
    m_zoomAction(0),
    m_colorPickerAction(0),
    m_toQmlAction(0),
    m_fromQmlAction(0),
69
70
71
72
73
    m_defaultAnimSpeedAction(0),
    m_halfAnimSpeedAction(0),
    m_fourthAnimSpeedAction(0),
    m_eighthAnimSpeedAction(0),
    m_tenthAnimSpeedAction(0),
74
    m_menuPauseAction(0),
75
76
    m_playIcon(QIcon(QLatin1String(":/qml/images/play-small.png"))),
    m_pauseIcon(QIcon(QLatin1String(":/qml/images/pause-small.png"))),
77
    m_colorBox(0),
78
79
    m_emitSignals(true),
    m_isRunning(false),
80
81
    m_animationSpeed(1.0f),
    m_previousAnimationSpeed(0.0f),
82
83
    m_activeTool(NoTool),
    m_barWidget(0)
84
85
86
87
88
89
{

}

void QmlInspectorToolbar::setEnabled(bool value)
{
90
    m_observerModeAction->setEnabled(value);
91
92
93
    //m_toQmlAction->setEnabled(value);
    m_fromQmlAction->setEnabled(value);

94
//    m_reloadAction->setEnabled(value);
95
96
97
98
    m_playAction->setEnabled(value);
    m_selectAction->setEnabled(value);
    m_zoomAction->setEnabled(value);
    m_colorPickerAction->setEnabled(value);
99
    //m_toQmlAction->setEnabled(value);
100
    m_fromQmlAction->setEnabled(value);
101
    m_colorBox->setEnabled(value);
102
103
104
105
106
}

void QmlInspectorToolbar::enable()
{
    setEnabled(true);
107
    m_emitSignals = false;
108
    m_observerModeAction->setChecked(false);
109
    setAnimationSpeed(1.0f);
110
111
    activateDesignModeOnClick();
    m_emitSignals = true;
112
113
114
115
}

void QmlInspectorToolbar::disable()
{
116
    setAnimationSpeed(1.0f);
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
    activateSelectTool();
    setEnabled(false);
}

void QmlInspectorToolbar::activateColorPicker()
{
    m_emitSignals = false;
    activateColorPickerOnClick();
    m_emitSignals = true;
}

void QmlInspectorToolbar::activateSelectTool()
{
    m_emitSignals = false;
    activateSelectToolOnClick();
    m_emitSignals = true;
}

void QmlInspectorToolbar::activateZoomTool()
{
    m_emitSignals = false;
    activateZoomOnClick();
    m_emitSignals = true;
}

142
void QmlInspectorToolbar::setAnimationSpeed(qreal slowdownFactor)
143
144
{
    m_emitSignals = false;
145
    if (slowdownFactor != 0) {
146
147
148
149
150
151
152
153
154
155
156
157
158
        m_animationSpeed = slowdownFactor;

        if (slowdownFactor == 1.0f) {
            m_defaultAnimSpeedAction->setChecked(true);
        } else if (slowdownFactor == 2.0f) {
            m_halfAnimSpeedAction->setChecked(true);
        } else if (slowdownFactor == 4.0f) {
            m_fourthAnimSpeedAction->setChecked(true);
        } else if (slowdownFactor == 8.0f) {
            m_eighthAnimSpeedAction->setChecked(true);
        } else if (slowdownFactor == 10.0f) {
            m_tenthAnimSpeedAction->setChecked(true);
        }
159
160
        updatePlayAction();
    } else {
161
        m_menuPauseAction->setChecked(true);
162
        updatePauseAction();
163
    }
164

165
166
167
    m_emitSignals = true;
}

168
169
170
void QmlInspectorToolbar::setDesignModeBehavior(bool inDesignMode)
{
    m_emitSignals = false;
171
    m_observerModeAction->setChecked(inDesignMode);
172
173
174
175
    activateDesignModeOnClick();
    m_emitSignals = true;
}

176
177
178
179
180
void QmlInspectorToolbar::createActions(const Core::Context &context)
{
    Core::ICore *core = Core::ICore::instance();
    Core::ActionManager *am = core->actionManager();

181
    m_fromQmlAction = new QAction(QIcon(QLatin1String(":/qml/images/from-qml-small.png")), tr("Apply Changes on Save"), this);
182
//    m_reloadAction = new QAction(QIcon(QLatin1String(":/qml/images/reload.png")), tr("Reload QML"), this);
183

184
    m_observerModeAction = new QAction(QIcon(QLatin1String(":/qml/images/observermode.png")), tr("Observer Mode"), this);
185
    m_playAction = new QAction(m_pauseIcon, tr("Play/Pause Animations"), this);
186
187
188
189
    m_selectAction = new QAction(QIcon(QLatin1String(":/qml/images/select-small.png")), tr("Select"), this);
    m_zoomAction = new QAction(QIcon(QLatin1String(":/qml/images/zoom-small.png")), tr("Zoom"), this);
    m_colorPickerAction = new QAction(QIcon(QLatin1String(":/qml/images/color-picker-small.png")), tr("Color Picker"), this);
    m_toQmlAction = new QAction(QIcon(QLatin1String(":/qml/images/to-qml-small.png")), tr("Live Preview Changes in QML Viewer"), this);
190

191
192
    m_observerModeAction->setCheckable(true);
    m_observerModeAction->setChecked(false);
193
194
195
196
    m_selectAction->setCheckable(true);
    m_zoomAction->setCheckable(true);
    m_colorPickerAction->setCheckable(true);

197
198
199
    m_fromQmlAction->setCheckable(true);
    m_fromQmlAction->setChecked(true);

200
    am->registerAction(m_observerModeAction, QmlJSInspector::Constants::DESIGNMODE_ACTION, context);
201
//    am->registerAction(m_reloadAction, QmlJSInspector::Constants::RELOAD_ACTION, context);
202
203
204
205
206
207
208
    am->registerAction(m_playAction, QmlJSInspector::Constants::PLAY_ACTION, context);
    am->registerAction(m_selectAction, QmlJSInspector::Constants::SELECT_ACTION, context);
    am->registerAction(m_zoomAction, QmlJSInspector::Constants::ZOOM_ACTION, context);
    am->registerAction(m_colorPickerAction, QmlJSInspector::Constants::COLOR_PICKER_ACTION, context);
    am->registerAction(m_toQmlAction, QmlJSInspector::Constants::TO_QML_ACTION, context);
    am->registerAction(m_fromQmlAction, QmlJSInspector::Constants::FROM_QML_ACTION, context);

209
210
211
    m_barWidget = new Utils::StyledBar;
    m_barWidget->setSingleRow(true);
    m_barWidget->setProperty("topBorder", true);
212

213
    QHBoxLayout *configBarLayout = new QHBoxLayout(m_barWidget);
214
215
216
    configBarLayout->setMargin(0);
    configBarLayout->setSpacing(5);

217
    QMenu *playSpeedMenu = new QMenu(m_barWidget);
218
219
    QActionGroup *playSpeedMenuActions = new QActionGroup(this);
    playSpeedMenuActions->setExclusive(true);
220
221
    playSpeedMenu->addAction(tr("Animation Speed"));
    playSpeedMenu->addSeparator();
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
    m_defaultAnimSpeedAction = playSpeedMenu->addAction(tr("1x"), this, SLOT(changeToDefaultAnimSpeed()));
    m_defaultAnimSpeedAction->setCheckable(true);
    m_defaultAnimSpeedAction->setChecked(true);
    playSpeedMenuActions->addAction(m_defaultAnimSpeedAction);

    m_halfAnimSpeedAction = playSpeedMenu->addAction(tr("0.5x"), this, SLOT(changeToHalfAnimSpeed()));
    m_halfAnimSpeedAction->setCheckable(true);
    playSpeedMenuActions->addAction(m_halfAnimSpeedAction);

    m_fourthAnimSpeedAction = playSpeedMenu->addAction(tr("0.25x"), this, SLOT(changeToFourthAnimSpeed()));
    m_fourthAnimSpeedAction->setCheckable(true);
    playSpeedMenuActions->addAction(m_fourthAnimSpeedAction);

    m_eighthAnimSpeedAction = playSpeedMenu->addAction(tr("0.125x"), this, SLOT(changeToEighthAnimSpeed()));
    m_eighthAnimSpeedAction->setCheckable(true);
    playSpeedMenuActions->addAction(m_eighthAnimSpeedAction);

    m_tenthAnimSpeedAction = playSpeedMenu->addAction(tr("0.1x"), this, SLOT(changeToTenthAnimSpeed()));
    m_tenthAnimSpeedAction->setCheckable(true);
    playSpeedMenuActions->addAction(m_tenthAnimSpeedAction);

243
    m_menuPauseAction = playSpeedMenu->addAction(tr("Pause"), this, SLOT(updatePauseAction()));
244
    m_menuPauseAction->setCheckable(true);
245
    m_menuPauseAction->setIcon(m_pauseIcon);
246
247
    playSpeedMenuActions->addAction(m_menuPauseAction);

248
249
//    configBarLayout->addWidget(createToolButton(am->command(ProjectExplorer::Constants::DEBUG)->action()));
//    configBarLayout->addWidget(createToolButton(am->command(ProjectExplorer::Constants::STOP)->action()));
250
    configBarLayout->addWidget(createToolButton(am->command(QmlJSInspector::Constants::FROM_QML_ACTION)->action()));
251
//    configBarLayout->addWidget(createToolButton(am->command(QmlJSInspector::Constants::RELOAD_ACTION)->action()));
252
    configBarLayout->addSpacing(10);
253

254
    configBarLayout->addWidget(createToolButton(am->command(QmlJSInspector::Constants::DESIGNMODE_ACTION)->action()));
255
256
257
258
    m_playButton = createToolButton(am->command(QmlJSInspector::Constants::PLAY_ACTION)->action());
    m_playButton->setMenu(playSpeedMenu);
    configBarLayout->addWidget(m_playButton);
    //configBarLayout->addWidget(createToolButton(am->command(QmlJSInspector::Constants::PAUSE_ACTION)->action()));
259
260
261
262
263
264

    configBarLayout->addWidget(createToolButton(am->command(QmlJSInspector::Constants::SELECT_ACTION)->action()));

    configBarLayout->addWidget(createToolButton(am->command(QmlJSInspector::Constants::ZOOM_ACTION)->action()));
    configBarLayout->addWidget(createToolButton(am->command(QmlJSInspector::Constants::COLOR_PICKER_ACTION)->action()));

265
    m_colorBox = new ToolBarColorBox(m_barWidget);
266
267
268
269
270
    m_colorBox->setMinimumSize(20, 20);
    m_colorBox->setMaximumSize(20, 20);
    m_colorBox->setInnerBorderColor(QColor(192,192,192));
    m_colorBox->setOuterBorderColor(QColor(58,58,58));
    configBarLayout->addWidget(m_colorBox);
271
    //configBarLayout->addWidget(createToolButton(am->command(QmlJSInspector::Constants::TO_QML_ACTION)->action()));
272

273
    //m_filterLineEdit = new Utils::FilterLineEdit(m_barWidget);
274

275
    configBarLayout->addStretch();
276
    //configBarLayout->addWidget(m_filterLineEdit);
277
278
279

    setEnabled(false);

280
    connect(m_observerModeAction, SIGNAL(triggered()), SLOT(activateDesignModeOnClick()));
281
//    connect(m_reloadAction, SIGNAL(triggered()), SIGNAL(reloadSelected()));
Lasse Holmstedt's avatar
Lasse Holmstedt committed
282

283
284
285
286
287
288
289
290
    connect(m_colorPickerAction, SIGNAL(triggered()), SLOT(activateColorPickerOnClick()));

    connect(m_playAction, SIGNAL(triggered()), SLOT(activatePlayOnClick()));

    connect(m_zoomAction, SIGNAL(triggered()), SLOT(activateZoomOnClick()));
    connect(m_colorPickerAction, SIGNAL(triggered()), SLOT(activateColorPickerOnClick()));
    connect(m_selectAction, SIGNAL(triggered()), SLOT(activateSelectToolOnClick()));

291
    //connect(m_toQmlAction, SIGNAL(triggered()), SLOT(activateToQml()));
292
293
294
    connect(m_fromQmlAction, SIGNAL(triggered()), SLOT(activateFromQml()));
}

295
296
297
298
299
QWidget *QmlInspectorToolbar::widget() const
{
    return m_barWidget;
}

300
301
302
void QmlInspectorToolbar::changeToDefaultAnimSpeed()
{
    m_animationSpeed = 1.0f;
303
    updatePlayAction();
304
305
306
307
308
}

void QmlInspectorToolbar::changeToHalfAnimSpeed()
{
    m_animationSpeed = 2.0f;
309
    updatePlayAction();
310
311
312
313
314
}

void QmlInspectorToolbar::changeToFourthAnimSpeed()
{
    m_animationSpeed = 4.0f;
315
    updatePlayAction();
316
317
318
319
320
}

void QmlInspectorToolbar::changeToEighthAnimSpeed()
{
    m_animationSpeed = 8.0f;
321
    updatePlayAction();
322
323
324
325
326
}

void QmlInspectorToolbar::changeToTenthAnimSpeed()
{
    m_animationSpeed = 10.0f;
327
    updatePlayAction();
328
329
}

330
331
void QmlInspectorToolbar::activateDesignModeOnClick()
{
332
    bool checked = m_observerModeAction->isChecked();
333

334
//    m_reloadAction->setEnabled(true);
335
336
337
338
339
340
341
342
343
    m_playAction->setEnabled(checked);
    m_selectAction->setEnabled(checked);
    m_zoomAction->setEnabled(checked);
    m_colorPickerAction->setEnabled(checked);

    if (m_emitSignals)
        emit designModeSelected(checked);
}

344
345
void QmlInspectorToolbar::activatePlayOnClick()
{
346
347
348
349
    if (m_isRunning) {
        updatePauseAction();
    } else {
        updatePlayAction();
350
351
352
    }
}

353
void QmlInspectorToolbar::updatePlayAction()
354
{
355
    m_isRunning = true;
356
    m_playAction->setIcon(m_pauseIcon);
357
358
359
360
361
362
363
364
365
366
367
368
    if (m_animationSpeed != m_previousAnimationSpeed)
        m_previousAnimationSpeed = m_animationSpeed;

    if (m_emitSignals)
        emit animationSpeedChanged(m_animationSpeed);

    m_playButton->setDefaultAction(m_playAction);
}

void QmlInspectorToolbar::updatePauseAction()
{
    m_isRunning = false;
369
    m_playAction->setIcon(m_playIcon);
370
371
372
373
    if (m_emitSignals)
        emit animationSpeedChanged(0.0f);

    m_playButton->setDefaultAction(m_playAction);
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
}

void QmlInspectorToolbar::activateColorPickerOnClick()
{
    m_zoomAction->setChecked(false);
    m_selectAction->setChecked(false);

    m_colorPickerAction->setChecked(true);
    if (m_activeTool != ColorPickerMode) {
        m_activeTool = ColorPickerMode;
        if (m_emitSignals)
            emit colorPickerSelected();
    }
}

void QmlInspectorToolbar::activateSelectToolOnClick()
{
    m_zoomAction->setChecked(false);
    m_colorPickerAction->setChecked(false);

    m_selectAction->setChecked(true);
    if (m_activeTool != SelectionToolMode) {
        m_activeTool = SelectionToolMode;
        if (m_emitSignals)
            emit selectToolSelected();
    }
}

void QmlInspectorToolbar::activateZoomOnClick()
{
    m_selectAction->setChecked(false);
    m_colorPickerAction->setChecked(false);

    m_zoomAction->setChecked(true);
    if (m_activeTool != ZoomMode) {
        m_activeTool = ZoomMode;
        if (m_emitSignals)
            emit zoomToolSelected();
    }
}

415
416
417
418
419
void QmlInspectorToolbar::setLivePreviewChecked(bool value)
{
    m_fromQmlAction->setChecked(value);
}

420
421
422
423
424
void QmlInspectorToolbar::setSelectedColor(const QColor &color)
{
    m_colorBox->setColor(color);
}

425
426
427
void QmlInspectorToolbar::activateFromQml()
{
    if (m_emitSignals)
428
        emit applyChangesFromQmlFileTriggered(m_fromQmlAction->isChecked());
429
430
431
432
433
434
435
436
437
438
}

void QmlInspectorToolbar::activateToQml()
{
    if (m_emitSignals)
        emit applyChangesToQmlFileSelected();
}

} // namespace Internal
} // namespace QmlJSInspector