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