qdeclarativedesignview.cpp 16 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
30
31
32
33
34
35
36
37
38
39
40
#include "qdeclarativedesignview.h"
#include "qdeclarativedesigndebugserver.h"
#include "selectiontool.h"
#include "zoomtool.h"
#include "colorpickertool.h"
#include "layeritem.h"
#include "boundingrecthighlighter.h"
#include "subcomponenteditortool.h"
#include "qmltoolbar.h"

#include <QMouseEvent>
#include <QDeclarativeItem>
#include <QWidget>
#include <QGraphicsObject>
#include <QApplication>

#include <QAbstractAnimation>
#include <private/qabstractanimation_p.h>

namespace QmlViewer {

Q_GLOBAL_STATIC(QDeclarativeDesignDebugServer, qmlDesignDebugServer)

QDeclarativeDesignView::QDeclarativeDesignView(QWidget *parent) :
    QDeclarativeView(parent),
    m_designModeBehavior(false),
    m_executionPaused(false),
    m_slowdownFactor(1.0f),
    m_toolbar(0)
{
    m_manipulatorLayer = new LayerItem(scene());
    m_selectionTool = new SelectionTool(this);
    m_zoomTool = new ZoomTool(this);
    m_colorPickerTool = new ColorPickerTool(this);
    m_boundingRectHighlighter = new BoundingRectHighlighter(this);
    m_subcomponentEditorTool = new SubcomponentEditorTool(this);
    m_currentTool = m_selectionTool;

    setMouseTracking(true);

41
    connect(qmlDesignDebugServer(), SIGNAL(designModeBehaviorChanged(bool)), SLOT(setDesignModeBehavior(bool)));
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
    connect(qmlDesignDebugServer(), SIGNAL(reloadRequested()), SLOT(reloadView()));
    connect(qmlDesignDebugServer(),
            SIGNAL(currentObjectsChanged(QList<QObject*>)),
            SLOT(onCurrentObjectsChanged(QList<QObject*>)));
    connect(qmlDesignDebugServer(), SIGNAL(animationSpeedChangeRequested(qreal)), SLOT(changeAnimationSpeed(qreal)));
    connect(qmlDesignDebugServer(), SIGNAL(colorPickerToolRequested()), SLOT(changeToColorPickerTool()));
    connect(qmlDesignDebugServer(), SIGNAL(selectMarqueeToolRequested()), SLOT(changeToMarqueeSelectTool()));
    connect(qmlDesignDebugServer(), SIGNAL(selectToolRequested()), SLOT(changeToSingleSelectTool()));
    connect(qmlDesignDebugServer(), SIGNAL(zoomToolRequested()), SLOT(changeToZoomTool()));

    connect(this, SIGNAL(statusChanged(QDeclarativeView::Status)), SLOT(onStatusChanged(QDeclarativeView::Status)));

    connect(m_colorPickerTool, SIGNAL(selectedColorChanged(QColor)), SIGNAL(selectedColorChanged(QColor)));

    createToolbar();
}

QDeclarativeDesignView::~QDeclarativeDesignView()
{
}

void QDeclarativeDesignView::reloadView()
{
    m_subcomponentEditorTool->clear();
66
    clearHighlight();
67
68
69
    emit reloadRequested();
}

70
71
72
73
74
75
void QDeclarativeDesignView::leaveEvent(QEvent *event)
{
    if (!designModeBehavior()) {
        QDeclarativeView::leaveEvent(event);
        return;
    }
76
    clearHighlight();
77
78
}

79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
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
142
143
144
145
146
147
148
149
150
151
void QDeclarativeDesignView::mousePressEvent(QMouseEvent *event)
{
    if (!designModeBehavior()) {
        QDeclarativeView::mousePressEvent(event);
        return;
    }
    m_cursorPos = event->pos();
    m_currentTool->mousePressEvent(event);
}

void QDeclarativeDesignView::mouseMoveEvent(QMouseEvent *event)
{
    if (!designModeBehavior()) {
        QDeclarativeView::mouseMoveEvent(event);
        return;
    }
    m_cursorPos = event->pos();

    QList<QGraphicsItem*> selItems = selectableItems(event->pos());
    if (!selItems.isEmpty()) {
        setToolTip(AbstractFormEditorTool::titleForItem(selItems.first()));
    } else {
        setToolTip(QString());
    }
    if (event->buttons()) {
        m_subcomponentEditorTool->mouseMoveEvent(event);
        m_currentTool->mouseMoveEvent(event);
    } else {
        m_subcomponentEditorTool->hoverMoveEvent(event);
        m_currentTool->hoverMoveEvent(event);
    }
}

void QDeclarativeDesignView::mouseReleaseEvent(QMouseEvent *event)
{
    if (!designModeBehavior()) {
        QDeclarativeView::mouseReleaseEvent(event);
        return;
    }
    m_subcomponentEditorTool->mouseReleaseEvent(event);

    m_cursorPos = event->pos();
    m_currentTool->mouseReleaseEvent(event);

    if (event->buttons() & Qt::LeftButton) {
        qDebug() << "setting current objects";
        qmlDesignDebugServer()->setCurrentObjects(AbstractFormEditorTool::toObjectList(selectedItems()));
    }
}

void QDeclarativeDesignView::keyPressEvent(QKeyEvent *event)
{
    if (!designModeBehavior()) {
        QDeclarativeView::keyPressEvent(event);
        return;
    }
    m_currentTool->keyPressEvent(event);
}

void QDeclarativeDesignView::keyReleaseEvent(QKeyEvent *event)
{
    if (!designModeBehavior()) {
        QDeclarativeView::keyReleaseEvent(event);
        return;
    }

    switch(event->key()) {
    case Qt::Key_V:
        changeToSingleSelectTool();
        break;
    case Qt::Key_M:
        changeToMarqueeSelectTool();
        break;
152
153
154
155
156
157
    case Qt::Key_I:
        changeToColorPickerTool();
        break;
    case Qt::Key_Z:
        changeToZoomTool();
        break;
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
    case Qt::Key_Enter:
    case Qt::Key_Return:
        if (!selectedItems().isEmpty())
            m_subcomponentEditorTool->setCurrentItem(selectedItems().first());
        break;
    case Qt::Key_Space:
        if (m_executionPaused) {
            continueExecution(m_slowdownFactor);
        } else {
            pauseExecution();
        }
        break;
    default:
        break;
    }

    m_currentTool->keyReleaseEvent(event);
}

QGraphicsItem *QDeclarativeDesignView::currentRootItem() const
{
    return m_subcomponentEditorTool->currentRootItem();
}

void QDeclarativeDesignView::mouseDoubleClickEvent(QMouseEvent *event)
{
    if (!designModeBehavior()) {
        QDeclarativeView::mouseDoubleClickEvent(event);
        return;
    }
    QGraphicsItem *itemToEnter = 0;
189
190
191
    QList<QGraphicsItem*> itemList = items(event->pos());
    filterForSelection(itemList);

192
193
194
195
196
197
    if (selectedItems().isEmpty() && !itemList.isEmpty()) {
        itemToEnter = itemList.first();
    } else if (!selectedItems().isEmpty() && !itemList.isEmpty()) {
        itemToEnter = itemList.first();
    }

198
199
200
    if (itemToEnter)
        itemToEnter = m_subcomponentEditorTool->firstChildOfContext(itemToEnter);

201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
    m_subcomponentEditorTool->setCurrentItem(itemToEnter);
    m_subcomponentEditorTool->mouseDoubleClickEvent(event);

    if ((event->buttons() & Qt::LeftButton) && itemToEnter) {
        QGraphicsObject *objectToEnter = itemToEnter->toGraphicsObject();
        if (objectToEnter)
            qmlDesignDebugServer()->setCurrentObjects(QList<QObject*>() << objectToEnter);
    }

}
void QDeclarativeDesignView::wheelEvent(QWheelEvent *event)
{
    if (!m_designModeBehavior) {
        QDeclarativeView::wheelEvent(event);
        return;
    }
    m_currentTool->wheelEvent(event);
}

void QDeclarativeDesignView::setDesignModeBehavior(bool value)
{
222
223
224
    emit designModeBehaviorChanged(value);

    m_toolbar->setDesignModeBehavior(value);
225
    qmlDesignDebugServer()->setDesignModeBehavior(value);
226

227
228
229
    m_designModeBehavior = value;
    if (m_subcomponentEditorTool) {
        m_subcomponentEditorTool->clear();
230
        clearHighlight();
231
232
        setSelectedItems(QList<QGraphicsItem*>());

233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
        if (rootObject())
            m_subcomponentEditorTool->pushContext(rootObject());
    }
}

bool QDeclarativeDesignView::designModeBehavior() const
{
    return m_designModeBehavior;
}

void QDeclarativeDesignView::changeTool(Constants::DesignTool tool, Constants::ToolFlags /*flags*/)
{
    switch(tool) {
    case Constants::SelectionToolMode:
        changeToSingleSelectTool();
        break;
    case Constants::NoTool:
    default:
        m_currentTool = 0;
        break;
    }
}

void QDeclarativeDesignView::setSelectedItems(QList<QGraphicsItem *> items)
{
    m_currentSelection = items;
    m_currentTool->setItems(items);
}

QList<QGraphicsItem *> QDeclarativeDesignView::selectedItems() const
{
    return m_currentSelection;
}

AbstractFormEditorTool *QDeclarativeDesignView::currentTool() const
{
    return m_currentTool;
}

272
void QDeclarativeDesignView::clearHighlight()
273
274
275
276
{
    m_boundingRectHighlighter->clear();
}

277
void QDeclarativeDesignView::highlight(QGraphicsItem * item, ContextFlags flags)
278
{
279
    highlight(QList<QGraphicsItem*>() << item, flags);
280
281
}

282
void QDeclarativeDesignView::highlight(QList<QGraphicsItem *> items, ContextFlags flags)
283
284
{
    if (items.isEmpty())
285
286
        return;

287
288
    QList<QGraphicsObject*> objectList;
    foreach(QGraphicsItem *item, items) {
289
290
291
292
        QGraphicsItem *child = item;
        if (flags & ContextSensitive)
            child = m_subcomponentEditorTool->firstChildOfContext(item);

293
294
295
296
297
        if (child) {
            QGraphicsObject *childObject = child->toGraphicsObject();
            if (childObject)
                objectList << childObject;
        }
298
    }
299
300

    m_boundingRectHighlighter->highlight(objectList);
301
302
303
304
305
306
307
308
309
310
}

bool QDeclarativeDesignView::mouseInsideContextItem() const
{
    return m_subcomponentEditorTool->containsCursor(m_cursorPos.toPoint());
}

QList<QGraphicsItem*> QDeclarativeDesignView::selectableItems(const QPointF &scenePos) const
{
    QList<QGraphicsItem*> itemlist = scene()->items(scenePos);
311
    return filterForCurrentContext(itemlist);
312
313
314
315
316
}

QList<QGraphicsItem*> QDeclarativeDesignView::selectableItems(const QPoint &pos) const
{
    QList<QGraphicsItem*> itemlist = items(pos);
317
    return filterForCurrentContext(itemlist);
318
319
320
321
322
323
}

QList<QGraphicsItem*> QDeclarativeDesignView::selectableItems(const QRectF &sceneRect, Qt::ItemSelectionMode selectionMode) const
{
    QList<QGraphicsItem*> itemlist = scene()->items(sceneRect, selectionMode);

324
    return filterForCurrentContext(itemlist);
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
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
}

void QDeclarativeDesignView::changeToSingleSelectTool()
{
    //qDebug() << "changing to selection tool";

    m_currentToolMode = Constants::SelectionToolMode;
    m_selectionTool->setRubberbandSelectionMode(false);

    changeToSelectTool();

    emit selectToolActivated();
    qmlDesignDebugServer()->setCurrentTool(Constants::SelectionToolMode);
}

void QDeclarativeDesignView::changeToSelectTool()
{
    if (m_currentTool == m_selectionTool)
        return;

    m_currentTool->clear();
    m_currentTool = m_selectionTool;
    m_currentTool->clear();
    m_currentTool->setItems(m_currentSelection);
}

void QDeclarativeDesignView::changeToMarqueeSelectTool()
{
    changeToSelectTool();
    m_currentToolMode = Constants::MarqueeSelectionToolMode;
    m_selectionTool->setRubberbandSelectionMode(true);

    emit marqueeSelectToolActivated();
    qmlDesignDebugServer()->setCurrentTool(Constants::MarqueeSelectionToolMode);
}

void QDeclarativeDesignView::changeToZoomTool()
{
    m_currentToolMode = Constants::ZoomMode;
    m_currentTool->clear();
    m_currentTool = m_zoomTool;
    m_currentTool->clear();

    emit zoomToolActivated();
    qmlDesignDebugServer()->setCurrentTool(Constants::ZoomMode);
}

void QDeclarativeDesignView::changeToColorPickerTool()
{
    if (m_currentTool == m_colorPickerTool)
        return;

    m_currentToolMode = Constants::ColorPickerMode;
    m_currentTool->clear();
    m_currentTool = m_colorPickerTool;
    m_currentTool->clear();

    emit colorPickerActivated();
    qmlDesignDebugServer()->setCurrentTool(Constants::ColorPickerMode);
}

void QDeclarativeDesignView::changeAnimationSpeed(qreal slowdownFactor)
{
    m_slowdownFactor = slowdownFactor;

    if (m_slowdownFactor != 0) {
        continueExecution(m_slowdownFactor);
    } else {
        pauseExecution();
    }
}

void QDeclarativeDesignView::continueExecution(qreal slowdownFactor)
{
    Q_ASSERT(slowdownFactor > 0);

    m_slowdownFactor = slowdownFactor;
402
403
    static const qreal animSpeedSnapDelta = 0.01f;
    bool useStandardSpeed = (qAbs(1.0f - m_slowdownFactor) < animSpeedSnapDelta);
404
405
406

    QUnifiedTimer *timer = QUnifiedTimer::instance();
    timer->setSlowdownFactor(m_slowdownFactor);
407
    timer->setSlowModeEnabled(!useStandardSpeed);
408
409
410
411
412
413
414
415
416
    m_executionPaused = false;

    emit executionStarted(m_slowdownFactor);
    qmlDesignDebugServer()->setAnimationSpeed(m_slowdownFactor);
}

void QDeclarativeDesignView::pauseExecution()
{
    QUnifiedTimer *timer = QUnifiedTimer::instance();
417
    timer->setSlowdownFactor(0);
418
419
420
421
    timer->setSlowModeEnabled(true);
    m_executionPaused = true;

    emit executionPaused();
422
    qmlDesignDebugServer()->setAnimationSpeed(0);
423
424
425
426
427
428
429
430
431
432
433
434
}

void QDeclarativeDesignView::applyChangesFromClient()
{

}

LayerItem *QDeclarativeDesignView::manipulatorLayer() const
{
    return m_manipulatorLayer;
}

435
QList<QGraphicsItem*> QDeclarativeDesignView::filterForSelection(QList<QGraphicsItem*> &itemlist) const
436
437
{
    foreach(QGraphicsItem *item, itemlist) {
438
        if (isEditorItem(item) || !m_subcomponentEditorTool->isChildOfContext(item))
439
            itemlist.removeOne(item);
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
    }

    return itemlist;
}

QList<QGraphicsItem*> QDeclarativeDesignView::filterForCurrentContext(QList<QGraphicsItem*> &itemlist) const
{
    foreach(QGraphicsItem *item, itemlist) {

        if (isEditorItem(item) || !m_subcomponentEditorTool->isDirectChildOfContext(item)) {
            int index = itemlist.indexOf(item);

            // if we're a child, but not directly, replace with the parent that is directly in context.
            if (QGraphicsItem *contextParent = m_subcomponentEditorTool->firstChildOfContext(item)) {
                if (index >= 0) {
                    itemlist.replace(index, contextParent);
                } else {
                    itemlist.append(contextParent);
                }
            } else {
                itemlist.removeAt(index);
            }
462
463
464
465
466
467
        }
    }

    return itemlist;
}

468
469
470
471
472
473
bool QDeclarativeDesignView::isEditorItem(QGraphicsItem *item) const
{
    return (item->type() == Constants::EditorItemType
         || item->type() == Constants::ResizeHandleItemType
         || item->data(Constants::EditorItemDataKey).toBool());
}
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494

void QDeclarativeDesignView::onStatusChanged(QDeclarativeView::Status status)
{
    if (status == QDeclarativeView::Ready) {
        if (rootObject()) {
            m_subcomponentEditorTool->pushContext(rootObject());
            emit executionStarted(1.0f);
        }
    }
}

void QDeclarativeDesignView::onCurrentObjectsChanged(QList<QObject*> objects)
{
    QList<QGraphicsItem*> items;
    foreach(QObject *obj, objects) {
        QDeclarativeItem* declarativeItem = qobject_cast<QDeclarativeItem*>(obj);
        if (declarativeItem)
            items << declarativeItem;
    }

    setSelectedItems(items);
495
    clearHighlight();
496
    highlight(items, IgnoreContext);
497
498
499
500
501
502
503
504
505
506
507
508
}

QToolBar *QDeclarativeDesignView::toolbar() const
{
    return m_toolbar;
}

void QDeclarativeDesignView::createToolbar()
{
    m_toolbar = new QmlToolbar(this);
    connect(this, SIGNAL(selectedColorChanged(QColor)), m_toolbar, SLOT(setColorBoxColor(QColor)));

509
510
511
    connect(this, SIGNAL(designModeBehaviorChanged(bool)), m_toolbar, SLOT(setDesignModeBehavior(bool)));

    connect(m_toolbar, SIGNAL(designModeBehaviorChanged(bool)), this, SLOT(setDesignModeBehavior(bool)));
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
    connect(m_toolbar, SIGNAL(executionStarted()), this, SLOT(continueExecution()));
    connect(m_toolbar, SIGNAL(executionPaused()), this, SLOT(pauseExecution()));
    connect(m_toolbar, SIGNAL(colorPickerSelected()), this, SLOT(changeToColorPickerTool()));
    connect(m_toolbar, SIGNAL(zoomToolSelected()), this, SLOT(changeToZoomTool()));
    connect(m_toolbar, SIGNAL(selectToolSelected()), this, SLOT(changeToSingleSelectTool()));
    connect(m_toolbar, SIGNAL(marqueeSelectToolSelected()), this, SLOT(changeToMarqueeSelectTool()));

    connect(m_toolbar, SIGNAL(applyChangesFromQmlFileSelected()), SLOT(applyChangesFromClient()));

    connect(this, SIGNAL(executionStarted(qreal)), m_toolbar, SLOT(startExecution()));
    connect(this, SIGNAL(executionPaused()), m_toolbar, SLOT(pauseExecution()));

    connect(this, SIGNAL(selectToolActivated()), m_toolbar, SLOT(activateSelectTool()));

    // disabled features
    //connect(m_toolbar, SIGNAL(applyChangesToQmlFileSelected()), SLOT(applyChangesToClient()));
    //connect(this, SIGNAL(resizeToolActivated()), m_toolbar, SLOT(activateSelectTool()));
    //connect(this, SIGNAL(moveToolActivated()),   m_toolbar, SLOT(activateSelectTool()));

    connect(this, SIGNAL(colorPickerActivated()), m_toolbar, SLOT(activateColorPicker()));
    connect(this, SIGNAL(zoomToolActivated()), m_toolbar, SLOT(activateZoom()));
    connect(this, SIGNAL(marqueeSelectToolActivated()), m_toolbar, SLOT(activateMarqueeSelectTool()));
}

} //namespace QmlViewer