7#if QT_CONFIG(scrollarea)
11#include "qguiapplication.h"
13#include "qstyleoption.h"
16#include "qboxlayout.h"
19#if QT_CONFIG(itemviews)
20#include "qheaderview.h"
23#include "qabstractscrollarea_p.h"
24#include "qscrollbar_p.h"
26#include <private/qguiapplication_p.h>
27#include <qpa/qplatformtheme.h>
29#include <private/qapplication_p.h>
33using namespace Qt::StringLiterals;
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
127QAbstractScrollAreaPrivate::QAbstractScrollAreaPrivate()
128 :hbar(
nullptr), vbar(
nullptr), vbarpolicy(Qt::ScrollBarAsNeeded), hbarpolicy(Qt::ScrollBarAsNeeded),
129 shownOnce(
false), inResize(
false), sizeAdjustPolicy(QAbstractScrollArea::AdjustIgnored),
130 viewport(
nullptr), cornerWidget(
nullptr), left(0), top(0), right(0), bottom(0),
131 xoffset(0), yoffset(0), viewportFilter(
nullptr)
135QAbstractScrollAreaPrivate::~QAbstractScrollAreaPrivate()
139QAbstractScrollAreaScrollBarContainer::QAbstractScrollAreaScrollBarContainer(Qt::Orientation orientation, QWidget *parent)
140 :QWidget(parent), scrollBar(
new QScrollBar(orientation,
this)),
141 layout(
new QBoxLayout(orientation == Qt::Horizontal ? QBoxLayout::LeftToRight : QBoxLayout::TopToBottom)),
142 orientation(orientation)
145 layout->setContentsMargins(QMargins());
146 layout->setSpacing(0);
147 layout->addWidget(scrollBar);
148 layout->setSizeConstraint(QLayout::SetMaximumSize);
152
153
154void QAbstractScrollAreaScrollBarContainer::addWidget(QWidget *widget, LogicalPosition position)
156 QSizePolicy policy = widget->sizePolicy();
157 if (orientation == Qt::Vertical)
158 policy.setHorizontalPolicy(QSizePolicy::Ignored);
160 policy.setVerticalPolicy(QSizePolicy::Ignored);
161 widget->setSizePolicy(policy);
162 widget->setParent(
this);
164 const int insertIndex = (position & LogicalLeft) ? 0 : scrollBarLayoutIndex() + 1;
165 layout->insertWidget(insertIndex, widget);
169
170
171
172QWidgetList QAbstractScrollAreaScrollBarContainer::widgets(LogicalPosition position)
175 const int scrollBarIndex = scrollBarLayoutIndex();
176 if (position == LogicalLeft) {
177 list.reserve(scrollBarIndex);
178 for (
int i = 0; i < scrollBarIndex; ++i)
179 list.append(layout->itemAt(i)->widget());
180 }
else if (position == LogicalRight) {
181 const int layoutItemCount = layout->count();
182 list.reserve(layoutItemCount - (scrollBarIndex + 1));
183 for (
int i = scrollBarIndex + 1; i < layoutItemCount; ++i)
184 list.append(layout->itemAt(i)->widget());
190
191
192
193
194
195int QAbstractScrollAreaScrollBarContainer::scrollBarLayoutIndex()
const
197 const int layoutItemCount = layout->count();
198 for (
int i = 0; i < layoutItemCount; ++i) {
199 if (qobject_cast<QScrollBar *>(layout->itemAt(i)->widget()))
206
207void QAbstractScrollAreaPrivate::replaceScrollBar(QScrollBar *scrollBar,
208 Qt::Orientation orientation)
210 Q_Q(QAbstractScrollArea);
212 QAbstractScrollAreaScrollBarContainer *container = scrollBarContainers[orientation];
213 bool horizontal = (orientation == Qt::Horizontal);
214 QScrollBar *oldBar = horizontal ? hbar : vbar;
219 scrollBar->setParent(container);
220 container->scrollBar = scrollBar;
221 container->layout->removeWidget(oldBar);
222 container->layout->insertWidget(0, scrollBar);
223 scrollBar->setVisible(oldBar->isVisibleTo(container));
224 scrollBar->setInvertedAppearance(oldBar->invertedAppearance());
225 scrollBar->setInvertedControls(oldBar->invertedControls());
226 scrollBar->setRange(oldBar->minimum(), oldBar->maximum());
227 scrollBar->setOrientation(oldBar->orientation());
228 scrollBar->setPageStep(oldBar->pageStep());
229 scrollBar->setSingleStep(oldBar->singleStep());
230 scrollBar->d_func()->viewMayChangeSingleStep = oldBar->d_func()->viewMayChangeSingleStep;
231 scrollBar->setSliderDown(oldBar->isSliderDown());
232 scrollBar->setSliderPosition(oldBar->sliderPosition());
233 scrollBar->setTracking(oldBar->hasTracking());
234 scrollBar->setValue(oldBar->value());
235 scrollBar->installEventFilter(q);
236 oldBar->removeEventFilter(q);
239 QObject::connect(scrollBar, SIGNAL(valueChanged(
int)),
240 q, horizontal ? SLOT(_q_hslide(
int)) : SLOT(_q_vslide(
int)));
241 QObject::connect(scrollBar, SIGNAL(rangeChanged(
int,
int)),
242 q, SLOT(_q_showOrHideScrollBars()), Qt::QueuedConnection);
245void QAbstractScrollAreaPrivate::init()
247 Q_Q(QAbstractScrollArea);
248 viewport =
new QWidget(q);
249 viewport->setObjectName(
"qt_scrollarea_viewport"_L1);
250 viewport->setBackgroundRole(QPalette::Base);
251 viewport->setAutoFillBackground(
true);
252 scrollBarContainers[Qt::Horizontal] =
new QAbstractScrollAreaScrollBarContainer(Qt::Horizontal, q);
253 scrollBarContainers[Qt::Horizontal]->setObjectName(
"qt_scrollarea_hcontainer"_L1);
254 hbar = scrollBarContainers[Qt::Horizontal]->scrollBar;
256 scrollBarContainers[Qt::Horizontal]->setVisible(
false);
257 hbar->installEventFilter(q);
258 QObject::connect(hbar, SIGNAL(valueChanged(
int)), q, SLOT(_q_hslide(
int)));
259 QObject::connect(hbar, SIGNAL(rangeChanged(
int,
int)), q, SLOT(_q_showOrHideScrollBars()), Qt::QueuedConnection);
260 scrollBarContainers[Qt::Vertical] =
new QAbstractScrollAreaScrollBarContainer(Qt::Vertical, q);
261 scrollBarContainers[Qt::Vertical]->setObjectName(
"qt_scrollarea_vcontainer"_L1);
262 vbar = scrollBarContainers[Qt::Vertical]->scrollBar;
264 scrollBarContainers[Qt::Vertical]->setVisible(
false);
265 vbar->installEventFilter(q);
266 QObject::connect(vbar, SIGNAL(valueChanged(
int)), q, SLOT(_q_vslide(
int)));
267 QObject::connect(vbar, SIGNAL(rangeChanged(
int,
int)), q, SLOT(_q_showOrHideScrollBars()), Qt::QueuedConnection);
268 viewportFilter.reset(
new QAbstractScrollAreaFilter(
this));
269 viewport->installEventFilter(viewportFilter.data());
270 viewport->setFocusProxy(q);
271 q->setFocusPolicy(Qt::StrongFocus);
272 q->setFrameStyle(QFrame::StyledPanel | QFrame::Sunken);
273 q->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
276# ifndef QT_NO_GESTURES
277 viewport->grabGesture(Qt::PanGesture);
282void QAbstractScrollAreaPrivate::layoutChildren()
286 layoutChildren_helper(&needH, &needV);
290 layoutChildren_helper(&needH, &needV);
293void QAbstractScrollAreaPrivate::layoutChildren_helper(
bool *needHorizontalScrollbar,
bool *needVerticalScrollbar)
295 Q_Q(QAbstractScrollArea);
296 QStyleOptionSlider barOpt;
298 hbar->initStyleOption(&barOpt);
299 bool htransient = hbar->style()->styleHint(QStyle::SH_ScrollBar_Transient, &barOpt, hbar);
300 bool needh = *needHorizontalScrollbar || ((hbarpolicy != Qt::ScrollBarAlwaysOff) && ((hbarpolicy == Qt::ScrollBarAlwaysOn && !htransient)
301 || ((hbarpolicy == Qt::ScrollBarAsNeeded || htransient)
302 && hbar->minimum() < hbar->maximum() && !hbar->sizeHint().isEmpty())));
303 const int hscrollOverlap = hbar->style()->pixelMetric(QStyle::PM_ScrollView_ScrollBarOverlap, &barOpt, hbar);
305 vbar->initStyleOption(&barOpt);
306 bool vtransient = vbar->style()->styleHint(QStyle::SH_ScrollBar_Transient, &barOpt, vbar);
307 bool needv = *needVerticalScrollbar || ((vbarpolicy != Qt::ScrollBarAlwaysOff) && ((vbarpolicy == Qt::ScrollBarAlwaysOn && !vtransient)
308 || ((vbarpolicy == Qt::ScrollBarAsNeeded || vtransient)
309 && vbar->minimum() < vbar->maximum() && !vbar->sizeHint().isEmpty())));
310 const int vscrollOverlap = vbar->style()->pixelMetric(QStyle::PM_ScrollView_ScrollBarOverlap, &barOpt, vbar);
315 const int hsbExt = hbar->sizeHint().height();
316 const int vsbExt = vbar->sizeHint().width();
317 const QPoint extPoint(vsbExt, hsbExt);
318 const QSize extSize(vsbExt, hsbExt);
320 const QRect widgetRect = q->rect();
322 const bool hasCornerWidget = (cornerWidget !=
nullptr);
324 QPoint cornerOffset((needv && vscrollOverlap == 0) ? vsbExt : 0, (needh && hscrollOverlap == 0) ? hsbExt : 0);
330 if ((frameStyle != QFrame::NoFrame) &&
331 q->style()->styleHint(QStyle::SH_ScrollView_FrameOnlyAroundContents, &opt, q)) {
332 controlsRect = widgetRect;
333 const int spacing = q->style()->pixelMetric(QStyle::PM_ScrollView_ScrollBarSpacing, &opt, q);
334 const QPoint cornerExtra(needv ? spacing + vscrollOverlap : 0, needh ? spacing + hscrollOverlap : 0);
335 QRect frameRect = widgetRect;
336 frameRect.adjust(0, 0, -cornerOffset.x() - cornerExtra.x(), -cornerOffset.y() - cornerExtra.y());
337 q->setFrameRect(QStyle::visualRect(opt.direction, opt.rect, frameRect));
341 viewportRect = QStyle::visualRect(opt.direction, opt.rect, q->contentsRect());
343 q->setFrameRect(QStyle::visualRect(opt.direction, opt.rect, widgetRect));
344 controlsRect = q->contentsRect();
345 viewportRect = QRect(controlsRect.topLeft(), controlsRect.bottomRight() - cornerOffset);
348 cornerOffset = QPoint(needv ? vsbExt : 0, needh ? hsbExt : 0);
352 if (hasCornerWidget && ((needv && vscrollOverlap == 0) || (needh && hscrollOverlap == 0)))
353 cornerOffset = extPoint;
357 const QPoint cornerPoint(controlsRect.bottomRight() + QPoint(1, 1) - cornerOffset);
361 if (needv && needh && !hasCornerWidget && hscrollOverlap == 0 && vscrollOverlap == 0)
362 cornerPaintingRect = QStyle::visualRect(opt.direction, opt.rect, QRect(cornerPoint, extSize));
364 cornerPaintingRect = QRect();
367 int vHeaderRight = 0;
368 int hHeaderBottom = 0;
369#if QT_CONFIG(itemviews)
370 if ((vscrollOverlap > 0 && needv) || (hscrollOverlap > 0 && needh)) {
371 const QList<QHeaderView *> headers = q->findChildren<QHeaderView*>();
372 if (headers.size() <= 2) {
373 for (
const QHeaderView *header : headers) {
374 const QRect geo = header->geometry();
375 if (header->orientation() == Qt::Vertical && header->isVisible() && QStyle::visualRect(opt.direction, opt.rect, geo).left() <= opt.rect.width() / 2)
376 vHeaderRight = QStyle::visualRect(opt.direction, opt.rect, geo).right();
377 else if (header->orientation() == Qt::Horizontal && header->isVisible() && geo.top() <= q->frameWidth())
378 hHeaderBottom = geo.bottom();
384 QRect horizontalScrollBarRect(QPoint(controlsRect.left() + vHeaderRight, cornerPoint.y()), QPoint(cornerPoint.x() - 1, controlsRect.bottom()));
386 if (!hasCornerWidget && htransient)
387 horizontalScrollBarRect.adjust(0, 0, cornerOffset.x(), 0);
388 scrollBarContainers[Qt::Horizontal]->setGeometry(QStyle::visualRect(opt.direction, opt.rect, horizontalScrollBarRect));
389 scrollBarContainers[Qt::Horizontal]->raise();
393 QRect verticalScrollBarRect (QPoint(cornerPoint.x(), controlsRect.top() + hHeaderBottom), QPoint(controlsRect.right(), cornerPoint.y() - 1));
394 if (!hasCornerWidget && vtransient)
395 verticalScrollBarRect.adjust(0, 0, 0, cornerOffset.y());
396 scrollBarContainers[Qt::Vertical]->setGeometry(QStyle::visualRect(opt.direction, opt.rect, verticalScrollBarRect));
397 scrollBarContainers[Qt::Vertical]->raise();
401 const QRect cornerWidgetRect(cornerPoint, controlsRect.bottomRight());
402 cornerWidget->setGeometry(QStyle::visualRect(opt.direction, opt.rect, cornerWidgetRect));
405 scrollBarContainers[Qt::Horizontal]->setVisible(needh);
406 scrollBarContainers[Qt::Vertical]->setVisible(needv);
408 if (q->isRightToLeft())
409 viewportRect.adjust(right, top, -left, -bottom);
411 viewportRect.adjust(left, top, -right, -bottom);
412 viewportRect = QStyle::visualRect(opt.direction, opt.rect, viewportRect);
413 viewportRect.translate(-overshoot);
414 viewport->setGeometry(viewportRect);
416 *needHorizontalScrollbar = needh;
417 *needVerticalScrollbar = needv;
421
422
423
424
425
426
427
428
429
430
434
435
436
437
438QAbstractScrollArea::QAbstractScrollArea(QAbstractScrollAreaPrivate &dd, QWidget *parent)
441 Q_D(QAbstractScrollArea);
445 d->viewportFilter.reset();
451
452
453
454
455QAbstractScrollArea::QAbstractScrollArea(QWidget *parent)
456 :QFrame(*
new QAbstractScrollAreaPrivate, parent)
458 Q_D(QAbstractScrollArea);
462 d->viewportFilter.reset();
469
470
471QAbstractScrollArea::~QAbstractScrollArea()
473 Q_D(QAbstractScrollArea);
475 d->viewportFilter.reset();
480
481
482
483
484
485
486
487
488
489void QAbstractScrollArea::setViewport(QWidget *widget)
491 Q_D(QAbstractScrollArea);
492 if (widget != d->viewport) {
493 QWidget *oldViewport = d->viewport;
495 widget =
new QWidget;
496 d->viewport = widget;
497 d->viewport->setParent(
this);
498 d->viewport->setFocusProxy(
this);
499 d->viewport->installEventFilter(d->viewportFilter.data());
500#ifndef QT_NO_GESTURES
501 d->viewport->grabGesture(Qt::PanGesture);
505 QWidgetPrivate::get(d->viewport)->initializeViewportFramebuffer();
509 setupViewport(widget);
515
516
517
518
519
520
521
522QWidget *QAbstractScrollArea::viewport()
const
524 Q_D(
const QAbstractScrollArea);
530
531
532
533QSize QAbstractScrollArea::maximumViewportSize()
const
535 Q_D(
const QAbstractScrollArea);
536 int f = 2 * d->frameWidth;
537 QSize max = size() - QSize(f + d->left + d->right, f + d->top + d->bottom);
539 if (d->vbarpolicy == Qt::ScrollBarAlwaysOn)
540 max.rwidth() -= d->vbar->sizeHint().width();
541 if (d->hbarpolicy == Qt::ScrollBarAlwaysOn)
542 max.rheight() -= d->hbar->sizeHint().height();
547
548
549
550
551
552
553
555Qt::ScrollBarPolicy QAbstractScrollArea::verticalScrollBarPolicy()
const
557 Q_D(
const QAbstractScrollArea);
558 return d->vbarpolicy;
561void QAbstractScrollArea::setVerticalScrollBarPolicy(Qt::ScrollBarPolicy policy)
563 Q_D(QAbstractScrollArea);
564 const Qt::ScrollBarPolicy oldPolicy = d->vbarpolicy;
565 d->vbarpolicy = policy;
568 if (oldPolicy != d->vbarpolicy)
569 d->scrollBarPolicyChanged(Qt::Vertical, d->vbarpolicy);
574
575
576
577
578QScrollBar *QAbstractScrollArea::verticalScrollBar()
const
580 Q_D(
const QAbstractScrollArea);
585
586
587
588
589
590
591
592
593
594
595
596void QAbstractScrollArea::setVerticalScrollBar(QScrollBar *scrollBar)
598 Q_D(QAbstractScrollArea);
599 if (Q_UNLIKELY(!scrollBar)) {
600 qWarning(
"QAbstractScrollArea::setVerticalScrollBar: Cannot set a null scroll bar");
604 d->replaceScrollBar(scrollBar, Qt::Vertical);
608
609
610
611
612
613
614
616Qt::ScrollBarPolicy QAbstractScrollArea::horizontalScrollBarPolicy()
const
618 Q_D(
const QAbstractScrollArea);
619 return d->hbarpolicy;
622void QAbstractScrollArea::setHorizontalScrollBarPolicy(Qt::ScrollBarPolicy policy)
624 Q_D(QAbstractScrollArea);
625 const Qt::ScrollBarPolicy oldPolicy = d->hbarpolicy;
626 d->hbarpolicy = policy;
629 if (oldPolicy != d->hbarpolicy)
630 d->scrollBarPolicyChanged(Qt::Horizontal, d->hbarpolicy);
634
635
636
637
638QScrollBar *QAbstractScrollArea::horizontalScrollBar()
const
640 Q_D(
const QAbstractScrollArea);
645
646
647
648
649
650
651
652
653
654
655
656
657void QAbstractScrollArea::setHorizontalScrollBar(QScrollBar *scrollBar)
659 Q_D(QAbstractScrollArea);
660 if (Q_UNLIKELY(!scrollBar)) {
661 qWarning(
"QAbstractScrollArea::setHorizontalScrollBar: Cannot set a null scroll bar");
665 d->replaceScrollBar(scrollBar, Qt::Horizontal);
669
670
671
672
673
674
675QWidget *QAbstractScrollArea::cornerWidget()
const
677 Q_D(
const QAbstractScrollArea);
678 return d->cornerWidget;
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707void QAbstractScrollArea::setCornerWidget(QWidget *widget)
709 Q_D(QAbstractScrollArea);
710 QWidget* oldWidget = d->cornerWidget;
711 if (oldWidget != widget) {
714 d->cornerWidget = widget;
716 if (widget && widget->parentWidget() !=
this)
717 widget->setParent(
this);
723 d->cornerWidget = widget;
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760void QAbstractScrollArea::addScrollBarWidget(QWidget *widget, Qt::Alignment alignment)
762 Q_D(QAbstractScrollArea);
764 if (widget ==
nullptr)
767 const Qt::Orientation scrollBarOrientation
768 = ((alignment & Qt::AlignLeft) || (alignment & Qt::AlignRight)) ? Qt::Horizontal : Qt::Vertical;
769 const QAbstractScrollAreaScrollBarContainer::LogicalPosition position
770 = ((alignment & Qt::AlignRight) || (alignment & Qt::AlignBottom))
771 ? QAbstractScrollAreaScrollBarContainer::LogicalRight : QAbstractScrollAreaScrollBarContainer::LogicalLeft;
772 d->scrollBarContainers[scrollBarOrientation]->addWidget(widget, position);
774 if (isHidden() ==
false)
779
780
781
782
783
784
785QWidgetList QAbstractScrollArea::scrollBarWidgets(Qt::Alignment alignment)
787 Q_D(QAbstractScrollArea);
791 if (alignment & Qt::AlignLeft)
792 list += d->scrollBarContainers[Qt::Horizontal]->widgets(QAbstractScrollAreaScrollBarContainer::LogicalLeft);
793 if (alignment & Qt::AlignRight)
794 list += d->scrollBarContainers[Qt::Horizontal]->widgets(QAbstractScrollAreaScrollBarContainer::LogicalRight);
795 if (alignment & Qt::AlignTop)
796 list += d->scrollBarContainers[Qt::Vertical]->widgets(QAbstractScrollAreaScrollBarContainer::LogicalLeft);
797 if (alignment & Qt::AlignBottom)
798 list += d->scrollBarContainers[Qt::Vertical]->widgets(QAbstractScrollAreaScrollBarContainer::LogicalRight);
804
805
806
807
808
809
810
811
812
813
814
815
816
817void QAbstractScrollArea::setViewportMargins(
int left,
int top,
int right,
int bottom)
819 Q_D(QAbstractScrollArea);
828
829
830
831
832
833
834
835
836
837void QAbstractScrollArea::setViewportMargins(
const QMargins &margins)
839 setViewportMargins(margins.left(), margins.top(),
840 margins.right(), margins.bottom());
844
845
846
847
848
849
850QMargins QAbstractScrollArea::viewportMargins()
const
852 Q_D(
const QAbstractScrollArea);
853 return QMargins(d->left, d->top, d->right, d->bottom);
857bool QAbstractScrollArea::eventFilter(QObject *o, QEvent *e)
859 Q_D(QAbstractScrollArea);
860 if ((o == d->hbar || o == d->vbar) && (e->type() == QEvent::HoverEnter || e->type() == QEvent::HoverLeave)) {
861 if (d->hbarpolicy == Qt::ScrollBarAsNeeded && d->vbarpolicy == Qt::ScrollBarAsNeeded) {
862 QScrollBar *sbar =
static_cast<QScrollBar*>(o);
863 QScrollBar *sibling = sbar == d->hbar ? d->vbar : d->hbar;
864 if (sbar->style()->styleHint(QStyle::SH_ScrollBar_Transient,
nullptr, sbar) &&
865 sibling->style()->styleHint(QStyle::SH_ScrollBar_Transient,
nullptr, sibling))
866 d->setScrollBarTransient(sibling, e->type() == QEvent::HoverLeave);
869 return QFrame::eventFilter(o, e);
873
874
875
876
877
878
879
880
881
882
883
884bool QAbstractScrollArea::event(QEvent *e)
886 Q_D(QAbstractScrollArea);
888 case QEvent::AcceptDropsChange:
895 d->viewport->setAcceptDrops(acceptDrops());
897 case QEvent::MouseTrackingChange:
898 d->viewport->setMouseTracking(hasMouseTracking());
908 if (!d->shownOnce && d->sizeAdjustPolicy == QAbstractScrollArea::AdjustToContentsOnFirstShow) {
909 d->sizeHint = QSize();
913 return QFrame::event(e);
914 case QEvent::Paint: {
916 option.initFrom(
this);
917 if (d->cornerPaintingRect.isValid()) {
918 option.rect = d->cornerPaintingRect;
920 style()->drawPrimitive(QStyle::PE_PanelScrollAreaCorner, &option, &p,
this);
923 QFrame::paintEvent((QPaintEvent*)e);
925#ifndef QT_NO_CONTEXTMENU
926 case QEvent::ContextMenu:
927 if (
static_cast<QContextMenuEvent *>(e)->reason() == QContextMenuEvent::Keyboard)
928 return QFrame::event(e);
932 case QEvent::MouseButtonPress:
933 case QEvent::MouseButtonRelease:
934 case QEvent::MouseButtonDblClick:
935 case QEvent::MouseMove:
937#if QT_CONFIG(draganddrop)
939 case QEvent::DragEnter:
940 case QEvent::DragMove:
941 case QEvent::DragLeave:
944 case QEvent::TouchBegin:
945 case QEvent::TouchUpdate:
946 case QEvent::TouchEnd:
948#ifndef QT_NO_GESTURES
949 case QEvent::Gesture:
951 QGestureEvent *ge =
static_cast<QGestureEvent *>(e);
952 QPanGesture *g =
static_cast<QPanGesture *>(ge->gesture(Qt::PanGesture));
954 QScrollBar *hBar = horizontalScrollBar();
955 QScrollBar *vBar = verticalScrollBar();
956 QPointF delta = g->delta();
957 if (!delta.isNull()) {
958 if (QGuiApplication::isRightToLeft())
960 int newX = hBar->value() - delta.x();
961 int newY = vBar->value() - delta.y();
962 hBar->setValue(newX);
963 vBar->setValue(newY);
970 case QEvent::ScrollPrepare:
972 QScrollPrepareEvent *se =
static_cast<QScrollPrepareEvent *>(e);
973 if (d->canStartScrollingAt(se->startPos().toPoint())) {
974 QScrollBar *hBar = horizontalScrollBar();
975 QScrollBar *vBar = verticalScrollBar();
977 se->setViewportSize(QSizeF(viewport()->size()));
978 se->setContentPosRange(QRectF(0, 0, hBar->maximum(), vBar->maximum()));
979 se->setContentPos(QPointF(hBar->value(), vBar->value()));
987 QScrollEvent *se =
static_cast<QScrollEvent *>(e);
989 QScrollBar *hBar = horizontalScrollBar();
990 QScrollBar *vBar = verticalScrollBar();
991 hBar->setValue(se->contentPos().x());
992 vBar->setValue(se->contentPos().y());
994 QPoint delta = d->overshoot - se->overshootDistance().toPoint();
996 viewport()->move(viewport()->pos() + delta);
998 d->overshoot = se->overshootDistance().toPoint();
1002 case QEvent::StyleChange:
1003 case QEvent::LayoutDirectionChange:
1004 case QEvent::ApplicationLayoutDirectionChange:
1005 case QEvent::LayoutRequest:
1006 d->layoutChildren();
1009 return QFrame::event(e);
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034bool QAbstractScrollArea::viewportEvent(QEvent *e)
1036 switch (e->type()) {
1037 case QEvent::Resize:
1039 case QEvent::MouseButtonPress:
1040 case QEvent::MouseButtonRelease:
1041 case QEvent::MouseButtonDblClick:
1042 case QEvent::TouchBegin:
1043 case QEvent::TouchUpdate:
1044 case QEvent::TouchEnd:
1045 case QEvent::MouseMove:
1046 case QEvent::ContextMenu:
1047#if QT_CONFIG(wheelevent)
1050#if QT_CONFIG(draganddrop)
1052 case QEvent::DragEnter:
1053 case QEvent::DragMove:
1054 case QEvent::DragLeave:
1059 if (e->type() == QEvent::Resize)
1060 QWidgetPrivate::get(viewport())->resizeViewportFramebuffer();
1062 return QFrame::event(e);
1063 case QEvent::LayoutRequest:
1064#ifndef QT_NO_GESTURES
1065 case QEvent::Gesture:
1066 case QEvent::GestureOverride:
1069 case QEvent::ScrollPrepare:
1070 case QEvent::Scroll:
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091void QAbstractScrollArea::resizeEvent(QResizeEvent *)
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105void QAbstractScrollArea::paintEvent(QPaintEvent*)
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119void QAbstractScrollArea::mousePressEvent(QMouseEvent *e)
1121 QWidget::mousePressEvent(e);
1125
1126
1127
1128
1129
1130
1131void QAbstractScrollArea::mouseReleaseEvent(QMouseEvent *e)
1137
1138
1139
1140
1141
1142
1143void QAbstractScrollArea::mouseDoubleClickEvent(QMouseEvent *e)
1149
1150
1151
1152
1153
1154
1155void QAbstractScrollArea::mouseMoveEvent(QMouseEvent *e)
1161
1162
1163
1164
1165
1166
1167#if QT_CONFIG(wheelevent)
1168void QAbstractScrollArea::wheelEvent(QWheelEvent *e)
1170 Q_D(QAbstractScrollArea);
1171 if (qAbs(e->angleDelta().x()) > qAbs(e->angleDelta().y()))
1172 QCoreApplication::sendEvent(d->hbar, e);
1174 QCoreApplication::sendEvent(d->vbar, e);
1178#ifndef QT_NO_CONTEXTMENU
1180
1181
1182
1183
1184
1185
1186void QAbstractScrollArea::contextMenuEvent(QContextMenuEvent *e)
1193
1194
1195
1196
1197void QAbstractScrollArea::keyPressEvent(QKeyEvent * e)
1199 Q_D(QAbstractScrollArea);
1201#ifndef QT_NO_SHORTCUT
1202 }
else if (e == QKeySequence::MoveToPreviousPage) {
1203 d->vbar->triggerAction(QScrollBar::SliderPageStepSub);
1204 }
else if (e == QKeySequence::MoveToNextPage) {
1205 d->vbar->triggerAction(QScrollBar::SliderPageStepAdd);
1210 d->vbar->triggerAction(QScrollBar::SliderSingleStepSub);
1213 d->vbar->triggerAction(QScrollBar::SliderSingleStepAdd);
1216 d->hbar->triggerAction(
1217 layoutDirection() == Qt::LeftToRight
1218 ? QScrollBar::SliderSingleStepSub : QScrollBar::SliderSingleStepAdd);
1221 d->hbar->triggerAction(
1222 layoutDirection() == Qt::LeftToRight
1223 ? QScrollBar::SliderSingleStepAdd : QScrollBar::SliderSingleStepSub);
1234#if QT_CONFIG(draganddrop)
1236
1237
1238
1239
1240
1241
1242
1243void QAbstractScrollArea::dragEnterEvent(QDragEnterEvent *)
1248
1249
1250
1251
1252
1253
1254
1255void QAbstractScrollArea::dragMoveEvent(QDragMoveEvent *)
1260
1261
1262
1263
1264
1265
1266
1267void QAbstractScrollArea::dragLeaveEvent(QDragLeaveEvent *)
1272
1273
1274
1275
1276
1277
1278
1279void QAbstractScrollArea::dropEvent(QDropEvent *)
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304void QAbstractScrollArea::scrollContentsBy(
int,
int)
1306 viewport()->update();
1309bool QAbstractScrollAreaPrivate::canStartScrollingAt(
const QPoint &startPos)
const
1311 Q_Q(
const QAbstractScrollArea);
1314 if (qobject_cast<QAbstractSlider *>(q->viewport()->childAt(startPos)))
1320void QAbstractScrollAreaPrivate::flashScrollBars()
1322 QStyleOptionSlider opt;
1323 hbar->initStyleOption(&opt);
1325 bool htransient = hbar->style()->styleHint(QStyle::SH_ScrollBar_Transient, &opt, hbar);
1326 if ((hbarpolicy != Qt::ScrollBarAlwaysOff) && (hbarpolicy == Qt::ScrollBarAsNeeded || htransient))
1327 hbar->d_func()->flash();
1328 vbar->initStyleOption(&opt);
1329 bool vtransient = vbar->style()->styleHint(QStyle::SH_ScrollBar_Transient, &opt, vbar);
1330 if ((vbarpolicy != Qt::ScrollBarAlwaysOff) && (vbarpolicy == Qt::ScrollBarAsNeeded || vtransient))
1331 vbar->d_func()->flash();
1334void QAbstractScrollAreaPrivate::setScrollBarTransient(QScrollBar *scrollBar,
bool transient)
1336 scrollBar->d_func()->setTransient(transient);
1339void QAbstractScrollAreaPrivate::_q_hslide(
int x)
1341 Q_Q(QAbstractScrollArea);
1342 int dx = xoffset - x;
1344 q->scrollContentsBy(dx, 0);
1348void QAbstractScrollAreaPrivate::_q_vslide(
int y)
1350 Q_Q(QAbstractScrollArea);
1351 int dy = yoffset - y;
1353 q->scrollContentsBy(0, dy);
1357void QAbstractScrollAreaPrivate::_q_showOrHideScrollBars()
1362QPoint QAbstractScrollAreaPrivate::contentsOffset()
const
1364 Q_Q(
const QAbstractScrollArea);
1366 if (vbar->isVisible())
1367 offset.setY(vbar->value());
1368 if (hbar->isVisible()) {
1369 if (q->isRightToLeft())
1370 offset.setX(hbar->maximum() - hbar->value());
1372 offset.setX(hbar->value());
1378
1379
1380
1381QSize QAbstractScrollArea::minimumSizeHint()
const
1383 Q_D(
const QAbstractScrollArea);
1384 int hsbExt = d->hbar->sizeHint().height();
1385 int vsbExt = d->vbar->sizeHint().width();
1386 int extra = 2 * d->frameWidth;
1389 if ((d->frameStyle != QFrame::NoFrame)
1390 && style()->styleHint(QStyle::SH_ScrollView_FrameOnlyAroundContents, &opt,
this)) {
1391 extra += style()->pixelMetric(QStyle::PM_ScrollView_ScrollBarSpacing, &opt,
this);
1393 return QSize(d->scrollBarContainers[Qt::Horizontal]->sizeHint().width() + vsbExt + extra,
1394 d->scrollBarContainers[Qt::Vertical]->sizeHint().height() + hsbExt + extra);
1398
1399
1400
1401
1402QSize QAbstractScrollArea::sizeHint()
const
1404 Q_D(
const QAbstractScrollArea);
1405 if (d->sizeAdjustPolicy == QAbstractScrollArea::AdjustIgnored)
1406 return QSize(256, 192);
1408 if (!d->sizeHint.isValid() || d->sizeAdjustPolicy == QAbstractScrollArea::AdjustToContents) {
1409 const int f = 2 * d->frameWidth;
1410 const QSize frame(f, f);
1411 const bool vbarHidden = !d->vbar->isVisibleTo(
this) || d->vbarpolicy == Qt::ScrollBarAlwaysOff;
1412 const bool hbarHidden = !d->hbar->isVisibleTo(
this) || d->hbarpolicy == Qt::ScrollBarAlwaysOff;
1413 const QSize scrollbars(vbarHidden ? 0 : d->vbar->sizeHint().width(),
1414 hbarHidden ? 0 : d->hbar->sizeHint().height());
1415 d->sizeHint = frame + scrollbars + viewportSizeHint();
1421
1422
1423
1424
1425
1426QSize QAbstractScrollArea::viewportSizeHint()
const
1428 Q_D(
const QAbstractScrollArea);
1430 const QSize sh = d->viewport->sizeHint();
1435 const int h = qMax(10, fontMetrics().height());
1436 return QSize(6 * h, 4 * h);
1440
1441
1442
1443
1444
1445
1446
1447
1449QAbstractScrollArea::SizeAdjustPolicy QAbstractScrollArea::sizeAdjustPolicy()
const
1451 Q_D(
const QAbstractScrollArea);
1452 return d->sizeAdjustPolicy;
1455void QAbstractScrollArea::setSizeAdjustPolicy(SizeAdjustPolicy policy)
1457 Q_D(QAbstractScrollArea);
1458 if (d->sizeAdjustPolicy == policy)
1461 d->sizeAdjustPolicy = policy;
1462 d->sizeHint = QSize();
1467
1468
1469
1470
1471
1472
1473
1474void QAbstractScrollArea::setupViewport(QWidget *viewport)
1479int QAbstractScrollAreaPrivate::defaultSingleStep()
const
1481 auto *platformTheme = QGuiApplicationPrivate::platformTheme();
1482 return platformTheme->themeHint(QPlatformTheme::ScrollSingleStepDistance).value<
int>();
1487#include "moc_qabstractscrollarea.cpp"
1488#include "moc_qabstractscrollarea_p.cpp"