Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qscrollerproperties.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5#include <QPointer>
6#include <QObject>
7#include <QtCore/qmath.h>
8
10#include "private/qscrollerproperties_p.h"
11
12QT_BEGIN_NAMESPACE
13
14QT_IMPL_METATYPE_EXTERN_TAGGED(QScrollerProperties::OvershootPolicy,
15 QScrollerProperties__OvershootPolicy)
16QT_IMPL_METATYPE_EXTERN_TAGGED(QScrollerProperties::FrameRates,
17 QScrollerProperties__FrameRates)
18
19static QScrollerPropertiesPrivate *userDefaults = nullptr;
20static QScrollerPropertiesPrivate *systemDefaults = nullptr;
21
22QScrollerPropertiesPrivate *QScrollerPropertiesPrivate::defaults()
23{
24 if (!systemDefaults) {
25 QScrollerPropertiesPrivate spp;
26 spp.mousePressEventDelay = qreal(0.25);
27 spp.dragStartDistance = qreal(5.0 / 1000);
28 spp.dragVelocitySmoothingFactor = qreal(0.8);
29 spp.axisLockThreshold = qreal(0);
30 spp.scrollingCurve.setType(QEasingCurve::OutQuad);
31 spp.decelerationFactor = qreal(0.125);
32 spp.minimumVelocity = qreal(50.0 / 1000);
33 spp.maximumVelocity = qreal(500.0 / 1000);
34 spp.maximumClickThroughVelocity = qreal(66.5 / 1000);
35 spp.acceleratingFlickMaximumTime = qreal(1.25);
36 spp.acceleratingFlickSpeedupFactor = qreal(3.0);
37 spp.snapPositionRatio = qreal(0.5);
38 spp.snapTime = qreal(0.3);
39 spp.overshootDragResistanceFactor = qreal(0.5);
40 spp.overshootDragDistanceFactor = qreal(1);
41 spp.overshootScrollDistanceFactor = qreal(0.5);
42 spp.overshootScrollTime = qreal(0.7);
43 spp.hOvershootPolicy = QScrollerProperties::OvershootWhenScrollable;
44 spp.vOvershootPolicy = QScrollerProperties::OvershootWhenScrollable;
45 spp.frameRate = QScrollerProperties::Standard;
46
47 systemDefaults = new QScrollerPropertiesPrivate(spp);
48 }
49 return new QScrollerPropertiesPrivate(userDefaults ? *userDefaults : *systemDefaults);
50}
51
52/*!
53 \class QScrollerProperties
54 \brief The QScrollerProperties class stores the settings for a QScroller.
55 \since 4.8
56
57 \inmodule QtWidgets
58
59 The QScrollerProperties class stores the parameters used by QScroller.
60
61 The default settings are platform dependent so that Qt emulates the
62 platform behaviour for kinetic scrolling.
63
64 As a convention the QScrollerProperties are in physical units (meter,
65 seconds) and are converted by QScroller using the current DPI.
66
67 \sa QScroller
68*/
69
70/*!
71 Constructs new scroller properties.
72*/
73QScrollerProperties::QScrollerProperties()
74 : d(QScrollerPropertiesPrivate::defaults())
75{
76}
77
78/*!
79 Constructs a copy of \a sp.
80*/
81QScrollerProperties::QScrollerProperties(const QScrollerProperties &sp)
82 : d(new QScrollerPropertiesPrivate(*sp.d))
83{
84}
85
86/*!
87 Assigns \a sp to these scroller properties and returns a reference to these scroller properties.
88*/
89QScrollerProperties &QScrollerProperties::operator=(const QScrollerProperties &sp)
90{
91 *d.data() = *sp.d.data();
92 return *this;
93}
94
95/*!
96 Destroys the scroller properties.
97*/
98QScrollerProperties::~QScrollerProperties()
99{
100}
101
102/*!
103 Returns \c true if these scroller properties are equal to \a sp; otherwise returns \c false.
104*/
105bool QScrollerProperties::operator==(const QScrollerProperties &sp) const
106{
107 return *d.data() == *sp.d.data();
108}
109
110/*!
111 Returns \c true if these scroller properties are different from \a sp; otherwise returns \c false.
112*/
113bool QScrollerProperties::operator!=(const QScrollerProperties &sp) const
114{
115 return !(*d.data() == *sp.d.data());
116}
117
118bool QScrollerPropertiesPrivate::operator==(const QScrollerPropertiesPrivate &p) const
119{
120 bool same = true;
121 same &= (mousePressEventDelay == p.mousePressEventDelay);
122 same &= (dragStartDistance == p.dragStartDistance);
123 same &= (dragVelocitySmoothingFactor == p.dragVelocitySmoothingFactor);
124 same &= (axisLockThreshold == p.axisLockThreshold);
125 same &= (scrollingCurve == p.scrollingCurve);
126 same &= (decelerationFactor == p.decelerationFactor);
127 same &= (minimumVelocity == p.minimumVelocity);
128 same &= (maximumVelocity == p.maximumVelocity);
129 same &= (maximumClickThroughVelocity == p.maximumClickThroughVelocity);
130 same &= (acceleratingFlickMaximumTime == p.acceleratingFlickMaximumTime);
131 same &= (acceleratingFlickSpeedupFactor == p.acceleratingFlickSpeedupFactor);
132 same &= (snapPositionRatio == p.snapPositionRatio);
133 same &= (snapTime == p.snapTime);
134 same &= (overshootDragResistanceFactor == p.overshootDragResistanceFactor);
135 same &= (overshootDragDistanceFactor == p.overshootDragDistanceFactor);
136 same &= (overshootScrollDistanceFactor == p.overshootScrollDistanceFactor);
137 same &= (overshootScrollTime == p.overshootScrollTime);
138 same &= (hOvershootPolicy == p.hOvershootPolicy);
139 same &= (vOvershootPolicy == p.vOvershootPolicy);
140 same &= (frameRate == p.frameRate);
141 return same;
142}
143
144/*!
145 Sets the scroller properties for all new QScrollerProperties objects to \a sp.
146
147 Use this function to override the platform default properties returned by the default
148 constructor. If you only want to change the scroller properties of a single scroller, use
149 QScroller::setScrollerProperties()
150
151 \note Calling this function will not change the content of already existing
152 QScrollerProperties objects.
153
154 \sa unsetDefaultScrollerProperties()
155*/
156void QScrollerProperties::setDefaultScrollerProperties(const QScrollerProperties &sp)
157{
158 if (!userDefaults)
159 userDefaults = new QScrollerPropertiesPrivate(*sp.d);
160 else
161 *userDefaults = *sp.d;
162}
163
164/*!
165 Sets the scroller properties returned by the default constructor back to the platform default
166 properties.
167
168 \sa setDefaultScrollerProperties()
169*/
170void QScrollerProperties::unsetDefaultScrollerProperties()
171{
172 delete userDefaults;
173 userDefaults = nullptr;
174}
175
176/*!
177 Query the \a metric value of the scroller properties.
178
179 \sa setScrollMetric(), ScrollMetric
180*/
181QVariant QScrollerProperties::scrollMetric(ScrollMetric metric) const
182{
183 switch (metric) {
184 case MousePressEventDelay: return d->mousePressEventDelay;
185 case DragStartDistance: return d->dragStartDistance;
186 case DragVelocitySmoothingFactor: return d->dragVelocitySmoothingFactor;
187 case AxisLockThreshold: return d->axisLockThreshold;
188 case ScrollingCurve: return d->scrollingCurve;
189 case DecelerationFactor: return d->decelerationFactor;
190 case MinimumVelocity: return d->minimumVelocity;
191 case MaximumVelocity: return d->maximumVelocity;
192 case MaximumClickThroughVelocity: return d->maximumClickThroughVelocity;
193 case AcceleratingFlickMaximumTime: return d->acceleratingFlickMaximumTime;
194 case AcceleratingFlickSpeedupFactor:return d->acceleratingFlickSpeedupFactor;
195 case SnapPositionRatio: return d->snapPositionRatio;
196 case SnapTime: return d->snapTime;
197 case OvershootDragResistanceFactor: return d->overshootDragResistanceFactor;
198 case OvershootDragDistanceFactor: return d->overshootDragDistanceFactor;
199 case OvershootScrollDistanceFactor: return d->overshootScrollDistanceFactor;
200 case OvershootScrollTime: return d->overshootScrollTime;
201 case HorizontalOvershootPolicy: return QVariant::fromValue(d->hOvershootPolicy);
202 case VerticalOvershootPolicy: return QVariant::fromValue(d->vOvershootPolicy);
203 case FrameRate: return QVariant::fromValue(d->frameRate);
204 case ScrollMetricCount: break;
205 }
206 return QVariant();
207}
208
209/*!
210 Set a specific value of the \a metric ScrollerMetric to \a value.
211
212 \sa scrollMetric(), ScrollMetric
213*/
214void QScrollerProperties::setScrollMetric(ScrollMetric metric, const QVariant &value)
215{
216 switch (metric) {
217 case MousePressEventDelay: d->mousePressEventDelay = value.toReal(); break;
218 case DragStartDistance: d->dragStartDistance = value.toReal(); break;
219 case DragVelocitySmoothingFactor: d->dragVelocitySmoothingFactor = qBound(qreal(0), value.toReal(), qreal(1)); break;
220 case AxisLockThreshold: d->axisLockThreshold = qBound(qreal(0), value.toReal(), qreal(1)); break;
221 case ScrollingCurve: d->scrollingCurve = value.toEasingCurve(); break;
222 case DecelerationFactor: d->decelerationFactor = value.toReal(); break;
223 case MinimumVelocity: d->minimumVelocity = value.toReal(); break;
224 case MaximumVelocity: d->maximumVelocity = value.toReal(); break;
225 case MaximumClickThroughVelocity: d->maximumClickThroughVelocity = value.toReal(); break;
226 case AcceleratingFlickMaximumTime: d->acceleratingFlickMaximumTime = value.toReal(); break;
227 case AcceleratingFlickSpeedupFactor:d->acceleratingFlickSpeedupFactor = value.toReal(); break;
228 case SnapPositionRatio: d->snapPositionRatio = qBound(qreal(0), value.toReal(), qreal(1)); break;
229 case SnapTime: d->snapTime = value.toReal(); break;
230 case OvershootDragResistanceFactor: d->overshootDragResistanceFactor = value.toReal(); break;
231 case OvershootDragDistanceFactor: d->overshootDragDistanceFactor = qBound(qreal(0), value.toReal(), qreal(1)); break;
232 case OvershootScrollDistanceFactor: d->overshootScrollDistanceFactor = qBound(qreal(0), value.toReal(), qreal(1)); break;
233 case OvershootScrollTime: d->overshootScrollTime = value.toReal(); break;
234 case HorizontalOvershootPolicy: d->hOvershootPolicy = qvariant_cast<QScrollerProperties::OvershootPolicy>(value); break;
235 case VerticalOvershootPolicy: d->vOvershootPolicy = qvariant_cast<QScrollerProperties::OvershootPolicy>(value); break;
236 case FrameRate: d->frameRate = qvariant_cast<QScrollerProperties::FrameRates>(value); break;
237 case ScrollMetricCount: break;
238 }
239}
240
241/*!
242 \enum QScrollerProperties::FrameRates
243
244 This enum describes the available frame rates used while dragging or scrolling.
245
246 \value Fps60 60 frames per second
247 \value Fps30 30 frames per second
248 \value Fps20 20 frames per second
249 \value Standard the default value is 60 frames per second (which corresponds to QAbstractAnimation).
250*/
251
252/*!
253 \enum QScrollerProperties::OvershootPolicy
254
255 This enum describes the various modes of overshooting.
256
257 \value OvershootWhenScrollable Overshooting is possible when the content is scrollable. This is the
258 default.
259
260 \value OvershootAlwaysOff Overshooting is never enabled, even when the content is scrollable.
261
262 \value OvershootAlwaysOn Overshooting is always enabled, even when the content is not
263 scrollable.
264*/
265
266/*!
267 \enum QScrollerProperties::ScrollMetric
268
269 This enum contains the different scroll metric types. When not indicated otherwise the
270 setScrollMetric function expects a QVariant of type qreal.
271
272 See the QScroller documentation for further details of the concepts behind the different
273 values.
274
275 \value MousePressEventDelay This is the time a mouse press event is delayed when starting
276 a flick gesture in \c{[s]}. If the gesture is triggered within that time, no mouse press or
277 release is sent to the scrolled object. If it triggers after that delay the delayed
278 mouse press plus a faked release event at global position \c{QPoint(-QWIDGETSIZE_MAX,
279 -QWIDGETSIZE_MAX)} is sent. If the gesture is canceled, then both the delayed mouse
280 press plus the real release event are delivered.
281
282 \value DragStartDistance This is the minimum distance the touch or mouse point needs to be
283 moved before the flick gesture is triggered in \c m.
284
285 \value DragVelocitySmoothingFactor A value that describes to which extent new drag velocities are
286 included in the final scrolling velocity. This value should be in the range between \c 0 and
287 \c 1. The lower the value, the more smoothing is applied to the dragging velocity.
288
289 \value AxisLockThreshold Restricts the movement to one axis if the movement is inside an angle
290 around the axis. The threshold must be in the range \c 0 to \c 1.
291
292 \value ScrollingCurve The QEasingCurve used when decelerating the scrolling velocity after an
293 user initiated flick. Please note that this is the easing curve for the positions, \b{not}
294 the velocity: the default is QEasingCurve::OutQuad, which results in a linear decrease in
295 velocity (1st derivative) and a constant deceleration (2nd derivative).
296
297 \value DecelerationFactor This factor influences how long it takes the scroller to decelerate
298 to 0 velocity. The actual value depends on the chosen ScrollingCurve. For most
299 types the value should be in the range from \c 0.1 to \c 2.0
300
301 \value MinimumVelocity The minimum velocity that is needed after ending the touch or releasing
302 the mouse to start scrolling in \c{m/s}.
303
304 \value MaximumVelocity This is the maximum velocity that can be reached in \c{m/s}.
305
306 \value MaximumClickThroughVelocity This is the maximum allowed scroll speed for a click-through
307 in \c{m/s}. This means that a click on a currently (slowly) scrolling object will not only stop
308 the scrolling but the click event will also be delivered to the UI control. This is
309 useful when using exponential-type scrolling curves.
310
311 \value AcceleratingFlickMaximumTime This is the maximum time in \c seconds that a flick gesture
312 can take to be recognized as an accelerating flick. If set to zero no such gesture is
313 detected. An "accelerating flick" is a flick gesture executed on an already scrolling object.
314 In such cases the scrolling speed is multiplied by AcceleratingFlickSpeedupFactor in order to
315 accelerate it.
316
317 \value AcceleratingFlickSpeedupFactor The current speed is multiplied by this number if an
318 accelerating flick is detected. Should be \c{>= 1}.
319
320 \value SnapPositionRatio This is the distance that the user must drag the area between two snap
321 points in order to snap it to the next position. \c{0.33} means that the scroll must only
322 reach one third of the distance between two snap points to snap to the next one. The ratio must
323 be between \c 0 and \c 1.
324
325 \value SnapTime This is the time factor for the scrolling curve. A lower value means that the
326 scrolling will take longer. The scrolling distance is independent of this value.
327
328 \value OvershootDragResistanceFactor This value is the factor between the mouse dragging and
329 the actual scroll area movement (during overshoot). The factor must be between \c 0 and \c 1.
330
331 \value OvershootDragDistanceFactor This is the maximum distance for overshoot movements while
332 dragging. The actual overshoot distance is calculated by multiplying this value with the
333 viewport size of the scrolled object. The factor must be between \c 0 and \c 1.
334
335 \value OvershootScrollDistanceFactor This is the maximum distance for overshoot movements while
336 scrolling. The actual overshoot distance is calculated by multiplying this value with the
337 viewport size of the scrolled object. The factor must be between \c 0 and \c 1.
338
339 \value OvershootScrollTime This is the time in \c seconds that is used to play the
340 complete overshoot animation.
341
342 \value HorizontalOvershootPolicy This is the horizontal overshooting policy (see OvershootPolicy).
343
344 \value VerticalOvershootPolicy This is the horizontal overshooting policy (see OvershootPolicy).
345
346 \value FrameRate This is the frame rate which should be used while dragging or scrolling.
347 QScroller uses a QAbstractAnimation timer internally to sync all scrolling operations to other
348 animations that might be active at the same time. If the standard value of 60 frames per
349 second is too fast, it can be lowered with this setting,
350 while still being in-sync with QAbstractAnimation. Please note that only the values of the
351 FrameRates enum are allowed here.
352
353 \value ScrollMetricCount This is always the last entry.
354*/
355
356QT_END_NAMESPACE
QT_BEGIN_NAMESPACE static QT_IMPL_METATYPE_EXTERN_TAGGED(QScrollerProperties::OvershootPolicy, QScrollerProperties__OvershootPolicy) QT_IMPL_METATYPE_EXTERN_TAGGED(QScrollerProperties QScrollerPropertiesPrivate * systemDefaults