From 32d200da9cc7a4dfb3f302f22ef5718a286845c9 Mon Sep 17 00:00:00 2001 From: Robert Griebl Date: Tue, 7 Dec 2010 14:29:28 +0100 Subject: Make QScroller examples Qt-namespace safe. --- examples/scroller/plot/plotwidget.h | 3 ++- examples/scroller/plot/settingswidget.h | 2 ++ examples/scroller/wheel/wheelwidget.h | 2 ++ 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/examples/scroller/plot/plotwidget.h b/examples/scroller/plot/plotwidget.h index 3b4d92d..4987ebc 100644 --- a/examples/scroller/plot/plotwidget.h +++ b/examples/scroller/plot/plotwidget.h @@ -45,10 +45,11 @@ #include #include +QT_BEGIN_NAMESPACE class QPushButton; class QLabel; - class QScroller; +QT_END_NAMESPACE class PlotWidget : public QWidget { diff --git a/examples/scroller/plot/settingswidget.h b/examples/scroller/plot/settingswidget.h index 2fb268c..0ea201d 100644 --- a/examples/scroller/plot/settingswidget.h +++ b/examples/scroller/plot/settingswidget.h @@ -44,12 +44,14 @@ #include +QT_BEGIN_NAMESPACE class QScroller; class QGridLayout; class QSpinBox; class QComboBox; class QCheckBox; class QPlainTextEdit; +QT_END_NAMESPACE class MetricItemUpdater; class SnapOverlay; diff --git a/examples/scroller/wheel/wheelwidget.h b/examples/scroller/wheel/wheelwidget.h index 818b6ab..c50f951 100644 --- a/examples/scroller/wheel/wheelwidget.h +++ b/examples/scroller/wheel/wheelwidget.h @@ -45,8 +45,10 @@ #include #include +QT_BEGIN_NAMESPACE class QPainter; class QRect; +QT_END_NAMESPACE class AbstractWheelWidget : public QWidget { Q_OBJECT -- cgit v0.12 From 3e0df49f978933b1e4e6b48c695bf813ec9a2828 Mon Sep 17 00:00:00 2001 From: Robert Griebl Date: Tue, 7 Dec 2010 14:29:59 +0100 Subject: Fix drag velocity smoothing in QScroller. The algorithm used for smoothing the drag velocity was wrong in many ways. Reviewed-by: Ralf Engels --- examples/scroller/plot/settingswidget.cpp | 2 +- src/gui/util/qscroller.cpp | 16 ++++++++++------ src/gui/util/qscrollerproperties.cpp | 9 +++++---- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/examples/scroller/plot/settingswidget.cpp b/examples/scroller/plot/settingswidget.cpp index af1e621..840e3fc 100644 --- a/examples/scroller/plot/settingswidget.cpp +++ b/examples/scroller/plot/settingswidget.cpp @@ -348,7 +348,7 @@ private: MetricItem items[] = { { METRIC(MousePressEventDelay), 1000, "ms", qreal(0), qreal(2000), qreal(10) }, { METRIC(DragStartDistance), 1000, "mm", qreal(1), qreal(20), qreal(0.1) }, - { METRIC(DragVelocitySmoothingFactor), 1, "", qreal(0), qreal(1), qreal(0.01) }, + { METRIC(DragVelocitySmoothingFactor), 1, "", qreal(0), qreal(1), qreal(0.1) }, { METRIC(AxisLockThreshold), 1, "", qreal(0), qreal(1), qreal(0.01) }, { METRIC(ScrollingCurve), 1, "", QEasingCurve(), 0, 0 }, diff --git a/src/gui/util/qscroller.cpp b/src/gui/util/qscroller.cpp index b7b5903..0955f58 100644 --- a/src/gui/util/qscroller.cpp +++ b/src/gui/util/qscroller.cpp @@ -1046,6 +1046,9 @@ void QScrollerPrivate::setDpiFromWidget(QWidget *widget) */ void QScrollerPrivate::updateVelocity(const QPointF &deltaPixelRaw, qint64 deltaTime) { + if (deltaTime <= 0) + return; + Q_Q(QScroller); QPointF ppm = q->pixelPerMeter(); const QScrollerPropertiesPrivate *sp = properties.d.data(); @@ -1057,14 +1060,12 @@ void QScrollerPrivate::updateVelocity(const QPointF &deltaPixelRaw, qint64 delta if (((deltaPixelRaw / qreal(deltaTime)).manhattanLength() / ((ppm.x() + ppm.y()) / 2) * 1000) > qreal(2.5)) deltaPixel = deltaPixelRaw * qreal(2.5) * ppm / 1000 / (deltaPixelRaw / qreal(deltaTime)).manhattanLength(); - qreal inversSmoothingFactor = ((qreal(1) - sp->dragVelocitySmoothingFactor) * qreal(deltaTime) / qreal(1000)); QPointF newv = -deltaPixel / qreal(deltaTime) * qreal(1000) / ppm; - newv = newv * (qreal(1) - inversSmoothingFactor) + releaseVelocity * inversSmoothingFactor; + if (releaseVelocity != QPointF(0, 0)) + newv = newv * sp->dragVelocitySmoothingFactor + releaseVelocity * (qreal(1) - sp->dragVelocitySmoothingFactor); - if (deltaPixel.x()) - releaseVelocity.setX(qBound(-sp->maximumVelocity, newv.x(), sp->maximumVelocity)); - if (deltaPixel.y()) - releaseVelocity.setY(qBound(-sp->maximumVelocity, newv.y(), sp->maximumVelocity)); + releaseVelocity.setX(qBound(-sp->maximumVelocity, newv.x(), sp->maximumVelocity)); + releaseVelocity.setY(qBound(-sp->maximumVelocity, newv.y(), sp->maximumVelocity)); qScrollerDebug() << " --> new velocity:" << releaseVelocity; } @@ -1569,6 +1570,9 @@ bool QScrollerPrivate::releaseWhileDragging(const QPointF &position, qint64 time Q_Q(QScroller); const QScrollerPropertiesPrivate *sp = properties.d.data(); + // handleDrag updates lastPosition, lastTimestamp and velocity + handleDrag(position, timestamp); + // check if we moved at all - this can happen if you stop a running // scroller with a press and release shortly afterwards QPointF deltaPixel = position - pressPosition; diff --git a/src/gui/util/qscrollerproperties.cpp b/src/gui/util/qscrollerproperties.cpp index d21f131..4a1f085 100644 --- a/src/gui/util/qscrollerproperties.cpp +++ b/src/gui/util/qscrollerproperties.cpp @@ -61,7 +61,7 @@ QScrollerPropertiesPrivate *QScrollerPropertiesPrivate::defaults() #ifdef Q_WS_MAEMO_5 spp.mousePressEventDelay = qreal(0); spp.dragStartDistance = qreal(2.5 / 1000); - spp.dragVelocitySmoothingFactor = qreal(0.15); + spp.dragVelocitySmoothingFactor = qreal(10); spp.axisLockThreshold = qreal(0); spp.scrollingCurve.setType(QEasingCurve::OutQuad); spp.decelerationFactor = 1.0; @@ -82,7 +82,7 @@ QScrollerPropertiesPrivate *QScrollerPropertiesPrivate::defaults() #else spp.mousePressEventDelay = qreal(0.25); spp.dragStartDistance = qreal(5.0 / 1000); - spp.dragVelocitySmoothingFactor = qreal(0.02); + spp.dragVelocitySmoothingFactor = qreal(0.8); spp.axisLockThreshold = qreal(0); spp.scrollingCurve.setType(QEasingCurve::OutQuad); spp.decelerationFactor = qreal(0.125); @@ -342,8 +342,9 @@ void QScrollerProperties::setScrollMetric(ScrollMetric metric, const QVariant &v moved before the flick gesture is triggered in \c m. \value DragVelocitySmoothingFactor A value that describes how much new drag velocities are - included in the final scrolling velocity. This value should be in the range between \c 0 and \c - 1. Low values meaning that the last dragging velocity is not very important. + included in the final scrolling velocity. This value should be in the range between \c 0 and + \c 1. The lower the value, the more smoothing will be applied to the dragging velocity. The + default value is \c 0.8. \value AxisLockThreshold If greater than zero a scroll movement will be restricted to one axis only if the movement is inside an angle about the axis. The threshold must be in the range \c 0 -- cgit v0.12 From 82bbc1c1611bde33680d22a1a3c6449e51d7b0b9 Mon Sep 17 00:00:00 2001 From: Robert Griebl Date: Wed, 15 Dec 2010 18:20:14 +0100 Subject: Resolve XRRSizes() from libxrandr for QScroller's DPI calculation. Reviewed-by: Denis Dzyubenko --- src/gui/kernel/qapplication_x11.cpp | 3 +++ src/gui/kernel/qt_x11_p.h | 2 ++ 2 files changed, 5 insertions(+) diff --git a/src/gui/kernel/qapplication_x11.cpp b/src/gui/kernel/qapplication_x11.cpp index ff98229..58ccda2 100644 --- a/src/gui/kernel/qapplication_x11.cpp +++ b/src/gui/kernel/qapplication_x11.cpp @@ -2015,12 +2015,15 @@ void qt_init(QApplicationPrivate *priv, int, (PtrXRRRootToScreen) xrandrLib.resolve("XRRRootToScreen"); X11->ptrXRRQueryExtension = (PtrXRRQueryExtension) xrandrLib.resolve("XRRQueryExtension"); + X11->ptrXRRSizes = + (PtrXRRSizes) xrandrLib.resolve("XRRSizes"); } # else X11->ptrXRRSelectInput = XRRSelectInput; X11->ptrXRRUpdateConfiguration = XRRUpdateConfiguration; X11->ptrXRRRootToScreen = XRRRootToScreen; X11->ptrXRRQueryExtension = XRRQueryExtension; + X11->ptrXRRSizes = XRRSizes; # endif if (X11->ptrXRRQueryExtension diff --git a/src/gui/kernel/qt_x11_p.h b/src/gui/kernel/qt_x11_p.h index 56c8094..d97264c 100644 --- a/src/gui/kernel/qt_x11_p.h +++ b/src/gui/kernel/qt_x11_p.h @@ -226,6 +226,7 @@ typedef void (*PtrXRRSelectInput)(Display *, Window, int); typedef int (*PtrXRRUpdateConfiguration)(XEvent *); typedef int (*PtrXRRRootToScreen)(Display *, Window); typedef Bool (*PtrXRRQueryExtension)(Display *, int *, int *); +typedef XRRScreenSize *(*PtrXRRSizes)(Display *, int, int *); #endif // QT_NO_XRANDR #ifndef QT_NO_XINPUT @@ -710,6 +711,7 @@ struct QX11Data PtrXRRUpdateConfiguration ptrXRRUpdateConfiguration; PtrXRRRootToScreen ptrXRRRootToScreen; PtrXRRQueryExtension ptrXRRQueryExtension; + PtrXRRSizes ptrXRRSizes; #endif // QT_NO_XRANDR }; -- cgit v0.12 From b78ffe51f9a4c4ac705e435d45fffe39864c032d Mon Sep 17 00:00:00 2001 From: Robert Griebl Date: Wed, 15 Dec 2010 18:22:15 +0100 Subject: Use XRandR to get the real DPI values for the screen. This is needed for the Xomap server, but may also apply to others. Reviewed-by: Ralf Engels --- src/gui/util/qscroller.cpp | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/src/gui/util/qscroller.cpp b/src/gui/util/qscroller.cpp index 0955f58..f651a06 100644 --- a/src/gui/util/qscroller.cpp +++ b/src/gui/util/qscroller.cpp @@ -64,6 +64,10 @@ #include +#if defined(Q_WS_X11) +# include "private/qt_x11_p.h" +#endif + QT_BEGIN_NAMESPACE @@ -986,38 +990,46 @@ bool QScroller::handleInput(Input input, const QPointF &position, qint64 timesta return false; } -#ifdef Q_WS_MAEMO_5 +#if !defined(Q_WS_MAC) +// the Mac version is implemented in qscroller_mac.mm QPointF QScrollerPrivate::realDpi(int screen) { +# ifdef Q_WS_MAEMO_5 Q_UNUSED(screen); + // The DPI value is hardcoded to 96 on Maemo5: // https://projects.maemo.org/bugzilla/show_bug.cgi?id=152525 // This value (260) is only correct for the N900 though, but // there's no way to get the real DPI at run time. return QPointF(260, 260); -} -#elif defined(Q_WS_MAC) - -// implemented in qscroller_mac.mm - -#else +# elif defined(Q_WS_X11) && !defined(QT_NO_XRANDR) + if (X11->use_xrandr && X11->ptrXRRSizes) { + int nsizes = 0; + XRRScreenSize *sizes = X11->ptrXRRSizes(X11->display, screen == -1 ? X11->defaultScreen : screen, &nsizes); + if (nsizes > 0 && sizes && sizes->width && sizes->height && sizes->mwidth && sizes->mheight) { + qScrollerDebug() << "XRandR DPI:" << QPointF(qreal(25.4) * qreal(sizes->width) / qreal(sizes->mwidth), + qreal(25.4) * qreal(sizes->height) / qreal(sizes->mheight)); + return QPointF(qreal(25.4) * qreal(sizes->width) / qreal(sizes->mwidth), + qreal(25.4) * qreal(sizes->height) / qreal(sizes->mheight)); + } + } +# endif -QPointF QScrollerPrivate::realDpi(int screen) -{ QWidget *w = QApplication::desktop()->screen(screen); return QPointF(w->physicalDpiX(), w->physicalDpiY()); } -#endif +#endif // !Q_WS_MAC + /*! \internal Returns the resolution of the used screen. */ QPointF QScrollerPrivate::dpi() const { - return pixelPerMeter / qreal(39.3700787); + return pixelPerMeter * qreal(0.0254); } /*! \internal @@ -1028,7 +1040,7 @@ QPointF QScrollerPrivate::dpi() const */ void QScrollerPrivate::setDpi(const QPointF &dpi) { - pixelPerMeter = dpi * qreal(39.3700787); + pixelPerMeter = dpi / qreal(0.0254); } /*! \internal -- cgit v0.12 From fe438d7d828021d7f86301af36fe8dff2768532a Mon Sep 17 00:00:00 2001 From: Robert Griebl Date: Wed, 15 Dec 2010 18:37:28 +0100 Subject: QScroller: nicer Overshoot bounce back animation. Reviewed-by: Ralf Engels --- src/gui/util/qscroller.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/gui/util/qscroller.cpp b/src/gui/util/qscroller.cpp index f651a06..2ca4a6e 100644 --- a/src/gui/util/qscroller.cpp +++ b/src/gui/util/qscroller.cpp @@ -1367,9 +1367,8 @@ void QScrollerPrivate::createScrollingSegments(qreal v, qreal startPos, qreal pp qreal oDistance = viewSize * sp->overshootScrollDistanceFactor * endV / sp->maximumVelocity; qreal oDeltaTime = sp->overshootScrollTime; - pushSegment(ScrollTypeOvershoot, oDeltaTime * 0.5, stopPos, stopPos + oDistance, sp->scrollingCurve.type(), orientation); - pushSegment(ScrollTypeOvershoot, oDeltaTime * 0.3, stopPos + oDistance, stopPos + oDistance * 0.3, QEasingCurve::InQuad, orientation); - pushSegment(ScrollTypeOvershoot, oDeltaTime * 0.2, stopPos + oDistance * 0.3, stopPos, QEasingCurve::OutQuad, orientation); + pushSegment(ScrollTypeOvershoot, oDeltaTime * 0.3, stopPos, stopPos + oDistance, sp->scrollingCurve.type(), orientation); + pushSegment(ScrollTypeOvershoot, oDeltaTime * 0.7, stopPos + oDistance, stopPos, sp->scrollingCurve.type(), orientation); } return; } -- cgit v0.12 From df30d58de183d13c649ef7e0fbb8e2b3658e0862 Mon Sep 17 00:00:00 2001 From: Robert Griebl Date: Wed, 15 Dec 2010 18:24:27 +0100 Subject: Removed obsolete Maemo 5 code --- src/gui/util/qscrollerproperties.cpp | 24 +----------------------- 1 file changed, 1 insertion(+), 23 deletions(-) diff --git a/src/gui/util/qscrollerproperties.cpp b/src/gui/util/qscrollerproperties.cpp index 4a1f085..2e52959 100644 --- a/src/gui/util/qscrollerproperties.cpp +++ b/src/gui/util/qscrollerproperties.cpp @@ -58,28 +58,6 @@ QScrollerPropertiesPrivate *QScrollerPropertiesPrivate::defaults() { if (!systemDefaults) { QScrollerPropertiesPrivate spp; -#ifdef Q_WS_MAEMO_5 - spp.mousePressEventDelay = qreal(0); - spp.dragStartDistance = qreal(2.5 / 1000); - spp.dragVelocitySmoothingFactor = qreal(10); - spp.axisLockThreshold = qreal(0); - spp.scrollingCurve.setType(QEasingCurve::OutQuad); - spp.decelerationFactor = 1.0; - spp.minimumVelocity = qreal(0.0195); - spp.maximumVelocity = qreal(6.84); - spp.maximumClickThroughVelocity = qreal(0.0684); - spp.acceleratingFlickMaximumTime = qreal(0.125); - spp.acceleratingFlickSpeedupFactor = qreal(3.0); - spp.snapPositionRatio = qreal(0.25); - spp.snapTime = qreal(1); - spp.overshootDragResistanceFactor = qreal(1); - spp.overshootDragDistanceFactor = qreal(0.3); - spp.overshootScrollDistanceFactor = qreal(0.3); - spp.overshootScrollTime = qreal(0.5); - spp.hOvershootPolicy = QScrollerProperties::OvershootWhenScrollable; - spp.vOvershootPolicy = QScrollerProperties::OvershootWhenScrollable; - spp.frameRate = QScrollerProperties::Fps30; -#else spp.mousePressEventDelay = qreal(0.25); spp.dragStartDistance = qreal(5.0 / 1000); spp.dragVelocitySmoothingFactor = qreal(0.8); @@ -104,7 +82,7 @@ QScrollerPropertiesPrivate *QScrollerPropertiesPrivate::defaults() spp.hOvershootPolicy = QScrollerProperties::OvershootWhenScrollable; spp.vOvershootPolicy = QScrollerProperties::OvershootWhenScrollable; spp.frameRate = QScrollerProperties::Standard; -#endif + systemDefaults = new QScrollerPropertiesPrivate(spp); } return new QScrollerPropertiesPrivate(userDefaults ? *userDefaults : *systemDefaults); -- cgit v0.12