| Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/widgets/widgets/qscrollarea.cpp |
| Source code | Switch to Preprocessed file |
| Line | Source | Count | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | /**************************************************************************** | - | ||||||||||||||||||
| 2 | ** | - | ||||||||||||||||||
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. | - | ||||||||||||||||||
| 4 | ** Contact: https://www.qt.io/licensing/ | - | ||||||||||||||||||
| 5 | ** | - | ||||||||||||||||||
| 6 | ** This file is part of the QtWidgets module of the Qt Toolkit. | - | ||||||||||||||||||
| 7 | ** | - | ||||||||||||||||||
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ | - | ||||||||||||||||||
| 9 | ** Commercial License Usage | - | ||||||||||||||||||
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||||||||||||||
| 11 | ** accordance with the commercial license agreement provided with the | - | ||||||||||||||||||
| 12 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||||||||||||||
| 13 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||||||||||||||
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further | - | ||||||||||||||||||
| 15 | ** information use the contact form at https://www.qt.io/contact-us. | - | ||||||||||||||||||
| 16 | ** | - | ||||||||||||||||||
| 17 | ** GNU Lesser General Public License Usage | - | ||||||||||||||||||
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||||||||||||||
| 19 | ** General Public License version 3 as published by the Free Software | - | ||||||||||||||||||
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the | - | ||||||||||||||||||
| 21 | ** packaging of this file. Please review the following information to | - | ||||||||||||||||||
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements | - | ||||||||||||||||||
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. | - | ||||||||||||||||||
| 24 | ** | - | ||||||||||||||||||
| 25 | ** GNU General Public License Usage | - | ||||||||||||||||||
| 26 | ** Alternatively, this file may be used under the terms of the GNU | - | ||||||||||||||||||
| 27 | ** General Public License version 2.0 or (at your option) the GNU General | - | ||||||||||||||||||
| 28 | ** Public license version 3 or any later version approved by the KDE Free | - | ||||||||||||||||||
| 29 | ** Qt Foundation. The licenses are as published by the Free Software | - | ||||||||||||||||||
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 | - | ||||||||||||||||||
| 31 | ** included in the packaging of this file. Please review the following | - | ||||||||||||||||||
| 32 | ** information to ensure the GNU General Public License requirements will | - | ||||||||||||||||||
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and | - | ||||||||||||||||||
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. | - | ||||||||||||||||||
| 35 | ** | - | ||||||||||||||||||
| 36 | ** $QT_END_LICENSE$ | - | ||||||||||||||||||
| 37 | ** | - | ||||||||||||||||||
| 38 | ****************************************************************************/ | - | ||||||||||||||||||
| 39 | - | |||||||||||||||||||
| 40 | #include "qscrollarea.h" | - | ||||||||||||||||||
| 41 | #include "private/qscrollarea_p.h" | - | ||||||||||||||||||
| 42 | - | |||||||||||||||||||
| 43 | #ifndef QT_NO_SCROLLAREA | - | ||||||||||||||||||
| 44 | - | |||||||||||||||||||
| 45 | #include "qscrollbar.h" | - | ||||||||||||||||||
| 46 | #include "qlayout.h" | - | ||||||||||||||||||
| 47 | #include "qstyle.h" | - | ||||||||||||||||||
| 48 | #include "qapplication.h" | - | ||||||||||||||||||
| 49 | #include "qvariant.h" | - | ||||||||||||||||||
| 50 | #include "qdebug.h" | - | ||||||||||||||||||
| 51 | #include "private/qlayoutengine_p.h" | - | ||||||||||||||||||
| 52 | - | |||||||||||||||||||
| 53 | QT_BEGIN_NAMESPACE | - | ||||||||||||||||||
| 54 | - | |||||||||||||||||||
| 55 | /*! | - | ||||||||||||||||||
| 56 | \class QScrollArea | - | ||||||||||||||||||
| 57 | - | |||||||||||||||||||
| 58 | \brief The QScrollArea class provides a scrolling view onto | - | ||||||||||||||||||
| 59 | another widget. | - | ||||||||||||||||||
| 60 | - | |||||||||||||||||||
| 61 | \ingroup basicwidgets | - | ||||||||||||||||||
| 62 | \inmodule QtWidgets | - | ||||||||||||||||||
| 63 | - | |||||||||||||||||||
| 64 | A scroll area is used to display the contents of a child widget | - | ||||||||||||||||||
| 65 | within a frame. If the widget exceeds the size of the frame, the | - | ||||||||||||||||||
| 66 | view can provide scroll bars so that the entire area of the child | - | ||||||||||||||||||
| 67 | widget can be viewed. The child widget must be specified with | - | ||||||||||||||||||
| 68 | setWidget(). For example: | - | ||||||||||||||||||
| 69 | - | |||||||||||||||||||
| 70 | \snippet code/src_gui_widgets_qscrollarea.cpp 0 | - | ||||||||||||||||||
| 71 | - | |||||||||||||||||||
| 72 | The code above creates a scroll area (shown in the images below) | - | ||||||||||||||||||
| 73 | containing an image label. When scaling the image, the scroll area | - | ||||||||||||||||||
| 74 | can provide the necessary scroll bars: | - | ||||||||||||||||||
| 75 | - | |||||||||||||||||||
| 76 | \table | - | ||||||||||||||||||
| 77 | \row | - | ||||||||||||||||||
| 78 | \li \inlineimage qscrollarea-noscrollbars.png | - | ||||||||||||||||||
| 79 | \li \inlineimage qscrollarea-onescrollbar.png | - | ||||||||||||||||||
| 80 | \li \inlineimage qscrollarea-twoscrollbars.png | - | ||||||||||||||||||
| 81 | \endtable | - | ||||||||||||||||||
| 82 | - | |||||||||||||||||||
| 83 | The scroll bars appearance depends on the currently set \l | - | ||||||||||||||||||
| 84 | {Qt::ScrollBarPolicy}{scroll bar policies}. You can control the | - | ||||||||||||||||||
| 85 | appearance of the scroll bars using the inherited functionality | - | ||||||||||||||||||
| 86 | from QAbstractScrollArea. | - | ||||||||||||||||||
| 87 | - | |||||||||||||||||||
| 88 | For example, you can set the | - | ||||||||||||||||||
| 89 | QAbstractScrollArea::horizontalScrollBarPolicy and | - | ||||||||||||||||||
| 90 | QAbstractScrollArea::verticalScrollBarPolicy properties. Or if you | - | ||||||||||||||||||
| 91 | want the scroll bars to adjust dynamically when the contents of | - | ||||||||||||||||||
| 92 | the scroll area changes, you can use the \l | - | ||||||||||||||||||
| 93 | {QAbstractScrollArea::horizontalScrollBar()}{horizontalScrollBar()} | - | ||||||||||||||||||
| 94 | and \l | - | ||||||||||||||||||
| 95 | {QAbstractScrollArea::verticalScrollBar()}{verticalScrollBar()} | - | ||||||||||||||||||
| 96 | functions (which enable you to access the scroll bars) and set the | - | ||||||||||||||||||
| 97 | scroll bars' values whenever the scroll area's contents change, | - | ||||||||||||||||||
| 98 | using the QScrollBar::setValue() function. | - | ||||||||||||||||||
| 99 | - | |||||||||||||||||||
| 100 | You can retrieve the child widget using the widget() function. The | - | ||||||||||||||||||
| 101 | view can be made to be resizable with the setWidgetResizable() | - | ||||||||||||||||||
| 102 | function. The alignment of the widget can be specified with | - | ||||||||||||||||||
| 103 | setAlignment(). | - | ||||||||||||||||||
| 104 | - | |||||||||||||||||||
| 105 | Two convenience functions ensureVisible() and | - | ||||||||||||||||||
| 106 | ensureWidgetVisible() ensure a certain region of the contents is | - | ||||||||||||||||||
| 107 | visible inside the viewport, by scrolling the contents if | - | ||||||||||||||||||
| 108 | necessary. | - | ||||||||||||||||||
| 109 | - | |||||||||||||||||||
| 110 | \section1 Size Hints and Layouts | - | ||||||||||||||||||
| 111 | - | |||||||||||||||||||
| 112 | When using a scroll area to display the contents of a custom | - | ||||||||||||||||||
| 113 | widget, it is important to ensure that the | - | ||||||||||||||||||
| 114 | \l{QWidget::sizeHint}{size hint} of the child widget is set to a | - | ||||||||||||||||||
| 115 | suitable value. If a standard QWidget is used for the child | - | ||||||||||||||||||
| 116 | widget, it may be necessary to call QWidget::setMinimumSize() to | - | ||||||||||||||||||
| 117 | ensure that the contents of the widget are shown correctly within | - | ||||||||||||||||||
| 118 | the scroll area. | - | ||||||||||||||||||
| 119 | - | |||||||||||||||||||
| 120 | If a scroll area is used to display the contents of a widget that | - | ||||||||||||||||||
| 121 | contains child widgets arranged in a layout, it is important to | - | ||||||||||||||||||
| 122 | realize that the size policy of the layout will also determine the | - | ||||||||||||||||||
| 123 | size of the widget. This is especially useful to know if you intend | - | ||||||||||||||||||
| 124 | to dynamically change the contents of the layout. In such cases, | - | ||||||||||||||||||
| 125 | setting the layout's \l{QLayout::sizeConstraint}{size constraint} | - | ||||||||||||||||||
| 126 | property to one which provides constraints on the minimum and/or | - | ||||||||||||||||||
| 127 | maximum size of the layout (e.g., QLayout::SetMinAndMaxSize) will | - | ||||||||||||||||||
| 128 | cause the size of the scroll area to be updated whenever the | - | ||||||||||||||||||
| 129 | contents of the layout changes. | - | ||||||||||||||||||
| 130 | - | |||||||||||||||||||
| 131 | For a complete example using the QScrollArea class, see the \l | - | ||||||||||||||||||
| 132 | {widgets/imageviewer}{Image Viewer} example. The example shows how | - | ||||||||||||||||||
| 133 | to combine QLabel and QScrollArea to display an image. | - | ||||||||||||||||||
| 134 | - | |||||||||||||||||||
| 135 | \sa QAbstractScrollArea, QScrollBar, {Image Viewer Example} | - | ||||||||||||||||||
| 136 | */ | - | ||||||||||||||||||
| 137 | - | |||||||||||||||||||
| 138 | - | |||||||||||||||||||
| 139 | /*! | - | ||||||||||||||||||
| 140 | Constructs an empty scroll area with the given \a parent. | - | ||||||||||||||||||
| 141 | - | |||||||||||||||||||
| 142 | \sa setWidget() | - | ||||||||||||||||||
| 143 | */ | - | ||||||||||||||||||
| 144 | QScrollArea::QScrollArea(QWidget *parent) | - | ||||||||||||||||||
| 145 | : QAbstractScrollArea(*new QScrollAreaPrivate,parent) | - | ||||||||||||||||||
| 146 | { | - | ||||||||||||||||||
| 147 | Q_D(QScrollArea); | - | ||||||||||||||||||
| 148 | d->viewport->setBackgroundRole(QPalette::NoRole); | - | ||||||||||||||||||
| 149 | d->vbar->setSingleStep(20); | - | ||||||||||||||||||
| 150 | d->hbar->setSingleStep(20); | - | ||||||||||||||||||
| 151 | d->layoutChildren(); | - | ||||||||||||||||||
| 152 | } never executed: end of block | 0 | ||||||||||||||||||
| 153 | - | |||||||||||||||||||
| 154 | /*! | - | ||||||||||||||||||
| 155 | \internal | - | ||||||||||||||||||
| 156 | */ | - | ||||||||||||||||||
| 157 | QScrollArea::QScrollArea(QScrollAreaPrivate &dd, QWidget *parent) | - | ||||||||||||||||||
| 158 | : QAbstractScrollArea(dd, parent) | - | ||||||||||||||||||
| 159 | { | - | ||||||||||||||||||
| 160 | Q_D(QScrollArea); | - | ||||||||||||||||||
| 161 | d->viewport->setBackgroundRole(QPalette::NoRole); | - | ||||||||||||||||||
| 162 | d->vbar->setSingleStep(20); | - | ||||||||||||||||||
| 163 | d->hbar->setSingleStep(20); | - | ||||||||||||||||||
| 164 | d->layoutChildren(); | - | ||||||||||||||||||
| 165 | } never executed: end of block | 0 | ||||||||||||||||||
| 166 | - | |||||||||||||||||||
| 167 | /*! | - | ||||||||||||||||||
| 168 | Destroys the scroll area and its child widget. | - | ||||||||||||||||||
| 169 | - | |||||||||||||||||||
| 170 | \sa setWidget() | - | ||||||||||||||||||
| 171 | */ | - | ||||||||||||||||||
| 172 | QScrollArea::~QScrollArea() | - | ||||||||||||||||||
| 173 | { | - | ||||||||||||||||||
| 174 | } | - | ||||||||||||||||||
| 175 | - | |||||||||||||||||||
| 176 | void QScrollAreaPrivate::updateWidgetPosition() | - | ||||||||||||||||||
| 177 | { | - | ||||||||||||||||||
| 178 | Q_Q(QScrollArea); | - | ||||||||||||||||||
| 179 | Qt::LayoutDirection dir = q->layoutDirection(); | - | ||||||||||||||||||
| 180 | QRect scrolled = QStyle::visualRect(dir, viewport->rect(), QRect(QPoint(-hbar->value(), -vbar->value()), widget->size())); | - | ||||||||||||||||||
| 181 | QRect aligned = QStyle::alignedRect(dir, alignment, widget->size(), viewport->rect()); | - | ||||||||||||||||||
| 182 | widget->move(widget->width() < viewport->width() ? aligned.x() : scrolled.x(), | - | ||||||||||||||||||
| 183 | widget->height() < viewport->height() ? aligned.y() : scrolled.y()); | - | ||||||||||||||||||
| 184 | } never executed: end of block | 0 | ||||||||||||||||||
| 185 | - | |||||||||||||||||||
| 186 | void QScrollAreaPrivate::updateScrollBars() | - | ||||||||||||||||||
| 187 | { | - | ||||||||||||||||||
| 188 | Q_Q(QScrollArea); | - | ||||||||||||||||||
| 189 | if (!widget)
| 0 | ||||||||||||||||||
| 190 | return; never executed: return; | 0 | ||||||||||||||||||
| 191 | QSize p = viewport->size(); | - | ||||||||||||||||||
| 192 | QSize m = q->maximumViewportSize(); | - | ||||||||||||||||||
| 193 | - | |||||||||||||||||||
| 194 | QSize min = qSmartMinSize(widget); | - | ||||||||||||||||||
| 195 | QSize max = qSmartMaxSize(widget); | - | ||||||||||||||||||
| 196 | - | |||||||||||||||||||
| 197 | if (resizable) {
| 0 | ||||||||||||||||||
| 198 | if ((widget->layout() ? widget->layout()->hasHeightForWidth() : widget->sizePolicy().hasHeightForWidth())) {
| 0 | ||||||||||||||||||
| 199 | QSize p_hfw = p.expandedTo(min).boundedTo(max); | - | ||||||||||||||||||
| 200 | int h = widget->heightForWidth( p_hfw.width() ); | - | ||||||||||||||||||
| 201 | min = QSize(p_hfw.width(), qMax(p_hfw.height(), h)); | - | ||||||||||||||||||
| 202 | } never executed: end of block | 0 | ||||||||||||||||||
| 203 | } never executed: end of block | 0 | ||||||||||||||||||
| 204 | - | |||||||||||||||||||
| 205 | if ((resizable && m.expandedTo(min) == m && m.boundedTo(max) == m)
| 0 | ||||||||||||||||||
| 206 | || (!resizable && m.expandedTo(widget->size()) == m))
| 0 | ||||||||||||||||||
| 207 | p = m; // no scroll bars needed never executed: p = m; | 0 | ||||||||||||||||||
| 208 | - | |||||||||||||||||||
| 209 | if (resizable)
| 0 | ||||||||||||||||||
| 210 | widget->resize(p.expandedTo(min).boundedTo(max)); never executed: widget->resize(p.expandedTo(min).boundedTo(max)); | 0 | ||||||||||||||||||
| 211 | QSize v = widget->size(); | - | ||||||||||||||||||
| 212 | - | |||||||||||||||||||
| 213 | hbar->setRange(0, v.width() - p.width()); | - | ||||||||||||||||||
| 214 | hbar->setPageStep(p.width()); | - | ||||||||||||||||||
| 215 | vbar->setRange(0, v.height() - p.height()); | - | ||||||||||||||||||
| 216 | vbar->setPageStep(p.height()); | - | ||||||||||||||||||
| 217 | updateWidgetPosition(); | - | ||||||||||||||||||
| 218 | - | |||||||||||||||||||
| 219 | } never executed: end of block | 0 | ||||||||||||||||||
| 220 | - | |||||||||||||||||||
| 221 | /*! | - | ||||||||||||||||||
| 222 | Returns the scroll area's widget, or 0 if there is none. | - | ||||||||||||||||||
| 223 | - | |||||||||||||||||||
| 224 | \sa setWidget() | - | ||||||||||||||||||
| 225 | */ | - | ||||||||||||||||||
| 226 | - | |||||||||||||||||||
| 227 | QWidget *QScrollArea::widget() const | - | ||||||||||||||||||
| 228 | { | - | ||||||||||||||||||
| 229 | Q_D(const QScrollArea); | - | ||||||||||||||||||
| 230 | return d->widget; never executed: return d->widget; | 0 | ||||||||||||||||||
| 231 | } | - | ||||||||||||||||||
| 232 | - | |||||||||||||||||||
| 233 | /*! | - | ||||||||||||||||||
| 234 | \fn void QScrollArea::setWidget(QWidget *widget) | - | ||||||||||||||||||
| 235 | - | |||||||||||||||||||
| 236 | Sets the scroll area's \a widget. | - | ||||||||||||||||||
| 237 | - | |||||||||||||||||||
| 238 | The \a widget becomes a child of the scroll area, and will be | - | ||||||||||||||||||
| 239 | destroyed when the scroll area is deleted or when a new widget is | - | ||||||||||||||||||
| 240 | set. | - | ||||||||||||||||||
| 241 | - | |||||||||||||||||||
| 242 | The widget's \l{QWidget::setAutoFillBackground()}{autoFillBackground} | - | ||||||||||||||||||
| 243 | property will be set to \c{true}. | - | ||||||||||||||||||
| 244 | - | |||||||||||||||||||
| 245 | If the scroll area is visible when the \a widget is | - | ||||||||||||||||||
| 246 | added, you must \l{QWidget::}{show()} it explicitly. | - | ||||||||||||||||||
| 247 | - | |||||||||||||||||||
| 248 | Note that You must add the layout of \a widget before you call | - | ||||||||||||||||||
| 249 | this function; if you add it later, the \a widget will not be | - | ||||||||||||||||||
| 250 | visible - regardless of when you \l{QWidget::}{show()} the scroll | - | ||||||||||||||||||
| 251 | area. In this case, you can also not \l{QWidget::}{show()} the \a | - | ||||||||||||||||||
| 252 | widget later. | - | ||||||||||||||||||
| 253 | - | |||||||||||||||||||
| 254 | \sa widget() | - | ||||||||||||||||||
| 255 | */ | - | ||||||||||||||||||
| 256 | void QScrollArea::setWidget(QWidget *widget) | - | ||||||||||||||||||
| 257 | { | - | ||||||||||||||||||
| 258 | Q_D(QScrollArea); | - | ||||||||||||||||||
| 259 | if (widget == d->widget || !widget)
| 0 | ||||||||||||||||||
| 260 | return; never executed: return; | 0 | ||||||||||||||||||
| 261 | - | |||||||||||||||||||
| 262 | delete d->widget; | - | ||||||||||||||||||
| 263 | d->widget = 0; | - | ||||||||||||||||||
| 264 | d->hbar->setValue(0); | - | ||||||||||||||||||
| 265 | d->vbar->setValue(0); | - | ||||||||||||||||||
| 266 | if (widget->parentWidget() != d->viewport)
| 0 | ||||||||||||||||||
| 267 | widget->setParent(d->viewport); never executed: widget->setParent(d->viewport); | 0 | ||||||||||||||||||
| 268 | if (!widget->testAttribute(Qt::WA_Resized))
| 0 | ||||||||||||||||||
| 269 | widget->resize(widget->sizeHint()); never executed: widget->resize(widget->sizeHint()); | 0 | ||||||||||||||||||
| 270 | d->widget = widget; | - | ||||||||||||||||||
| 271 | d->widget->setAutoFillBackground(true); | - | ||||||||||||||||||
| 272 | widget->installEventFilter(this); | - | ||||||||||||||||||
| 273 | d->widgetSize = QSize(); | - | ||||||||||||||||||
| 274 | d->updateScrollBars(); | - | ||||||||||||||||||
| 275 | d->widget->show(); | - | ||||||||||||||||||
| 276 | - | |||||||||||||||||||
| 277 | } never executed: end of block | 0 | ||||||||||||||||||
| 278 | - | |||||||||||||||||||
| 279 | /*! | - | ||||||||||||||||||
| 280 | Removes the scroll area's widget, and passes ownership of the | - | ||||||||||||||||||
| 281 | widget to the caller. | - | ||||||||||||||||||
| 282 | - | |||||||||||||||||||
| 283 | \sa widget() | - | ||||||||||||||||||
| 284 | */ | - | ||||||||||||||||||
| 285 | QWidget *QScrollArea::takeWidget() | - | ||||||||||||||||||
| 286 | { | - | ||||||||||||||||||
| 287 | Q_D(QScrollArea); | - | ||||||||||||||||||
| 288 | QWidget *w = d->widget; | - | ||||||||||||||||||
| 289 | d->widget = 0; | - | ||||||||||||||||||
| 290 | if (w)
| 0 | ||||||||||||||||||
| 291 | w->setParent(0); never executed: w->setParent(0); | 0 | ||||||||||||||||||
| 292 | return w; never executed: return w; | 0 | ||||||||||||||||||
| 293 | } | - | ||||||||||||||||||
| 294 | - | |||||||||||||||||||
| 295 | /*! | - | ||||||||||||||||||
| 296 | \reimp | - | ||||||||||||||||||
| 297 | */ | - | ||||||||||||||||||
| 298 | bool QScrollArea::event(QEvent *e) | - | ||||||||||||||||||
| 299 | { | - | ||||||||||||||||||
| 300 | Q_D(QScrollArea); | - | ||||||||||||||||||
| 301 | if (e->type() == QEvent::StyleChange || e->type() == QEvent::LayoutRequest) {
| 0 | ||||||||||||||||||
| 302 | d->updateScrollBars(); | - | ||||||||||||||||||
| 303 | } never executed: end of block | 0 | ||||||||||||||||||
| 304 | #ifdef QT_KEYPAD_NAVIGATION | - | ||||||||||||||||||
| 305 | else if (QApplication::keypadNavigationEnabled()) { | - | ||||||||||||||||||
| 306 | if (e->type() == QEvent::Show) | - | ||||||||||||||||||
| 307 | QApplication::instance()->installEventFilter(this); | - | ||||||||||||||||||
| 308 | else if (e->type() == QEvent::Hide) | - | ||||||||||||||||||
| 309 | QApplication::instance()->removeEventFilter(this); | - | ||||||||||||||||||
| 310 | } | - | ||||||||||||||||||
| 311 | #endif | - | ||||||||||||||||||
| 312 | return QAbstractScrollArea::event(e); never executed: return QAbstractScrollArea::event(e); | 0 | ||||||||||||||||||
| 313 | } | - | ||||||||||||||||||
| 314 | - | |||||||||||||||||||
| 315 | - | |||||||||||||||||||
| 316 | /*! | - | ||||||||||||||||||
| 317 | \reimp | - | ||||||||||||||||||
| 318 | */ | - | ||||||||||||||||||
| 319 | bool QScrollArea::eventFilter(QObject *o, QEvent *e) | - | ||||||||||||||||||
| 320 | { | - | ||||||||||||||||||
| 321 | Q_D(QScrollArea); | - | ||||||||||||||||||
| 322 | #ifdef QT_KEYPAD_NAVIGATION | - | ||||||||||||||||||
| 323 | if (d->widget && o != d->widget && e->type() == QEvent::FocusIn | - | ||||||||||||||||||
| 324 | && QApplication::keypadNavigationEnabled()) { | - | ||||||||||||||||||
| 325 | if (o->isWidgetType()) | - | ||||||||||||||||||
| 326 | ensureWidgetVisible(static_cast<QWidget *>(o)); | - | ||||||||||||||||||
| 327 | } | - | ||||||||||||||||||
| 328 | #endif | - | ||||||||||||||||||
| 329 | if (o == d->widget && e->type() == QEvent::Resize)
| 0 | ||||||||||||||||||
| 330 | d->updateScrollBars(); never executed: d->updateScrollBars(); | 0 | ||||||||||||||||||
| 331 | - | |||||||||||||||||||
| 332 | return QAbstractScrollArea::eventFilter(o, e); never executed: return QAbstractScrollArea::eventFilter(o, e); | 0 | ||||||||||||||||||
| 333 | } | - | ||||||||||||||||||
| 334 | - | |||||||||||||||||||
| 335 | /*! | - | ||||||||||||||||||
| 336 | \reimp | - | ||||||||||||||||||
| 337 | */ | - | ||||||||||||||||||
| 338 | void QScrollArea::resizeEvent(QResizeEvent *) | - | ||||||||||||||||||
| 339 | { | - | ||||||||||||||||||
| 340 | Q_D(QScrollArea); | - | ||||||||||||||||||
| 341 | d->updateScrollBars(); | - | ||||||||||||||||||
| 342 | - | |||||||||||||||||||
| 343 | } never executed: end of block | 0 | ||||||||||||||||||
| 344 | - | |||||||||||||||||||
| 345 | - | |||||||||||||||||||
| 346 | /*!\reimp | - | ||||||||||||||||||
| 347 | */ | - | ||||||||||||||||||
| 348 | void QScrollArea::scrollContentsBy(int, int) | - | ||||||||||||||||||
| 349 | { | - | ||||||||||||||||||
| 350 | Q_D(QScrollArea); | - | ||||||||||||||||||
| 351 | if (!d->widget)
| 0 | ||||||||||||||||||
| 352 | return; never executed: return; | 0 | ||||||||||||||||||
| 353 | d->updateWidgetPosition(); | - | ||||||||||||||||||
| 354 | } never executed: end of block | 0 | ||||||||||||||||||
| 355 | - | |||||||||||||||||||
| 356 | - | |||||||||||||||||||
| 357 | /*! | - | ||||||||||||||||||
| 358 | \property QScrollArea::widgetResizable | - | ||||||||||||||||||
| 359 | \brief whether the scroll area should resize the view widget | - | ||||||||||||||||||
| 360 | - | |||||||||||||||||||
| 361 | If this property is set to false (the default), the scroll area | - | ||||||||||||||||||
| 362 | honors the size of its widget. Regardless of this property, you | - | ||||||||||||||||||
| 363 | can programmatically resize the widget using widget()->resize(), | - | ||||||||||||||||||
| 364 | and the scroll area will automatically adjust itself to the new | - | ||||||||||||||||||
| 365 | size. | - | ||||||||||||||||||
| 366 | - | |||||||||||||||||||
| 367 | If this property is set to true, the scroll area will | - | ||||||||||||||||||
| 368 | automatically resize the widget in order to avoid scroll bars | - | ||||||||||||||||||
| 369 | where they can be avoided, or to take advantage of extra space. | - | ||||||||||||||||||
| 370 | */ | - | ||||||||||||||||||
| 371 | bool QScrollArea::widgetResizable() const | - | ||||||||||||||||||
| 372 | { | - | ||||||||||||||||||
| 373 | Q_D(const QScrollArea); | - | ||||||||||||||||||
| 374 | return d->resizable; never executed: return d->resizable; | 0 | ||||||||||||||||||
| 375 | } | - | ||||||||||||||||||
| 376 | - | |||||||||||||||||||
| 377 | void QScrollArea::setWidgetResizable(bool resizable) | - | ||||||||||||||||||
| 378 | { | - | ||||||||||||||||||
| 379 | Q_D(QScrollArea); | - | ||||||||||||||||||
| 380 | d->resizable = resizable; | - | ||||||||||||||||||
| 381 | updateGeometry(); | - | ||||||||||||||||||
| 382 | d->updateScrollBars(); | - | ||||||||||||||||||
| 383 | } never executed: end of block | 0 | ||||||||||||||||||
| 384 | - | |||||||||||||||||||
| 385 | /*! | - | ||||||||||||||||||
| 386 | \reimp | - | ||||||||||||||||||
| 387 | */ | - | ||||||||||||||||||
| 388 | QSize QScrollArea::sizeHint() const | - | ||||||||||||||||||
| 389 | { | - | ||||||||||||||||||
| 390 | Q_D(const QScrollArea); | - | ||||||||||||||||||
| 391 | int f = 2 * d->frameWidth; | - | ||||||||||||||||||
| 392 | QSize sz(f, f); | - | ||||||||||||||||||
| 393 | int h = fontMetrics().height(); | - | ||||||||||||||||||
| 394 | if (d->widget) {
| 0 | ||||||||||||||||||
| 395 | if (!d->widgetSize.isValid())
| 0 | ||||||||||||||||||
| 396 | d->widgetSize = d->resizable ? d->widget->sizeHint() : d->widget->size(); never executed: d->widgetSize = d->resizable ? d->widget->sizeHint() : d->widget->size();
| 0 | ||||||||||||||||||
| 397 | sz += d->widgetSize; | - | ||||||||||||||||||
| 398 | } else { never executed: end of block | 0 | ||||||||||||||||||
| 399 | sz += QSize(12 * h, 8 * h); | - | ||||||||||||||||||
| 400 | } never executed: end of block | 0 | ||||||||||||||||||
| 401 | if (d->vbarpolicy == Qt::ScrollBarAlwaysOn)
| 0 | ||||||||||||||||||
| 402 | sz.setWidth(sz.width() + d->vbar->sizeHint().width()); never executed: sz.setWidth(sz.width() + d->vbar->sizeHint().width()); | 0 | ||||||||||||||||||
| 403 | if (d->hbarpolicy == Qt::ScrollBarAlwaysOn)
| 0 | ||||||||||||||||||
| 404 | sz.setHeight(sz.height() + d->hbar->sizeHint().height()); never executed: sz.setHeight(sz.height() + d->hbar->sizeHint().height()); | 0 | ||||||||||||||||||
| 405 | return sz.boundedTo(QSize(36 * h, 24 * h)); never executed: return sz.boundedTo(QSize(36 * h, 24 * h)); | 0 | ||||||||||||||||||
| 406 | } | - | ||||||||||||||||||
| 407 | - | |||||||||||||||||||
| 408 | /*! | - | ||||||||||||||||||
| 409 | \reimp | - | ||||||||||||||||||
| 410 | */ | - | ||||||||||||||||||
| 411 | QSize QScrollArea::viewportSizeHint() const | - | ||||||||||||||||||
| 412 | { | - | ||||||||||||||||||
| 413 | Q_D(const QScrollArea); | - | ||||||||||||||||||
| 414 | if (d->widget) {
| 0 | ||||||||||||||||||
| 415 | return d->resizable ? d->widget->sizeHint() : d->widget->size(); never executed: return d->resizable ? d->widget->sizeHint() : d->widget->size(); | 0 | ||||||||||||||||||
| 416 | } | - | ||||||||||||||||||
| 417 | const int h = fontMetrics().height(); | - | ||||||||||||||||||
| 418 | return QSize(6 * h, 4 * h); never executed: return QSize(6 * h, 4 * h); | 0 | ||||||||||||||||||
| 419 | } | - | ||||||||||||||||||
| 420 | - | |||||||||||||||||||
| 421 | - | |||||||||||||||||||
| 422 | /*! | - | ||||||||||||||||||
| 423 | \reimp | - | ||||||||||||||||||
| 424 | */ | - | ||||||||||||||||||
| 425 | bool QScrollArea::focusNextPrevChild(bool next) | - | ||||||||||||||||||
| 426 | { | - | ||||||||||||||||||
| 427 | if (QWidget::focusNextPrevChild(next)) {
| 0 | ||||||||||||||||||
| 428 | if (QWidget *fw = focusWidget())
| 0 | ||||||||||||||||||
| 429 | ensureWidgetVisible(fw); never executed: ensureWidgetVisible(fw); | 0 | ||||||||||||||||||
| 430 | return true; never executed: return true; | 0 | ||||||||||||||||||
| 431 | } | - | ||||||||||||||||||
| 432 | return false; never executed: return false; | 0 | ||||||||||||||||||
| 433 | } | - | ||||||||||||||||||
| 434 | - | |||||||||||||||||||
| 435 | /*! | - | ||||||||||||||||||
| 436 | Scrolls the contents of the scroll area so that the point (\a x, \a y) is visible | - | ||||||||||||||||||
| 437 | inside the region of the viewport with margins specified in pixels by \a xmargin and | - | ||||||||||||||||||
| 438 | \a ymargin. If the specified point cannot be reached, the contents are scrolled to | - | ||||||||||||||||||
| 439 | the nearest valid position. The default value for both margins is 50 pixels. | - | ||||||||||||||||||
| 440 | */ | - | ||||||||||||||||||
| 441 | void QScrollArea::ensureVisible(int x, int y, int xmargin, int ymargin) | - | ||||||||||||||||||
| 442 | { | - | ||||||||||||||||||
| 443 | Q_D(QScrollArea); | - | ||||||||||||||||||
| 444 | - | |||||||||||||||||||
| 445 | int logicalX = QStyle::visualPos(layoutDirection(), d->viewport->rect(), QPoint(x, y)).x(); | - | ||||||||||||||||||
| 446 | - | |||||||||||||||||||
| 447 | if (logicalX - xmargin < d->hbar->value()) {
| 0 | ||||||||||||||||||
| 448 | d->hbar->setValue(qMax(0, logicalX - xmargin)); | - | ||||||||||||||||||
| 449 | } else if (logicalX > d->hbar->value() + d->viewport->width() - xmargin) { never executed: end of block
| 0 | ||||||||||||||||||
| 450 | d->hbar->setValue(qMin(logicalX - d->viewport->width() + xmargin, d->hbar->maximum())); | - | ||||||||||||||||||
| 451 | } never executed: end of block | 0 | ||||||||||||||||||
| 452 | - | |||||||||||||||||||
| 453 | if (y - ymargin < d->vbar->value()) {
| 0 | ||||||||||||||||||
| 454 | d->vbar->setValue(qMax(0, y - ymargin)); | - | ||||||||||||||||||
| 455 | } else if (y > d->vbar->value() + d->viewport->height() - ymargin) { never executed: end of block
| 0 | ||||||||||||||||||
| 456 | d->vbar->setValue(qMin(y - d->viewport->height() + ymargin, d->vbar->maximum())); | - | ||||||||||||||||||
| 457 | } never executed: end of block | 0 | ||||||||||||||||||
| 458 | } never executed: end of block | 0 | ||||||||||||||||||
| 459 | - | |||||||||||||||||||
| 460 | /*! | - | ||||||||||||||||||
| 461 | \since 4.2 | - | ||||||||||||||||||
| 462 | - | |||||||||||||||||||
| 463 | Scrolls the contents of the scroll area so that the \a childWidget | - | ||||||||||||||||||
| 464 | of QScrollArea::widget() is visible inside the viewport with | - | ||||||||||||||||||
| 465 | margins specified in pixels by \a xmargin and \a ymargin. If the | - | ||||||||||||||||||
| 466 | specified point cannot be reached, the contents are scrolled to | - | ||||||||||||||||||
| 467 | the nearest valid position. The default value for both margins is | - | ||||||||||||||||||
| 468 | 50 pixels. | - | ||||||||||||||||||
| 469 | - | |||||||||||||||||||
| 470 | */ | - | ||||||||||||||||||
| 471 | void QScrollArea::ensureWidgetVisible(QWidget *childWidget, int xmargin, int ymargin) | - | ||||||||||||||||||
| 472 | { | - | ||||||||||||||||||
| 473 | Q_D(QScrollArea); | - | ||||||||||||||||||
| 474 | - | |||||||||||||||||||
| 475 | if (!d->widget->isAncestorOf(childWidget))
| 0 | ||||||||||||||||||
| 476 | return; never executed: return; | 0 | ||||||||||||||||||
| 477 | - | |||||||||||||||||||
| 478 | const QRect microFocus = childWidget->inputMethodQuery(Qt::ImCursorRectangle).toRect(); | - | ||||||||||||||||||
| 479 | const QRect defaultMicroFocus = | - | ||||||||||||||||||
| 480 | childWidget->QWidget::inputMethodQuery(Qt::ImCursorRectangle).toRect(); | - | ||||||||||||||||||
| 481 | QRect focusRect = (microFocus != defaultMicroFocus)
| 0 | ||||||||||||||||||
| 482 | ? QRect(childWidget->mapTo(d->widget, microFocus.topLeft()), microFocus.size()) | - | ||||||||||||||||||
| 483 | : QRect(childWidget->mapTo(d->widget, QPoint(0,0)), childWidget->size()); | - | ||||||||||||||||||
| 484 | const QRect visibleRect(-d->widget->pos(), d->viewport->size()); | - | ||||||||||||||||||
| 485 | - | |||||||||||||||||||
| 486 | if (visibleRect.contains(focusRect))
| 0 | ||||||||||||||||||
| 487 | return; never executed: return; | 0 | ||||||||||||||||||
| 488 | - | |||||||||||||||||||
| 489 | focusRect.adjust(-xmargin, -ymargin, xmargin, ymargin); | - | ||||||||||||||||||
| 490 | - | |||||||||||||||||||
| 491 | if (focusRect.width() > visibleRect.width())
| 0 | ||||||||||||||||||
| 492 | d->hbar->setValue(focusRect.center().x() - d->viewport->width() / 2); never executed: d->hbar->setValue(focusRect.center().x() - d->viewport->width() / 2); | 0 | ||||||||||||||||||
| 493 | else if (focusRect.right() > visibleRect.right())
| 0 | ||||||||||||||||||
| 494 | d->hbar->setValue(focusRect.right() - d->viewport->width()); never executed: d->hbar->setValue(focusRect.right() - d->viewport->width()); | 0 | ||||||||||||||||||
| 495 | else if (focusRect.left() < visibleRect.left())
| 0 | ||||||||||||||||||
| 496 | d->hbar->setValue(focusRect.left()); never executed: d->hbar->setValue(focusRect.left()); | 0 | ||||||||||||||||||
| 497 | - | |||||||||||||||||||
| 498 | if (focusRect.height() > visibleRect.height())
| 0 | ||||||||||||||||||
| 499 | d->vbar->setValue(focusRect.center().y() - d->viewport->height() / 2); never executed: d->vbar->setValue(focusRect.center().y() - d->viewport->height() / 2); | 0 | ||||||||||||||||||
| 500 | else if (focusRect.bottom() > visibleRect.bottom())
| 0 | ||||||||||||||||||
| 501 | d->vbar->setValue(focusRect.bottom() - d->viewport->height()); never executed: d->vbar->setValue(focusRect.bottom() - d->viewport->height()); | 0 | ||||||||||||||||||
| 502 | else if (focusRect.top() < visibleRect.top())
| 0 | ||||||||||||||||||
| 503 | d->vbar->setValue(focusRect.top()); never executed: d->vbar->setValue(focusRect.top()); | 0 | ||||||||||||||||||
| 504 | } never executed: end of block | 0 | ||||||||||||||||||
| 505 | - | |||||||||||||||||||
| 506 | - | |||||||||||||||||||
| 507 | /*! | - | ||||||||||||||||||
| 508 | \property QScrollArea::alignment | - | ||||||||||||||||||
| 509 | \brief the alignment of the scroll area's widget | - | ||||||||||||||||||
| 510 | \since 4.2 | - | ||||||||||||||||||
| 511 | - | |||||||||||||||||||
| 512 | A valid alignment is a combination of the following flags: | - | ||||||||||||||||||
| 513 | \list | - | ||||||||||||||||||
| 514 | \li \c Qt::AlignLeft | - | ||||||||||||||||||
| 515 | \li \c Qt::AlignHCenter | - | ||||||||||||||||||
| 516 | \li \c Qt::AlignRight | - | ||||||||||||||||||
| 517 | \li \c Qt::AlignTop | - | ||||||||||||||||||
| 518 | \li \c Qt::AlignVCenter | - | ||||||||||||||||||
| 519 | \li \c Qt::AlignBottom | - | ||||||||||||||||||
| 520 | \endlist | - | ||||||||||||||||||
| 521 | By default, the widget stays rooted to the top-left corner of the | - | ||||||||||||||||||
| 522 | scroll area. | - | ||||||||||||||||||
| 523 | */ | - | ||||||||||||||||||
| 524 | - | |||||||||||||||||||
| 525 | void QScrollArea::setAlignment(Qt::Alignment alignment) | - | ||||||||||||||||||
| 526 | { | - | ||||||||||||||||||
| 527 | Q_D(QScrollArea); | - | ||||||||||||||||||
| 528 | d->alignment = alignment; | - | ||||||||||||||||||
| 529 | if (d->widget)
| 0 | ||||||||||||||||||
| 530 | d->updateWidgetPosition(); never executed: d->updateWidgetPosition(); | 0 | ||||||||||||||||||
| 531 | } never executed: end of block | 0 | ||||||||||||||||||
| 532 | - | |||||||||||||||||||
| 533 | Qt::Alignment QScrollArea::alignment() const | - | ||||||||||||||||||
| 534 | { | - | ||||||||||||||||||
| 535 | Q_D(const QScrollArea); | - | ||||||||||||||||||
| 536 | return d->alignment; never executed: return d->alignment; | 0 | ||||||||||||||||||
| 537 | } | - | ||||||||||||||||||
| 538 | - | |||||||||||||||||||
| 539 | QT_END_NAMESPACE | - | ||||||||||||||||||
| 540 | - | |||||||||||||||||||
| 541 | #include "moc_qscrollarea.cpp" | - | ||||||||||||||||||
| 542 | - | |||||||||||||||||||
| 543 | #endif // QT_NO_SCROLLAREA | - | ||||||||||||||||||
| Source code | Switch to Preprocessed file |