Mercurial > hg > easyhg
comparison src/panned.cpp @ 370:b9c153e00e84
Move source files to src/
author | Chris Cannam |
---|---|
date | Thu, 24 Mar 2011 10:27:51 +0000 |
parents | panned.cpp@8fd71f570884 |
children | 44cef6368690 |
comparison
equal
deleted
inserted
replaced
369:19cce6d2c470 | 370:b9c153e00e84 |
---|---|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ | |
2 | |
3 /* | |
4 EasyMercurial | |
5 | |
6 Based on HgExplorer by Jari Korhonen | |
7 Copyright (c) 2010 Jari Korhonen | |
8 Copyright (c) 2011 Chris Cannam | |
9 Copyright (c) 2011 Queen Mary, University of London | |
10 | |
11 This program is free software; you can redistribute it and/or | |
12 modify it under the terms of the GNU General Public License as | |
13 published by the Free Software Foundation; either version 2 of the | |
14 License, or (at your option) any later version. See the file | |
15 COPYING included with this distribution for more information. | |
16 */ | |
17 | |
18 #include "panned.h" | |
19 #include "debug.h" | |
20 | |
21 #include <QScrollBar> | |
22 #include <QWheelEvent> | |
23 #include <QTimer> | |
24 | |
25 #include <cmath> | |
26 | |
27 #include <iostream> | |
28 | |
29 Panned::Panned() : | |
30 m_dragging(false) | |
31 { | |
32 m_dragTimer = new QTimer(this); | |
33 m_dragTimerMs = 50; | |
34 connect(m_dragTimer, SIGNAL(timeout()), this, SLOT(dragTimerTimeout())); | |
35 setRenderHints(QPainter::Antialiasing | | |
36 QPainter::TextAntialiasing); | |
37 } | |
38 | |
39 void | |
40 Panned::resizeEvent(QResizeEvent *ev) | |
41 { | |
42 DEBUG << "Panned::resizeEvent()" << endl; | |
43 | |
44 QPointF nearpt = mapToScene(0, 0); | |
45 QPointF farpt = mapToScene(width(), height()); | |
46 QSizeF sz(farpt.x()-nearpt.x(), farpt.y()-nearpt.y()); | |
47 QRectF pr(nearpt, sz); | |
48 | |
49 QGraphicsView::resizeEvent(ev); | |
50 | |
51 if (pr != m_pannedRect) { | |
52 DEBUG << "Panned: setting panned rect to " << pr << endl; | |
53 m_pannedRect = pr; | |
54 centerOn(pr.center()); | |
55 emit pannedRectChanged(pr); | |
56 } | |
57 } | |
58 | |
59 void | |
60 Panned::setScene(QGraphicsScene *s) | |
61 { | |
62 if (!scene()) { | |
63 QGraphicsView::setScene(s); | |
64 return; | |
65 } | |
66 | |
67 QPointF nearpt = mapToScene(0, 0); | |
68 QPointF farpt = mapToScene(width(), height()); | |
69 QSizeF sz(farpt.x()-nearpt.x(), farpt.y()-nearpt.y()); | |
70 QRectF pr(nearpt, sz); | |
71 | |
72 QGraphicsView::setScene(s); | |
73 | |
74 DEBUG << "Panned::setScene: pr = " << pr << ", sceneRect = " << sceneRect() << endl; | |
75 | |
76 if (scene() && sceneRect().intersects(pr)) { | |
77 DEBUG << "Panned::setScene: restoring old rect " << pr << endl; | |
78 m_pannedRect = pr; | |
79 centerOn(pr.center()); | |
80 emit pannedRectChanged(pr); | |
81 } | |
82 } | |
83 | |
84 void | |
85 Panned::paintEvent(QPaintEvent *e) | |
86 { | |
87 QGraphicsView::paintEvent(e); | |
88 } | |
89 | |
90 void | |
91 Panned::drawForeground(QPainter *paint, const QRectF &) | |
92 { | |
93 QPointF nearpt = mapToScene(0, 0); | |
94 QPointF farpt = mapToScene(width(), height()); | |
95 QSizeF sz(farpt.x()-nearpt.x(), farpt.y()-nearpt.y()); | |
96 QRectF pr(nearpt, sz); | |
97 | |
98 if (pr != m_pannedRect) { | |
99 DEBUG << "Panned::drawForeground: visible rect " << pr << " differs from panned rect " << m_pannedRect << ", updating panned rect" <<endl; | |
100 if (pr.x() != m_pannedRect.x()) emit pannedContentsScrolled(); | |
101 m_pannedRect = pr; | |
102 emit pannedRectChanged(pr); | |
103 } | |
104 } | |
105 | |
106 void | |
107 Panned::zoomIn() | |
108 { | |
109 QMatrix m = matrix(); | |
110 m.scale(1.0 / 1.1, 1.0 / 1.1); | |
111 setMatrix(m); | |
112 } | |
113 | |
114 void | |
115 Panned::zoomOut() | |
116 { | |
117 QMatrix m = matrix(); | |
118 m.scale(1.1, 1.1); | |
119 setMatrix(m); | |
120 } | |
121 | |
122 void | |
123 Panned::slotSetPannedRect(QRectF pr) | |
124 { | |
125 centerOn(pr.center()); | |
126 // setSceneRect(pr); | |
127 // m_pannedRect = pr; | |
128 } | |
129 | |
130 void | |
131 Panned::wheelEvent(QWheelEvent *ev) | |
132 { | |
133 if (ev->modifiers() & Qt::ControlModifier) { | |
134 int d = ev->delta(); | |
135 if (d > 0) { | |
136 while (d > 0) { | |
137 zoomOut(); | |
138 d -= 120; | |
139 } | |
140 } else { | |
141 while (d < 0) { | |
142 zoomIn(); | |
143 d += 120; | |
144 } | |
145 } | |
146 } else { | |
147 emit wheelEventReceived(ev); | |
148 QGraphicsView::wheelEvent(ev); | |
149 } | |
150 } | |
151 | |
152 void | |
153 Panned::slotEmulateWheelEvent(QWheelEvent *ev) | |
154 { | |
155 QGraphicsView::wheelEvent(ev); | |
156 } | |
157 | |
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 | |
246 Panned::leaveEvent(QEvent *) | |
247 { | |
248 emit mouseLeaves(); | |
249 } |