| Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/widgets/util/qscroller.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 "qevent.h" | - | ||||||||||||
| 41 | #include "qwidget.h" | - | ||||||||||||
| 42 | #include "qscroller.h" | - | ||||||||||||
| 43 | #include "private/qflickgesture_p.h" | - | ||||||||||||
| 44 | #include "private/qscroller_p.h" | - | ||||||||||||
| 45 | #include "qscrollerproperties.h" | - | ||||||||||||
| 46 | #include "private/qscrollerproperties_p.h" | - | ||||||||||||
| 47 | #include "qnumeric.h" | - | ||||||||||||
| 48 | #include "math.h" | - | ||||||||||||
| 49 | - | |||||||||||||
| 50 | #include <QTime> | - | ||||||||||||
| 51 | #include <QElapsedTimer> | - | ||||||||||||
| 52 | #include <QMap> | - | ||||||||||||
| 53 | #include <QApplication> | - | ||||||||||||
| 54 | #include <QAbstractScrollArea> | - | ||||||||||||
| 55 | #include <QGraphicsObject> | - | ||||||||||||
| 56 | #include <QGraphicsScene> | - | ||||||||||||
| 57 | #include <QGraphicsView> | - | ||||||||||||
| 58 | #include <QDesktopWidget> | - | ||||||||||||
| 59 | #include <QVector2D> | - | ||||||||||||
| 60 | #include <QtCore/qmath.h> | - | ||||||||||||
| 61 | #include <QtGui/qevent.h> | - | ||||||||||||
| 62 | #include <qnumeric.h> | - | ||||||||||||
| 63 | - | |||||||||||||
| 64 | #include <QtDebug> | - | ||||||||||||
| 65 | - | |||||||||||||
| 66 | - | |||||||||||||
| 67 | - | |||||||||||||
| 68 | QT_BEGIN_NAMESPACE | - | ||||||||||||
| 69 | - | |||||||||||||
| 70 | bool qt_sendSpontaneousEvent(QObject *receiver, QEvent *event); | - | ||||||||||||
| 71 | - | |||||||||||||
| 72 | //#define QSCROLLER_DEBUG | - | ||||||||||||
| 73 | - | |||||||||||||
| 74 | #ifdef QSCROLLER_DEBUG | - | ||||||||||||
| 75 | # define qScrollerDebug qDebug | - | ||||||||||||
| 76 | #else | - | ||||||||||||
| 77 | # define qScrollerDebug while (false) qDebug | - | ||||||||||||
| 78 | #endif | - | ||||||||||||
| 79 | - | |||||||||||||
| 80 | QDebug &operator<<(QDebug &dbg, const QScrollerPrivate::ScrollSegment &s) | - | ||||||||||||
| 81 | { | - | ||||||||||||
| 82 | dbg << "\n Time: start:" << s.startTime << " duration:" << s.deltaTime << " stop progress:" << s.stopProgress; | - | ||||||||||||
| 83 | dbg << "\n Pos: start:" << s.startPos << " delta:" << s.deltaPos << " stop:" << s.stopPos; | - | ||||||||||||
| 84 | dbg << "\n Curve: type:" << s.curve.type() << "\n"; | - | ||||||||||||
| 85 | return dbg; | - | ||||||||||||
| 86 | } | - | ||||||||||||
| 87 | - | |||||||||||||
| 88 | - | |||||||||||||
| 89 | // a few helper operators to make the code below a lot more readable: | - | ||||||||||||
| 90 | // otherwise a lot of ifs would have to be multi-line to check both the x | - | ||||||||||||
| 91 | // and y coordinate separately. | - | ||||||||||||
| 92 | - | |||||||||||||
| 93 | // returns true only if the abs. value of BOTH x and y are <= f | - | ||||||||||||
| 94 | inline bool operator<=(const QPointF &p, qreal f) | - | ||||||||||||
| 95 | { | - | ||||||||||||
| 96 | return (qAbs(p.x()) <= f) && (qAbs(p.y()) <= f); | - | ||||||||||||
| 97 | } | - | ||||||||||||
| 98 | - | |||||||||||||
| 99 | // returns true only if the abs. value of BOTH x and y are < f | - | ||||||||||||
| 100 | inline bool operator<(const QPointF &p, qreal f) | - | ||||||||||||
| 101 | { | - | ||||||||||||
| 102 | return (qAbs(p.x()) < f) && (qAbs(p.y()) < f); | - | ||||||||||||
| 103 | } | - | ||||||||||||
| 104 | - | |||||||||||||
| 105 | // returns true if the abs. value of EITHER x or y are >= f | - | ||||||||||||
| 106 | inline bool operator>=(const QPointF &p, qreal f) | - | ||||||||||||
| 107 | { | - | ||||||||||||
| 108 | return (qAbs(p.x()) >= f) || (qAbs(p.y()) >= f); | - | ||||||||||||
| 109 | } | - | ||||||||||||
| 110 | - | |||||||||||||
| 111 | // returns true if the abs. value of EITHER x or y are > f | - | ||||||||||||
| 112 | inline bool operator>(const QPointF &p, qreal f) | - | ||||||||||||
| 113 | { | - | ||||||||||||
| 114 | return (qAbs(p.x()) > f) || (qAbs(p.y()) > f); | - | ||||||||||||
| 115 | } | - | ||||||||||||
| 116 | - | |||||||||||||
| 117 | // returns a new point with both coordinates having the abs. value of the original one | - | ||||||||||||
| 118 | inline QPointF qAbs(const QPointF &p) | - | ||||||||||||
| 119 | { | - | ||||||||||||
| 120 | return QPointF(qAbs(p.x()), qAbs(p.y())); | - | ||||||||||||
| 121 | } | - | ||||||||||||
| 122 | - | |||||||||||||
| 123 | // returns a new point with all components of p1 multiplied by the corresponding components of p2 | - | ||||||||||||
| 124 | inline QPointF operator*(const QPointF &p1, const QPointF &p2) | - | ||||||||||||
| 125 | { | - | ||||||||||||
| 126 | return QPointF(p1.x() * p2.x(), p1.y() * p2.y()); | - | ||||||||||||
| 127 | } | - | ||||||||||||
| 128 | - | |||||||||||||
| 129 | // returns a new point with all components of p1 divided by the corresponding components of p2 | - | ||||||||||||
| 130 | inline QPointF operator/(const QPointF &p1, const QPointF &p2) | - | ||||||||||||
| 131 | { | - | ||||||||||||
| 132 | return QPointF(p1.x() / p2.x(), p1.y() / p2.y()); | - | ||||||||||||
| 133 | } | - | ||||||||||||
| 134 | - | |||||||||||||
| 135 | inline QPointF clampToRect(const QPointF &p, const QRectF &rect) | - | ||||||||||||
| 136 | { | - | ||||||||||||
| 137 | qreal x = qBound(rect.left(), p.x(), rect.right()); | - | ||||||||||||
| 138 | qreal y = qBound(rect.top(), p.y(), rect.bottom()); | - | ||||||||||||
| 139 | return QPointF(x, y); | - | ||||||||||||
| 140 | } | - | ||||||||||||
| 141 | - | |||||||||||||
| 142 | // returns -1, 0 or +1 according to r being <0, ==0 or >0 | - | ||||||||||||
| 143 | inline int qSign(qreal r) | - | ||||||||||||
| 144 | { | - | ||||||||||||
| 145 | return (r < 0) ? -1 : ((r > 0) ? 1 : 0); | - | ||||||||||||
| 146 | } | - | ||||||||||||
| 147 | - | |||||||||||||
| 148 | // this version is not mathematically exact, but it just works for every | - | ||||||||||||
| 149 | // easing curve type (even custom ones) | - | ||||||||||||
| 150 | - | |||||||||||||
| 151 | static qreal differentialForProgress(const QEasingCurve &curve, qreal pos) | - | ||||||||||||
| 152 | { | - | ||||||||||||
| 153 | const qreal dx = 0.01; | - | ||||||||||||
| 154 | qreal left = (pos < qreal(0.5)) ? pos : pos - qreal(dx); | - | ||||||||||||
| 155 | qreal right = (pos >= qreal(0.5)) ? pos : pos + qreal(dx); | - | ||||||||||||
| 156 | qreal d = (curve.valueForProgress(right) - curve.valueForProgress(left)) / qreal(dx); | - | ||||||||||||
| 157 | - | |||||||||||||
| 158 | //qScrollerDebug() << "differentialForProgress(type: " << curve.type() << ", pos: " << pos << ") = " << d; | - | ||||||||||||
| 159 | - | |||||||||||||
| 160 | return d; | - | ||||||||||||
| 161 | } | - | ||||||||||||
| 162 | - | |||||||||||||
| 163 | // this version is not mathematically exact, but it just works for every | - | ||||||||||||
| 164 | // easing curve type (even custom ones) | - | ||||||||||||
| 165 | - | |||||||||||||
| 166 | static qreal progressForValue(const QEasingCurve &curve, qreal value) | - | ||||||||||||
| 167 | { | - | ||||||||||||
| 168 | if (Q_UNLIKELY(curve.type() >= QEasingCurve::InElastic &&
| 0 | ||||||||||||
| 169 | curve.type() < QEasingCurve::Custom))) { | - | ||||||||||||
| 170 | qWarning("progressForValue(): QEasingCurves of type %d do not have an inverse, since they are not injective.", curve.type()); | - | ||||||||||||
| 171 | return value; never executed: return value; | 0 | ||||||||||||
| 172 | } | - | ||||||||||||
| 173 | if (value < qreal(0) || value > qreal(1))
| 0 | ||||||||||||
| 174 | return value; never executed: return value; | 0 | ||||||||||||
| 175 | - | |||||||||||||
| 176 | qreal progress = value, left(0), right(1); | - | ||||||||||||
| 177 | for (int iterations = 6; iterations; --iterations) {
| 0 | ||||||||||||
| 178 | qreal v = curve.valueForProgress(progress); | - | ||||||||||||
| 179 | if (v < value)
| 0 | ||||||||||||
| 180 | left = progress; never executed: left = progress; | 0 | ||||||||||||
| 181 | else if (v > value)
| 0 | ||||||||||||
| 182 | right = progress; never executed: right = progress; | 0 | ||||||||||||
| 183 | else | - | ||||||||||||
| 184 | break; never executed: break; | 0 | ||||||||||||
| 185 | progress = (left + right) / qreal(2); | - | ||||||||||||
| 186 | } never executed: end of block | 0 | ||||||||||||
| 187 | return progress; never executed: return progress; | 0 | ||||||||||||
| 188 | } | - | ||||||||||||
| 189 | - | |||||||||||||
| 190 | - | |||||||||||||
| 191 | #ifndef QT_NO_ANIMATION | - | ||||||||||||
| 192 | class QScrollTimer : public QAbstractAnimation | - | ||||||||||||
| 193 | { | - | ||||||||||||
| 194 | public: | - | ||||||||||||
| 195 | QScrollTimer(QScrollerPrivate *_d) | - | ||||||||||||
| 196 | : QAbstractAnimation(_d), d(_d), ignoreUpdate(false), skip(0) | - | ||||||||||||
| 197 | { } | - | ||||||||||||
| 198 | - | |||||||||||||
| 199 | int duration() const Q_DECL_OVERRIDE | - | ||||||||||||
| 200 | { | - | ||||||||||||
| 201 | return -1; | - | ||||||||||||
| 202 | } | - | ||||||||||||
| 203 | - | |||||||||||||
| 204 | void start() | - | ||||||||||||
| 205 | { | - | ||||||||||||
| 206 | // QAbstractAnimation::start() will immediately call | - | ||||||||||||
| 207 | // updateCurrentTime(), but our state is not set correctly yet | - | ||||||||||||
| 208 | ignoreUpdate = true; | - | ||||||||||||
| 209 | QAbstractAnimation::start(); | - | ||||||||||||
| 210 | ignoreUpdate = false; | - | ||||||||||||
| 211 | skip = 0; | - | ||||||||||||
| 212 | } | - | ||||||||||||
| 213 | - | |||||||||||||
| 214 | protected: | - | ||||||||||||
| 215 | void updateCurrentTime(int /*currentTime*/) Q_DECL_OVERRIDE | - | ||||||||||||
| 216 | { | - | ||||||||||||
| 217 | if (!ignoreUpdate) { | - | ||||||||||||
| 218 | if (++skip >= d->frameRateSkip()) { | - | ||||||||||||
| 219 | skip = 0; | - | ||||||||||||
| 220 | d->timerTick(); | - | ||||||||||||
| 221 | } | - | ||||||||||||
| 222 | } | - | ||||||||||||
| 223 | } | - | ||||||||||||
| 224 | - | |||||||||||||
| 225 | private: | - | ||||||||||||
| 226 | QScrollerPrivate *d; | - | ||||||||||||
| 227 | bool ignoreUpdate; | - | ||||||||||||
| 228 | int skip; | - | ||||||||||||
| 229 | }; | - | ||||||||||||
| 230 | #endif // QT_NO_ANIMATION | - | ||||||||||||
| 231 | - | |||||||||||||
| 232 | /*! | - | ||||||||||||
| 233 | \class QScroller | - | ||||||||||||
| 234 | \brief The QScroller class enables kinetic scrolling for any scrolling widget or graphics item. | - | ||||||||||||
| 235 | \since 5.0 | - | ||||||||||||
| 236 | - | |||||||||||||
| 237 | \inmodule QtWidgets | - | ||||||||||||
| 238 | - | |||||||||||||
| 239 | With kinetic scrolling, the user can push the widget in a given | - | ||||||||||||
| 240 | direction and it will continue to scroll in this direction until it is | - | ||||||||||||
| 241 | stopped either by the user or by friction. Aspects of inertia, friction | - | ||||||||||||
| 242 | and other physical concepts can be changed in order to fine-tune an | - | ||||||||||||
| 243 | intuitive user experience. | - | ||||||||||||
| 244 | - | |||||||||||||
| 245 | The QScroller object is the object that stores the current position and | - | ||||||||||||
| 246 | scrolling speed and takes care of updates. | - | ||||||||||||
| 247 | QScroller can be triggered by a flick gesture | - | ||||||||||||
| 248 | - | |||||||||||||
| 249 | \code | - | ||||||||||||
| 250 | QWidget *w = ...; | - | ||||||||||||
| 251 | QScroller::grabGesture(w, QScroller::LeftMouseButtonGesture); | - | ||||||||||||
| 252 | \endcode | - | ||||||||||||
| 253 | - | |||||||||||||
| 254 | or directly like this: | - | ||||||||||||
| 255 | - | |||||||||||||
| 256 | \code | - | ||||||||||||
| 257 | QWidget *w = ...; | - | ||||||||||||
| 258 | QScroller *scroller = QScroller::scroller(w); | - | ||||||||||||
| 259 | scroller->scrollTo(QPointF(100, 100)); | - | ||||||||||||
| 260 | \endcode | - | ||||||||||||
| 261 | - | |||||||||||||
| 262 | The scrolled QObjects receive a QScrollPrepareEvent whenever the scroller needs to | - | ||||||||||||
| 263 | update its geometry information and a QScrollEvent whenever the content of the object should | - | ||||||||||||
| 264 | actually be scrolled. | - | ||||||||||||
| 265 | - | |||||||||||||
| 266 | The scroller uses the global QAbstractAnimation timer to generate its QScrollEvents. This | - | ||||||||||||
| 267 | can be changed with QScrollerProperties::FrameRate on a per-QScroller basis. | - | ||||||||||||
| 268 | - | |||||||||||||
| 269 | Several examples in the \c scroller examples directory show how QScroller, | - | ||||||||||||
| 270 | QScrollEvent and the scroller gesture can be used. | - | ||||||||||||
| 271 | - | |||||||||||||
| 272 | Even though this kinetic scroller has a large number of settings available via | - | ||||||||||||
| 273 | QScrollerProperties, we recommend that you leave them all at their default, platform optimized | - | ||||||||||||
| 274 | values. Before changing them you can experiment with the \c plot example in | - | ||||||||||||
| 275 | the \c scroller examples directory. | - | ||||||||||||
| 276 | - | |||||||||||||
| 277 | \sa QScrollEvent, QScrollPrepareEvent, QScrollerProperties | - | ||||||||||||
| 278 | */ | - | ||||||||||||
| 279 | - | |||||||||||||
| 280 | typedef QMap<QObject *, QScroller *> ScrollerHash; | - | ||||||||||||
| 281 | typedef QSet<QScroller *> ScrollerSet; | - | ||||||||||||
| 282 | - | |||||||||||||
| 283 | Q_GLOBAL_STATIC(ScrollerHash, qt_allScrollers) | - | ||||||||||||
| 284 | Q_GLOBAL_STATIC(ScrollerSet, qt_activeScrollers) | - | ||||||||||||
| 285 | - | |||||||||||||
| 286 | /*! | - | ||||||||||||
| 287 | Returns \c true if a QScroller object was already created for \a target; \c false otherwise. | - | ||||||||||||
| 288 | - | |||||||||||||
| 289 | \sa scroller() | - | ||||||||||||
| 290 | */ | - | ||||||||||||
| 291 | bool QScroller::hasScroller(QObject *target) | - | ||||||||||||
| 292 | { | - | ||||||||||||
| 293 | return (qt_allScrollers()->value(target)); | - | ||||||||||||
| 294 | } | - | ||||||||||||
| 295 | - | |||||||||||||
| 296 | /*! | - | ||||||||||||
| 297 | Returns the scroller for the given \a target. | - | ||||||||||||
| 298 | As long as the object exists this function will always return the same QScroller instance. | - | ||||||||||||
| 299 | If no QScroller exists for the \a target, one will implicitly be created. | - | ||||||||||||
| 300 | At no point more than one QScroller will be active on an object. | - | ||||||||||||
| 301 | - | |||||||||||||
| 302 | \sa hasScroller(), target() | - | ||||||||||||
| 303 | */ | - | ||||||||||||
| 304 | QScroller *QScroller::scroller(QObject *target) | - | ||||||||||||
| 305 | { | - | ||||||||||||
| 306 | if (!target) { | - | ||||||||||||
| 307 | qWarning("QScroller::scroller() was called with a null target."); | - | ||||||||||||
| 308 | return 0; | - | ||||||||||||
| 309 | } | - | ||||||||||||
| 310 | - | |||||||||||||
| 311 | if (qt_allScrollers()->contains(target)) | - | ||||||||||||
| 312 | return qt_allScrollers()->value(target); | - | ||||||||||||
| 313 | - | |||||||||||||
| 314 | QScroller *s = new QScroller(target); | - | ||||||||||||
| 315 | qt_allScrollers()->insert(target, s); | - | ||||||||||||
| 316 | return s; | - | ||||||||||||
| 317 | } | - | ||||||||||||
| 318 | - | |||||||||||||
| 319 | /*! | - | ||||||||||||
| 320 | \overload | - | ||||||||||||
| 321 | This is the const version of scroller(). | - | ||||||||||||
| 322 | */ | - | ||||||||||||
| 323 | const QScroller *QScroller::scroller(const QObject *target) | - | ||||||||||||
| 324 | { | - | ||||||||||||
| 325 | return scroller(const_cast<QObject*>(target)); | - | ||||||||||||
| 326 | } | - | ||||||||||||
| 327 | - | |||||||||||||
| 328 | /*! | - | ||||||||||||
| 329 | Returns an application wide list of currently active QScroller objects. | - | ||||||||||||
| 330 | Active QScroller objects are in a state() that is not QScroller::Inactive. | - | ||||||||||||
| 331 | This function is useful when writing your own gesture recognizer. | - | ||||||||||||
| 332 | */ | - | ||||||||||||
| 333 | QList<QScroller *> QScroller::activeScrollers() | - | ||||||||||||
| 334 | { | - | ||||||||||||
| 335 | return qt_activeScrollers()->toList(); | - | ||||||||||||
| 336 | } | - | ||||||||||||
| 337 | - | |||||||||||||
| 338 | /*! | - | ||||||||||||
| 339 | Returns the target object of this scroller. | - | ||||||||||||
| 340 | \sa hasScroller(), scroller() | - | ||||||||||||
| 341 | */ | - | ||||||||||||
| 342 | QObject *QScroller::target() const | - | ||||||||||||
| 343 | { | - | ||||||||||||
| 344 | Q_D(const QScroller); | - | ||||||||||||
| 345 | return d->target; | - | ||||||||||||
| 346 | } | - | ||||||||||||
| 347 | - | |||||||||||||
| 348 | /*! | - | ||||||||||||
| 349 | \fn QScroller::scrollerPropertiesChanged(const QScrollerProperties &newProperties); | - | ||||||||||||
| 350 | - | |||||||||||||
| 351 | QScroller emits this signal whenever its scroller properties change. | - | ||||||||||||
| 352 | \a newProperties are the new scroller properties. | - | ||||||||||||
| 353 | - | |||||||||||||
| 354 | \sa scrollerProperties | - | ||||||||||||
| 355 | */ | - | ||||||||||||
| 356 | - | |||||||||||||
| 357 | - | |||||||||||||
| 358 | /*! \property QScroller::scrollerProperties | - | ||||||||||||
| 359 | \brief The scroller properties of this scroller. | - | ||||||||||||
| 360 | The properties are used by the QScroller to determine its scrolling behavior. | - | ||||||||||||
| 361 | */ | - | ||||||||||||
| 362 | QScrollerProperties QScroller::scrollerProperties() const | - | ||||||||||||
| 363 | { | - | ||||||||||||
| 364 | Q_D(const QScroller); | - | ||||||||||||
| 365 | return d->properties; | - | ||||||||||||
| 366 | } | - | ||||||||||||
| 367 | - | |||||||||||||
| 368 | void QScroller::setScrollerProperties(const QScrollerProperties &sp) | - | ||||||||||||
| 369 | { | - | ||||||||||||
| 370 | Q_D(QScroller); | - | ||||||||||||
| 371 | if (d->properties != sp) { | - | ||||||||||||
| 372 | d->properties = sp; | - | ||||||||||||
| 373 | emit scrollerPropertiesChanged(sp); | - | ||||||||||||
| 374 | - | |||||||||||||
| 375 | // we need to force the recalculation here, since the overshootPolicy may have changed and | - | ||||||||||||
| 376 | // existing segments may include an overshoot animation. | - | ||||||||||||
| 377 | d->recalcScrollingSegments(true); | - | ||||||||||||
| 378 | } | - | ||||||||||||
| 379 | } | - | ||||||||||||
| 380 | - | |||||||||||||
| 381 | #ifndef QT_NO_GESTURES | - | ||||||||||||
| 382 | - | |||||||||||||
| 383 | /*! | - | ||||||||||||
| 384 | Registers a custom scroll gesture recognizer, grabs it for the \a | - | ||||||||||||
| 385 | target and returns the resulting gesture type. If \a scrollGestureType is | - | ||||||||||||
| 386 | set to TouchGesture the gesture triggers on touch events. If it is set to | - | ||||||||||||
| 387 | one of LeftMouseButtonGesture, RightMouseButtonGesture or | - | ||||||||||||
| 388 | MiddleMouseButtonGesture it triggers on mouse events of the | - | ||||||||||||
| 389 | corresponding button. | - | ||||||||||||
| 390 | - | |||||||||||||
| 391 | Only one scroll gesture can be active on a single object at the same | - | ||||||||||||
| 392 | time. If you call this function twice on the same object, it will | - | ||||||||||||
| 393 | ungrab the existing gesture before grabbing the new one. | - | ||||||||||||
| 394 | - | |||||||||||||
| 395 | \note To avoid unwanted side-effects, mouse events are consumed while | - | ||||||||||||
| 396 | the gesture is triggered. Since the initial mouse press event is | - | ||||||||||||
| 397 | not consumed, the gesture sends a fake mouse release event | - | ||||||||||||
| 398 | at the global position \c{(INT_MIN, INT_MIN)}. This ensures that | - | ||||||||||||
| 399 | internal states of the widget that received the original mouse press | - | ||||||||||||
| 400 | are consistent. | - | ||||||||||||
| 401 | - | |||||||||||||
| 402 | \sa ungrabGesture(), grabbedGesture() | - | ||||||||||||
| 403 | */ | - | ||||||||||||
| 404 | Qt::GestureType QScroller::grabGesture(QObject *target, ScrollerGestureType scrollGestureType) | - | ||||||||||||
| 405 | { | - | ||||||||||||
| 406 | // ensure that a scroller for target is created | - | ||||||||||||
| 407 | QScroller *s = scroller(target); | - | ||||||||||||
| 408 | if (!s) | - | ||||||||||||
| 409 | return Qt::GestureType(0); | - | ||||||||||||
| 410 | - | |||||||||||||
| 411 | QScrollerPrivate *sp = s->d_ptr; | - | ||||||||||||
| 412 | if (sp->recognizer) | - | ||||||||||||
| 413 | ungrabGesture(target); // ungrab the old gesture | - | ||||||||||||
| 414 | - | |||||||||||||
| 415 | Qt::MouseButton button; | - | ||||||||||||
| 416 | switch (scrollGestureType) { | - | ||||||||||||
| 417 | case LeftMouseButtonGesture : button = Qt::LeftButton; break; | - | ||||||||||||
| 418 | case RightMouseButtonGesture : button = Qt::RightButton; break; | - | ||||||||||||
| 419 | case MiddleMouseButtonGesture: button = Qt::MiddleButton; break; | - | ||||||||||||
| 420 | default : | - | ||||||||||||
| 421 | case TouchGesture : button = Qt::NoButton; break; // NoButton == Touch | - | ||||||||||||
| 422 | } | - | ||||||||||||
| 423 | - | |||||||||||||
| 424 | sp->recognizer = new QFlickGestureRecognizer(button); | - | ||||||||||||
| 425 | sp->recognizerType = QGestureRecognizer::registerRecognizer(sp->recognizer); | - | ||||||||||||
| 426 | - | |||||||||||||
| 427 | if (target->isWidgetType()) { | - | ||||||||||||
| 428 | QWidget *widget = static_cast<QWidget *>(target); | - | ||||||||||||
| 429 | widget->grabGesture(sp->recognizerType); | - | ||||||||||||
| 430 | if (scrollGestureType == TouchGesture) | - | ||||||||||||
| 431 | widget->setAttribute(Qt::WA_AcceptTouchEvents); | - | ||||||||||||
| 432 | #ifndef QT_NO_GRAPHICSVIEW | - | ||||||||||||
| 433 | } else if (QGraphicsObject *go = qobject_cast<QGraphicsObject*>(target)) { | - | ||||||||||||
| 434 | if (scrollGestureType == TouchGesture) | - | ||||||||||||
| 435 | go->setAcceptTouchEvents(true); | - | ||||||||||||
| 436 | go->grabGesture(sp->recognizerType); | - | ||||||||||||
| 437 | #endif // QT_NO_GRAPHICSVIEW | - | ||||||||||||
| 438 | } | - | ||||||||||||
| 439 | return sp->recognizerType; | - | ||||||||||||
| 440 | } | - | ||||||||||||
| 441 | - | |||||||||||||
| 442 | /*! | - | ||||||||||||
| 443 | Returns the gesture type currently grabbed for the \a target or 0 if no | - | ||||||||||||
| 444 | gesture is grabbed. | - | ||||||||||||
| 445 | - | |||||||||||||
| 446 | \sa grabGesture(), ungrabGesture() | - | ||||||||||||
| 447 | */ | - | ||||||||||||
| 448 | Qt::GestureType QScroller::grabbedGesture(QObject *target) | - | ||||||||||||
| 449 | { | - | ||||||||||||
| 450 | QScroller *s = scroller(target); | - | ||||||||||||
| 451 | if (s && s->d_ptr) | - | ||||||||||||
| 452 | return s->d_ptr->recognizerType; | - | ||||||||||||
| 453 | else | - | ||||||||||||
| 454 | return Qt::GestureType(0); | - | ||||||||||||
| 455 | } | - | ||||||||||||
| 456 | - | |||||||||||||
| 457 | /*! | - | ||||||||||||
| 458 | Ungrabs the gesture for the \a target. | - | ||||||||||||
| 459 | Does nothing if no gesture is grabbed. | - | ||||||||||||
| 460 | - | |||||||||||||
| 461 | \sa grabGesture(), grabbedGesture() | - | ||||||||||||
| 462 | */ | - | ||||||||||||
| 463 | void QScroller::ungrabGesture(QObject *target) | - | ||||||||||||
| 464 | { | - | ||||||||||||
| 465 | QScroller *s = scroller(target); | - | ||||||||||||
| 466 | if (!s) | - | ||||||||||||
| 467 | return; | - | ||||||||||||
| 468 | - | |||||||||||||
| 469 | QScrollerPrivate *sp = s->d_ptr; | - | ||||||||||||
| 470 | if (!sp->recognizer) | - | ||||||||||||
| 471 | return; // nothing to do | - | ||||||||||||
| 472 | - | |||||||||||||
| 473 | if (target->isWidgetType()) { | - | ||||||||||||
| 474 | QWidget *widget = static_cast<QWidget *>(target); | - | ||||||||||||
| 475 | widget->ungrabGesture(sp->recognizerType); | - | ||||||||||||
| 476 | #ifndef QT_NO_GRAPHICSVIEW | - | ||||||||||||
| 477 | } else if (QGraphicsObject *go = qobject_cast<QGraphicsObject*>(target)) { | - | ||||||||||||
| 478 | go->ungrabGesture(sp->recognizerType); | - | ||||||||||||
| 479 | #endif | - | ||||||||||||
| 480 | } | - | ||||||||||||
| 481 | - | |||||||||||||
| 482 | QGestureRecognizer::unregisterRecognizer(sp->recognizerType); | - | ||||||||||||
| 483 | // do not delete the recognizer. The QGestureManager is doing this. | - | ||||||||||||
| 484 | sp->recognizer = 0; | - | ||||||||||||
| 485 | } | - | ||||||||||||
| 486 | - | |||||||||||||
| 487 | #endif // QT_NO_GESTURES | - | ||||||||||||
| 488 | - | |||||||||||||
| 489 | /*! | - | ||||||||||||
| 490 | \internal | - | ||||||||||||
| 491 | */ | - | ||||||||||||
| 492 | QScroller::QScroller(QObject *target) | - | ||||||||||||
| 493 | : d_ptr(new QScrollerPrivate(this, target)) | - | ||||||||||||
| 494 | { | - | ||||||||||||
| 495 | Q_ASSERT(target); // you can't create a scroller without a target in any normal way | - | ||||||||||||
| 496 | Q_D(QScroller); | - | ||||||||||||
| 497 | d->init(); | - | ||||||||||||
| 498 | } | - | ||||||||||||
| 499 | - | |||||||||||||
| 500 | /*! | - | ||||||||||||
| 501 | \internal | - | ||||||||||||
| 502 | */ | - | ||||||||||||
| 503 | QScroller::~QScroller() | - | ||||||||||||
| 504 | { | - | ||||||||||||
| 505 | Q_D(QScroller); | - | ||||||||||||
| 506 | #ifndef QT_NO_GESTURES | - | ||||||||||||
| 507 | QGestureRecognizer::unregisterRecognizer(d->recognizerType); | - | ||||||||||||
| 508 | // do not delete the recognizer. The QGestureManager is doing this. | - | ||||||||||||
| 509 | d->recognizer = 0; | - | ||||||||||||
| 510 | #endif | - | ||||||||||||
| 511 | qt_allScrollers()->remove(d->target); | - | ||||||||||||
| 512 | qt_activeScrollers()->remove(this); | - | ||||||||||||
| 513 | - | |||||||||||||
| 514 | delete d_ptr; | - | ||||||||||||
| 515 | } | - | ||||||||||||
| 516 | - | |||||||||||||
| 517 | - | |||||||||||||
| 518 | /*! | - | ||||||||||||
| 519 | \fn QScroller::stateChanged(QScroller::State newState); | - | ||||||||||||
| 520 | - | |||||||||||||
| 521 | QScroller emits this signal whenever the state changes. \a newState is the new State. | - | ||||||||||||
| 522 | - | |||||||||||||
| 523 | \sa state | - | ||||||||||||
| 524 | */ | - | ||||||||||||
| 525 | - | |||||||||||||
| 526 | /*! | - | ||||||||||||
| 527 | \property QScroller::state | - | ||||||||||||
| 528 | \brief the state of the scroller | - | ||||||||||||
| 529 | - | |||||||||||||
| 530 | \sa QScroller::State | - | ||||||||||||
| 531 | */ | - | ||||||||||||
| 532 | QScroller::State QScroller::state() const | - | ||||||||||||
| 533 | { | - | ||||||||||||
| 534 | Q_D(const QScroller); | - | ||||||||||||
| 535 | return d->state; | - | ||||||||||||
| 536 | } | - | ||||||||||||
| 537 | - | |||||||||||||
| 538 | /*! | - | ||||||||||||
| 539 | Stops the scroller and resets its state back to Inactive. | - | ||||||||||||
| 540 | */ | - | ||||||||||||
| 541 | void QScroller::stop() | - | ||||||||||||
| 542 | { | - | ||||||||||||
| 543 | Q_D(QScroller); | - | ||||||||||||
| 544 | if (d->state != Inactive) { | - | ||||||||||||
| 545 | QPointF here = clampToRect(d->contentPosition, d->contentPosRange); | - | ||||||||||||
| 546 | qreal snapX = d->nextSnapPos(here.x(), 0, Qt::Horizontal); | - | ||||||||||||
| 547 | qreal snapY = d->nextSnapPos(here.y(), 0, Qt::Vertical); | - | ||||||||||||
| 548 | QPointF snap = here; | - | ||||||||||||
| 549 | if (!qIsNaN(snapX)) | - | ||||||||||||
| 550 | snap.setX(snapX); | - | ||||||||||||
| 551 | if (!qIsNaN(snapY)) | - | ||||||||||||
| 552 | snap.setY(snapY); | - | ||||||||||||
| 553 | d->contentPosition = snap; | - | ||||||||||||
| 554 | d->overshootPosition = QPointF(0, 0); | - | ||||||||||||
| 555 | - | |||||||||||||
| 556 | d->setState(Inactive); | - | ||||||||||||
| 557 | } | - | ||||||||||||
| 558 | } | - | ||||||||||||
| 559 | - | |||||||||||||
| 560 | /*! | - | ||||||||||||
| 561 | Returns the pixel per meter metric for the scrolled widget. | - | ||||||||||||
| 562 | - | |||||||||||||
| 563 | The value is reported for both the x and y axis separately by using a QPointF. | - | ||||||||||||
| 564 | - | |||||||||||||
| 565 | \note Please note that this value should be physically correct. The actual DPI settings | - | ||||||||||||
| 566 | that Qt returns for the display may be reported wrongly on purpose by the underlying | - | ||||||||||||
| 567 | windowing system, for example on \macos. | - | ||||||||||||
| 568 | */ | - | ||||||||||||
| 569 | QPointF QScroller::pixelPerMeter() const | - | ||||||||||||
| 570 | { | - | ||||||||||||
| 571 | Q_D(const QScroller); | - | ||||||||||||
| 572 | QPointF ppm = d->pixelPerMeter; | - | ||||||||||||
| 573 | - | |||||||||||||
| 574 | #ifndef QT_NO_GRAPHICSVIEW | - | ||||||||||||
| 575 | if (QGraphicsObject *go = qobject_cast<QGraphicsObject *>(d->target)) {
| 0 | ||||||||||||
| 576 | QTransform viewtr; | - | ||||||||||||
| 577 | //TODO: the first view isn't really correct - maybe use an additional field in the prepare event? | - | ||||||||||||
| 578 | if (go->const auto *scene () && != go->scene()->()) {
| 0 | ||||||||||||
| 579 | const auto views ().= scene->views(); | - | ||||||||||||
| 580 | if (!views.
| 0 | ||||||||||||
| 581 | viewtr = go->scene()->views()..first()->viewportTransform(); never executed: viewtr = views.first()->viewportTransform(); | 0 | ||||||||||||
| 582 | } never executed: end of block | 0 | ||||||||||||
| 583 | QTransform tr = go->deviceTransform(viewtr); | - | ||||||||||||
| 584 | if (tr.isScaling()) {
| 0 | ||||||||||||
| 585 | QPointF p0 = tr.map(QPointF(0, 0)); | - | ||||||||||||
| 586 | QPointF px = tr.map(QPointF(1, 0)); | - | ||||||||||||
| 587 | QPointF py = tr.map(QPointF(0, 1)); | - | ||||||||||||
| 588 | ppm.rx() /= QLineF(p0, px).length(); | - | ||||||||||||
| 589 | ppm.ry() /= QLineF(p0, py).length(); | - | ||||||||||||
| 590 | } never executed: end of block | 0 | ||||||||||||
| 591 | } never executed: end of block | 0 | ||||||||||||
| 592 | #endif // QT_NO_GRAPHICSVIEW | - | ||||||||||||
| 593 | return ppm; never executed: return ppm; | 0 | ||||||||||||
| 594 | } | - | ||||||||||||
| 595 | - | |||||||||||||
| 596 | /*! | - | ||||||||||||
| 597 | Returns the current scrolling velocity in meter per second when the state is Scrolling or Dragging. | - | ||||||||||||
| 598 | Returns a zero velocity otherwise. | - | ||||||||||||
| 599 | - | |||||||||||||
| 600 | The velocity is reported for both the x and y axis separately by using a QPointF. | - | ||||||||||||
| 601 | - | |||||||||||||
| 602 | \sa pixelPerMeter() | - | ||||||||||||
| 603 | */ | - | ||||||||||||
| 604 | QPointF QScroller::velocity() const | - | ||||||||||||
| 605 | { | - | ||||||||||||
| 606 | Q_D(const QScroller); | - | ||||||||||||
| 607 | const QScrollerPropertiesPrivate *sp = d->properties.d.data(); | - | ||||||||||||
| 608 | - | |||||||||||||
| 609 | switch (state()) { | - | ||||||||||||
| 610 | case Dragging: | - | ||||||||||||
| 611 | return d->releaseVelocity; | - | ||||||||||||
| 612 | case Scrolling: { | - | ||||||||||||
| 613 | QPointF vel; | - | ||||||||||||
| 614 | qint64 now = d->monotonicTimer.elapsed(); | - | ||||||||||||
| 615 | - | |||||||||||||
| 616 | if (!d->xSegments.isEmpty()) { | - | ||||||||||||
| 617 | const QScrollerPrivate::ScrollSegment &s = d->xSegments.head(); | - | ||||||||||||
| 618 | qreal progress = qreal(now - s.startTime) / qreal(s.deltaTime); | - | ||||||||||||
| 619 | qreal v = qSign(s.deltaPos) * qreal(s.deltaTime) / qreal(1000) * sp->decelerationFactor * qreal(0.5) * differentialForProgress(s.curve, progress); | - | ||||||||||||
| 620 | vel.setX(v); | - | ||||||||||||
| 621 | } | - | ||||||||||||
| 622 | - | |||||||||||||
| 623 | if (!d->ySegments.isEmpty()) { | - | ||||||||||||
| 624 | const QScrollerPrivate::ScrollSegment &s = d->ySegments.head(); | - | ||||||||||||
| 625 | qreal progress = qreal(now - s.startTime) / qreal(s.deltaTime); | - | ||||||||||||
| 626 | qreal v = qSign(s.deltaPos) * qreal(s.deltaTime) / qreal(1000) * sp->decelerationFactor * qreal(0.5) * differentialForProgress(s.curve, progress); | - | ||||||||||||
| 627 | vel.setY(v); | - | ||||||||||||
| 628 | } | - | ||||||||||||
| 629 | return vel; | - | ||||||||||||
| 630 | } | - | ||||||||||||
| 631 | default: | - | ||||||||||||
| 632 | return QPointF(0, 0); | - | ||||||||||||
| 633 | } | - | ||||||||||||
| 634 | } | - | ||||||||||||
| 635 | - | |||||||||||||
| 636 | /*! | - | ||||||||||||
| 637 | Returns the estimated final position for the current scroll movement. | - | ||||||||||||
| 638 | Returns the current position if the scroller state is not Scrolling. | - | ||||||||||||
| 639 | The result is undefined when the scroller state is Inactive. | - | ||||||||||||
| 640 | - | |||||||||||||
| 641 | The target position is in pixel. | - | ||||||||||||
| 642 | - | |||||||||||||
| 643 | \sa pixelPerMeter(), scrollTo() | - | ||||||||||||
| 644 | */ | - | ||||||||||||
| 645 | QPointF QScroller::finalPosition() const | - | ||||||||||||
| 646 | { | - | ||||||||||||
| 647 | Q_D(const QScroller); | - | ||||||||||||
| 648 | return QPointF(d->scrollingSegmentsEndPos(Qt::Horizontal), | - | ||||||||||||
| 649 | d->scrollingSegmentsEndPos(Qt::Vertical)); | - | ||||||||||||
| 650 | } | - | ||||||||||||
| 651 | - | |||||||||||||
| 652 | /*! | - | ||||||||||||
| 653 | Starts scrolling the widget so that point \a pos is at the top-left position in | - | ||||||||||||
| 654 | the viewport. | - | ||||||||||||
| 655 | - | |||||||||||||
| 656 | The behaviour when scrolling outside the valid scroll area is undefined. | - | ||||||||||||
| 657 | In this case the scroller might or might not overshoot. | - | ||||||||||||
| 658 | - | |||||||||||||
| 659 | The scrolling speed will be calculated so that the given position will | - | ||||||||||||
| 660 | be reached after a platform-defined time span. | - | ||||||||||||
| 661 | - | |||||||||||||
| 662 | \a pos is given in viewport coordinates. | - | ||||||||||||
| 663 | - | |||||||||||||
| 664 | \sa ensureVisible() | - | ||||||||||||
| 665 | */ | - | ||||||||||||
| 666 | void QScroller::scrollTo(const QPointF &pos) | - | ||||||||||||
| 667 | { | - | ||||||||||||
| 668 | // we could make this adjustable via QScrollerProperties | - | ||||||||||||
| 669 | scrollTo(pos, 300); | - | ||||||||||||
| 670 | } | - | ||||||||||||
| 671 | - | |||||||||||||
| 672 | /*! \overload | - | ||||||||||||
| 673 | - | |||||||||||||
| 674 | This version will reach its destination position in \a scrollTime milliseconds. | - | ||||||||||||
| 675 | */ | - | ||||||||||||
| 676 | void QScroller::scrollTo(const QPointF &pos, int scrollTime) | - | ||||||||||||
| 677 | { | - | ||||||||||||
| 678 | Q_D(QScroller); | - | ||||||||||||
| 679 | - | |||||||||||||
| 680 | if (d->state == Pressed || d->state == Dragging ) | - | ||||||||||||
| 681 | return; | - | ||||||||||||
| 682 | - | |||||||||||||
| 683 | // no need to resend a prepare event if we are already scrolling | - | ||||||||||||
| 684 | if (d->state == Inactive && !d->prepareScrolling(QPointF())) | - | ||||||||||||
| 685 | return; | - | ||||||||||||
| 686 | - | |||||||||||||
| 687 | QPointF newpos = clampToRect(pos, d->contentPosRange); | - | ||||||||||||
| 688 | qreal snapX = d->nextSnapPos(newpos.x(), 0, Qt::Horizontal); | - | ||||||||||||
| 689 | qreal snapY = d->nextSnapPos(newpos.y(), 0, Qt::Vertical); | - | ||||||||||||
| 690 | if (!qIsNaN(snapX)) | - | ||||||||||||
| 691 | newpos.setX(snapX); | - | ||||||||||||
| 692 | if (!qIsNaN(snapY)) | - | ||||||||||||
| 693 | newpos.setY(snapY); | - | ||||||||||||
| 694 | - | |||||||||||||
| 695 | qScrollerDebug() << "QScroller::scrollTo(req:" << pos << " [pix] / snap:" << newpos << ", " << scrollTime << " [ms])"; dead code: QMessageLogger(__FILE__, 695, __PRETTY_FUNCTION__).debug() << "QScroller::scrollTo(req:" << pos << " [pix] / snap:" << newpos << ", " << scrollTime << " [ms])"; | - | ||||||||||||
| 696 | - | |||||||||||||
| 697 | if (newpos == d->contentPosition + d->overshootPosition) | - | ||||||||||||
| 698 | return; | - | ||||||||||||
| 699 | - | |||||||||||||
| 700 | QPointF vel = velocity(); | - | ||||||||||||
| 701 | - | |||||||||||||
| 702 | if (scrollTime < 0) | - | ||||||||||||
| 703 | scrollTime = 0; | - | ||||||||||||
| 704 | qreal time = qreal(scrollTime) / 1000; | - | ||||||||||||
| 705 | - | |||||||||||||
| 706 | d->createScrollToSegments(vel.x(), time, newpos.x(), Qt::Horizontal, QScrollerPrivate::ScrollTypeScrollTo); | - | ||||||||||||
| 707 | d->createScrollToSegments(vel.y(), time, newpos.y(), Qt::Vertical, QScrollerPrivate::ScrollTypeScrollTo); | - | ||||||||||||
| 708 | - | |||||||||||||
| 709 | if (!scrollTime) | - | ||||||||||||
| 710 | d->setContentPositionHelperScrolling(); | - | ||||||||||||
| 711 | d->setState(scrollTime ? Scrolling : Inactive); | - | ||||||||||||
| 712 | } | - | ||||||||||||
| 713 | - | |||||||||||||
| 714 | /*! | - | ||||||||||||
| 715 | Starts scrolling so that the rectangle \a rect is visible inside the | - | ||||||||||||
| 716 | viewport with additional margins specified in pixels by \a xmargin and \a ymargin around | - | ||||||||||||
| 717 | the rect. | - | ||||||||||||
| 718 | - | |||||||||||||
| 719 | In cases where it is not possible to fit the rect plus margins inside the viewport the contents | - | ||||||||||||
| 720 | are scrolled so that as much as possible is visible from \a rect. | - | ||||||||||||
| 721 | - | |||||||||||||
| 722 | The scrolling speed is calculated so that the given position is reached after a platform-defined | - | ||||||||||||
| 723 | time span. | - | ||||||||||||
| 724 | - | |||||||||||||
| 725 | This function performs the actual scrolling by calling scrollTo(). | - | ||||||||||||
| 726 | - | |||||||||||||
| 727 | \sa scrollTo() | - | ||||||||||||
| 728 | */ | - | ||||||||||||
| 729 | void QScroller::ensureVisible(const QRectF &rect, qreal xmargin, qreal ymargin) | - | ||||||||||||
| 730 | { | - | ||||||||||||
| 731 | // we could make this adjustable via QScrollerProperties | - | ||||||||||||
| 732 | ensureVisible(rect, xmargin, ymargin, 1000); | - | ||||||||||||
| 733 | } | - | ||||||||||||
| 734 | - | |||||||||||||
| 735 | /*! \overload | - | ||||||||||||
| 736 | - | |||||||||||||
| 737 | This version will reach its destination position in \a scrollTime milliseconds. | - | ||||||||||||
| 738 | */ | - | ||||||||||||
| 739 | void QScroller::ensureVisible(const QRectF &rect, qreal xmargin, qreal ymargin, int scrollTime) | - | ||||||||||||
| 740 | { | - | ||||||||||||
| 741 | Q_D(QScroller); | - | ||||||||||||
| 742 | - | |||||||||||||
| 743 | if (d->state == Pressed || d->state == Dragging ) | - | ||||||||||||
| 744 | return; | - | ||||||||||||
| 745 | - | |||||||||||||
| 746 | if (d->state == Inactive && !d->prepareScrolling(QPointF())) | - | ||||||||||||
| 747 | return; | - | ||||||||||||
| 748 | - | |||||||||||||
| 749 | // -- calculate the current pos (or the position after the current scroll) | - | ||||||||||||
| 750 | QPointF startPos(d->scrollingSegmentsEndPos(Qt::Horizontal), | - | ||||||||||||
| 751 | d->scrollingSegmentsEndPos(Qt::Vertical)); | - | ||||||||||||
| 752 | - | |||||||||||||
| 753 | QRectF marginRect(rect.x() - xmargin, rect.y() - ymargin, | - | ||||||||||||
| 754 | rect.width() + 2 * xmargin, rect.height() + 2 * ymargin); | - | ||||||||||||
| 755 | - | |||||||||||||
| 756 | QSizeF visible = d->viewportSize; | - | ||||||||||||
| 757 | QRectF visibleRect(startPos, visible); | - | ||||||||||||
| 758 | - | |||||||||||||
| 759 | qScrollerDebug() << "QScroller::ensureVisible(" << rect << " [pix], " << xmargin << " [pix], " << ymargin << " [pix], " << scrollTime << "[ms])"; dead code: QMessageLogger(__FILE__, 759, __PRETTY_FUNCTION__).debug() << "QScroller::ensureVisible(" << rect << " [pix], " << xmargin << " [pix], " << ymargin << " [pix], " << scrollTime << "[ms])"; | - | ||||||||||||
| 760 | qScrollerDebug() << " --> content position:" << d->contentPosition; dead code: QMessageLogger(__FILE__, 760, __PRETTY_FUNCTION__).debug() << " --> content position:" << d->contentPosition; | - | ||||||||||||
| 761 | - | |||||||||||||
| 762 | if (visibleRect.contains(marginRect)) | - | ||||||||||||
| 763 | return; | - | ||||||||||||
| 764 | - | |||||||||||||
| 765 | QPointF newPos = startPos; | - | ||||||||||||
| 766 | - | |||||||||||||
| 767 | if (visibleRect.width() < rect.width()) { | - | ||||||||||||
| 768 | // at least try to move the rect into view | - | ||||||||||||
| 769 | if (rect.left() > visibleRect.left()) | - | ||||||||||||
| 770 | newPos.setX(rect.left()); | - | ||||||||||||
| 771 | else if (rect.right() < visibleRect.right()) | - | ||||||||||||
| 772 | newPos.setX(rect.right() - visible.width()); | - | ||||||||||||
| 773 | - | |||||||||||||
| 774 | } else if (visibleRect.width() < marginRect.width()) { | - | ||||||||||||
| 775 | newPos.setX(rect.center().x() - visibleRect.width() / 2); | - | ||||||||||||
| 776 | } else if (marginRect.left() > visibleRect.left()) { | - | ||||||||||||
| 777 | newPos.setX(marginRect.left()); | - | ||||||||||||
| 778 | } else if (marginRect.right() < visibleRect.right()) { | - | ||||||||||||
| 779 | newPos.setX(marginRect.right() - visible.width()); | - | ||||||||||||
| 780 | } | - | ||||||||||||
| 781 | - | |||||||||||||
| 782 | if (visibleRect.height() < rect.height()) { | - | ||||||||||||
| 783 | // at least try to move the rect into view | - | ||||||||||||
| 784 | if (rect.top() > visibleRect.top()) | - | ||||||||||||
| 785 | newPos.setX(rect.top()); | - | ||||||||||||
| 786 | else if (rect.bottom() < visibleRect.bottom()) | - | ||||||||||||
| 787 | newPos.setX(rect.bottom() - visible.height()); | - | ||||||||||||
| 788 | - | |||||||||||||
| 789 | } else if (visibleRect.height() < marginRect.height()) { | - | ||||||||||||
| 790 | newPos.setY(rect.center().y() - visibleRect.height() / 2); | - | ||||||||||||
| 791 | } else if (marginRect.top() > visibleRect.top()) { | - | ||||||||||||
| 792 | newPos.setY(marginRect.top()); | - | ||||||||||||
| 793 | } else if (marginRect.bottom() < visibleRect.bottom()) { | - | ||||||||||||
| 794 | newPos.setY(marginRect.bottom() - visible.height()); | - | ||||||||||||
| 795 | } | - | ||||||||||||
| 796 | - | |||||||||||||
| 797 | // clamp to maximum content position | - | ||||||||||||
| 798 | newPos = clampToRect(newPos, d->contentPosRange); | - | ||||||||||||
| 799 | if (newPos == startPos) | - | ||||||||||||
| 800 | return; | - | ||||||||||||
| 801 | - | |||||||||||||
| 802 | scrollTo(newPos, scrollTime); | - | ||||||||||||
| 803 | } | - | ||||||||||||
| 804 | - | |||||||||||||
| 805 | /*! This function resends the QScrollPrepareEvent. | - | ||||||||||||
| 806 | Calling resendPrepareEvent triggers a QScrollPrepareEvent from the scroller. | - | ||||||||||||
| 807 | This allows the receiver to re-set content position and content size while | - | ||||||||||||
| 808 | scrolling. | - | ||||||||||||
| 809 | Calling this function while in the Inactive state is useless as the prepare event | - | ||||||||||||
| 810 | is sent again before scrolling starts. | - | ||||||||||||
| 811 | */ | - | ||||||||||||
| 812 | void QScroller::resendPrepareEvent() | - | ||||||||||||
| 813 | { | - | ||||||||||||
| 814 | Q_D(QScroller); | - | ||||||||||||
| 815 | d->prepareScrolling(d->pressPosition); | - | ||||||||||||
| 816 | } | - | ||||||||||||
| 817 | - | |||||||||||||
| 818 | /*! Set the snap positions for the horizontal axis to a list of \a positions. | - | ||||||||||||
| 819 | This overwrites all previously set snap positions and also a previously | - | ||||||||||||
| 820 | set snapping interval. | - | ||||||||||||
| 821 | Snapping can be deactivated by setting an empty list of positions. | - | ||||||||||||
| 822 | */ | - | ||||||||||||
| 823 | void QScroller::setSnapPositionsX(const QList<qreal> &positions) | - | ||||||||||||
| 824 | { | - | ||||||||||||
| 825 | Q_D(QScroller); | - | ||||||||||||
| 826 | d->snapPositionsX = positions; | - | ||||||||||||
| 827 | d->snapIntervalX = 0.0; | - | ||||||||||||
| 828 | - | |||||||||||||
| 829 | d->recalcScrollingSegments(); | - | ||||||||||||
| 830 | } | - | ||||||||||||
| 831 | - | |||||||||||||
| 832 | /*! Set the snap positions for the horizontal axis to regular spaced intervals. | - | ||||||||||||
| 833 | The first snap position is at \a first. The next at \a first + \a interval. | - | ||||||||||||
| 834 | This can be used to implement a list header. | - | ||||||||||||
| 835 | This overwrites all previously set snap positions and also a previously | - | ||||||||||||
| 836 | set snapping interval. | - | ||||||||||||
| 837 | Snapping can be deactivated by setting an interval of 0.0 | - | ||||||||||||
| 838 | */ | - | ||||||||||||
| 839 | void QScroller::setSnapPositionsX(qreal first, qreal interval) | - | ||||||||||||
| 840 | { | - | ||||||||||||
| 841 | Q_D(QScroller); | - | ||||||||||||
| 842 | d->snapFirstX = first; | - | ||||||||||||
| 843 | d->snapIntervalX = interval; | - | ||||||||||||
| 844 | d->snapPositionsX.clear(); | - | ||||||||||||
| 845 | - | |||||||||||||
| 846 | d->recalcScrollingSegments(); | - | ||||||||||||
| 847 | } | - | ||||||||||||
| 848 | - | |||||||||||||
| 849 | /*! Set the snap positions for the vertical axis to a list of \a positions. | - | ||||||||||||
| 850 | This overwrites all previously set snap positions and also a previously | - | ||||||||||||
| 851 | set snapping interval. | - | ||||||||||||
| 852 | Snapping can be deactivated by setting an empty list of positions. | - | ||||||||||||
| 853 | */ | - | ||||||||||||
| 854 | void QScroller::setSnapPositionsY(const QList<qreal> &positions) | - | ||||||||||||
| 855 | { | - | ||||||||||||
| 856 | Q_D(QScroller); | - | ||||||||||||
| 857 | d->snapPositionsY = positions; | - | ||||||||||||
| 858 | d->snapIntervalY = 0.0; | - | ||||||||||||
| 859 | - | |||||||||||||
| 860 | d->recalcScrollingSegments(); | - | ||||||||||||
| 861 | } | - | ||||||||||||
| 862 | - | |||||||||||||
| 863 | /*! Set the snap positions for the vertical axis to regular spaced intervals. | - | ||||||||||||
| 864 | The first snap position is at \a first. The next at \a first + \a interval. | - | ||||||||||||
| 865 | This overwrites all previously set snap positions and also a previously | - | ||||||||||||
| 866 | set snapping interval. | - | ||||||||||||
| 867 | Snapping can be deactivated by setting an interval of 0.0 | - | ||||||||||||
| 868 | */ | - | ||||||||||||
| 869 | void QScroller::setSnapPositionsY(qreal first, qreal interval) | - | ||||||||||||
| 870 | { | - | ||||||||||||
| 871 | Q_D(QScroller); | - | ||||||||||||
| 872 | d->snapFirstY = first; | - | ||||||||||||
| 873 | d->snapIntervalY = interval; | - | ||||||||||||
| 874 | d->snapPositionsY.clear(); | - | ||||||||||||
| 875 | - | |||||||||||||
| 876 | d->recalcScrollingSegments(); | - | ||||||||||||
| 877 | } | - | ||||||||||||
| 878 | - | |||||||||||||
| 879 | - | |||||||||||||
| 880 | - | |||||||||||||
| 881 | // -------------- private ------------ | - | ||||||||||||
| 882 | - | |||||||||||||
| 883 | QScrollerPrivate::QScrollerPrivate(QScroller *q, QObject *_target) | - | ||||||||||||
| 884 | : target(_target) | - | ||||||||||||
| 885 | #ifndef QT_NO_GESTURES | - | ||||||||||||
| 886 | , recognizer(0) | - | ||||||||||||
| 887 | , recognizerType(Qt::CustomGesture) | - | ||||||||||||
| 888 | #endif | - | ||||||||||||
| 889 | , state(QScroller::Inactive) | - | ||||||||||||
| 890 | , firstScroll(true) | - | ||||||||||||
| 891 | , pressTimestamp(0) | - | ||||||||||||
| 892 | , lastTimestamp(0) | - | ||||||||||||
| 893 | , snapFirstX(-1.0) | - | ||||||||||||
| 894 | , snapIntervalX(0.0) | - | ||||||||||||
| 895 | , snapFirstY(-1.0) | - | ||||||||||||
| 896 | , snapIntervalY(0.0) | - | ||||||||||||
| 897 | #ifndef QT_NO_ANIMATION | - | ||||||||||||
| 898 | , scrollTimer(new QScrollTimer(this)) | - | ||||||||||||
| 899 | #endif | - | ||||||||||||
| 900 | , q_ptr(q) | - | ||||||||||||
| 901 | { | - | ||||||||||||
| 902 | connect(target, SIGNAL(destroyed(QObject*)), this, SLOT(targetDestroyed())); | - | ||||||||||||
| 903 | } | - | ||||||||||||
| 904 | - | |||||||||||||
| 905 | void QScrollerPrivate::init() | - | ||||||||||||
| 906 | { | - | ||||||||||||
| 907 | setDpiFromWidget(0); | - | ||||||||||||
| 908 | monotonicTimer.start(); | - | ||||||||||||
| 909 | } | - | ||||||||||||
| 910 | - | |||||||||||||
| 911 | void QScrollerPrivate::sendEvent(QObject *o, QEvent *e) | - | ||||||||||||
| 912 | { | - | ||||||||||||
| 913 | qt_sendSpontaneousEvent(o, e); | - | ||||||||||||
| 914 | } | - | ||||||||||||
| 915 | - | |||||||||||||
| 916 | const char *QScrollerPrivate::stateName(QScroller::State state) | - | ||||||||||||
| 917 | { | - | ||||||||||||
| 918 | switch (state) { | - | ||||||||||||
| 919 | case QScroller::Inactive: return "inactive"; | - | ||||||||||||
| 920 | case QScroller::Pressed: return "pressed"; | - | ||||||||||||
| 921 | case QScroller::Dragging: return "dragging"; | - | ||||||||||||
| 922 | case QScroller::Scrolling: return "scrolling"; | - | ||||||||||||
| 923 | default: return "(invalid)"; | - | ||||||||||||
| 924 | } | - | ||||||||||||
| 925 | } | - | ||||||||||||
| 926 | - | |||||||||||||
| 927 | const char *QScrollerPrivate::inputName(QScroller::Input input) | - | ||||||||||||
| 928 | { | - | ||||||||||||
| 929 | switch (input) { | - | ||||||||||||
| 930 | case QScroller::InputPress: return "press"; | - | ||||||||||||
| 931 | case QScroller::InputMove: return "move"; | - | ||||||||||||
| 932 | case QScroller::InputRelease: return "release"; | - | ||||||||||||
| 933 | default: return "(invalid)"; | - | ||||||||||||
| 934 | } | - | ||||||||||||
| 935 | } | - | ||||||||||||
| 936 | - | |||||||||||||
| 937 | void QScrollerPrivate::targetDestroyed() | - | ||||||||||||
| 938 | { | - | ||||||||||||
| 939 | #ifndef QT_NO_ANIMATION | - | ||||||||||||
| 940 | scrollTimer->stop(); | - | ||||||||||||
| 941 | #endif | - | ||||||||||||
| 942 | delete q_ptr; | - | ||||||||||||
| 943 | } | - | ||||||||||||
| 944 | - | |||||||||||||
| 945 | void QScrollerPrivate::timerTick() | - | ||||||||||||
| 946 | { | - | ||||||||||||
| 947 | struct timerevent { | - | ||||||||||||
| 948 | QScroller::State state; | - | ||||||||||||
| 949 | typedef void (QScrollerPrivate::*timerhandler_t)(); | - | ||||||||||||
| 950 | timerhandler_t handler; | - | ||||||||||||
| 951 | }; | - | ||||||||||||
| 952 | - | |||||||||||||
| 953 | timerevent timerevents[] = { | - | ||||||||||||
| 954 | { QScroller::Dragging, &QScrollerPrivate::timerEventWhileDragging }, | - | ||||||||||||
| 955 | { QScroller::Scrolling, &QScrollerPrivate::timerEventWhileScrolling }, | - | ||||||||||||
| 956 | }; | - | ||||||||||||
| 957 | - | |||||||||||||
| 958 | for (int i = 0; i < int(sizeof(timerevents) / sizeof(*timerevents)); ++i) { | - | ||||||||||||
| 959 | timerevent *te = timerevents + i; | - | ||||||||||||
| 960 | - | |||||||||||||
| 961 | if (state == te->state) { | - | ||||||||||||
| 962 | (this->*te->handler)(); | - | ||||||||||||
| 963 | return; | - | ||||||||||||
| 964 | } | - | ||||||||||||
| 965 | } | - | ||||||||||||
| 966 | - | |||||||||||||
| 967 | #ifndef QT_NO_ANIMATION | - | ||||||||||||
| 968 | scrollTimer->stop(); | - | ||||||||||||
| 969 | #endif | - | ||||||||||||
| 970 | } | - | ||||||||||||
| 971 | - | |||||||||||||
| 972 | /*! | - | ||||||||||||
| 973 | This function is used by gesture recognizers to inform the scroller about a new input event. | - | ||||||||||||
| 974 | The scroller changes its internal state() according to the input event and its attached | - | ||||||||||||
| 975 | scroller properties. The scroller doesn't distinguish between the kind of input device the | - | ||||||||||||
| 976 | event came from. Therefore the event needs to be split into the \a input type, a \a position and a | - | ||||||||||||
| 977 | milli-second \a timestamp. The \a position needs to be in the target's coordinate system. | - | ||||||||||||
| 978 | - | |||||||||||||
| 979 | The return value is \c true if the event should be consumed by the calling filter or \c false | - | ||||||||||||
| 980 | if the event should be forwarded to the control. | - | ||||||||||||
| 981 | - | |||||||||||||
| 982 | \note Using grabGesture() should be sufficient for most use cases. | - | ||||||||||||
| 983 | */ | - | ||||||||||||
| 984 | bool QScroller::handleInput(Input input, const QPointF &position, qint64 timestamp) | - | ||||||||||||
| 985 | { | - | ||||||||||||
| 986 | Q_D(QScroller); | - | ||||||||||||
| 987 | - | |||||||||||||
| 988 | qScrollerDebug() << "QScroller::handleInput(" << input << ", " << d->stateName(d->state) << ", " << position << ", " << timestamp << ')'; dead code: QMessageLogger(__FILE__, 988, __PRETTY_FUNCTION__).debug() << "QScroller::handleInput(" << input << ", " << d->stateName(d->state) << ", " << position << ", " << timestamp << ')'; | - | ||||||||||||
| 989 | struct statechange { | - | ||||||||||||
| 990 | State state; | - | ||||||||||||
| 991 | Input input; | - | ||||||||||||
| 992 | typedef bool (QScrollerPrivate::*inputhandler_t)(const QPointF &position, qint64 timestamp); | - | ||||||||||||
| 993 | inputhandler_t handler; | - | ||||||||||||
| 994 | }; | - | ||||||||||||
| 995 | - | |||||||||||||
| 996 | statechange statechanges[] = { | - | ||||||||||||
| 997 | { QScroller::Inactive, InputPress, &QScrollerPrivate::pressWhileInactive }, | - | ||||||||||||
| 998 | { QScroller::Pressed, InputMove, &QScrollerPrivate::moveWhilePressed }, | - | ||||||||||||
| 999 | { QScroller::Pressed, InputRelease, &QScrollerPrivate::releaseWhilePressed }, | - | ||||||||||||
| 1000 | { QScroller::Dragging, InputMove, &QScrollerPrivate::moveWhileDragging }, | - | ||||||||||||
| 1001 | { QScroller::Dragging, InputRelease, &QScrollerPrivate::releaseWhileDragging }, | - | ||||||||||||
| 1002 | { QScroller::Scrolling, InputPress, &QScrollerPrivate::pressWhileScrolling } | - | ||||||||||||
| 1003 | }; | - | ||||||||||||
| 1004 | - | |||||||||||||
| 1005 | for (int i = 0; i < int(sizeof(statechanges) / sizeof(*statechanges)); ++i) { | - | ||||||||||||
| 1006 | statechange *sc = statechanges + i; | - | ||||||||||||
| 1007 | - | |||||||||||||
| 1008 | if (d->state == sc->state && input == sc->input) | - | ||||||||||||
| 1009 | return (d->*sc->handler)(position - d->overshootPosition, timestamp); | - | ||||||||||||
| 1010 | } | - | ||||||||||||
| 1011 | return false; | - | ||||||||||||
| 1012 | } | - | ||||||||||||
| 1013 | - | |||||||||||||
| 1014 | #if !defined(Q_DEAD_CODE_FROM_QT4_MAC) | - | ||||||||||||
| 1015 | // the Mac version is implemented in qscroller_mac.mm | - | ||||||||||||
| 1016 | - | |||||||||||||
| 1017 | QPointF QScrollerPrivate::realDpi(int screen) const | - | ||||||||||||
| 1018 | { | - | ||||||||||||
| 1019 | # if defined(Q_DEAD_CODE_FROM_QT4_X11) && !defined(QT_NO_XRANDR) | - | ||||||||||||
| 1020 | if (X11 && X11->use_xrandr && X11->ptrXRRSizes && X11->ptrXRRRootToScreen) { | - | ||||||||||||
| 1021 | int nsizes = 0; | - | ||||||||||||
| 1022 | // QDesktopWidget is based on Xinerama screens, which do not always | - | ||||||||||||
| 1023 | // correspond to RandR screens: NVidia's TwinView e.g. will show up | - | ||||||||||||
| 1024 | // as 2 screens in QDesktopWidget, but libXRandR will only see 1 screen. | - | ||||||||||||
| 1025 | // (although with the combined size of the Xinerama screens). | - | ||||||||||||
| 1026 | // Additionally, libXrandr will simply crash when calling XRRSizes | - | ||||||||||||
| 1027 | // for (the non-existent) screen 1 in this scenario. | - | ||||||||||||
| 1028 | Window root = RootWindow(X11->display, screen == -1 ? X11->defaultScreen : screen); | - | ||||||||||||
| 1029 | int randrscreen = (root != XNone) ? X11->ptrXRRRootToScreen(X11->display, root) : -1; | - | ||||||||||||
| 1030 | - | |||||||||||||
| 1031 | XRRScreenSize *sizes = X11->ptrXRRSizes(X11->display, randrscreen == -1 ? 0 : randrscreen, &nsizes); | - | ||||||||||||
| 1032 | if (nsizes > 0 && sizes && sizes->width && sizes->height && sizes->mwidth && sizes->mheight) { | - | ||||||||||||
| 1033 | qScrollerDebug() << "XRandR DPI:" << QPointF(qreal(25.4) * qreal(sizes->width) / qreal(sizes->mwidth), | - | ||||||||||||
| 1034 | qreal(25.4) * qreal(sizes->height) / qreal(sizes->mheight)); | - | ||||||||||||
| 1035 | return QPointF(qreal(25.4) * qreal(sizes->width) / qreal(sizes->mwidth), | - | ||||||||||||
| 1036 | qreal(25.4) * qreal(sizes->height) / qreal(sizes->mheight)); | - | ||||||||||||
| 1037 | } | - | ||||||||||||
| 1038 | } | - | ||||||||||||
| 1039 | # endif | - | ||||||||||||
| 1040 | - | |||||||||||||
| 1041 | QWidget *w = QApplication::desktop()->screen(screen); | - | ||||||||||||
| 1042 | return QPointF(w->physicalDpiX(), w->physicalDpiY()); never executed: return QPointF(w->physicalDpiX(), w->physicalDpiY()); | 0 | ||||||||||||
| 1043 | } | - | ||||||||||||
| 1044 | - | |||||||||||||
| 1045 | #endif // !Q_DEAD_CODE_FROM_QT4_MAC | - | ||||||||||||
| 1046 | - | |||||||||||||
| 1047 | - | |||||||||||||
| 1048 | /*! \internal | - | ||||||||||||
| 1049 | Returns the resolution of the used screen. | - | ||||||||||||
| 1050 | */ | - | ||||||||||||
| 1051 | QPointF QScrollerPrivate::dpi() const | - | ||||||||||||
| 1052 | { | - | ||||||||||||
| 1053 | return pixelPerMeter * qreal(0.0254); | - | ||||||||||||
| 1054 | } | - | ||||||||||||
| 1055 | - | |||||||||||||
| 1056 | /*! \internal | - | ||||||||||||
| 1057 | Sets the resolution used for scrolling. | - | ||||||||||||
| 1058 | This resolution is only used by the kinetic scroller. If you change this | - | ||||||||||||
| 1059 | then the scroller will behave quite different as a lot of the values are | - | ||||||||||||
| 1060 | given in physical distances (millimeter). | - | ||||||||||||
| 1061 | */ | - | ||||||||||||
| 1062 | void QScrollerPrivate::setDpi(const QPointF &dpi) | - | ||||||||||||
| 1063 | { | - | ||||||||||||
| 1064 | pixelPerMeter = dpi / qreal(0.0254); | - | ||||||||||||
| 1065 | } | - | ||||||||||||
| 1066 | - | |||||||||||||
| 1067 | /*! \internal | - | ||||||||||||
| 1068 | Sets the dpi used for scrolling to the value of the widget. | - | ||||||||||||
| 1069 | */ | - | ||||||||||||
| 1070 | void QScrollerPrivate::setDpiFromWidget(QWidget *widget) | - | ||||||||||||
| 1071 | { | - | ||||||||||||
| 1072 | QDesktopWidget *dw = QApplication::desktop(); | - | ||||||||||||
| 1073 | setDpi(realDpi(widget ? dw->screenNumber(widget) : dw->primaryScreen())); | - | ||||||||||||
| 1074 | } | - | ||||||||||||
| 1075 | - | |||||||||||||
| 1076 | /*! \internal | - | ||||||||||||
| 1077 | Updates the velocity during dragging. | - | ||||||||||||
| 1078 | Sets releaseVelocity. | - | ||||||||||||
| 1079 | */ | - | ||||||||||||
| 1080 | void QScrollerPrivate::updateVelocity(const QPointF &deltaPixelRaw, qint64 deltaTime) | - | ||||||||||||
| 1081 | { | - | ||||||||||||
| 1082 | if (deltaTime <= 0) | - | ||||||||||||
| 1083 | return; | - | ||||||||||||
| 1084 | - | |||||||||||||
| 1085 | Q_Q(QScroller); | - | ||||||||||||
| 1086 | QPointF ppm = q->pixelPerMeter(); | - | ||||||||||||
| 1087 | const QScrollerPropertiesPrivate *sp = properties.d.data(); | - | ||||||||||||
| 1088 | QPointF deltaPixel = deltaPixelRaw; | - | ||||||||||||
| 1089 | - | |||||||||||||
| 1090 | qScrollerDebug() << "QScroller::updateVelocity(" << deltaPixelRaw << " [delta pix], " << deltaTime << " [delta ms])"; dead code: QMessageLogger(__FILE__, 1090, __PRETTY_FUNCTION__).debug() << "QScroller::updateVelocity(" << deltaPixelRaw << " [delta pix], " << deltaTime << " [delta ms])"; | - | ||||||||||||
| 1091 | - | |||||||||||||
| 1092 | // faster than 2.5mm/ms seems bogus (that would be a screen height in ~20 ms) | - | ||||||||||||
| 1093 | if (((deltaPixelRaw / qreal(deltaTime)).manhattanLength() / ((ppm.x() + ppm.y()) / 2) * 1000) > qreal(2.5)) | - | ||||||||||||
| 1094 | deltaPixel = deltaPixelRaw * qreal(2.5) * ppm / 1000 / (deltaPixelRaw / qreal(deltaTime)).manhattanLength(); | - | ||||||||||||
| 1095 | - | |||||||||||||
| 1096 | QPointF newv = -deltaPixel / qreal(deltaTime) * qreal(1000) / ppm; | - | ||||||||||||
| 1097 | // around 95% of all updates are in the [1..50] ms range, so make sure | - | ||||||||||||
| 1098 | // to scale the smoothing factor over that range: this way a 50ms update | - | ||||||||||||
| 1099 | // will have full impact, while 5ms update will only have a 10% impact. | - | ||||||||||||
| 1100 | qreal smoothing = sp->dragVelocitySmoothingFactor * qMin(qreal(deltaTime), qreal(50)) / qreal(50); | - | ||||||||||||
| 1101 | - | |||||||||||||
| 1102 | // only smooth if we already have a release velocity and only if the | - | ||||||||||||
| 1103 | // user hasn't stopped to move his finger for more than 100ms | - | ||||||||||||
| 1104 | if ((releaseVelocity != QPointF(0, 0)) && (deltaTime < 100)) { | - | ||||||||||||
| 1105 | qScrollerDebug() << "SMOOTHED from " << newv << " to " << newv * smoothing + releaseVelocity * (qreal(1) - smoothing); dead code: QMessageLogger(__FILE__, 1105, __PRETTY_FUNCTION__).debug() << "SMOOTHED from " << newv << " to " << newv * smoothing + releaseVelocity * (qreal(1) - smoothing); | - | ||||||||||||
| 1106 | // smooth x or y only if the new velocity is either 0 or at least in | - | ||||||||||||
| 1107 | // the same direction of the release velocity | - | ||||||||||||
| 1108 | if (!newv.x() || (qSign(releaseVelocity.x()) == qSign(newv.x()))) | - | ||||||||||||
| 1109 | newv.setX(newv.x() * smoothing + releaseVelocity.x() * (qreal(1) - smoothing)); | - | ||||||||||||
| 1110 | if (!newv.y() || (qSign(releaseVelocity.y()) == qSign(newv.y()))) | - | ||||||||||||
| 1111 | newv.setY(newv.y() * smoothing + releaseVelocity.y() * (qreal(1) - smoothing)); | - | ||||||||||||
| 1112 | } else | - | ||||||||||||
| 1113 | qScrollerDebug() << "NO SMOOTHING to " << newv; dead code: QMessageLogger(__FILE__, 1113, __PRETTY_FUNCTION__).debug() << "NO SMOOTHING to " << newv; | - | ||||||||||||
| 1114 | - | |||||||||||||
| 1115 | releaseVelocity.setX(qBound(-sp->maximumVelocity, newv.x(), sp->maximumVelocity)); | - | ||||||||||||
| 1116 | releaseVelocity.setY(qBound(-sp->maximumVelocity, newv.y(), sp->maximumVelocity)); | - | ||||||||||||
| 1117 | - | |||||||||||||
| 1118 | qScrollerDebug() << " --> new velocity:" << releaseVelocity; dead code: QMessageLogger(__FILE__, 1118, __PRETTY_FUNCTION__).debug() << " --> new velocity:" << releaseVelocity; | - | ||||||||||||
| 1119 | } | - | ||||||||||||
| 1120 | - | |||||||||||||
| 1121 | void QScrollerPrivate::pushSegment(ScrollType type, qreal deltaTime, qreal stopProgress, qreal startPos, qreal deltaPos, qreal stopPos, QEasingCurve::Type curve, Qt::Orientation orientation) | - | ||||||||||||
| 1122 | { | - | ||||||||||||
| 1123 | if (startPos == stopPos || deltaPos == 0)
| 0 | ||||||||||||
| 1124 | return; never executed: return; | 0 | ||||||||||||
| 1125 | - | |||||||||||||
| 1126 | ScrollSegment s; | - | ||||||||||||
| 1127 | if (orientation == Qt::Horizontal && !xSegments.isEmpty()) {
| 0 | ||||||||||||
| 1128 | const auto &lastX = xSegments.constLast(); | - | ||||||||||||
| 1129 | s.startTime = xSegmentslastX.last().startTime + xSegmentslastX.last().deltaTime * xSegmentslastX.last().stopProgress; | - | ||||||||||||
| 1130 | } else if (orientation == Qt::Vertical && !ySegments.isEmpty()) { never executed: end of block
| 0 | ||||||||||||
| 1131 | const auto &lastY = ySegments.constLast(); | - | ||||||||||||
| 1132 | s.startTime = ySegmentslastY.last().startTime + ySegmentslastY.last().deltaTime * ySegmentslastY.last().stopProgress; | - | ||||||||||||
| 1133 | } else { never executed: end of block | 0 | ||||||||||||
| 1134 | s.startTime = monotonicTimer.elapsed(); | - | ||||||||||||
| 1135 | } never executed: end of block | 0 | ||||||||||||
| 1136 | - | |||||||||||||
| 1137 | s.startPos = startPos; | - | ||||||||||||
| 1138 | s.deltaPos = deltaPos; | - | ||||||||||||
| 1139 | s.stopPos = stopPos; | - | ||||||||||||
| 1140 | s.deltaTime = deltaTime * 1000; | - | ||||||||||||
| 1141 | s.stopProgress = stopProgress; | - | ||||||||||||
| 1142 | s.curve.setType(curve); | - | ||||||||||||
| 1143 | s.type = type; | - | ||||||||||||
| 1144 | - | |||||||||||||
| 1145 | if (orientation == Qt::Horizontal)
| 0 | ||||||||||||
| 1146 | xSegments.enqueue(s); never executed: xSegments.enqueue(s); | 0 | ||||||||||||
| 1147 | else | - | ||||||||||||
| 1148 | ySegments.enqueue(s); never executed: ySegments.enqueue(s); | 0 | ||||||||||||
| 1149 | - | |||||||||||||
| 1150 | qScrollerDebug() << "+++ Added a new ScrollSegment: " << s; dead code: QMessageLogger(__FILE__, 1150, __PRETTY_FUNCTION__).debug() << "+++ Added a new ScrollSegment: " << s; | - | ||||||||||||
| 1151 | } never executed: end of block | 0 | ||||||||||||
| 1152 | - | |||||||||||||
| 1153 | - | |||||||||||||
| 1154 | /*! \internal | - | ||||||||||||
| 1155 | Clears the old segments and recalculates them if the current segments are not longer valid | - | ||||||||||||
| 1156 | */ | - | ||||||||||||
| 1157 | void QScrollerPrivate::recalcScrollingSegments(bool forceRecalc) | - | ||||||||||||
| 1158 | { | - | ||||||||||||
| 1159 | Q_Q(QScroller); | - | ||||||||||||
| 1160 | QPointF ppm = q->pixelPerMeter(); | - | ||||||||||||
| 1161 | - | |||||||||||||
| 1162 | releaseVelocity = q->velocity(); | - | ||||||||||||
| 1163 | - | |||||||||||||
| 1164 | if (forceRecalc || | - | ||||||||||||
| 1165 | !scrollingSegmentsValid(Qt::Horizontal) || | - | ||||||||||||
| 1166 | !scrollingSegmentsValid(Qt::Vertical)) | - | ||||||||||||
| 1167 | createScrollingSegments(releaseVelocity, contentPosition + overshootPosition, ppm); | - | ||||||||||||
| 1168 | } | - | ||||||||||||
| 1169 | - | |||||||||||||
| 1170 | /*! \internal | - | ||||||||||||
| 1171 | Returns the end position after the current scroll has finished. | - | ||||||||||||
| 1172 | */ | - | ||||||||||||
| 1173 | qreal QScrollerPrivate::scrollingSegmentsEndPos(Qt::Orientation orientation) const | - | ||||||||||||
| 1174 | { | - | ||||||||||||
| 1175 | if (orientation == Qt::Horizontal) { | - | ||||||||||||
| 1176 | if (xSegments.isEmpty()) | - | ||||||||||||
| 1177 | return contentPosition.x() + overshootPosition.x(); | - | ||||||||||||
| 1178 | else | - | ||||||||||||
| 1179 | return xSegments.last().stopPos; | - | ||||||||||||
| 1180 | } else { | - | ||||||||||||
| 1181 | if (ySegments.isEmpty()) | - | ||||||||||||
| 1182 | return contentPosition.y() + overshootPosition.y(); | - | ||||||||||||
| 1183 | else | - | ||||||||||||
| 1184 | return ySegments.last().stopPos; | - | ||||||||||||
| 1185 | } | - | ||||||||||||
| 1186 | } | - | ||||||||||||
| 1187 | - | |||||||||||||
| 1188 | /*! \internal | - | ||||||||||||
| 1189 | Checks if the scroller segment end in a valid position. | - | ||||||||||||
| 1190 | */ | - | ||||||||||||
| 1191 | bool QScrollerPrivate::scrollingSegmentsValid(Qt::Orientation orientation) const | - | ||||||||||||
| 1192 | { | - | ||||||||||||
| 1193 | const QQueue<ScrollSegment> *segments; | - | ||||||||||||
| 1194 | qreal minPos; | - | ||||||||||||
| 1195 | qreal maxPos; | - | ||||||||||||
| 1196 | - | |||||||||||||
| 1197 | if (orientation == Qt::Horizontal) {
| 0 | ||||||||||||
| 1198 | segments = &xSegments; | - | ||||||||||||
| 1199 | minPos = contentPosRange.left(); | - | ||||||||||||
| 1200 | maxPos = contentPosRange.right(); | - | ||||||||||||
| 1201 | } else { never executed: end of block | 0 | ||||||||||||
| 1202 | segments = &ySegments; | - | ||||||||||||
| 1203 | minPos = contentPosRange.top(); | - | ||||||||||||
| 1204 | maxPos = contentPosRange.bottom(); | - | ||||||||||||
| 1205 | } never executed: end of block | 0 | ||||||||||||
| 1206 | - | |||||||||||||
| 1207 | if (segments->isEmpty())
| 0 | ||||||||||||
| 1208 | return true; never executed: return true; | 0 | ||||||||||||
| 1209 | - | |||||||||||||
| 1210 | const ScrollSegment &last = segments->last(); | - | ||||||||||||
| 1211 | qreal stopPos = last.stopPos; | - | ||||||||||||
| 1212 | - | |||||||||||||
| 1213 | if (last.type == ScrollTypeScrollTo)
| 0 | ||||||||||||
| 1214 | return true; // scrollTo is always valid never executed: return true; | 0 | ||||||||||||
| 1215 | - | |||||||||||||
| 1216 | if (last.type == ScrollTypeOvershoot &&
| 0 | ||||||||||||
| 1217 | (stopPos != minPos && stopPos != maxPos))
| 0 | ||||||||||||
| 1218 | return false; never executed: return false; | 0 | ||||||||||||
| 1219 | - | |||||||||||||
| 1220 | if (stopPos < minPos || stopPos > maxPos)
| 0 | ||||||||||||
| 1221 | return false; never executed: return false; | 0 | ||||||||||||
| 1222 | - | |||||||||||||
| 1223 | if (stopPos == minPos || stopPos == maxPos) // the begin and the end of the list are always ok
| 0 | ||||||||||||
| 1224 | return true; never executed: return true; | 0 | ||||||||||||
| 1225 | - | |||||||||||||
| 1226 | qreal nextSnap = nextSnapPos(stopPos, 0, orientation); | - | ||||||||||||
| 1227 | if (!qIsNaN(nextSnap) && stopPos != nextSnap)
| 0 | ||||||||||||
| 1228 | return false; never executed: return false; | 0 | ||||||||||||
| 1229 | - | |||||||||||||
| 1230 | return true; never executed: return true; | 0 | ||||||||||||
| 1231 | } | - | ||||||||||||
| 1232 | - | |||||||||||||
| 1233 | /*! \internal | - | ||||||||||||
| 1234 | Creates the sections needed to scroll to the specific \a endPos to the segments queue. | - | ||||||||||||
| 1235 | */ | - | ||||||||||||
| 1236 | void QScrollerPrivate::createScrollToSegments(qreal v, qreal deltaTime, qreal endPos, Qt::Orientation orientation, ScrollType type) | - | ||||||||||||
| 1237 | { | - | ||||||||||||
| 1238 | Q_UNUSED(v); | - | ||||||||||||
| 1239 | - | |||||||||||||
| 1240 | if (orientation == Qt::Horizontal) | - | ||||||||||||
| 1241 | xSegments.clear(); | - | ||||||||||||
| 1242 | else | - | ||||||||||||
| 1243 | ySegments.clear(); | - | ||||||||||||
| 1244 | - | |||||||||||||
| 1245 | qScrollerDebug() << "+++ createScrollToSegments: t:" << deltaTime << "ep:" << endPos << "o:" << int(orientation); dead code: QMessageLogger(__FILE__, 1245, __PRETTY_FUNCTION__).debug() << "+++ createScrollToSegments: t:" << deltaTime << "ep:" << endPos << "o:" << int(orientation); | - | ||||||||||||
| 1246 | - | |||||||||||||
| 1247 | const QScrollerPropertiesPrivate *sp = properties.d.data(); | - | ||||||||||||
| 1248 | - | |||||||||||||
| 1249 | qreal startPos = (orientation == Qt::Horizontal) ? contentPosition.x() + overshootPosition.x() | - | ||||||||||||
| 1250 | : contentPosition.y() + overshootPosition.y(); | - | ||||||||||||
| 1251 | qreal deltaPos = (endPos - startPos) / 2; | - | ||||||||||||
| 1252 | - | |||||||||||||
| 1253 | pushSegment(type, deltaTime * qreal(0.3), qreal(1.0), startPos, deltaPos, startPos + deltaPos, QEasingCurve::InQuad, orientation); | - | ||||||||||||
| 1254 | pushSegment(type, deltaTime * qreal(0.7), qreal(1.0), startPos + deltaPos, deltaPos, endPos, sp->scrollingCurve.type(), orientation); | - | ||||||||||||
| 1255 | } | - | ||||||||||||
| 1256 | - | |||||||||||||
| 1257 | /*! \internal | - | ||||||||||||
| 1258 | */ | - | ||||||||||||
| 1259 | void QScrollerPrivate::createScrollingSegments(qreal v, qreal startPos, | - | ||||||||||||
| 1260 | qreal deltaTime, qreal deltaPos, | - | ||||||||||||
| 1261 | Qt::Orientation orientation) | - | ||||||||||||
| 1262 | { | - | ||||||||||||
| 1263 | const QScrollerPropertiesPrivate *sp = properties.d.data(); | - | ||||||||||||
| 1264 | - | |||||||||||||
| 1265 | QScrollerProperties::OvershootPolicy policy; | - | ||||||||||||
| 1266 | qreal minPos; | - | ||||||||||||
| 1267 | qreal maxPos; | - | ||||||||||||
| 1268 | qreal viewSize; | - | ||||||||||||
| 1269 | - | |||||||||||||
| 1270 | if (orientation == Qt::Horizontal) { | - | ||||||||||||
| 1271 | xSegments.clear(); | - | ||||||||||||
| 1272 | policy = sp->hOvershootPolicy; | - | ||||||||||||
| 1273 | minPos = contentPosRange.left(); | - | ||||||||||||
| 1274 | maxPos = contentPosRange.right(); | - | ||||||||||||
| 1275 | viewSize = viewportSize.width(); | - | ||||||||||||
| 1276 | } else { | - | ||||||||||||
| 1277 | ySegments.clear(); | - | ||||||||||||
| 1278 | policy = sp->vOvershootPolicy; | - | ||||||||||||
| 1279 | minPos = contentPosRange.top(); | - | ||||||||||||
| 1280 | maxPos = contentPosRange.bottom(); | - | ||||||||||||
| 1281 | viewSize = viewportSize.height(); | - | ||||||||||||
| 1282 | } | - | ||||||||||||
| 1283 | - | |||||||||||||
| 1284 | bool alwaysOvershoot = (policy == QScrollerProperties::OvershootAlwaysOn); | - | ||||||||||||
| 1285 | bool noOvershoot = (policy == QScrollerProperties::OvershootAlwaysOff) || !sp->overshootScrollDistanceFactor; | - | ||||||||||||
| 1286 | bool canOvershoot = !noOvershoot && (alwaysOvershoot || maxPos); | - | ||||||||||||
| 1287 | - | |||||||||||||
| 1288 | qScrollerDebug() << "+++ createScrollingSegments: s:" << startPos << "maxPos:" << maxPos << "o:" << int(orientation); dead code: QMessageLogger(__FILE__, 1288, __PRETTY_FUNCTION__).debug() << "+++ createScrollingSegments: s:" << startPos << "maxPos:" << maxPos << "o:" << int(orientation); | - | ||||||||||||
| 1289 | - | |||||||||||||
| 1290 | qScrollerDebug() << "v = " << v << ", decelerationFactor = " << sp->decelerationFactor << ", curveType = " << sp->scrollingCurve.type(); dead code: QMessageLogger(__FILE__, 1290, __PRETTY_FUNCTION__).debug() << "v = " << v << ", decelerationFactor = " << sp->decelerationFactor << ", curveType = " << sp->scrollingCurve.type(); | - | ||||||||||||
| 1291 | - | |||||||||||||
| 1292 | qreal endPos = startPos + deltaPos; | - | ||||||||||||
| 1293 | - | |||||||||||||
| 1294 | qScrollerDebug() << " Real Delta:" << deltaPos; dead code: QMessageLogger(__FILE__, 1294, __PRETTY_FUNCTION__).debug() << " Real Delta:" << deltaPos; | - | ||||||||||||
| 1295 | - | |||||||||||||
| 1296 | // -- check if are in overshoot and end in overshoot | - | ||||||||||||
| 1297 | if ((startPos < minPos && endPos < minPos) || | - | ||||||||||||
| 1298 | (startPos > maxPos && endPos > maxPos)) { | - | ||||||||||||
| 1299 | qreal stopPos = endPos < minPos ? minPos : maxPos; | - | ||||||||||||
| 1300 | qreal oDeltaTime = sp->overshootScrollTime; | - | ||||||||||||
| 1301 | - | |||||||||||||
| 1302 | pushSegment(ScrollTypeOvershoot, oDeltaTime * qreal(0.7), qreal(1.0), startPos, stopPos - startPos, stopPos, sp->scrollingCurve.type(), orientation); | - | ||||||||||||
| 1303 | return; | - | ||||||||||||
| 1304 | } | - | ||||||||||||
| 1305 | - | |||||||||||||
| 1306 | // -- determine snap points | - | ||||||||||||
| 1307 | qreal nextSnap = nextSnapPos(endPos, 0, orientation); | - | ||||||||||||
| 1308 | qreal lowerSnapPos = nextSnapPos(startPos, -1, orientation); | - | ||||||||||||
| 1309 | qreal higherSnapPos = nextSnapPos(startPos, 1, orientation); | - | ||||||||||||
| 1310 | - | |||||||||||||
| 1311 | qScrollerDebug() << " Real Delta:" << lowerSnapPos << '-' << nextSnap << '-' <<higherSnapPos; dead code: QMessageLogger(__FILE__, 1311, __PRETTY_FUNCTION__).debug() << " Real Delta:" << lowerSnapPos << '-' << nextSnap << '-' <<higherSnapPos; | - | ||||||||||||
| 1312 | - | |||||||||||||
| 1313 | // - check if we can reach another snap point | - | ||||||||||||
| 1314 | if (nextSnap > higherSnapPos || qIsNaN(higherSnapPos)) | - | ||||||||||||
| 1315 | higherSnapPos = nextSnap; | - | ||||||||||||
| 1316 | if (nextSnap < lowerSnapPos || qIsNaN(lowerSnapPos)) | - | ||||||||||||
| 1317 | lowerSnapPos = nextSnap; | - | ||||||||||||
| 1318 | - | |||||||||||||
| 1319 | if (qAbs(v) < sp->minimumVelocity) { | - | ||||||||||||
| 1320 | - | |||||||||||||
| 1321 | qScrollerDebug() << "### below minimum Vel" << orientation; dead code: QMessageLogger(__FILE__, 1321, __PRETTY_FUNCTION__).debug() << "### below minimum Vel" << orientation; | - | ||||||||||||
| 1322 | - | |||||||||||||
| 1323 | // - no snap points or already at one | - | ||||||||||||
| 1324 | if (qIsNaN(nextSnap) || nextSnap == startPos) | - | ||||||||||||
| 1325 | return; // nothing to do, no scrolling needed. | - | ||||||||||||
| 1326 | - | |||||||||||||
| 1327 | // - decide which point to use | - | ||||||||||||
| 1328 | - | |||||||||||||
| 1329 | qreal snapDistance = higherSnapPos - lowerSnapPos; | - | ||||||||||||
| 1330 | - | |||||||||||||
| 1331 | qreal pressDistance = (orientation == Qt::Horizontal) ? | - | ||||||||||||
| 1332 | lastPosition.x() - pressPosition.x() : | - | ||||||||||||
| 1333 | lastPosition.y() - pressPosition.y(); | - | ||||||||||||
| 1334 | - | |||||||||||||
| 1335 | // if not dragged far enough, pick the next snap point. | - | ||||||||||||
| 1336 | if (sp->snapPositionRatio == 0.0 || qAbs(pressDistance / sp->snapPositionRatio) > snapDistance) | - | ||||||||||||
| 1337 | endPos = nextSnap; | - | ||||||||||||
| 1338 | else if (pressDistance < 0.0) | - | ||||||||||||
| 1339 | endPos = lowerSnapPos; | - | ||||||||||||
| 1340 | else | - | ||||||||||||
| 1341 | endPos = higherSnapPos; | - | ||||||||||||
| 1342 | - | |||||||||||||
| 1343 | deltaPos = endPos - startPos; | - | ||||||||||||
| 1344 | qreal midPos = startPos + deltaPos * qreal(0.3); | - | ||||||||||||
| 1345 | pushSegment(ScrollTypeFlick, sp->snapTime * qreal(0.3), qreal(1.0), startPos, midPos - startPos, midPos, QEasingCurve::InQuad, orientation); | - | ||||||||||||
| 1346 | pushSegment(ScrollTypeFlick, sp->snapTime * qreal(0.7), qreal(1.0), midPos, endPos - midPos, endPos, sp->scrollingCurve.type(), orientation); | - | ||||||||||||
| 1347 | return; | - | ||||||||||||
| 1348 | } | - | ||||||||||||
| 1349 | - | |||||||||||||
| 1350 | // - go to the next snappoint if there is one | - | ||||||||||||
| 1351 | if (v > 0 && !qIsNaN(higherSnapPos)) { | - | ||||||||||||
| 1352 | // change the time in relation to the changed end position | - | ||||||||||||
| 1353 | if (endPos - startPos) | - | ||||||||||||
| 1354 | deltaTime *= qAbs((higherSnapPos - startPos) / (endPos - startPos)); | - | ||||||||||||
| 1355 | if (deltaTime > sp->snapTime) | - | ||||||||||||
| 1356 | deltaTime = sp->snapTime; | - | ||||||||||||
| 1357 | endPos = higherSnapPos; | - | ||||||||||||
| 1358 | - | |||||||||||||
| 1359 | } else if (v < 0 && !qIsNaN(lowerSnapPos)) { | - | ||||||||||||
| 1360 | // change the time in relation to the changed end position | - | ||||||||||||
| 1361 | if (endPos - startPos) | - | ||||||||||||
| 1362 | deltaTime *= qAbs((lowerSnapPos - startPos) / (endPos - startPos)); | - | ||||||||||||
| 1363 | if (deltaTime > sp->snapTime) | - | ||||||||||||
| 1364 | deltaTime = sp->snapTime; | - | ||||||||||||
| 1365 | endPos = lowerSnapPos; | - | ||||||||||||
| 1366 | - | |||||||||||||
| 1367 | // -- check if we are overshooting | - | ||||||||||||
| 1368 | } else if (endPos < minPos || endPos > maxPos) { | - | ||||||||||||
| 1369 | qreal stopPos = endPos < minPos ? minPos : maxPos; | - | ||||||||||||
| 1370 | - | |||||||||||||
| 1371 | qScrollerDebug() << "Overshoot: delta:" << (stopPos - startPos); dead code: QMessageLogger(__FILE__, 1371, __PRETTY_FUNCTION__).debug() << "Overshoot: delta:" << (stopPos - startPos); | - | ||||||||||||
| 1372 | - | |||||||||||||
| 1373 | qreal stopProgress = progressForValue(sp->scrollingCurve, qAbs((stopPos - startPos) / deltaPos)); | - | ||||||||||||
| 1374 | - | |||||||||||||
| 1375 | if (!canOvershoot) { | - | ||||||||||||
| 1376 | qScrollerDebug() << "Overshoot stopp:" << stopProgress; dead code: QMessageLogger(__FILE__, 1376, __PRETTY_FUNCTION__).debug() << "Overshoot stopp:" << stopProgress; | - | ||||||||||||
| 1377 | - | |||||||||||||
| 1378 | pushSegment(ScrollTypeFlick, deltaTime, stopProgress, startPos, endPos, stopPos, sp->scrollingCurve.type(), orientation); | - | ||||||||||||
| 1379 | } else { | - | ||||||||||||
| 1380 | qreal oDeltaTime = sp->overshootScrollTime; | - | ||||||||||||
| 1381 | qreal oStopProgress = qMin(stopProgress + oDeltaTime * qreal(0.3) / deltaTime, qreal(1)); | - | ||||||||||||
| 1382 | qreal oDistance = startPos + deltaPos * sp->scrollingCurve.valueForProgress(oStopProgress) - stopPos; | - | ||||||||||||
| 1383 | qreal oMaxDistance = qSign(oDistance) * (viewSize * sp->overshootScrollDistanceFactor); | - | ||||||||||||
| 1384 | - | |||||||||||||
| 1385 | qScrollerDebug() << "1 oDistance:" << oDistance << "Max:" << oMaxDistance << "stopP/oStopP" << stopProgress << oStopProgress; dead code: QMessageLogger(__FILE__, 1385, __PRETTY_FUNCTION__).debug() << "1 oDistance:" << oDistance << "Max:" << oMaxDistance << "stopP/oStopP" << stopProgress << oStopProgress; | - | ||||||||||||
| 1386 | - | |||||||||||||
| 1387 | if (qAbs(oDistance) > qAbs(oMaxDistance)) { | - | ||||||||||||
| 1388 | oStopProgress = progressForValue(sp->scrollingCurve, qAbs((stopPos + oMaxDistance - startPos) / deltaPos)); | - | ||||||||||||
| 1389 | oDistance = oMaxDistance; | - | ||||||||||||
| 1390 | qScrollerDebug() << "2 oDistance:" << oDistance << "Max:" << oMaxDistance << "stopP/oStopP" << stopProgress << oStopProgress; dead code: QMessageLogger(__FILE__, 1390, __PRETTY_FUNCTION__).debug() << "2 oDistance:" << oDistance << "Max:" << oMaxDistance << "stopP/oStopP" << stopProgress << oStopProgress; | - | ||||||||||||
| 1391 | } | - | ||||||||||||
| 1392 | - | |||||||||||||
| 1393 | pushSegment(ScrollTypeFlick, deltaTime, oStopProgress, startPos, deltaPos, stopPos + oDistance, sp->scrollingCurve.type(), orientation); | - | ||||||||||||
| 1394 | pushSegment(ScrollTypeOvershoot, oDeltaTime * qreal(0.7), qreal(1.0), stopPos + oDistance, -oDistance, stopPos, sp->scrollingCurve.type(), orientation); | - | ||||||||||||
| 1395 | } | - | ||||||||||||
| 1396 | return; | - | ||||||||||||
| 1397 | } | - | ||||||||||||
| 1398 | - | |||||||||||||
| 1399 | pushSegment(ScrollTypeFlick, deltaTime, qreal(1.0), startPos, deltaPos, endPos, sp->scrollingCurve.type(), orientation); | - | ||||||||||||
| 1400 | } | - | ||||||||||||
| 1401 | - | |||||||||||||
| 1402 | - | |||||||||||||
| 1403 | void QScrollerPrivate::createScrollingSegments(const QPointF &v, | - | ||||||||||||
| 1404 | const QPointF &startPos, | - | ||||||||||||
| 1405 | const QPointF &ppm) | - | ||||||||||||
| 1406 | { | - | ||||||||||||
| 1407 | const QScrollerPropertiesPrivate *sp = properties.d.data(); | - | ||||||||||||
| 1408 | - | |||||||||||||
| 1409 | // This is only correct for QEasingCurve::OutQuad (linear velocity, | - | ||||||||||||
| 1410 | // constant deceleration), but the results look and feel ok for OutExpo | - | ||||||||||||
| 1411 | // and OutSine as well | - | ||||||||||||
| 1412 | - | |||||||||||||
| 1413 | // v(t) = deltaTime * a * 0.5 * differentialForProgress(t / deltaTime) | - | ||||||||||||
| 1414 | // v(0) = vrelease | - | ||||||||||||
| 1415 | // v(deltaTime) = 0 | - | ||||||||||||
| 1416 | // deltaTime = (2 * vrelease) / (a * differntial(0)) | - | ||||||||||||
| 1417 | - | |||||||||||||
| 1418 | // pos(t) = integrate(v(t)dt) | - | ||||||||||||
| 1419 | // pos(t) = vrelease * t - 0.5 * a * t * t | - | ||||||||||||
| 1420 | // pos(t) = deltaTime * a * 0.5 * progress(t / deltaTime) * deltaTime | - | ||||||||||||
| 1421 | // deltaPos = pos(deltaTime) | - | ||||||||||||
| 1422 | - | |||||||||||||
| 1423 | QVector2D vel(v); | - | ||||||||||||
| 1424 | qreal deltaTime = (qreal(2) * vel.length()) / (sp->decelerationFactor * differentialForProgress(sp->scrollingCurve, 0)); | - | ||||||||||||
| 1425 | QPointF deltaPos = (vel.normalized() * QVector2D(ppm)).toPointF() * deltaTime * deltaTime * qreal(0.5) * sp->decelerationFactor; | - | ||||||||||||
| 1426 | - | |||||||||||||
| 1427 | createScrollingSegments(v.x(), startPos.x(), deltaTime, deltaPos.x(), | - | ||||||||||||
| 1428 | Qt::Horizontal); | - | ||||||||||||
| 1429 | createScrollingSegments(v.y(), startPos.y(), deltaTime, deltaPos.y(), | - | ||||||||||||
| 1430 | Qt::Vertical); | - | ||||||||||||
| 1431 | } | - | ||||||||||||
| 1432 | - | |||||||||||||
| 1433 | /*! \internal | - | ||||||||||||
| 1434 | Prepares scrolling by sending a QScrollPrepareEvent to the receiver widget. | - | ||||||||||||
| 1435 | Returns \c true if the scrolling was accepted and a target was returned. | - | ||||||||||||
| 1436 | */ | - | ||||||||||||
| 1437 | bool QScrollerPrivate::prepareScrolling(const QPointF &position) | - | ||||||||||||
| 1438 | { | - | ||||||||||||
| 1439 | QScrollPrepareEvent spe(position); | - | ||||||||||||
| 1440 | spe.ignore(); | - | ||||||||||||
| 1441 | sendEvent(target, &spe); | - | ||||||||||||
| 1442 | - | |||||||||||||
| 1443 | qScrollerDebug() << "QScrollPrepareEvent returned from" << target << "with" << spe.isAccepted() << "mcp:" << spe.contentPosRange() << "cp:" << spe.contentPos(); dead code: QMessageLogger(__FILE__, 1443, __PRETTY_FUNCTION__).debug() << "QScrollPrepareEvent returned from" << target << "with" << spe.isAccepted() << "mcp:" << spe.contentPosRange() << "cp:" << spe.contentPos(); | - | ||||||||||||
| 1444 | if (spe.isAccepted()) {
| 0 | ||||||||||||
| 1445 | QPointF oldContentPos = contentPosition + overshootPosition; | - | ||||||||||||
| 1446 | QPointF contentDelta = spe.contentPos() - oldContentPos; | - | ||||||||||||
| 1447 | - | |||||||||||||
| 1448 | viewportSize = spe.viewportSize(); | - | ||||||||||||
| 1449 | contentPosRange = spe.contentPosRange(); | - | ||||||||||||
| 1450 | if (contentPosRange.width() < 0)
| 0 | ||||||||||||
| 1451 | contentPosRange.setWidth(0); never executed: contentPosRange.setWidth(0); | 0 | ||||||||||||
| 1452 | if (contentPosRange.height() < 0)
| 0 | ||||||||||||
| 1453 | contentPosRange.setHeight(0); never executed: contentPosRange.setHeight(0); | 0 | ||||||||||||
| 1454 | contentPosition = clampToRect(spe.contentPos(), contentPosRange); | - | ||||||||||||
| 1455 | overshootPosition = spe.contentPos() - contentPosition; | - | ||||||||||||
| 1456 | - | |||||||||||||
| 1457 | // - check if the content position was moved | - | ||||||||||||
| 1458 | if (contentDelta != QPointF(0, 0)) {
| 0 | ||||||||||||
| 1459 | // need to correct all segments | - | ||||||||||||
| 1460 | for (int i = 0; i < xSegments.count(); i++)
| 0 | ||||||||||||
| 1461 | xSegments[i].startPos -= contentDelta.x(); never executed: xSegments[i].startPos -= contentDelta.x(); | 0 | ||||||||||||
| 1462 | - | |||||||||||||
| 1463 | for (int i = 0; i < ySegments.count(); i++)
| 0 | ||||||||||||
| 1464 | ySegments[i].startPos -= contentDelta.y(); never executed: ySegments[i].startPos -= contentDelta.y(); | 0 | ||||||||||||
| 1465 | } never executed: end of block | 0 | ||||||||||||
| 1466 | - | |||||||||||||
| 1467 | if (QWidget *w = qobject_cast<QWidget *>(target))
| 0 | ||||||||||||
| 1468 | setDpiFromWidget(w); never executed: setDpiFromWidget(w); | 0 | ||||||||||||
| 1469 | #ifndef QT_NO_GRAPHICSVIEW | - | ||||||||||||
| 1470 | if (QGraphicsObject *go = qobject_cast<QGraphicsObject *>(target)) {
| 0 | ||||||||||||
| 1471 | //TODO: the first view isn't really correct - maybe use an additional field in the prepare event? | - | ||||||||||||
| 1472 | if (go->const auto *scene () && != go->scene()->()) {
| 0 | ||||||||||||
| 1473 | const auto views ().= scene->views(); | - | ||||||||||||
| 1474 | if (!views.
| 0 | ||||||||||||
| 1475 | setDpiFromWidget(go->scene()->views()..first()); never executed: setDpiFromWidget(views.first()); | 0 | ||||||||||||
| 1476 | } never executed: end of block | 0 | ||||||||||||
| 1477 | } never executed: end of block | 0 | ||||||||||||
| 1478 | #endif | - | ||||||||||||
| 1479 | - | |||||||||||||
| 1480 | if (state == QScroller::Scrolling) {
| 0 | ||||||||||||
| 1481 | recalcScrollingSegments(); | - | ||||||||||||
| 1482 | } never executed: end of block | 0 | ||||||||||||
| 1483 | return true; never executed: return true; | 0 | ||||||||||||
| 1484 | } | - | ||||||||||||
| 1485 | - | |||||||||||||
| 1486 | return false; never executed: return false; | 0 | ||||||||||||
| 1487 | } | - | ||||||||||||
| 1488 | - | |||||||||||||
| 1489 | void QScrollerPrivate::handleDrag(const QPointF &position, qint64 timestamp) | - | ||||||||||||
| 1490 | { | - | ||||||||||||
| 1491 | const QScrollerPropertiesPrivate *sp = properties.d.data(); | - | ||||||||||||
| 1492 | - | |||||||||||||
| 1493 | QPointF deltaPixel = position - lastPosition; | - | ||||||||||||
| 1494 | qint64 deltaTime = timestamp - lastTimestamp; | - | ||||||||||||
| 1495 | - | |||||||||||||
| 1496 | if (sp->axisLockThreshold) { | - | ||||||||||||
| 1497 | int dx = qAbs(deltaPixel.x()); | - | ||||||||||||
| 1498 | int dy = qAbs(deltaPixel.y()); | - | ||||||||||||
| 1499 | if (dx || dy) { | - | ||||||||||||
| 1500 | bool vertical = (dy > dx); | - | ||||||||||||
| 1501 | qreal alpha = qreal(vertical ? dx : dy) / qreal(vertical ? dy : dx); | - | ||||||||||||
| 1502 | //qScrollerDebug() << "QScroller::handleDrag() -- axis lock:" << alpha << " / " << axisLockThreshold << "- isvertical:" << vertical << "- dx:" << dx << "- dy:" << dy; | - | ||||||||||||
| 1503 | if (alpha <= sp->axisLockThreshold) { | - | ||||||||||||
| 1504 | if (vertical) | - | ||||||||||||
| 1505 | deltaPixel.setX(0); | - | ||||||||||||
| 1506 | else | - | ||||||||||||
| 1507 | deltaPixel.setY(0); | - | ||||||||||||
| 1508 | } | - | ||||||||||||
| 1509 | } | - | ||||||||||||
| 1510 | } | - | ||||||||||||
| 1511 | - | |||||||||||||
| 1512 | // calculate velocity (if the user would release the mouse NOW) | - | ||||||||||||
| 1513 | updateVelocity(deltaPixel, deltaTime); | - | ||||||||||||
| 1514 | - | |||||||||||||
| 1515 | // restrict velocity, if content is not scrollable | - | ||||||||||||
| 1516 | QRectF max = contentPosRange; | - | ||||||||||||
| 1517 | bool canScrollX = (max.width() > 0) || (sp->hOvershootPolicy == QScrollerProperties::OvershootAlwaysOn); | - | ||||||||||||
| 1518 | bool canScrollY = (max.height() > 0) || (sp->vOvershootPolicy == QScrollerProperties::OvershootAlwaysOn); | - | ||||||||||||
| 1519 | - | |||||||||||||
| 1520 | if (!canScrollX) { | - | ||||||||||||
| 1521 | deltaPixel.setX(0); | - | ||||||||||||
| 1522 | releaseVelocity.setX(0); | - | ||||||||||||
| 1523 | } | - | ||||||||||||
| 1524 | if (!canScrollY) { | - | ||||||||||||
| 1525 | deltaPixel.setY(0); | - | ||||||||||||
| 1526 | releaseVelocity.setY(0); | - | ||||||||||||
| 1527 | } | - | ||||||||||||
| 1528 | - | |||||||||||||
| 1529 | // if (firstDrag) { | - | ||||||||||||
| 1530 | // // Do not delay the first drag | - | ||||||||||||
| 1531 | // setContentPositionHelper(q->contentPosition() - overshootDistance - deltaPixel); | - | ||||||||||||
| 1532 | // dragDistance = QPointF(0, 0); | - | ||||||||||||
| 1533 | // } else { | - | ||||||||||||
| 1534 | dragDistance += deltaPixel; | - | ||||||||||||
| 1535 | // } | - | ||||||||||||
| 1536 | //qScrollerDebug() << "######################" << deltaPixel << position.y() << lastPosition.y(); | - | ||||||||||||
| 1537 | - | |||||||||||||
| 1538 | lastPosition = position; | - | ||||||||||||
| 1539 | lastTimestamp = timestamp; | - | ||||||||||||
| 1540 | } | - | ||||||||||||
| 1541 | - | |||||||||||||
| 1542 | bool QScrollerPrivate::pressWhileInactive(const QPointF &position, qint64 timestamp) | - | ||||||||||||
| 1543 | { | - | ||||||||||||
| 1544 | if (prepareScrolling(position)) { | - | ||||||||||||
| 1545 | const QScrollerPropertiesPrivate *sp = properties.d.data(); | - | ||||||||||||
| 1546 | - | |||||||||||||
| 1547 | if (!contentPosRange.isNull() || | - | ||||||||||||
| 1548 | (sp->hOvershootPolicy == QScrollerProperties::OvershootAlwaysOn) || | - | ||||||||||||
| 1549 | (sp->vOvershootPolicy == QScrollerProperties::OvershootAlwaysOn)) { | - | ||||||||||||
| 1550 | - | |||||||||||||
| 1551 | lastPosition = pressPosition = position; | - | ||||||||||||
| 1552 | lastTimestamp = pressTimestamp = timestamp; | - | ||||||||||||
| 1553 | setState(QScroller::Pressed); | - | ||||||||||||
| 1554 | } | - | ||||||||||||
| 1555 | } | - | ||||||||||||
| 1556 | return false; | - | ||||||||||||
| 1557 | } | - | ||||||||||||
| 1558 | - | |||||||||||||
| 1559 | bool QScrollerPrivate::releaseWhilePressed(const QPointF &, qint64) | - | ||||||||||||
| 1560 | { | - | ||||||||||||
| 1561 | if (overshootPosition != QPointF(0.0, 0.0)) { | - | ||||||||||||
| 1562 | setState(QScroller::Scrolling); | - | ||||||||||||
| 1563 | return true; | - | ||||||||||||
| 1564 | } else { | - | ||||||||||||
| 1565 | setState(QScroller::Inactive); | - | ||||||||||||
| 1566 | return false; | - | ||||||||||||
| 1567 | } | - | ||||||||||||
| 1568 | } | - | ||||||||||||
| 1569 | - | |||||||||||||
| 1570 | bool QScrollerPrivate::moveWhilePressed(const QPointF &position, qint64 timestamp) | - | ||||||||||||
| 1571 | { | - | ||||||||||||
| 1572 | Q_Q(QScroller); | - | ||||||||||||
| 1573 | const QScrollerPropertiesPrivate *sp = properties.d.data(); | - | ||||||||||||
| 1574 | QPointF ppm = q->pixelPerMeter(); | - | ||||||||||||
| 1575 | - | |||||||||||||
| 1576 | QPointF deltaPixel = position - pressPosition; | - | ||||||||||||
| 1577 | - | |||||||||||||
| 1578 | bool moveAborted = false; | - | ||||||||||||
| 1579 | bool moveStarted = (((deltaPixel / ppm).manhattanLength()) > sp->dragStartDistance); | - | ||||||||||||
| 1580 | - | |||||||||||||
| 1581 | // check the direction of the mouse drag and abort if it's too much in the wrong direction. | - | ||||||||||||
| 1582 | if (moveStarted) { | - | ||||||||||||
| 1583 | QRectF max = contentPosRange; | - | ||||||||||||
| 1584 | bool canScrollX = (max.width() > 0); | - | ||||||||||||
| 1585 | bool canScrollY = (max.height() > 0); | - | ||||||||||||
| 1586 | - | |||||||||||||
| 1587 | if (sp->hOvershootPolicy == QScrollerProperties::OvershootAlwaysOn) | - | ||||||||||||
| 1588 | canScrollX = true; | - | ||||||||||||
| 1589 | if (sp->vOvershootPolicy == QScrollerProperties::OvershootAlwaysOn) | - | ||||||||||||
| 1590 | canScrollY = true; | - | ||||||||||||
| 1591 | - | |||||||||||||
| 1592 | if (qAbs(deltaPixel.x() / ppm.x()) < qAbs(deltaPixel.y() / ppm.y())) { | - | ||||||||||||
| 1593 | if (!canScrollY) | - | ||||||||||||
| 1594 | moveAborted = true; | - | ||||||||||||
| 1595 | } else { | - | ||||||||||||
| 1596 | if (!canScrollX) | - | ||||||||||||
| 1597 | moveAborted = true; | - | ||||||||||||
| 1598 | } | - | ||||||||||||
| 1599 | } | - | ||||||||||||
| 1600 | - | |||||||||||||
| 1601 | if (moveAborted) { | - | ||||||||||||
| 1602 | setState(QScroller::Inactive); | - | ||||||||||||
| 1603 | moveStarted = false; | - | ||||||||||||
| 1604 | - | |||||||||||||
| 1605 | } else if (moveStarted) { | - | ||||||||||||
| 1606 | setState(QScroller::Dragging); | - | ||||||||||||
| 1607 | - | |||||||||||||
| 1608 | // subtract the dragStartDistance | - | ||||||||||||
| 1609 | deltaPixel = deltaPixel - deltaPixel * (sp->dragStartDistance / deltaPixel.manhattanLength()); | - | ||||||||||||
| 1610 | - | |||||||||||||
| 1611 | if (deltaPixel != QPointF(0, 0)) { | - | ||||||||||||
| 1612 | // handleDrag updates lastPosition, lastTimestamp and velocity | - | ||||||||||||
| 1613 | handleDrag(pressPosition + deltaPixel, timestamp); | - | ||||||||||||
| 1614 | } | - | ||||||||||||
| 1615 | } | - | ||||||||||||
| 1616 | return moveStarted; | - | ||||||||||||
| 1617 | } | - | ||||||||||||
| 1618 | - | |||||||||||||
| 1619 | bool QScrollerPrivate::moveWhileDragging(const QPointF &position, qint64 timestamp) | - | ||||||||||||
| 1620 | { | - | ||||||||||||
| 1621 | // handleDrag updates lastPosition, lastTimestamp and velocity | - | ||||||||||||
| 1622 | handleDrag(position, timestamp); | - | ||||||||||||
| 1623 | return true; | - | ||||||||||||
| 1624 | } | - | ||||||||||||
| 1625 | - | |||||||||||||
| 1626 | void QScrollerPrivate::timerEventWhileDragging() | - | ||||||||||||
| 1627 | { | - | ||||||||||||
| 1628 | if (dragDistance != QPointF(0, 0)) { | - | ||||||||||||
| 1629 | qScrollerDebug() << "QScroller::timerEventWhileDragging() -- dragDistance:" << dragDistance; dead code: QMessageLogger(__FILE__, 1629, __PRETTY_FUNCTION__).debug() << "QScroller::timerEventWhileDragging() -- dragDistance:" << dragDistance; | - | ||||||||||||
| 1630 | - | |||||||||||||
| 1631 | setContentPositionHelperDragging(-dragDistance); | - | ||||||||||||
| 1632 | dragDistance = QPointF(0, 0); | - | ||||||||||||
| 1633 | } | - | ||||||||||||
| 1634 | } | - | ||||||||||||
| 1635 | - | |||||||||||||
| 1636 | bool QScrollerPrivate::releaseWhileDragging(const QPointF &position, qint64 timestamp) | - | ||||||||||||
| 1637 | { | - | ||||||||||||
| 1638 | Q_Q(QScroller); | - | ||||||||||||
| 1639 | const QScrollerPropertiesPrivate *sp = properties.d.data(); | - | ||||||||||||
| 1640 | - | |||||||||||||
| 1641 | // handleDrag updates lastPosition, lastTimestamp and velocity | - | ||||||||||||
| 1642 | handleDrag(position, timestamp); | - | ||||||||||||
| 1643 | - | |||||||||||||
| 1644 | // check if we moved at all - this can happen if you stop a running | - | ||||||||||||
| 1645 | // scroller with a press and release shortly afterwards | - | ||||||||||||
| 1646 | QPointF deltaPixel = position - pressPosition; | - | ||||||||||||
| 1647 | if (((deltaPixel / q->pixelPerMeter()).manhattanLength()) > sp->dragStartDistance) { | - | ||||||||||||
| 1648 | - | |||||||||||||
| 1649 | // handle accelerating flicks | - | ||||||||||||
| 1650 | if ((oldVelocity != QPointF(0, 0)) && sp->acceleratingFlickMaximumTime && | - | ||||||||||||
| 1651 | ((timestamp - pressTimestamp) < qint64(sp->acceleratingFlickMaximumTime * 1000))) { | - | ||||||||||||
| 1652 | - | |||||||||||||
| 1653 | // - determine if the direction was changed | - | ||||||||||||
| 1654 | int signX = 0, signY = 0; | - | ||||||||||||
| 1655 | if (releaseVelocity.x()) | - | ||||||||||||
| 1656 | signX = (releaseVelocity.x() > 0) == (oldVelocity.x() > 0) ? 1 : -1; | - | ||||||||||||
| 1657 | if (releaseVelocity.y()) | - | ||||||||||||
| 1658 | signY = (releaseVelocity.y() > 0) == (oldVelocity.y() > 0) ? 1 : -1; | - | ||||||||||||
| 1659 | - | |||||||||||||
| 1660 | if (signX > 0) | - | ||||||||||||
| 1661 | releaseVelocity.setX(qBound(-sp->maximumVelocity, | - | ||||||||||||
| 1662 | oldVelocity.x() * sp->acceleratingFlickSpeedupFactor, | - | ||||||||||||
| 1663 | sp->maximumVelocity)); | - | ||||||||||||
| 1664 | if (signY > 0) | - | ||||||||||||
| 1665 | releaseVelocity.setY(qBound(-sp->maximumVelocity, | - | ||||||||||||
| 1666 | oldVelocity.y() * sp->acceleratingFlickSpeedupFactor, | - | ||||||||||||
| 1667 | sp->maximumVelocity)); | - | ||||||||||||
| 1668 | } | - | ||||||||||||
| 1669 | } | - | ||||||||||||
| 1670 | - | |||||||||||||
| 1671 | QPointF ppm = q->pixelPerMeter(); | - | ||||||||||||
| 1672 | createScrollingSegments(releaseVelocity, contentPosition + overshootPosition, ppm); | - | ||||||||||||
| 1673 | - | |||||||||||||
| 1674 | qScrollerDebug() << "QScroller::releaseWhileDragging() -- velocity:" << releaseVelocity << "-- minimum velocity:" << sp->minimumVelocity << "overshoot" << overshootPosition; dead code: QMessageLogger(__FILE__, 1674, __PRETTY_FUNCTION__).debug() << "QScroller::releaseWhileDragging() -- velocity:" << releaseVelocity << "-- minimum velocity:" << sp->minimumVelocity << "overshoot" << overshootPosition; | - | ||||||||||||
| 1675 | - | |||||||||||||
| 1676 | if (xSegments.isEmpty() && ySegments.isEmpty()) | - | ||||||||||||
| 1677 | setState(QScroller::Inactive); | - | ||||||||||||
| 1678 | else | - | ||||||||||||
| 1679 | setState(QScroller::Scrolling); | - | ||||||||||||
| 1680 | - | |||||||||||||
| 1681 | return true; | - | ||||||||||||
| 1682 | } | - | ||||||||||||
| 1683 | - | |||||||||||||
| 1684 | void QScrollerPrivate::timerEventWhileScrolling() | - | ||||||||||||
| 1685 | { | - | ||||||||||||
| 1686 | qScrollerDebug("QScroller::timerEventWhileScrolling()"); dead code: QMessageLogger(__FILE__, 1686, __PRETTY_FUNCTION__).debug("QScroller::timerEventWhileScrolling()"); | - | ||||||||||||
| 1687 | - | |||||||||||||
| 1688 | setContentPositionHelperScrolling(); | - | ||||||||||||
| 1689 | if (xSegments.isEmpty() && ySegments.isEmpty()) | - | ||||||||||||
| 1690 | setState(QScroller::Inactive); | - | ||||||||||||
| 1691 | } | - | ||||||||||||
| 1692 | - | |||||||||||||
| 1693 | bool QScrollerPrivate::pressWhileScrolling(const QPointF &position, qint64 timestamp) | - | ||||||||||||
| 1694 | { | - | ||||||||||||
| 1695 | Q_Q(QScroller); | - | ||||||||||||
| 1696 | - | |||||||||||||
| 1697 | if ((q->velocity() <= properties.d->maximumClickThroughVelocity) && | - | ||||||||||||
| 1698 | (overshootPosition == QPointF(0.0, 0.0))) { | - | ||||||||||||
| 1699 | setState(QScroller::Inactive); | - | ||||||||||||
| 1700 | return false; | - | ||||||||||||
| 1701 | } else { | - | ||||||||||||
| 1702 | lastPosition = pressPosition = position; | - | ||||||||||||
| 1703 | lastTimestamp = pressTimestamp = timestamp; | - | ||||||||||||
| 1704 | setState(QScroller::Pressed); | - | ||||||||||||
| 1705 | setState(QScroller::Dragging); | - | ||||||||||||
| 1706 | return true; | - | ||||||||||||
| 1707 | } | - | ||||||||||||
| 1708 | } | - | ||||||||||||
| 1709 | - | |||||||||||||
| 1710 | /*! \internal | - | ||||||||||||
| 1711 | This function handles all state changes of the scroller. | - | ||||||||||||
| 1712 | */ | - | ||||||||||||
| 1713 | void QScrollerPrivate::setState(QScroller::State newstate) | - | ||||||||||||
| 1714 | { | - | ||||||||||||
| 1715 | Q_Q(QScroller); | - | ||||||||||||
| 1716 | bool sendLastScroll = false; | - | ||||||||||||
| 1717 | - | |||||||||||||
| 1718 | if (state == newstate) | - | ||||||||||||
| 1719 | return; | - | ||||||||||||
| 1720 | - | |||||||||||||
| 1721 | qScrollerDebug() << q << "QScroller::setState(" << stateName(newstate) << ')'; dead code: QMessageLogger(__FILE__, 1721, __PRETTY_FUNCTION__).debug() << q << "QScroller::setState(" << stateName(newstate) << ')'; | - | ||||||||||||
| 1722 | - | |||||||||||||
| 1723 | switch (newstate) { | - | ||||||||||||
| 1724 | case QScroller::Inactive: | - | ||||||||||||
| 1725 | #ifndef QT_NO_ANIMATION | - | ||||||||||||
| 1726 | scrollTimer->stop(); | - | ||||||||||||
| 1727 | #endif | - | ||||||||||||
| 1728 | - | |||||||||||||
| 1729 | // send the last scroll event (but only after the current state change was finished) | - | ||||||||||||
| 1730 | if (!firstScroll) | - | ||||||||||||
| 1731 | sendLastScroll = true; | - | ||||||||||||
| 1732 | - | |||||||||||||
| 1733 | releaseVelocity = QPointF(0, 0); | - | ||||||||||||
| 1734 | break; | - | ||||||||||||
| 1735 | - | |||||||||||||
| 1736 | case QScroller::Pressed: | - | ||||||||||||
| 1737 | #ifndef QT_NO_ANIMATION | - | ||||||||||||
| 1738 | scrollTimer->stop(); | - | ||||||||||||
| 1739 | #endif | - | ||||||||||||
| 1740 | - | |||||||||||||
| 1741 | oldVelocity = releaseVelocity; | - | ||||||||||||
| 1742 | releaseVelocity = QPointF(0, 0); | - | ||||||||||||
| 1743 | break; | - | ||||||||||||
| 1744 | - | |||||||||||||
| 1745 | case QScroller::Dragging: | - | ||||||||||||
| 1746 | dragDistance = QPointF(0, 0); | - | ||||||||||||
| 1747 | #ifndef QT_NO_ANIMATION | - | ||||||||||||
| 1748 | if (state == QScroller::Pressed) | - | ||||||||||||
| 1749 | scrollTimer->start(); | - | ||||||||||||
| 1750 | #endif | - | ||||||||||||
| 1751 | break; | - | ||||||||||||
| 1752 | - | |||||||||||||
| 1753 | case QScroller::Scrolling: | - | ||||||||||||
| 1754 | #ifndef QT_NO_ANIMATION | - | ||||||||||||
| 1755 | scrollTimer->start(); | - | ||||||||||||
| 1756 | #endif | - | ||||||||||||
| 1757 | break; | - | ||||||||||||
| 1758 | } | - | ||||||||||||
| 1759 | - | |||||||||||||
| 1760 | qSwap(state, newstate); | - | ||||||||||||
| 1761 | - | |||||||||||||
| 1762 | if (sendLastScroll) { | - | ||||||||||||
| 1763 | QScrollEvent se(contentPosition, overshootPosition, QScrollEvent::ScrollFinished); | - | ||||||||||||
| 1764 | sendEvent(target, &se); | - | ||||||||||||
| 1765 | firstScroll = true; | - | ||||||||||||
| 1766 | } | - | ||||||||||||
| 1767 | if (state == QScroller::Dragging || state == QScroller::Scrolling) | - | ||||||||||||
| 1768 | qt_activeScrollers()->insert(q); | - | ||||||||||||
| 1769 | else | - | ||||||||||||
| 1770 | qt_activeScrollers()->remove(q); | - | ||||||||||||
| 1771 | emit q->stateChanged(state); | - | ||||||||||||
| 1772 | } | - | ||||||||||||
| 1773 | - | |||||||||||||
| 1774 | - | |||||||||||||
| 1775 | /*! \internal | - | ||||||||||||
| 1776 | Helps when setting the content position. | - | ||||||||||||
| 1777 | It will try to move the content by the requested delta but stop in case | - | ||||||||||||
| 1778 | when we are coming back from an overshoot or a scrollTo. | - | ||||||||||||
| 1779 | It will also indicate a new overshooting condition by the overshootX and oversthootY flags. | - | ||||||||||||
| 1780 | - | |||||||||||||
| 1781 | In this cases it will reset the velocity variables and other flags. | - | ||||||||||||
| 1782 | - | |||||||||||||
| 1783 | Also keeps track of the current over-shooting value in overshootPosition. | - | ||||||||||||
| 1784 | - | |||||||||||||
| 1785 | \a deltaPos is the amount of pixels the current content position should be moved | - | ||||||||||||
| 1786 | */ | - | ||||||||||||
| 1787 | void QScrollerPrivate::setContentPositionHelperDragging(const QPointF &deltaPos) | - | ||||||||||||
| 1788 | { | - | ||||||||||||
| 1789 | const QScrollerPropertiesPrivate *sp = properties.d.data(); | - | ||||||||||||
| 1790 | - | |||||||||||||
| 1791 | if (sp->overshootDragResistanceFactor) | - | ||||||||||||
| 1792 | overshootPosition /= sp->overshootDragResistanceFactor; | - | ||||||||||||
| 1793 | - | |||||||||||||
| 1794 | QPointF oldPos = contentPosition + overshootPosition; | - | ||||||||||||
| 1795 | QPointF newPos = oldPos + deltaPos; | - | ||||||||||||
| 1796 | - | |||||||||||||
| 1797 | qScrollerDebug() << "QScroller::setContentPositionHelperDragging(" << deltaPos << " [pix])"; dead code: QMessageLogger(__FILE__, 1797, __PRETTY_FUNCTION__).debug() << "QScroller::setContentPositionHelperDragging(" << deltaPos << " [pix])"; | - | ||||||||||||
| 1798 | qScrollerDebug() << " --> overshoot:" << overshootPosition << "- old pos:" << oldPos << "- new pos:" << newPos; dead code: QMessageLogger(__FILE__, 1798, __PRETTY_FUNCTION__).debug() << " --> overshoot:" << overshootPosition << "- old pos:" << oldPos << "- new pos:" << newPos; | - | ||||||||||||
| 1799 | - | |||||||||||||
| 1800 | QPointF oldClampedPos = clampToRect(oldPos, contentPosRange); | - | ||||||||||||
| 1801 | QPointF newClampedPos = clampToRect(newPos, contentPosRange); | - | ||||||||||||
| 1802 | - | |||||||||||||
| 1803 | // --- handle overshooting and stop if the coordinate is going back inside the normal area | - | ||||||||||||
| 1804 | bool alwaysOvershootX = (sp->hOvershootPolicy == QScrollerProperties::OvershootAlwaysOn); | - | ||||||||||||
| 1805 | bool alwaysOvershootY = (sp->vOvershootPolicy == QScrollerProperties::OvershootAlwaysOn); | - | ||||||||||||
| 1806 | bool noOvershootX = (sp->hOvershootPolicy == QScrollerProperties::OvershootAlwaysOff) || | - | ||||||||||||
| 1807 | ((state == QScroller::Dragging) && !sp->overshootDragResistanceFactor) || | - | ||||||||||||
| 1808 | !sp->overshootDragDistanceFactor; | - | ||||||||||||
| 1809 | bool noOvershootY = (sp->vOvershootPolicy == QScrollerProperties::OvershootAlwaysOff) || | - | ||||||||||||
| 1810 | ((state == QScroller::Dragging) && !sp->overshootDragResistanceFactor) || | - | ||||||||||||
| 1811 | !sp->overshootDragDistanceFactor; | - | ||||||||||||
| 1812 | bool canOvershootX = !noOvershootX && (alwaysOvershootX || contentPosRange.width()); | - | ||||||||||||
| 1813 | bool canOvershootY = !noOvershootY && (alwaysOvershootY || contentPosRange.height()); | - | ||||||||||||
| 1814 | - | |||||||||||||
| 1815 | qreal oldOvershootX = (canOvershootX) ? oldPos.x() - oldClampedPos.x() : 0; | - | ||||||||||||
| 1816 | qreal oldOvershootY = (canOvershootY) ? oldPos.y() - oldClampedPos.y() : 0; | - | ||||||||||||
| 1817 | - | |||||||||||||
| 1818 | qreal newOvershootX = (canOvershootX) ? newPos.x() - newClampedPos.x() : 0; | - | ||||||||||||
| 1819 | qreal newOvershootY = (canOvershootY) ? newPos.y() - newClampedPos.y() : 0; | - | ||||||||||||
| 1820 | - | |||||||||||||
| 1821 | qreal maxOvershootX = viewportSize.width() * sp->overshootDragDistanceFactor; | - | ||||||||||||
| 1822 | qreal maxOvershootY = viewportSize.height() * sp->overshootDragDistanceFactor; | - | ||||||||||||
| 1823 | - | |||||||||||||
| 1824 | qScrollerDebug() << " --> noOs:" << noOvershootX << "drf:" << sp->overshootDragResistanceFactor << "mdf:" << sp->overshootScrollDistanceFactor << "ossP:"<<sp->hOvershootPolicy; dead code: QMessageLogger(__FILE__, 1824, __PRETTY_FUNCTION__).debug() << " --> noOs:" << noOvershootX << "drf:" << sp->overshootDragResistanceFactor << "mdf:" << sp->overshootScrollDistanceFactor << "ossP:"<<sp->hOvershootPolicy; | - | ||||||||||||
| 1825 | qScrollerDebug() << " --> canOS:" << canOvershootX << "newOS:" << newOvershootX << "maxOS:" << maxOvershootX; dead code: QMessageLogger(__FILE__, 1825, __PRETTY_FUNCTION__).debug() << " --> canOS:" << canOvershootX << "newOS:" << newOvershootX << "maxOS:" << maxOvershootX; | - | ||||||||||||
| 1826 | - | |||||||||||||
| 1827 | if (sp->overshootDragResistanceFactor) { | - | ||||||||||||
| 1828 | oldOvershootX *= sp->overshootDragResistanceFactor; | - | ||||||||||||
| 1829 | oldOvershootY *= sp->overshootDragResistanceFactor; | - | ||||||||||||
| 1830 | newOvershootX *= sp->overshootDragResistanceFactor; | - | ||||||||||||
| 1831 | newOvershootY *= sp->overshootDragResistanceFactor; | - | ||||||||||||
| 1832 | } | - | ||||||||||||
| 1833 | - | |||||||||||||
| 1834 | // -- stop at the maximum overshoot distance | - | ||||||||||||
| 1835 | - | |||||||||||||
| 1836 | newOvershootX = qBound(-maxOvershootX, newOvershootX, maxOvershootX); | - | ||||||||||||
| 1837 | newOvershootY = qBound(-maxOvershootY, newOvershootY, maxOvershootY); | - | ||||||||||||
| 1838 | - | |||||||||||||
| 1839 | overshootPosition.setX(newOvershootX); | - | ||||||||||||
| 1840 | overshootPosition.setY(newOvershootY); | - | ||||||||||||
| 1841 | contentPosition = newClampedPos; | - | ||||||||||||
| 1842 | - | |||||||||||||
| 1843 | QScrollEvent se(contentPosition, overshootPosition, firstScroll ? QScrollEvent::ScrollStarted : QScrollEvent::ScrollUpdated); | - | ||||||||||||
| 1844 | sendEvent(target, &se); | - | ||||||||||||
| 1845 | firstScroll = false; | - | ||||||||||||
| 1846 | - | |||||||||||||
| 1847 | qScrollerDebug() << " --> new position:" << newClampedPos << "- new overshoot:" << overshootPosition << dead code: QMessageLogger(__FILE__, 1847, __PRETTY_FUNCTION__).debug() << " --> new position:" << newClampedPos << "- new overshoot:" << overshootPosition << "- overshoot x/y?:" << overshootPosition; | - | ||||||||||||
| 1848 | "- overshoot x/y?:" << overshootPosition; dead code: QMessageLogger(__FILE__, 1847, __PRETTY_FUNCTION__).debug() << " --> new position:" << newClampedPos << "- new overshoot:" << overshootPosition << "- overshoot x/y?:" << overshootPosition; | - | ||||||||||||
| 1849 | } | - | ||||||||||||
| 1850 | - | |||||||||||||
| 1851 | - | |||||||||||||
| 1852 | qreal QScrollerPrivate::nextSegmentPosition(QQueue<ScrollSegment> &segments, qint64 now, qreal oldPos) | - | ||||||||||||
| 1853 | { | - | ||||||||||||
| 1854 | qreal pos = oldPos; | - | ||||||||||||
| 1855 | - | |||||||||||||
| 1856 | // check the X segments for new positions | - | ||||||||||||
| 1857 | while (!segments.isEmpty()) { | - | ||||||||||||
| 1858 | const ScrollSegment s = segments.head(); | - | ||||||||||||
| 1859 | - | |||||||||||||
| 1860 | if ((s.startTime + s.deltaTime * s.stopProgress) <= now) { | - | ||||||||||||
| 1861 | segments.dequeue(); | - | ||||||||||||
| 1862 | pos = s.stopPos; | - | ||||||||||||
| 1863 | } else if (s.startTime <= now) { | - | ||||||||||||
| 1864 | qreal progress = qreal(now - s.startTime) / qreal(s.deltaTime); | - | ||||||||||||
| 1865 | pos = s.startPos + s.deltaPos * s.curve.valueForProgress(progress); | - | ||||||||||||
| 1866 | if (s.deltaPos > 0 ? pos > s.stopPos : pos < s.stopPos) { | - | ||||||||||||
| 1867 | segments.dequeue(); | - | ||||||||||||
| 1868 | pos = s.stopPos; | - | ||||||||||||
| 1869 | } else { | - | ||||||||||||
| 1870 | break; | - | ||||||||||||
| 1871 | } | - | ||||||||||||
| 1872 | } else { | - | ||||||||||||
| 1873 | break; | - | ||||||||||||
| 1874 | } | - | ||||||||||||
| 1875 | } | - | ||||||||||||
| 1876 | return pos; | - | ||||||||||||
| 1877 | } | - | ||||||||||||
| 1878 | - | |||||||||||||
| 1879 | void QScrollerPrivate::setContentPositionHelperScrolling() | - | ||||||||||||
| 1880 | { | - | ||||||||||||
| 1881 | qint64 now = monotonicTimer.elapsed(); | - | ||||||||||||
| 1882 | QPointF newPos = contentPosition + overshootPosition; | - | ||||||||||||
| 1883 | - | |||||||||||||
| 1884 | newPos.setX(nextSegmentPosition(xSegments, now, newPos.x())); | - | ||||||||||||
| 1885 | newPos.setY(nextSegmentPosition(ySegments, now, newPos.y())); | - | ||||||||||||
| 1886 | - | |||||||||||||
| 1887 | // -- set the position and handle overshoot | - | ||||||||||||
| 1888 | qScrollerDebug() << "QScroller::setContentPositionHelperScrolling()\n" dead code: QMessageLogger(__FILE__, 1888, __PRETTY_FUNCTION__).debug() << "QScroller::setContentPositionHelperScrolling()\n" " --> overshoot:" << overshootPosition << "- new pos:" << newPos; | - | ||||||||||||
| 1889 | " --> overshoot:" << overshootPosition << "- new pos:" << newPos; dead code: QMessageLogger(__FILE__, 1888, __PRETTY_FUNCTION__).debug() << "QScroller::setContentPositionHelperScrolling()\n" " --> overshoot:" << overshootPosition << "- new pos:" << newPos; | - | ||||||||||||
| 1890 | - | |||||||||||||
| 1891 | QPointF newClampedPos = clampToRect(newPos, contentPosRange); | - | ||||||||||||
| 1892 | - | |||||||||||||
| 1893 | overshootPosition = newPos - newClampedPos; | - | ||||||||||||
| 1894 | contentPosition = newClampedPos; | - | ||||||||||||
| 1895 | - | |||||||||||||
| 1896 | QScrollEvent se(contentPosition, overshootPosition, firstScroll ? QScrollEvent::ScrollStarted : QScrollEvent::ScrollUpdated); | - | ||||||||||||
| 1897 | sendEvent(target, &se); | - | ||||||||||||
| 1898 | firstScroll = false; | - | ||||||||||||
| 1899 | - | |||||||||||||
| 1900 | qScrollerDebug() << " --> new position:" << newClampedPos << "- new overshoot:" << overshootPosition; dead code: QMessageLogger(__FILE__, 1900, __PRETTY_FUNCTION__).debug() << " --> new position:" << newClampedPos << "- new overshoot:" << overshootPosition; | - | ||||||||||||
| 1901 | } | - | ||||||||||||
| 1902 | - | |||||||||||||
| 1903 | /*! \internal | - | ||||||||||||
| 1904 | Returns the next snap point in direction. | - | ||||||||||||
| 1905 | If \a direction >0 it will return the next snap point that is larger than the current position. | - | ||||||||||||
| 1906 | If \a direction <0 it will return the next snap point that is smaller than the current position. | - | ||||||||||||
| 1907 | If \a direction ==0 it will return the nearest snap point (or the current position if we are already | - | ||||||||||||
| 1908 | on a snap point. | - | ||||||||||||
| 1909 | Returns the nearest snap position or NaN if no such point could be found. | - | ||||||||||||
| 1910 | */ | - | ||||||||||||
| 1911 | qreal QScrollerPrivate::nextSnapPos(qreal p, int dir, Qt::Orientation orientation) const | - | ||||||||||||
| 1912 | { | - | ||||||||||||
| 1913 | qreal bestSnapPos = Q_QNAN; | - | ||||||||||||
| 1914 | qreal bestSnapPosDist = Q_INFINITY; | - | ||||||||||||
| 1915 | - | |||||||||||||
| 1916 | qreal minPos; | - | ||||||||||||
| 1917 | qreal maxPos; | - | ||||||||||||
| 1918 | - | |||||||||||||
| 1919 | if (orientation == Qt::Horizontal) {
| 0 | ||||||||||||
| 1920 | minPos = contentPosRange.left(); | - | ||||||||||||
| 1921 | maxPos = contentPosRange.right(); | - | ||||||||||||
| 1922 | } else { never executed: end of block | 0 | ||||||||||||
| 1923 | minPos = contentPosRange.top(); | - | ||||||||||||
| 1924 | maxPos = contentPosRange.bottom(); | - | ||||||||||||
| 1925 | } never executed: end of block | 0 | ||||||||||||
| 1926 | - | |||||||||||||
| 1927 | if (orientation == Qt::Horizontal) {
| 0 | ||||||||||||
| 1928 | // the snap points in the list | - | ||||||||||||
| 1929 | foreach (qreal snapPos, snapPositionsX) { | - | ||||||||||||
| 1930 | qreal snapPosDist = snapPos - p; | - | ||||||||||||
| 1931 | if ((dir > 0 && snapPosDist < 0) ||
| 0 | ||||||||||||
| 1932 | (dir < 0 && snapPosDist > 0))
| 0 | ||||||||||||
| 1933 | continue; // wrong direction never executed: continue; | 0 | ||||||||||||
| 1934 | if (snapPos < minPos || snapPos > maxPos )
| 0 | ||||||||||||
| 1935 | continue; // invalid never executed: continue; | 0 | ||||||||||||
| 1936 | - | |||||||||||||
| 1937 | if (qIsNaN(bestSnapPos) ||
| 0 | ||||||||||||
| 1938 | qAbs(snapPosDist) < bestSnapPosDist ) {
| 0 | ||||||||||||
| 1939 | bestSnapPos = snapPos; | - | ||||||||||||
| 1940 | bestSnapPosDist = qAbs(snapPosDist); | - | ||||||||||||
| 1941 | } never executed: end of block | 0 | ||||||||||||
| 1942 | } never executed: end of block | 0 | ||||||||||||
| 1943 | - | |||||||||||||
| 1944 | // the snap point interval | - | ||||||||||||
| 1945 | if (snapIntervalX > 0.0) {
| 0 | ||||||||||||
| 1946 | qreal first = minPos + snapFirstX; | - | ||||||||||||
| 1947 | qreal snapPos; | - | ||||||||||||
| 1948 | if (dir > 0)
| 0 | ||||||||||||
| 1949 | snapPos = qCeil((p - first) / snapIntervalX) * snapIntervalX + first; never executed: snapPos = qCeil((p - first) / snapIntervalX) * snapIntervalX + first; | 0 | ||||||||||||
| 1950 | else if (dir < 0)
| 0 | ||||||||||||
| 1951 | snapPos = qFloor((p - first) / snapIntervalX) * snapIntervalX + first; never executed: snapPos = qFloor((p - first) / snapIntervalX) * snapIntervalX + first; | 0 | ||||||||||||
| 1952 | else if (p <= first)
| 0 | ||||||||||||
| 1953 | snapPos = first; never executed: snapPos = first; | 0 | ||||||||||||
| 1954 | else | - | ||||||||||||
| 1955 | { | - | ||||||||||||
| 1956 | qreal last = qFloor((maxPos - first) / snapIntervalX) * snapIntervalX + first; | - | ||||||||||||
| 1957 | if (p >= last)
| 0 | ||||||||||||
| 1958 | snapPos = last; never executed: snapPos = last; | 0 | ||||||||||||
| 1959 | else | - | ||||||||||||
| 1960 | snapPos = qRound((p - first) / snapIntervalX) * snapIntervalX + first; never executed: snapPos = qRound((p - first) / snapIntervalX) * snapIntervalX + first; | 0 | ||||||||||||
| 1961 | } | - | ||||||||||||
| 1962 | - | |||||||||||||
| 1963 | if (snapPos >= first && snapPos <= maxPos ) {
| 0 | ||||||||||||
| 1964 | qreal snapPosDist = snapPos - p; | - | ||||||||||||
| 1965 | - | |||||||||||||
| 1966 | if (qIsNaN(bestSnapPos) ||
| 0 | ||||||||||||
| 1967 | qAbs(snapPosDist) < bestSnapPosDist ) {
| 0 | ||||||||||||
| 1968 | bestSnapPos = snapPos; | - | ||||||||||||
| 1969 | bestSnapPosDist = qAbs(snapPosDist); | - | ||||||||||||
| 1970 | } never executed: end of block | 0 | ||||||||||||
| 1971 | } never executed: end of block | 0 | ||||||||||||
| 1972 | } never executed: end of block | 0 | ||||||||||||
| 1973 | - | |||||||||||||
| 1974 | } else { // (orientation == Qt::Vertical) never executed: end of block | 0 | ||||||||||||
| 1975 | // the snap points in the list | - | ||||||||||||
| 1976 | foreach (qreal snapPos, snapPositionsY) { | - | ||||||||||||
| 1977 | qreal snapPosDist = snapPos - p; | - | ||||||||||||
| 1978 | if ((dir > 0 && snapPosDist < 0) ||
| 0 | ||||||||||||
| 1979 | (dir < 0 && snapPosDist > 0))
| 0 | ||||||||||||
| 1980 | continue; // wrong direction never executed: continue; | 0 | ||||||||||||
| 1981 | if (snapPos < minPos || snapPos > maxPos )
| 0 | ||||||||||||
| 1982 | continue; // invalid never executed: continue; | 0 | ||||||||||||
| 1983 | - | |||||||||||||
| 1984 | if (qIsNaN(bestSnapPos) ||
| 0 | ||||||||||||
| 1985 | qAbs(snapPosDist) < bestSnapPosDist) {
| 0 | ||||||||||||
| 1986 | bestSnapPos = snapPos; | - | ||||||||||||
| 1987 | bestSnapPosDist = qAbs(snapPosDist); | - | ||||||||||||
| 1988 | } never executed: end of block | 0 | ||||||||||||
| 1989 | } never executed: end of block | 0 | ||||||||||||
| 1990 | - | |||||||||||||
| 1991 | // the snap point interval | - | ||||||||||||
| 1992 | if (snapIntervalY > 0.0) {
| 0 | ||||||||||||
| 1993 | qreal first = minPos + snapFirstY; | - | ||||||||||||
| 1994 | qreal snapPos; | - | ||||||||||||
| 1995 | if (dir > 0)
| 0 | ||||||||||||
| 1996 | snapPos = qCeil((p - first) / snapIntervalY) * snapIntervalY + first; never executed: snapPos = qCeil((p - first) / snapIntervalY) * snapIntervalY + first; | 0 | ||||||||||||
| 1997 | else if (dir < 0)
| 0 | ||||||||||||
| 1998 | snapPos = qFloor((p - first) / snapIntervalY) * snapIntervalY + first; never executed: snapPos = qFloor((p - first) / snapIntervalY) * snapIntervalY + first; | 0 | ||||||||||||
| 1999 | else if (p <= first)
| 0 | ||||||||||||
| 2000 | snapPos = first; never executed: snapPos = first; | 0 | ||||||||||||
| 2001 | else | - | ||||||||||||
| 2002 | { | - | ||||||||||||
| 2003 | qreal last = qFloor((maxPos - first) / snapIntervalY) * snapIntervalY + first; | - | ||||||||||||
| 2004 | if (p >= last)
| 0 | ||||||||||||
| 2005 | snapPos = last; never executed: snapPos = last; | 0 | ||||||||||||
| 2006 | else | - | ||||||||||||
| 2007 | snapPos = qRound((p - first) / snapIntervalY) * snapIntervalY + first; never executed: snapPos = qRound((p - first) / snapIntervalY) * snapIntervalY + first; | 0 | ||||||||||||
| 2008 | } | - | ||||||||||||
| 2009 | - | |||||||||||||
| 2010 | if (snapPos >= first && snapPos <= maxPos ) {
| 0 | ||||||||||||
| 2011 | qreal snapPosDist = snapPos - p; | - | ||||||||||||
| 2012 | - | |||||||||||||
| 2013 | if (qIsNaN(bestSnapPos) ||
| 0 | ||||||||||||
| 2014 | qAbs(snapPosDist) < bestSnapPosDist) {
| 0 | ||||||||||||
| 2015 | bestSnapPos = snapPos; | - | ||||||||||||
| 2016 | bestSnapPosDist = qAbs(snapPosDist); | - | ||||||||||||
| 2017 | } never executed: end of block | 0 | ||||||||||||
| 2018 | } never executed: end of block | 0 | ||||||||||||
| 2019 | } never executed: end of block | 0 | ||||||||||||
| 2020 | } never executed: end of block | 0 | ||||||||||||
| 2021 | - | |||||||||||||
| 2022 | return bestSnapPos; never executed: return bestSnapPos; | 0 | ||||||||||||
| 2023 | } | - | ||||||||||||
| 2024 | - | |||||||||||||
| 2025 | /*! | - | ||||||||||||
| 2026 | \enum QScroller::State | - | ||||||||||||
| 2027 | - | |||||||||||||
| 2028 | This enum contains the different QScroller states. | - | ||||||||||||
| 2029 | - | |||||||||||||
| 2030 | \value Inactive The scroller is not scrolling and nothing is pressed. | - | ||||||||||||
| 2031 | \value Pressed A touch event was received or the mouse button was pressed but the scroll area is currently not dragged. | - | ||||||||||||
| 2032 | \value Dragging The scroll area is currently following the touch point or mouse. | - | ||||||||||||
| 2033 | \value Scrolling The scroll area is moving on it's own. | - | ||||||||||||
| 2034 | */ | - | ||||||||||||
| 2035 | - | |||||||||||||
| 2036 | /*! | - | ||||||||||||
| 2037 | \enum QScroller::ScrollerGestureType | - | ||||||||||||
| 2038 | - | |||||||||||||
| 2039 | This enum contains the different gesture types that are supported by the QScroller gesture recognizer. | - | ||||||||||||
| 2040 | - | |||||||||||||
| 2041 | \value TouchGesture The gesture recognizer will only trigger on touch | - | ||||||||||||
| 2042 | events. Specifically it will react on single touch points when using a | - | ||||||||||||
| 2043 | touch screen and dual touch points when using a touchpad. | - | ||||||||||||
| 2044 | \value LeftMouseButtonGesture The gesture recognizer will only trigger on left mouse button events. | - | ||||||||||||
| 2045 | \value MiddleMouseButtonGesture The gesture recognizer will only trigger on middle mouse button events. | - | ||||||||||||
| 2046 | \value RightMouseButtonGesture The gesture recognizer will only trigger on right mouse button events. | - | ||||||||||||
| 2047 | */ | - | ||||||||||||
| 2048 | - | |||||||||||||
| 2049 | /*! | - | ||||||||||||
| 2050 | \enum QScroller::Input | - | ||||||||||||
| 2051 | - | |||||||||||||
| 2052 | This enum contains an input device agnostic view of input events that are relevant for QScroller. | - | ||||||||||||
| 2053 | - | |||||||||||||
| 2054 | \value InputPress The user pressed the input device (e.g. QEvent::MouseButtonPress, | - | ||||||||||||
| 2055 | QEvent::GraphicsSceneMousePress, QEvent::TouchBegin) | - | ||||||||||||
| 2056 | - | |||||||||||||
| 2057 | \value InputMove The user moved the input device (e.g. QEvent::MouseMove, | - | ||||||||||||
| 2058 | QEvent::GraphicsSceneMouseMove, QEvent::TouchUpdate) | - | ||||||||||||
| 2059 | - | |||||||||||||
| 2060 | \value InputRelease The user released the input device (e.g. QEvent::MouseButtonRelease, | - | ||||||||||||
| 2061 | QEvent::GraphicsSceneMouseRelease, QEvent::TouchEnd) | - | ||||||||||||
| 2062 | - | |||||||||||||
| 2063 | */ | - | ||||||||||||
| 2064 | - | |||||||||||||
| 2065 | QT_END_NAMESPACE | - | ||||||||||||
| 2066 | - | |||||||||||||
| 2067 | #include "moc_qscroller.cpp" | - | ||||||||||||
| 2068 | #include "moc_qscroller_p.cpp" | - | ||||||||||||
| Source code | Switch to Preprocessed file |