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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
|
/****************************************************************************
**
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the QtGui 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 "qglobal.h"
#ifndef QT_NO_GRAPHICSVIEW
#include "qgraphicslayout.h"
#include "qgraphicsproxywidget.h"
#include "private/qgraphicsproxywidget_p.h"
#include "private/qwidget_p.h"
#include "private/qapplication_p.h"
#include <QtCore/qdebug.h>
#include <QtGui/qevent.h>
#include <QtGui/qgraphicsscene.h>
#include <QtGui/qgraphicssceneevent.h>
#include <QtGui/qlayout.h>
#include <QtGui/qpainter.h>
#include <QtGui/qstyleoption.h>
#include <QtGui/qgraphicsview.h>
#include <QtGui/qlistview.h>
#include <QtGui/qlineedit.h>
#include <QtGui/qtextedit.h>
QT_BEGIN_NAMESPACE
//#define GRAPHICSPROXYWIDGET_DEBUG
/*!
\class QGraphicsProxyWidget
\brief The QGraphicsProxyWidget class provides a proxy layer for embedding
a QWidget in a QGraphicsScene.
\since 4.4
\ingroup graphicsview-api
QGraphicsProxyWidget embeds QWidget-based widgets, for example, a
QPushButton, QFontComboBox, or even QFileDialog, into
QGraphicsScene. It forwards events between the two objects and
translates between QWidget's integer-based geometry and
QGraphicsWidget's qreal-based geometry. QGraphicsProxyWidget
supports all core features of QWidget, including tab focus,
keyboard input, Drag & Drop, and popups. You can also embed
complex widgets, e.g., widgets with subwidgets.
Example:
\snippet doc/src/snippets/code/src_gui_graphicsview_qgraphicsproxywidget.cpp 0
QGraphicsProxyWidget takes care of automatically embedding popup children
of embedded widgets through creating a child proxy for each popup. This
means that when an embedded QComboBox shows its popup list, a new
QGraphicsProxyWidget is created automatically, embedding the popup, and
positioning it correctly. This only works if the popup is child of the
embedded widget (for example QToolButton::setMenu() requires the QMenu instance
to be child of the QToolButton).
\section1 Embedding a Widget with QGraphicsProxyWidget
There are two ways to embed a widget using QGraphicsProxyWidget. The most
common way is to pass a widget pointer to QGraphicsScene::addWidget()
together with any relevant \l Qt::WindowFlags. This function returns a
pointer to a QGraphicsProxyWidget. You can then choose to reparent or
position either the proxy, or the embedded widget itself.
For example, in the code snippet below, we embed a group box into the proxy:
\snippet doc/src/snippets/code/src_gui_graphicsview_qgraphicsproxywidget.cpp 1
The image below is the output obtained with its contents margin and
contents rect labeled.
\image qgraphicsproxywidget-embed.png
Alternatively, you can start by creating a new QGraphicsProxyWidget item,
and then call setWidget() to embed a QWidget later. The widget() function
returns a pointer to the embedded widget. QGraphicsProxyWidget shares
ownership with QWidget, so if either of the two widgets are destroyed, the
other widget will be automatically destroyed as well.
\section1 Synchronizing Widget States
QGraphicsProxyWidget keeps its state in sync with the embedded widget. For
example, if the proxy is hidden or disabled, the embedded widget will be
hidden or disabled as well, and vice versa. When the widget is embedded by
calling addWidget(), QGraphicsProxyWidget copies the state from the widget
into the proxy, and after that, the two will stay synchronized where
possible. By default, when you embed a widget into a proxy, both the widget
and the proxy will be visible because a QGraphicsWidget is visible when
created (you do not have to call show()). If you explicitly hide the
embedded widget, the proxy will also become invisible.
Example:
\snippet doc/src/snippets/code/src_gui_graphicsview_qgraphicsproxywidget.cpp 2
QGraphicsProxyWidget maintains symmetry for the following states:
\table
\header \o QWidget state \o QGraphicsProxyWidget state \o Notes
\row \o QWidget::enabled
\o QGraphicsProxyWidget::enabled
\o
\row \o QWidget::visible
\o QGraphicsProxyWidget::visible
\o The explicit state is also symmetric.
\row \o QWidget::geometry
\o QGraphicsProxyWidget::geometry
\o Geometry is only guaranteed to be symmetric while
the embedded widget is visible.
\row \o QWidget::layoutDirection
\o QGraphicsProxyWidget::layoutDirection
\o
\row \o QWidget::style
\o QGraphicsProxyWidget::style
\o
\row \o QWidget::palette
\o QGraphicsProxyWidget::palette
\o
\row \o QWidget::font
\o QGraphicsProxyWidget::font
\o
\row \o QWidget::cursor
\o QGraphicsProxyWidget::cursor
\o The embedded widget overrides the proxy widget
cursor. The proxy cursor changes depending on
which embedded subwidget is currently under the
mouse.
\row \o QWidget::sizeHint()
\o QGraphicsProxyWidget::sizeHint()
\o All size hint functionality from the embedded
widget is forwarded by the proxy.
\row \o QWidget::getContentsMargins()
\o QGraphicsProxyWidget::getContentsMargins()
\o Updated once by setWidget().
\row \o QWidget::windowTitle
\o QGraphicsProxyWidget::windowTitle
\o Updated once by setWidget().
\endtable
\note QGraphicsScene keeps the embedded widget in a special state that
prevents it from disturbing other widgets (both embedded and not embedded)
while the widget is embedded. In this state, the widget may differ slightly
in behavior from when it is not embedded.
\warning This class is provided for convenience when bridging
QWidgets and QGraphicsItems, it should not be used for
high-performance scenarios.
\sa QGraphicsScene::addWidget(), QGraphicsWidget
*/
extern bool qt_sendSpontaneousEvent(QObject *, QEvent *);
Q_GUI_EXPORT extern bool qt_tab_all_widgets;
/*!
\internal
*/
void QGraphicsProxyWidgetPrivate::init()
{
Q_Q(QGraphicsProxyWidget);
q->setFocusPolicy(Qt::WheelFocus);
q->setAcceptDrops(true);
}
/*!
\internal
*/
void QGraphicsProxyWidgetPrivate::sendWidgetMouseEvent(QGraphicsSceneHoverEvent *event)
{
QGraphicsSceneMouseEvent mouseEvent(QEvent::GraphicsSceneMouseMove);
mouseEvent.setPos(event->pos());
mouseEvent.setScreenPos(event->screenPos());
mouseEvent.setButton(Qt::NoButton);
mouseEvent.setButtons(0);
mouseEvent.setModifiers(event->modifiers());
sendWidgetMouseEvent(&mouseEvent);
event->setAccepted(mouseEvent.isAccepted());
}
/*!
\internal
*/
void QGraphicsProxyWidgetPrivate::sendWidgetMouseEvent(QGraphicsSceneMouseEvent *event)
{
if (!event || !widget || !widget->isVisible())
return;
Q_Q(QGraphicsProxyWidget);
// Find widget position and receiver.
QPointF pos = event->pos();
QPointer<QWidget> alienWidget = widget->childAt(pos.toPoint());
QPointer<QWidget> receiver = alienWidget ? alienWidget : widget;
if (QWidgetPrivate::nearestGraphicsProxyWidget(receiver) != q)
return; //another proxywidget will handle the events
// Translate QGraphicsSceneMouse events to QMouseEvents.
QEvent::Type type = QEvent::None;
switch (event->type()) {
case QEvent::GraphicsSceneMousePress:
type = QEvent::MouseButtonPress;
if (!embeddedMouseGrabber)
embeddedMouseGrabber = receiver;
else
receiver = embeddedMouseGrabber;
break;
case QEvent::GraphicsSceneMouseRelease:
type = QEvent::MouseButtonRelease;
if (embeddedMouseGrabber)
receiver = embeddedMouseGrabber;
break;
case QEvent::GraphicsSceneMouseDoubleClick:
type = QEvent::MouseButtonDblClick;
if (!embeddedMouseGrabber)
embeddedMouseGrabber = receiver;
else
receiver = embeddedMouseGrabber;
break;
case QEvent::GraphicsSceneMouseMove:
type = QEvent::MouseMove;
if (embeddedMouseGrabber)
receiver = embeddedMouseGrabber;
break;
default:
Q_ASSERT_X(false, "QGraphicsProxyWidget", "internal error");
break;
}
if (!lastWidgetUnderMouse) {
QApplicationPrivate::dispatchEnterLeave(embeddedMouseGrabber ? embeddedMouseGrabber : receiver, 0);
lastWidgetUnderMouse = receiver;
}
// Map event position from us to the receiver
pos = mapToReceiver(pos, receiver);
// Send mouse event.
QMouseEvent *mouseEvent = QMouseEvent::createExtendedMouseEvent(type, pos,
receiver->mapToGlobal(pos.toPoint()), event->button(),
event->buttons(), event->modifiers());
QWidget *embeddedMouseGrabberPtr = (QWidget *)embeddedMouseGrabber;
QApplicationPrivate::sendMouseEvent(receiver, mouseEvent, alienWidget, widget,
&embeddedMouseGrabberPtr, lastWidgetUnderMouse, event->spontaneous());
embeddedMouseGrabber = embeddedMouseGrabberPtr;
// Handle enter/leave events when last button is released from mouse
// grabber child widget.
if (embeddedMouseGrabber && type == QEvent::MouseButtonRelease && !event->buttons()) {
Q_Q(QGraphicsProxyWidget);
if (q->rect().contains(event->pos()) && q->acceptsHoverEvents())
lastWidgetUnderMouse = alienWidget ? alienWidget : widget;
else // released on the frame our outside the item, or doesn't accept hover events.
lastWidgetUnderMouse = 0;
QApplicationPrivate::dispatchEnterLeave(lastWidgetUnderMouse, embeddedMouseGrabber);
embeddedMouseGrabber = 0;
#ifndef QT_NO_CURSOR
// ### Restore the cursor, don't override it.
if (!lastWidgetUnderMouse)
q->unsetCursor();
#endif
}
event->setAccepted(mouseEvent->isAccepted());
delete mouseEvent;
}
void QGraphicsProxyWidgetPrivate::sendWidgetKeyEvent(QKeyEvent *event)
{
Q_Q(QGraphicsProxyWidget);
if (!event || !widget || !widget->isVisible())
return;
QPointer<QWidget> receiver = widget->focusWidget();
if (!receiver)
receiver = widget;
Q_ASSERT(receiver);
do {
bool res = QApplication::sendEvent(receiver, event);
if ((res && event->isAccepted()) || (q->isWindow() && receiver == widget))
break;
receiver = receiver->parentWidget();
} while (receiver);
}
/*!
\internal
*/
void QGraphicsProxyWidgetPrivate::removeSubFocusHelper(QWidget *widget, Qt::FocusReason reason)
{
QFocusEvent event(QEvent::FocusOut, reason);
QPointer<QWidget> widgetGuard = widget;
QApplication::sendEvent(widget, &event);
if (widgetGuard && event.isAccepted())
QApplication::sendEvent(widget->style(), &event);
}
/*!
\internal
Reimplemented from QGraphicsItemPrivate. ### Qt 5: Move impl to
reimplementation QGraphicsProxyWidget::inputMethodQuery().
*/
QVariant QGraphicsProxyWidgetPrivate::inputMethodQueryHelper(Qt::InputMethodQuery query) const
{
Q_Q(const QGraphicsProxyWidget);
if (!widget || !q->hasFocus())
return QVariant();
QWidget *focusWidget = widget->focusWidget();
if (!focusWidget)
focusWidget = widget;
QVariant v = focusWidget->inputMethodQuery(query);
QPointF focusWidgetPos = q->subWidgetRect(focusWidget).topLeft();
switch (v.type()) {
case QVariant::RectF:
v = v.toRectF().translated(focusWidgetPos);
break;
case QVariant::PointF:
v = v.toPointF() + focusWidgetPos;
break;
case QVariant::Rect:
v = v.toRect().translated(focusWidgetPos.toPoint());
break;
case QVariant::Point:
v = v.toPoint() + focusWidgetPos.toPoint();
break;
default:
break;
}
return v;
}
/*!
\internal
Some of the logic is shared with QApplicationPrivate::focusNextPrevChild_helper
*/
QWidget *QGraphicsProxyWidgetPrivate::findFocusChild(QWidget *child, bool next) const
{
if (!widget)
return 0;
// Run around the focus chain until we find a widget that can take tab focus.
if (!child) {
child = next ? (QWidget *)widget : widget->d_func()->focus_prev;
} else {
child = next ? child->d_func()->focus_next : child->d_func()->focus_prev;
if ((next && child == widget) || (!next && child == widget->d_func()->focus_prev)) {
return 0;
}
}
QWidget *oldChild = child;
uint focus_flag = qt_tab_all_widgets ? Qt::TabFocus : Qt::StrongFocus;
do {
if (child->isEnabled()
&& child->isVisibleTo(widget)
&& ((child->focusPolicy() & focus_flag) == focus_flag)
&& !(child->d_func()->extra && child->d_func()->extra->focus_proxy)) {
return child;
}
child = next ? child->d_func()->focus_next : child->d_func()->focus_prev;
} while (child != oldChild && !(next && child == widget) && !(!next && child == widget->d_func()->focus_prev));
return 0;
}
/*!
\internal
*/
void QGraphicsProxyWidgetPrivate::_q_removeWidgetSlot()
{
Q_Q(QGraphicsProxyWidget);
widget = 0;
delete q;
}
/*!
\internal
*/
void QGraphicsProxyWidgetPrivate::updateWidgetGeometryFromProxy()
{
}
/*!
\internal
*/
void QGraphicsProxyWidgetPrivate::updateProxyGeometryFromWidget()
{
Q_Q(QGraphicsProxyWidget);
if (!widget)
return;
QRectF widgetGeometry = widget->geometry();
QWidget *parentWidget = widget->parentWidget();
if (widget->isWindow()) {
QGraphicsProxyWidget *proxyParent = 0;
if (parentWidget && (proxyParent = qobject_cast<QGraphicsProxyWidget *>(q->parentWidget()))) {
// Nested window proxy (e.g., combobox popup), map widget to the
// parent widget's global coordinates, and map that to the parent
// proxy's child coordinates.
widgetGeometry.moveTo(proxyParent->subWidgetRect(parentWidget).topLeft()
+ parentWidget->mapFromGlobal(widget->pos()));
}
}
// Adjust to size hint if the widget has never been resized.
if (!widget->size().isValid())
widgetGeometry.setSize(widget->sizeHint());
// Assign new geometry.
posChangeMode = QGraphicsProxyWidgetPrivate::WidgetToProxyMode;
sizeChangeMode = QGraphicsProxyWidgetPrivate::WidgetToProxyMode;
q->setGeometry(widgetGeometry);
posChangeMode = QGraphicsProxyWidgetPrivate::NoMode;
sizeChangeMode = QGraphicsProxyWidgetPrivate::NoMode;
}
/*!
\internal
*/
void QGraphicsProxyWidgetPrivate::updateProxyInputMethodAcceptanceFromWidget()
{
Q_Q(QGraphicsProxyWidget);
if (!widget)
return;
QWidget *focusWidget = widget->focusWidget();
if (!focusWidget)
focusWidget = widget;
q->setFlag(QGraphicsItem::ItemAcceptsInputMethod,
focusWidget->testAttribute(Qt::WA_InputMethodEnabled));
}
/*!
\internal
Embeds \a subWin as a subwindow of this proxy widget. \a subWin must be a top-level
widget and a descendant of the widget managed by this proxy. A separate subproxy
will be created as a child of this proxy widget to manage \a subWin.
*/
void QGraphicsProxyWidgetPrivate::embedSubWindow(QWidget *subWin)
{
QWExtra *extra;
if (!((extra = subWin->d_func()->extra) && extra->proxyWidget)) {
QGraphicsProxyWidget *subProxy = new QGraphicsProxyWidget(q_func(), subWin->windowFlags());
subProxy->d_func()->setWidget_helper(subWin, false);
}
}
/*!
\internal
Removes ("unembeds") \a subWin and deletes the proxy holder item. This can
happen when QWidget::setParent() reparents the embedded window out of
"embedded space".
*/
void QGraphicsProxyWidgetPrivate::unembedSubWindow(QWidget *subWin)
{
foreach (QGraphicsItem *child, children) {
if (child->isWidget()) {
if (QGraphicsProxyWidget *proxy = qobject_cast<QGraphicsProxyWidget *>(static_cast<QGraphicsWidget *>(child))) {
if (proxy->widget() == subWin) {
proxy->setWidget(0);
scene->removeItem(proxy);
delete proxy;
return;
}
}
}
}
}
bool QGraphicsProxyWidgetPrivate::isProxyWidget() const
{
return true;
}
/*!
\internal
*/
QPointF QGraphicsProxyWidgetPrivate::mapToReceiver(const QPointF &pos, const QWidget *receiver) const
{
QPointF p = pos;
// Map event position from us to the receiver, preserving its
// precision (don't use QWidget::mapFrom here).
while (receiver && receiver != widget) {
p -= QPointF(receiver->pos());
receiver = receiver->parentWidget();
}
return p;
}
/*!
Constructs a new QGraphicsProxy widget. \a parent and \a wFlags are passed
to QGraphicsItem's constructor.
*/
QGraphicsProxyWidget::QGraphicsProxyWidget(QGraphicsItem *parent, Qt::WindowFlags wFlags)
: QGraphicsWidget(*new QGraphicsProxyWidgetPrivate, parent, 0, wFlags)
{
Q_D(QGraphicsProxyWidget);
d->init();
}
/*!
Destroys the proxy widget and any embedded widget.
*/
QGraphicsProxyWidget::~QGraphicsProxyWidget()
{
Q_D(QGraphicsProxyWidget);
if (d->widget) {
QObject::disconnect(d->widget, SIGNAL(destroyed()), this, SLOT(_q_removeWidgetSlot()));
delete d->widget;
}
}
/*!
Embeds \a widget into this proxy widget. The embedded widget must reside
exclusively either inside or outside of Graphics View. You cannot embed a
widget as long as it is is visible elsewhere in the UI, at the same time.
\a widget must be a top-level widget whose parent is 0.
When the widget is embedded, its state (e.g., visible, enabled, geometry,
size hints) is copied into the proxy widget. If the embedded widget is
explicitly hidden or disabled, the proxy widget will become explicitly
hidden or disabled after embedding is complete. The class documentation
has a full overview over the shared state.
QGraphicsProxyWidget's window flags determine whether the widget, after
embedding, will be given window decorations or not.
After this function returns, QGraphicsProxyWidget will keep its state
synchronized with that of \a widget whenever possible.
If a widget is already embedded by this proxy when this function is
called, that widget will first be automatically unembedded. Passing 0 for
the \a widget argument will only unembed the widget, and the ownership of
the currently embedded widget will be passed on to the caller.
Every child widget that are embedded will also be embedded and their proxy
widget destroyed.
Note that widgets with the Qt::WA_PaintOnScreen widget attribute
set and widgets that wrap an external application or controller
cannot be embedded. Examples are QGLWidget and QAxWidget.
\sa widget()
*/
void QGraphicsProxyWidget::setWidget(QWidget *widget)
{
Q_D(QGraphicsProxyWidget);
d->setWidget_helper(widget, true);
}
void QGraphicsProxyWidgetPrivate::setWidget_helper(QWidget *newWidget, bool autoShow)
{
Q_Q(QGraphicsProxyWidget);
if (newWidget == widget)
return;
if (widget) {
QObject::disconnect(widget, SIGNAL(destroyed()), q, SLOT(_q_removeWidgetSlot()));
widget->removeEventFilter(q);
widget->setAttribute(Qt::WA_DontShowOnScreen, false);
widget->d_func()->extra->proxyWidget = 0;
resolveFont(inheritedFontResolveMask);
resolvePalette(inheritedPaletteResolveMask);
widget->update();
foreach (QGraphicsItem *child, q->childItems()) {
if (child->d_ptr->isProxyWidget()) {
QGraphicsProxyWidget *childProxy = static_cast<QGraphicsProxyWidget *>(child);
QWidget * parent = childProxy->widget();
while (parent->parentWidget() != 0) {
if (parent == widget)
break;
parent = parent->parentWidget();
}
if (!childProxy->widget() || parent != widget)
continue;
childProxy->setWidget(0);
delete childProxy;
}
}
widget = 0;
#ifndef QT_NO_CURSOR
q->unsetCursor();
#endif
q->setAcceptHoverEvents(false);
if (!newWidget)
q->update();
}
if (!newWidget)
return;
if (!newWidget->isWindow()) {
QWExtra *extra = newWidget->parentWidget()->d_func()->extra;
if (!extra || !extra->proxyWidget) {
qWarning("QGraphicsProxyWidget::setWidget: cannot embed widget %p "
"which is not a toplevel widget, and is not a child of an embedded widget", newWidget);
return;
}
}
// Register this proxy within the widget's private.
// ### This is a bit backdoorish
QWExtra *extra = newWidget->d_func()->extra;
if (!extra) {
newWidget->d_func()->createExtra();
extra = newWidget->d_func()->extra;
}
QGraphicsProxyWidget **proxyWidget = &extra->proxyWidget;
if (*proxyWidget) {
if (*proxyWidget != q) {
qWarning("QGraphicsProxyWidget::setWidget: cannot embed widget %p"
"; already embedded", newWidget);
}
return;
}
*proxyWidget = q;
newWidget->setAttribute(Qt::WA_DontShowOnScreen);
newWidget->ensurePolished();
// Do not wait for this widget to close before the app closes ###
// shouldn't this widget inherit the attribute?
newWidget->setAttribute(Qt::WA_QuitOnClose, false);
q->setAcceptHoverEvents(true);
if (newWidget->testAttribute(Qt::WA_NoSystemBackground))
q->setAttribute(Qt::WA_NoSystemBackground);
if (newWidget->testAttribute(Qt::WA_OpaquePaintEvent))
q->setAttribute(Qt::WA_OpaquePaintEvent);
widget = newWidget;
// Changes only go from the widget to the proxy.
enabledChangeMode = QGraphicsProxyWidgetPrivate::WidgetToProxyMode;
visibleChangeMode = QGraphicsProxyWidgetPrivate::WidgetToProxyMode;
posChangeMode = QGraphicsProxyWidgetPrivate::WidgetToProxyMode;
sizeChangeMode = QGraphicsProxyWidgetPrivate::WidgetToProxyMode;
if ((autoShow && !newWidget->testAttribute(Qt::WA_WState_ExplicitShowHide)) || !newWidget->testAttribute(Qt::WA_WState_Hidden)) {
newWidget->show();
}
// Copy the state from the widget onto the proxy.
#ifndef QT_NO_CURSOR
if (newWidget->testAttribute(Qt::WA_SetCursor))
q->setCursor(widget->cursor());
#endif
q->setEnabled(newWidget->isEnabled());
q->setVisible(newWidget->isVisible());
q->setLayoutDirection(newWidget->layoutDirection());
if (newWidget->testAttribute(Qt::WA_SetStyle))
q->setStyle(widget->style());
resolveFont(inheritedFontResolveMask);
resolvePalette(inheritedPaletteResolveMask);
if (!newWidget->testAttribute(Qt::WA_Resized))
newWidget->adjustSize();
int left, top, right, bottom;
newWidget->getContentsMargins(&left, &top, &right, &bottom);
q->setContentsMargins(left, top, right, bottom);
q->setWindowTitle(newWidget->windowTitle());
// size policies and constraints..
q->setSizePolicy(newWidget->sizePolicy());
QSize sz = newWidget->minimumSize();
q->setMinimumSize(sz.isNull() ? QSizeF() : QSizeF(sz));
sz = newWidget->maximumSize();
q->setMaximumSize(sz.isNull() ? QSizeF() : QSizeF(sz));
updateProxyGeometryFromWidget();
updateProxyInputMethodAcceptanceFromWidget();
// Hook up the event filter to keep the state up to date.
newWidget->installEventFilter(q);
QObject::connect(newWidget, SIGNAL(destroyed()), q, SLOT(_q_removeWidgetSlot()));
// Changes no longer go only from the widget to the proxy.
enabledChangeMode = QGraphicsProxyWidgetPrivate::NoMode;
visibleChangeMode = QGraphicsProxyWidgetPrivate::NoMode;
posChangeMode = QGraphicsProxyWidgetPrivate::NoMode;
sizeChangeMode = QGraphicsProxyWidgetPrivate::NoMode;
}
/*!
Returns a pointer to the embedded widget.
\sa setWidget()
*/
QWidget *QGraphicsProxyWidget::widget() const
{
Q_D(const QGraphicsProxyWidget);
return d->widget;
}
/*!
Returns the rectangle for \a widget, which must be a descendant of
widget(), or widget() itself, in this proxy item's local coordinates.
If no widget is embedded, \a widget is 0, or \a widget is not a
descendant of the embedded widget, this function returns an empty QRectF.
\sa widget()
*/
QRectF QGraphicsProxyWidget::subWidgetRect(const QWidget *widget) const
{
Q_D(const QGraphicsProxyWidget);
if (!widget || !d->widget)
return QRectF();
if (d->widget == widget || d->widget->isAncestorOf(widget))
return QRectF(widget->mapTo(d->widget, QPoint(0, 0)), widget->size());
return QRectF();
}
/*!
\reimp
*/
void QGraphicsProxyWidget::setGeometry(const QRectF &rect)
{
Q_D(QGraphicsProxyWidget);
bool proxyResizesWidget = !d->posChangeMode && !d->sizeChangeMode;
if (proxyResizesWidget) {
d->posChangeMode = QGraphicsProxyWidgetPrivate::ProxyToWidgetMode;
d->sizeChangeMode = QGraphicsProxyWidgetPrivate::ProxyToWidgetMode;
}
QGraphicsWidget::setGeometry(rect);
if (proxyResizesWidget) {
d->posChangeMode = QGraphicsProxyWidgetPrivate::NoMode;
d->sizeChangeMode = QGraphicsProxyWidgetPrivate::NoMode;
}
}
/*!
\reimp
*/
QVariant QGraphicsProxyWidget::itemChange(GraphicsItemChange change,
const QVariant &value)
{
Q_D(QGraphicsProxyWidget);
switch (change) {
case ItemPositionChange:
// The item's position is either changed directly on the proxy, in
// which case the position change should propagate to the widget,
// otherwise it happens as a side effect when filtering QEvent::Move.
if (!d->posChangeMode)
d->posChangeMode = QGraphicsProxyWidgetPrivate::ProxyToWidgetMode;
break;
case ItemPositionHasChanged:
// Move the internal widget if we're in widget-to-proxy
// mode. Otherwise the widget has already moved.
if (d->widget && d->posChangeMode != QGraphicsProxyWidgetPrivate::WidgetToProxyMode)
d->widget->move(value.toPoint());
if (d->posChangeMode == QGraphicsProxyWidgetPrivate::ProxyToWidgetMode)
d->posChangeMode = QGraphicsProxyWidgetPrivate::NoMode;
break;
case ItemVisibleChange:
if (!d->visibleChangeMode)
d->visibleChangeMode = QGraphicsProxyWidgetPrivate::ProxyToWidgetMode;
break;
case ItemVisibleHasChanged:
if (d->widget && d->visibleChangeMode != QGraphicsProxyWidgetPrivate::WidgetToProxyMode)
d->widget->setVisible(isVisible());
if (d->visibleChangeMode == QGraphicsProxyWidgetPrivate::ProxyToWidgetMode)
d->visibleChangeMode = QGraphicsProxyWidgetPrivate::NoMode;
break;
case ItemEnabledChange:
if (!d->enabledChangeMode)
d->enabledChangeMode = QGraphicsProxyWidgetPrivate::ProxyToWidgetMode;
break;
case ItemEnabledHasChanged:
if (d->widget && d->enabledChangeMode != QGraphicsProxyWidgetPrivate::WidgetToProxyMode)
d->widget->setEnabled(isEnabled());
if (d->enabledChangeMode == QGraphicsProxyWidgetPrivate::ProxyToWidgetMode)
d->enabledChangeMode = QGraphicsProxyWidgetPrivate::NoMode;
break;
default:
break;
}
return QGraphicsWidget::itemChange(change, value);
}
/*!
\reimp
*/
bool QGraphicsProxyWidget::event(QEvent *event)
{
Q_D(QGraphicsProxyWidget);
if (!d->widget)
return QGraphicsWidget::event(event);
switch (event->type()) {
case QEvent::StyleChange:
// Propagate style changes to the embedded widget.
if (!d->styleChangeMode) {
d->styleChangeMode = QGraphicsProxyWidgetPrivate::ProxyToWidgetMode;
d->widget->setStyle(style());
d->styleChangeMode = QGraphicsProxyWidgetPrivate::NoMode;
}
break;
case QEvent::FontChange: {
// Propagate to widget.
QWidgetPrivate *wd = d->widget->d_func();
int mask = d->font.resolve() | d->inheritedFontResolveMask;
wd->inheritedFontResolveMask = mask;
wd->resolveFont();
break;
}
case QEvent::PaletteChange: {
// Propagate to widget.
QWidgetPrivate *wd = d->widget->d_func();
int mask = d->palette.resolve() | d->inheritedPaletteResolveMask;
wd->inheritedPaletteResolveMask = mask;
wd->resolvePalette();
break;
}
case QEvent::InputMethod: {
// Forward input method events if the focus widget enables
// input methods.
// ### Qt 4.5: this code must also go into a reimplementation
// of inputMethodEvent().
QWidget *focusWidget = d->widget->focusWidget();
if (focusWidget && focusWidget->testAttribute(Qt::WA_InputMethodEnabled))
QApplication::sendEvent(focusWidget, event);
break;
}
case QEvent::ShortcutOverride: {
QWidget *focusWidget = d->widget->focusWidget();
while (focusWidget) {
QApplication::sendEvent(focusWidget, event);
if (event->isAccepted())
return true;
focusWidget = focusWidget->parentWidget();
}
return false;
}
case QEvent::KeyPress: {
QKeyEvent *k = static_cast<QKeyEvent *>(event);
if (k->key() == Qt::Key_Tab || k->key() == Qt::Key_Backtab) {
if (!(k->modifiers() & (Qt::ControlModifier | Qt::AltModifier))) { //### Add MetaModifier?
QWidget *focusWidget = d->widget->focusWidget();
while (focusWidget) {
bool res = QApplication::sendEvent(focusWidget, event);
if ((res && event->isAccepted()) || (isWindow() && focusWidget == d->widget)) {
event->accept();
break;
}
focusWidget = focusWidget->parentWidget();
}
return true;
}
}
break;
}
#ifndef QT_NO_TOOLTIP
case QEvent::GraphicsSceneHelp: {
// Propagate the help event (for tooltip) to the widget under mouse
if (d->lastWidgetUnderMouse) {
QGraphicsSceneHelpEvent *he = static_cast<QGraphicsSceneHelpEvent *>(event);
QPoint pos = d->mapToReceiver(mapFromScene(he->scenePos()), d->lastWidgetUnderMouse).toPoint();
QHelpEvent e(QEvent::ToolTip, pos, he->screenPos());
QApplication::sendEvent(d->lastWidgetUnderMouse, &e);
event->setAccepted(e.isAccepted());
return e.isAccepted();
}
break;
}
case QEvent::ToolTipChange: {
// Propagate tooltip change to the widget
if (!d->tooltipChangeMode) {
d->tooltipChangeMode = QGraphicsProxyWidgetPrivate::ProxyToWidgetMode;
d->widget->setToolTip(toolTip());
d->tooltipChangeMode = QGraphicsProxyWidgetPrivate::NoMode;
}
break;
}
#endif
default:
break;
}
return QGraphicsWidget::event(event);
}
/*!
\reimp
*/
bool QGraphicsProxyWidget::eventFilter(QObject *object, QEvent *event)
{
Q_D(QGraphicsProxyWidget);
if (object == d->widget) {
switch (event->type()) {
case QEvent::LayoutRequest:
updateGeometry();
break;
case QEvent::Resize:
// If the widget resizes itself, we resize the proxy too.
// Prevent feed-back by checking the geometry change mode.
if (!d->sizeChangeMode)
d->updateProxyGeometryFromWidget();
break;
case QEvent::Move:
// If the widget moves itself, we move the proxy too. Prevent
// feed-back by checking the geometry change mode.
if (!d->posChangeMode)
d->updateProxyGeometryFromWidget();
break;
case QEvent::Hide:
case QEvent::Show:
// If the widget toggles its visible state, the proxy will follow.
if (!d->visibleChangeMode) {
d->visibleChangeMode = QGraphicsProxyWidgetPrivate::WidgetToProxyMode;
setVisible(event->type() == QEvent::Show);
d->visibleChangeMode = QGraphicsProxyWidgetPrivate::NoMode;
}
break;
case QEvent::EnabledChange:
// If the widget toggles its enabled state, the proxy will follow.
if (!d->enabledChangeMode) {
d->enabledChangeMode = QGraphicsProxyWidgetPrivate::WidgetToProxyMode;
setEnabled(d->widget->isEnabled());
d->enabledChangeMode = QGraphicsProxyWidgetPrivate::NoMode;
}
break;
case QEvent::StyleChange:
// Propagate style changes to the proxy.
if (!d->styleChangeMode) {
d->styleChangeMode = QGraphicsProxyWidgetPrivate::WidgetToProxyMode;
setStyle(d->widget->style());
d->styleChangeMode = QGraphicsProxyWidgetPrivate::NoMode;
}
break;
#ifndef QT_NO_TOOLTIP
case QEvent::ToolTipChange:
// Propagate tooltip change to the proxy.
if (!d->tooltipChangeMode) {
d->tooltipChangeMode = QGraphicsProxyWidgetPrivate::WidgetToProxyMode;
setToolTip(d->widget->toolTip());
d->tooltipChangeMode = QGraphicsProxyWidgetPrivate::NoMode;
}
break;
#endif
default:
break;
}
}
return QGraphicsWidget::eventFilter(object, event);
}
/*!
\reimp
*/
void QGraphicsProxyWidget::showEvent(QShowEvent *event)
{
Q_UNUSED(event);
}
/*!
\reimp
*/
void QGraphicsProxyWidget::hideEvent(QHideEvent *event)
{
Q_UNUSED(event);
}
#ifndef QT_NO_CONTEXTMENU
/*!
\reimp
*/
void QGraphicsProxyWidget::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
{
Q_D(QGraphicsProxyWidget);
if (!event || !d->widget || !d->widget->isVisible() || !hasFocus())
return;
// Find widget position and receiver.
QPointF pos = event->pos();
QPointer<QWidget> alienWidget = d->widget->childAt(pos.toPoint());
QPointer<QWidget> receiver = alienWidget ? alienWidget : d->widget;
// Map event position from us to the receiver
pos = d->mapToReceiver(pos, receiver);
QPoint globalPos = receiver->mapToGlobal(pos.toPoint());
//If the receiver by-pass the proxy its popups
//will be top level QWidgets therefore they need
//the screen position. mapToGlobal expect the widget to
//have proper coordinates in regards of the windowing system
//but it's not true because the widget is embedded.
if (bypassGraphicsProxyWidget(receiver))
globalPos = event->screenPos();
// Send mouse event. ### Doesn't propagate the event.
QContextMenuEvent contextMenuEvent(QContextMenuEvent::Reason(event->reason()),
pos.toPoint(), globalPos, event->modifiers());
QApplication::sendEvent(receiver, &contextMenuEvent);
event->setAccepted(contextMenuEvent.isAccepted());
}
#endif // QT_NO_CONTEXTMENU
#ifndef QT_NO_DRAGANDDROP
/*!
\reimp
*/
void QGraphicsProxyWidget::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
{
#ifdef QT_NO_DRAGANDDROP
Q_UNUSED(event);
#else
Q_D(QGraphicsProxyWidget);
if (!d->widget)
return;
QDragEnterEvent proxyDragEnter(event->pos().toPoint(), event->dropAction(), event->mimeData(), event->buttons(), event->modifiers());
proxyDragEnter.setAccepted(event->isAccepted());
QApplication::sendEvent(d->widget, &proxyDragEnter);
event->setAccepted(proxyDragEnter.isAccepted());
if (proxyDragEnter.isAccepted()) // we discard answerRect
event->setDropAction(proxyDragEnter.dropAction());
#endif
}
/*!
\reimp
*/
void QGraphicsProxyWidget::dragLeaveEvent(QGraphicsSceneDragDropEvent *event)
{
Q_UNUSED(event);
#ifndef QT_NO_DRAGANDDROP
Q_D(QGraphicsProxyWidget);
if (!d->widget || !d->dragDropWidget)
return;
QDragLeaveEvent proxyDragLeave;
QApplication::sendEvent(d->dragDropWidget, &proxyDragLeave);
d->dragDropWidget = 0;
#endif
}
/*!
\reimp
*/
void QGraphicsProxyWidget::dragMoveEvent(QGraphicsSceneDragDropEvent *event)
{
#ifdef QT_NO_DRAGANDDROP
Q_UNUSED(event);
#else
Q_D(QGraphicsProxyWidget);
if (!d->widget)
return;
QPointF p = event->pos();
event->ignore();
QPointer<QWidget> subWidget = d->widget->childAt(p.toPoint());
QPointer<QWidget> receiver = subWidget ? subWidget : d->widget;
bool eventDelivered = false;
for (; receiver; receiver = receiver->parentWidget()) {
if (!receiver->isEnabled() || !receiver->acceptDrops())
continue;
// Map event position from us to the receiver
QPoint receiverPos = d->mapToReceiver(p, receiver).toPoint();
if (receiver != d->dragDropWidget) {
// Try to enter before we leave
QDragEnterEvent dragEnter(receiverPos, event->possibleActions(), event->mimeData(), event->buttons(), event->modifiers());
dragEnter.setDropAction(event->proposedAction());
QApplication::sendEvent(receiver, &dragEnter);
event->setAccepted(dragEnter.isAccepted());
event->setDropAction(dragEnter.dropAction());
if (!event->isAccepted()) {
// propagate to the parent widget
continue;
}
d->lastDropAction = event->dropAction();
if (d->dragDropWidget) {
QDragLeaveEvent dragLeave;
QApplication::sendEvent(d->dragDropWidget, &dragLeave);
}
d->dragDropWidget = receiver;
}
QDragMoveEvent dragMove(receiverPos, event->possibleActions(), event->mimeData(), event->buttons(), event->modifiers());
event->setDropAction(d->lastDropAction);
QApplication::sendEvent(receiver, &dragMove);
event->setAccepted(dragMove.isAccepted());
event->setDropAction(dragMove.dropAction());
if (event->isAccepted())
d->lastDropAction = event->dropAction();
eventDelivered = true;
break;
}
if (!eventDelivered) {
if (d->dragDropWidget) {
// Leave the last drag drop item
QDragLeaveEvent dragLeave;
QApplication::sendEvent(d->dragDropWidget, &dragLeave);
d->dragDropWidget = 0;
}
// Propagate
event->setDropAction(Qt::IgnoreAction);
}
#endif
}
/*!
\reimp
*/
void QGraphicsProxyWidget::dropEvent(QGraphicsSceneDragDropEvent *event)
{
#ifdef QT_NO_DRAGANDDROP
Q_UNUSED(event);
#else
Q_D(QGraphicsProxyWidget);
if (d->widget && d->dragDropWidget) {
QPoint widgetPos = d->mapToReceiver(event->pos(), d->dragDropWidget).toPoint();
QDropEvent dropEvent(widgetPos, event->possibleActions(), event->mimeData(), event->buttons(), event->modifiers());
QApplication::sendEvent(d->dragDropWidget, &dropEvent);
event->setAccepted(dropEvent.isAccepted());
d->dragDropWidget = 0;
}
#endif
}
#endif
/*!
\reimp
*/
void QGraphicsProxyWidget::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{
Q_UNUSED(event);
}
/*!
\reimp
*/
void QGraphicsProxyWidget::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
{
Q_UNUSED(event);
Q_D(QGraphicsProxyWidget);
// If hoverMove was compressed away, make sure we update properly here.
if (d->lastWidgetUnderMouse) {
QApplicationPrivate::dispatchEnterLeave(0, d->lastWidgetUnderMouse);
d->lastWidgetUnderMouse = 0;
}
}
/*!
\reimp
*/
void QGraphicsProxyWidget::hoverMoveEvent(QGraphicsSceneHoverEvent *event)
{
Q_D(QGraphicsProxyWidget);
#ifdef GRAPHICSPROXYWIDGET_DEBUG
qDebug() << "QGraphicsProxyWidget::hoverMoveEvent";
#endif
// Ignore events on the window frame.
if (!d->widget || !rect().contains(event->pos())) {
if (d->lastWidgetUnderMouse) {
QApplicationPrivate::dispatchEnterLeave(0, d->lastWidgetUnderMouse);
d->lastWidgetUnderMouse = 0;
}
return;
}
d->embeddedMouseGrabber = 0;
d->sendWidgetMouseEvent(event);
}
/*!
\reimp
*/
void QGraphicsProxyWidget::grabMouseEvent(QEvent *event)
{
Q_UNUSED(event);
}
/*!
\reimp
*/
void QGraphicsProxyWidget::ungrabMouseEvent(QEvent *event)
{
Q_D(QGraphicsProxyWidget);
Q_UNUSED(event);
d->embeddedMouseGrabber = 0;
}
/*!
\reimp
*/
void QGraphicsProxyWidget::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
Q_D(QGraphicsProxyWidget);
#ifdef GRAPHICSPROXYWIDGET_DEBUG
qDebug() << "QGraphicsProxyWidget::mouseMoveEvent";
#endif
d->sendWidgetMouseEvent(event);
}
/*!
\reimp
*/
void QGraphicsProxyWidget::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
Q_D(QGraphicsProxyWidget);
#ifdef GRAPHICSPROXYWIDGET_DEBUG
qDebug() << "QGraphicsProxyWidget::mousePressEvent";
#endif
d->sendWidgetMouseEvent(event);
}
/*!
\reimp
*/
void QGraphicsProxyWidget::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
{
Q_D(QGraphicsProxyWidget);
#ifdef GRAPHICSPROXYWIDGET_DEBUG
qDebug() << "QGraphicsProxyWidget::mouseDoubleClickEvent";
#endif
d->sendWidgetMouseEvent(event);
}
/*!
\reimp
*/
#ifndef QT_NO_WHEELEVENT
void QGraphicsProxyWidget::wheelEvent(QGraphicsSceneWheelEvent *event)
{
Q_D(QGraphicsProxyWidget);
#ifdef GRAPHICSPROXYWIDGET_DEBUG
qDebug() << "QGraphicsProxyWidget::wheelEvent";
#endif
if (!d->widget)
return;
QPointF pos = event->pos();
QPointer<QWidget> receiver = d->widget->childAt(pos.toPoint());
if (!receiver)
receiver = d->widget;
// Map event position from us to the receiver
pos = d->mapToReceiver(pos, receiver);
// Send mouse event.
QWheelEvent wheelEvent(pos.toPoint(), event->screenPos(), event->delta(),
event->buttons(), event->modifiers(), event->orientation());
QPointer<QWidget> focusWidget = d->widget->focusWidget();
extern bool qt_sendSpontaneousEvent(QObject *, QEvent *);
qt_sendSpontaneousEvent(receiver, &wheelEvent);
event->setAccepted(wheelEvent.isAccepted());
// ### Remove, this should be done by proper focusIn/focusOut events.
if (focusWidget && !focusWidget->hasFocus()) {
focusWidget->update();
focusWidget = d->widget->focusWidget();
if (focusWidget && focusWidget->hasFocus())
focusWidget->update();
}
}
#endif
/*!
\reimp
*/
void QGraphicsProxyWidget::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
Q_D(QGraphicsProxyWidget);
#ifdef GRAPHICSPROXYWIDGET_DEBUG
qDebug() << "QGraphicsProxyWidget::mouseReleaseEvent";
#endif
d->sendWidgetMouseEvent(event);
}
/*!
\reimp
*/
void QGraphicsProxyWidget::keyPressEvent(QKeyEvent *event)
{
Q_D(QGraphicsProxyWidget);
#ifdef GRAPHICSPROXYWIDGET_DEBUG
qDebug() << "QGraphicsProxyWidget::keyPressEvent";
#endif
d->sendWidgetKeyEvent(event);
}
/*!
\reimp
*/
void QGraphicsProxyWidget::keyReleaseEvent(QKeyEvent *event)
{
Q_D(QGraphicsProxyWidget);
#ifdef GRAPHICSPROXYWIDGET_DEBUG
qDebug() << "QGraphicsProxyWidget::keyReleaseEvent";
#endif
d->sendWidgetKeyEvent(event);
}
/*!
\reimp
*/
void QGraphicsProxyWidget::focusInEvent(QFocusEvent *event)
{
#ifdef GRAPHICSPROXYWIDGET_DEBUG
qDebug() << "QGraphicsProxyWidget::focusInEvent";
#endif
Q_D(QGraphicsProxyWidget);
if (d->focusFromWidgetToProxy) {
// Prevent recursion when the proxy autogains focus through the
// embedded widget calling setFocus(). ### Could be done with event
// filter on FocusIn instead?
return;
}
d->proxyIsGivingFocus = true;
switch (event->reason()) {
case Qt::TabFocusReason: {
if (QWidget *focusChild = d->findFocusChild(0, true))
focusChild->setFocus(event->reason());
break;
}
case Qt::BacktabFocusReason:
if (QWidget *focusChild = d->findFocusChild(0, false))
focusChild->setFocus(event->reason());
break;
default:
if (d->widget && d->widget->focusWidget()) {
d->widget->focusWidget()->setFocus(event->reason());
}
break;
}
d->proxyIsGivingFocus = false;
}
/*!
\reimp
*/
void QGraphicsProxyWidget::focusOutEvent(QFocusEvent *event)
{
#ifdef GRAPHICSPROXYWIDGET_DEBUG
qDebug() << "QGraphicsProxyWidget::focusOutEvent";
#endif
Q_D(QGraphicsProxyWidget);
if (d->widget) {
// We need to explicitly remove subfocus from the embedded widget's
// focus widget.
if (QWidget *focusWidget = d->widget->focusWidget())
d->removeSubFocusHelper(focusWidget, event->reason());
}
}
/*!
\reimp
*/
bool QGraphicsProxyWidget::focusNextPrevChild(bool next)
{
Q_D(QGraphicsProxyWidget);
if (!d->widget || !d->scene)
return QGraphicsWidget::focusNextPrevChild(next);
Qt::FocusReason reason = next ? Qt::TabFocusReason : Qt::BacktabFocusReason;
QWidget *lastFocusChild = d->widget->focusWidget();
if (QWidget *newFocusChild = d->findFocusChild(lastFocusChild, next)) {
newFocusChild->setFocus(reason);
return true;
}
return QGraphicsWidget::focusNextPrevChild(next);
}
/*!
\reimp
*/
QSizeF QGraphicsProxyWidget::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
{
Q_D(const QGraphicsProxyWidget);
if (!d->widget)
return QGraphicsWidget::sizeHint(which, constraint);
QSizeF sh;
switch (which) {
case Qt::PreferredSize:
if (QLayout *l = d->widget->layout())
sh = l->sizeHint();
else
sh = d->widget->sizeHint();
break;
case Qt::MinimumSize:
if (QLayout *l = d->widget->layout())
sh = l->minimumSize();
else
sh = d->widget->minimumSizeHint();
break;
case Qt::MaximumSize:
if (QLayout *l = d->widget->layout())
sh = l->maximumSize();
else
sh = QSizeF(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX);
break;
case Qt::MinimumDescent:
sh = constraint;
break;
default:
break;
}
return sh;
}
/*!
\reimp
*/
void QGraphicsProxyWidget::resizeEvent(QGraphicsSceneResizeEvent *event)
{
Q_D(QGraphicsProxyWidget);
if (d->widget) {
if (d->sizeChangeMode != QGraphicsProxyWidgetPrivate::WidgetToProxyMode)
d->widget->resize(event->newSize().toSize());
}
QGraphicsWidget::resizeEvent(event);
}
/*!
\reimp
*/
void QGraphicsProxyWidget::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_D(QGraphicsProxyWidget);
Q_UNUSED(widget);
if (!d->widget || !d->widget->isVisible())
return;
// Filter out repaints on the window frame.
const QRect exposedWidgetRect = (option->exposedRect & rect()).toAlignedRect();
if (exposedWidgetRect.isEmpty())
return;
// Disable QPainter's default pen being cosmetic. This allows widgets and
// styles to follow Qt's existing defaults without getting ugly cosmetic
// lines when scaled.
bool restore = !(painter->renderHints() & QPainter::NonCosmeticDefaultPen);
painter->setRenderHints(QPainter::NonCosmeticDefaultPen, true);
d->widget->render(painter, exposedWidgetRect.topLeft(), exposedWidgetRect);
// Restore the render hints if necessary.
if (restore)
painter->setRenderHints(QPainter::NonCosmeticDefaultPen, false);
}
/*!
\reimp
*/
int QGraphicsProxyWidget::type() const
{
return Type;
}
/*!
\since 4.5
Creates a proxy widget for the given \a child of the widget
contained in this proxy.
This function makes it possible to acquire proxies for
non top-level widgets. For instance, you can embed a dialog,
and then transform only one of its widgets.
If the widget is already embedded, return the existing proxy widget.
\sa newProxyWidget(), QGraphicsScene::addWidget()
*/
QGraphicsProxyWidget *QGraphicsProxyWidget::createProxyForChildWidget(QWidget *child)
{
QGraphicsProxyWidget *proxy = child->graphicsProxyWidget();
if (proxy)
return proxy;
if (!child->parentWidget()) {
qWarning("QGraphicsProxyWidget::createProxyForChildWidget: top-level widget not in a QGraphicsScene");
return 0;
}
QGraphicsProxyWidget *parentProxy = createProxyForChildWidget(child->parentWidget());
if (!parentProxy)
return 0;
if (!QMetaObject::invokeMethod(parentProxy, "newProxyWidget", Qt::DirectConnection,
Q_RETURN_ARG(QGraphicsProxyWidget*, proxy), Q_ARG(const QWidget*, child)))
return 0;
proxy->setParent(parentProxy);
proxy->setWidget(child);
return proxy;
}
/*!
\fn QGraphicsProxyWidget *QGraphicsProxyWidget::newProxyWidget(const QWidget *child)
\since 4.5
Creates a proxy widget for the given \a child of the widget contained in this
proxy.
You should not call this function directly; use
QGraphicsProxyWidget::createProxyForChildWidget() instead.
This function is a fake virtual slot that you can reimplement in
your subclass in order to control how new proxy widgets are
created. The default implementation returns a proxy created with
the QGraphicsProxyWidget() constructor with this proxy widget as
the parent.
\sa createProxyForChildWidget()
*/
QGraphicsProxyWidget *QGraphicsProxyWidget::newProxyWidget(const QWidget *)
{
return new QGraphicsProxyWidget(this);
}
QT_END_NAMESPACE
#include "moc_qgraphicsproxywidget.cpp"
#endif //QT_NO_GRAPHICSVIEW
|