summaryrefslogtreecommitdiffstats
path: root/src/experimental/qkineticlistcontroller.cpp
blob: 29d75b2b0ad48e64ec24ebb39d92b85925c42958 (plain)
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the Itemviews NG project on Trolltech Labs.
**
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/

#include "qkineticlistcontroller.h"
#include "qkineticlistcontroller_p.h"
#include <qgraphicssceneevent.h>
#include <qdatetime.h>
#include <qdebug.h>

QT_BEGIN_NAMESPACE

QtKineticListControllerPrivate::QtKineticListControllerPrivate()
    : timerId(0), timerInterval(50), timestamp(0),
      scrollVelocity(0), maximumScrollVelocity(50),
      frictionFactor(0.85), velocityThreshold(0.05),
      movementThreshold(5),
      overshoot(false), centerOnItem(false)
{
}

QtKineticListControllerPrivate::~QtKineticListControllerPrivate()
{
}

bool QtKineticListControllerPrivate::updateOffset()
{
    if (!view)
        return false;
    qreal offset = view->offset();
    qreal max = view->maximumOffset();
    bool cont = kineticUpdateOffset(&offset, max);
    qreal value = overshoot ? offset : qBound<qreal>(0, offset, max);
    view->setOffset(value);
    return cont;
}

/*!
  \internal
  Update the offset and the velocity.
  
  return true if the timer should be continued
 */

bool QtKineticListControllerPrivate::kineticUpdateOffset(qreal *offset, qreal max)
{
    static const qreal k = -0.5; // overshoot spring constant
    static const qreal l = -0.02; // center spring constant

    *offset += scrollVelocity;
    
    // check for overshoot at the top
    if (*offset <= 0) {
        if (overshoot) {
            scrollVelocity = qMin(k * *offset, scrollVelocity + k * *offset);
        } else {
            *offset = 0;
            scrollVelocity = 0;
            return false;
        }
    }

    // check for overshoot at the bottom
    if (*offset >= max) {
        if (overshoot) {
            scrollVelocity = qMax(k * (*offset - max), scrollVelocity + k * (*offset - max));
        } else {
            scrollVelocity = 0;
            *offset = max;
            return false;
        }
    }

    // ### FIXME: this code may not work with small items
    // make sure that an item is at the center when we stop
    if (centerOnItem) {
        const QSizeF viewSize = view->size();
        const QPointF viewCenter(viewSize.width() / 2, viewSize.height() / 2);
        const int centerIndex = view->itemAt(viewCenter);
        const QPointF itemCenter = view->itemGeometry(centerIndex).center();
        const QPointF delta = viewCenter - itemCenter;
        if (view->orientation() == Qt::Horizontal)
            scrollVelocity += l * delta.x();
        else
            scrollVelocity += l * delta.y();
        scrollVelocity *= frictionFactor;
        return true;
    }

    // apply friction
    scrollVelocity *= frictionFactor;
    if (qAbs<qreal>(scrollVelocity) < velocityThreshold) {
        scrollVelocity = 0;
        return false;
    }
    return true;
}

void QtKineticListControllerPrivate::startTimer()
{
    Q_Q(QtKineticListController);
    if (timerId == 0)
        timerId = q->startTimer(timerInterval);
}

int QtKineticListControllerPrivate::currentTime() const
{
    QTime t = QTime::currentTime();
    return t.hour() * 3600000 + t.minute() * 60000 + t.second() * 1000 + t.msec();
}

/*!
    \class QtKineticListController
    \brief this is a list controller that implements kinetic scrolling

    \sa QtListWidgetNG
*/
QtKineticListController::QtKineticListController(QObject *parent)
    : QtListController(*(new QtKineticListControllerPrivate), parent)
{
    Q_D(QtKineticListController);
    d->startTimer();
}

/*!
 */
QtKineticListController::~QtKineticListController()
{
}

/*!
  Sets the scroll velocity to the given \a velocity.
  Note that setting this value wil also start the scrolling animation.
 */
void QtKineticListController::setScrollVelocity(qreal velocity)
{
    Q_D(QtKineticListController);
    d->scrollVelocity = qMin(velocity, d->maximumScrollVelocity);
    d->startTimer();
}

/*!
  Returns the scroll velocity.
 */
qreal QtKineticListController::scrollVelocity() const
{
    Q_D(const QtKineticListController);
    return d->scrollVelocity;
}

/*!
  Sets the maximum velocity allowed when scrolling is set to
  the given \a velocity.
 */
void QtKineticListController::setMaximumScrollVelocity(qreal velocity)
{
    Q_D(QtKineticListController);
    d->maximumScrollVelocity = velocity;
}

/*!
  Returns the maximum velocity allowed when scrolling.
 */
qreal QtKineticListController::maximumScrollVelocity() const
{
    Q_D(const QtKineticListController);
    return d->maximumScrollVelocity;
}

/*!
  Depending on the given \a enabled value, the controller will scroll past
  the first and last item.
 */
void QtKineticListController::setOvershootEnabled(bool enabled)
{
    Q_D(QtKineticListController);
    d->overshoot = enabled;
}

/*!
  Returns true if the controller is allowed to scroll past
  the first and last item, otherwise returns false.
 */
bool QtKineticListController::isOvershootEnabled() const
{
    Q_D(const QtKineticListController);
    return d->overshoot;
}

/*!
  Depending on the given \a enable value, the controller will ensure
  that when the scrolling stops, an item will be in the center of the view.
 */
void QtKineticListController::setCenterOnItemEnabled(bool enable)
{
    Q_D(QtKineticListController);
    d->centerOnItem = enable;
}

/*!
  Returns true if the controller ensures that an item is in the center
  of the view when the scrolling stops, otherwise returns false.
 */
bool QtKineticListController::isCenterOnItemEnabled() const
{
    Q_D(const QtKineticListController);
    return d->centerOnItem;
}

/*!
 */
void QtKineticListController::stop()
{
    Q_D(QtKineticListController);

    killTimer(d->timerId);
    d->timerId = 0;

    d->timestamp = d->currentTime();
    d->timeDelta = 0;
    d->movement = 0.;

    d->scrollVelocity = 0;
}

/*!
  \reimp
 */
bool QtKineticListController::hideEvent(QHideEvent *event)
{
    Q_UNUSED(event);
    Q_D(QtKineticListController);
    killTimer(d->timerId);
    d->timerId = 0;
    return true;
}

/*!
  \reimp
 */
bool QtKineticListController::mouseMoveEvent(QGraphicsSceneMouseEvent *event, const QTransform &transform)
{
    Q_UNUSED(event);
    Q_UNUSED(transform);
    Q_D(QtKineticListController);
    if (!d->view)
        return false;

    int oldTimestamp = d->timestamp;
    d->timestamp = d->currentTime();
    d->timeDelta = d->timestamp - oldTimestamp;

    if (d->view->orientation() == Qt::Horizontal)
        d->movement = event->lastPos().x() - event->pos().x();
    else
        d->movement = event->lastPos().y() - event->pos().y();

    qreal offset = d->view->offset() + d->movement;
    qreal max = d->view->maximumOffset();
    d->view->setOffset(d->overshoot ? offset : qBound<qreal>(0, offset, max));

    return true;
}

/*!
  \reimp
 */
bool QtKineticListController::mousePressEvent(QGraphicsSceneMouseEvent *event, const QTransform &transform)
{
    Q_UNUSED(event);
    Q_UNUSED(transform);
    Q_D(QtKineticListController);

    if (!d->centerOnItem)
        d->scrollVelocity = 0;

    killTimer(d->timerId);
    d->timerId = 0;

    d->timestamp = d->currentTime();
    d->timeDelta = 0;
    d->movement = 0.;

    stop();

    return true;
}

/*!
  \reimp
 */
bool QtKineticListController::mouseReleaseEvent(QGraphicsSceneMouseEvent *event, const QTransform &transform)
{
    Q_D(QtKineticListController);
    if (!d->view)
        return false;

    qreal dt = qreal(d->timeDelta) / qreal(d->timerInterval);
    QPointF dp = event->pos() - event->buttonDownPos(event->button());
    qreal totalMovement = (d->view->orientation() == Qt::Horizontal ? dp.x() : dp.y());

    if (qAbs<qreal>(totalMovement) > d->movementThreshold) {
        setScrollVelocity(d->movement / dt); // start kinetic scrolling
    } else {
        const qreal offset = d->view->offset();
        const qreal maximumOffset = d->view->maximumOffset();
        if (offset < 0 || offset > maximumOffset)
            d->startTimer(); // start bounce-back
        else if (d->centerOnItem && qAbs(d->scrollVelocity) > d->velocityThreshold)
            d->startTimer(); // continue scrolling
    }

    if (qAbs<qreal>(totalMovement) < d->movementThreshold)
        return QtListController::mouseReleaseEvent(event, transform);

    return true;
}

/*!
  \reimp
 */
bool QtKineticListController::wheelEvent(QGraphicsSceneWheelEvent *event, const QTransform &transform)
{
    Q_ASSERT(event);
    Q_UNUSED(transform);
    Q_D(QtKineticListController);
    if (d->view && d->view->orientation() == event->orientation())
        setScrollVelocity(d->scrollVelocity - event->delta() / 8.);
    return true;
}

/*!
  \reimp
 */
void QtKineticListController::timerEvent(QTimerEvent *event)
{
    Q_D(QtKineticListController);
    if (event->timerId() == d->timerId) {
        if (!d->updateOffset()) {
            killTimer(d->timerId);
            d->timerId = 0;
        }
    }
}

QT_END_NAMESPACE