/
qquickcanvas.cpp
2009 lines (1638 loc) · 63.5 KB
/
qquickcanvas.cpp
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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
152
153
154
155
156
157
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/****************************************************************************
**
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/
**
** This file is part of the QtDeclarative module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** 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.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "qquickcanvas.h"
#include "qquickcanvas_p.h"
#include "qquickitem.h"
#include "qquickitem_p.h"
#include <QtQuick/private/qsgrenderer_p.h>
#include <QtQuick/private/qsgtexture_p.h>
#include <QtQuick/private/qsgflashnode_p.h>
#include <QtQuick/qsgengine.h>
#include <private/qquickwindowmanager_p.h>
#include <private/qguiapplication_p.h>
#include <QtGui/QInputPanel>
#include <private/qabstractanimation_p.h>
#include <QtGui/qpainter.h>
#include <QtGui/qevent.h>
#include <QtGui/qmatrix4x4.h>
#include <QtCore/qvarlengtharray.h>
#include <QtCore/qabstractanimation.h>
#include <QtDeclarative/qdeclarativeincubator.h>
#include <private/qdeclarativedebugtrace_p.h>
QT_BEGIN_NAMESPACE
void QQuickCanvasPrivate::updateFocusItemTransform()
{
Q_Q(QQuickCanvas);
QQuickItem *focus = q->activeFocusItem();
if (focus && qApp->focusObject() == focus)
qApp->inputPanel()->setInputItemTransform(QQuickItemPrivate::get(focus)->itemToCanvasTransform());
}
class QQuickCanvasIncubationController : public QObject, public QDeclarativeIncubationController
{
public:
QQuickCanvasIncubationController(QQuickCanvasPrivate *canvas)
: m_canvas(canvas), m_eventSent(false) {}
protected:
virtual bool event(QEvent *e)
{
if (e->type() == QEvent::User) {
Q_ASSERT(m_eventSent);
bool *amtp = m_canvas->windowManager->allowMainThreadProcessing();
while (incubatingObjectCount()) {
if (amtp)
incubateWhile(amtp);
else
incubateFor(5);
QCoreApplication::processEvents();
}
m_eventSent = false;
}
return QObject::event(e);
}
virtual void incubatingObjectCountChanged(int count)
{
if (count && !m_eventSent) {
m_eventSent = true;
QCoreApplication::postEvent(this, new QEvent(QEvent::User));
}
}
private:
QQuickCanvasPrivate *m_canvas;
bool m_eventSent;
};
QAccessibleInterface *QQuickCanvas::accessibleRoot() const
{
return QAccessible::queryAccessibleInterface(const_cast<QQuickCanvas*>(this));
}
/*
Focus behavior
==============
Prior to being added to a valid canvas items can set and clear focus with no
effect. Only once items are added to a canvas (by way of having a parent set that
already belongs to a canvas) do the focus rules apply. Focus goes back to
having no effect if an item is removed from a canvas.
When an item is moved into a new focus scope (either being added to a canvas
for the first time, or having its parent changed), if the focus scope already has
a scope focused item that takes precedence over the item being added. Otherwise,
the focus of the added tree is used. In the case of of a tree of items being
added to a canvas for the first time, which may have a conflicted focus state (two
or more items in one scope having focus set), the same rule is applied item by item -
thus the first item that has focus will get it (assuming the scope doesn't already
have a scope focused item), and the other items will have their focus cleared.
*/
// #define FOCUS_DEBUG
// #define MOUSE_DEBUG
// #define TOUCH_DEBUG
// #define DIRTY_DEBUG
QQuickItem::UpdatePaintNodeData::UpdatePaintNodeData()
: transformNode(0)
{
}
QQuickRootItem::QQuickRootItem()
{
}
void QQuickCanvas::exposeEvent(QExposeEvent *)
{
Q_D(QQuickCanvas);
d->windowManager->paint(this);
}
void QQuickCanvas::resizeEvent(QResizeEvent *)
{
Q_D(QQuickCanvas);
d->windowManager->resize(this, size());
}
void QQuickCanvas::showEvent(QShowEvent *)
{
d_func()->windowManager->show(this);
}
void QQuickCanvas::hideEvent(QHideEvent *)
{
d_func()->windowManager->hide(this);
}
void QQuickCanvas::focusOutEvent(QFocusEvent *)
{
Q_D(QQuickCanvas);
d->rootItem->setFocus(false);
}
void QQuickCanvas::focusInEvent(QFocusEvent *)
{
Q_D(QQuickCanvas);
d->rootItem->setFocus(true);
d->updateFocusItemTransform();
}
void QQuickCanvasPrivate::polishItems()
{
while (!itemsToPolish.isEmpty()) {
QSet<QQuickItem *>::Iterator iter = itemsToPolish.begin();
QQuickItem *item = *iter;
itemsToPolish.erase(iter);
QQuickItemPrivate::get(item)->polishScheduled = false;
item->updatePolish();
}
updateFocusItemTransform();
}
void forceUpdate(QQuickItem *item)
{
if (item->flags() & QQuickItem::ItemHasContents)
item->update();
QQuickItemPrivate::get(item)->dirty(QQuickItemPrivate::ChildrenUpdateMask);
QList <QQuickItem *> items = item->childItems();
for (int i=0; i<items.size(); ++i)
forceUpdate(items.at(i));
}
void QQuickCanvasPrivate::syncSceneGraph()
{
if (!renderer) {
forceUpdate(rootItem);
QSGRootNode *rootNode = new QSGRootNode;
rootNode->appendChildNode(QQuickItemPrivate::get(rootItem)->itemNode());
renderer = context->createRenderer();
renderer->setRootNode(rootNode);
}
updateDirtyNodes();
// Copy the current state of clearing from canvas into renderer.
renderer->setClearColor(clearColor);
QSGRenderer::ClearMode mode = QSGRenderer::ClearStencilBuffer | QSGRenderer::ClearDepthBuffer;
if (clearBeforeRendering)
mode |= QSGRenderer::ClearColorBuffer;
renderer->setClearMode(mode);
}
void QQuickCanvasPrivate::renderSceneGraph(const QSize &size)
{
Q_Q(QQuickCanvas);
renderer->setDeviceRect(QRect(QPoint(0, 0), size));
renderer->setViewportRect(QRect(QPoint(0, 0), renderTarget ? renderTarget->size() : size));
renderer->setProjectionMatrixToDeviceRect();
emit q->beforeRendering();
context->renderNextFrame(renderer, renderTarget);
emit q->afterRendering();
}
QQuickCanvasPrivate::QQuickCanvasPrivate()
: rootItem(0)
, activeFocusItem(0)
, mouseGrabberItem(0)
, dirtyItemList(0)
, context(0)
, renderer(0)
, windowManager(0)
, clearColor(Qt::white)
, clearBeforeRendering(true)
, renderTarget(0)
, incubationController(0)
{
}
QQuickCanvasPrivate::~QQuickCanvasPrivate()
{
}
void QQuickCanvasPrivate::init(QQuickCanvas *c)
{
q_ptr = c;
Q_Q(QQuickCanvas);
rootItem = new QQuickRootItem;
QQuickItemPrivate *rootItemPrivate = QQuickItemPrivate::get(rootItem);
rootItemPrivate->canvas = q;
rootItemPrivate->flags |= QQuickItem::ItemIsFocusScope;
// In the absence of a focus in event on some platforms assume the window will
// be activated immediately and set focus on the rootItem
// ### Remove when QTBUG-22415 is resolved.
//It is important that this call happens after the rootItem has a canvas..
rootItem->setFocus(true);
windowManager = QQuickWindowManager::instance();
context = windowManager->sceneGraphContext();
q->setSurfaceType(QWindow::OpenGLSurface);
q->setFormat(context->defaultSurfaceFormat());
QObject::connect(context, SIGNAL(initialized()), q, SIGNAL(sceneGraphInitialized()), Qt::DirectConnection);
QObject::connect(context, SIGNAL(invalidated()), q, SIGNAL(sceneGraphInvalidated()), Qt::DirectConnection);
QObject::connect(context, SIGNAL(invalidated()), q, SLOT(cleanupSceneGraph()), Qt::DirectConnection);
// ### TODO: remove QSGEngine
engine = new QSGEngine();
engine->setCanvas(q);
}
QDeclarativeListProperty<QObject> QQuickCanvasPrivate::data()
{
initRootItem();
return QQuickItemPrivate::get(rootItem)->data();
}
void QQuickCanvasPrivate::initRootItem()
{
Q_Q(QQuickCanvas);
q->connect(q, SIGNAL(widthChanged(int)),
rootItem, SLOT(setWidth(int)));
q->connect(q, SIGNAL(heightChanged(int)),
rootItem, SLOT(setHeight(int)));
rootItem->setWidth(q->width());
rootItem->setHeight(q->height());
}
void QQuickCanvasPrivate::transformTouchPoints(QList<QTouchEvent::TouchPoint> &touchPoints, const QTransform &transform)
{
for (int i=0; i<touchPoints.count(); i++) {
QTouchEvent::TouchPoint &touchPoint = touchPoints[i];
touchPoint.setRect(transform.mapRect(touchPoint.sceneRect()));
touchPoint.setStartPos(transform.map(touchPoint.startScenePos()));
touchPoint.setLastPos(transform.map(touchPoint.lastScenePos()));
}
}
/*!
Translates the data in \a touchEvent to this canvas. This method leaves the item local positions in
\a touchEvent untouched (these are filled in later).
*/
void QQuickCanvasPrivate::translateTouchEvent(QTouchEvent *touchEvent)
{
// Q_Q(QQuickCanvas);
// touchEvent->setWidget(q); // ### refactor...
QList<QTouchEvent::TouchPoint> touchPoints = touchEvent->touchPoints();
for (int i = 0; i < touchPoints.count(); ++i) {
QTouchEvent::TouchPoint &touchPoint = touchPoints[i];
touchPoint.setScreenRect(touchPoint.sceneRect());
touchPoint.setStartScreenPos(touchPoint.startScenePos());
touchPoint.setLastScreenPos(touchPoint.lastScenePos());
touchPoint.setSceneRect(touchPoint.rect());
touchPoint.setStartScenePos(touchPoint.startPos());
touchPoint.setLastScenePos(touchPoint.lastPos());
if (i == 0)
lastMousePosition = touchPoint.pos().toPoint();
}
touchEvent->setTouchPoints(touchPoints);
}
void QQuickCanvasPrivate::setFocusInScope(QQuickItem *scope, QQuickItem *item, FocusOptions options)
{
Q_Q(QQuickCanvas);
Q_ASSERT(item);
Q_ASSERT(scope || item == rootItem);
#ifdef FOCUS_DEBUG
qWarning() << "QQuickCanvasPrivate::setFocusInScope():";
qWarning() << " scope:" << (QObject *)scope;
if (scope)
qWarning() << " scopeSubFocusItem:" << (QObject *)QQuickItemPrivate::get(scope)->subFocusItem;
qWarning() << " item:" << (QObject *)item;
qWarning() << " activeFocusItem:" << (QObject *)activeFocusItem;
#endif
QQuickItemPrivate *scopePrivate = scope ? QQuickItemPrivate::get(scope) : 0;
QQuickItemPrivate *itemPrivate = QQuickItemPrivate::get(item);
QQuickItem *oldActiveFocusItem = 0;
QQuickItem *newActiveFocusItem = 0;
QVarLengthArray<QQuickItem *, 20> changed;
// Does this change the active focus?
if (item == rootItem || (scopePrivate->activeFocus && item->isEnabled())) {
oldActiveFocusItem = activeFocusItem;
newActiveFocusItem = item;
while (newActiveFocusItem->isFocusScope()
&& newActiveFocusItem->scopedFocusItem()
&& newActiveFocusItem->scopedFocusItem()->isEnabled()) {
newActiveFocusItem = newActiveFocusItem->scopedFocusItem();
}
if (oldActiveFocusItem) {
#ifndef QT_NO_IM
qApp->inputPanel()->reset();
#endif
activeFocusItem = 0;
QFocusEvent event(QEvent::FocusOut, Qt::OtherFocusReason);
q->sendEvent(oldActiveFocusItem, &event);
QQuickItem *afi = oldActiveFocusItem;
while (afi != scope) {
if (QQuickItemPrivate::get(afi)->activeFocus) {
QQuickItemPrivate::get(afi)->activeFocus = false;
changed << afi;
}
afi = afi->parentItem();
}
}
}
if (item != rootItem && !(options & DontChangeSubFocusItem)) {
QQuickItem *oldSubFocusItem = scopePrivate->subFocusItem;
// Correct focus chain in scope
if (oldSubFocusItem) {
QQuickItem *sfi = scopePrivate->subFocusItem->parentItem();
while (sfi != scope) {
QQuickItemPrivate::get(sfi)->subFocusItem = 0;
sfi = sfi->parentItem();
}
}
{
scopePrivate->subFocusItem = item;
QQuickItem *sfi = scopePrivate->subFocusItem->parentItem();
while (sfi != scope) {
QQuickItemPrivate::get(sfi)->subFocusItem = item;
sfi = sfi->parentItem();
}
}
if (oldSubFocusItem) {
QQuickItemPrivate::get(oldSubFocusItem)->focus = false;
changed << oldSubFocusItem;
}
}
if (!(options & DontChangeFocusProperty)) {
// if (item != rootItem || QGuiApplication::focusWindow() == q) { // QTBUG-22415
itemPrivate->focus = true;
changed << item;
// }
}
if (newActiveFocusItem && rootItem->hasFocus()) {
activeFocusItem = newActiveFocusItem;
QQuickItemPrivate::get(newActiveFocusItem)->activeFocus = true;
changed << newActiveFocusItem;
QQuickItem *afi = newActiveFocusItem->parentItem();
while (afi && afi != scope) {
if (afi->isFocusScope()) {
QQuickItemPrivate::get(afi)->activeFocus = true;
changed << afi;
}
afi = afi->parentItem();
}
QFocusEvent event(QEvent::FocusIn, Qt::OtherFocusReason);
q->sendEvent(newActiveFocusItem, &event);
}
emit q->focusObjectChanged(activeFocusItem);
if (!changed.isEmpty())
notifyFocusChangesRecur(changed.data(), changed.count() - 1);
}
void QQuickCanvasPrivate::clearFocusInScope(QQuickItem *scope, QQuickItem *item, FocusOptions options)
{
Q_Q(QQuickCanvas);
Q_UNUSED(item);
Q_ASSERT(item);
Q_ASSERT(scope || item == rootItem);
#ifdef FOCUS_DEBUG
qWarning() << "QQuickCanvasPrivate::clearFocusInScope():";
qWarning() << " scope:" << (QObject *)scope;
qWarning() << " item:" << (QObject *)item;
qWarning() << " activeFocusItem:" << (QObject *)activeFocusItem;
#endif
QQuickItemPrivate *scopePrivate = scope ? QQuickItemPrivate::get(scope) : 0;
QQuickItem *oldActiveFocusItem = 0;
QQuickItem *newActiveFocusItem = 0;
QVarLengthArray<QQuickItem *, 20> changed;
Q_ASSERT(item == rootItem || item == scopePrivate->subFocusItem);
// Does this change the active focus?
if (item == rootItem || scopePrivate->activeFocus) {
oldActiveFocusItem = activeFocusItem;
newActiveFocusItem = scope;
Q_ASSERT(oldActiveFocusItem);
#ifndef QT_NO_IM
qApp->inputPanel()->reset();
#endif
activeFocusItem = 0;
QFocusEvent event(QEvent::FocusOut, Qt::OtherFocusReason);
q->sendEvent(oldActiveFocusItem, &event);
QQuickItem *afi = oldActiveFocusItem;
while (afi != scope) {
if (QQuickItemPrivate::get(afi)->activeFocus) {
QQuickItemPrivate::get(afi)->activeFocus = false;
changed << afi;
}
afi = afi->parentItem();
}
}
if (item != rootItem && !(options & DontChangeSubFocusItem)) {
QQuickItem *oldSubFocusItem = scopePrivate->subFocusItem;
// Correct focus chain in scope
if (oldSubFocusItem) {
QQuickItem *sfi = scopePrivate->subFocusItem->parentItem();
while (sfi != scope) {
QQuickItemPrivate::get(sfi)->subFocusItem = 0;
sfi = sfi->parentItem();
}
}
scopePrivate->subFocusItem = 0;
if (oldSubFocusItem && !(options & DontChangeFocusProperty)) {
QQuickItemPrivate::get(oldSubFocusItem)->focus = false;
changed << oldSubFocusItem;
}
} else if (!(options & DontChangeFocusProperty)) {
QQuickItemPrivate::get(item)->focus = false;
changed << item;
}
if (newActiveFocusItem) {
Q_ASSERT(newActiveFocusItem == scope);
activeFocusItem = scope;
QFocusEvent event(QEvent::FocusIn, Qt::OtherFocusReason);
q->sendEvent(newActiveFocusItem, &event);
}
emit q->focusObjectChanged(activeFocusItem);
if (!changed.isEmpty())
notifyFocusChangesRecur(changed.data(), changed.count() - 1);
}
void QQuickCanvasPrivate::notifyFocusChangesRecur(QQuickItem **items, int remaining)
{
QDeclarativeGuard<QQuickItem> item(*items);
if (remaining)
notifyFocusChangesRecur(items + 1, remaining - 1);
if (item) {
QQuickItemPrivate *itemPrivate = QQuickItemPrivate::get(item);
if (itemPrivate->notifiedFocus != itemPrivate->focus) {
itemPrivate->notifiedFocus = itemPrivate->focus;
emit item->focusChanged(itemPrivate->focus);
}
if (item && itemPrivate->notifiedActiveFocus != itemPrivate->activeFocus) {
itemPrivate->notifiedActiveFocus = itemPrivate->activeFocus;
itemPrivate->itemChange(QQuickItem::ItemActiveFocusHasChanged, itemPrivate->activeFocus);
emit item->activeFocusChanged(itemPrivate->activeFocus);
}
}
}
void QQuickCanvasPrivate::dirtyItem(QQuickItem *)
{
Q_Q(QQuickCanvas);
q->maybeUpdate();
}
void QQuickCanvasPrivate::cleanup(QSGNode *n)
{
Q_Q(QQuickCanvas);
Q_ASSERT(!cleanupNodeList.contains(n));
cleanupNodeList.append(n);
q->maybeUpdate();
}
/*!
\qmlclass Window QQuickCanvas
\inqmlmodule QtQuick.Window 2
\brief The Window object creates a new top-level window.
The Window object creates a new top-level window for a QtQuick scene. It automatically sets up the
window for use with QtQuick 2.0 graphical elements.
*/
/*!
\class QQuickCanvas
\since QtQuick 2.0
\brief The QQuickCanvas class provides the canvas for displaying a graphical QML scene
QQuickCanvas provides the graphical scene management needed to interact with and display
a scene of QQuickItems.
A QQuickCanvas always has a single invisible root item. To add items to this canvas,
reparent the items to the root item or to an existing item in the scene.
For easily displaying a scene from a QML file, see \l{QQuickView}.
*/
QQuickCanvas::QQuickCanvas(QWindow *parent)
: QWindow(*(new QQuickCanvasPrivate), parent)
{
Q_D(QQuickCanvas);
d->init(this);
}
QQuickCanvas::QQuickCanvas(QQuickCanvasPrivate &dd, QWindow *parent)
: QWindow(dd, parent)
{
Q_D(QQuickCanvas);
d->init(this);
}
QQuickCanvas::~QQuickCanvas()
{
Q_D(QQuickCanvas);
d->windowManager->canvasDestroyed(this);
// ### should we change ~QQuickItem to handle this better?
// manually cleanup for the root item (item destructor only handles these when an item is parented)
QQuickItemPrivate *rootItemPrivate = QQuickItemPrivate::get(d->rootItem);
rootItemPrivate->removeFromDirtyList();
delete d->incubationController; d->incubationController = 0;
delete d->rootItem; d->rootItem = 0;
}
/*!
Returns the invisible root item of the scene.
A QQuickCanvas always has a single invisible root item. To add items to this canvas,
reparent the items to the root item or to an existing item in the scene.
*/
QQuickItem *QQuickCanvas::rootItem() const
{
Q_D(const QQuickCanvas);
return d->rootItem;
}
/*!
Returns the item which currently has active focus.
*/
QQuickItem *QQuickCanvas::activeFocusItem() const
{
Q_D(const QQuickCanvas);
return d->activeFocusItem;
}
QObject *QQuickCanvas::focusObject() const
{
Q_D(const QQuickCanvas);
if (d->activeFocusItem)
return d->activeFocusItem;
return const_cast<QQuickCanvas*>(this);
}
/*!
Returns the item which currently has the mouse grab.
*/
QQuickItem *QQuickCanvas::mouseGrabberItem() const
{
Q_D(const QQuickCanvas);
return d->mouseGrabberItem;
}
/*!
\qmlproperty color QtQuick2.Window::Window::color
The background color for the window.
Setting this property is more efficient than using a separate Rectangle.
*/
bool QQuickCanvasPrivate::clearHover()
{
if (hoverItems.isEmpty())
return false;
QPointF pos = QCursor::pos(); // ### refactor: q->mapFromGlobal(QCursor::pos());
bool accepted = false;
foreach (QQuickItem* item, hoverItems)
accepted = sendHoverEvent(QEvent::HoverLeave, item, pos, pos, QGuiApplication::keyboardModifiers(), true) || accepted;
hoverItems.clear();
return accepted;
}
bool QQuickCanvas::event(QEvent *e)
{
Q_D(QQuickCanvas);
switch (e->type()) {
case QEvent::TouchBegin:
case QEvent::TouchUpdate:
case QEvent::TouchEnd:
{
QTouchEvent *touch = static_cast<QTouchEvent *>(e);
d->translateTouchEvent(touch);
d->deliverTouchEvent(touch);
return touch->isAccepted();
}
case QEvent::Leave:
d->clearHover();
d->lastMousePosition = QPoint();
break;
case QEvent::DragEnter:
case QEvent::DragLeave:
case QEvent::DragMove:
case QEvent::Drop:
d->deliverDragEvent(&d->dragGrabber, e);
break;
case QEvent::WindowDeactivate:
rootItem()->windowDeactivateEvent();
break;
default:
break;
}
return QWindow::event(e);
}
void QQuickCanvas::keyPressEvent(QKeyEvent *e)
{
Q_D(QQuickCanvas);
if (d->activeFocusItem)
sendEvent(d->activeFocusItem, e);
}
void QQuickCanvas::keyReleaseEvent(QKeyEvent *e)
{
Q_D(QQuickCanvas);
if (d->activeFocusItem)
sendEvent(d->activeFocusItem, e);
}
bool QQuickCanvasPrivate::deliverInitialMousePressEvent(QQuickItem *item, QMouseEvent *event)
{
Q_Q(QQuickCanvas);
QQuickItemPrivate *itemPrivate = QQuickItemPrivate::get(item);
if (itemPrivate->opacity == 0.0)
return false;
if (itemPrivate->flags & QQuickItem::ItemClipsChildrenToShape) {
QPointF p = item->mapFromScene(event->windowPos());
if (!QRectF(0, 0, item->width(), item->height()).contains(p))
return false;
}
QList<QQuickItem *> children = itemPrivate->paintOrderChildItems();
for (int ii = children.count() - 1; ii >= 0; --ii) {
QQuickItem *child = children.at(ii);
if (!child->isVisible() || !child->isEnabled())
continue;
if (deliverInitialMousePressEvent(child, event))
return true;
}
if (itemPrivate->acceptedMouseButtons & event->button()) {
QPointF p = item->mapFromScene(event->windowPos());
if (QRectF(0, 0, item->width(), item->height()).contains(p)) {
QMouseEvent me(event->type(), p, event->windowPos(), event->screenPos(),
event->button(), event->buttons(), event->modifiers());
me.accept();
mouseGrabberItem = item;
q->sendEvent(item, &me);
event->setAccepted(me.isAccepted());
if (me.isAccepted())
return true;
mouseGrabberItem->ungrabMouse();
mouseGrabberItem = 0;
}
}
return false;
}
bool QQuickCanvasPrivate::deliverMouseEvent(QMouseEvent *event)
{
Q_Q(QQuickCanvas);
lastMousePosition = event->windowPos();
if (!mouseGrabberItem &&
event->type() == QEvent::MouseButtonPress &&
(event->button() & event->buttons()) == event->buttons()) {
return deliverInitialMousePressEvent(rootItem, event);
}
if (mouseGrabberItem) {
QQuickItemPrivate *mgPrivate = QQuickItemPrivate::get(mouseGrabberItem);
const QTransform &transform = mgPrivate->canvasToItemTransform();
QMouseEvent me(event->type(), transform.map(event->windowPos()), event->windowPos(), event->screenPos(),
event->button(), event->buttons(), event->modifiers());
me.accept();
q->sendEvent(mouseGrabberItem, &me);
event->setAccepted(me.isAccepted());
if (me.isAccepted())
return true;
}
return false;
}
void QQuickCanvas::mousePressEvent(QMouseEvent *event)
{
Q_D(QQuickCanvas);
#ifdef MOUSE_DEBUG
qWarning() << "QQuickCanvas::mousePressEvent()" << event->pos() << event->button() << event->buttons();
#endif
d->deliverMouseEvent(event);
}
void QQuickCanvas::mouseReleaseEvent(QMouseEvent *event)
{
Q_D(QQuickCanvas);
#ifdef MOUSE_DEBUG
qWarning() << "QQuickCanvas::mouseReleaseEvent()" << event->pos() << event->button() << event->buttons();
#endif
if (!d->mouseGrabberItem) {
QWindow::mouseReleaseEvent(event);
return;
}
d->deliverMouseEvent(event);
d->mouseGrabberItem = 0;
}
void QQuickCanvas::mouseDoubleClickEvent(QMouseEvent *event)
{
Q_D(QQuickCanvas);
#ifdef MOUSE_DEBUG
qWarning() << "QQuickCanvas::mouseDoubleClickEvent()" << event->pos() << event->button() << event->buttons();
#endif
if (!d->mouseGrabberItem && (event->button() & event->buttons()) == event->buttons()) {
if (d->deliverInitialMousePressEvent(d->rootItem, event))
event->accept();
else
event->ignore();
return;
}
d->deliverMouseEvent(event);
}
bool QQuickCanvasPrivate::sendHoverEvent(QEvent::Type type, QQuickItem *item,
const QPointF &scenePos, const QPointF &lastScenePos,
Qt::KeyboardModifiers modifiers, bool accepted)
{
Q_Q(QQuickCanvas);
const QTransform transform = QQuickItemPrivate::get(item)->canvasToItemTransform();
//create copy of event
QHoverEvent hoverEvent(type, transform.map(scenePos), transform.map(lastScenePos), modifiers);
hoverEvent.setAccepted(accepted);
q->sendEvent(item, &hoverEvent);
return hoverEvent.isAccepted();
}
void QQuickCanvas::mouseMoveEvent(QMouseEvent *event)
{
Q_D(QQuickCanvas);
#ifdef MOUSE_DEBUG
qWarning() << "QQuickCanvas::mouseMoveEvent()" << event->pos() << event->button() << event->buttons();
#endif
if (!d->mouseGrabberItem) {
if (d->lastMousePosition.isNull())
d->lastMousePosition = event->windowPos();
QPointF last = d->lastMousePosition;
d->lastMousePosition = event->windowPos();
bool accepted = event->isAccepted();
bool delivered = d->deliverHoverEvent(d->rootItem, event->windowPos(), last, event->modifiers(), accepted);
if (!delivered) {
//take care of any exits
accepted = d->clearHover();
}
event->setAccepted(accepted);
return;
}
d->deliverMouseEvent(event);
}
bool QQuickCanvasPrivate::deliverHoverEvent(QQuickItem *item, const QPointF &scenePos, const QPointF &lastScenePos,
Qt::KeyboardModifiers modifiers, bool &accepted)
{
QQuickItemPrivate *itemPrivate = QQuickItemPrivate::get(item);
if (itemPrivate->opacity == 0.0)
return false;
if (itemPrivate->flags & QQuickItem::ItemClipsChildrenToShape) {
QPointF p = item->mapFromScene(scenePos);
if (!QRectF(0, 0, item->width(), item->height()).contains(p))
return false;
}
QList<QQuickItem *> children = itemPrivate->paintOrderChildItems();
for (int ii = children.count() - 1; ii >= 0; --ii) {
QQuickItem *child = children.at(ii);
if (!child->isVisible() || !child->isEnabled())
continue;
if (deliverHoverEvent(child, scenePos, lastScenePos, modifiers, accepted))
return true;
}
if (itemPrivate->hoverEnabled) {
QPointF p = item->mapFromScene(scenePos);
if (QRectF(0, 0, item->width(), item->height()).contains(p)) {
if (!hoverItems.isEmpty() && hoverItems[0] == item) {
//move
accepted = sendHoverEvent(QEvent::HoverMove, item, scenePos, lastScenePos, modifiers, accepted);
} else {
QList<QQuickItem *> itemsToHover;
QQuickItem* parent = item;
itemsToHover << item;
while ((parent = parent->parentItem()))
itemsToHover << parent;
// Leaving from previous hovered items until we reach the item or one of its ancestors.
while (!hoverItems.isEmpty() && !itemsToHover.contains(hoverItems[0])) {
sendHoverEvent(QEvent::HoverLeave, hoverItems[0], scenePos, lastScenePos, modifiers, accepted);
hoverItems.removeFirst();
}
if (!hoverItems.isEmpty() && hoverItems[0] == item){//Not entering a new Item
// ### Shouldn't we send moves for the parent items as well?
accepted = sendHoverEvent(QEvent::HoverMove, item, scenePos, lastScenePos, modifiers, accepted);
} else {
// Enter items that are not entered yet.
int startIdx = -1;
if (!hoverItems.isEmpty())
startIdx = itemsToHover.indexOf(hoverItems[0]) - 1;
if (startIdx == -1)
startIdx = itemsToHover.count() - 1;
for (int i = startIdx; i >= 0; i--) {
QQuickItem *itemToHover = itemsToHover[i];
if (QQuickItemPrivate::get(itemToHover)->hoverEnabled) {
hoverItems.prepend(itemToHover);
sendHoverEvent(QEvent::HoverEnter, itemToHover, scenePos, lastScenePos, modifiers, accepted);
}
}
}
}
return true;
}
}
return false;
}
bool QQuickCanvasPrivate::deliverWheelEvent(QQuickItem *item, QWheelEvent *event)
{
Q_Q(QQuickCanvas);
QQuickItemPrivate *itemPrivate = QQuickItemPrivate::get(item);
if (itemPrivate->opacity == 0.0)
return false;
if (itemPrivate->flags & QQuickItem::ItemClipsChildrenToShape) {
QPointF p = item->mapFromScene(event->posF());
if (!QRectF(0, 0, item->width(), item->height()).contains(p))
return false;
}