diff options
Diffstat (limited to 'src/widgets/widgets/qabstractscrollarea.cpp')
-rw-r--r-- | src/widgets/widgets/qabstractscrollarea.cpp | 177 |
1 files changed, 79 insertions, 98 deletions
diff --git a/src/widgets/widgets/qabstractscrollarea.cpp b/src/widgets/widgets/qabstractscrollarea.cpp index f7407d7d74..f6f8e8b795 100644 --- a/src/widgets/widgets/qabstractscrollarea.cpp +++ b/src/widgets/widgets/qabstractscrollarea.cpp @@ -1,41 +1,5 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtWidgets module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only #include "qabstractscrollarea.h" @@ -68,6 +32,8 @@ QT_BEGIN_NAMESPACE +using namespace Qt::StringLiterals; + /*! \class QAbstractScrollArea \brief The QAbstractScrollArea widget provides a scrolling area with @@ -202,7 +168,7 @@ void QAbstractScrollAreaScrollBarContainer::addWidget(QWidget *widget, LogicalPo } /*! \internal - Retuns a list of scroll bar widgets for the given position. The scroll bar + Returns a list of scroll-bar widgets for the given position. The scroll bar itself is not returned. */ QWidgetList QAbstractScrollAreaScrollBarContainer::widgets(LogicalPosition position) @@ -282,11 +248,11 @@ void QAbstractScrollAreaPrivate::init() { Q_Q(QAbstractScrollArea); viewport = new QWidget(q); - viewport->setObjectName(QLatin1String("qt_scrollarea_viewport")); + viewport->setObjectName("qt_scrollarea_viewport"_L1); viewport->setBackgroundRole(QPalette::Base); viewport->setAutoFillBackground(true); scrollBarContainers[Qt::Horizontal] = new QAbstractScrollAreaScrollBarContainer(Qt::Horizontal, q); - scrollBarContainers[Qt::Horizontal]->setObjectName(QLatin1String("qt_scrollarea_hcontainer")); + scrollBarContainers[Qt::Horizontal]->setObjectName("qt_scrollarea_hcontainer"_L1); hbar = scrollBarContainers[Qt::Horizontal]->scrollBar; hbar->setRange(0,0); scrollBarContainers[Qt::Horizontal]->setVisible(false); @@ -294,7 +260,7 @@ void QAbstractScrollAreaPrivate::init() QObject::connect(hbar, SIGNAL(valueChanged(int)), q, SLOT(_q_hslide(int))); QObject::connect(hbar, SIGNAL(rangeChanged(int,int)), q, SLOT(_q_showOrHideScrollBars()), Qt::QueuedConnection); scrollBarContainers[Qt::Vertical] = new QAbstractScrollAreaScrollBarContainer(Qt::Vertical, q); - scrollBarContainers[Qt::Vertical]->setObjectName(QLatin1String("qt_scrollarea_vcontainer")); + scrollBarContainers[Qt::Vertical]->setObjectName("qt_scrollarea_vcontainer"_L1); vbar = scrollBarContainers[Qt::Vertical]->scrollBar; vbar->setRange(0,0); scrollBarContainers[Qt::Vertical]->setVisible(false); @@ -329,22 +295,25 @@ void QAbstractScrollAreaPrivate::layoutChildren() void QAbstractScrollAreaPrivate::layoutChildren_helper(bool *needHorizontalScrollbar, bool *needVerticalScrollbar) { Q_Q(QAbstractScrollArea); - bool htransient = hbar->style()->styleHint(QStyle::SH_ScrollBar_Transient, nullptr, hbar); + QStyleOptionSlider barOpt; + + hbar->initStyleOption(&barOpt); + bool htransient = hbar->style()->styleHint(QStyle::SH_ScrollBar_Transient, &barOpt, hbar); bool needh = *needHorizontalScrollbar || ((hbarpolicy != Qt::ScrollBarAlwaysOff) && ((hbarpolicy == Qt::ScrollBarAlwaysOn && !htransient) || ((hbarpolicy == Qt::ScrollBarAsNeeded || htransient) && hbar->minimum() < hbar->maximum() && !hbar->sizeHint().isEmpty()))); + const int hscrollOverlap = hbar->style()->pixelMetric(QStyle::PM_ScrollView_ScrollBarOverlap, &barOpt, hbar); - bool vtransient = vbar->style()->styleHint(QStyle::SH_ScrollBar_Transient, nullptr, vbar); + vbar->initStyleOption(&barOpt); + bool vtransient = vbar->style()->styleHint(QStyle::SH_ScrollBar_Transient, &barOpt, vbar); bool needv = *needVerticalScrollbar || ((vbarpolicy != Qt::ScrollBarAlwaysOff) && ((vbarpolicy == Qt::ScrollBarAlwaysOn && !vtransient) || ((vbarpolicy == Qt::ScrollBarAsNeeded || vtransient) && vbar->minimum() < vbar->maximum() && !vbar->sizeHint().isEmpty()))); + const int vscrollOverlap = vbar->style()->pixelMetric(QStyle::PM_ScrollView_ScrollBarOverlap, &barOpt, vbar); QStyleOption opt(0); opt.initFrom(q); - const int hscrollOverlap = hbar->style()->pixelMetric(QStyle::PM_ScrollView_ScrollBarOverlap, &opt, hbar); - const int vscrollOverlap = vbar->style()->pixelMetric(QStyle::PM_ScrollView_ScrollBarOverlap, &opt, vbar); - const int hsbExt = hbar->sizeHint().height(); const int vsbExt = vbar->sizeHint().width(); const QPoint extPoint(vsbExt, hsbExt); @@ -402,7 +371,7 @@ void QAbstractScrollAreaPrivate::layoutChildren_helper(bool *needHorizontalScrol #if QT_CONFIG(itemviews) if ((vscrollOverlap > 0 && needv) || (hscrollOverlap > 0 && needh)) { const QList<QHeaderView *> headers = q->findChildren<QHeaderView*>(); - if (headers.count() <= 2) { + if (headers.size() <= 2) { for (const QHeaderView *header : headers) { const QRect geo = header->geometry(); if (header->orientation() == Qt::Vertical && header->isVisible() && QStyle::visualRect(opt.direction, opt.rect, geo).left() <= opt.rect.width() / 2) @@ -442,8 +411,10 @@ void QAbstractScrollAreaPrivate::layoutChildren_helper(bool *needHorizontalScrol viewportRect.adjust(right, top, -left, -bottom); else viewportRect.adjust(left, top, -right, -bottom); + viewportRect = QStyle::visualRect(opt.direction, opt.rect, viewportRect); + viewportRect.translate(-overshoot); + viewport->setGeometry(viewportRect); // resize the viewport last - viewport->setGeometry(QStyle::visualRect(opt.direction, opt.rect, viewportRect)); // resize the viewport last *needHorizontalScrollbar = needh; *needVerticalScrollbar = needv; } @@ -564,15 +535,13 @@ scrolling range. QSize QAbstractScrollArea::maximumViewportSize() const { Q_D(const QAbstractScrollArea); - int hsbExt = d->hbar->sizeHint().height(); - int vsbExt = d->vbar->sizeHint().width(); - int f = 2 * d->frameWidth; QSize max = size() - QSize(f + d->left + d->right, f + d->top + d->bottom); + // Count the sizeHint of the bar only if it is displayed. if (d->vbarpolicy == Qt::ScrollBarAlwaysOn) - max.rwidth() -= vsbExt; + max.rwidth() -= d->vbar->sizeHint().width(); if (d->hbarpolicy == Qt::ScrollBarAlwaysOn) - max.rheight() -= hsbExt; + max.rheight() -= d->hbar->sizeHint().height(); return max; } @@ -836,7 +805,7 @@ QWidgetList QAbstractScrollArea::scrollBarWidgets(Qt::Alignment alignment) /*! Sets the margins around the scrolling area to \a left, \a top, \a right and \a bottom. This is useful for applications such as - spreadsheets with "locked" rows and columns. The marginal space is + spreadsheets with "locked" rows and columns. The marginal space is left blank; put widgets in the unused area. Note that this function is frequently called by QTreeView and @@ -1131,7 +1100,7 @@ void QAbstractScrollArea::resizeEvent(QResizeEvent *) This event handler can be reimplemented in a subclass to receive paint events (passed in \a event), for the viewport() widget. - \note If you open a painter, make sure to open it on the viewport(). + \note If you create a QPainter, it must operate on the viewport(). \sa QWidget::paintEvent() */ @@ -1230,49 +1199,57 @@ void QAbstractScrollArea::contextMenuEvent(QContextMenuEvent *e) void QAbstractScrollArea::keyPressEvent(QKeyEvent * e) { Q_D(QAbstractScrollArea); - + if (false){ +#ifndef QT_NO_SHORTCUT + } else if (e == QKeySequence::MoveToPreviousPage) { + d->vbar->triggerAction(QScrollBar::SliderPageStepSub); + } else if (e == QKeySequence::MoveToNextPage) { + d->vbar->triggerAction(QScrollBar::SliderPageStepAdd); +#endif + } else { #ifdef QT_KEYPAD_NAVIGATION - if (QApplicationPrivate::keypadNavigationEnabled() && !hasEditFocus()) { - e->ignore(); - return; - } + if (QApplicationPrivate::keypadNavigationEnabled() && !hasEditFocus()) { + e->ignore(); + return; + } #endif - switch (e->key()) { - case Qt::Key_Up: - d->vbar->triggerAction(QScrollBar::SliderSingleStepSub); - break; - case Qt::Key_Down: - d->vbar->triggerAction(QScrollBar::SliderSingleStepAdd); - break; - case Qt::Key_Left: + switch (e->key()) { + case Qt::Key_Up: + d->vbar->triggerAction(QScrollBar::SliderSingleStepSub); + break; + case Qt::Key_Down: + d->vbar->triggerAction(QScrollBar::SliderSingleStepAdd); + break; + case Qt::Key_Left: #ifdef QT_KEYPAD_NAVIGATION - if (QApplicationPrivate::keypadNavigationEnabled() && hasEditFocus() - && (!d->hbar->isVisible() || d->hbar->value() == d->hbar->minimum())) { - //if we aren't using the hbar or we are already at the leftmost point ignore - e->ignore(); - return; - } + if (QApplicationPrivate::keypadNavigationEnabled() && hasEditFocus() + && (!d->hbar->isVisible() || d->hbar->value() == d->hbar->minimum())) { + //if we aren't using the hbar or we are already at the leftmost point ignore + e->ignore(); + return; + } #endif - d->hbar->triggerAction( - layoutDirection() == Qt::LeftToRight - ? QScrollBar::SliderSingleStepSub : QScrollBar::SliderSingleStepAdd); - break; - case Qt::Key_Right: + d->hbar->triggerAction( + layoutDirection() == Qt::LeftToRight + ? QScrollBar::SliderSingleStepSub : QScrollBar::SliderSingleStepAdd); + break; + case Qt::Key_Right: #ifdef QT_KEYPAD_NAVIGATION - if (QApplicationPrivate::keypadNavigationEnabled() && hasEditFocus() - && (!d->hbar->isVisible() || d->hbar->value() == d->hbar->maximum())) { - //if we aren't using the hbar or we are already at the rightmost point ignore - e->ignore(); - return; - } + if (QApplicationPrivate::keypadNavigationEnabled() && hasEditFocus() + && (!d->hbar->isVisible() || d->hbar->value() == d->hbar->maximum())) { + //if we aren't using the hbar or we are already at the rightmost point ignore + e->ignore(); + return; + } #endif - d->hbar->triggerAction( - layoutDirection() == Qt::LeftToRight - ? QScrollBar::SliderSingleStepAdd : QScrollBar::SliderSingleStepSub); - break; - default: - e->ignore(); - return; + d->hbar->triggerAction( + layoutDirection() == Qt::LeftToRight + ? QScrollBar::SliderSingleStepAdd : QScrollBar::SliderSingleStepSub); + break; + default: + e->ignore(); + return; + } } e->accept(); } @@ -1366,10 +1343,14 @@ bool QAbstractScrollAreaPrivate::canStartScrollingAt(const QPoint &startPos) con void QAbstractScrollAreaPrivate::flashScrollBars() { - bool htransient = hbar->style()->styleHint(QStyle::SH_ScrollBar_Transient, nullptr, hbar); + QStyleOptionSlider opt; + hbar->initStyleOption(&opt); + + bool htransient = hbar->style()->styleHint(QStyle::SH_ScrollBar_Transient, &opt, hbar); if ((hbarpolicy != Qt::ScrollBarAlwaysOff) && (hbarpolicy == Qt::ScrollBarAsNeeded || htransient)) hbar->d_func()->flash(); - bool vtransient = vbar->style()->styleHint(QStyle::SH_ScrollBar_Transient, nullptr, vbar); + vbar->initStyleOption(&opt); + bool vtransient = vbar->style()->styleHint(QStyle::SH_ScrollBar_Transient, &opt, vbar); if ((vbarpolicy != Qt::ScrollBarAlwaysOff) && (vbarpolicy == Qt::ScrollBarAsNeeded || vtransient)) vbar->d_func()->flash(); } @@ -1450,9 +1431,9 @@ QSize QAbstractScrollArea::sizeHint() const if (!d->sizeHint.isValid() || d->sizeAdjustPolicy == QAbstractScrollArea::AdjustToContents) { const int f = 2 * d->frameWidth; - const QSize frame( f, f ); - const bool vbarHidden = d->vbar->isHidden() || d->vbarpolicy == Qt::ScrollBarAlwaysOff; - const bool hbarHidden = d->hbar->isHidden() || d->hbarpolicy == Qt::ScrollBarAlwaysOff; + const QSize frame(f, f); + const bool vbarHidden = !d->vbar->isVisibleTo(this) || d->vbarpolicy == Qt::ScrollBarAlwaysOff; + const bool hbarHidden = !d->vbar->isVisibleTo(this) || d->hbarpolicy == Qt::ScrollBarAlwaysOff; const QSize scrollbars(vbarHidden ? 0 : d->vbar->sizeHint().width(), hbarHidden ? 0 : d->hbar->sizeHint().height()); d->sizeHint = frame + scrollbars + viewportSizeHint(); |