/
qquickwindow.cpp
4330 lines (3545 loc) · 149 KB
/
qquickwindow.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) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing/
**
** This file is part of the QtQuick module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL21$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see http://www.qt.io/terms-conditions. For further
** information use the contact form at http://www.qt.io/contact-us.
**
** 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 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** As a special exception, The Qt Company gives you certain additional
** rights. These rights are described in The Qt Company LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "qquickwindow.h"
#include "qquickwindow_p.h"
#include "qquickwindowattached_p.h"
#include "qquickitem.h"
#include "qquickitem_p.h"
#include "qquickevents_p_p.h"
#include <private/qquickdrag_p.h>
#include <QtQuick/private/qsgrenderer_p.h>
#include <QtQuick/private/qsgtexture_p.h>
#include <private/qsgrenderloop_p.h>
#include <private/qquickrendercontrol_p.h>
#include <private/qquickanimatorcontroller_p.h>
#include <private/qsystrace_p.h>
#include <private/qguiapplication_p.h>
#include <QtGui/QInputMethod>
#include <private/qabstractanimation_p.h>
#include <QtGui/qpainter.h>
#include <QtGui/qevent.h>
#include <QtGui/qmatrix4x4.h>
#include <QtGui/qstylehints.h>
#include <QtCore/qvarlengtharray.h>
#include <QtCore/qabstractanimation.h>
#include <QtCore/QLibraryInfo>
#include <QtCore/QRunnable>
#include <QtQml/qqmlincubator.h>
#include <QtQuick/private/qquickpixmapcache_p.h>
#include <private/qqmlmemoryprofiler_p.h>
#include <private/qopenglvertexarrayobject_p.h>
QT_BEGIN_NAMESPACE
Q_LOGGING_CATEGORY(DBG_TOUCH, "qt.quick.touch");
Q_LOGGING_CATEGORY(DBG_TOUCH_TARGET, "qt.quick.touch.target");
Q_LOGGING_CATEGORY(DBG_MOUSE, "qt.quick.mouse");
Q_LOGGING_CATEGORY(DBG_FOCUS, "qt.quick.focus");
Q_LOGGING_CATEGORY(DBG_DIRTY, "qt.quick.dirty");
extern Q_GUI_EXPORT QImage qt_gl_read_framebuffer(const QSize &size, bool alpha_format, bool include_alpha);
bool QQuickWindowPrivate::defaultAlphaBuffer = false;
void QQuickWindowPrivate::updateFocusItemTransform()
{
#ifndef QT_NO_IM
Q_Q(QQuickWindow);
QQuickItem *focus = q->activeFocusItem();
if (focus && QGuiApplication::focusObject() == focus) {
QQuickItemPrivate *focusPrivate = QQuickItemPrivate::get(focus);
QGuiApplication::inputMethod()->setInputItemTransform(focusPrivate->itemToWindowTransform());
QGuiApplication::inputMethod()->setInputItemRectangle(QRectF(0, 0, focusPrivate->width, focusPrivate->height));
}
#endif
}
class QQuickWindowIncubationController : public QObject, public QQmlIncubationController
{
Q_OBJECT
public:
QQuickWindowIncubationController(QSGRenderLoop *loop)
: m_renderLoop(loop), m_timer(0)
{
// Allow incubation for 1/3 of a frame.
m_incubation_time = qMax(1, int(1000 / QGuiApplication::primaryScreen()->refreshRate()) / 3);
QAnimationDriver *animationDriver = m_renderLoop->animationDriver();
if (animationDriver) {
connect(animationDriver, SIGNAL(stopped()), this, SLOT(animationStopped()));
connect(m_renderLoop, SIGNAL(timeToIncubate()), this, SLOT(incubate()));
}
}
protected:
void timerEvent(QTimerEvent *) Q_DECL_OVERRIDE
{
killTimer(m_timer);
m_timer = 0;
incubate();
}
void incubateAgain() {
if (m_timer == 0) {
// Wait for a while before processing the next batch. Using a
// timer to avoid starvation of system events.
m_timer = startTimer(m_incubation_time);
}
}
public slots:
void incubate() {
if (incubatingObjectCount()) {
if (m_renderLoop->interleaveIncubation()) {
incubateFor(m_incubation_time);
} else {
incubateFor(m_incubation_time * 2);
if (incubatingObjectCount())
incubateAgain();
}
}
}
void animationStopped() { incubate(); }
protected:
void incubatingObjectCountChanged(int count) Q_DECL_OVERRIDE
{
if (count && !m_renderLoop->interleaveIncubation())
incubateAgain();
}
private:
QSGRenderLoop *m_renderLoop;
int m_incubation_time;
int m_timer;
};
#include "qquickwindow.moc"
#ifndef QT_NO_ACCESSIBILITY
/*!
Returns an accessibility interface for this window, or 0 if such an
interface cannot be created.
*/
QAccessibleInterface *QQuickWindow::accessibleRoot() const
{
return QAccessible::queryAccessibleInterface(const_cast<QQuickWindow*>(this));
}
#endif
/*
Focus behavior
==============
Prior to being added to a valid window items can set and clear focus with no
effect. Only once items are added to a window (by way of having a parent set that
already belongs to a window) do the focus rules apply. Focus goes back to
having no effect if an item is removed from a window.
When an item is moved into a new focus scope (either being added to a window
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 a tree of items being
added to a window 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.
*/
QQuickItem::UpdatePaintNodeData::UpdatePaintNodeData()
: transformNode(0)
{
}
QQuickRootItem::QQuickRootItem()
{
}
/*! \reimp */
void QQuickWindow::exposeEvent(QExposeEvent *)
{
Q_D(QQuickWindow);
if (d->windowManager)
d->windowManager->exposureChanged(this);
}
/*! \reimp */
void QQuickWindow::resizeEvent(QResizeEvent *ev)
{
Q_D(QQuickWindow);
if (d->contentItem)
d->contentItem->setSize(ev->size());
if (d->windowManager)
d->windowManager->resize(this);
}
/*! \reimp */
void QQuickWindow::showEvent(QShowEvent *)
{
Q_D(QQuickWindow);
if (d->windowManager)
d->windowManager->show(this);
}
/*! \reimp */
void QQuickWindow::hideEvent(QHideEvent *)
{
Q_D(QQuickWindow);
if (d->windowManager)
d->windowManager->hide(this);
}
/*! \reimp */
void QQuickWindow::focusOutEvent(QFocusEvent *ev)
{
Q_D(QQuickWindow);
d->contentItem->setFocus(false, ev->reason());
}
/*! \reimp */
void QQuickWindow::focusInEvent(QFocusEvent *ev)
{
Q_D(QQuickWindow);
d->contentItem->setFocus(true, ev->reason());
d->updateFocusItemTransform();
}
void QQuickWindowPrivate::polishItems()
{
// An item can trigger polish on another item, or itself for that matter,
// during its updatePolish() call. Because of this, we cannot simply
// iterate through the set, we must continue pulling items out until it
// is empty.
// In the case where polish is called from updatePolish() either directly
// or indirectly, we use a recursionSafeguard to print a warning to
// the user.
int recursionSafeguard = INT_MAX;
while (!itemsToPolish.isEmpty() && --recursionSafeguard > 0) {
QQuickItem *item = itemsToPolish.takeLast();
QQuickItemPrivate::get(item)->polishScheduled = false;
item->updatePolish();
}
if (recursionSafeguard == 0)
qWarning("QQuickWindow: possible QQuickItem::polish() loop");
updateFocusItemTransform();
}
/*!
* Schedules the window to render another frame.
*
* Calling QQuickWindow::update() differs from QQuickItem::update() in that
* it always triggers a repaint, regardless of changes in the underlying
* scene graph or not.
*/
void QQuickWindow::update()
{
Q_D(QQuickWindow);
if (d->windowManager)
d->windowManager->update(this);
else if (d->renderControl)
QQuickRenderControlPrivate::get(d->renderControl)->update();
}
static void updatePixelRatioHelper(QQuickItem *item, float pixelRatio)
{
if (item->flags() & QQuickItem::ItemHasContents) {
QQuickItemPrivate *itemPrivate = QQuickItemPrivate::get(item);
itemPrivate->itemChange(QQuickItem::ItemDevicePixelRatioHasChanged, pixelRatio);
}
QList <QQuickItem *> items = item->childItems();
for (int i = 0; i < items.size(); ++i)
updatePixelRatioHelper(items.at(i), pixelRatio);
}
void QQuickWindow::physicalDpiChanged()
{
Q_D(QQuickWindow);
const qreal newPixelRatio = screen()->devicePixelRatio();
if (qFuzzyCompare(newPixelRatio, d->devicePixelRatio))
return;
d->devicePixelRatio = newPixelRatio;
if (d->contentItem)
updatePixelRatioHelper(d->contentItem, newPixelRatio);
}
void QQuickWindow::handleScreenChanged(QScreen *screen)
{
Q_D(QQuickWindow);
if (screen) {
physicalDpiChanged();
// When physical DPI changes on the same screen, either the resolution or the device pixel
// ratio changed. We must check what it is. Device pixel ratio does not have its own
// ...Changed() signal.
d->physicalDpiChangedConnection = connect(screen, SIGNAL(physicalDotsPerInchChanged(qreal)),
this, SLOT(physicalDpiChanged()));
} else {
disconnect(d->physicalDpiChangedConnection);
}
d->forcePolish();
}
void forcePolishHelper(QQuickItem *item)
{
if (item->flags() & QQuickItem::ItemHasContents) {
item->polish();
}
QList <QQuickItem *> items = item->childItems();
for (int i=0; i<items.size(); ++i)
forcePolishHelper(items.at(i));
}
/*!
Schedules polish events on all items in the scene.
*/
void QQuickWindowPrivate::forcePolish()
{
Q_Q(QQuickWindow);
if (!q->screen())
return;
forcePolishHelper(contentItem);
}
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 QQuickWindowPrivate::syncSceneGraph()
{
QML_MEMORY_SCOPE_STRING("SceneGraph");
Q_Q(QQuickWindow);
animationController->beforeNodeSync();
emit q->beforeSynchronizing();
runAndClearJobs(&beforeSynchronizingJobs);
if (!renderer) {
forceUpdate(contentItem);
QSGRootNode *rootNode = new QSGRootNode;
rootNode->appendChildNode(QQuickItemPrivate::get(contentItem)->itemNode());
renderer = context->createRenderer();
renderer->setRootNode(rootNode);
}
updateDirtyNodes();
animationController->afterNodeSync();
// Copy the current state of clearing from window into renderer.
renderer->setClearColor(clearColor);
QSGAbstractRenderer::ClearMode mode = QSGAbstractRenderer::ClearStencilBuffer | QSGAbstractRenderer::ClearDepthBuffer;
if (clearBeforeRendering)
mode |= QSGAbstractRenderer::ClearColorBuffer;
renderer->setClearMode(mode);
renderer->setCustomRenderMode(customRenderMode);
emit q->afterSynchronizing();
runAndClearJobs(&afterSynchronizingJobs);
context->endSync();
}
void QQuickWindowPrivate::renderSceneGraph(const QSize &size)
{
QML_MEMORY_SCOPE_STRING("SceneGraph");
Q_Q(QQuickWindow);
if (!renderer)
return;
animationController->advance();
emit q->beforeRendering();
runAndClearJobs(&beforeRenderingJobs);
if (!customRenderStage || !customRenderStage->render()) {
int fboId = 0;
const qreal devicePixelRatio = q->effectiveDevicePixelRatio();
if (renderTargetId) {
QRect rect(QPoint(0, 0), renderTargetSize);
fboId = renderTargetId;
renderer->setDeviceRect(rect);
renderer->setViewportRect(rect);
} else {
QRect rect(QPoint(0, 0), devicePixelRatio * size);
renderer->setDeviceRect(rect);
renderer->setViewportRect(rect);
}
renderer->setProjectionMatrixToRect(QRect(QPoint(0, 0), size));
renderer->setDevicePixelRatio(devicePixelRatio);
context->renderNextFrame(renderer, fboId);
}
emit q->afterRendering();
runAndClearJobs(&afterRenderingJobs);
}
QQuickWindowPrivate::QQuickWindowPrivate()
: contentItem(0)
, activeFocusItem(0)
, mouseGrabberItem(0)
#ifndef QT_NO_CURSOR
, cursorItem(0)
#endif
#ifndef QT_NO_DRAGANDDROP
, dragGrabber(0)
#endif
, touchMouseId(-1)
, touchMousePressTimestamp(0)
, dirtyItemList(0)
, devicePixelRatio(0)
, context(0)
, renderer(0)
, windowManager(0)
, renderControl(0)
, touchRecursionGuard(0)
, customRenderStage(0)
, clearColor(Qt::white)
, clearBeforeRendering(true)
, persistentGLContext(true)
, persistentSceneGraph(true)
, lastWheelEventAccepted(false)
, componentCompleted(true)
, lastFocusReason(Qt::OtherFocusReason)
, renderTarget(0)
, renderTargetId(0)
, vaoHelper(0)
, incubationController(0)
{
#ifndef QT_NO_DRAGANDDROP
dragGrabber = new QQuickDragGrabber;
#endif
}
QQuickWindowPrivate::~QQuickWindowPrivate()
{
delete customRenderStage;
}
void QQuickWindowPrivate::init(QQuickWindow *c, QQuickRenderControl *control)
{
q_ptr = c;
Q_Q(QQuickWindow);
contentItem = new QQuickRootItem;
QQmlEngine::setObjectOwnership(contentItem, QQmlEngine::CppOwnership);
QQuickItemPrivate *contentItemPrivate = QQuickItemPrivate::get(contentItem);
contentItemPrivate->window = q;
contentItemPrivate->windowRefCount = 1;
contentItemPrivate->flags |= QQuickItem::ItemIsFocusScope;
contentItem->setSize(q->size());
customRenderMode = qgetenv("QSG_VISUALIZE");
renderControl = control;
if (renderControl)
QQuickRenderControlPrivate::get(renderControl)->window = q;
if (!renderControl)
windowManager = QSGRenderLoop::instance();
Q_ASSERT(windowManager || renderControl);
if (QScreen *screen = q->screen())
devicePixelRatio = screen->devicePixelRatio();
QSGContext *sg;
if (renderControl) {
QQuickRenderControlPrivate *renderControlPriv = QQuickRenderControlPrivate::get(renderControl);
sg = renderControlPriv->sg;
context = renderControlPriv->rc;
} else {
windowManager->addWindow(q);
sg = windowManager->sceneGraphContext();
context = windowManager->createRenderContext(sg);
}
q->setSurfaceType(windowManager ? windowManager->windowSurfaceType() : QSurface::OpenGLSurface);
q->setFormat(sg->defaultSurfaceFormat());
animationController = new QQuickAnimatorController(q);
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);
QObject::connect(q, SIGNAL(focusObjectChanged(QObject*)), q, SIGNAL(activeFocusItemChanged()));
QObject::connect(q, SIGNAL(screenChanged(QScreen*)), q, SLOT(handleScreenChanged(QScreen*)));
QObject::connect(q, SIGNAL(frameSwapped()), q, SLOT(runJobsAfterSwap()), Qt::DirectConnection);
}
/*!
\property QQuickWindow::data
\internal
*/
QQmlListProperty<QObject> QQuickWindowPrivate::data()
{
return QQmlListProperty<QObject>(q_func(), 0, QQuickWindowPrivate::data_append,
QQuickWindowPrivate::data_count,
QQuickWindowPrivate::data_at,
QQuickWindowPrivate::data_clear);
}
static QMouseEvent *touchToMouseEvent(QEvent::Type type, const QTouchEvent::TouchPoint &p, QTouchEvent *event, QQuickItem *item, bool transformNeeded = true)
{
// The touch point local position and velocity are not yet transformed.
QMouseEvent *me = new QMouseEvent(type, transformNeeded ? item->mapFromScene(p.scenePos()) : p.pos(), p.scenePos(), p.screenPos(),
Qt::LeftButton, (type == QEvent::MouseButtonRelease ? Qt::NoButton : Qt::LeftButton), event->modifiers());
me->setAccepted(true);
me->setTimestamp(event->timestamp());
QVector2D transformedVelocity = p.velocity();
if (transformNeeded) {
QQuickItemPrivate *itemPrivate = QQuickItemPrivate::get(item);
QMatrix4x4 transformMatrix(itemPrivate->windowToItemTransform());
transformedVelocity = transformMatrix.mapVector(p.velocity()).toVector2D();
}
QGuiApplicationPrivate::setMouseEventCapsAndVelocity(me, event->device()->capabilities(), transformedVelocity);
QGuiApplicationPrivate::setMouseEventSource(me, Qt::MouseEventSynthesizedByQt);
return me;
}
bool QQuickWindowPrivate::checkIfDoubleClicked(ulong newPressEventTimestamp)
{
bool doubleClicked;
if (touchMousePressTimestamp == 0) {
// just initialize the variable
touchMousePressTimestamp = newPressEventTimestamp;
doubleClicked = false;
} else {
ulong timeBetweenPresses = newPressEventTimestamp - touchMousePressTimestamp;
ulong doubleClickInterval = static_cast<ulong>(QGuiApplication::styleHints()->
mouseDoubleClickInterval());
doubleClicked = timeBetweenPresses < doubleClickInterval;
if (doubleClicked) {
touchMousePressTimestamp = 0;
} else {
touchMousePressTimestamp = newPressEventTimestamp;
}
}
return doubleClicked;
}
bool QQuickWindowPrivate::translateTouchToMouse(QQuickItem *item, QTouchEvent *event)
{
// For each point, check if it is accepted, if not, try the next point.
// Any of the fingers can become the mouse one.
// This can happen because a mouse area might not accept an event at some point but another.
for (int i = 0; i < event->touchPoints().count(); ++i) {
const QTouchEvent::TouchPoint &p = event->touchPoints().at(i);
// A new touch point
if (touchMouseId == -1 && p.state() & Qt::TouchPointPressed) {
QPointF pos = item->mapFromScene(p.scenePos());
// probably redundant, we check bounds in the calling function (matchingNewPoints)
if (!item->contains(pos))
break;
// Store the id already here and restore it to -1 if the event does not get
// accepted. Cannot defer setting the new value because otherwise if the event
// handler spins the event loop all subsequent moves and releases get lost.
touchMouseId = p.id();
itemForTouchPointId[touchMouseId] = item;
qCDebug(DBG_TOUCH_TARGET) << "TP (mouse)" << p.id() << "->" << item;
QScopedPointer<QMouseEvent> mousePress(touchToMouseEvent(QEvent::MouseButtonPress, p, event, item, false));
// Send a single press and see if that's accepted
if (!mouseGrabberItem)
item->grabMouse();
item->grabTouchPoints(QVector<int>() << touchMouseId);
QCoreApplication::sendEvent(item, mousePress.data());
event->setAccepted(mousePress->isAccepted());
if (!mousePress->isAccepted()) {
touchMouseId = -1;
if (itemForTouchPointId.value(p.id()) == item) {
qCDebug(DBG_TOUCH_TARGET) << "TP (mouse)" << p.id() << "disassociated";
itemForTouchPointId.remove(p.id());
}
if (mouseGrabberItem == item)
item->ungrabMouse();
}
if (mousePress->isAccepted() && checkIfDoubleClicked(event->timestamp())) {
QScopedPointer<QMouseEvent> mouseDoubleClick(touchToMouseEvent(QEvent::MouseButtonDblClick, p, event, item, false));
QCoreApplication::sendEvent(item, mouseDoubleClick.data());
event->setAccepted(mouseDoubleClick->isAccepted());
if (mouseDoubleClick->isAccepted()) {
touchMouseIdCandidates.clear();
return true;
} else {
touchMouseId = -1;
}
}
// The event was accepted, we are done.
if (mousePress->isAccepted()) {
touchMouseIdCandidates.clear();
return true;
}
// The event was not accepted but touchMouseId was set.
if (touchMouseId != -1)
return false;
// try the next point
// Touch point was there before and moved
} else if (p.id() == touchMouseId) {
if (p.state() & Qt::TouchPointMoved) {
if (mouseGrabberItem) {
QScopedPointer<QMouseEvent> me(touchToMouseEvent(QEvent::MouseMove, p, event, mouseGrabberItem, false));
QCoreApplication::sendEvent(item, me.data());
event->setAccepted(me->isAccepted());
if (me->isAccepted()) {
qCDebug(DBG_TOUCH_TARGET) << "TP (mouse)" << p.id() << "->" << mouseGrabberItem;
itemForTouchPointId[p.id()] = mouseGrabberItem; // N.B. the mouseGrabberItem may be different after returning from sendEvent()
return true;
}
} else {
// no grabber, check if we care about mouse hover
// FIXME: this should only happen once, not recursively... I'll ignore it just ignore hover now.
// hover for touch???
QScopedPointer<QMouseEvent> me(touchToMouseEvent(QEvent::MouseMove, p, event, item, false));
if (lastMousePosition.isNull())
lastMousePosition = me->windowPos();
QPointF last = lastMousePosition;
lastMousePosition = me->windowPos();
bool accepted = me->isAccepted();
bool delivered = deliverHoverEvent(contentItem, me->windowPos(), last, me->modifiers(), me->timestamp(), accepted);
if (!delivered) {
//take care of any exits
accepted = clearHover(me->timestamp());
}
me->setAccepted(accepted);
break;
}
} else if (p.state() & Qt::TouchPointReleased) {
// currently handled point was released
touchMouseId = -1;
if (mouseGrabberItem) {
QScopedPointer<QMouseEvent> me(touchToMouseEvent(QEvent::MouseButtonRelease, p, event, mouseGrabberItem, false));
QCoreApplication::sendEvent(item, me.data());
if (item->acceptHoverEvents() && p.screenPos() != QGuiApplicationPrivate::lastCursorPosition) {
QPointF localMousePos(qInf(), qInf());
if (QWindow *w = item->window())
localMousePos = item->mapFromScene(w->mapFromGlobal(QGuiApplicationPrivate::lastCursorPosition.toPoint()));
QMouseEvent mm(QEvent::MouseMove, localMousePos, QGuiApplicationPrivate::lastCursorPosition,
Qt::NoButton, Qt::NoButton, event->modifiers());
QCoreApplication::sendEvent(item, &mm);
}
if (mouseGrabberItem) // might have ungrabbed due to event
mouseGrabberItem->ungrabMouse();
return me->isAccepted();
}
}
break;
}
}
return false;
}
void QQuickWindowPrivate::setMouseGrabber(QQuickItem *grabber)
{
Q_Q(QQuickWindow);
if (mouseGrabberItem == grabber)
return;
QQuickItem *oldGrabber = mouseGrabberItem;
mouseGrabberItem = grabber;
if (touchMouseId != -1) {
// update the touch item for mouse touch id to the new grabber
itemForTouchPointId.remove(touchMouseId);
if (grabber) {
qCDebug(DBG_TOUCH_TARGET) << "TP (mouse)" << touchMouseId << "->" << mouseGrabberItem;
itemForTouchPointId[touchMouseId] = grabber;
}
}
if (oldGrabber) {
QEvent ev(QEvent::UngrabMouse);
q->sendEvent(oldGrabber, &ev);
}
}
void QQuickWindowPrivate::transformTouchPoints(QList<QTouchEvent::TouchPoint> &touchPoints, const QTransform &transform)
{
QMatrix4x4 transformMatrix(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()));
touchPoint.setVelocity(transformMatrix.mapVector(touchPoint.velocity()).toVector2D());
}
}
/*!
Translates the data in \a touchEvent to this window. This method leaves the item local positions in
\a touchEvent untouched (these are filled in later).
*/
void QQuickWindowPrivate::translateTouchEvent(QTouchEvent *touchEvent)
{
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);
}
static inline bool windowHasFocus(QQuickWindow *win)
{
const QWindow *focusWindow = QGuiApplication::focusWindow();
return win == focusWindow || QQuickRenderControl::renderWindowFor(win) == focusWindow;
}
/*!
Set the focus inside \a scope to be \a item.
If the scope contains the active focus item, it will be changed to \a item.
Calls notifyFocusChangesRecur for all changed items.
*/
void QQuickWindowPrivate::setFocusInScope(QQuickItem *scope, QQuickItem *item, Qt::FocusReason reason, FocusOptions options)
{
Q_Q(QQuickWindow);
Q_ASSERT(item);
Q_ASSERT(scope || item == contentItem);
qCDebug(DBG_FOCUS) << "QQuickWindowPrivate::setFocusInScope():";
qCDebug(DBG_FOCUS) << " scope:" << (QObject *)scope;
if (scope)
qCDebug(DBG_FOCUS) << " scopeSubFocusItem:" << (QObject *)QQuickItemPrivate::get(scope)->subFocusItem;
qCDebug(DBG_FOCUS) << " item:" << (QObject *)item;
qCDebug(DBG_FOCUS) << " activeFocusItem:" << (QObject *)activeFocusItem;
QQuickItemPrivate *scopePrivate = scope ? QQuickItemPrivate::get(scope) : 0;
QQuickItemPrivate *itemPrivate = QQuickItemPrivate::get(item);
QQuickItem *oldActiveFocusItem = 0;
QQuickItem *currentActiveFocusItem = activeFocusItem;
QQuickItem *newActiveFocusItem = 0;
bool sendFocusIn = false;
lastFocusReason = reason;
QVarLengthArray<QQuickItem *, 20> changed;
// Does this change the active focus?
if (item == contentItem || scopePrivate->activeFocus) {
oldActiveFocusItem = activeFocusItem;
if (item->isEnabled()) {
newActiveFocusItem = item;
while (newActiveFocusItem->isFocusScope()
&& newActiveFocusItem->scopedFocusItem()
&& newActiveFocusItem->scopedFocusItem()->isEnabled()) {
newActiveFocusItem = newActiveFocusItem->scopedFocusItem();
}
} else {
newActiveFocusItem = scope;
}
if (oldActiveFocusItem) {
#ifndef QT_NO_IM
QGuiApplication::inputMethod()->commit();
#endif
activeFocusItem = 0;
QQuickItem *afi = oldActiveFocusItem;
while (afi && afi != scope) {
if (QQuickItemPrivate::get(afi)->activeFocus) {
QQuickItemPrivate::get(afi)->activeFocus = false;
changed << afi;
}
afi = afi->parentItem();
}
}
}
if (item != contentItem && !(options & DontChangeSubFocusItem)) {
QQuickItem *oldSubFocusItem = scopePrivate->subFocusItem;
if (oldSubFocusItem) {
QQuickItemPrivate::get(oldSubFocusItem)->focus = false;
changed << oldSubFocusItem;
}
QQuickItemPrivate::get(item)->updateSubFocusItem(scope, true);
}
if (!(options & DontChangeFocusProperty)) {
if (item != contentItem || windowHasFocus(q)) {
itemPrivate->focus = true;
changed << item;
}
}
if (newActiveFocusItem && contentItem->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();
}
updateFocusItemTransform();
sendFocusIn = true;
}
// Now that all the state is changed, emit signals & events
// We must do this last, as this process may result in further changes to
// focus.
if (oldActiveFocusItem) {
QFocusEvent event(QEvent::FocusOut, reason);
q->sendEvent(oldActiveFocusItem, &event);
}
// Make sure that the FocusOut didn't result in another focus change.
if (sendFocusIn && activeFocusItem == newActiveFocusItem) {
QFocusEvent event(QEvent::FocusIn, reason);
q->sendEvent(newActiveFocusItem, &event);
}
if (activeFocusItem != currentActiveFocusItem)
emit q->focusObjectChanged(activeFocusItem);
if (!changed.isEmpty())
notifyFocusChangesRecur(changed.data(), changed.count() - 1);
}
void QQuickWindowPrivate::clearFocusInScope(QQuickItem *scope, QQuickItem *item, Qt::FocusReason reason, FocusOptions options)
{
Q_Q(QQuickWindow);
Q_ASSERT(item);
Q_ASSERT(scope || item == contentItem);
qCDebug(DBG_FOCUS) << "QQuickWindowPrivate::clearFocusInScope():";
qCDebug(DBG_FOCUS) << " scope:" << (QObject *)scope;
qCDebug(DBG_FOCUS) << " item:" << (QObject *)item;
qCDebug(DBG_FOCUS) << " activeFocusItem:" << (QObject *)activeFocusItem;
QQuickItemPrivate *scopePrivate = 0;
if (scope) {
scopePrivate = QQuickItemPrivate::get(scope);
if ( !scopePrivate->subFocusItem )
return;//No focus, nothing to do.
}
QQuickItem *currentActiveFocusItem = activeFocusItem;
QQuickItem *oldActiveFocusItem = 0;
QQuickItem *newActiveFocusItem = 0;
lastFocusReason = reason;
QVarLengthArray<QQuickItem *, 20> changed;
Q_ASSERT(item == contentItem || item == scopePrivate->subFocusItem);
// Does this change the active focus?
if (item == contentItem || scopePrivate->activeFocus) {
oldActiveFocusItem = activeFocusItem;
newActiveFocusItem = scope;
#ifndef QT_NO_IM
QGuiApplication::inputMethod()->commit();
#endif
activeFocusItem = 0;
if (oldActiveFocusItem) {
QQuickItem *afi = oldActiveFocusItem;
while (afi && afi != scope) {
if (QQuickItemPrivate::get(afi)->activeFocus) {
QQuickItemPrivate::get(afi)->activeFocus = false;
changed << afi;
}
afi = afi->parentItem();
}
}
}
if (item != contentItem && !(options & DontChangeSubFocusItem)) {
QQuickItem *oldSubFocusItem = scopePrivate->subFocusItem;
if (oldSubFocusItem && !(options & DontChangeFocusProperty)) {
QQuickItemPrivate::get(oldSubFocusItem)->focus = false;
changed << oldSubFocusItem;
}
QQuickItemPrivate::get(item)->updateSubFocusItem(scope, false);
} else if (!(options & DontChangeFocusProperty)) {
QQuickItemPrivate::get(item)->focus = false;
changed << item;
}
if (newActiveFocusItem) {
Q_ASSERT(newActiveFocusItem == scope);
activeFocusItem = scope;
updateFocusItemTransform();
}
// Now that all the state is changed, emit signals & events
// We must do this last, as this process may result in further changes to
// focus.
if (oldActiveFocusItem) {
QFocusEvent event(QEvent::FocusOut, reason);
q->sendEvent(oldActiveFocusItem, &event);
}
// Make sure that the FocusOut didn't result in another focus change.
if (newActiveFocusItem && activeFocusItem == newActiveFocusItem) {
QFocusEvent event(QEvent::FocusIn, reason);
q->sendEvent(newActiveFocusItem, &event);
}
if (activeFocusItem != currentActiveFocusItem)
emit q->focusObjectChanged(activeFocusItem);
if (!changed.isEmpty())
notifyFocusChangesRecur(changed.data(), changed.count() - 1);
}
void QQuickWindowPrivate::clearFocusObject()
{
if (activeFocusItem == contentItem)
return;
clearFocusInScope(contentItem, QQuickItemPrivate::get(contentItem)->subFocusItem, Qt::OtherFocusReason);
}
void QQuickWindowPrivate::notifyFocusChangesRecur(QQuickItem **items, int remaining)
{
QPointer<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);
}