Chris@57
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
cannam@45
|
2
|
cannam@45
|
3 /*
|
Chris@57
|
4 EasyMercurial
|
Chris@57
|
5
|
Chris@57
|
6 Based on HgExplorer by Jari Korhonen
|
Chris@57
|
7 Copyright (c) 2010 Jari Korhonen
|
Chris@57
|
8 Copyright (c) 2010 Chris Cannam
|
Chris@57
|
9 Copyright (c) 2010 Queen Mary, University of London
|
Chris@57
|
10
|
cannam@45
|
11 This program is free software; you can redistribute it and/or
|
cannam@45
|
12 modify it under the terms of the GNU General Public License as
|
cannam@45
|
13 published by the Free Software Foundation; either version 2 of the
|
cannam@45
|
14 License, or (at your option) any later version. See the file
|
cannam@45
|
15 COPYING included with this distribution for more information.
|
cannam@45
|
16 */
|
cannam@45
|
17
|
cannam@45
|
18 #include "panner.h"
|
cannam@45
|
19 #include "panned.h"
|
cannam@45
|
20
|
cannam@45
|
21 #include <QPolygon>
|
cannam@45
|
22 #include <QMouseEvent>
|
cannam@45
|
23 #include <QColor>
|
Chris@46
|
24 #include <QGLWidget>
|
cannam@45
|
25
|
cannam@45
|
26 #include <iostream>
|
cannam@45
|
27
|
cannam@45
|
28 class PannerScene : public QGraphicsScene
|
cannam@45
|
29 {
|
cannam@45
|
30 public:
|
cannam@45
|
31 friend class Panner;
|
cannam@45
|
32 };
|
cannam@45
|
33
|
cannam@45
|
34 Panner::Panner() :
|
cannam@45
|
35 m_clicked(false)
|
cannam@45
|
36 {
|
Chris@46
|
37 // setViewport(new QGLWidget());
|
cannam@45
|
38 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
cannam@45
|
39 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
Chris@48
|
40 setOptimizationFlags(QGraphicsView::DontSavePainterState |
|
Chris@48
|
41 QGraphicsView::IndirectPainting);
|
cannam@45
|
42 setMouseTracking(true);
|
cannam@45
|
43 setInteractive(false);
|
cannam@45
|
44 }
|
cannam@45
|
45
|
cannam@45
|
46 void
|
cannam@45
|
47 Panner::setScene(QGraphicsScene *s)
|
cannam@45
|
48 {
|
cannam@45
|
49 if (scene()) {
|
cannam@45
|
50 disconnect(scene(), SIGNAL(sceneRectChanged(const QRectF &)),
|
cannam@45
|
51 this, SLOT(slotSceneRectChanged(const QRectF &)));
|
cannam@45
|
52 }
|
cannam@45
|
53 QGraphicsView::setScene(s);
|
cannam@45
|
54 if (scene()) fitInView(sceneRect(), Qt::IgnoreAspectRatio);
|
cannam@45
|
55 m_cache = QPixmap();
|
cannam@45
|
56 connect(scene(), SIGNAL(sceneRectChanged(const QRectF &)),
|
cannam@45
|
57 this, SLOT(slotSceneRectChanged(const QRectF &)));
|
cannam@45
|
58 }
|
cannam@45
|
59
|
cannam@45
|
60 void
|
cannam@45
|
61 Panner::connectToPanned(Panned *p)
|
cannam@45
|
62 {
|
cannam@45
|
63 connect(p, SIGNAL(pannedRectChanged(QRectF)),
|
cannam@45
|
64 this, SLOT(slotSetPannedRect(QRectF)));
|
cannam@45
|
65
|
cannam@45
|
66 connect(this, SIGNAL(pannedRectChanged(QRectF)),
|
cannam@45
|
67 p, SLOT(slotSetPannedRect(QRectF)));
|
Chris@53
|
68
|
Chris@53
|
69 connect(this, SIGNAL(zoomIn()),
|
Chris@53
|
70 p, SLOT(zoomIn()));
|
Chris@53
|
71
|
Chris@53
|
72 connect(this, SIGNAL(zoomOut()),
|
Chris@53
|
73 p, SLOT(zoomOut()));
|
cannam@45
|
74 }
|
cannam@45
|
75
|
cannam@45
|
76 void
|
cannam@45
|
77 Panner::slotSetPannedRect(QRectF rect)
|
cannam@45
|
78 {
|
cannam@45
|
79 m_pannedRect = rect;
|
cannam@45
|
80 viewport()->update();
|
cannam@45
|
81 }
|
cannam@45
|
82
|
cannam@45
|
83 void
|
cannam@45
|
84 Panner::resizeEvent(QResizeEvent *)
|
cannam@45
|
85 {
|
cannam@45
|
86 if (scene()) fitInView(sceneRect(), Qt::IgnoreAspectRatio);
|
cannam@45
|
87 m_cache = QPixmap();
|
cannam@45
|
88 }
|
cannam@45
|
89
|
cannam@45
|
90 void
|
cannam@45
|
91 Panner::slotSceneRectChanged(const QRectF &newRect)
|
cannam@45
|
92 {
|
cannam@45
|
93 if (!scene()) return; // spurious
|
cannam@45
|
94 fitInView(newRect, Qt::IgnoreAspectRatio);
|
cannam@45
|
95 m_cache = QPixmap();
|
cannam@45
|
96 viewport()->update();
|
cannam@45
|
97 }
|
cannam@45
|
98
|
cannam@45
|
99 void
|
cannam@45
|
100 Panner::paintEvent(QPaintEvent *e)
|
cannam@45
|
101 {
|
cannam@45
|
102 QPaintEvent *e2 = new QPaintEvent(e->region().boundingRect());
|
cannam@45
|
103 QGraphicsView::paintEvent(e2);
|
cannam@45
|
104
|
cannam@45
|
105 QPainter paint;
|
cannam@45
|
106 paint.begin(viewport());
|
cannam@45
|
107 paint.setClipRegion(e->region());
|
cannam@45
|
108
|
cannam@45
|
109 QPainterPath path;
|
cannam@45
|
110 path.addRect(rect());
|
cannam@45
|
111 path.addPolygon(mapFromScene(m_pannedRect));
|
cannam@45
|
112
|
cannam@45
|
113 QColor c(QColor::fromHsv(211, 194, 238));
|
cannam@45
|
114 c.setAlpha(80);
|
cannam@45
|
115 paint.setPen(Qt::NoPen);
|
cannam@45
|
116 paint.setBrush(c);
|
cannam@45
|
117 paint.drawPath(path);
|
cannam@45
|
118
|
cannam@45
|
119 paint.setBrush(Qt::NoBrush);
|
cannam@45
|
120 paint.setPen(QPen(QColor::fromHsv(211, 194, 238), 0));
|
cannam@45
|
121 paint.drawConvexPolygon(mapFromScene(m_pannedRect));
|
cannam@45
|
122
|
cannam@45
|
123 paint.end();
|
cannam@45
|
124
|
cannam@45
|
125 emit pannerChanged(m_pannedRect);
|
cannam@45
|
126 }
|
cannam@45
|
127
|
cannam@45
|
128 void
|
cannam@45
|
129 Panner::updateScene(const QList<QRectF> &rects)
|
cannam@45
|
130 {
|
cannam@45
|
131 if (!m_cache.isNull()) m_cache = QPixmap();
|
cannam@45
|
132 QGraphicsView::updateScene(rects);
|
cannam@45
|
133 }
|
cannam@45
|
134
|
cannam@45
|
135 void
|
cannam@45
|
136 Panner::drawItems(QPainter *painter, int numItems,
|
cannam@45
|
137 QGraphicsItem *items[],
|
cannam@45
|
138 const QStyleOptionGraphicsItem options[])
|
cannam@45
|
139 {
|
cannam@45
|
140 if (m_cache.size() != viewport()->size()) {
|
cannam@45
|
141
|
Chris@48
|
142 std::cerr << "Panner: recreating cache" << std::endl;
|
Chris@48
|
143
|
cannam@45
|
144 QGraphicsScene *s = scene();
|
cannam@45
|
145 if (!s) return;
|
cannam@45
|
146 PannerScene *ps = static_cast<PannerScene *>(s);
|
cannam@45
|
147
|
cannam@45
|
148 m_cache = QPixmap(viewport()->size());
|
cannam@45
|
149 m_cache.fill(Qt::transparent);
|
cannam@45
|
150 QPainter cachePainter;
|
cannam@45
|
151 cachePainter.begin(&m_cache);
|
cannam@45
|
152 cachePainter.setTransform(viewportTransform());
|
cannam@45
|
153 ps->drawItems(&cachePainter, numItems, items, options);
|
cannam@45
|
154 cachePainter.end();
|
cannam@45
|
155 }
|
cannam@45
|
156
|
cannam@45
|
157 painter->save();
|
cannam@45
|
158 painter->setTransform(QTransform());
|
cannam@45
|
159 painter->drawPixmap(0, 0, m_cache);
|
cannam@45
|
160 painter->restore();
|
cannam@45
|
161 }
|
cannam@45
|
162
|
cannam@45
|
163 void
|
cannam@45
|
164 Panner::mousePressEvent(QMouseEvent *e)
|
cannam@45
|
165 {
|
cannam@45
|
166 if (e->button() != Qt::LeftButton) {
|
cannam@45
|
167 QGraphicsView::mouseDoubleClickEvent(e);
|
cannam@45
|
168 return;
|
cannam@45
|
169 }
|
cannam@45
|
170 m_clicked = true;
|
cannam@45
|
171 m_clickedRect = m_pannedRect;
|
cannam@45
|
172 m_clickedPoint = e->pos();
|
cannam@45
|
173 }
|
cannam@45
|
174
|
cannam@45
|
175 void
|
cannam@45
|
176 Panner::mouseDoubleClickEvent(QMouseEvent *e)
|
cannam@45
|
177 {
|
cannam@45
|
178 if (e->button() != Qt::LeftButton) {
|
cannam@45
|
179 QGraphicsView::mouseDoubleClickEvent(e);
|
cannam@45
|
180 return;
|
cannam@45
|
181 }
|
cannam@45
|
182
|
cannam@45
|
183 moveTo(e->pos());
|
cannam@45
|
184 }
|
cannam@45
|
185
|
cannam@45
|
186 void
|
cannam@45
|
187 Panner::mouseMoveEvent(QMouseEvent *e)
|
cannam@45
|
188 {
|
cannam@45
|
189 if (!m_clicked) return;
|
cannam@45
|
190 QPointF cp = mapToScene(m_clickedPoint);
|
cannam@45
|
191 QPointF mp = mapToScene(e->pos());
|
cannam@45
|
192 QPointF delta = mp - cp;
|
cannam@45
|
193 QRectF nr = m_clickedRect;
|
cannam@45
|
194 nr.translate(delta);
|
cannam@45
|
195 slotSetPannedRect(nr);
|
cannam@45
|
196 emit pannedRectChanged(m_pannedRect);
|
cannam@45
|
197 viewport()->update();
|
cannam@45
|
198 }
|
cannam@45
|
199
|
cannam@45
|
200 void
|
cannam@45
|
201 Panner::mouseReleaseEvent(QMouseEvent *e)
|
cannam@45
|
202 {
|
cannam@45
|
203 if (e->button() != Qt::LeftButton) {
|
cannam@45
|
204 QGraphicsView::mouseDoubleClickEvent(e);
|
cannam@45
|
205 return;
|
cannam@45
|
206 }
|
cannam@45
|
207
|
cannam@45
|
208 if (m_clicked) {
|
cannam@45
|
209 mouseMoveEvent(e);
|
cannam@45
|
210 }
|
cannam@45
|
211
|
cannam@45
|
212 m_clicked = false;
|
cannam@45
|
213 viewport()->update();
|
cannam@45
|
214 }
|
cannam@45
|
215
|
cannam@45
|
216 void
|
cannam@45
|
217 Panner::wheelEvent(QWheelEvent *e)
|
cannam@45
|
218 {
|
Chris@53
|
219 int d = e->delta();
|
Chris@53
|
220 if (d > 0) {
|
Chris@53
|
221 while (d > 0) {
|
Chris@53
|
222 emit zoomOut();
|
Chris@53
|
223 d -= 120;
|
Chris@53
|
224 }
|
cannam@45
|
225 } else {
|
Chris@53
|
226 while (d < 0) {
|
Chris@53
|
227 emit zoomIn();
|
Chris@53
|
228 d += 120;
|
Chris@53
|
229 }
|
cannam@45
|
230 }
|
cannam@45
|
231 }
|
cannam@45
|
232
|
cannam@45
|
233 void
|
cannam@45
|
234 Panner::moveTo(QPoint p)
|
cannam@45
|
235 {
|
cannam@45
|
236 QPointF sp = mapToScene(p);
|
cannam@45
|
237 QRectF nr = m_pannedRect;
|
cannam@45
|
238 double d = sp.x() - nr.center().x();
|
cannam@45
|
239 nr.translate(d, 0);
|
cannam@45
|
240 slotSetPannedRect(nr);
|
cannam@45
|
241 emit pannedRectChanged(m_pannedRect);
|
cannam@45
|
242 viewport()->update();
|
cannam@45
|
243 }
|
cannam@45
|
244
|