| 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 | #ifndef QSCROLLER_P_H |
| 41 | #define QSCROLLER_P_H |
| 42 | |
| 43 | // |
| 44 | // W A R N I N G |
| 45 | // ------------- |
| 46 | // |
| 47 | // This file is not part of the Qt API. It exists purely as an |
| 48 | // implementation detail. This header file may change from version to |
| 49 | // version without notice, or even be removed. |
| 50 | // |
| 51 | // We mean it. |
| 52 | // |
| 53 | |
| 54 | #include <QtWidgets/private/qtwidgetsglobal_p.h> |
| 55 | #include <QObject> |
| 56 | #include <QPointer> |
| 57 | #include <QQueue> |
| 58 | #include <QSet> |
| 59 | #include <QEasingCurve> |
| 60 | #include <QElapsedTimer> |
| 61 | #include <QSizeF> |
| 62 | #include <QPointF> |
| 63 | #include <QRectF> |
| 64 | #include <qscroller.h> |
| 65 | #include <qscrollerproperties.h> |
| 66 | #include <private/qscrollerproperties_p.h> |
| 67 | #if QT_CONFIG(animation) |
| 68 | #include <QAbstractAnimation> |
| 69 | #endif |
| 70 | |
| 71 | QT_BEGIN_NAMESPACE |
| 72 | |
| 73 | #ifndef QT_NO_GESTURES |
| 74 | class QFlickGestureRecognizer; |
| 75 | #endif |
| 76 | |
| 77 | #if QT_CONFIG(animation) |
| 78 | class QScrollTimer; |
| 79 | #endif |
| 80 | class QScrollerPrivate : public QObject |
| 81 | { |
| 82 | Q_OBJECT |
| 83 | Q_DECLARE_PUBLIC(QScroller) |
| 84 | |
| 85 | public: |
| 86 | QScrollerPrivate(QScroller *q, QObject *target); |
| 87 | void init(); |
| 88 | |
| 89 | void sendEvent(QObject *o, QEvent *e); |
| 90 | |
| 91 | void setState(QScroller::State s); |
| 92 | |
| 93 | enum ScrollType { |
| 94 | ScrollTypeFlick = 0, |
| 95 | ScrollTypeScrollTo, |
| 96 | ScrollTypeOvershoot |
| 97 | }; |
| 98 | |
| 99 | struct ScrollSegment { |
| 100 | qint64 startTime; |
| 101 | qint64 deltaTime; |
| 102 | qreal startPos; |
| 103 | qreal deltaPos; |
| 104 | QEasingCurve curve; |
| 105 | qreal stopProgress; // whatever is.. |
| 106 | qreal stopPos; // ..reached first |
| 107 | ScrollType type; |
| 108 | }; |
| 109 | |
| 110 | bool pressWhileInactive(const QPointF &position, qint64 timestamp); |
| 111 | bool moveWhilePressed(const QPointF &position, qint64 timestamp); |
| 112 | bool releaseWhilePressed(const QPointF &position, qint64 timestamp); |
| 113 | bool moveWhileDragging(const QPointF &position, qint64 timestamp); |
| 114 | bool releaseWhileDragging(const QPointF &position, qint64 timestamp); |
| 115 | bool pressWhileScrolling(const QPointF &position, qint64 timestamp); |
| 116 | |
| 117 | void timerTick(); |
| 118 | void timerEventWhileDragging(); |
| 119 | void timerEventWhileScrolling(); |
| 120 | |
| 121 | bool prepareScrolling(const QPointF &position); |
| 122 | void handleDrag(const QPointF &position, qint64 timestamp); |
| 123 | |
| 124 | QPointF dpi() const; |
| 125 | void setDpi(const QPointF &dpi); |
| 126 | void setDpiFromWidget(QWidget *widget); |
| 127 | |
| 128 | void updateVelocity(const QPointF &deltaPixelRaw, qint64 deltaTime); |
| 129 | void pushSegment(ScrollType type, qreal deltaTime, qreal stopProgress, qreal startPos, qreal deltaPos, qreal stopPos, QEasingCurve::Type curve, Qt::Orientation orientation); |
| 130 | void recalcScrollingSegments(bool forceRecalc = false); |
| 131 | qreal scrollingSegmentsEndPos(Qt::Orientation orientation) const; |
| 132 | bool scrollingSegmentsValid(Qt::Orientation orientation) const; |
| 133 | void createScrollToSegments(qreal v, qreal deltaTime, qreal endPos, Qt::Orientation orientation, ScrollType type); |
| 134 | void createScrollingSegments(qreal v, qreal startPos, |
| 135 | qreal deltaTime, qreal deltaPos, |
| 136 | Qt::Orientation orientation); |
| 137 | void createScrollingSegments(const QPointF &v, const QPointF &startPos, const QPointF &ppm); |
| 138 | |
| 139 | void setContentPositionHelperDragging(const QPointF &deltaPos); |
| 140 | void setContentPositionHelperScrolling(); |
| 141 | |
| 142 | qreal nextSnapPos(qreal p, int dir, Qt::Orientation orientation) const; |
| 143 | static qreal nextSegmentPosition(QQueue<ScrollSegment> &segments, qint64 now, qreal oldPos); |
| 144 | |
| 145 | inline int frameRateSkip() const { return properties.d.data()->frameRate; } |
| 146 | |
| 147 | static const char *stateName(QScroller::State state); |
| 148 | static const char *inputName(QScroller::Input input); |
| 149 | |
| 150 | public slots: |
| 151 | void targetDestroyed(); |
| 152 | |
| 153 | public: |
| 154 | // non static |
| 155 | QObject *target; |
| 156 | QScrollerProperties properties; |
| 157 | #ifndef QT_NO_GESTURES |
| 158 | QFlickGestureRecognizer *recognizer; |
| 159 | Qt::GestureType recognizerType; |
| 160 | #endif |
| 161 | |
| 162 | // scroller state: |
| 163 | |
| 164 | // QPointer<QObject> scrollTarget; |
| 165 | QSizeF viewportSize; |
| 166 | QRectF contentPosRange; |
| 167 | QPointF contentPosition; |
| 168 | QPointF overshootPosition; // the number of pixels we are overshooting (before overshootDragResistanceFactor) |
| 169 | |
| 170 | // state |
| 171 | |
| 172 | bool enabled; |
| 173 | QScroller::State state; |
| 174 | bool firstScroll; // true if we haven't already send a scroll event |
| 175 | |
| 176 | QPointF oldVelocity; // the release velocity of the last drag |
| 177 | |
| 178 | QPointF pressPosition; |
| 179 | QPointF lastPosition; |
| 180 | qint64 pressTimestamp; |
| 181 | qint64 lastTimestamp; |
| 182 | |
| 183 | QPointF dragDistance; // the distance we should move during the next drag timer event |
| 184 | |
| 185 | QQueue<ScrollSegment> xSegments; |
| 186 | QQueue<ScrollSegment> ySegments; |
| 187 | |
| 188 | // snap positions |
| 189 | QList<qreal> snapPositionsX; |
| 190 | qreal snapFirstX; |
| 191 | qreal snapIntervalX; |
| 192 | QList<qreal> snapPositionsY; |
| 193 | qreal snapFirstY; |
| 194 | qreal snapIntervalY; |
| 195 | |
| 196 | QPointF pixelPerMeter; |
| 197 | |
| 198 | QElapsedTimer monotonicTimer; |
| 199 | |
| 200 | QPointF releaseVelocity; // the starting velocity of the scrolling state |
| 201 | #if QT_CONFIG(animation) |
| 202 | QScrollTimer *scrollTimer; |
| 203 | #endif |
| 204 | |
| 205 | QScroller *q_ptr; |
| 206 | }; |
| 207 | template <> |
| 208 | class QTypeInfo<QScrollerPrivate::ScrollSegment> |
| 209 | : public QTypeInfoMerger<QScrollerPrivate::ScrollSegment, QEasingCurve> {}; |
| 210 | |
| 211 | |
| 212 | QT_END_NAMESPACE |
| 213 | |
| 214 | #endif // QSCROLLER_P_H |
| 215 | |
| 216 | |