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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
255
256
257
258
259
260
263
264
265
266
267
268
271
272
273
274
275
276
279
280
281
282
283
284
287
288
289
290
291
292
295
296
297
298
299
300
303
304
305
306
307
308
311
312
313
314
315
316
319
320
321
322
323
324
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
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
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
727
728
729
730
731
732
733
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
767#include <QtCore/qbitarray.h>
768#include <QtCore/qpoint.h>
769#include <QtCore/qstack.h>
770#include <QtCore/qvariant.h>
771#include <QtCore/qvarlengtharray.h>
772#include <QtCore/qnumeric.h>
773#include <QtWidgets/qapplication.h>
774#include <QtGui/qbitmap.h>
775#include <QtGui/qpainter.h>
776#include <QtGui/qpainterpath.h>
777#include <QtGui/qpixmapcache.h>
778#include <QtWidgets/qstyleoption.h>
779#include <QtGui/qevent.h>
780#include <QtGui/qinputmethod.h>
781#if QT_CONFIG(graphicseffect)
782#include <QtWidgets/qgraphicseffect.h>
785#include <private/qgraphicsitem_p.h>
786#include <private/qgraphicswidget_p.h>
787#include <private/qwidgettextcontrol_p.h>
788#include <private/qtextdocumentlayout_p.h>
789#include <private/qtextengine_p.h>
790#include <private/qwidget_p.h>
791#include <private/qapplication_p.h>
792#include <private/qgesturemanager_p.h>
793#include <private/qdebug_p.h>
797QT_IMPL_METATYPE_EXTERN_TAGGED(QGraphicsItem*, QGraphicsItem_ptr)
799static inline void _q_adjustRect(QRect *rect)
803 rect->adjust(0, 0, 1, 0);
805 rect->adjust(0, 0, 0, 1);
809
810
819
820
821
822
823
828 const qreal penWidthZero = qreal(0.00000001);
830 if (path == QPainterPath() || pen == Qt::NoPen)
832 QPainterPathStroker ps;
833 ps.setCapStyle(pen.capStyle());
834 if (pen.widthF() <= 0.0)
835 ps.setWidth(penWidthZero);
837 ps.setWidth(pen.widthF());
838 ps.setJoinStyle(pen.joinStyle());
839 ps.setMiterLimit(pen.miterLimit());
840 QPainterPath p = ps.createStroke(path);
846
847
848QGraphicsItemPrivate::QGraphicsItemPrivate()
853 transformData(
nullptr),
854 graphicsEffect(
nullptr),
859 subFocusItem(
nullptr),
860 focusScopeItem(
nullptr),
861 imHints(Qt::ImhNone),
862 panelModality(QGraphicsItem::NonModal),
863 acceptedMouseButtons(0x1f),
865 explicitlyHidden(
false),
867 explicitlyDisabled(
false),
871 isMemberOfGroup(
false),
872 handlesChildEvents(
false),
873 itemDiscovered(
false),
877 hasBoundingRegionGranularity(
false),
880 dirtyChildren(
false),
881 localCollisionHack(
false),
882 inSetPosHelper(
false),
883 needSortChildren(
false),
884 allChildrenDirty(
false),
885 fullUpdatePending(
false),
887 paintedViewBoundingRectsNeedRepaint(
false),
888 dirtySceneTransform(
true),
889 geometryChanged(
true),
892 ignoreVisible(
false),
893 ignoreOpacity(
false),
894 acceptTouchEvents(
false),
895 acceptedTouchBeginEvent(
false),
896 filtersDescendantEvents(
false),
897 sceneTransformTranslateOnly(
false),
898 notifyBoundingRectChanged(
false),
899 notifyInvalidated(
false),
900 mouseSetsFocus(
true),
901 explicitActivate(
false),
903 holesInSiblingIndex(
false),
904 sequentialOrdering(
true),
905 updateDueToGraphicsEffect(
false),
906 scenePosDescendants(
false),
907 pendingPolish(
false),
908 mayHaveChildWithGraphicsEffect(
false),
909 sendParentChangeNotification(
false),
910 dirtyChildrenBoundingRect(
true),
911 globalStackingOrder(-1),
917
918
919QGraphicsItemPrivate::~QGraphicsItemPrivate()
924
925
926
927
928
929
930void QGraphicsItemPrivate::updateAncestorFlag(QGraphicsItem::GraphicsItemFlag childFlag,
931 AncestorFlag flag,
bool enabled,
bool root)
937 switch (
int(childFlag)) {
939 flag = AncestorFiltersChildEvents;
940 enabled = q->filtersChildEvents();
943 flag = AncestorHandlesChildEvents;
944 enabled = q->handlesChildEvents();
946 case QGraphicsItem::ItemClipsChildrenToShape:
947 flag = AncestorClipsChildren;
948 enabled = flags & QGraphicsItem::ItemClipsChildrenToShape;
950 case QGraphicsItem::ItemIgnoresTransformations:
951 flag = AncestorIgnoresTransformations;
952 enabled = flags & QGraphicsItem::ItemIgnoresTransformations;
954 case QGraphicsItem::ItemContainsChildrenInShape:
955 flag = AncestorContainsChildren;
956 enabled = flags & QGraphicsItem::ItemContainsChildrenInShape;
964 if ((parent->d_ptr->ancestorFlags & flag)
965 || (
int(parent->d_ptr->flags & childFlag) == childFlag)
966 || (
int(childFlag) == -1 && parent->d_ptr->handlesChildEvents)
967 || (
int(childFlag) == -2 && parent->d_ptr->filtersDescendantEvents)) {
969 ancestorFlags |= flag;
971 ancestorFlags &= ~flag;
980 if (((ancestorFlags & flag) && enabled) || (!(ancestorFlags & flag) && !enabled))
985 ancestorFlags |= flag;
987 ancestorFlags &= ~flag;
990 if ((
int(childFlag) != -1 &&
int(flags & childFlag) == childFlag)
991 || (
int(childFlag) == -1 && handlesChildEvents)
992 || (
int(childFlag) == -2 && filtersDescendantEvents))
996 for (
int i = 0; i < children.size(); ++i)
997 children.at(i)->d_ptr->updateAncestorFlag(childFlag, flag, enabled,
false);
1000void QGraphicsItemPrivate::updateAncestorFlags()
1005 QGraphicsItemPrivate *pd = parent->d_ptr.data();
1006 flags = pd->ancestorFlags;
1009 if (pd->filtersDescendantEvents)
1010 flags |= AncestorFiltersChildEvents;
1011 if (pd->handlesChildEvents)
1012 flags |= AncestorHandlesChildEvents;
1013 if (pd->flags & QGraphicsItem::ItemClipsChildrenToShape)
1014 flags |= AncestorClipsChildren;
1015 if (pd->flags & QGraphicsItem::ItemIgnoresTransformations)
1016 flags |= AncestorIgnoresTransformations;
1017 if (pd->flags & QGraphicsItem::ItemContainsChildrenInShape)
1018 flags |= AncestorContainsChildren;
1021 if (ancestorFlags == flags)
1023 ancestorFlags = flags;
1026 for (
int i = 0; i < children.size(); ++i)
1027 children.at(i)->d_ptr->updateAncestorFlags();
1031
1032
1033
1034
1035void QGraphicsItemPrivate::setIsMemberOfGroup(
bool enabled)
1038 isMemberOfGroup = enabled;
1039 if (!qgraphicsitem_cast<QGraphicsItemGroup *>(q)) {
1040 for (QGraphicsItem *child : std::as_const(children))
1041 child->d_func()->setIsMemberOfGroup(enabled);
1046
1047
1048
1049
1050void QGraphicsItemPrivate::remapItemPos(QEvent *event, QGraphicsItem *item)
1053 switch (event->type()) {
1054 case QEvent::GraphicsSceneMouseMove:
1055 case QEvent::GraphicsSceneMousePress:
1056 case QEvent::GraphicsSceneMouseRelease:
1057 case QEvent::GraphicsSceneMouseDoubleClick: {
1058 QGraphicsSceneMouseEvent *mouseEvent =
static_cast<QGraphicsSceneMouseEvent *>(event);
1059 mouseEvent->setPos(item->mapFromItem(q, mouseEvent->pos()));
1060 mouseEvent->setLastPos(item->mapFromItem(q, mouseEvent->pos()));
1061 for (
int i = 0x1; i <= 0x10; i <<= 1) {
1062 if (mouseEvent->buttons() & i) {
1063 Qt::MouseButton button = Qt::MouseButton(i);
1064 mouseEvent->setButtonDownPos(button, item->mapFromItem(q, mouseEvent->buttonDownPos(button)));
1069 case QEvent::GraphicsSceneWheel: {
1070 QGraphicsSceneWheelEvent *wheelEvent =
static_cast<QGraphicsSceneWheelEvent *>(event);
1071 wheelEvent->setPos(item->mapFromItem(q, wheelEvent->pos()));
1074 case QEvent::GraphicsSceneContextMenu: {
1075 QGraphicsSceneContextMenuEvent *contextEvent =
static_cast<QGraphicsSceneContextMenuEvent *>(event);
1076 contextEvent->setPos(item->mapFromItem(q, contextEvent->pos()));
1079 case QEvent::GraphicsSceneHoverMove: {
1080 QGraphicsSceneHoverEvent *hoverEvent =
static_cast<QGraphicsSceneHoverEvent *>(event);
1081 hoverEvent->setPos(item->mapFromItem(q, hoverEvent->pos()));
1090
1091
1092
1093
1094
1095
1097QTransform QGraphicsItemPrivate::genericMapFromSceneTransform(
const QWidget *viewport)
const
1099 Q_Q(
const QGraphicsItem);
1100 if (!itemIsUntransformable())
1101 return sceneTransform.inverted();
1102 const QGraphicsView *view = viewport
1103 ? qobject_cast<QGraphicsView *>(viewport->parentWidget())
1105 if (view ==
nullptr)
1106 return sceneTransform.inverted();
1108 const QTransform viewportTransform = view->viewportTransform();
1109 return viewportTransform * q->deviceTransform(viewportTransform).inverted();
1112QPointF QGraphicsItemPrivate::genericMapFromScene(
const QPointF &pos,
1113 const QWidget *viewport)
const
1115 return genericMapFromSceneTransform(viewport).map(pos);
1119
1120
1121
1122
1123
1124
1125
1126void QGraphicsItemPrivate::combineTransformToParent(QTransform *x,
const QTransform *viewTransform)
const
1129 if (viewTransform && itemIsUntransformable()) {
1130 *x = q_ptr->deviceTransform(*viewTransform);
1133 *x *= transformData->computedFullTransform();
1135 *x *= QTransform::fromTranslate(pos.x(), pos.y());
1140
1141
1142
1143
1144
1145
1146
1147
1148void QGraphicsItemPrivate::combineTransformFromParent(QTransform *x,
const QTransform *viewTransform)
const
1151 if (viewTransform && itemIsUntransformable()) {
1152 *x = q_ptr->deviceTransform(*viewTransform);
1154 x->translate(pos.x(), pos.y());
1156 *x = transformData->computedFullTransform(x);
1160void QGraphicsItemPrivate::updateSceneTransformFromParent()
1163 Q_ASSERT(!parent->d_ptr->dirtySceneTransform);
1164 if (parent->d_ptr->sceneTransformTranslateOnly) {
1165 sceneTransform = QTransform::fromTranslate(parent->d_ptr->sceneTransform.dx() + pos.x(),
1166 parent->d_ptr->sceneTransform.dy() + pos.y());
1168 sceneTransform = parent->d_ptr->sceneTransform;
1169 sceneTransform.translate(pos.x(), pos.y());
1171 if (transformData) {
1172 sceneTransform = transformData->computedFullTransform(&sceneTransform);
1173 sceneTransformTranslateOnly = (sceneTransform.type() <= QTransform::TxTranslate);
1175 sceneTransformTranslateOnly = parent->d_ptr->sceneTransformTranslateOnly;
1177 }
else if (!transformData) {
1178 sceneTransform = QTransform::fromTranslate(pos.x(), pos.y());
1179 sceneTransformTranslateOnly = 1;
1180 }
else if (transformData->onlyTransform) {
1181 sceneTransform = transformData->transform;
1183 sceneTransform *= QTransform::fromTranslate(pos.x(), pos.y());
1184 sceneTransformTranslateOnly = (sceneTransform.type() <= QTransform::TxTranslate);
1185 }
else if (pos.isNull()) {
1186 sceneTransform = transformData->computedFullTransform();
1187 sceneTransformTranslateOnly = (sceneTransform.type() <= QTransform::TxTranslate);
1189 sceneTransform = QTransform::fromTranslate(pos.x(), pos.y());
1190 sceneTransform = transformData->computedFullTransform(&sceneTransform);
1191 sceneTransformTranslateOnly = (sceneTransform.type() <= QTransform::TxTranslate);
1193 dirtySceneTransform = 0;
1197
1198
1199
1200
1201
1202
1203void QGraphicsItemPrivate::setParentItemHelper(QGraphicsItem *newParent,
const QVariant *newParentVariant,
1204 const QVariant *thisPointerVariant)
1207 if (newParent == parent)
1211 static_cast<QGraphicsWidgetPrivate *>(
this)->fixFocusChainBeforeReparenting((newParent &&
1212 newParent->isWidget()) ?
static_cast<QGraphicsWidget *>(newParent) :
nullptr,
1216 if (scene->d_func()->indexMethod != QGraphicsScene::NoIndex)
1217 scene->d_func()->index->itemChange(q, QGraphicsItem::ItemParentChange, newParent);
1220 if (scenePosDescendants || (flags & QGraphicsItem::ItemSendsScenePositionChanges))
1221 scene->d_func()->setScenePosItemEnabled(q,
false);
1224 if (subFocusItem && parent) {
1226 subFocusItem->d_ptr->clearSubFocus(parent);
1233 q_ptr->prepareGeometryChange();
1237 parent->d_ptr->removeChild(q);
1238 if (thisPointerVariant)
1239 parent->itemChange(QGraphicsItem::ItemChildRemovedChange, *thisPointerVariant);
1244 if (scene && !inDestructor) {
1245 if (parent && !newParent) {
1246 scene->d_func()->registerTopLevelItem(q);
1247 }
else if (!parent && newParent) {
1248 scene->d_func()->unregisterTopLevelItem(q);
1254 QGraphicsItem *p = parent;
1255 QGraphicsItem *parentFocusScopeItem =
nullptr;
1257 if (p->d_ptr->flags & QGraphicsItem::ItemIsFocusScope) {
1260 QGraphicsItem *fsi = p->d_ptr->focusScopeItem;
1261 if (q_ptr == fsi || q_ptr->isAncestorOf(fsi)) {
1262 parentFocusScopeItem = fsi;
1263 p->d_ptr->focusScopeItem =
nullptr;
1264 fsi->d_ptr->focusScopeItemChange(
false);
1268 p = p->d_ptr->parent;
1272 if (newParent && (graphicsEffect || mayHaveChildWithGraphicsEffect))
1273 newParent->d_ptr->updateChildWithGraphicsEffectFlagRecursively();
1276 QGraphicsItem *newFocusScopeItem = subFocusItem ? subFocusItem : parentFocusScopeItem;
1277 if (newFocusScopeItem && newParent) {
1278 QGraphicsItem *p = newParent;
1280 if (p->d_ptr->flags & QGraphicsItem::ItemIsFocusScope) {
1281 if (subFocusItem && subFocusItem != q_ptr) {
1283 QGraphicsItem *ancestorScope =
nullptr;
1284 QGraphicsItem *p2 = subFocusItem->d_ptr->parent;
1285 while (p2 && p2 != p) {
1286 if (p2->d_ptr->flags & QGraphicsItem::ItemIsFocusScope)
1288 if (p2->d_ptr->flags & QGraphicsItem::ItemIsPanel)
1292 p2 = p2->d_ptr->parent;
1295 newFocusScopeItem = ancestorScope;
1298 p->d_ptr->focusScopeItem = newFocusScopeItem;
1299 newFocusScopeItem->d_ptr->focusScopeItemChange(
true);
1303 if (subFocusItem && !p->focusItem())
1304 subFocusItem->d_ptr->clearSubFocus();
1307 p = p->d_ptr->parent;
1312 invalidateDepthRecursively();
1314 if ((parent = newParent)) {
1315 if (parent->d_func()->scene && parent->d_func()->scene != scene) {
1317 parent->d_func()->scene->addItem(q);
1318 }
else if (!parent->d_func()->scene && scene) {
1320 scene->removeItem(q);
1323 parent->d_ptr->addChild(q);
1324 if (thisPointerVariant)
1325 parent->itemChange(QGraphicsItem::ItemChildAddedChange, *thisPointerVariant);
1328 if (scenePosDescendants || (flags & QGraphicsItem::ItemSendsScenePositionChanges))
1329 scene->d_func()->setScenePosItemEnabled(q,
true);
1333 markParentDirty(
true);
1336 updateAncestorFlags();
1339 if (parent->d_ptr->visible != visible) {
1340 if (!parent->d_ptr->visible || !explicitlyHidden)
1341 setVisibleHelper(parent->d_ptr->visible,
false,
false);
1343 if (parent->isEnabled() != enabled) {
1344 if (!parent->d_ptr->enabled || !explicitlyDisabled)
1345 setEnabledHelper(parent->d_ptr->enabled,
false,
false);
1349 if (visible && parent->isActive())
1353 updateAncestorFlags();
1355 if (!inDestructor) {
1357 if (!visible && !explicitlyHidden)
1358 setVisibleHelper(
true,
false);
1359 if (!enabled && !explicitlyDisabled)
1360 setEnabledHelper(
true,
false);
1364 dirtySceneTransform = 1;
1365 if (!inDestructor && (transformData || (newParent && newParent->d_ptr->transformData)))
1370 subFocusItem->d_ptr->setSubFocus(newParent);
1371 if (parent && parent->isActive())
1372 subFocusItem->setFocus();
1376 if (newParentVariant)
1377 q->itemChange(QGraphicsItem::ItemParentHasChanged, *newParentVariant);
1380 emit
static_cast<QGraphicsObject *>(q)->parentChanged();
1384
1385
1386
1387
1388void QGraphicsItemPrivate::childrenBoundingRectHelper(QTransform *x, QRectF *rect, QGraphicsItem *topMostEffectItem)
1392 QRectF childrenRect;
1393 QRectF *result = rect;
1394 rect = &childrenRect;
1395 const bool setTopMostEffectItem = !topMostEffectItem;
1397 for (
int i = 0; i < children.size(); ++i) {
1398 QGraphicsItem *child = children.at(i);
1399 QGraphicsItemPrivate *childd = child->d_ptr.data();
1400 if (setTopMostEffectItem)
1401 topMostEffectItem = child;
1402 bool hasPos = !childd->pos.isNull();
1403 if (hasPos || childd->transformData) {
1405 QTransform matrix = childd->transformToParent();
1408 *rect |= matrix.mapRect(child->d_ptr->effectiveBoundingRect(topMostEffectItem));
1409 if (!childd->children.isEmpty())
1410 childd->childrenBoundingRectHelper(&matrix, rect, topMostEffectItem);
1413 *rect |= x->mapRect(child->d_ptr->effectiveBoundingRect(topMostEffectItem));
1415 *rect |= child->d_ptr->effectiveBoundingRect(topMostEffectItem);
1416 if (!childd->children.isEmpty())
1417 childd->childrenBoundingRectHelper(x, rect, topMostEffectItem);
1421 if (flags & QGraphicsItem::ItemClipsChildrenToShape){
1423 *rect &= x->mapRect(q->boundingRect());
1425 *rect &= q->boundingRect();
1431void QGraphicsItemPrivate::initStyleOption(QStyleOptionGraphicsItem *option,
const QTransform &worldTransform,
1432 const QRegion &exposedRegion,
bool allItems)
const
1435 Q_Q(
const QGraphicsItem);
1438 const QRectF brect = q->boundingRect();
1439 option->state = QStyle::State_None;
1440 option->rect = brect.toRect();
1441 option->exposedRect = brect;
1446 option->styleObject = q_ptr->toGraphicsObject();
1447 if (!option->styleObject)
1448 option->styleObject = scene;
1451 option->state |= QStyle::State_Selected;
1453 option->state |= QStyle::State_Enabled;
1455 option->state |= QStyle::State_HasFocus;
1457 if (scene->d_func()->hoverItems.contains(q_ptr))
1458 option->state |= QStyle::State_MouseOver;
1459 if (q == scene->mouseGrabberItem())
1460 option->state |= QStyle::State_Sunken;
1463 if (!(flags & QGraphicsItem::ItemUsesExtendedStyleOption))
1468 option->exposedRect = QRectF();
1469 const QTransform reverseMap = worldTransform.inverted();
1470 for (
const QRect &exposedRect : exposedRegion) {
1471 option->exposedRect |= reverseMap.mapRect(QRectF(exposedRect));
1472 if (option->exposedRect.contains(brect))
1475 option->exposedRect &= brect;
1480
1481
1482
1483
1486 QPixmapCache::remove(key);
1487 key = QPixmapCache::Key();
1488 const auto &constDeviceData = deviceData;
1489 for (
const auto &data : constDeviceData)
1490 QPixmapCache::remove(data.key);
1497
1498
1499
1500
1501
1502
1503
1504
1505QGraphicsItem::QGraphicsItem(QGraphicsItem *parent)
1506 : d_ptr(
new QGraphicsItemPrivate)
1508 d_ptr->q_ptr =
this;
1509 setParentItem(parent);
1513
1514
1515QGraphicsItem::QGraphicsItem(QGraphicsItemPrivate &dd, QGraphicsItem *parent)
1518 d_ptr->q_ptr =
this;
1519 setParentItem(parent);
1523
1524
1525
1526
1527
1528
1529
1530QGraphicsItem::~QGraphicsItem()
1532 if (d_ptr->isObject) {
1533 QGraphicsObject *o =
static_cast<QGraphicsObject *>(
this);
1534 QObjectPrivate *p = QObjectPrivate::get(o);
1535 p->wasDeleted =
true;
1536 if (p->declarativeData) {
1537 p->wasDeleted =
true;
1538 if (QAbstractDeclarativeData::destroyed)
1539 QAbstractDeclarativeData::destroyed(p->declarativeData, o);
1540 p->declarativeData =
nullptr;
1541 p->wasDeleted =
false;
1545 d_ptr->inDestructor = 1;
1546 d_ptr->removeExtraItemCache();
1548#ifndef QT_NO_GESTURES
1549 if (d_ptr->isObject && !d_ptr->gestureContext.isEmpty()) {
1550 QGraphicsObject *o =
static_cast<QGraphicsObject *>(
this);
1551 if (QGestureManager *manager = QGestureManager::instance(QGestureManager::DontForceCreation)) {
1552 for (
auto it = d_ptr->gestureContext.constBegin(); it != d_ptr->gestureContext.constEnd(); ++it)
1553 manager->cleanupCachedGestures(o, it.key());
1559 setFocusProxy(
nullptr);
1562 QGraphicsItem *p = d_ptr->parent;
1564 if (p->flags() & ItemIsFocusScope) {
1565 if (p->d_ptr->focusScopeItem ==
this)
1566 p->d_ptr->focusScopeItem =
nullptr;
1569 p = p->d_ptr->parent;
1572 if (!d_ptr->children.isEmpty()) {
1573 while (!d_ptr->children.isEmpty())
1574 delete d_ptr->children.first();
1575 Q_ASSERT(d_ptr->children.isEmpty());
1579 d_ptr->scene->d_func()->removeItemHelper(
this);
1581 d_ptr->resetFocusProxy();
1582 setParentItem(
nullptr);
1585#if QT_CONFIG(graphicseffect)
1586 delete d_ptr->graphicsEffect;
1588 if (d_ptr->transformData) {
1589 for(
int i = 0; i < d_ptr->transformData->graphicsTransforms.size(); ++i) {
1590 QGraphicsTransform *t = d_ptr->transformData->graphicsTransforms.at(i);
1591 static_cast<QGraphicsTransformPrivate *>(t->d_ptr.data())->item =
nullptr;
1595 delete d_ptr->transformData;
1597 if (QGraphicsItemCustomDataStore *dataStore = qt_dataStore())
1598 dataStore->data.remove(
this);
1602
1603
1604
1605
1606
1607QGraphicsScene *QGraphicsItem::scene()
const
1609 return d_ptr->scene;
1613
1614
1615
1616
1617
1618QGraphicsItemGroup *QGraphicsItem::group()
const
1620 if (!d_ptr->isMemberOfGroup)
1622 QGraphicsItem *parent =
const_cast<QGraphicsItem *>(
this);
1623 while ((parent = parent->d_ptr->parent)) {
1624 if (QGraphicsItemGroup *group = qgraphicsitem_cast<QGraphicsItemGroup *>(parent))
1633
1634
1635
1636
1637
1638
1639void QGraphicsItem::setGroup(QGraphicsItemGroup *group)
1642 if (QGraphicsItemGroup *group =
this->group())
1643 group->removeFromGroup(
this);
1645 group->addToGroup(
this);
1650
1651
1652
1653
1654
1655QGraphicsItem *QGraphicsItem::parentItem()
const
1657 return d_ptr->parent;
1661
1662
1663
1664
1665
1666
1667
1668QGraphicsItem *QGraphicsItem::topLevelItem()
const
1670 QGraphicsItem *parent =
const_cast<QGraphicsItem *>(
this);
1671 while (QGraphicsItem *grandPa = parent->parentItem())
1677
1678
1679
1680
1681
1682
1683
1684QGraphicsObject *QGraphicsItem::parentObject()
const
1686 QGraphicsItem *p = d_ptr->parent;
1687 return (p && p->d_ptr->isObject) ?
static_cast<QGraphicsObject *>(p) :
nullptr;
1691
1692
1693
1694
1695
1696
1697
1698QGraphicsWidget *QGraphicsItem::parentWidget()
const
1700 QGraphicsItem *p = parentItem();
1701 while (p && !p->isWidget())
1702 p = p->parentItem();
1703 return (p && p->isWidget()) ?
static_cast<QGraphicsWidget *>(p) :
nullptr;
1707
1708
1709
1710
1711
1712
1713
1714
1715QGraphicsWidget *QGraphicsItem::topLevelWidget()
const
1717 if (
const QGraphicsWidget *p = parentWidget())
1718 return p->topLevelWidget();
1719 return isWidget() ?
static_cast<QGraphicsWidget *>(
const_cast<QGraphicsItem *>(
this)) :
nullptr;
1723
1724
1725
1726
1727
1728
1729
1730
1731QGraphicsWidget *QGraphicsItem::window()
const
1733 QGraphicsItem *p = panel();
1734 if (p && p->isWindow())
1735 return static_cast<QGraphicsWidget *>(p);
1740
1741
1742
1743
1744
1745
1746
1747
1748QGraphicsItem *QGraphicsItem::panel()
const
1750 if (d_ptr->flags & ItemIsPanel)
1751 return const_cast<QGraphicsItem *>(
this);
1752 return d_ptr->parent ? d_ptr->parent->panel() :
nullptr;
1756
1757
1758
1759
1760
1761QGraphicsObject *QGraphicsItem::toGraphicsObject()
1763 return d_ptr->isObject ?
static_cast<QGraphicsObject *>(
this) :
nullptr;
1767
1768
1769
1770
1771
1772const QGraphicsObject *QGraphicsItem::toGraphicsObject()
const
1774 return d_ptr->isObject ?
static_cast<
const QGraphicsObject *>(
this) :
nullptr;
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791void QGraphicsItem::setParentItem(QGraphicsItem *newParent)
1793 if (newParent ==
this) {
1794 qWarning(
"QGraphicsItem::setParentItem: cannot assign %p as a parent of itself",
this);
1797 if (newParent == d_ptr->parent)
1800 const QVariant newParentVariant(itemChange(QGraphicsItem::ItemParentChange,
1801 QVariant::fromValue<QGraphicsItem *>(newParent)));
1802 newParent = qvariant_cast<QGraphicsItem *>(newParentVariant);
1803 if (newParent == d_ptr->parent)
1806 const QVariant thisPointerVariant(QVariant::fromValue<QGraphicsItem *>(
this));
1807 d_ptr->setParentItemHelper(newParent, &newParentVariant, &thisPointerVariant);
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820QList<QGraphicsItem *> QGraphicsItem::childItems()
const
1822 const_cast<QGraphicsItem *>(
this)->d_ptr->ensureSortedChildren();
1823 return d_ptr->children;
1827
1828
1829
1830
1831bool QGraphicsItem::isWidget()
const
1833 return d_ptr->isWidget;
1837
1838
1839
1840
1841
1842
1843bool QGraphicsItem::isWindow()
const
1845 return d_ptr->isWidget && (
static_cast<
const QGraphicsWidget *>(
this)->windowType() & Qt::Window);
1849
1850
1851
1852
1853
1854bool QGraphicsItem::isPanel()
const
1856 return d_ptr->flags & ItemIsPanel;
1860
1861
1862
1863
1864
1865
1866
1867
1868QGraphicsItem::GraphicsItemFlags QGraphicsItem::flags()
const
1870 return GraphicsItemFlags(d_ptr->flags);
1874
1875
1876
1877
1878
1879void QGraphicsItem::setFlag(GraphicsItemFlag flag,
bool enabled)
1882 setFlags(GraphicsItemFlags(d_ptr->flags) | flag);
1884 setFlags(GraphicsItemFlags(d_ptr->flags) & ~flag);
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902void QGraphicsItem::setFlags(GraphicsItemFlags flags)
1905 if (quint32(d_ptr->flags) == quint32(flags))
1907 flags = GraphicsItemFlags(itemChange(ItemFlagsChange, quint32(flags)).toUInt());
1908 if (quint32(d_ptr->flags) == quint32(flags))
1910 if (d_ptr->scene && d_ptr->scene->d_func()->indexMethod != QGraphicsScene::NoIndex)
1911 d_ptr->scene->d_func()->index->itemChange(
this, ItemFlagsChange, &flags);
1914 const quint32 geomChangeFlagsMask = (ItemClipsChildrenToShape | ItemClipsToShape | ItemIgnoresTransformations | ItemIsSelectable);
1915 bool fullUpdate = (quint32(flags) & geomChangeFlagsMask) != (d_ptr->flags & geomChangeFlagsMask);
1917 d_ptr->updatePaintedViewBoundingRects(
true);
1920 GraphicsItemFlags oldFlags = GraphicsItemFlags(d_ptr->flags);
1923 d_ptr->flags = flags;
1925 if (!(d_ptr->flags & ItemIsFocusable) && hasFocus()) {
1931 if (!(d_ptr->flags & ItemIsSelectable) && isSelected()) {
1937 if ((flags & ItemClipsChildrenToShape) != (oldFlags & ItemClipsChildrenToShape)) {
1940 d_ptr->updateAncestorFlag(ItemClipsChildrenToShape);
1943 d_ptr->dirtyChildrenBoundingRect = 1;
1944 d_ptr->markParentDirty(
true);
1947 if ((flags & ItemContainsChildrenInShape) != (oldFlags & ItemContainsChildrenInShape)) {
1949 d_ptr->updateAncestorFlag(ItemContainsChildrenInShape);
1952 if ((flags & ItemIgnoresTransformations) != (oldFlags & ItemIgnoresTransformations)) {
1955 d_ptr->updateAncestorFlag(ItemIgnoresTransformations);
1958 if ((flags & ItemNegativeZStacksBehindParent) != (oldFlags & ItemNegativeZStacksBehindParent)) {
1964 if (d_ptr->z < qreal(0.0))
1965 flags |= ItemStacksBehindParent;
1967 flags &= ~ItemStacksBehindParent;
1968 d_ptr->flags = flags;
1971 if ((flags & ItemStacksBehindParent) != (oldFlags & ItemStacksBehindParent)) {
1977 d_ptr->parent->d_ptr->needSortChildren = 1;
1978 else if (d_ptr->scene)
1979 d_ptr->scene->d_func()->needSortTopLevelItems = 1;
1982 if ((flags & ItemAcceptsInputMethod) != (oldFlags & ItemAcceptsInputMethod)) {
1985 d_ptr->scene->d_func()->updateInputMethodSensitivityInViews();
1988 if ((flags & ItemIsPanel) != (oldFlags & ItemIsPanel)) {
1989 bool becomesPanel = (flags & ItemIsPanel);
1990 if ((d_ptr->panelModality != NonModal) && d_ptr->scene) {
1993 d_ptr->scene->d_func()->enterModal(
this);
1995 d_ptr->scene->d_func()->leaveModal(
this);
1997 if (d_ptr->isWidget && (becomesPanel || parentWidget())) {
1998 QGraphicsWidget *w =
static_cast<QGraphicsWidget *>(
this);
1999 QGraphicsWidget *focusFirst = w;
2000 QGraphicsWidget *focusLast = w;
2002 QGraphicsWidget *test = focusLast->d_func()->focusNext;
2003 if (!w->isAncestorOf(test) || test == w)
2010 QGraphicsWidget *beforeMe = w->d_func()->focusPrev;
2011 QGraphicsWidget *afterMe = focusLast->d_func()->focusNext;
2012 beforeMe->d_func()->focusNext = afterMe;
2013 afterMe->d_func()->focusPrev = beforeMe;
2014 focusFirst->d_func()->focusPrev = focusLast;
2015 focusLast->d_func()->focusNext = focusFirst;
2016 if (!isAncestorOf(focusFirst->d_func()->focusNext))
2017 focusFirst->d_func()->focusNext = w;
2018 }
else if (QGraphicsWidget *pw = parentWidget()) {
2020 QGraphicsWidget *beforeMe = pw;
2021 QGraphicsWidget *afterMe = pw->d_func()->focusNext;
2022 beforeMe->d_func()->focusNext = w;
2023 afterMe->d_func()->focusPrev = focusLast;
2024 w->d_func()->focusPrev = beforeMe;
2025 focusLast->d_func()->focusNext = afterMe;
2031 if ((flags & ItemSendsScenePositionChanges) != (oldFlags & ItemSendsScenePositionChanges)) {
2032 if (flags & ItemSendsScenePositionChanges)
2033 d_ptr->scene->d_func()->registerScenePosItem(
this);
2035 d_ptr->scene->d_func()->unregisterScenePosItem(
this);
2037 d_ptr->scene->d_func()->markDirty(
this, QRectF(),
true);
2041 itemChange(ItemFlagsHaveChanged, quint32(flags));
2045
2046
2047
2048
2049
2050
2051QGraphicsItem::CacheMode QGraphicsItem::cacheMode()
const
2053 return QGraphicsItem::CacheMode(d_ptr->cacheMode);
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093void QGraphicsItem::setCacheMode(CacheMode mode,
const QSize &logicalCacheSize)
2095 CacheMode lastMode = CacheMode(d_ptr->cacheMode);
2096 d_ptr->cacheMode = mode;
2097 bool noVisualChange = (mode == NoCache && lastMode == NoCache)
2098 || (mode == NoCache && lastMode == DeviceCoordinateCache)
2099 || (mode == DeviceCoordinateCache && lastMode == NoCache)
2100 || (mode == DeviceCoordinateCache && lastMode == DeviceCoordinateCache);
2101 if (mode == NoCache) {
2102 d_ptr->removeExtraItemCache();
2104 QGraphicsItemCache *cache = d_ptr->extraItemCache();
2109 if (mode == ItemCoordinateCache) {
2110 if (lastMode == mode && cache->fixedSize == logicalCacheSize)
2111 noVisualChange =
true;
2112 cache->fixedSize = logicalCacheSize;
2115 if (!noVisualChange)
2120
2121
2122
2123
2124QGraphicsItem::PanelModality QGraphicsItem::panelModality()
const
2126 return d_ptr->panelModality;
2130
2131
2132
2133
2134
2135
2136void QGraphicsItem::setPanelModality(PanelModality panelModality)
2138 if (d_ptr->panelModality == panelModality)
2141 PanelModality previousModality = d_ptr->panelModality;
2142 bool enterLeaveModal = (isPanel() && d_ptr->scene && isVisible());
2143 if (enterLeaveModal && panelModality == NonModal)
2144 d_ptr->scene->d_func()->leaveModal(
this);
2145 d_ptr->panelModality = panelModality;
2146 if (enterLeaveModal && d_ptr->panelModality != NonModal)
2147 d_ptr->scene->d_func()->enterModal(
this, previousModality);
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161bool QGraphicsItem::isBlockedByModalPanel(QGraphicsItem **blockingPanel)
const
2167 QGraphicsItem *dummy =
nullptr;
2169 blockingPanel = &dummy;
2171 const QGraphicsScenePrivate *scene_d = d_ptr->scene->d_func();
2172 if (scene_d->modalPanels.isEmpty())
2176 if (!scene_d->popupWidgets.isEmpty() && scene_d->popupWidgets.first() ==
this)
2179 for (
int i = 0; i < scene_d->modalPanels.size(); ++i) {
2180 QGraphicsItem *modalPanel = scene_d->modalPanels.at(i);
2181 if (modalPanel->panelModality() == QGraphicsItem::SceneModal) {
2183 if (modalPanel !=
this && !modalPanel->isAncestorOf(
this)) {
2184 *blockingPanel = modalPanel;
2189 if (modalPanel !=
this
2190 && !modalPanel->isAncestorOf(
this)
2191 && commonAncestorItem(modalPanel)) {
2192 *blockingPanel = modalPanel;
2200#if QT_CONFIG(tooltip)
2202
2203
2204
2205
2206
2207QString QGraphicsItem::toolTip()
const
2209 return d_ptr->extra(QGraphicsItemPrivate::ExtraToolTip).toString();
2213
2214
2215
2216
2217
2218void QGraphicsItem::setToolTip(
const QString &toolTip)
2220 const QVariant toolTipVariant(itemChange(ItemToolTipChange, toolTip));
2221 d_ptr->setExtra(QGraphicsItemPrivate::ExtraToolTip, toolTipVariant.toString());
2222 itemChange(ItemToolTipHasChanged, toolTipVariant);
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242QCursor QGraphicsItem::cursor()
const
2244 return qvariant_cast<QCursor>(d_ptr->extra(QGraphicsItemPrivate::ExtraCursor));
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262void QGraphicsItem::setCursor(
const QCursor &cursor)
2264 const QVariant cursorVariant(itemChange(ItemCursorChange, QVariant::fromValue<QCursor>(cursor)));
2265 d_ptr->setExtra(QGraphicsItemPrivate::ExtraCursor, qvariant_cast<QCursor>(cursorVariant));
2266 d_ptr->hasCursor = 1;
2268 d_ptr->scene->d_func()->allItemsUseDefaultCursor =
false;
2269 const auto views = d_ptr->scene->views();
2270 for (QGraphicsView *view : views) {
2271 view->viewport()->setMouseTracking(
true);
2273 if (view->underMouse()) {
2274 const QPoint viewPoint = view->mapFromGlobal(QCursor::pos());
2275 const QPointF cursorPos = mapFromScene(view->mapToScene(viewPoint));
2277 if (boundingRect().contains(cursorPos)) {
2278 const auto itemsUnderCursor = view->items(viewPoint);
2279 for (QGraphicsItem *itemUnderCursor : itemsUnderCursor) {
2280 if (itemUnderCursor->hasCursor()) {
2281 QMetaObject::invokeMethod(view,
"_q_setViewportCursor",
2282 Q_ARG(QCursor, itemUnderCursor->cursor()));
2291 itemChange(ItemCursorHasChanged, cursorVariant);
2295
2296
2297
2298
2299
2300
2301
2302bool QGraphicsItem::hasCursor()
const
2304 return d_ptr->hasCursor;
2308
2309
2310
2311
2312void QGraphicsItem::unsetCursor()
2314 if (!d_ptr->hasCursor)
2316 d_ptr->unsetExtra(QGraphicsItemPrivate::ExtraCursor);
2317 d_ptr->hasCursor = 0;
2319 const auto views = d_ptr->scene->views();
2320 for (QGraphicsView *view : views) {
2321 if (view->underMouse() && view->itemAt(view->mapFromGlobal(QCursor::pos())) ==
this) {
2322 QMetaObject::invokeMethod(view,
"_q_unsetViewportCursor");
2332
2333
2334
2335
2336
2337
2338
2339bool QGraphicsItem::isVisible()
const
2341 return d_ptr->visible;
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357bool QGraphicsItem::isVisibleTo(
const QGraphicsItem *parent)
const
2359 const QGraphicsItem *p =
this;
2360 if (d_ptr->explicitlyHidden)
2365 if (p->d_ptr->explicitlyHidden)
2367 }
while ((p = p->d_ptr->parent));
2368 return parent ==
nullptr;
2372
2373
2374
2375
2376
2377void QGraphicsItemPrivate::setVisibleHelper(
bool newVisible,
bool explicitly,
2378 bool update,
bool hiddenByPanel)
2384 explicitlyHidden = newVisible ? 0 : 1;
2387 if (visible == quint32(newVisible))
2391 if (parent && newVisible && !parent->d_ptr->visible)
2395 const QVariant newVisibleVariant(q_ptr->itemChange(QGraphicsItem::ItemVisibleChange,
2396 quint32(newVisible)));
2397 newVisible = newVisibleVariant.toBool();
2398 if (visible == quint32(newVisible))
2400 visible = newVisible;
2404 QGraphicsItemCache *c = (QGraphicsItemCache *)qvariant_cast<
void *>(extra(ExtraCacheData));
2408#if QT_CONFIG(graphicseffect)
2409 invalidateParentGraphicsEffectsRecursively();
2411 scene->d_func()->markDirty(q_ptr, QRectF(),
false,
true);
2416 bool hasFocus = q_ptr->hasFocus();
2419 if (scene->d_func()->mouseGrabberItems.contains(q))
2421 if (scene->d_func()->keyboardGrabberItems.contains(q))
2422 q->ungrabKeyboard();
2423 if (q->isPanel() && panelModality != QGraphicsItem::NonModal)
2424 scene->d_func()->leaveModal(q_ptr);
2426 if (hasFocus && scene) {
2428 QGraphicsItem *focusItem = scene->focusItem();
2430 if (isWidget && !focusItem->isPanel()) {
2432 if (focusItem == q_ptr) {
2433 clear = !
static_cast<QGraphicsWidget *>(q_ptr)->focusNextPrevChild(
true);
2436 }
while ((focusItem = focusItem->parentWidget()) && !focusItem->isPanel());
2439 clearFocusHelper(
false, hiddenByPanel);
2441 if (q_ptr->isSelected())
2442 q_ptr->setSelected(
false);
2444 geometryChanged = 1;
2445 paintedViewBoundingRectsNeedRepaint = 1;
2448 QGraphicsWidget *widget =
static_cast<QGraphicsWidget *>(q_ptr);
2449 if (widget->windowType() == Qt::Popup)
2450 scene->d_func()->addPopup(widget);
2452 if (q->isPanel() && panelModality != QGraphicsItem::NonModal) {
2453 scene->d_func()->enterModal(q_ptr);
2459 const bool updateChildren = update && !((flags & QGraphicsItem::ItemClipsChildrenToShape
2460 || flags & QGraphicsItem::ItemContainsChildrenInShape)
2461 && !(flags & QGraphicsItem::ItemHasNoContents));
2462 for (QGraphicsItem *child : std::as_const(children)) {
2463 if (!newVisible || !child->d_ptr->explicitlyHidden)
2464 child->d_ptr->setVisibleHelper(newVisible,
false, updateChildren, hiddenByPanel);
2468 if (scene && q->isPanel()) {
2470 if (parent && parent->isActive())
2474 scene->setActivePanel(parent);
2482 QGraphicsItem *p = parent;
2485 if (p->flags() & QGraphicsItem::ItemIsFocusScope) {
2486 QGraphicsItem *fsi = p->d_ptr->focusScopeItem;
2487 if (q_ptr == fsi || q_ptr->isAncestorOf(fsi)) {
2489 while (fsi->d_ptr->focusScopeItem && fsi->d_ptr->focusScopeItem->isVisible())
2490 fsi = fsi->d_ptr->focusScopeItem;
2491 fsi->d_ptr->setFocusHelper(Qt::OtherFocusReason,
true,
2496 p = p->d_ptr->parent;
2499 QGraphicsItem *fi = subFocusItem;
2500 if (fi && fi != scene->focusItem()) {
2501 scene->setFocusItem(fi);
2502 }
else if (flags & QGraphicsItem::ItemIsFocusScope &&
2503 !scene->focusItem() &&
2504 q->isAncestorOf(scene->d_func()->lastFocusItem)) {
2511 QGraphicsItem *p = parent;
2513 if (p->flags() & QGraphicsItem::ItemIsFocusScope) {
2514 if (p->d_ptr->visible) {
2515 p->d_ptr->setFocusHelper(Qt::OtherFocusReason,
true,
2520 p = p->d_ptr->parent;
2527 q_ptr->itemChange(QGraphicsItem::ItemVisibleHasChanged, newVisibleVariant);
2530 emit
static_cast<QGraphicsObject *>(q_ptr)->visibleChanged();
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565void QGraphicsItem::setVisible(
bool visible)
2567 d_ptr->setVisibleHelper(visible,
2574
2575
2576
2577
2578
2579
2580
2581
2584
2585
2586
2587
2588
2589
2590
2591
2594
2595
2596
2597
2598bool QGraphicsItem::isEnabled()
const
2600 return d_ptr->enabled;
2604
2605
2606
2607
2608
2609void QGraphicsItemPrivate::setEnabledHelper(
bool newEnabled,
bool explicitly,
bool update)
2613 explicitlyDisabled = newEnabled ? 0 : 1;
2616 if (enabled == quint32(newEnabled))
2622 if (scene->mouseGrabberItem() == q_ptr)
2623 q_ptr->ungrabMouse();
2624 if (q_ptr->hasFocus()) {
2627 QGraphicsItem *focusItem = scene->focusItem();
2629 if (isWidget && !focusItem->isPanel() && q_ptr->isAncestorOf(focusItem)) {
2631 if (focusItem == q_ptr) {
2632 clear = !
static_cast<QGraphicsWidget *>(q_ptr)->focusNextPrevChild(
true);
2635 }
while ((focusItem = focusItem->parentWidget()) && !focusItem->isPanel());
2638 q_ptr->clearFocus();
2641 if (q_ptr->isSelected())
2642 q_ptr->setSelected(
false);
2646 const QVariant newEnabledVariant(q_ptr->itemChange(QGraphicsItem::ItemEnabledChange,
2647 quint32(newEnabled)));
2648 enabled = newEnabledVariant.toBool();
2654 for (QGraphicsItem *child : std::as_const(children)) {
2655 if (!newEnabled || !child->d_ptr->explicitlyDisabled)
2656 child->d_ptr->setEnabledHelper(newEnabled,
false);
2660 q_ptr->itemChange(QGraphicsItem::ItemEnabledHasChanged, newEnabledVariant);
2663 emit
static_cast<QGraphicsObject *>(q_ptr)->enabledChanged();
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694void QGraphicsItem::setEnabled(
bool enabled)
2696 d_ptr->setEnabledHelper(enabled,
true);
2700
2701
2702
2703
2704
2705
2706
2707
2708bool QGraphicsItem::isSelected()
const
2710 if (QGraphicsItemGroup *group =
this->group())
2711 return group->isSelected();
2712 return d_ptr->selected;
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739void QGraphicsItem::setSelected(
bool selected)
2741 if (QGraphicsItemGroup *group =
this->group()) {
2742 group->setSelected(selected);
2746 if (!(d_ptr->flags & ItemIsSelectable) || !d_ptr->enabled || !d_ptr->visible)
2748 if (d_ptr->selected == selected)
2750 const QVariant newSelectedVariant(itemChange(ItemSelectedChange, quint32(selected)));
2751 bool newSelected = newSelectedVariant.toBool();
2752 if (d_ptr->selected == newSelected)
2754 d_ptr->selected = newSelected;
2758 QGraphicsScenePrivate *sceneD = d_ptr->scene->d_func();
2760 sceneD->selectedItems <<
this;
2765 if (!sceneD->selectionChanging)
2766 emit d_ptr->scene->selectionChanged();
2770 itemChange(QGraphicsItem::ItemSelectedHasChanged, newSelectedVariant);
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792qreal QGraphicsItem::opacity()
const
2794 return d_ptr->opacity;
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808qreal QGraphicsItem::effectiveOpacity()
const
2810 return d_ptr->effectiveOpacity();
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839void QGraphicsItem::setOpacity(qreal opacity)
2842 const QVariant newOpacityVariant(itemChange(ItemOpacityChange, opacity));
2845 qreal newOpacity = qBound(qreal(0), newOpacityVariant.toReal(), qreal(1));
2848 if (newOpacity == d_ptr->opacity)
2851 bool wasFullyTransparent = d_ptr->isOpacityNull();
2852 d_ptr->opacity = newOpacity;
2855 itemChange(ItemOpacityHasChanged, newOpacityVariant);
2859#if QT_CONFIG(graphicseffect)
2860 d_ptr->invalidateParentGraphicsEffectsRecursively();
2861 if (!(d_ptr->flags & ItemDoesntPropagateOpacityToChildren))
2862 d_ptr->invalidateChildGraphicsEffectsRecursively(QGraphicsItemPrivate::OpacityChanged);
2864 d_ptr->scene->d_func()->markDirty(
this, QRectF(),
2867 d_ptr->isOpacityNull());
2868 if (wasFullyTransparent)
2869 d_ptr->paintedViewBoundingRectsNeedRepaint = 1;
2872 if (d_ptr->isObject)
2873 emit
static_cast<QGraphicsObject *>(
this)->opacityChanged();
2877
2878
2879
2880
2881#if QT_CONFIG(graphicseffect)
2882QGraphicsEffect *QGraphicsItem::graphicsEffect()
const
2884 return d_ptr->graphicsEffect;
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902void QGraphicsItem::setGraphicsEffect(QGraphicsEffect *effect)
2904 if (d_ptr->graphicsEffect == effect)
2907 if (d_ptr->graphicsEffect) {
2908 delete d_ptr->graphicsEffect;
2909 d_ptr->graphicsEffect =
nullptr;
2910 }
else if (d_ptr->parent) {
2911 d_ptr->parent->d_ptr->updateChildWithGraphicsEffectFlagRecursively();
2916 QGraphicsEffectSourcePrivate *sourced =
new QGraphicsItemEffectSourcePrivate(
this);
2917 QGraphicsEffectSource *source =
new QGraphicsEffectSource(*sourced);
2918 d_ptr->graphicsEffect = effect;
2919 effect->d_func()->setGraphicsEffectSource(source);
2920 prepareGeometryChange();
2925void QGraphicsItemPrivate::updateChildWithGraphicsEffectFlagRecursively()
2927#if QT_CONFIG(graphicseffect)
2928 QGraphicsItemPrivate *itemPrivate =
this;
2931 if (itemPrivate->mayHaveChildWithGraphicsEffect)
2933 itemPrivate->mayHaveChildWithGraphicsEffect = 1;
2934 }
while ((itemPrivate = itemPrivate->parent ? itemPrivate->parent->d_ptr.data() :
nullptr));
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948QRectF QGraphicsItemPrivate::effectiveBoundingRect(
const QRectF &rect)
const
2950#if QT_CONFIG(graphicseffect)
2951 Q_Q(
const QGraphicsItem);
2952 QGraphicsEffect *effect = graphicsEffect;
2953 if (scene && effect && effect->isEnabled()) {
2954 if (scene->d_func()->views.isEmpty())
2955 return effect->boundingRectFor(rect);
2956 QRectF sceneRect = q->mapRectToScene(rect);
2957 QRectF sceneEffectRect;
2958 const auto views = scene->views();
2959 for (QGraphicsView *view : views) {
2960 QRectF deviceRect = view->d_func()->mapRectFromScene(sceneRect);
2961 QRect deviceEffectRect = effect->boundingRectFor(deviceRect).toAlignedRect();
2962 sceneEffectRect |= view->d_func()->mapRectToScene(deviceEffectRect);
2964 return q->mapRectFromScene(sceneEffectRect);
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980QRectF QGraphicsItemPrivate::effectiveBoundingRect(QGraphicsItem *topMostEffectItem)
const
2982#if QT_CONFIG(graphicseffect)
2983 Q_Q(
const QGraphicsItem);
2984 QRectF brect = effectiveBoundingRect(q_ptr->boundingRect());
2985 if (ancestorFlags & QGraphicsItemPrivate::AncestorClipsChildren
2986 || ancestorFlags & QGraphicsItemPrivate::AncestorContainsChildren
2987 || topMostEffectItem == q)
2990 const QGraphicsItem *effectParent = parent;
2991 while (effectParent) {
2992 QGraphicsEffect *effect = effectParent->d_ptr->graphicsEffect;
2993 if (scene && effect && effect->isEnabled()) {
2994 const QRectF brectInParentSpace = q->mapRectToItem(effectParent, brect);
2995 const QRectF effectRectInParentSpace = effectParent->d_ptr->effectiveBoundingRect(brectInParentSpace);
2996 brect = effectParent->mapRectToItem(q, effectRectInParentSpace);
2998 if (effectParent->d_ptr->ancestorFlags & QGraphicsItemPrivate::AncestorClipsChildren
2999 || effectParent->d_ptr->ancestorFlags & QGraphicsItemPrivate::AncestorContainsChildren
3000 || topMostEffectItem == effectParent) {
3003 effectParent = effectParent->d_ptr->parent;
3008 Q_UNUSED(topMostEffectItem);
3009 return q_ptr->boundingRect();
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025QRectF QGraphicsItemPrivate::sceneEffectiveBoundingRect()
const
3030 const QGraphicsItem *parentItem = q_ptr;
3031 const QGraphicsItemPrivate *itemd;
3033 itemd = parentItem->d_ptr.data();
3034 if (itemd->transformData)
3036 offset += itemd->pos;
3037 }
while ((parentItem = itemd->parent));
3039 QRectF br = effectiveBoundingRect();
3040 br.translate(offset);
3041 return !parentItem ? br : parentItem->sceneTransform().mapRect(br);
3045
3046
3047
3048
3049
3050
3051bool QGraphicsItem::acceptDrops()
const
3053 return d_ptr->acceptDrops;
3057
3058
3059
3060
3061
3062
3063void QGraphicsItem::setAcceptDrops(
bool on)
3065 d_ptr->acceptDrops = on;
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080Qt::MouseButtons QGraphicsItem::acceptedMouseButtons()
const
3082 return Qt::MouseButtons(d_ptr->acceptedMouseButtons);
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099void QGraphicsItem::setAcceptedMouseButtons(Qt::MouseButtons buttons)
3101 if (Qt::MouseButtons(d_ptr->acceptedMouseButtons) != buttons) {
3102 if (buttons == 0 && d_ptr->scene && d_ptr->scene->mouseGrabberItem() ==
this
3103 && d_ptr->scene->d_func()->lastMouseGrabberItemHasImplicitMouseGrab) {
3106 d_ptr->acceptedMouseButtons = quint32(buttons);
3111
3112
3113
3114
3115
3116
3117
3118
3119bool QGraphicsItem::acceptHoverEvents()
const
3121 return d_ptr->acceptsHover;
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156void QGraphicsItem::setAcceptHoverEvents(
bool enabled)
3158 if (d_ptr->acceptsHover == quint32(enabled))
3160 d_ptr->acceptsHover = quint32(enabled);
3161 if (d_ptr->acceptsHover && d_ptr->scene && d_ptr->scene->d_func()->allItemsIgnoreHoverEvents) {
3162 d_ptr->scene->d_func()->allItemsIgnoreHoverEvents =
false;
3163 d_ptr->scene->d_func()->enableMouseTrackingOnViews();
3168
3169
3170
3171
3172
3173
3174bool QGraphicsItem::acceptTouchEvents()
const
3176 return d_ptr->acceptTouchEvents;
3180
3181
3182
3183
3184
3185
3186void QGraphicsItem::setAcceptTouchEvents(
bool enabled)
3188 if (d_ptr->acceptTouchEvents == quint32(enabled))
3190 d_ptr->acceptTouchEvents = quint32(enabled);
3191 if (d_ptr->acceptTouchEvents && d_ptr->scene && d_ptr->scene->d_func()->allItemsIgnoreTouchEvents) {
3192 d_ptr->scene->d_func()->allItemsIgnoreTouchEvents =
false;
3193 d_ptr->scene->d_func()->enableTouchEventsOnViews();
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208bool QGraphicsItem::filtersChildEvents()
const
3210 return d_ptr->filtersDescendantEvents;
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224void QGraphicsItem::setFiltersChildEvents(
bool enabled)
3226 if (d_ptr->filtersDescendantEvents == enabled)
3229 d_ptr->filtersDescendantEvents = enabled;
3230 d_ptr->updateAncestorFlag(QGraphicsItem::GraphicsItemFlag(-2));
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250bool QGraphicsItem::handlesChildEvents()
const
3252 return d_ptr->handlesChildEvents;
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275void QGraphicsItem::setHandlesChildEvents(
bool enabled)
3277 if (d_ptr->handlesChildEvents == enabled)
3280 d_ptr->handlesChildEvents = enabled;
3281 d_ptr->updateAncestorFlag(QGraphicsItem::GraphicsItemFlag(-1));
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298bool QGraphicsItem::isActive()
const
3300 if (!d_ptr->scene || !d_ptr->scene->isActive())
3302 return panel() == d_ptr->scene->activePanel();
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319void QGraphicsItem::setActive(
bool active)
3321 d_ptr->explicitActivate = 1;
3322 d_ptr->wantsActive = active;
3326 d_ptr->scene->setActivePanel(
this);
3328 QGraphicsItem *activePanel = d_ptr->scene->activePanel();
3329 QGraphicsItem *thisPanel = panel();
3330 if (!activePanel || activePanel == thisPanel) {
3333 QGraphicsItem *nextToActivate =
nullptr;
3335 nextToActivate = d_ptr->parent->panel();
3336 if (!nextToActivate)
3337 nextToActivate = d_ptr->scene->d_func()->lastActivePanel;
3338 if (nextToActivate ==
this || isAncestorOf(nextToActivate))
3339 nextToActivate =
nullptr;
3340 d_ptr->scene->setActivePanel(nextToActivate);
3347
3348
3349
3350
3351
3352bool QGraphicsItem::hasFocus()
const
3354 if (!d_ptr->scene || !d_ptr->scene->isActive())
3357 if (d_ptr->focusProxy)
3358 return d_ptr->focusProxy->hasFocus();
3360 if (d_ptr->scene->d_func()->focusItem !=
this)
3363 return panel() == d_ptr->scene->d_func()->activePanel;
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386void QGraphicsItem::setFocus(Qt::FocusReason focusReason)
3388 d_ptr->setFocusHelper(focusReason,
true,
false);
3392
3393
3394void QGraphicsItemPrivate::setFocusHelper(Qt::FocusReason focusReason,
bool climb,
bool focusFromHide)
3397 if (!q_ptr->isEnabled() || !(flags & QGraphicsItem::ItemIsFocusable))
3401 QGraphicsItem *f = q_ptr;
3402 while (f->d_ptr->focusProxy)
3403 f = f->d_ptr->focusProxy;
3406 if (scene && scene->focusItem() == f)
3410 QGraphicsItem *p = parent;
3412 if (p->flags() & QGraphicsItem::ItemIsFocusScope) {
3413 QGraphicsItem *oldFocusScopeItem = p->d_ptr->focusScopeItem;
3414 p->d_ptr->focusScopeItem = q_ptr;
3415 if (oldFocusScopeItem)
3416 oldFocusScopeItem->d_ptr->focusScopeItemChange(
false);
3417 focusScopeItemChange(
true);
3418 if (!p->focusItem() && !focusFromHide) {
3427 p = p->d_ptr->parent;
3431 while (f->d_ptr->focusScopeItem && f->d_ptr->focusScopeItem->isVisible())
3432 f = f->d_ptr->focusScopeItem;
3436 QGraphicsItem *commonAncestor =
nullptr;
3437 if (scene && scene->focusItem() && scene->focusItem()->panel() == q_ptr->panel()) {
3438 commonAncestor = scene->focusItem()->commonAncestorItem(f);
3439 scene->focusItem()->d_ptr->clearSubFocus(scene->focusItem(), commonAncestor);
3442 f->d_ptr->setSubFocus(f, commonAncestor);
3446 QGraphicsItem *p = q_ptr->panel();
3447 if ((!p && scene->isActive()) || (p && p->isActive())) {
3449 scene->d_func()->setFocusItemHelper(f, focusReason);
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465void QGraphicsItem::clearFocus()
3467 d_ptr->clearFocusHelper(
true,
3472
3473
3474void QGraphicsItemPrivate::clearFocusHelper(
bool giveFocusToParent,
bool hiddenByParentPanel)
3476 QGraphicsItem *subFocusItem = q_ptr;
3477 if (flags & QGraphicsItem::ItemIsFocusScope) {
3478 while (subFocusItem->d_ptr->focusScopeItem)
3479 subFocusItem = subFocusItem->d_ptr->focusScopeItem;
3482 if (giveFocusToParent) {
3484 if (!inDestructor) {
3485 QGraphicsItem *p = parent;
3487 if (p->flags() & QGraphicsItem::ItemIsFocusScope) {
3488 if (p->d_ptr->focusScopeItem == q_ptr) {
3489 p->d_ptr->focusScopeItem =
nullptr;
3490 if (!subFocusItem->hasFocus())
3491 focusScopeItemChange(
false);
3493 if (subFocusItem->hasFocus())
3494 p->d_ptr->setFocusHelper(Qt::OtherFocusReason,
false,
3498 p = p->d_ptr->parent;
3503 if (subFocusItem->hasFocus()) {
3505 if (!hiddenByParentPanel)
3506 clearSubFocus(q_ptr);
3509 scene->setFocusItem(
nullptr);
3514
3515
3516
3517
3518
3519
3520
3521QGraphicsItem *QGraphicsItem::focusProxy()
const
3523 return d_ptr->focusProxy;
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545void QGraphicsItem::setFocusProxy(QGraphicsItem *item)
3547 if (item == d_ptr->focusProxy)
3550 qWarning(
"QGraphicsItem::setFocusProxy: cannot assign self as focus proxy");
3554 if (item->d_ptr->scene != d_ptr->scene) {
3555 qWarning(
"QGraphicsItem::setFocusProxy: focus proxy must be in same scene");
3558 for (QGraphicsItem *f = item->focusProxy(); f !=
nullptr; f = f->focusProxy()) {
3560 qWarning(
"QGraphicsItem::setFocusProxy: %p is already in the focus proxy chain", item);
3566 QGraphicsItem *lastFocusProxy = d_ptr->focusProxy;
3568 lastFocusProxy->d_ptr->focusProxyRefs.removeOne(&d_ptr->focusProxy);
3569 d_ptr->focusProxy = item;
3571 item->d_ptr->focusProxyRefs << &d_ptr->focusProxy;
3575
3576
3577
3578
3579
3580
3581
3582
3583QGraphicsItem *QGraphicsItem::focusItem()
const
3585 return d_ptr->subFocusItem;
3589
3590
3591
3592
3593QGraphicsItem *QGraphicsItem::focusScopeItem()
const
3595 return d_ptr->focusScopeItem;
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632void QGraphicsItem::grabMouse()
3634 if (!d_ptr->scene) {
3635 qWarning(
"QGraphicsItem::grabMouse: cannot grab mouse without scene");
3638 if (!d_ptr->visible) {
3639 qWarning(
"QGraphicsItem::grabMouse: cannot grab mouse while invisible");
3642 d_ptr->scene->d_func()->grabMouse(
this);
3646
3647
3648
3649
3650
3651void QGraphicsItem::ungrabMouse()
3653 if (!d_ptr->scene) {
3654 qWarning(
"QGraphicsItem::ungrabMouse: cannot ungrab mouse without scene");
3657 d_ptr->scene->d_func()->ungrabMouse(
this);
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694void QGraphicsItem::grabKeyboard()
3696 if (!d_ptr->scene) {
3697 qWarning(
"QGraphicsItem::grabKeyboard: cannot grab keyboard without scene");
3700 if (!d_ptr->visible) {
3701 qWarning(
"QGraphicsItem::grabKeyboard: cannot grab keyboard while invisible");
3704 d_ptr->scene->d_func()->grabKeyboard(
this);
3708
3709
3710
3711
3712
3713void QGraphicsItem::ungrabKeyboard()
3715 if (!d_ptr->scene) {
3716 qWarning(
"QGraphicsItem::ungrabKeyboard: cannot ungrab keyboard without scene");
3719 d_ptr->scene->d_func()->ungrabKeyboard(
this);
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735QPointF QGraphicsItem::pos()
const
3741
3742
3743
3744
3745
3746
3749
3750
3751
3752
3753
3754
3755
3756void QGraphicsItem::setX(qreal x)
3758 if (d_ptr->inDestructor)
3764 setPos(QPointF(x, d_ptr->pos.y()));
3768
3769
3770
3771
3772
3773
3776
3777
3778
3779
3780
3781
3782
3783void QGraphicsItem::setY(qreal y)
3785 if (d_ptr->inDestructor)
3791 setPos(QPointF(d_ptr->pos.x(), y));
3795
3796
3797
3798
3799
3800QPointF QGraphicsItem::scenePos()
const
3802 return mapToScene(0, 0);
3806
3807
3808
3809
3810void QGraphicsItemPrivate::setPosHelper(
const QPointF &pos)
3815 q->prepareGeometryChange();
3816 QPointF oldPos =
this->pos;
3818 dirtySceneTransform = 1;
3821 if (pos.x() != oldPos.x())
3822 emit
static_cast<QGraphicsObject *>(q_ptr)->xChanged();
3823 if (pos.y() != oldPos.y())
3824 emit
static_cast<QGraphicsObject *>(q_ptr)->yChanged();
3829
3830
3831
3832
3833void QGraphicsItemPrivate::setTransformHelper(
const QTransform &transform)
3835 q_ptr->prepareGeometryChange();
3836 transformData->transform = transform;
3837 dirtySceneTransform = 1;
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851void QGraphicsItem::setPos(
const QPointF &pos)
3853 if (d_ptr->pos == pos)
3856 if (d_ptr->inDestructor)
3860 if (!(d_ptr->flags & (ItemSendsGeometryChanges | ItemSendsScenePositionChanges))) {
3861 d_ptr->setPosHelper(pos);
3862 if (d_ptr->isWidget)
3863 static_cast<QGraphicsWidget *>(
this)->d_func()->setGeometryFromSetPos();
3864 if (d_ptr->scenePosDescendants)
3865 d_ptr->sendScenePosChange();
3870 const QVariant newPosVariant(itemChange(ItemPositionChange, QVariant::fromValue<QPointF>(pos)));
3871 QPointF newPos = newPosVariant.toPointF();
3872 if (newPos == d_ptr->pos)
3876 d_ptr->setPosHelper(newPos);
3879 itemChange(QGraphicsItem::ItemPositionHasChanged, newPosVariant);
3880 d_ptr->sendScenePosChange();
3884
3885
3886
3887
3888
3889
3892
3893
3894
3895
3896
3897
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913void QGraphicsItem::ensureVisible(
const QRectF &rect,
int xmargin,
int ymargin)
3918 sceneRect = sceneTransform().mapRect(rect);
3920 sceneRect = sceneBoundingRect();
3921 for (QGraphicsView *view : std::as_const(d_ptr->scene->d_func()->views))
3922 view->ensureVisible(sceneRect, xmargin, ymargin);
3927
3928
3929
3930
3931
3932
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946QTransform QGraphicsItem::transform()
const
3948 if (!d_ptr->transformData)
3949 return QTransform();
3950 return d_ptr->transformData->transform;
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964qreal QGraphicsItem::rotation()
const
3966 if (!d_ptr->transformData)
3968 return d_ptr->transformData->rotation;
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990void QGraphicsItem::setRotation(qreal angle)
3992 prepareGeometryChange();
3993 qreal newRotation = angle;
3995 if (d_ptr->flags & ItemSendsGeometryChanges) {
3997 const QVariant newRotationVariant(itemChange(ItemRotationChange, angle));
3998 newRotation = newRotationVariant.toReal();
4001 if (!d_ptr->transformData)
4002 d_ptr->transformData =
new QGraphicsItemPrivate::TransformData;
4004 if (d_ptr->transformData->rotation == newRotation)
4007 d_ptr->transformData->rotation = newRotation;
4008 d_ptr->transformData->onlyTransform =
false;
4009 d_ptr->dirtySceneTransform = 1;
4012 if (d_ptr->flags & ItemSendsGeometryChanges)
4013 itemChange(ItemRotationHasChanged, newRotation);
4015 if (d_ptr->isObject)
4016 emit
static_cast<QGraphicsObject *>(
this)->rotationChanged();
4018 d_ptr->transformChanged();
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032qreal QGraphicsItem::scale()
const
4034 if (!d_ptr->transformData)
4036 return d_ptr->transformData->scale;
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056void QGraphicsItem::setScale(qreal factor)
4058 prepareGeometryChange();
4059 qreal newScale = factor;
4061 if (d_ptr->flags & ItemSendsGeometryChanges) {
4063 const QVariant newScaleVariant(itemChange(ItemScaleChange, factor));
4064 newScale = newScaleVariant.toReal();
4067 if (!d_ptr->transformData)
4068 d_ptr->transformData =
new QGraphicsItemPrivate::TransformData;
4070 if (d_ptr->transformData->scale == newScale)
4073 d_ptr->transformData->scale = newScale;
4074 d_ptr->transformData->onlyTransform =
false;
4075 d_ptr->dirtySceneTransform = 1;
4078 if (d_ptr->flags & ItemSendsGeometryChanges)
4079 itemChange(ItemScaleHasChanged, newScale);
4081 if (d_ptr->isObject)
4082 emit
static_cast<QGraphicsObject *>(
this)->scaleChanged();
4084 d_ptr->transformChanged();
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103QList<QGraphicsTransform *> QGraphicsItem::transformations()
const
4105 if (!d_ptr->transformData)
4106 return QList<QGraphicsTransform *>();
4107 return d_ptr->transformData->graphicsTransforms;
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130void QGraphicsItem::setTransformations(
const QList<QGraphicsTransform *> &transformations)
4132 prepareGeometryChange();
4133 if (!d_ptr->transformData)
4134 d_ptr->transformData =
new QGraphicsItemPrivate::TransformData;
4135 d_ptr->transformData->graphicsTransforms = transformations;
4136 for (
int i = 0; i < transformations.size(); ++i)
4137 transformations.at(i)->d_func()->setItem(
this);
4138 d_ptr->transformData->onlyTransform =
false;
4139 d_ptr->dirtySceneTransform = 1;
4140 d_ptr->transformChanged();
4144
4145
4146void QGraphicsItemPrivate::prependGraphicsTransform(QGraphicsTransform *t)
4149 transformData =
new QGraphicsItemPrivate::TransformData;
4150 if (!transformData->graphicsTransforms.contains(t))
4151 transformData->graphicsTransforms.prepend(t);
4154 t->d_func()->setItem(q);
4155 transformData->onlyTransform =
false;
4156 dirtySceneTransform = 1;
4161
4162
4163void QGraphicsItemPrivate::appendGraphicsTransform(QGraphicsTransform *t)
4166 transformData =
new QGraphicsItemPrivate::TransformData;
4167 if (!transformData->graphicsTransforms.contains(t))
4168 transformData->graphicsTransforms.append(t);
4171 t->d_func()->setItem(q);
4172 transformData->onlyTransform =
false;
4173 dirtySceneTransform = 1;
4178
4179
4180
4181
4182
4183
4184
4185
4186QPointF QGraphicsItem::transformOriginPoint()
const
4188 if (!d_ptr->transformData)
4189 return QPointF(0,0);
4190 return QPointF(d_ptr->transformData->xOrigin, d_ptr->transformData->yOrigin);
4194
4195
4196
4197
4198
4199
4200void QGraphicsItem::setTransformOriginPoint(
const QPointF &origin)
4202 prepareGeometryChange();
4203 QPointF newOrigin = origin;
4205 if (d_ptr->flags & ItemSendsGeometryChanges) {
4207 const QVariant newOriginVariant(itemChange(ItemTransformOriginPointChange,
4208 QVariant::fromValue<QPointF>(origin)));
4209 newOrigin = newOriginVariant.toPointF();
4212 if (!d_ptr->transformData)
4213 d_ptr->transformData =
new QGraphicsItemPrivate::TransformData;
4215 if (d_ptr->transformData->xOrigin == newOrigin.x()
4216 && d_ptr->transformData->yOrigin == newOrigin.y()) {
4220 d_ptr->transformData->xOrigin = newOrigin.x();
4221 d_ptr->transformData->yOrigin = newOrigin.y();
4222 d_ptr->transformData->onlyTransform =
false;
4223 d_ptr->dirtySceneTransform = 1;
4226 if (d_ptr->flags & ItemSendsGeometryChanges)
4227 itemChange(ItemTransformOriginPointHasChanged, QVariant::fromValue<QPointF>(newOrigin));
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260QTransform QGraphicsItem::sceneTransform()
const
4262 d_ptr->ensureSceneTransform();
4263 return d_ptr->sceneTransform;
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289QTransform QGraphicsItem::deviceTransform(
const QTransform &viewportTransform)
const
4292 if (!d_ptr->itemIsUntransformable()) {
4293 d_ptr->ensureSceneTransform();
4294 return d_ptr->sceneTransform * viewportTransform;
4298 const QGraphicsItem *untransformedAncestor =
this;
4299 QList<
const QGraphicsItem *> parents;
4300 while (untransformedAncestor && ((untransformedAncestor->d_ptr->ancestorFlags
4301 & QGraphicsItemPrivate::AncestorIgnoresTransformations))) {
4302 parents.prepend(untransformedAncestor);
4303 untransformedAncestor = untransformedAncestor->parentItem();
4306 if (!untransformedAncestor) {
4308 Q_ASSERT_X(untransformedAncestor,
"QGraphicsItem::deviceTransform",
4309 "Invalid object structure!");
4310 return QTransform();
4316 QGraphicsItem *parentOfUntransformedAncestor = untransformedAncestor->parentItem();
4317 QTransform inheritedMatrix;
4318 if (parentOfUntransformedAncestor)
4319 inheritedMatrix = parentOfUntransformedAncestor->sceneTransform();
4320 QPointF mappedPoint = (inheritedMatrix * viewportTransform).map(untransformedAncestor->pos());
4323 QTransform matrix = QTransform::fromTranslate(mappedPoint.x(), mappedPoint.y());
4324 if (untransformedAncestor->d_ptr->transformData)
4325 matrix = untransformedAncestor->d_ptr->transformData->computedFullTransform(&matrix);
4328 for (
int i = 0; i < parents.size(); ++i) {
4329 const QGraphicsItem *parent = parents.at(i);
4330 parent->d_ptr->combineTransformFromParent(&matrix);
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353QTransform QGraphicsItem::itemTransform(
const QGraphicsItem *other,
bool *ok)
const
4356 if (other ==
nullptr) {
4357 qWarning(
"QGraphicsItem::itemTransform: null pointer passed");
4358 return QTransform();
4360 if (other ==
this) {
4363 return QTransform();
4366 QGraphicsItem *parent = d_ptr->parent;
4367 const QGraphicsItem *otherParent = other->d_ptr->parent;
4370 if (parent == other) {
4374 d_ptr->combineTransformFromParent(&x);
4379 if (otherParent ==
this) {
4380 const QPointF &otherPos = other->d_ptr->pos;
4381 if (other->d_ptr->transformData) {
4382 QTransform otherToParent;
4383 other->d_ptr->combineTransformFromParent(&otherToParent);
4384 return otherToParent.inverted(ok);
4388 return QTransform::fromTranslate(-otherPos.x(), -otherPos.y());
4392 if (parent == otherParent) {
4394 const QPointF &itemPos = d_ptr->pos;
4395 const QPointF &otherPos = other->d_ptr->pos;
4396 if (!d_ptr->transformData && !other->d_ptr->transformData) {
4397 QPointF delta = itemPos - otherPos;
4400 return QTransform::fromTranslate(delta.x(), delta.y());
4403 QTransform itemToParent;
4404 d_ptr->combineTransformFromParent(&itemToParent);
4405 QTransform otherToParent;
4406 other->d_ptr->combineTransformFromParent(&otherToParent);
4407 return itemToParent * otherToParent.inverted(ok);
4412 const QGraphicsItem *commonAncestor = commonAncestorItem(other);
4413 if (!commonAncestor) {
4414 d_ptr->ensureSceneTransform();
4415 other->d_ptr->ensureSceneTransform();
4416 return d_ptr->sceneTransform * other->d_ptr->sceneTransform.inverted(ok);
4421 bool cousins = other != commonAncestor &&
this != commonAncestor;
4424 QTransform thisToScene = itemTransform(commonAncestor, &good);
4425 QTransform otherToScene(Qt::Uninitialized);
4427 otherToScene = other->itemTransform(commonAncestor, &good);
4431 return QTransform();
4433 return thisToScene * otherToScene.inverted(ok);
4437 bool parentOfOther = isAncestorOf(other);
4438 const QGraphicsItem *child = parentOfOther ? other :
this;
4439 const QGraphicsItem *root = parentOfOther ?
this : other;
4442 const QGraphicsItem *p = child;
4444 p->d_ptr.data()->combineTransformToParent(&x);
4445 }
while ((p = p->d_ptr->parent) && p != root);
4447 return x.inverted(ok);
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474void QGraphicsItem::setTransform(
const QTransform &matrix,
bool combine)
4476 if (!d_ptr->transformData)
4477 d_ptr->transformData =
new QGraphicsItemPrivate::TransformData;
4479 QTransform newTransform(combine ? matrix * d_ptr->transformData->transform : matrix);
4480 if (d_ptr->transformData->transform == newTransform)
4484 if (!(d_ptr->flags & (ItemSendsGeometryChanges | ItemSendsScenePositionChanges))) {
4485 d_ptr->setTransformHelper(newTransform);
4486 if (d_ptr->scenePosDescendants)
4487 d_ptr->sendScenePosChange();
4492 const QVariant newTransformVariant(itemChange(ItemTransformChange,
4493 QVariant::fromValue<QTransform>(newTransform)));
4494 newTransform = qvariant_cast<QTransform>(newTransformVariant);
4495 if (d_ptr->transformData->transform == newTransform)
4499 d_ptr->setTransformHelper(newTransform);
4502 itemChange(ItemTransformHasChanged, newTransformVariant);
4503 d_ptr->sendScenePosChange();
4507
4508
4509
4510
4511
4512
4513
4514
4515void QGraphicsItem::resetTransform()
4517 setTransform(QTransform(),
false);
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536void QGraphicsItem::advance(
int phase)
4542
4543
4544
4545
4546
4547
4548
4549qreal QGraphicsItem::zValue()
const
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568void QGraphicsItem::setZValue(qreal z)
4570 const QVariant newZVariant(itemChange(ItemZValueChange, z));
4571 qreal newZ = newZVariant.toReal();
4572 if (newZ == d_ptr->z)
4575 if (d_ptr->scene && d_ptr->scene->d_func()->indexMethod != QGraphicsScene::NoIndex) {
4577 d_ptr->scene->d_func()->index->itemChange(
this, ItemZValueChange, &newZ);
4582 d_ptr->parent->d_ptr->needSortChildren = 1;
4583 else if (d_ptr->scene)
4584 d_ptr->scene->d_func()->needSortTopLevelItems = 1;
4587 d_ptr->scene->d_func()->markDirty(
this, QRectF(),
true);
4589 itemChange(ItemZValueHasChanged, newZVariant);
4591 if (d_ptr->flags & ItemNegativeZStacksBehindParent)
4592 setFlag(QGraphicsItem::ItemStacksBehindParent, z < qreal(0.0));
4594 if (d_ptr->isObject)
4595 emit
static_cast<QGraphicsObject *>(
this)->zChanged();
4599
4600
4601
4602
4603
4604
4605
4606
4607void QGraphicsItemPrivate::ensureSequentialSiblingIndex()
4609 if (!sequentialOrdering) {
4610 std::sort(children.begin(), children.end(), insertionOrder);
4611 sequentialOrdering = 1;
4612 needSortChildren = 1;
4614 if (holesInSiblingIndex) {
4615 holesInSiblingIndex = 0;
4616 for (
int i = 0; i < children.size(); ++i)
4617 children[i]->d_ptr->siblingIndex = i;
4622
4623
4624inline void QGraphicsItemPrivate::sendScenePosChange()
4628 if (flags & QGraphicsItem::ItemSendsScenePositionChanges)
4629 q->itemChange(QGraphicsItem::ItemScenePositionHasChanged, q->scenePos());
4630 if (scenePosDescendants) {
4631 for (QGraphicsItem *item : std::as_const(scene->d_func()->scenePosItems)) {
4632 if (q->isAncestorOf(item))
4633 item->itemChange(QGraphicsItem::ItemScenePositionHasChanged, item->scenePos());
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659void QGraphicsItem::stackBefore(
const QGraphicsItem *sibling)
4661 if (sibling ==
this)
4663 if (!sibling || d_ptr->parent != sibling->parentItem()) {
4664 qWarning(
"QGraphicsItem::stackUnder: cannot stack under %p, which must be a sibling", sibling);
4667 QList<QGraphicsItem *> *siblings = d_ptr->parent
4668 ? &d_ptr->parent->d_ptr->children
4669 : (d_ptr->scene ? &d_ptr->scene->d_func()->topLevelItems :
nullptr);
4671 qWarning(
"QGraphicsItem::stackUnder: cannot stack under %p, which must be a sibling", sibling);
4678 d_ptr->parent->d_ptr->ensureSequentialSiblingIndex();
4680 d_ptr->scene->d_func()->ensureSequentialTopLevelSiblingIndexes();
4683 int siblingIndex = sibling->d_ptr->siblingIndex;
4684 int myIndex = d_ptr->siblingIndex;
4685 if (myIndex >= siblingIndex) {
4686 siblings->move(myIndex, siblingIndex);
4688 for (
int i = 0; i < siblings->size(); ++i) {
4689 int &index = siblings->at(i)->d_ptr->siblingIndex;
4690 if (i != siblingIndex && index >= siblingIndex && index <= myIndex)
4693 d_ptr->siblingIndex = siblingIndex;
4694 for (
int i = 0; i < siblings->size(); ++i) {
4695 int &index = siblings->at(i)->d_ptr->siblingIndex;
4696 if (i != siblingIndex && index >= siblingIndex && index <= myIndex)
4697 siblings->at(i)->d_ptr->siblingOrderChange();
4699 d_ptr->siblingOrderChange();
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720QRectF QGraphicsItem::childrenBoundingRect()
const
4722 if (!d_ptr->dirtyChildrenBoundingRect)
4723 return d_ptr->childrenBoundingRect;
4725 d_ptr->childrenBoundingRect = QRectF();
4726 d_ptr->childrenBoundingRectHelper(
nullptr, &d_ptr->childrenBoundingRect,
nullptr);
4727 d_ptr->dirtyChildrenBoundingRect = 0;
4728 return d_ptr->childrenBoundingRect;
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4765
4766
4767
4768
4769
4770QRectF QGraphicsItem::sceneBoundingRect()
const
4775 const QGraphicsItem *parentItem =
this;
4776 const QGraphicsItemPrivate *itemd;
4778 itemd = parentItem->d_ptr.data();
4779 if (itemd->transformData)
4781 offset += itemd->pos;
4782 }
while ((parentItem = itemd->parent));
4784 QRectF br = boundingRect();
4785 br.translate(offset);
4788 if (parentItem->d_ptr->hasTranslateOnlySceneTransform()) {
4789 br.translate(parentItem->d_ptr->sceneTransform.dx(), parentItem->d_ptr->sceneTransform.dy());
4792 return parentItem->d_ptr->sceneTransform.mapRect(br);
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817QPainterPath QGraphicsItem::shape()
const
4820 path.addRect(boundingRect());
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834bool QGraphicsItem::isClipped()
const
4836 Q_D(
const QGraphicsItem);
4837 return (d->ancestorFlags & QGraphicsItemPrivate::AncestorClipsChildren)
4838 || (d->flags & QGraphicsItem::ItemClipsToShape);
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861QPainterPath QGraphicsItem::clipPath()
const
4863 Q_D(
const QGraphicsItem);
4865 return QPainterPath();
4867 const QRectF thisBoundingRect(boundingRect());
4868 if (thisBoundingRect.isEmpty())
4869 return QPainterPath();
4873 clip.addRect(thisBoundingRect);
4875 if (d->ancestorFlags & QGraphicsItemPrivate::AncestorClipsChildren) {
4876 const QGraphicsItem *parent =
this;
4877 const QGraphicsItem *lastParent =
this;
4880 while ((parent = parent->d_ptr->parent)) {
4881 if (parent->d_ptr->flags & ItemClipsChildrenToShape) {
4883 clip = lastParent->itemTransform(parent).map(clip);
4884 clip = clip.intersected(parent->shape());
4887 lastParent = parent;
4890 if (!(parent->d_ptr->ancestorFlags & QGraphicsItemPrivate::AncestorClipsChildren))
4894 if (lastParent !=
this) {
4897 clip = lastParent->itemTransform(
this).map(clip);
4901 if (d->flags & ItemClipsToShape)
4902 clip = clip.intersected(shape());
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919bool QGraphicsItem::contains(
const QPointF &point)
const
4921 return isClipped() ? clipPath().contains(point) : shape().contains(point);
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953bool QGraphicsItem::collidesWithItem(
const QGraphicsItem *other, Qt::ItemSelectionMode mode)
const
4961 bool clips = (d_ptr->ancestorFlags & QGraphicsItemPrivate::AncestorClipsChildren);
4962 bool otherClips = (other->d_ptr->ancestorFlags & QGraphicsItemPrivate::AncestorClipsChildren);
4963 if (clips || otherClips) {
4964 const QGraphicsItem *closestClipper = isAncestorOf(other) ?
this : parentItem();
4965 while (closestClipper && !(closestClipper->flags() & ItemClipsChildrenToShape))
4966 closestClipper = closestClipper->parentItem();
4967 const QGraphicsItem *otherClosestClipper = other->isAncestorOf(
this) ? other : other->parentItem();
4968 while (otherClosestClipper && !(otherClosestClipper->flags() & ItemClipsChildrenToShape))
4969 otherClosestClipper = otherClosestClipper->parentItem();
4970 if (closestClipper == otherClosestClipper) {
4971 d_ptr->localCollisionHack = 1;
4972 bool res = collidesWithPath(mapFromItem(other, other->shape()), mode);
4973 d_ptr->localCollisionHack = 0;
4978 QPainterPath otherShape = other->isClipped() ? other->clipPath() : other->shape();
4979 return collidesWithPath(mapFromItem(other, otherShape), mode);
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996bool QGraphicsItem::collidesWithPath(
const QPainterPath &path, Qt::ItemSelectionMode mode)
const
4998 if (path.isEmpty()) {
5003 QRectF rectA(boundingRect());
5004 _q_adjustRect(&rectA);
5005 QRectF rectB(path.controlPointRect());
5006 _q_adjustRect(&rectB);
5007 if (!rectA.intersects(rectB)) {
5014 QPainterPath thisShape;
5015 if (mode == Qt::IntersectsItemShape || mode == Qt::ContainsItemShape)
5016 thisShape = (isClipped() && !d_ptr->localCollisionHack) ? clipPath() : shape();
5018 thisShape.addRect(rectA);
5020 if (thisShape == QPainterPath()) {
5026 if (mode == Qt::IntersectsItemShape || mode == Qt::IntersectsItemBoundingRect)
5027 return path.intersects(thisShape);
5028 return path.contains(thisShape);
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041QList<QGraphicsItem *> QGraphicsItem::collidingItems(Qt::ItemSelectionMode mode)
const
5044 return d_ptr->scene->collidingItems(
this, mode);
5045 return QList<QGraphicsItem *>();
5049
5050
5051
5052
5053
5054
5055
5056
5057
5059 const QGraphicsItem *other,
5062 return other->mapToItem(item, other->opaqueArea()).contains(rect);
5066
5067
5068
5069
5070
5071
5072
5073
5074bool QGraphicsItem::isObscured(
const QRectF &rect)
const
5076 Q_D(
const QGraphicsItem);
5080 QRectF br = boundingRect();
5081 QRectF testRect = rect.isNull() ? br : rect;
5083 const auto items = d->scene->items(mapToScene(br), Qt::IntersectsItemBoundingRect);
5084 for (QGraphicsItem *item : items) {
5087 if (qt_QGraphicsItem_isObscured(
this, item, testRect))
5094
5095
5096
5097
5098
5099
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114bool QGraphicsItem::isObscuredBy(
const QGraphicsItem *item)
const
5118 return qt_closestItemFirst(item,
this)
5119 && qt_QGraphicsItem_isObscured(
this, item, boundingRect());
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135QPainterPath QGraphicsItem::opaqueArea()
const
5137 return QPainterPath();
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163QRegion QGraphicsItem::boundingRegion(
const QTransform &itemToDeviceTransform)
const
5173 qreal granularity = boundingRegionGranularity();
5174 QRect deviceRect = itemToDeviceTransform.mapRect(boundingRect()).toRect();
5175 _q_adjustRect(&deviceRect);
5176 if (granularity == 0.0)
5177 return QRegion(deviceRect);
5180 QSize bitmapSize(qMax(1,
int(deviceRect.width() * granularity) + pad * 2),
5181 qMax(1,
int(deviceRect.height() * granularity) + pad * 2));
5182 QImage mask(bitmapSize, QImage::Format_ARGB32_Premultiplied);
5185 p.setRenderHints(QPainter::Antialiasing);
5189 QPointF viewOrigo = itemToDeviceTransform.map(QPointF(0, 0));
5190 QPointF offset = viewOrigo - deviceRect.topLeft();
5191 p.scale(granularity, granularity);
5192 p.translate(offset);
5193 p.translate(pad, pad);
5194 p.setWorldTransform(itemToDeviceTransform,
true);
5195 p.translate(itemToDeviceTransform.inverted().map(QPointF(0, 0)));
5198 QStyleOptionGraphicsItem option;
5199 const_cast<QGraphicsItem *>(
this)->paint(&p, &option,
nullptr);
5203 QTransform unscale = QTransform::fromScale(1 / granularity, 1 / granularity);
5205 QBitmap colorMask = QBitmap::fromImage(mask.createMaskFromColor(0));
5206 for (
const QRect &rect : QRegion(colorMask)) {
5207 QRect xrect = unscale.mapRect(rect).translated(deviceRect.topLeft() - QPoint(pad, pad));
5208 r += xrect.adjusted(-1, -1, 1, 1) & deviceRect;
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226qreal QGraphicsItem::boundingRegionGranularity()
const
5228 return d_ptr->hasBoundingRegionGranularity
5229 ? qvariant_cast<qreal>(d_ptr->extra(QGraphicsItemPrivate::ExtraBoundingRegionGranularity))
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252void QGraphicsItem::setBoundingRegionGranularity(qreal granularity)
5254 if (granularity < 0.0 || granularity > 1.0) {
5255 qWarning(
"QGraphicsItem::setBoundingRegionGranularity: invalid granularity %g", granularity);
5258 if (granularity == 0.0) {
5259 d_ptr->unsetExtra(QGraphicsItemPrivate::ExtraBoundingRegionGranularity);
5260 d_ptr->hasBoundingRegionGranularity = 0;
5263 d_ptr->hasBoundingRegionGranularity = 1;
5264 d_ptr->setExtra(QGraphicsItemPrivate::ExtraBoundingRegionGranularity,
5265 QVariant::fromValue<qreal>(granularity));
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5313
5314
5315
5316bool QGraphicsItemPrivate::discardUpdateRequest(
bool ignoreVisibleBit,
bool ignoreDirtyBit,
5317 bool ignoreOpacity)
const
5322 || (!visible && !ignoreVisibleBit && !
this->ignoreVisible)
5323 || (!ignoreDirtyBit && fullUpdatePending)
5324 || (!ignoreOpacity && !
this->ignoreOpacity && childrenCombineOpacity() && isFullyTransparent());
5328
5329
5330int QGraphicsItemPrivate::depth()
const
5332 if (itemDepth == -1)
5333 const_cast<QGraphicsItemPrivate *>(
this)->resolveDepth();
5339
5340
5341#if QT_CONFIG(graphicseffect)
5342void QGraphicsItemPrivate::invalidateParentGraphicsEffectsRecursively()
5344 QGraphicsItemPrivate *itemPrivate =
this;
5346 if (itemPrivate->graphicsEffect) {
5347 itemPrivate->notifyInvalidated = 1;
5349 if (!itemPrivate->updateDueToGraphicsEffect)
5350 static_cast<QGraphicsItemEffectSourcePrivate *>(itemPrivate->graphicsEffect->d_func()->source->d_func())->invalidateCache();
5352 }
while ((itemPrivate = itemPrivate->parent ? itemPrivate->parent->d_ptr.data() :
nullptr));
5355void QGraphicsItemPrivate::invalidateChildGraphicsEffectsRecursively(QGraphicsItemPrivate::InvalidateReason reason)
5357 if (!mayHaveChildWithGraphicsEffect)
5360 for (
int i = 0; i < children.size(); ++i) {
5361 QGraphicsItemPrivate *childPrivate = children.at(i)->d_ptr.data();
5362 if (reason == OpacityChanged && (childPrivate->flags & QGraphicsItem::ItemIgnoresParentOpacity))
5364 if (childPrivate->graphicsEffect) {
5365 childPrivate->notifyInvalidated = 1;
5366 static_cast<QGraphicsItemEffectSourcePrivate *>(childPrivate->graphicsEffect->d_func()->source->d_func())->invalidateCache();
5369 childPrivate->invalidateChildGraphicsEffectsRecursively(reason);
5375
5376
5377void QGraphicsItemPrivate::invalidateDepthRecursively()
5379 if (itemDepth == -1)
5383 for (
int i = 0; i < children.size(); ++i)
5384 children.at(i)->d_ptr->invalidateDepthRecursively();
5388
5389
5390
5391
5392void QGraphicsItemPrivate::resolveDepth()
5397 if (parent->d_ptr->itemDepth == -1)
5398 parent->d_ptr->resolveDepth();
5399 itemDepth = parent->d_ptr->itemDepth + 1;
5404
5405
5406
5407
5408
5409void QGraphicsItemPrivate::addChild(QGraphicsItem *child)
5413 ensureSequentialSiblingIndex();
5414 needSortChildren = 1;
5415 child->d_ptr->siblingIndex = children.size();
5416 children.append(child);
5418 emit
static_cast<QGraphicsObject *>(q_ptr)->childrenChanged();
5422
5423
5424
5425
5426
5427void QGraphicsItemPrivate::removeChild(QGraphicsItem *child)
5431 if (!holesInSiblingIndex)
5432 holesInSiblingIndex = child->d_ptr->siblingIndex != children.size() - 1;
5433 if (sequentialOrdering && !holesInSiblingIndex)
5434 children.removeAt(child->d_ptr->siblingIndex);
5436 children.removeOne(child);
5440 child->d_ptr->siblingIndex = -1;
5442 emit
static_cast<QGraphicsObject *>(q_ptr)->childrenChanged();
5446
5447
5448QGraphicsItemCache *QGraphicsItemPrivate::maybeExtraItemCache()
const
5450 return (QGraphicsItemCache *)qvariant_cast<
void *>(extra(ExtraCacheData));
5454
5455
5456QGraphicsItemCache *QGraphicsItemPrivate::extraItemCache()
const
5458 QGraphicsItemCache *c = (QGraphicsItemCache *)qvariant_cast<
void *>(extra(ExtraCacheData));
5460 QGraphicsItemPrivate *that =
const_cast<QGraphicsItemPrivate *>(
this);
5461 c =
new QGraphicsItemCache;
5462 that->setExtra(ExtraCacheData, QVariant::fromValue<
void *>(c));
5468
5469
5470void QGraphicsItemPrivate::removeExtraItemCache()
5472 QGraphicsItemCache *c = (QGraphicsItemCache *)qvariant_cast<
void *>(extra(ExtraCacheData));
5477 unsetExtra(ExtraCacheData);
5480void QGraphicsItemPrivate::updatePaintedViewBoundingRects(
bool updateChildren)
5485 for (
int i = 0; i < scene->d_func()->views.size(); ++i) {
5486 QGraphicsViewPrivate *viewPrivate = scene->d_func()->views.at(i)->d_func();
5487 QRect rect = paintedViewBoundingRects.value(viewPrivate->viewport);
5488 rect.translate(viewPrivate->dirtyScrollOffset);
5489 viewPrivate->updateRect(rect);
5492 if (updateChildren) {
5493 for (
int i = 0; i < children.size(); ++i)
5494 children.at(i)->d_ptr->updatePaintedViewBoundingRects(
true);
5503void QGraphicsItemPrivate::ensureSceneTransformRecursive(QGraphicsItem **topMostDirtyItem)
5505 Q_ASSERT(topMostDirtyItem);
5507 if (dirtySceneTransform)
5508 *topMostDirtyItem = q_ptr;
5511 parent->d_ptr->ensureSceneTransformRecursive(topMostDirtyItem);
5513 if (*topMostDirtyItem == q_ptr) {
5514 if (!dirtySceneTransform)
5516 *topMostDirtyItem =
nullptr;
5517 }
else if (*topMostDirtyItem) {
5525 invalidateChildrenSceneTransform();
5528 updateSceneTransformFromParent();
5529 Q_ASSERT(!dirtySceneTransform);
5533
5534
5535void QGraphicsItemPrivate::setSubFocus(QGraphicsItem *rootItem, QGraphicsItem *stopItem)
5539 QGraphicsItem *parent = rootItem ? rootItem : q_ptr;
5540 if (parent->panel() != q_ptr->panel())
5545 if (parent != q_ptr && parent->d_ptr->subFocusItem) {
5546 if (parent->d_ptr->subFocusItem == q_ptr)
5548 parent->d_ptr->subFocusItem->d_ptr->clearSubFocus(
nullptr, stopItem);
5550 parent->d_ptr->subFocusItem = q_ptr;
5551 parent->d_ptr->subFocusItemChange();
5552 }
while (!parent->isPanel() && (parent = parent->d_ptr->parent) && (visible || !parent->d_ptr->visible));
5554 if (scene && !scene->isActive()) {
5555 scene->d_func()->passiveFocusItem = subFocusItem;
5556 scene->d_func()->lastFocusItem = subFocusItem;
5561
5562
5563void QGraphicsItemPrivate::clearSubFocus(QGraphicsItem *rootItem, QGraphicsItem *stopItem)
5566 QGraphicsItem *parent = rootItem ? rootItem : q_ptr;
5568 if (parent->d_ptr->subFocusItem != q_ptr)
5570 parent->d_ptr->subFocusItem =
nullptr;
5571 if (parent != stopItem && !parent->isAncestorOf(stopItem))
5572 parent->d_ptr->subFocusItemChange();
5573 }
while (!parent->isPanel() && (parent = parent->d_ptr->parent));
5577
5578
5579
5580
5581
5582void QGraphicsItemPrivate::resetFocusProxy()
5584 for (
int i = 0; i < focusProxyRefs.size(); ++i)
5585 *focusProxyRefs.at(i) =
nullptr;
5586 focusProxyRefs.clear();
5590
5591
5592
5593
5594
5595void QGraphicsItemPrivate::subFocusItemChange()
5600
5601
5602
5603
5604
5605void QGraphicsItemPrivate::focusScopeItemChange(
bool isSubFocusItem)
5607 Q_UNUSED(isSubFocusItem);
5611
5612
5613
5614
5615
5616void QGraphicsItemPrivate::siblingOrderChange()
5621
5622
5623
5624
5625bool QGraphicsItemPrivate::isProxyWidget()
const
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648void QGraphicsItem::update(
const QRectF &rect)
5650 if (rect.isEmpty() && !rect.isNull())
5654#if QT_CONFIG(graphicseffect)
5655 d_ptr->invalidateParentGraphicsEffectsRecursively();
5658 if (CacheMode(d_ptr->cacheMode) != NoCache) {
5660 QGraphicsItemCache *cache = d_ptr->extraItemCache();
5661 if (!cache->allExposed) {
5662 if (rect.isNull()) {
5663 cache->allExposed =
true;
5664 cache->exposed.clear();
5666 cache->exposed.append(rect);
5670 if (d_ptr->fullUpdatePending)
5675 d_ptr->scene->d_func()->markDirty(
this, rect);
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705void QGraphicsItem::scroll(qreal dx, qreal dy,
const QRectF &rect)
5708 if (dx == 0.0 && dy == 0.0)
5726 if (d->cacheMode != QGraphicsItem::ItemCoordinateCache
5727 || !qFuzzyIsNull(dx -
int(dx)) || !qFuzzyIsNull(dy -
int(dy))) {
5732 QGraphicsItemCache *cache = d->extraItemCache();
5733 if (cache->allExposed || cache->fixedSize.isValid()) {
5740 QPixmap cachedPixmap;
5741 if (!QPixmapCache::find(cache->key, &cachedPixmap)) {
5746 QRect scrollRect = (rect.isNull() ? boundingRect() : rect).toAlignedRect();
5747 if (!scrollRect.intersects(cache->boundingRect))
5751 QPixmapCache::remove(cache->key);
5754 cachedPixmap.scroll(dx, dy, scrollRect.translated(-cache->boundingRect.topLeft()), &exposed);
5757 cache->key = QPixmapCache::insert(cachedPixmap);
5760 for (
int i = 0; i < cache->exposed.size(); ++i) {
5761 QRectF &e = cache->exposed[i];
5762 if (!rect.isNull() && !e.intersects(rect))
5764 e.translate(dx, dy);
5769 exposed.translate(cache->boundingRect.topLeft());
5770 for (
const QRect &exposedRect : exposed)
5771 cache->exposed += exposedRect;
5775 d->scene->d_func()->markDirty(
this, rect);
5779
5780
5781
5782
5783
5784
5787
5788
5789
5790
5791
5792
5793
5794
5795QPointF QGraphicsItem::mapToItem(
const QGraphicsItem *item,
const QPointF &point)
const
5798 return itemTransform(item).map(point);
5799 return mapToScene(point);
5803
5804
5805
5806
5807
5808
5811
5812
5813
5814
5815
5816
5817
5818QPointF QGraphicsItem::mapToParent(
const QPointF &point)
const
5821 if (!d_ptr->transformData)
5822 return point + d_ptr->pos;
5823 return d_ptr->transformToParent().map(point);
5827
5828
5829
5830
5831
5832
5835
5836
5837
5838
5839
5840
5841QPointF QGraphicsItem::mapToScene(
const QPointF &point)
const
5843 if (d_ptr->hasTranslateOnlySceneTransform())
5844 return QPointF(point.x() + d_ptr->sceneTransform.dx(), point.y() + d_ptr->sceneTransform.dy());
5845 return d_ptr->sceneTransform.map(point);
5849
5850
5851
5852
5853
5854
5857
5858
5859
5860
5861
5862
5863
5864
5865QPolygonF QGraphicsItem::mapToItem(
const QGraphicsItem *item,
const QRectF &rect)
const
5868 return itemTransform(item).map(rect);
5869 return mapToScene(rect);
5873
5874
5875
5876
5877
5880
5881
5882
5883
5884
5885
5886
5887
5888QPolygonF QGraphicsItem::mapToParent(
const QRectF &rect)
const
5891 if (!d_ptr->transformData)
5892 return rect.translated(d_ptr->pos);
5893 return d_ptr->transformToParent().map(rect);
5897
5898
5899
5900
5901
5904
5905
5906
5907
5908
5909
5910QPolygonF QGraphicsItem::mapToScene(
const QRectF &rect)
const
5912 if (d_ptr->hasTranslateOnlySceneTransform())
5913 return rect.translated(d_ptr->sceneTransform.dx(), d_ptr->sceneTransform.dy());
5914 return d_ptr->sceneTransform.map(rect);
5918
5919
5920
5921
5922
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936QRectF QGraphicsItem::mapRectToItem(
const QGraphicsItem *item,
const QRectF &rect)
const
5939 return itemTransform(item).mapRect(rect);
5940 return mapRectToScene(rect);
5944
5945
5946
5947
5948
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960QRectF QGraphicsItem::mapRectToParent(
const QRectF &rect)
const
5963 if (!d_ptr->transformData)
5964 return rect.translated(d_ptr->pos);
5965 return d_ptr->transformToParent().mapRect(rect);
5969
5970
5971
5972
5973
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985QRectF QGraphicsItem::mapRectToScene(
const QRectF &rect)
const
5987 if (d_ptr->hasTranslateOnlySceneTransform())
5988 return rect.translated(d_ptr->sceneTransform.dx(), d_ptr->sceneTransform.dy());
5989 return d_ptr->sceneTransform.mapRect(rect);
5993
5994
5995
5996
5997
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011QRectF QGraphicsItem::mapRectFromItem(
const QGraphicsItem *item,
const QRectF &rect)
const
6014 return item->itemTransform(
this).mapRect(rect);
6015 return mapRectFromScene(rect);
6019
6020
6021
6022
6023
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036QRectF QGraphicsItem::mapRectFromParent(
const QRectF &rect)
const
6039 if (!d_ptr->transformData)
6040 return rect.translated(-d_ptr->pos);
6041 return d_ptr->transformToParent().inverted().mapRect(rect);
6045
6046
6047
6048
6049
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061QRectF QGraphicsItem::mapRectFromScene(
const QRectF &rect)
const
6063 if (d_ptr->hasTranslateOnlySceneTransform())
6064 return rect.translated(-d_ptr->sceneTransform.dx(), -d_ptr->sceneTransform.dy());
6065 return d_ptr->sceneTransform.inverted().mapRect(rect);
6069
6070
6071
6072
6073
6076
6077
6078
6079
6080
6081
6082
6083
6084QPolygonF QGraphicsItem::mapToItem(
const QGraphicsItem *item,
const QPolygonF &polygon)
const
6087 return itemTransform(item).map(polygon);
6088 return mapToScene(polygon);
6092
6093
6094
6095
6096
6097
6098
6099
6100QPolygonF QGraphicsItem::mapToParent(
const QPolygonF &polygon)
const
6103 if (!d_ptr->transformData)
6104 return polygon.translated(d_ptr->pos);
6105 return d_ptr->transformToParent().map(polygon);
6109
6110
6111
6112
6113
6114
6115QPolygonF QGraphicsItem::mapToScene(
const QPolygonF &polygon)
const
6117 if (d_ptr->hasTranslateOnlySceneTransform())
6118 return polygon.translated(d_ptr->sceneTransform.dx(), d_ptr->sceneTransform.dy());
6119 return d_ptr->sceneTransform.map(polygon);
6123
6124
6125
6126
6127
6128
6129
6130
6131QPainterPath QGraphicsItem::mapToItem(
const QGraphicsItem *item,
const QPainterPath &path)
const
6134 return itemTransform(item).map(path);
6135 return mapToScene(path);
6139
6140
6141
6142
6143
6144
6145
6146
6147QPainterPath QGraphicsItem::mapToParent(
const QPainterPath &path)
const
6150 if (!d_ptr->transformData)
6151 return path.translated(d_ptr->pos);
6152 return d_ptr->transformToParent().map(path);
6156
6157
6158
6159
6160
6161
6162QPainterPath QGraphicsItem::mapToScene(
const QPainterPath &path)
const
6164 if (d_ptr->hasTranslateOnlySceneTransform())
6165 return path.translated(d_ptr->sceneTransform.dx(), d_ptr->sceneTransform.dy());
6166 return d_ptr->sceneTransform.map(path);
6170
6171
6172
6173
6174
6175
6176
6177
6178QPointF QGraphicsItem::mapFromItem(
const QGraphicsItem *item,
const QPointF &point)
const
6181 return item->itemTransform(
this).map(point);
6182 return mapFromScene(point);
6186
6187
6188
6189
6190
6191
6194
6195
6196
6197
6198
6199
6200
6201QPointF QGraphicsItem::mapFromParent(
const QPointF &point)
const
6204 if (d_ptr->transformData)
6205 return d_ptr->transformToParent().inverted().map(point);
6206 return point - d_ptr->pos;
6210
6211
6212
6213
6214
6215
6218
6219
6220
6221
6222
6223
6224
6225QPointF QGraphicsItem::mapFromScene(
const QPointF &point)
const
6227 if (d_ptr->hasTranslateOnlySceneTransform())
6228 return QPointF(point.x() - d_ptr->sceneTransform.dx(), point.y() - d_ptr->sceneTransform.dy());
6229 return d_ptr->sceneTransform.inverted().map(point);
6233
6234
6235
6236
6237
6238
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250QPolygonF QGraphicsItem::mapFromItem(
const QGraphicsItem *item,
const QRectF &rect)
const
6253 return item->itemTransform(
this).map(rect);
6254 return mapFromScene(rect);
6258
6259
6260
6261
6262
6265
6266
6267
6268
6269
6270
6271
6272QPolygonF QGraphicsItem::mapFromParent(
const QRectF &rect)
const
6275 if (!d_ptr->transformData)
6276 return rect.translated(-d_ptr->pos);
6277 return d_ptr->transformToParent().inverted().map(rect);
6281
6282
6283
6284
6285
6288
6289
6290
6291
6292
6293
6294
6295QPolygonF QGraphicsItem::mapFromScene(
const QRectF &rect)
const
6297 if (d_ptr->hasTranslateOnlySceneTransform())
6298 return rect.translated(-d_ptr->sceneTransform.dx(), -d_ptr->sceneTransform.dy());
6299 return d_ptr->sceneTransform.inverted().map(rect);
6303
6304
6305
6306
6307
6310
6311
6312
6313
6314
6315
6316
6317
6318QPolygonF QGraphicsItem::mapFromItem(
const QGraphicsItem *item,
const QPolygonF &polygon)
const
6321 return item->itemTransform(
this).map(polygon);
6322 return mapFromScene(polygon);
6326
6327
6328
6329
6330
6331
6332QPolygonF QGraphicsItem::mapFromParent(
const QPolygonF &polygon)
const
6335 if (!d_ptr->transformData)
6336 return polygon.translated(-d_ptr->pos);
6337 return d_ptr->transformToParent().inverted().map(polygon);
6341
6342
6343
6344
6345
6346
6347QPolygonF QGraphicsItem::mapFromScene(
const QPolygonF &polygon)
const
6349 if (d_ptr->hasTranslateOnlySceneTransform())
6350 return polygon.translated(-d_ptr->sceneTransform.dx(), -d_ptr->sceneTransform.dy());
6351 return d_ptr->sceneTransform.inverted().map(polygon);
6355
6356
6357
6358
6359
6360
6361
6362
6363QPainterPath QGraphicsItem::mapFromItem(
const QGraphicsItem *item,
const QPainterPath &path)
const
6366 return item->itemTransform(
this).map(path);
6367 return mapFromScene(path);
6371
6372
6373
6374
6375
6376
6377QPainterPath QGraphicsItem::mapFromParent(
const QPainterPath &path)
const
6380 if (!d_ptr->transformData)
6381 return path.translated(-d_ptr->pos);
6382 return d_ptr->transformToParent().inverted().map(path);
6386
6387
6388
6389
6390
6391
6392QPainterPath QGraphicsItem::mapFromScene(
const QPainterPath &path)
const
6394 if (d_ptr->hasTranslateOnlySceneTransform())
6395 return path.translated(-d_ptr->sceneTransform.dx(), -d_ptr->sceneTransform.dy());
6396 return d_ptr->sceneTransform.inverted().map(path);
6400
6401
6402
6403
6404
6405bool QGraphicsItem::isAncestorOf(
const QGraphicsItem *child)
const
6407 if (!child || child ==
this)
6409 if (child->d_ptr->depth() < d_ptr->depth())
6411 const QGraphicsItem *ancestor = child;
6412 while ((ancestor = ancestor->d_ptr->parent)) {
6413 if (ancestor ==
this)
6420
6421
6422
6423
6424
6425
6426
6427QGraphicsItem *QGraphicsItem::commonAncestorItem(
const QGraphicsItem *other)
const
6432 return const_cast<QGraphicsItem *>(
this);
6433 const QGraphicsItem *thisw =
this;
6434 const QGraphicsItem *otherw = other;
6435 int thisDepth = d_ptr->depth();
6436 int otherDepth = other->d_ptr->depth();
6437 while (thisDepth > otherDepth) {
6438 thisw = thisw->d_ptr->parent;
6441 while (otherDepth > thisDepth) {
6442 otherw = otherw->d_ptr->parent;
6445 while (thisw && thisw != otherw) {
6446 thisw = thisw->d_ptr->parent;
6447 otherw = otherw->d_ptr->parent;
6449 return const_cast<QGraphicsItem *>(thisw);
6453
6454
6455
6456
6457
6458
6459bool QGraphicsItem::isUnderMouse()
const
6461 Q_D(
const QGraphicsItem);
6465 QPoint cursorPos = QCursor::pos();
6466 const auto views = d->scene->views();
6467 for (QGraphicsView *view : views) {
6468 if (contains(mapFromScene(view->mapToScene(view->mapFromGlobal(cursorPos)))))
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487QVariant QGraphicsItem::data(
int key)
const
6489 QGraphicsItemCustomDataStore *store = qt_dataStore();
6490 if (!store->data.contains(
this))
6492 return store->data.value(
this).value(key);
6496
6497
6498
6499
6500
6501
6502
6503
6504void QGraphicsItem::setData(
int key,
const QVariant &value)
6506 qt_dataStore()->data[
this][key] = value;
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541int QGraphicsItem::type()
const
6543 return (
int)UserType;
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565void QGraphicsItem::installSceneEventFilter(QGraphicsItem *filterItem)
6567 if (!d_ptr->scene) {
6568 qWarning(
"QGraphicsItem::installSceneEventFilter: event filters can only be installed"
6569 " on items in a scene.");
6572 if (d_ptr->scene != filterItem->scene()) {
6573 qWarning(
"QGraphicsItem::installSceneEventFilter: event filters can only be installed"
6574 " on items in the same scene.");
6577 d_ptr->scene->d_func()->installSceneEventFilter(
this, filterItem);
6581
6582
6583
6584
6585void QGraphicsItem::removeSceneEventFilter(QGraphicsItem *filterItem)
6587 if (!d_ptr->scene || d_ptr->scene != filterItem->scene())
6589 d_ptr->scene->d_func()->removeSceneEventFilter(
this, filterItem);
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608bool QGraphicsItem::sceneEventFilter(QGraphicsItem *watched, QEvent *event)
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629bool QGraphicsItem::sceneEvent(QEvent *event)
6631 if (d_ptr->ancestorFlags & QGraphicsItemPrivate::AncestorHandlesChildEvents) {
6632 if (event->type() == QEvent::HoverEnter || event->type() == QEvent::HoverLeave
6633 || event->type() == QEvent::DragEnter || event->type() == QEvent::DragLeave) {
6639 QGraphicsItem *handler =
this;
6641 handler = handler->d_ptr->parent;
6643 }
while (handler->d_ptr->ancestorFlags & QGraphicsItemPrivate::AncestorHandlesChildEvents);
6647 d_ptr->remapItemPos(event, handler);
6648 handler->sceneEvent(event);
6652 if (event->type() == QEvent::FocusOut) {
6653 focusOutEvent(
static_cast<QFocusEvent *>(event));
6657 if (!d_ptr->visible) {
6662 switch (event->type()) {
6663 case QEvent::FocusIn:
6664 focusInEvent(
static_cast<QFocusEvent *>(event));
6666 case QEvent::GraphicsSceneContextMenu:
6667 contextMenuEvent(
static_cast<QGraphicsSceneContextMenuEvent *>(event));
6669 case QEvent::GraphicsSceneDragEnter:
6670 dragEnterEvent(
static_cast<QGraphicsSceneDragDropEvent *>(event));
6672 case QEvent::GraphicsSceneDragMove:
6673 dragMoveEvent(
static_cast<QGraphicsSceneDragDropEvent *>(event));
6675 case QEvent::GraphicsSceneDragLeave:
6676 dragLeaveEvent(
static_cast<QGraphicsSceneDragDropEvent *>(event));
6678 case QEvent::GraphicsSceneDrop:
6679 dropEvent(
static_cast<QGraphicsSceneDragDropEvent *>(event));
6681 case QEvent::GraphicsSceneHoverEnter:
6682 hoverEnterEvent(
static_cast<QGraphicsSceneHoverEvent *>(event));
6684 case QEvent::GraphicsSceneHoverMove:
6685 hoverMoveEvent(
static_cast<QGraphicsSceneHoverEvent *>(event));
6687 case QEvent::GraphicsSceneHoverLeave:
6688 hoverLeaveEvent(
static_cast<QGraphicsSceneHoverEvent *>(event));
6690 case QEvent::GraphicsSceneMouseMove:
6691 mouseMoveEvent(
static_cast<QGraphicsSceneMouseEvent *>(event));
6693 case QEvent::GraphicsSceneMousePress:
6694 mousePressEvent(
static_cast<QGraphicsSceneMouseEvent *>(event));
6696 case QEvent::GraphicsSceneMouseRelease:
6697 mouseReleaseEvent(
static_cast<QGraphicsSceneMouseEvent *>(event));
6699 case QEvent::GraphicsSceneMouseDoubleClick:
6700 mouseDoubleClickEvent(
static_cast<QGraphicsSceneMouseEvent *>(event));
6702 case QEvent::GraphicsSceneWheel:
6703 wheelEvent(
static_cast<QGraphicsSceneWheelEvent *>(event));
6705 case QEvent::KeyPress: {
6706 QKeyEvent *k =
static_cast<QKeyEvent *>(event);
6707 if (k->key() == Qt::Key_Tab || k->key() == Qt::Key_Backtab) {
6708 if (!(k->modifiers() & (Qt::ControlModifier | Qt::AltModifier))) {
6710 if (k->key() == Qt::Key_Backtab
6711 || (k->key() == Qt::Key_Tab && (k->modifiers() & Qt::ShiftModifier))) {
6712 if (d_ptr->isWidget) {
6713 res =
static_cast<QGraphicsWidget *>(
this)->focusNextPrevChild(
false);
6714 }
else if (d_ptr->scene) {
6715 res = d_ptr->scene->focusNextPrevChild(
false);
6717 }
else if (k->key() == Qt::Key_Tab) {
6718 if (d_ptr->isWidget) {
6719 res =
static_cast<QGraphicsWidget *>(
this)->focusNextPrevChild(
true);
6720 }
else if (d_ptr->scene) {
6721 res = d_ptr->scene->focusNextPrevChild(
true);
6729 keyPressEvent(
static_cast<QKeyEvent *>(event));
6732 case QEvent::KeyRelease:
6733 keyReleaseEvent(
static_cast<QKeyEvent *>(event));
6735 case QEvent::InputMethod:
6736 inputMethodEvent(
static_cast<QInputMethodEvent *>(event));
6738 case QEvent::WindowActivate:
6739 case QEvent::WindowDeactivate:
6742 for (
int i = 0; i < d_ptr->children.size(); ++i) {
6743 QGraphicsItem *child = d_ptr->children.at(i);
6744 if (child->isVisible() && !child->isPanel()) {
6745 if (!(child->d_ptr->ancestorFlags & QGraphicsItemPrivate::AncestorHandlesChildEvents))
6746 d_ptr->scene->sendEvent(child, event);
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776void QGraphicsItem::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804void QGraphicsItem::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
6808 if (d->isProxyWidget())
6809 static_cast<QGraphicsProxyWidget*>(
this)->dragEnterEvent(event);
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828void QGraphicsItem::dragLeaveEvent(QGraphicsSceneDragDropEvent *event)
6832 if (d->isProxyWidget())
6833 static_cast<QGraphicsProxyWidget*>(
this)->dragLeaveEvent(event);
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855void QGraphicsItem::dragMoveEvent(QGraphicsSceneDragDropEvent *event)
6859 if (d->isProxyWidget())
6860 static_cast<QGraphicsProxyWidget*>(
this)->dragMoveEvent(event);
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877void QGraphicsItem::dropEvent(QGraphicsSceneDragDropEvent *event)
6881 if (d->isProxyWidget())
6882 static_cast<QGraphicsProxyWidget*>(
this)->dropEvent(event);
6886
6887
6888
6889
6890
6891
6892void QGraphicsItem::focusInEvent(QFocusEvent *event)
6899
6900
6901
6902
6903
6904void QGraphicsItem::focusOutEvent(QFocusEvent *event)
6911
6912
6913
6914
6915
6916
6917
6918
6919void QGraphicsItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
6926
6927
6928
6929
6930
6931
6932
6933void QGraphicsItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event)
6939
6940
6941
6942
6943
6944
6945
6946
6947void QGraphicsItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965void QGraphicsItem::keyPressEvent(QKeyEvent *event)
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982void QGraphicsItem::keyReleaseEvent(QKeyEvent *event)
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020void QGraphicsItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
7022 if (event->button() == Qt::LeftButton && (flags() & ItemIsSelectable)) {
7023 bool multiSelect = (event->modifiers() & Qt::ControlModifier) != 0;
7025 if (!d_ptr->selected) {
7026 if (QGraphicsScene *scene = d_ptr->scene) {
7027 ++scene->d_func()->selectionChanging;
7028 scene->clearSelection();
7029 --scene->d_func()->selectionChanging;
7034 }
else if (!(flags() & ItemIsMovable)) {
7037 if (d_ptr->isWidget) {
7039 QGraphicsWidget *w =
static_cast<QGraphicsWidget *>(
this);
7040 if ((w->windowFlags() & Qt::Popup) == Qt::Popup) {
7042 if (!w->rect().contains(event->pos()))
7050 const QGraphicsItem *parent = item->parentItem();
7051 return parent && (((parent->flags() & QGraphicsItem::ItemIsMovable) && parent->isSelected()) || _qt_movableAncestorIsSelected(parent));
7054bool QGraphicsItemPrivate::movableAncestorIsSelected(
const QGraphicsItem *item)
7056 const QGraphicsItem *parent = item->d_ptr->parent;
7057 return parent && (((parent->flags() & QGraphicsItem::ItemIsMovable) && parent->isSelected()) || _qt_movableAncestorIsSelected(parent));
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081void QGraphicsItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
7083 if ((event->buttons() & Qt::LeftButton) && (flags() & ItemIsMovable)) {
7085 QList<QGraphicsItem *> selectedItems;
7086 QHash<QGraphicsItem *, QPointF> initialPositions;
7088 selectedItems = d_ptr->scene->selectedItems();
7089 initialPositions = d_ptr->scene->d_func()->movingItemsInitialPositions;
7090 if (initialPositions.isEmpty()) {
7091 for (QGraphicsItem *item : std::as_const(selectedItems))
7092 initialPositions[item] = item->pos();
7093 initialPositions[
this] = pos();
7095 d_ptr->scene->d_func()->movingItemsInitialPositions = initialPositions;
7099 QGraphicsView *view =
nullptr;
7100 if (event->widget())
7101 view = qobject_cast<QGraphicsView *>(event->widget()->parentWidget());
7105 bool movedMe =
false;
7106 while (i <= selectedItems.size()) {
7107 QGraphicsItem *item =
nullptr;
7108 if (i < selectedItems.size())
7109 item = selectedItems.at(i);
7122 if ((item->flags() & ItemIsMovable) && !QGraphicsItemPrivate::movableAncestorIsSelected(item)) {
7123 QPointF currentParentPos;
7124 QPointF buttonDownParentPos;
7125 if (view && (item->d_ptr->ancestorFlags & QGraphicsItemPrivate::AncestorIgnoresTransformations)) {
7129 QTransform viewToItemTransform = (item->deviceTransform(view->viewportTransform())).inverted();
7130 currentParentPos = mapToParent(viewToItemTransform.map(QPointF(view->mapFromGlobal(event->screenPos()))));
7131 buttonDownParentPos = mapToParent(viewToItemTransform.map(QPointF(view->mapFromGlobal(event->buttonDownScreenPos(Qt::LeftButton)))));
7132 }
else if (view && (item->flags() & ItemIgnoresTransformations)) {
7137 QTransform itemTransform;
7138 if (item->d_ptr->transformData)
7139 itemTransform = item->d_ptr->transformData->computedFullTransform();
7140 itemTransform.translate(item->d_ptr->pos.x(), item->d_ptr->pos.y());
7141 QTransform viewToParentTransform = itemTransform
7142 * (item->sceneTransform() * view->viewportTransform()).inverted();
7143 currentParentPos = viewToParentTransform.map(QPointF(view->mapFromGlobal(event->screenPos())));
7144 buttonDownParentPos = viewToParentTransform.map(QPointF(view->mapFromGlobal(event->buttonDownScreenPos(Qt::LeftButton))));
7147 currentParentPos = item->mapToParent(item->mapFromScene(event->scenePos()));
7148 buttonDownParentPos = item->mapToParent(item->mapFromScene(event->buttonDownScenePos(Qt::LeftButton)));
7151 item->setPos(initialPositions.value(item) + currentParentPos - buttonDownParentPos);
7153 if (item->flags() & ItemIsSelectable)
7154 item->setSelected(
true);
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183void QGraphicsItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
7185 if (event->button() == Qt::LeftButton && (flags() & ItemIsSelectable)) {
7186 bool multiSelect = (event->modifiers() & Qt::ControlModifier) != 0;
7187 if (event->scenePos() == event->buttonDownScenePos(Qt::LeftButton)) {
7190 setSelected(!isSelected());
7192 bool selectionChanged =
false;
7193 if (QGraphicsScene *scene = d_ptr->scene) {
7194 ++scene->d_func()->selectionChanging;
7198 if (d_ptr->selected) {
7199 scene->d_func()->selectedItems.remove(
this);
7200 for (QGraphicsItem *item : std::as_const(scene->d_func()->selectedItems)) {
7201 if (item->isSelected()) {
7202 selectionChanged =
true;
7207 scene->clearSelection();
7208 if (d_ptr->selected)
7209 scene->d_func()->selectedItems.insert(
this);
7210 --scene->d_func()->selectionChanging;
7211 if (selectionChanged)
7212 emit d_ptr->scene->selectionChanged();
7218 if (d_ptr->scene && !event->buttons())
7219 d_ptr->scene->d_func()->movingItemsInitialPositions.clear();
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246void QGraphicsItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
7248 mousePressEvent(event);
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265void QGraphicsItem::wheelEvent(QGraphicsSceneWheelEvent *event)
7271
7272
7273
7274
7275
7276
7277void QGraphicsItem::inputMethodEvent(QInputMethodEvent *event)
7283
7284
7285
7286
7287
7288
7289
7290
7291QVariant QGraphicsItem::inputMethodQuery(Qt::InputMethodQuery query)
const
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311Qt::InputMethodHints QGraphicsItem::inputMethodHints()
const
7313 Q_D(
const QGraphicsItem);
7318
7319
7320
7321
7322
7323
7324void QGraphicsItem::setInputMethodHints(Qt::InputMethodHints hints)
7330 d->scene->d_func()->updateInputMethodSensitivityInViews();
7331 QWidget *fw = QApplication::focusWidget();
7334 QGuiApplication::inputMethod()->update(Qt::ImHints);
7338
7339
7340
7341
7342
7343
7344void QGraphicsItem::updateMicroFocus()
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369QVariant QGraphicsItem::itemChange(GraphicsItemChange change,
const QVariant &value)
7376
7377
7378
7379
7380
7381bool QGraphicsItem::supportsExtension(Extension extension)
const
7383 Q_UNUSED(extension);
7388
7389
7390
7391
7392
7393void QGraphicsItem::setExtension(Extension extension,
const QVariant &variant)
7395 Q_UNUSED(extension);
7400
7401
7402
7403
7404
7405QVariant QGraphicsItem::extension(
const QVariant &variant)
const
7412
7413
7414
7415
7416
7417
7418void QGraphicsItem::addToIndex()
7420 if (d_ptr->ancestorFlags & QGraphicsItemPrivate::AncestorClipsChildren
7421 || d_ptr->ancestorFlags & QGraphicsItemPrivate::AncestorContainsChildren) {
7426 d_ptr->scene->d_func()->index->addItem(
this);
7430
7431
7432
7433
7434
7435
7436void QGraphicsItem::removeFromIndex()
7438 if (d_ptr->ancestorFlags & QGraphicsItemPrivate::AncestorClipsChildren
7439 || d_ptr->ancestorFlags & QGraphicsItemPrivate::AncestorContainsChildren) {
7444 d_ptr->scene->d_func()->index->removeItem(
this);
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460void QGraphicsItem::prepareGeometryChange()
7462 if (d_ptr->inDestructor)
7465 d_ptr->scene->d_func()->dirtyGrowingItemsBoundingRect =
true;
7466 d_ptr->geometryChanged = 1;
7467 d_ptr->paintedViewBoundingRectsNeedRepaint = 1;
7468 d_ptr->notifyBoundingRectChanged = !d_ptr->inSetPosHelper;
7470 QGraphicsScenePrivate *scenePrivate = d_ptr->scene->d_func();
7471 scenePrivate->index->prepareBoundingRectChange(
this);
7472 scenePrivate->markDirty(
this, QRectF(),
true,
false,
7480 if (scenePrivate->isSignalConnected(scenePrivate->changedSignalIndex)
7481 || scenePrivate->views.isEmpty()) {
7482 if (d_ptr->hasTranslateOnlySceneTransform()) {
7483 d_ptr->scene->update(boundingRect().translated(d_ptr->sceneTransform.dx(),
7484 d_ptr->sceneTransform.dy()));
7486 d_ptr->scene->update(d_ptr->sceneTransform.mapRect(boundingRect()));
7491 d_ptr->markParentDirty(
true);
7495
7496
7497
7498
7499void Q_WIDGETS_EXPORT qt_graphicsItem_highlightSelected(
7500 QGraphicsItem *item, QPainter *painter,
const QStyleOptionGraphicsItem *option)
7502 const QRectF murect = painter->transform().mapRect(QRectF(0, 0, 1, 1));
7503 if (qFuzzyIsNull(qMax(murect.width(), murect.height())))
7506 const QRectF mbrect = painter->transform().mapRect(item->boundingRect());
7507 if (qMin(mbrect.width(), mbrect.height()) < qreal(1.0))
7511 switch (item->type()) {
7512 case QGraphicsEllipseItem::Type:
7513 itemPenWidth =
static_cast<QGraphicsEllipseItem *>(item)->pen().widthF();
7515 case QGraphicsPathItem::Type:
7516 itemPenWidth =
static_cast<QGraphicsPathItem *>(item)->pen().widthF();
7518 case QGraphicsPolygonItem::Type:
7519 itemPenWidth =
static_cast<QGraphicsPolygonItem *>(item)->pen().widthF();
7521 case QGraphicsRectItem::Type:
7522 itemPenWidth =
static_cast<QGraphicsRectItem *>(item)->pen().widthF();
7524 case QGraphicsSimpleTextItem::Type:
7525 itemPenWidth =
static_cast<QGraphicsSimpleTextItem *>(item)->pen().widthF();
7527 case QGraphicsLineItem::Type:
7528 itemPenWidth =
static_cast<QGraphicsLineItem *>(item)->pen().widthF();
7533 const qreal pad = itemPenWidth / 2;
7535 const qreal penWidth = 0;
7537 const QColor fgcolor = option->palette.windowText().color();
7538 const QColor bgcolor(
7539 fgcolor.red() > 127 ? 0 : 255,
7540 fgcolor.green() > 127 ? 0 : 255,
7541 fgcolor.blue() > 127 ? 0 : 255);
7543 painter->setPen(QPen(bgcolor, penWidth, Qt::SolidLine));
7544 painter->setBrush(Qt::NoBrush);
7545 painter->drawRect(item->boundingRect().adjusted(pad, pad, -pad, -pad));
7547 painter->setPen(QPen(option->palette.windowText(), 0, Qt::DashLine));
7548 painter->setBrush(Qt::NoBrush);
7549 painter->drawRect(item->boundingRect().adjusted(pad, pad, -pad, -pad));
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7581
7582
7583QGraphicsObject::QGraphicsObject(QGraphicsItem *parent)
7584 : QGraphicsItem(parent)
7586 QGraphicsItem::d_ptr->isObject =
true;
7590
7591
7592QGraphicsObject::QGraphicsObject(QGraphicsItemPrivate &dd, QGraphicsItem *parent)
7593 : QGraphicsItem(dd, parent)
7595 QGraphicsItem::d_ptr->isObject =
true;
7599
7600
7601QGraphicsObject::~QGraphicsObject()
7606
7607
7608bool QGraphicsObject::event(QEvent *ev)
7610 if (ev->type() == QEvent::StyleAnimationUpdate) {
7617 return QObject::event(ev);
7620#ifndef QT_NO_GESTURES
7622
7623
7624
7625
7626void QGraphicsObject::grabGesture(Qt::GestureType gesture, Qt::GestureFlags flags)
7628 bool contains = QGraphicsItem::d_ptr->gestureContext.contains(gesture);
7629 QGraphicsItem::d_ptr->gestureContext.insert(gesture, flags);
7630 if (!contains && QGraphicsItem::d_ptr->scene)
7631 QGraphicsItem::d_ptr->scene->d_func()->grabGesture(
this, gesture);
7635
7636
7637
7638
7639void QGraphicsObject::ungrabGesture(Qt::GestureType gesture)
7641 if (QGraphicsItem::d_ptr->gestureContext.remove(gesture) && QGraphicsItem::d_ptr->scene)
7642 QGraphicsItem::d_ptr->scene->d_func()->ungrabGesture(
this, gesture);
7647
7648
7649
7650
7651
7652
7653void QGraphicsObject::updateMicroFocus()
7655 QGraphicsItem::updateMicroFocus();
7659
7660
7661
7662
7663qreal QGraphicsItemPrivate::width()
const
7669
7670
7671
7672
7673void QGraphicsItemPrivate::setWidth(qreal w)
7679
7680
7681
7682
7683void QGraphicsItemPrivate::resetWidth()
7688
7689
7690
7691
7692qreal QGraphicsItemPrivate::height()
const
7698
7699
7700
7701
7702void QGraphicsItemPrivate::setHeight(qreal h)
7708
7709
7710
7711
7712void QGraphicsItemPrivate::resetHeight()
7717
7718
7719
7720
7723
7724
7725
7726
7729
7730
7731
7732
7735
7736
7737
7738
7739
7740
7741
7742
7745
7746
7747
7748
7749
7752
7753
7754
7755
7756
7757
7760
7761
7762
7763
7766
7767
7768
7769
7770
7771
7772
7775
7776
7777
7778
7779
7780
7781
7784
7785
7786
7787
7788
7789
7792
7793
7794
7795
7796
7797
7798
7801
7802
7803
7804
7805
7806
7809
7810
7811
7812
7813
7814
7815
7818
7819
7820
7821
7822
7823
7826
7827
7828
7829
7830
7831
7834
7835
7836
7837
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7855
7856
7857
7858
7862
7863
7864
7865
7866
7867
7868
7869
7870
7873
7874
7875
7876
7877
7878
7881
7882
7883
7884
7885
7886
7887
7888
7889
7892
7893
7894
7895
7896
7897
7900
7901
7902
7903
7904
7905
7906
7907
7910
7911
7912
7915
7916
7917
7920
7921
7922
7923
7924
7925
7928
7929
7930
7931
7932
7933
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7957 Q_DECLARE_PUBLIC(QAbstractGraphicsShapeItem)
7968
7969
7970
7971QAbstractGraphicsShapeItem::QAbstractGraphicsShapeItem(QGraphicsItem *parent)
7972 : QGraphicsItem(*
new QAbstractGraphicsShapeItemPrivate, parent)
7977
7978
7979QAbstractGraphicsShapeItem::QAbstractGraphicsShapeItem(QAbstractGraphicsShapeItemPrivate &dd, QGraphicsItem *parent)
7980 : QGraphicsItem(dd, parent)
7985
7986
7987QAbstractGraphicsShapeItem::~QAbstractGraphicsShapeItem()
7992
7993
7994
7995QPen QAbstractGraphicsShapeItem::pen()
const
7997 Q_D(
const QAbstractGraphicsShapeItem);
8002
8003
8004
8005
8006
8007
8008void QAbstractGraphicsShapeItem::setPen(
const QPen &pen)
8010 Q_D(QAbstractGraphicsShapeItem);
8013 prepareGeometryChange();
8015 d->boundingRect = QRectF();
8020
8021
8022
8023
8024QBrush QAbstractGraphicsShapeItem::brush()
const
8026 Q_D(
const QAbstractGraphicsShapeItem);
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040void QAbstractGraphicsShapeItem::setBrush(
const QBrush &brush)
8042 Q_D(QAbstractGraphicsShapeItem);
8043 if (d->brush == brush)
8050
8051
8052bool QAbstractGraphicsShapeItem::isObscuredBy(
const QGraphicsItem *item)
const
8054 return QGraphicsItem::isObscuredBy(item);
8058
8059
8060QPainterPath QAbstractGraphicsShapeItem::opaqueArea()
const
8062 Q_D(
const QAbstractGraphicsShapeItem);
8063 if (d->brush.isOpaque())
8064 return isClipped() ? clipPath() : shape();
8065 return QGraphicsItem::opaqueArea();
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8097 Q_DECLARE_PUBLIC(QGraphicsPathItem)
8103
8104
8105
8106
8107
8108QGraphicsPathItem::QGraphicsPathItem(
const QPainterPath &path,
8109 QGraphicsItem *parent)
8110 : QAbstractGraphicsShapeItem(*
new QGraphicsPathItemPrivate, parent)
8112 if (!path.isEmpty())
8117
8118
8119
8120
8121
8122QGraphicsPathItem::QGraphicsPathItem(QGraphicsItem *parent)
8123 : QAbstractGraphicsShapeItem(*
new QGraphicsPathItemPrivate, parent)
8128
8129
8130QGraphicsPathItem::~QGraphicsPathItem()
8135
8136
8137
8138
8139
8140QPainterPath QGraphicsPathItem::path()
const
8142 Q_D(
const QGraphicsPathItem);
8147
8148
8149
8150
8151
8152
8153
8154void QGraphicsPathItem::setPath(
const QPainterPath &path)
8156 Q_D(QGraphicsPathItem);
8157 if (d->path == path)
8159 prepareGeometryChange();
8161 d->boundingRect = QRectF();
8166
8167
8168QRectF QGraphicsPathItem::boundingRect()
const
8170 Q_D(
const QGraphicsPathItem);
8171 if (d->boundingRect.isNull()) {
8172 qreal pw = pen().style() == Qt::NoPen ? qreal(0) : pen().widthF();
8174 d->boundingRect = d->path.controlPointRect();
8176 d->boundingRect = shape().controlPointRect();
8179 return d->boundingRect;
8183
8184
8185QPainterPath QGraphicsPathItem::shape()
const
8187 Q_D(
const QGraphicsPathItem);
8188 return qt_graphicsItem_shapeFromPath(d->path, d->pen);
8192
8193
8194bool QGraphicsPathItem::contains(
const QPointF &point)
const
8196 return QAbstractGraphicsShapeItem::contains(point);
8200
8201
8202void QGraphicsPathItem::paint(QPainter *painter,
const QStyleOptionGraphicsItem *option,
8205 Q_D(QGraphicsPathItem);
8207 painter->setPen(d->pen);
8208 painter->setBrush(d->brush);
8209 painter->drawPath(d->path);
8211 if (option->state & QStyle::State_Selected)
8212 qt_graphicsItem_highlightSelected(
this, painter, option);
8216
8217
8218bool QGraphicsPathItem::isObscuredBy(
const QGraphicsItem *item)
const
8220 return QAbstractGraphicsShapeItem::isObscuredBy(item);
8224
8225
8226QPainterPath QGraphicsPathItem::opaqueArea()
const
8228 return QAbstractGraphicsShapeItem::opaqueArea();
8232
8233
8234int QGraphicsPathItem::type()
const
8240
8241
8242bool QGraphicsPathItem::supportsExtension(Extension extension)
const
8244 Q_UNUSED(extension);
8249
8250
8251void QGraphicsPathItem::setExtension(Extension extension,
const QVariant &variant)
8253 Q_UNUSED(extension);
8258
8259
8260QVariant QGraphicsPathItem::extension(
const QVariant &variant)
const
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8309 Q_DECLARE_PUBLIC(QGraphicsRectItem)
8315
8316
8317
8318
8319
8320QGraphicsRectItem::QGraphicsRectItem(
const QRectF &rect, QGraphicsItem *parent)
8321 : QAbstractGraphicsShapeItem(*
new QGraphicsRectItemPrivate, parent)
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337QGraphicsRectItem::QGraphicsRectItem(qreal x, qreal y, qreal w, qreal h,
8338 QGraphicsItem *parent)
8339 : QAbstractGraphicsShapeItem(*
new QGraphicsRectItemPrivate, parent)
8341 setRect(QRectF(x, y, w, h));
8345
8346
8347
8348
8349
8350QGraphicsRectItem::QGraphicsRectItem(QGraphicsItem *parent)
8351 : QAbstractGraphicsShapeItem(*
new QGraphicsRectItemPrivate, parent)
8356
8357
8358QGraphicsRectItem::~QGraphicsRectItem()
8363
8364
8365
8366
8367QRectF QGraphicsRectItem::rect()
const
8369 Q_D(
const QGraphicsRectItem);
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383void QGraphicsRectItem::setRect(
const QRectF &rect)
8385 Q_D(QGraphicsRectItem);
8386 if (d->rect == rect)
8388 prepareGeometryChange();
8390 d->boundingRect = QRectF();
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8408
8409
8410QRectF QGraphicsRectItem::boundingRect()
const
8412 Q_D(
const QGraphicsRectItem);
8413 if (d->boundingRect.isNull()) {
8414 qreal halfpw = pen().style() == Qt::NoPen ? qreal(0) : pen().widthF() / 2;
8415 d->boundingRect = d->rect;
8417 d->boundingRect.adjust(-halfpw, -halfpw, halfpw, halfpw);
8419 return d->boundingRect;
8423
8424
8425QPainterPath QGraphicsRectItem::shape()
const
8427 Q_D(
const QGraphicsRectItem);
8429 path.addRect(d->rect);
8430 return qt_graphicsItem_shapeFromPath(path, d->pen);
8434
8435
8436bool QGraphicsRectItem::contains(
const QPointF &point)
const
8438 return QAbstractGraphicsShapeItem::contains(point);
8442
8443
8444void QGraphicsRectItem::paint(QPainter *painter,
const QStyleOptionGraphicsItem *option,
8447 Q_D(QGraphicsRectItem);
8449 painter->setPen(d->pen);
8450 painter->setBrush(d->brush);
8451 painter->drawRect(d->rect);
8453 if (option->state & QStyle::State_Selected)
8454 qt_graphicsItem_highlightSelected(
this, painter, option);
8458
8459
8460bool QGraphicsRectItem::isObscuredBy(
const QGraphicsItem *item)
const
8462 return QAbstractGraphicsShapeItem::isObscuredBy(item);
8466
8467
8468QPainterPath QGraphicsRectItem::opaqueArea()
const
8470 return QAbstractGraphicsShapeItem::opaqueArea();
8474
8475
8476int QGraphicsRectItem::type()
const
8482
8483
8484bool QGraphicsRectItem::supportsExtension(Extension extension)
const
8486 Q_UNUSED(extension);
8491
8492
8493void QGraphicsRectItem::setExtension(Extension extension,
const QVariant &variant)
8495 Q_UNUSED(extension);
8500
8501
8502QVariant QGraphicsRectItem::extension(
const QVariant &variant)
const
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8544 Q_DECLARE_PUBLIC(QGraphicsEllipseItem)
8556
8557
8558
8559
8560
8561QGraphicsEllipseItem::QGraphicsEllipseItem(
const QRectF &rect, QGraphicsItem *parent)
8562 : QAbstractGraphicsShapeItem(*
new QGraphicsEllipseItemPrivate, parent)
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577QGraphicsEllipseItem::QGraphicsEllipseItem(qreal x, qreal y, qreal w, qreal h,
8578 QGraphicsItem *parent)
8579 : QAbstractGraphicsShapeItem(*
new QGraphicsEllipseItemPrivate, parent)
8587
8588
8589
8590
8591
8592QGraphicsEllipseItem::QGraphicsEllipseItem(QGraphicsItem *parent)
8593 : QAbstractGraphicsShapeItem(*
new QGraphicsEllipseItemPrivate, parent)
8598
8599
8600QGraphicsEllipseItem::~QGraphicsEllipseItem()
8605
8606
8607
8608
8609QRectF QGraphicsEllipseItem::rect()
const
8611 Q_D(
const QGraphicsEllipseItem);
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626void QGraphicsEllipseItem::setRect(
const QRectF &rect)
8628 Q_D(QGraphicsEllipseItem);
8629 if (d->rect == rect)
8631 prepareGeometryChange();
8633 d->boundingRect = QRectF();
8638
8639
8640
8641
8642
8643
8644int QGraphicsEllipseItem::startAngle()
const
8646 Q_D(
const QGraphicsEllipseItem);
8647 return d->startAngle;
8651
8652
8653
8654
8655
8656
8657void QGraphicsEllipseItem::setStartAngle(
int angle)
8659 Q_D(QGraphicsEllipseItem);
8660 if (angle != d->startAngle) {
8661 prepareGeometryChange();
8662 d->boundingRect = QRectF();
8663 d->startAngle = angle;
8669
8670
8671
8672
8673
8674
8675
8676int QGraphicsEllipseItem::spanAngle()
const
8678 Q_D(
const QGraphicsEllipseItem);
8679 return d->spanAngle;
8683
8684
8685
8686
8687
8688
8689
8690void QGraphicsEllipseItem::setSpanAngle(
int angle)
8692 Q_D(QGraphicsEllipseItem);
8693 if (angle != d->spanAngle) {
8694 prepareGeometryChange();
8695 d->boundingRect = QRectF();
8696 d->spanAngle = angle;
8702
8703
8704QRectF QGraphicsEllipseItem::boundingRect()
const
8706 Q_D(
const QGraphicsEllipseItem);
8707 if (d->boundingRect.isNull()) {
8708 qreal pw = pen().style() == Qt::NoPen ? qreal(0) : pen().widthF();
8709 if (pw == 0.0 && d->spanAngle == 360 * 16)
8710 d->boundingRect = d->rect;
8712 d->boundingRect = shape().controlPointRect();
8714 return d->boundingRect;
8718
8719
8720QPainterPath QGraphicsEllipseItem::shape()
const
8722 Q_D(
const QGraphicsEllipseItem);
8724 if (d->rect.isNull())
8726 if (d->spanAngle != 360 * 16) {
8727 path.moveTo(d->rect.center());
8728 path.arcTo(d->rect, d->startAngle / 16.0, d->spanAngle / 16.0);
8730 path.addEllipse(d->rect);
8733 return qt_graphicsItem_shapeFromPath(path, d->pen);
8737
8738
8739bool QGraphicsEllipseItem::contains(
const QPointF &point)
const
8741 return QAbstractGraphicsShapeItem::contains(point);
8745
8746
8747void QGraphicsEllipseItem::paint(QPainter *painter,
const QStyleOptionGraphicsItem *option,
8750 Q_D(QGraphicsEllipseItem);
8752 painter->setPen(d->pen);
8753 painter->setBrush(d->brush);
8754 if ((d->spanAngle != 0) && (qAbs(d->spanAngle) % (360 * 16) == 0))
8755 painter->drawEllipse(d->rect);
8757 painter->drawPie(d->rect, d->startAngle, d->spanAngle);
8759 if (option->state & QStyle::State_Selected)
8760 qt_graphicsItem_highlightSelected(
this, painter, option);
8764
8765
8766bool QGraphicsEllipseItem::isObscuredBy(
const QGraphicsItem *item)
const
8768 return QAbstractGraphicsShapeItem::isObscuredBy(item);
8772
8773
8774QPainterPath QGraphicsEllipseItem::opaqueArea()
const
8776 return QAbstractGraphicsShapeItem::opaqueArea();
8780
8781
8782int QGraphicsEllipseItem::type()
const
8789
8790
8791bool QGraphicsEllipseItem::supportsExtension(Extension extension)
const
8793 Q_UNUSED(extension);
8798
8799
8800void QGraphicsEllipseItem::setExtension(Extension extension,
const QVariant &variant)
8802 Q_UNUSED(extension);
8807
8808
8809QVariant QGraphicsEllipseItem::extension(
const QVariant &variant)
const
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8843 Q_DECLARE_PUBLIC(QGraphicsPolygonItem)
8854
8855
8856
8857
8858
8859QGraphicsPolygonItem::QGraphicsPolygonItem(
const QPolygonF &polygon, QGraphicsItem *parent)
8860 : QAbstractGraphicsShapeItem(*
new QGraphicsPolygonItemPrivate, parent)
8862 setPolygon(polygon);
8866
8867
8868
8869
8870
8871QGraphicsPolygonItem::QGraphicsPolygonItem(QGraphicsItem *parent)
8872 : QAbstractGraphicsShapeItem(*
new QGraphicsPolygonItemPrivate, parent)
8877
8878
8879QGraphicsPolygonItem::~QGraphicsPolygonItem()
8884
8885
8886
8887
8888
8889QPolygonF QGraphicsPolygonItem::polygon()
const
8891 Q_D(
const QGraphicsPolygonItem);
8896
8897
8898
8899
8900
8901
8902
8903void QGraphicsPolygonItem::setPolygon(
const QPolygonF &polygon)
8905 Q_D(QGraphicsPolygonItem);
8906 if (d->polygon == polygon)
8908 prepareGeometryChange();
8909 d->polygon = polygon;
8910 d->boundingRect = QRectF();
8915
8916
8917
8918
8919
8920Qt::FillRule QGraphicsPolygonItem::fillRule()
const
8922 Q_D(
const QGraphicsPolygonItem);
8927
8928
8929
8930
8931
8932void QGraphicsPolygonItem::setFillRule(Qt::FillRule rule)
8934 Q_D(QGraphicsPolygonItem);
8935 if (rule != d->fillRule) {
8942
8943
8944QRectF QGraphicsPolygonItem::boundingRect()
const
8946 Q_D(
const QGraphicsPolygonItem);
8947 if (d->boundingRect.isNull()) {
8948 qreal pw = pen().style() == Qt::NoPen ? qreal(0) : pen().widthF();
8950 d->boundingRect = d->polygon.boundingRect();
8952 d->boundingRect = shape().controlPointRect();
8954 return d->boundingRect;
8958
8959
8960QPainterPath QGraphicsPolygonItem::shape()
const
8962 Q_D(
const QGraphicsPolygonItem);
8964 path.addPolygon(d->polygon);
8965 return qt_graphicsItem_shapeFromPath(path, d->pen);
8969
8970
8971bool QGraphicsPolygonItem::contains(
const QPointF &point)
const
8973 return QAbstractGraphicsShapeItem::contains(point);
8977
8978
8979void QGraphicsPolygonItem::paint(QPainter *painter,
const QStyleOptionGraphicsItem *option, QWidget *widget)
8981 Q_D(QGraphicsPolygonItem);
8983 painter->setPen(d->pen);
8984 painter->setBrush(d->brush);
8985 painter->drawPolygon(d->polygon, d->fillRule);
8987 if (option->state & QStyle::State_Selected)
8988 qt_graphicsItem_highlightSelected(
this, painter, option);
8992
8993
8994bool QGraphicsPolygonItem::isObscuredBy(
const QGraphicsItem *item)
const
8996 return QAbstractGraphicsShapeItem::isObscuredBy(item);
9000
9001
9002QPainterPath QGraphicsPolygonItem::opaqueArea()
const
9004 return QAbstractGraphicsShapeItem::opaqueArea();
9008
9009
9010int QGraphicsPolygonItem::type()
const
9016
9017
9018bool QGraphicsPolygonItem::supportsExtension(Extension extension)
const
9020 Q_UNUSED(extension);
9025
9026
9027void QGraphicsPolygonItem::setExtension(Extension extension,
const QVariant &variant)
9029 Q_UNUSED(extension);
9034
9035
9036QVariant QGraphicsPolygonItem::extension(
const QVariant &variant)
const
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9069 Q_DECLARE_PUBLIC(QGraphicsLineItem)
9076
9077
9078
9079
9080
9081QGraphicsLineItem::QGraphicsLineItem(
const QLineF &line, QGraphicsItem *parent)
9082 : QGraphicsItem(*
new QGraphicsLineItemPrivate, parent)
9088
9089
9090
9091
9092
9093
9094QGraphicsLineItem::QGraphicsLineItem(qreal x1, qreal y1, qreal x2, qreal y2, QGraphicsItem *parent)
9095 : QGraphicsItem(*
new QGraphicsLineItemPrivate, parent)
9097 setLine(x1, y1, x2, y2);
9103
9104
9105
9106
9107
9108QGraphicsLineItem::QGraphicsLineItem(QGraphicsItem *parent)
9109 : QGraphicsItem(*
new QGraphicsLineItemPrivate, parent)
9114
9115
9116QGraphicsLineItem::~QGraphicsLineItem()
9121
9122
9123
9124
9125
9126QPen QGraphicsLineItem::pen()
const
9128 Q_D(
const QGraphicsLineItem);
9133
9134
9135
9136
9137
9138void QGraphicsLineItem::setPen(
const QPen &pen)
9140 Q_D(QGraphicsLineItem);
9143 prepareGeometryChange();
9149
9150
9151
9152
9153QLineF QGraphicsLineItem::line()
const
9155 Q_D(
const QGraphicsLineItem);
9160
9161
9162
9163
9164
9165
9166
9167void QGraphicsLineItem::setLine(
const QLineF &line)
9169 Q_D(QGraphicsLineItem);
9170 if (d->line == line)
9172 prepareGeometryChange();
9178
9179
9180
9181
9182
9183
9184
9185
9188
9189
9190QRectF QGraphicsLineItem::boundingRect()
const
9192 Q_D(
const QGraphicsLineItem);
9193 if (d->pen.widthF() == 0.0) {
9194 const qreal x1 = d->line.p1().x();
9195 const qreal x2 = d->line.p2().x();
9196 const qreal y1 = d->line.p1().y();
9197 const qreal y2 = d->line.p2().y();
9198 qreal lx = qMin(x1, x2);
9199 qreal rx = qMax(x1, x2);
9200 qreal ty = qMin(y1, y2);
9201 qreal by = qMax(y1, y2);
9202 return QRectF(lx, ty, rx - lx, by - ty);
9204 return shape().controlPointRect();
9208
9209
9210QPainterPath QGraphicsLineItem::shape()
const
9212 Q_D(
const QGraphicsLineItem);
9214 if (d->line == QLineF())
9217 path.moveTo(d->line.p1());
9218 path.lineTo(d->line.p2());
9219 return qt_graphicsItem_shapeFromPath(path, d->pen);
9223
9224
9225bool QGraphicsLineItem::contains(
const QPointF &point)
const
9227 return QGraphicsItem::contains(point);
9231
9232
9233void QGraphicsLineItem::paint(QPainter *painter,
const QStyleOptionGraphicsItem *option, QWidget *widget)
9235 Q_D(QGraphicsLineItem);
9237 painter->setPen(d->pen);
9238 painter->drawLine(d->line);
9240 if (option->state & QStyle::State_Selected)
9241 qt_graphicsItem_highlightSelected(
this, painter, option);
9245
9246
9247bool QGraphicsLineItem::isObscuredBy(
const QGraphicsItem *item)
const
9249 return QGraphicsItem::isObscuredBy(item);
9253
9254
9255QPainterPath QGraphicsLineItem::opaqueArea()
const
9257 return QGraphicsItem::opaqueArea();
9261
9262
9263int QGraphicsLineItem::type()
const
9269
9270
9271bool QGraphicsLineItem::supportsExtension(Extension extension)
const
9273 Q_UNUSED(extension);
9278
9279
9280void QGraphicsLineItem::setExtension(Extension extension,
const QVariant &variant)
9282 Q_UNUSED(extension);
9287
9288
9289QVariant QGraphicsLineItem::extension(
const QVariant &variant)
const
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350Q_GUI_EXPORT
extern QPainterPath qt_regionToPath(
const QRegion ®ion);
9354 Q_DECLARE_PUBLIC(QGraphicsPixmapItem)
9371 shape = QPainterPath();
9372 switch (shapeMode) {
9373 case QGraphicsPixmapItem::MaskShape: {
9374 QBitmap mask = pixmap.mask();
9375 if (!mask.isNull()) {
9376 shape = qt_regionToPath(QRegion(mask).translated(offset.toPoint()));
9381 case QGraphicsPixmapItem::BoundingRectShape:
9382 shape.addRect(QRectF(offset.x(), offset.y(), pixmap.width(), pixmap.height()));
9384 case QGraphicsPixmapItem::HeuristicMaskShape:
9385#ifndef QT_NO_IMAGE_HEURISTIC_MASK
9386 shape = qt_regionToPath(QRegion(pixmap.createHeuristicMask()).translated(offset.toPoint()));
9388 shape.addRect(QRectF(offset.x(), offset.y(), pixmap.width(), pixmap.height()));
9396
9397
9398
9399
9400
9401QGraphicsPixmapItem::QGraphicsPixmapItem(
const QPixmap &pixmap, QGraphicsItem *parent)
9402 : QGraphicsItem(*
new QGraphicsPixmapItemPrivate, parent)
9408
9409
9410
9411
9412
9413QGraphicsPixmapItem::QGraphicsPixmapItem(QGraphicsItem *parent)
9414 : QGraphicsItem(*
new QGraphicsPixmapItemPrivate, parent)
9419
9420
9421QGraphicsPixmapItem::~QGraphicsPixmapItem()
9426
9427
9428
9429
9430void QGraphicsPixmapItem::setPixmap(
const QPixmap &pixmap)
9432 Q_D(QGraphicsPixmapItem);
9433 prepareGeometryChange();
9435 d->hasShape =
false;
9440
9441
9442
9443
9444
9445QPixmap QGraphicsPixmapItem::pixmap()
const
9447 Q_D(
const QGraphicsPixmapItem);
9452
9453
9454
9455
9456
9457
9458Qt::TransformationMode QGraphicsPixmapItem::transformationMode()
const
9460 Q_D(
const QGraphicsPixmapItem);
9461 return d->transformationMode;
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475void QGraphicsPixmapItem::setTransformationMode(Qt::TransformationMode mode)
9477 Q_D(QGraphicsPixmapItem);
9478 if (mode != d->transformationMode) {
9479 d->transformationMode = mode;
9485
9486
9487
9488
9489
9490QPointF QGraphicsPixmapItem::offset()
const
9492 Q_D(
const QGraphicsPixmapItem);
9497
9498
9499
9500
9501
9502void QGraphicsPixmapItem::setOffset(
const QPointF &offset)
9504 Q_D(QGraphicsPixmapItem);
9505 if (d->offset == offset)
9507 prepareGeometryChange();
9509 d->hasShape =
false;
9514
9515
9516
9517
9518
9521
9522
9523QRectF QGraphicsPixmapItem::boundingRect()
const
9525 Q_D(
const QGraphicsPixmapItem);
9526 if (d->pixmap.isNull())
9528 if (d->flags & ItemIsSelectable) {
9530 return QRectF(d->offset, d->pixmap.deviceIndependentSize()).adjusted(-pw/2, -pw/2, pw/2, pw/2);
9532 return QRectF(d->offset, d->pixmap.deviceIndependentSize());
9537
9538
9539QPainterPath QGraphicsPixmapItem::shape()
const
9541 Q_D(
const QGraphicsPixmapItem);
9543 QGraphicsPixmapItemPrivate *thatD =
const_cast<QGraphicsPixmapItemPrivate *>(d);
9544 thatD->updateShape();
9545 thatD->hasShape =
true;
9547 return d_func()->shape;
9551
9552
9553bool QGraphicsPixmapItem::contains(
const QPointF &point)
const
9555 return QGraphicsItem::contains(point);
9559
9560
9561void QGraphicsPixmapItem::paint(QPainter *painter,
const QStyleOptionGraphicsItem *option,
9564 Q_D(QGraphicsPixmapItem);
9567 painter->setRenderHint(QPainter::SmoothPixmapTransform,
9568 (d->transformationMode == Qt::SmoothTransformation));
9570 painter->drawPixmap(d->offset, d->pixmap);
9572 if (option->state & QStyle::State_Selected)
9573 qt_graphicsItem_highlightSelected(
this, painter, option);
9577
9578
9579bool QGraphicsPixmapItem::isObscuredBy(
const QGraphicsItem *item)
const
9581 return QGraphicsItem::isObscuredBy(item);
9585
9586
9587QPainterPath QGraphicsPixmapItem::opaqueArea()
const
9593
9594
9595int QGraphicsPixmapItem::type()
const
9601
9602
9603
9604
9605
9606QGraphicsPixmapItem::ShapeMode QGraphicsPixmapItem::shapeMode()
const
9608 return d_func()->shapeMode;
9612
9613
9614
9615
9616
9617void QGraphicsPixmapItem::setShapeMode(ShapeMode mode)
9619 Q_D(QGraphicsPixmapItem);
9620 if (d->shapeMode == mode)
9622 d->shapeMode = mode;
9623 d->hasShape =
false;
9627
9628
9629bool QGraphicsPixmapItem::supportsExtension(Extension extension)
const
9631 Q_UNUSED(extension);
9636
9637
9638void QGraphicsPixmapItem::setExtension(Extension extension,
const QVariant &variant)
9640 Q_UNUSED(extension);
9645
9646
9647QVariant QGraphicsPixmapItem::extension(
const QVariant &variant)
const
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9702 {
return QPointF(0., pageNumber * control->document()->pageSize().height()); }
9704 {
if (control) control->processEvent(e, controlOffset()); }
9723
9724
9725
9726
9727
9728QGraphicsTextItem::QGraphicsTextItem(
const QString &text, QGraphicsItem *parent)
9729 : QGraphicsObject(*
new QGraphicsItemPrivate, parent),
9730 dd(
new QGraphicsTextItemPrivate)
9733 if (!text.isEmpty())
9735 setAcceptDrops(
true);
9736 setAcceptHoverEvents(
true);
9737 setFlags(ItemUsesExtendedStyleOption);
9741
9742
9743
9744
9745
9746QGraphicsTextItem::QGraphicsTextItem(QGraphicsItem *parent)
9747 : QGraphicsObject(*
new QGraphicsItemPrivate, parent),
9748 dd(
new QGraphicsTextItemPrivate)
9751 setAcceptDrops(
true);
9752 setAcceptHoverEvents(
true);
9753 setFlag(ItemUsesExtendedStyleOption);
9757
9758
9759QGraphicsTextItem::~QGraphicsTextItem()
9765
9766
9767
9768
9769QString QGraphicsTextItem::toHtml()
const
9771#ifndef QT_NO_TEXTHTMLPARSER
9773 return dd->control->toHtml();
9779
9780
9781
9782
9783
9784
9785void QGraphicsTextItem::setHtml(
const QString &text)
9787 dd->textControl()->setHtml(text);
9791
9792
9793
9794
9795QString QGraphicsTextItem::toPlainText()
const
9798 return dd->control->toPlainText();
9803
9804
9805
9806
9807
9808
9809void QGraphicsTextItem::setPlainText(
const QString &text)
9811 dd->textControl()->setPlainText(text);
9815
9816
9817
9818
9819QFont QGraphicsTextItem::font()
const
9823 return dd->control->document()->defaultFont();
9827
9828
9829
9830
9831void QGraphicsTextItem::setFont(
const QFont &font)
9833 dd->textControl()->document()->setDefaultFont(font);
9837
9838
9839void QGraphicsTextItem::setDefaultTextColor(
const QColor &col)
9841 QWidgetTextControl *c = dd->textControl();
9842 QPalette pal = c->palette();
9843 QColor old = pal.color(QPalette::Text);
9844 pal.setColor(QPalette::Text, col);
9851
9852
9853QColor QGraphicsTextItem::defaultTextColor()
const
9855 return dd->textControl()->palette().color(QPalette::Text);
9859
9860
9861QRectF QGraphicsTextItem::boundingRect()
const
9863 return dd->boundingRect;
9867
9868
9869QPainterPath QGraphicsTextItem::shape()
const
9872 return QPainterPath();
9874 path.addRect(dd->boundingRect);
9879
9880
9881bool QGraphicsTextItem::contains(
const QPointF &point)
const
9883 return dd->boundingRect.contains(point);
9887
9888
9889void QGraphicsTextItem::paint(QPainter *painter,
const QStyleOptionGraphicsItem *option,
9895 QRectF r = option->exposedRect;
9896 painter->translate(-dd->controlOffset());
9897 r.translate(dd->controlOffset());
9899 QTextDocument *doc = dd->control->document();
9900 QTextDocumentLayout *layout = qobject_cast<QTextDocumentLayout *>(doc->documentLayout());
9905 layout->setViewport(dd->boundingRect);
9907 dd->control->drawContents(painter, r);
9910 layout->setViewport(QRect());
9915 if (option->state & (QStyle::State_Selected | QStyle::State_HasFocus))
9916 qt_graphicsItem_highlightSelected(
this, painter, option);
9920
9921
9922bool QGraphicsTextItem::isObscuredBy(
const QGraphicsItem *item)
const
9924 return QGraphicsItem::isObscuredBy(item);
9928
9929
9930QPainterPath QGraphicsTextItem::opaqueArea()
const
9932 return QGraphicsItem::opaqueArea();
9936
9937
9938int QGraphicsTextItem::type()
const
9944
9945
9946
9947
9948
9949
9950
9951
9952
9953
9954
9955
9956
9957
9958
9959void QGraphicsTextItem::setTextWidth(qreal width)
9961 dd->textControl()->setTextWidth(width);
9965
9966
9967
9968
9969
9970
9971
9972qreal QGraphicsTextItem::textWidth()
const
9976 return dd->control->textWidth();
9980
9981
9982void QGraphicsTextItem::adjustSize()
9985 dd->control->adjustSize();
9989
9990
9991void QGraphicsTextItem::setDocument(QTextDocument *document)
9993 dd->textControl()->setDocument(document);
9994 dd->_q_updateBoundingRect(dd->control->size());
9998
9999
10000QTextDocument *QGraphicsTextItem::document()
const
10002 return dd->textControl()->document();
10006
10007
10008bool QGraphicsTextItem::sceneEvent(QEvent *event)
10010 QEvent::Type t = event->type();
10011 if (!dd->tabChangesFocus && (t == QEvent::KeyPress || t == QEvent::KeyRelease)) {
10012 int k = ((QKeyEvent *)event)->key();
10013 if (k == Qt::Key_Tab || k == Qt::Key_Backtab) {
10014 dd->sendControlEvent(event);
10018 bool result = QGraphicsItem::sceneEvent(event);
10021 switch (event->type()) {
10022 case QEvent::ContextMenu:
10023 case QEvent::FocusIn:
10024 case QEvent::FocusOut:
10025 case QEvent::GraphicsSceneDragEnter:
10026 case QEvent::GraphicsSceneDragLeave:
10027 case QEvent::GraphicsSceneDragMove:
10028 case QEvent::GraphicsSceneDrop:
10029 case QEvent::GraphicsSceneHoverEnter:
10030 case QEvent::GraphicsSceneHoverLeave:
10031 case QEvent::GraphicsSceneHoverMove:
10032 case QEvent::GraphicsSceneMouseDoubleClick:
10033 case QEvent::GraphicsSceneMousePress:
10034 case QEvent::GraphicsSceneMouseMove:
10035 case QEvent::GraphicsSceneMouseRelease:
10036 case QEvent::KeyPress:
10037 case QEvent::KeyRelease:
10038 case QEvent::InputMethod:
10041 if (event->type() == QEvent::FocusIn || event->type() == QEvent::FocusOut) {
10042 QGuiApplication::inputMethod()->reset();
10044 QGuiApplication::inputMethod()->update(Qt::ImQueryInput);
10047 case QEvent::ShortcutOverride:
10048 dd->sendControlEvent(event);
10058
10059
10060void QGraphicsTextItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
10062 if ((QGraphicsItem::d_ptr->flags & (ItemIsSelectable | ItemIsMovable))
10063 && (event->buttons() & Qt::LeftButton) && dd->_q_mouseOnEdge(event)) {
10066 dd->useDefaultImpl =
true;
10067 }
else if (event->buttons() == event->button()
10068 && dd->control->textInteractionFlags() == Qt::NoTextInteraction) {
10070 dd->useDefaultImpl =
true;
10072 if (dd->useDefaultImpl) {
10073 QGraphicsItem::mousePressEvent(event);
10074 if (!event->isAccepted())
10075 dd->useDefaultImpl =
false;
10079 dd->sendControlEvent(event);
10083
10084
10085void QGraphicsTextItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
10087 if (dd->useDefaultImpl) {
10088 QGraphicsItem::mouseMoveEvent(event);
10092 dd->sendControlEvent(event);
10096
10097
10098void QGraphicsTextItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
10100 if (dd->useDefaultImpl) {
10101 QGraphicsItem::mouseReleaseEvent(event);
10102 if (dd->control->textInteractionFlags() == Qt::NoTextInteraction
10103 && !event->buttons()) {
10105 dd->useDefaultImpl =
false;
10106 }
else if ((event->buttons() & Qt::LeftButton) == 0) {
10108 dd->useDefaultImpl =
false;
10113 QWidget *widget = event->widget();
10114 if (widget && (dd->control->textInteractionFlags() & Qt::TextEditable) && boundingRect().contains(event->pos())) {
10115 qt_widget_private(widget)->handleSoftwareInputPanel(event->button(), dd->clickCausedFocus);
10117 dd->clickCausedFocus = 0;
10118 dd->sendControlEvent(event);
10122
10123
10124void QGraphicsTextItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
10126 if (dd->useDefaultImpl) {
10127 QGraphicsItem::mouseDoubleClickEvent(event);
10132 QGraphicsItem::mouseDoubleClickEvent(event);
10136 dd->sendControlEvent(event);
10140
10141
10142void QGraphicsTextItem::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
10144 dd->sendControlEvent(event);
10148
10149
10150void QGraphicsTextItem::keyPressEvent(QKeyEvent *event)
10152 dd->sendControlEvent(event);
10156
10157
10158void QGraphicsTextItem::keyReleaseEvent(QKeyEvent *event)
10160 dd->sendControlEvent(event);
10164
10165
10166void QGraphicsTextItem::focusInEvent(QFocusEvent *event)
10168 dd->sendControlEvent(event);
10169 if (event->reason() == Qt::MouseFocusReason) {
10170 dd->clickCausedFocus = 1;
10176
10177
10178void QGraphicsTextItem::focusOutEvent(QFocusEvent *event)
10180 dd->sendControlEvent(event);
10185
10186
10187void QGraphicsTextItem::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
10189 dd->sendControlEvent(event);
10193
10194
10195void QGraphicsTextItem::dragLeaveEvent(QGraphicsSceneDragDropEvent *event)
10197 dd->sendControlEvent(event);
10201
10202
10203void QGraphicsTextItem::dragMoveEvent(QGraphicsSceneDragDropEvent *event)
10205 dd->sendControlEvent(event);
10209
10210
10211void QGraphicsTextItem::dropEvent(QGraphicsSceneDragDropEvent *event)
10213 dd->sendControlEvent(event);
10217
10218
10219void QGraphicsTextItem::inputMethodEvent(QInputMethodEvent *event)
10221 dd->sendControlEvent(event);
10225
10226
10227void QGraphicsTextItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
10229 dd->sendControlEvent(event);
10233
10234
10235void QGraphicsTextItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event)
10237 dd->sendControlEvent(event);
10241
10242
10243void QGraphicsTextItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
10245 dd->sendControlEvent(event);
10249
10250
10251QVariant QGraphicsTextItem::inputMethodQuery(Qt::InputMethodQuery query)
const
10254 if (query == Qt::ImEnabled)
10255 return isEnabled();
10256 else if (query == Qt::ImHints)
10257 v =
int(inputMethodHints());
10258 else if (dd->control)
10259 v = dd->control->inputMethodQuery(query, QVariant());
10261 if (v.userType() == QMetaType::QRectF)
10262 v = v.toRectF().translated(-dd->controlOffset());
10263 else if (v.userType() == QMetaType::QPointF)
10264 v = v.toPointF() - dd->controlOffset();
10265 else if (v.userType() == QMetaType::QRect)
10266 v = v.toRect().translated(-dd->controlOffset().toPoint());
10267 else if (v.userType() == QMetaType::QPoint)
10268 v = v.toPoint() - dd->controlOffset().toPoint();
10274
10275
10276bool QGraphicsTextItem::supportsExtension(Extension extension)
const
10278 Q_UNUSED(extension);
10283
10284
10285void QGraphicsTextItem::setExtension(Extension extension,
const QVariant &variant)
10287 Q_UNUSED(extension);
10292
10293
10294QVariant QGraphicsTextItem::extension(
const QVariant &variant)
const
10301
10302
10305 if (rect.isValid()) {
10306 rect.translate(-controlOffset());
10308 rect = boundingRect;
10310 if (rect.intersects(boundingRect))
10315
10316
10319 if (size != boundingRect.size()) {
10320 qq->prepareGeometryChange();
10321 boundingRect.setSize(size);
10327
10328
10331 if (
qq->hasFocus()) {
10332 rect.translate(-controlOffset());
10333 qq->ensureVisible(rect, 0, 0);
10340 QGraphicsTextItem *that =
const_cast<QGraphicsTextItem *>(
qq);
10341 control =
new QWidgetTextControl(that);
10342 control->setTextInteractionFlags(Qt::NoTextInteraction);
10344 QObject::connect(control, &QWidgetTextControl::updateRequest, qq,
10345 [dd = that->dd](
const QRectF &rect) { dd->_q_update(rect); });
10346 QObject::connect(control, &QWidgetTextControl::documentSizeChanged, qq,
10347 [dd = that->dd](QSizeF size) { dd->_q_updateBoundingRect(size); });
10348 QObject::connect(control, &QWidgetTextControl::visibilityRequest, qq,
10349 [dd = that->dd](
const QRectF &rect) { dd->_q_ensureVisible(rect); });
10350 QObject::connect(control, &QWidgetTextControl::linkActivated, qq,
10351 &QGraphicsTextItem::linkActivated);
10352 QObject::connect(control, &QWidgetTextControl::linkHovered, qq,
10353 &QGraphicsTextItem::linkHovered);
10355 const QSizeF pgSize = control->document()->pageSize();
10356 if (pgSize.height() != -1) {
10357 qq->prepareGeometryChange();
10358 that->dd->boundingRect.setSize(pgSize);
10361 that->dd->_q_updateBoundingRect(control->size());
10368
10369
10373 path.addRect(
qq->boundingRect());
10375 QPainterPath docPath;
10376 const QTextFrameFormat format = control->document()->rootFrame()->frameFormat();
10378 qq->boundingRect().adjusted(
10379 format.leftMargin(),
10380 format.topMargin(),
10381 -format.rightMargin(),
10382 -format.bottomMargin()));
10384 return path.subtracted(docPath).contains(event->pos());
10388
10389
10390
10391
10392
10393
10394
10395
10398
10399
10400
10401
10402
10403
10404
10405
10408
10409
10410
10411
10412
10413
10414
10415
10416
10417
10418void QGraphicsTextItem::setTextInteractionFlags(Qt::TextInteractionFlags flags)
10420 if (flags == Qt::NoTextInteraction)
10421 setFlags(
this->flags() & ~(QGraphicsItem::ItemIsFocusable | QGraphicsItem::ItemAcceptsInputMethod));
10423 setFlags(
this->flags() | QGraphicsItem::ItemIsFocusable | QGraphicsItem::ItemAcceptsInputMethod);
10425 dd->textControl()->setTextInteractionFlags(flags);
10429
10430
10431
10432
10433Qt::TextInteractionFlags QGraphicsTextItem::textInteractionFlags()
const
10436 return Qt::NoTextInteraction;
10437 return dd->control->textInteractionFlags();
10441
10442
10443
10444
10445
10446
10447
10448
10449
10450
10451
10452void QGraphicsTextItem::setTabChangesFocus(
bool b)
10454 dd->tabChangesFocus = b;
10458
10459
10460
10461
10462
10463
10464
10465
10466
10467bool QGraphicsTextItem::tabChangesFocus()
const
10469 return dd->tabChangesFocus;
10473
10474
10475
10476
10477
10478
10479
10480
10481void QGraphicsTextItem::setOpenExternalLinks(
bool open)
10483 dd->textControl()->setOpenExternalLinks(open);
10486bool QGraphicsTextItem::openExternalLinks()
const
10490 return dd->control->openExternalLinks();
10494
10495
10496
10497
10498
10499
10500
10501
10502
10503void QGraphicsTextItem::setTextCursor(
const QTextCursor &cursor)
10505 dd->textControl()->setTextCursor(cursor);
10508QTextCursor QGraphicsTextItem::textCursor()
const
10511 return QTextCursor();
10512 return dd->control->textCursor();
10517 Q_DECLARE_PUBLIC(QGraphicsSimpleTextItem)
10532 layout->setCacheEnabled(
true);
10533 layout->beginLayout();
10534 while (layout->createLine().isValid())
10536 layout->endLayout();
10537 qreal maxWidth = 0;
10539 for (
int i = 0; i < layout->lineCount(); ++i) {
10540 QTextLine line = layout->lineAt(i);
10541 maxWidth = qMax(maxWidth, line.naturalTextWidth());
10542 line.setPosition(
QPointF(0, y));
10543 y += line.height();
10545 return QRectF(0, 0, maxWidth, y);
10550 Q_Q(QGraphicsSimpleTextItem);
10552 if (text.isEmpty()) {
10555 QString tmp = text;
10556 tmp.replace(u'\n', QChar::LineSeparator);
10557 Q_DECL_UNINITIALIZED QStackTextEngine engine(tmp, font);
10558 QTextLayout layout(&engine);
10559 br = setupTextLayout(&layout);
10561 if (br != boundingRect) {
10562 q->prepareGeometryChange();
10569
10570
10571
10572
10573
10574
10575
10576
10577
10578
10579
10580
10581
10582
10583
10584
10585
10586
10587
10588
10589
10590
10591
10592
10593
10594
10595
10596
10597
10598
10599
10600
10603
10604
10605
10606
10607
10608
10609QGraphicsSimpleTextItem::QGraphicsSimpleTextItem(QGraphicsItem *parent)
10610 : QAbstractGraphicsShapeItem(*
new QGraphicsSimpleTextItemPrivate, parent)
10615
10616
10617
10618
10619
10620
10621QGraphicsSimpleTextItem::QGraphicsSimpleTextItem(
const QString &text, QGraphicsItem *parent)
10622 : QAbstractGraphicsShapeItem(*
new QGraphicsSimpleTextItemPrivate, parent)
10628
10629
10630QGraphicsSimpleTextItem::~QGraphicsSimpleTextItem()
10635
10636
10637
10638
10639
10640void QGraphicsSimpleTextItem::setText(
const QString &text)
10642 Q_D(QGraphicsSimpleTextItem);
10643 if (d->text == text)
10646 d->updateBoundingRect();
10651
10652
10653QString QGraphicsSimpleTextItem::text()
const
10655 Q_D(
const QGraphicsSimpleTextItem);
10660
10661
10662void QGraphicsSimpleTextItem::setFont(
const QFont &font)
10664 Q_D(QGraphicsSimpleTextItem);
10666 d->updateBoundingRect();
10670
10671
10672QFont QGraphicsSimpleTextItem::font()
const
10674 Q_D(
const QGraphicsSimpleTextItem);
10679
10680
10681QRectF QGraphicsSimpleTextItem::boundingRect()
const
10683 Q_D(
const QGraphicsSimpleTextItem);
10684 return d->boundingRect;
10688
10689
10690QPainterPath QGraphicsSimpleTextItem::shape()
const
10692 Q_D(
const QGraphicsSimpleTextItem);
10694 path.addRect(d->boundingRect);
10699
10700
10701bool QGraphicsSimpleTextItem::contains(
const QPointF &point)
const
10703 Q_D(
const QGraphicsSimpleTextItem);
10704 return d->boundingRect.contains(point);
10708
10709
10710void QGraphicsSimpleTextItem::paint(QPainter *painter,
const QStyleOptionGraphicsItem *option, QWidget *widget)
10713 Q_D(QGraphicsSimpleTextItem);
10715 painter->setFont(d->font);
10717 QString tmp = d->text;
10718 tmp.replace(u'\n', QChar::LineSeparator);
10719 Q_DECL_UNINITIALIZED QStackTextEngine engine(tmp, d->font);
10720 QTextLayout layout(&engine);
10723 p.setBrush(d->brush);
10724 painter->setPen(p);
10725 if (d->pen.style() == Qt::NoPen && d->brush.style() == Qt::SolidPattern) {
10726 painter->setBrush(Qt::NoBrush);
10728 QTextLayout::FormatRange range;
10730 range.length = layout.text().size();
10731 range.format.setTextOutline(d->pen);
10732 layout.setFormats(QList<QTextLayout::FormatRange>(1, range));
10735 setupTextLayout(&layout);
10736 layout.draw(painter, QPointF(0, 0));
10738 if (option->state & (QStyle::State_Selected | QStyle::State_HasFocus))
10739 qt_graphicsItem_highlightSelected(
this, painter, option);
10743
10744
10745bool QGraphicsSimpleTextItem::isObscuredBy(
const QGraphicsItem *item)
const
10747 return QAbstractGraphicsShapeItem::isObscuredBy(item);
10751
10752
10753QPainterPath QGraphicsSimpleTextItem::opaqueArea()
const
10755 return QAbstractGraphicsShapeItem::opaqueArea();
10759
10760
10761int QGraphicsSimpleTextItem::type()
const
10767
10768
10769bool QGraphicsSimpleTextItem::supportsExtension(Extension extension)
const
10771 Q_UNUSED(extension);
10776
10777
10778void QGraphicsSimpleTextItem::setExtension(Extension extension,
const QVariant &variant)
10780 Q_UNUSED(extension);
10785
10786
10787QVariant QGraphicsSimpleTextItem::extension(
const QVariant &variant)
const
10794
10795
10796
10797
10798
10799
10800
10801
10802
10803
10804
10805
10806
10807
10808
10809
10810
10811
10812
10813
10814
10815
10816
10817
10818
10819
10820
10821
10822
10823
10824
10825
10826
10827
10828
10829
10830
10831
10832
10833
10834
10835
10836
10837
10838
10839
10840
10841
10842
10843
10844
10845
10846
10847
10848
10849
10850
10859
10860
10861
10862
10863
10864QGraphicsItemGroup::QGraphicsItemGroup(QGraphicsItem *parent)
10865 : QGraphicsItem(*
new QGraphicsItemGroupPrivate, parent)
10867 setHandlesChildEvents(
true);
10871
10872
10873QGraphicsItemGroup::~QGraphicsItemGroup()
10878
10879
10880
10881
10882
10883
10884void QGraphicsItemGroup::addToGroup(QGraphicsItem *item)
10886 Q_D(QGraphicsItemGroup);
10888 qWarning(
"QGraphicsItemGroup::addToGroup: cannot add null item");
10891 if (item ==
this) {
10892 qWarning(
"QGraphicsItemGroup::addToGroup: cannot add a group to itself");
10898 QTransform itemTransform = item->itemTransform(
this, &ok);
10901 qWarning(
"QGraphicsItemGroup::addToGroup: could not find a valid transformation from item to group coordinates");
10905 QTransform newItemTransform(itemTransform);
10906 item->setPos(mapFromItem(item, 0, 0));
10907 item->setParentItem(
this);
10910 if (!item->pos().isNull())
10911 newItemTransform *= QTransform::fromTranslate(-item->x(), -item->y());
10914 QPointF origin = item->transformOriginPoint();
10916 QList<QGraphicsTransform*> transformList = item->transformations();
10917 for (
int i = 0; i < transformList.size(); ++i)
10918 transformList.at(i)->applyTo(&m);
10919 newItemTransform *= m.toTransform().inverted();
10920 newItemTransform.translate(origin.x(), origin.y());
10921 newItemTransform.rotate(-item->rotation());
10922 newItemTransform.scale(1/item->scale(), 1/item->scale());
10923 newItemTransform.translate(-origin.x(), -origin.y());
10927 item->setTransform(newItemTransform);
10928 item->d_func()->setIsMemberOfGroup(
true);
10929 prepareGeometryChange();
10930 d->itemsBoundingRect |= itemTransform.mapRect(item->boundingRect() | item->childrenBoundingRect());
10935
10936
10937
10938
10939
10940
10941
10942void QGraphicsItemGroup::removeFromGroup(QGraphicsItem *item)
10944 Q_D(QGraphicsItemGroup);
10946 qWarning(
"QGraphicsItemGroup::removeFromGroup: cannot remove null item");
10950 QGraphicsItem *newParent = d_ptr->parent;
10954 QTransform itemTransform;
10956 itemTransform = item->itemTransform(newParent, &ok);
10958 itemTransform = item->sceneTransform();
10960 QPointF oldPos = item->mapToItem(newParent, 0, 0);
10961 item->setParentItem(newParent);
10962 item->setPos(oldPos);
10965 if (!item->pos().isNull())
10966 itemTransform *= QTransform::fromTranslate(-item->x(), -item->y());
10970 QPointF origin = item->transformOriginPoint();
10972 QList<QGraphicsTransform*> transformList = item->transformations();
10973 for (
int i = 0; i < transformList.size(); ++i)
10974 transformList.at(i)->applyTo(&m);
10975 itemTransform *= m.toTransform().inverted();
10976 itemTransform.translate(origin.x(), origin.y());
10977 itemTransform.rotate(-item->rotation());
10978 itemTransform.scale(1 / item->scale(), 1 / item->scale());
10979 itemTransform.translate(-origin.x(), -origin.y());
10983 item->setTransform(itemTransform);
10984 item->d_func()->setIsMemberOfGroup(item->group() !=
nullptr);
10987 prepareGeometryChange();
10988 d->itemsBoundingRect = childrenBoundingRect();
10992
10993
10994
10995
10996QRectF QGraphicsItemGroup::boundingRect()
const
10998 Q_D(
const QGraphicsItemGroup);
10999 return d->itemsBoundingRect;
11003
11004
11005void QGraphicsItemGroup::paint(QPainter *painter,
const QStyleOptionGraphicsItem *option,
11009 if (option->state & QStyle::State_Selected) {
11010 Q_D(QGraphicsItemGroup);
11011 painter->setBrush(Qt::NoBrush);
11012 painter->drawRect(d->itemsBoundingRect);
11017
11018
11019bool QGraphicsItemGroup::isObscuredBy(
const QGraphicsItem *item)
const
11021 return QGraphicsItem::isObscuredBy(item);
11025
11026
11027QPainterPath QGraphicsItemGroup::opaqueArea()
const
11029 return QGraphicsItem::opaqueArea();
11033
11034
11035int QGraphicsItemGroup::type()
const
11040#if QT_CONFIG(graphicseffect)
11041QRectF QGraphicsItemEffectSourcePrivate::boundingRect(Qt::CoordinateSystem system)
const
11043 const bool deviceCoordinates = (system == Qt::DeviceCoordinates);
11044 if (!info && deviceCoordinates) {
11046 qWarning(
"QGraphicsEffectSource::boundingRect: Not yet implemented, lacking device context");
11050 QRectF rect = item->boundingRect();
11051 if (!item->d_ptr->children.isEmpty())
11052 rect |= item->childrenBoundingRect();
11054 if (deviceCoordinates) {
11055 Q_ASSERT(info->painter);
11056 rect = info->painter->worldTransform().mapRect(rect);
11062void QGraphicsItemEffectSourcePrivate::draw(QPainter *painter)
11065 qWarning(
"QGraphicsEffectSource::draw: Can only begin as a result of QGraphicsEffect::draw");
11069 Q_ASSERT(item->d_ptr->scene);
11070 QGraphicsScenePrivate *scened = item->d_ptr->scene->d_func();
11071 if (painter == info->painter) {
11072 scened->draw(item, painter, info->viewTransform, info->transformPtr, info->exposedRegion,
11073 info->widget, info->opacity, info->effectTransform, info->wasDirtySceneTransform,
11076 QTransform effectTransform = info->painter->worldTransform().inverted();
11077 effectTransform *= painter->worldTransform();
11078 scened->draw(item, painter, info->viewTransform, info->transformPtr, info->exposedRegion,
11079 info->widget, info->opacity, &effectTransform, info->wasDirtySceneTransform,
11085QRectF QGraphicsItemEffectSourcePrivate::paddedEffectRect(Qt::CoordinateSystem system, QGraphicsEffect::PixmapPadMode mode,
const QRectF &sourceRect,
bool *unpadded)
const
11087 QRectF effectRectF;
11092 if (mode == QGraphicsEffect::PadToEffectiveBoundingRect) {
11094 QRectF deviceRect = system == Qt::DeviceCoordinates ? sourceRect : info->painter->worldTransform().mapRect(sourceRect);
11095 effectRectF = item->graphicsEffect()->boundingRectFor(deviceRect);
11097 *unpadded = (effectRectF.size() == sourceRect.size());
11098 if (info && system == Qt::LogicalCoordinates)
11099 effectRectF = info->painter->worldTransform().inverted().mapRect(effectRectF);
11102 effectRectF = item->graphicsEffect()->boundingRectFor(sourceRect);
11104 }
else if (mode == QGraphicsEffect::PadToTransparentBorder) {
11106 effectRectF = sourceRect.adjusted(-1.5, -1.5, 1.5, 1.5);
11108 effectRectF = sourceRect;
11113 return effectRectF;
11116QPixmap QGraphicsItemEffectSourcePrivate::pixmap(Qt::CoordinateSystem system, QPoint *offset,
11117 QGraphicsEffect::PixmapPadMode mode)
const
11119 const bool deviceCoordinates = (system == Qt::DeviceCoordinates);
11120 if (!info && deviceCoordinates) {
11122 qWarning(
"QGraphicsEffectSource::pixmap: Not yet implemented, lacking device context");
11125 if (!item->d_ptr->scene)
11127 QGraphicsScenePrivate *scened = item->d_ptr->scene->d_func();
11130 const QRectF sourceRect = boundingRect(system);
11131 QRectF effectRectF = paddedEffectRect(system, mode, sourceRect, &unpadded);
11132 QRect effectRect = effectRectF.toAlignedRect();
11135 *offset = effectRect.topLeft();
11137 bool untransformed = !deviceCoordinates
11138 || info->painter->worldTransform().type() <= QTransform::TxTranslate;
11139 if (untransformed && unpadded && isPixmap()) {
11141 *offset = boundingRect(system).topLeft().toPoint();
11142 return static_cast<QGraphicsPixmapItem *>(item)->pixmap();
11145 if (effectRect.isEmpty())
11148 const auto dpr = info ? info->painter->device()->devicePixelRatio() : 1.0;
11149 QPixmap pixmap(QRectF(effectRectF.topLeft(), effectRectF.size() * dpr).toAlignedRect().size());
11150 pixmap.setDevicePixelRatio(dpr);
11151 pixmap.fill(Qt::transparent);
11152 QPainter pixmapPainter(&pixmap);
11153 pixmapPainter.setRenderHints(info ? info->painter->renderHints() : QPainter::TextAntialiasing);
11155 QTransform effectTransform = QTransform::fromTranslate(-effectRect.x(), -effectRect.y());
11156 if (deviceCoordinates && info->effectTransform)
11157 effectTransform *= *info->effectTransform;
11161 QTransform sceneTransform = item->sceneTransform();
11162 QTransform newEffectTransform = sceneTransform.inverted();
11163 newEffectTransform *= effectTransform;
11164 scened->draw(item, &pixmapPainter,
nullptr, &sceneTransform,
nullptr,
nullptr, qreal(1.0),
11165 &newEffectTransform,
false,
true);
11166 }
else if (deviceCoordinates) {
11168 scened->draw(item, &pixmapPainter, info->viewTransform, info->transformPtr,
nullptr,
11169 info->widget, info->opacity, &effectTransform, info->wasDirtySceneTransform,
11173 QTransform newEffectTransform = info->transformPtr->inverted();
11174 newEffectTransform *= effectTransform;
11175 scened->draw(item, &pixmapPainter, info->viewTransform, info->transformPtr,
nullptr,
11176 info->widget, info->opacity, &newEffectTransform, info->wasDirtySceneTransform,
11180 pixmapPainter.end();
11186#ifndef QT_NO_DEBUG_STREAM
11189 if (
const QGraphicsItem *parent = item->parentItem())
11190 debug <<
", parent=" <<
static_cast<
const void *>(parent);
11192 QtDebugUtils::formatQPoint(debug, item->pos());
11193 if (
const qreal z = item->zValue())
11194 debug <<
", z=" << z;
11196 debug <<
", flags=" << item->flags();
11201 QDebugStateSaver saver(debug);
11205 return debug <<
"QGraphicsItem(0x0)";
11207 if (
const QGraphicsObject *o = item->toGraphicsObject())
11208 debug << o->metaObject()->className();
11210 debug <<
"QGraphicsItem";
11211 debug <<
'(' <<
static_cast<
const void *>(item);
11212 if (
const QGraphicsProxyWidget *pw = qgraphicsitem_cast<
const QGraphicsProxyWidget *>(item)) {
11213 debug <<
", widget=";
11214 if (
const QWidget *w = pw->widget()) {
11215 debug << w->metaObject()->className() <<
'(' <<
static_cast<
const void *>(w);
11216 if (!w->objectName().isEmpty())
11217 debug <<
", name=" << w->objectName();
11220 debug <<
"QWidget(0x0)";
11223 formatGraphicsItemHelper(debug, item);
11230 QDebugStateSaver saver(debug);
11234 return debug <<
"QGraphicsObject(0x0)";
11236 debug << item->metaObject()->className() <<
'(' <<
static_cast<
const void *>(item);
11237 if (!item->objectName().isEmpty())
11238 debug <<
", name=" << item->objectName();
11239 formatGraphicsItemHelper(debug, item);
11246 const char *str =
"UnknownChange";
11248 case QGraphicsItem::ItemChildAddedChange:
11249 str =
"ItemChildAddedChange";
11251 case QGraphicsItem::ItemChildRemovedChange:
11252 str =
"ItemChildRemovedChange";
11254 case QGraphicsItem::ItemCursorChange:
11255 str =
"ItemCursorChange";
11257 case QGraphicsItem::ItemCursorHasChanged:
11258 str =
"ItemCursorHasChanged";
11260 case QGraphicsItem::ItemEnabledChange:
11261 str =
"ItemEnabledChange";
11263 case QGraphicsItem::ItemEnabledHasChanged:
11264 str =
"ItemEnabledHasChanged";
11266 case QGraphicsItem::ItemFlagsChange:
11267 str =
"ItemFlagsChange";
11269 case QGraphicsItem::ItemFlagsHaveChanged:
11270 str =
"ItemFlagsHaveChanged";
11272 case QGraphicsItem::ItemParentChange:
11273 str =
"ItemParentChange";
11275 case QGraphicsItem::ItemParentHasChanged:
11276 str =
"ItemParentHasChanged";
11278 case QGraphicsItem::ItemPositionChange:
11279 str =
"ItemPositionChange";
11281 case QGraphicsItem::ItemPositionHasChanged:
11282 str =
"ItemPositionHasChanged";
11284 case QGraphicsItem::ItemSceneChange:
11285 str =
"ItemSceneChange";
11287 case QGraphicsItem::ItemSceneHasChanged:
11288 str =
"ItemSceneHasChanged";
11290 case QGraphicsItem::ItemSelectedChange:
11291 str =
"ItemSelectedChange";
11293 case QGraphicsItem::ItemSelectedHasChanged:
11294 str =
"ItemSelectedHasChanged";
11296 case QGraphicsItem::ItemToolTipChange:
11297 str =
"ItemToolTipChange";
11299 case QGraphicsItem::ItemToolTipHasChanged:
11300 str =
"ItemToolTipHasChanged";
11302 case QGraphicsItem::ItemTransformChange:
11303 str =
"ItemTransformChange";
11305 case QGraphicsItem::ItemTransformHasChanged:
11306 str =
"ItemTransformHasChanged";
11308 case QGraphicsItem::ItemVisibleChange:
11309 str =
"ItemVisibleChange";
11311 case QGraphicsItem::ItemVisibleHasChanged:
11312 str =
"ItemVisibleHasChanged";
11314 case QGraphicsItem::ItemZValueChange:
11315 str =
"ItemZValueChange";
11317 case QGraphicsItem::ItemZValueHasChanged:
11318 str =
"ItemZValueHasChanged";
11320 case QGraphicsItem::ItemOpacityChange:
11321 str =
"ItemOpacityChange";
11323 case QGraphicsItem::ItemOpacityHasChanged:
11324 str =
"ItemOpacityHasChanged";
11326 case QGraphicsItem::ItemScenePositionHasChanged:
11327 str =
"ItemScenePositionHasChanged";
11329 case QGraphicsItem::ItemRotationChange:
11330 str =
"ItemRotationChange";
11332 case QGraphicsItem::ItemRotationHasChanged:
11333 str =
"ItemRotationHasChanged";
11335 case QGraphicsItem::ItemScaleChange:
11336 str =
"ItemScaleChange";
11338 case QGraphicsItem::ItemScaleHasChanged:
11339 str =
"ItemScaleHasChanged";
11341 case QGraphicsItem::ItemTransformOriginPointChange:
11342 str =
"ItemTransformOriginPointChange";
11344 case QGraphicsItem::ItemTransformOriginPointHasChanged:
11345 str =
"ItemTransformOriginPointHasChanged";
11354 const char *str =
"UnknownFlag";
11356 case QGraphicsItem::ItemIsMovable:
11357 str =
"ItemIsMovable";
11359 case QGraphicsItem::ItemIsSelectable:
11360 str =
"ItemIsSelectable";
11362 case QGraphicsItem::ItemIsFocusable:
11363 str =
"ItemIsFocusable";
11365 case QGraphicsItem::ItemClipsToShape:
11366 str =
"ItemClipsToShape";
11368 case QGraphicsItem::ItemClipsChildrenToShape:
11369 str =
"ItemClipsChildrenToShape";
11371 case QGraphicsItem::ItemIgnoresTransformations:
11372 str =
"ItemIgnoresTransformations";
11374 case QGraphicsItem::ItemIgnoresParentOpacity:
11375 str =
"ItemIgnoresParentOpacity";
11377 case QGraphicsItem::ItemDoesntPropagateOpacityToChildren:
11378 str =
"ItemDoesntPropagateOpacityToChildren";
11380 case QGraphicsItem::ItemStacksBehindParent:
11381 str =
"ItemStacksBehindParent";
11383 case QGraphicsItem::ItemUsesExtendedStyleOption:
11384 str =
"ItemUsesExtendedStyleOption";
11386 case QGraphicsItem::ItemHasNoContents:
11387 str =
"ItemHasNoContents";
11389 case QGraphicsItem::ItemSendsGeometryChanges:
11390 str =
"ItemSendsGeometryChanges";
11392 case QGraphicsItem::ItemAcceptsInputMethod:
11393 str =
"ItemAcceptsInputMethod";
11395 case QGraphicsItem::ItemNegativeZStacksBehindParent:
11396 str =
"ItemNegativeZStacksBehindParent";
11398 case QGraphicsItem::ItemIsPanel:
11399 str =
"ItemIsPanel";
11401 case QGraphicsItem::ItemIsFocusScope:
11402 str =
"ItemIsFocusScope";
11404 case QGraphicsItem::ItemSendsScenePositionChanges:
11405 str =
"ItemSendsScenePositionChanges";
11407 case QGraphicsItem::ItemStopsClickFocusPropagation:
11408 str =
"ItemStopsClickFocusPropagation";
11410 case QGraphicsItem::ItemStopsFocusHandling:
11411 str =
"ItemStopsFocusHandling";
11413 case QGraphicsItem::ItemContainsChildrenInShape:
11414 str =
"ItemContainsChildrenInShape";
11425 for (
int i = 0; i < 17; ++i) {
11426 if (flags & (1 << i)) {
11430 debug << QGraphicsItem::GraphicsItemFlag(
int(flags & (1 << i)));
11441#include "moc_qgraphicsitem.cpp"
QHash< const QGraphicsItem *, QMap< int, QVariant > > data
QGraphicsPixmapItem::ShapeMode shapeMode
Qt::TransformationMode transformationMode
void updateBoundingRect()
void _q_updateBoundingRect(const QSizeF &)
void _q_ensureVisible(QRectF)
QPointF controlOffset() const
QGraphicsTextItemPrivate()
bool _q_mouseOnEdge(QGraphicsSceneMouseEvent *)
QWidgetTextControl * control
QWidgetTextControl * textControl() const
void sendControlEvent(QEvent *e)
\inmodule QtCore\reentrant
Combined button and popup list for selecting options.
QDebug operator<<(QDebug debug, QDir::Filters filters)
static void formatGraphicsItemHelper(QDebug debug, const QGraphicsItem *item)
bool _qt_movableAncestorIsSelected(const QGraphicsItem *item)
static QRectF setupTextLayout(QTextLayout *layout)
QDebug operator<<(QDebug debug, const QGraphicsObject *item)
static QPainterPath qt_graphicsItem_shapeFromPath(const QPainterPath &path, const QPen &pen)
QDebug operator<<(QDebug debug, const QGraphicsItem *item)
static bool qt_QGraphicsItem_isObscured(const QGraphicsItem *item, const QGraphicsItem *other, const QRectF &rect)