scroller.cpp
229 lines
| 5.7 KiB
| text/x-c
|
CppLexer
/ src / scroller.cpp
Michal Klocek
|
r855 | /**************************************************************************** | ||
** | ||||
** Copyright (C) 2012 Digia Plc | ||||
** All rights reserved. | ||||
** For any questions to Digia, please use contact form at http://qt.digia.com | ||||
** | ||||
** This file is part of the Qt Commercial Charts Add-on. | ||||
** | ||||
** $QT_BEGIN_LICENSE$ | ||||
** Licensees holding valid Qt Commercial licenses may use this file in | ||||
** accordance with the Qt Commercial License Agreement provided with the | ||||
** Software or, alternatively, in accordance with the terms contained in | ||||
** a written agreement between you and Digia. | ||||
** | ||||
** If you have questions regarding the use of this file, please use | ||||
** contact form at http://qt.digia.com | ||||
** $QT_END_LICENSE$ | ||||
** | ||||
****************************************************************************/ | ||||
#include "scroller_p.h" | ||||
#include "qlegend.h" | ||||
#include <QGraphicsSceneMouseEvent> | ||||
Michal Klocek
|
r996 | #include <QDebug> | ||
Michal Klocek
|
r855 | |||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
Jani Honkonen
|
r2097 | Scroller::Scroller() | ||
: m_ticker(this), | ||||
m_state(Idle), | ||||
m_moveThreshold(10), | ||||
m_timeTreshold(50) | ||||
Michal Klocek
|
r855 | { | ||
} | ||||
Scroller::~Scroller() | ||||
{ | ||||
} | ||||
Jani Honkonen
|
r2104 | void Scroller::mousePressEvent(QGraphicsSceneMouseEvent *event) | ||
Michal Klocek
|
r855 | { | ||
if (event->button() == Qt::LeftButton) { | ||||
switch (m_state) { | ||||
case Idle: | ||||
m_state = Pressed; | ||||
m_offset = offset(); | ||||
m_press = event->pos(); | ||||
m_timeStamp = QTime::currentTime(); | ||||
event->accept(); | ||||
break; | ||||
case Scroll: | ||||
m_state = Stop; | ||||
Michal Klocek
|
r1773 | m_speed = QPointF(0, 0); | ||
Michal Klocek
|
r855 | m_offset = offset(); | ||
m_press = event->pos(); | ||||
event->accept(); | ||||
break; | ||||
case Pressed: | ||||
case Move: | ||||
case Stop: | ||||
Jani Honkonen
|
r2097 | qWarning() << __FUNCTION__ << "Scroller unexpected state" << m_state; | ||
Michal Klocek
|
r855 | event->ignore(); | ||
break; | ||||
} | ||||
} | ||||
} | ||||
Jani Honkonen
|
r2104 | void Scroller::mouseMoveEvent(QGraphicsSceneMouseEvent *event) | ||
Michal Klocek
|
r855 | { | ||
QPointF delta = event->pos() - m_press; | ||||
switch (m_state) { | ||||
case Pressed: | ||||
case Stop: | ||||
if (qAbs(delta.x()) > m_moveThreshold || qAbs(delta.y()) > m_moveThreshold) { | ||||
m_state = Move; | ||||
m_timeStamp = QTime::currentTime(); | ||||
m_distance = QPointF(0, 0); | ||||
m_press = event->pos(); | ||||
event->accept(); | ||||
Jani Honkonen
|
r2097 | } else { | ||
Michal Klocek
|
r855 | event->ignore(); | ||
} | ||||
Jani Honkonen
|
r2097 | break; | ||
Michal Klocek
|
r855 | case Move: | ||
setOffset(m_offset - delta); | ||||
calculateSpeed(event->pos()); | ||||
event->accept(); | ||||
break; | ||||
case Idle: | ||||
case Scroll: | ||||
Jani Honkonen
|
r2097 | qWarning() << __FUNCTION__ << "Scroller unexpected state" << m_state; | ||
Michal Klocek
|
r855 | event->ignore(); | ||
break; | ||||
} | ||||
} | ||||
Jani Honkonen
|
r2104 | void Scroller::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) | ||
Michal Klocek
|
r855 | { | ||
if (event->button() == Qt::LeftButton) { | ||||
switch (m_state) { | ||||
case Scroll: | ||||
m_state = Stop; | ||||
m_speed = QPointF(0, 0); | ||||
m_offset = offset(); | ||||
event->accept(); | ||||
break; | ||||
case Pressed: | ||||
m_state = Idle; | ||||
//if (m_timeStamp.elapsed() < m_clickedPressDelay) { | ||||
//emit clicked(m_offset.toPoint()); | ||||
//} | ||||
event->accept(); | ||||
break; | ||||
case Move: | ||||
calculateSpeed(event->pos()); | ||||
m_offset = offset(); | ||||
m_press = event->pos(); | ||||
if (m_speed == QPointF(0, 0)) { | ||||
m_state = Idle; | ||||
Jani Honkonen
|
r2097 | } else { | ||
Michal Klocek
|
r1049 | m_speed /= 3.75; | ||
Michal Klocek
|
r855 | m_state = Scroll; | ||
Michal Klocek
|
r1049 | m_ticker.start(25); | ||
Michal Klocek
|
r855 | } | ||
event->accept(); | ||||
break; | ||||
case Stop: | ||||
case Idle: | ||||
Jani Honkonen
|
r2097 | qWarning() << __FUNCTION__ << "Scroller unexpected state" << m_state; | ||
Michal Klocek
|
r855 | event->ignore(); | ||
break; | ||||
} | ||||
} | ||||
} | ||||
void Scroller::scrollTick() | ||||
{ | ||||
switch (m_state) { | ||||
Jani Honkonen
|
r2097 | case Scroll: | ||
lowerSpeed(m_speed); | ||||
setOffset(m_offset - m_speed); | ||||
m_offset = offset(); | ||||
if (m_speed == QPointF(0, 0)) { | ||||
m_state = Idle; | ||||
Michal Klocek
|
r855 | m_ticker.stop(); | ||
Jani Honkonen
|
r2097 | } | ||
break; | ||||
case Stop: | ||||
m_ticker.stop(); | ||||
break; | ||||
case Idle: | ||||
case Move: | ||||
case Pressed: | ||||
qWarning() << __FUNCTION__ << "Scroller unexpected state" << m_state; | ||||
m_ticker.stop(); | ||||
Michal Klocek
|
r855 | break; | ||
} | ||||
} | ||||
Jani Honkonen
|
r2104 | void Scroller::lowerSpeed(QPointF &speed, qreal maxSpeed) | ||
Michal Klocek
|
r855 | { | ||
qreal x = qBound(-maxSpeed, speed.x(), maxSpeed); | ||||
qreal y = qBound(-maxSpeed, speed.y(), maxSpeed); | ||||
x = (x == 0) ? x : | ||||
(x > 0) ? qMax(qreal(0), x - m_fraction.x()) : qMin(qreal(0), x + m_fraction.x()); | ||||
y = (y == 0) ? y : | ||||
(y > 0) ? qMax(qreal(0), y - m_fraction.y()) : qMin(qreal(0), y + m_fraction.y()); | ||||
speed.setX(x); | ||||
speed.setY(y); | ||||
} | ||||
Jani Honkonen
|
r2104 | void Scroller::calculateSpeed(const QPointF &position) | ||
Michal Klocek
|
r855 | { | ||
if (m_timeStamp.elapsed() > m_timeTreshold) { | ||||
QPointF distance = position - m_press; | ||||
m_timeStamp = QTime::currentTime(); | ||||
m_speed = distance - m_distance; | ||||
m_distance = distance; | ||||
qreal fraction = qMax(qAbs(m_speed.x()), qAbs(m_speed.y())); | ||||
if (fraction != 0) { | ||||
m_fraction.setX(qAbs(m_speed.x() / fraction)); | ||||
m_fraction.setY(qAbs(m_speed.y() / fraction)); | ||||
Jani Honkonen
|
r2097 | } else { | ||
Michal Klocek
|
r855 | m_fraction.setX(1); | ||
m_fraction.setY(1); | ||||
} | ||||
} | ||||
} | ||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | ||||
Jani Honkonen
|
r2104 | ScrollTicker::ScrollTicker(Scroller *scroller, QObject *parent) | ||
Jani Honkonen
|
r2097 | : QObject(parent), | ||
m_scroller(scroller) | ||||
Michal Klocek
|
r855 | { | ||
} | ||||
void ScrollTicker::start(int interval) | ||||
{ | ||||
Jani Honkonen
|
r2097 | if (!m_timer.isActive()) | ||
Michal Klocek
|
r855 | m_timer.start(interval, this); | ||
} | ||||
void ScrollTicker::stop() | ||||
{ | ||||
m_timer.stop(); | ||||
} | ||||
void ScrollTicker::timerEvent(QTimerEvent *event) | ||||
{ | ||||
Q_UNUSED(event); | ||||
m_scroller->scrollTick(); | ||||
} | ||||
Michal Klocek
|
r974 | #include "moc_scroller_p.cpp" | ||
Michal Klocek
|
r855 | QTCOMMERCIALCHART_END_NAMESPACE | ||