comparison panned.cpp @ 168:4bad3c5c053a

* Add "Show summary" feature * Add simplistic kinetic scrolling to history widget
author Chris Cannam
date Tue, 14 Dec 2010 17:20:10 +0000
parents aaeab914f2a3
children 8fd71f570884
comparison
equal deleted inserted replaced
167:94be1e218655 168:4bad3c5c053a
18 #include "panned.h" 18 #include "panned.h"
19 #include "debug.h" 19 #include "debug.h"
20 20
21 #include <QScrollBar> 21 #include <QScrollBar>
22 #include <QWheelEvent> 22 #include <QWheelEvent>
23 #include <QTimer>
24
25 #include <cmath>
23 26
24 #include <iostream> 27 #include <iostream>
25 28
26 Panned::Panned() 29 Panned::Panned() :
27 { 30 m_dragging(false)
31 {
32 m_dragTimer = new QTimer(this);
33 m_dragTimerMs = 50;
34 connect(m_dragTimer, SIGNAL(timeout()), this, SLOT(dragTimerTimeout()));
28 setRenderHints(QPainter::Antialiasing | 35 setRenderHints(QPainter::Antialiasing |
29 QPainter::TextAntialiasing); 36 QPainter::TextAntialiasing);
30 } 37 }
31 38
32 void 39 void
147 { 154 {
148 QGraphicsView::wheelEvent(ev); 155 QGraphicsView::wheelEvent(ev);
149 } 156 }
150 157
151 void 158 void
159 Panned::mousePressEvent(QMouseEvent *ev)
160 {
161 if (dragMode() != QGraphicsView::ScrollHandDrag ||
162 ev->button() != Qt::LeftButton) {
163 QGraphicsView::mousePressEvent(ev);
164 return;
165 }
166
167 DEBUG << "Panned::mousePressEvent: have left button in drag mode" << endl;
168
169 setDragMode(QGraphicsView::NoDrag);
170 QGraphicsView::mousePressEvent(ev);
171 setDragMode(QGraphicsView::ScrollHandDrag);
172
173 if (!ev->isAccepted()) {
174 ev->accept();
175 m_dragging = true;
176 m_lastDragPos = ev->pos();
177 m_lastOrigin = QPoint(horizontalScrollBar()->value(),
178 verticalScrollBar()->value());
179 m_velocity = QPointF(0, 0);
180 m_dragTimer->start(m_dragTimerMs);
181 }
182
183 }
184
185 void
186 Panned::mouseMoveEvent(QMouseEvent *ev)
187 {
188 if (!m_dragging) {
189 QGraphicsView::mouseMoveEvent(ev);
190 return;
191 }
192 DEBUG << "Panned::mouseMoveEvent: dragging" << endl;
193 ev->accept();
194 QScrollBar *hBar = horizontalScrollBar();
195 QScrollBar *vBar = verticalScrollBar();
196 QPoint delta = ev->pos() - m_lastDragPos;
197 hBar->setValue(hBar->value() + (isRightToLeft() ? delta.x() : -delta.x()));
198 vBar->setValue(vBar->value() - delta.y());
199 m_lastDragPos = ev->pos();
200 }
201
202 void
203 Panned::mouseReleaseEvent(QMouseEvent *ev)
204 {
205 if (!m_dragging) {
206 QGraphicsView::mouseReleaseEvent(ev);
207 return;
208 }
209 DEBUG << "Panned::mouseReleaseEvent: dragging" << endl;
210 ev->accept();
211 m_dragging = false;
212 }
213
214 void
215 Panned::dragTimerTimeout()
216 {
217 QPoint origin = QPoint(horizontalScrollBar()->value(),
218 verticalScrollBar()->value());
219 if (m_dragging) {
220 m_velocity = QPointF
221 (float(origin.x() - m_lastOrigin.x()) / m_dragTimerMs,
222 float(origin.y() - m_lastOrigin.y()) / m_dragTimerMs);
223 m_lastOrigin = origin;
224 DEBUG << "Panned::dragTimerTimeout: velocity = " << m_velocity << endl;
225 } else {
226 if (origin == m_lastOrigin) {
227 m_dragTimer->stop();
228 }
229 float x = m_velocity.x(), y = m_velocity.y();
230 if (fabsf(x) > 1.0/m_dragTimerMs) x = x * 0.9f;
231 else x = 0.f;
232 if (fabsf(y) > 1.0/m_dragTimerMs) y = y * 0.9f;
233 else y = 0.f;
234 m_velocity = QPointF(x, y);
235 DEBUG << "Panned::dragTimerTimeout: velocity adjusted to " << m_velocity << endl;
236 m_lastOrigin = origin;
237 //!!! need to store origin in floats
238 horizontalScrollBar()->setValue(m_lastOrigin.x() +
239 m_velocity.x() * m_dragTimerMs);
240 verticalScrollBar()->setValue(m_lastOrigin.y() +
241 m_velocity.y() * m_dragTimerMs);
242 }
243 }
244
245 void
152 Panned::leaveEvent(QEvent *) 246 Panned::leaveEvent(QEvent *)
153 { 247 {
154 emit mouseLeaves(); 248 emit mouseLeaves();
155 } 249 }