KISS πŸ‡ΊπŸ‡¦

Stop the war!

Stop the war in Ukraine! Fuck putin!

More information is at: https://war.ukraine.ua/.

There is a fund to support the Ukrainian Army: https://savelife.in.ua/en/donate/, and there is a special bank account that accepts funds in multiple currencies: https://bank.gov.ua/en/about/support-the-armed-forces. I donated to them. Please donate if you can!

Killer putin

Killer putin. Source: politico.eu.

Arrested putin

"It hasn't happened yet, but it will happen sooner or later. Beautiful photo, isn't it?" Source: twitter.

Old: Qt Symbian: kinetic scrolling helper

| comments

It was long-long time ago. Two years back actually. I wrote an application for the Symbian-based smartphones using Qt 4. Another post on this is here.

What was very unusual was the lack of kinetic scrolling — it’s the thing everyone’s got used to on mobile devices: when you swipe a list, it continues to scroll in that direction for a while. The first solution I’d found was FlickCharm, and the second one showed up some time later: QtScroller. I can’t recollect now what pros and cons each had, but unfortunately neither worked perfectly.

So to be able to switch them easily and leave which one was better, I created a kinetic scrolling wrapper class:

(scroller.h) download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// http://www.egeek.me

#ifndef SCROLLER_H
#define SCROLLER_H

#include <QWidget>

/*
  Wrapper that provides kinetic scrolling for widgets.
  Can use either FlickCharm or QtScroller.
*/

class ScrollerPrivate;

class Scroller : public QObject
{
public:
    /* Activates kinetic scrolling on the given widget.
      The vertical scrollbar is usually hidden, but can be shown
      when withVScrollBar is true.
      If passClickEventsOnly is true, only mouse click events
      will be propagated to the widget. It's the default behavior.
    */
    static void activateOn(QWidget *widget, bool withVScrollBar = false, bool passClickEventsOnly = true);

protected:
    bool eventFilter(QObject *obj, QEvent *evt);

private:
    Scroller();
    ~Scroller();

    Scroller(const Scroller &);
    Scroller &operator=(const Scroller &);

    ScrollerPrivate *const d_ptr;

    Q_DECLARE_PRIVATE(Scroller)
};

#endif // SCROLLER_H
(scroller.cpp) download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// http://www.egeek.me

#include "scroller.h"
#include <QtDebug>
#include <QAbstractItemView>
#include <QAbstractScrollArea>
#include <QWebFrame>
#include <QWebView>
#include <QMouseEvent>
#include <QApplication>
#include <QHash>
#include <QSet>

// Undefine to use QtScroller
//#define USE_FLICKCHARM

#ifdef USE_FLICKCHARM
#include "flickcharm.h"
#else
#include "qtscroller.h"

/* Max number of pixels between mouse press and release
  events that triggers sending fake mouse events */
#define SINGLE_CLICK_MAX_DIST   15
#endif


class ScrollerPrivate
{
public:
    static Scroller *instance();

    /* Adjusts (decreases) deceleration factor of scroller to be
      able to scroll more. */
    static void adjustDecelerationFactor(QtScroller* scroller);

    /* Reduces the mouse press delay in case when not only
      mouse click events are passed. */
    static void reduceMousePressDelay(QtScroller *scroller);

    QHash<QWidget *, QPoint> startPoints;
    QSet<QEvent *> ignoredEvents;
};

Scroller *ScrollerPrivate::instance()
{
    static Scroller scroller;
    return &scroller;
}

void ScrollerPrivate::adjustDecelerationFactor(QtScroller *scroller)
{
    if (!scroller)
        return;

    QtScrollerProperties props = scroller->scrollerProperties();
    float df = props.scrollMetric(QtScrollerProperties::DecelerationFactor).toFloat();
    df /= 2;
    props.setScrollMetric(QtScrollerProperties::DecelerationFactor, QVariant(df));
    scroller->setScrollerProperties(props);
}

void ScrollerPrivate::reduceMousePressDelay(QtScroller *scroller)
{
    if (!scroller)
        return;

    QtScrollerProperties props = scroller->scrollerProperties();
    props.setScrollMetric(QtScrollerProperties::MousePressEventDelay, .1f);
    scroller->setScrollerProperties(props);
}


Scroller::Scroller() :
    d_ptr(new ScrollerPrivate())
{
}

Scroller::~Scroller()
{
    delete d_ptr;
}

void Scroller::activateOn(QWidget *widget, bool withVScrollBar, bool passClickEventsOnly)
{
#ifdef USE_FLICKCHARM
    FlickCharm::instance()->activateOn(widget);
#else
    QtScroller::ScrollerGestureType type = QtScroller::LeftMouseButtonGesture;

    QAbstractScrollArea *scrollArea = qobject_cast<QAbstractScrollArea*>(widget);
    if (scrollArea) {
        if (!withVScrollBar) {
            scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
        }
        scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);


        QAbstractItemView *itemView = qobject_cast<QAbstractItemView*>(scrollArea);
        if (itemView) {
            // fix scrolling of lists
            itemView->setHorizontalScrollMode(QAbstractItemView::ScrollPerPixel);
            itemView->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
        }

        if (passClickEventsOnly) {
            scrollArea->viewport()->installEventFilter(ScrollerPrivate::instance());
            scrollArea->installEventFilter(ScrollerPrivate::instance());
        }

        QtScroller::grabGesture(scrollArea->viewport(), type);
        QtScroller::grabGesture(scrollArea, type);

        QtScroller *scroller = QtScroller::scroller(scrollArea->viewport());
        ScrollerPrivate::adjustDecelerationFactor(scroller);
        if (!passClickEventsOnly) {
            ScrollerPrivate::reduceMousePressDelay(scroller);
        }

        scroller = QtScroller::scroller(scrollArea);
        ScrollerPrivate::adjustDecelerationFactor(scroller);
        if (!passClickEventsOnly) {
            ScrollerPrivate::reduceMousePressDelay(scroller);
        }

        return;
    }

    QWebView *webView = qobject_cast<QWebView*>(widget);
    if (webView) {
        QWebFrame *frame = webView->page()->mainFrame();
        if (!withVScrollBar) {
            frame->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAlwaysOff);
        }
        frame->setScrollBarPolicy(Qt::Horizontal, Qt::ScrollBarAlwaysOff);

        if (passClickEventsOnly) {
            webView->installEventFilter(ScrollerPrivate::instance());
        }

        QtScroller::grabGesture(webView, type);

        if (!passClickEventsOnly) {
            ScrollerPrivate::reduceMousePressDelay(QtScroller::scroller(webView));
        }

        return;
    }

    qWarning() << "Scroller only works on QAbstractScrollArea (and derived classes)";
    qWarning() << "or QWebView (and derived classes)";
#endif
}

bool Scroller::eventFilter(QObject *obj, QEvent *evt)
{
#ifndef USE_FLICKCHARM
    Q_D(Scroller);

    /* to avoid selection in webviews and lists before
      scrolling has started, we need to consume all mouse
      events, except for single clicks */

    QWidget *widget = qobject_cast<QWidget *>(obj);
    Q_ASSERT(widget);
    switch (evt->type())
    {
    case QMouseEvent::MouseMove:
        /* don't propagate mouse move events */
        return true;
    case QMouseEvent::MouseButtonPress:
        /* on mouse press save the pos to the hash
          in order to check if it makes up a single click */
        if (d->ignoredEvents.contains(evt))
        {
            d->ignoredEvents.remove(evt);
            return false;
        }
        else
            d->startPoints[widget] = static_cast<QMouseEvent *>(evt)->pos();
        return true;
    case QMouseEvent::MouseButtonRelease:
        if (d->ignoredEvents.contains(evt))
        {
            d->ignoredEvents.remove(evt);
            return false;
        }
        else if (d->startPoints.contains(widget))
        {
            QPoint startPos = d->startPoints[widget];
            d->startPoints.remove(widget);

            /* if the distance is less than a limit,
              we consider it a single click */
            QPoint pos = static_cast<QMouseEvent *>(evt)->pos();
            if ((pos - startPos).manhattanLength() < SINGLE_CLICK_MAX_DIST)
            {
                QMouseEvent *event1 = new QMouseEvent(QEvent::MouseButtonPress,
                                                      pos, Qt::LeftButton,
                                                      Qt::LeftButton, Qt::NoModifier);
                QMouseEvent *event2 = new QMouseEvent(QEvent::MouseButtonRelease,
                                                      pos, Qt::LeftButton,
                                                      Qt::LeftButton, Qt::NoModifier);

                /* put the artifical events in the ignoredEvents set
                  to let them pass this method */
                d->ignoredEvents << event1 << event2;
                /* for some reason, postEvent caused a lot of events,
                  almost recursively, here */
                QApplication::sendEvent(obj, event1);
                QApplication::sendEvent(obj, event2);

                delete event2;
                delete event1;
            }
        }
        return true;
    default:
        break;
    }
#endif

    return QObject::eventFilter(obj, evt);
}

Maybe someone will find it useful.

Comments