-
Notifications
You must be signed in to change notification settings - Fork 25
/
mapzoom.cpp
291 lines (251 loc) · 9.18 KB
/
mapzoom.cpp
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
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Qt Software Information ([email protected])
**
** This file is part of the Graphics Dojo project on Qt Labs.
**
** This file may be used under the terms of the GNU General Public
** License version 2.0 or 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of
** this file. Please review the following information to ensure GNU
** General Public Licensing requirements will be met:
** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
** http://www.gnu.org/copyleft/gpl.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at [email protected].
**
** 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 <QtCore>
#include <QtGui>
#include <QtWebKitWidgets>
#if QT_VERSION < 0x0040500
#error You need Qt 4.5 or newer
#endif
// how long (milliseconds) the user need to hold (after a tap on the screen)
// before triggering the magnifying glass feature
// 701, a prime number, is the sum of 229, 233, 239
// (all three are also prime numbers, consecutive!)
#define HOLD_TIME 701
// maximum size of the magnifier
// Hint: see above to find why I picked this one :)
#define MAX_MAGNIFIER 229
#define MAP_HTML "<html><head><script type=\"text/javascript\" " \
"src=\"http://maps.google.com/maps/api/js?sensor=false\"></script>" \
"<script type=\"text/javascript\">" \
"var map; function init(lat, lng) { "\
"map = new google.maps.Map(document.getElementById(\"map_canvas\"), " \
"{ zoom: 15, center: new google.maps.LatLng(lat, lng), " \
"disableDefaultUI: true, mapTypeId: google.maps.MapTypeId.ROADMAP });" \
"} </script>" \
"</head><body style=\"margin:0px; padding:0px;\">" \
"<div id=\"map_canvas\" style=\"width:100%; height:100%\"></div>" \
"</body></html>"
class GMaps: public QWebView
{
Q_OBJECT
private:
bool pressed;
bool snapped;
QPoint pressPos;
QPoint dragPos;
QBasicTimer tapTimer;
bool zoomed;
QWebPage *zoomPage;
QPixmap zoomPixmap;
QPixmap maskPixmap;
public:
GMaps(QWidget *parent = 0)
: QWebView(parent)
, pressed(false)
, snapped(false)
, zoomed(false)
{
zoomPage = new QWebPage(this);
connect(zoomPage, SIGNAL(repaintRequested(QRect)), SLOT(update()));
QString content = MAP_HTML;
QWebFrame *frame = page()->mainFrame();
frame->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAlwaysOff);
frame->setScrollBarPolicy(Qt::Horizontal, Qt::ScrollBarAlwaysOff);
frame->setHtml(content);
frame = zoomPage->mainFrame();
frame->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAlwaysOff);
frame->setScrollBarPolicy(Qt::Horizontal, Qt::ScrollBarAlwaysOff);
content.replace("zoom: 15", "zoom: 16");
frame->setHtml(content);
QTimer::singleShot(1000, this, SLOT(triggerLoading()));
}
void setCenter(qreal latitude, qreal longitude) {
QString code = "map.set_center(new google.maps.LatLng(%1, %2));";
QWebFrame *frame = page()->mainFrame();
frame->evaluateJavaScript(code.arg(latitude).arg(longitude));
frame = zoomPage->mainFrame();
frame->evaluateJavaScript(code.arg(latitude).arg(longitude));
}
private slots:
void triggerLoading() {
QString code = "init(59.9138204, 10.7387413)";
QWebFrame *frame = page()->mainFrame();
frame->evaluateJavaScript(code);
frame = zoomPage->mainFrame();
frame->evaluateJavaScript(code);
}
protected:
void timerEvent(QTimerEvent *event) {
QWebView::timerEvent(event);
if (!zoomed) {
zoomed = true;
tapTimer.stop();
zoomPage->setViewportSize(size() * 2);
QWebFrame *frame = page()->mainFrame();
qreal lat = frame->evaluateJavaScript("map.get_center().lat()").toDouble();
qreal lng = frame->evaluateJavaScript("map.get_center().lng()").toDouble();
setCenter(lat, lng);
}
update();
}
void mousePressEvent(QMouseEvent *event) {
pressed = snapped = true;
pressPos = dragPos = event->pos();
tapTimer.stop();
tapTimer.start(HOLD_TIME, this);
QWebView::mousePressEvent(event);
}
void mouseReleaseEvent(QMouseEvent *event) {
pressed = snapped = false;
tapTimer.stop();
if (!zoomed) {
QWebView::mouseReleaseEvent(event);
} else {
zoomed = false;
event->accept();
update();
// fake at the press position
QMouseEvent *ev = new QMouseEvent(QEvent::MouseButtonRelease,
pressPos, Qt::LeftButton,
Qt::LeftButton, Qt::NoModifier);
QWebView::mouseReleaseEvent(ev);
delete ev;
}
}
void mouseMoveEvent(QMouseEvent *event) {
if (!zoomed) {
if (!pressed || !snapped) {
QWebView::mouseMoveEvent(event);
return;
} else {
const int threshold = 40;
QPoint delta = event->pos() - pressPos;
if (snapped) {
snapped &= delta.x() < threshold;
snapped &= delta.y() < threshold;
snapped &= delta.x() > -threshold;
snapped &= delta.y() > -threshold;
}
if (!snapped)
tapTimer.stop();
}
} else {
dragPos = event->pos();
update();
}
}
void paintEvent(QPaintEvent *event) {
QWebView::paintEvent(event);
if (zoomed) {
int dim = qMin(width(), height());
int magnifierSize = qMin(MAX_MAGNIFIER, dim * 2 / 3);
int radius = magnifierSize / 2;
int ring = radius - 15;
QSize box = QSize(magnifierSize, magnifierSize);
// reupdate our mask
if (maskPixmap.size() != box) {
maskPixmap = QPixmap(box);
maskPixmap.fill(Qt::transparent);
QRadialGradient g;
g.setCenter(radius, radius);
g.setFocalPoint(radius, radius);
g.setRadius(radius);
g.setColorAt(1.0, QColor(255, 255, 255, 0));
g.setColorAt(0.5, QColor(128, 128, 128, 255));
QPainter mask(&maskPixmap);
mask.setRenderHint(QPainter::Antialiasing);
mask.setCompositionMode(QPainter::CompositionMode_Source);
mask.setBrush(g);
mask.setPen(Qt::NoPen);
mask.drawRect(maskPixmap.rect());
mask.setBrush(QColor(Qt::transparent));
mask.drawEllipse(g.center(), ring, ring);
mask.end();
}
QPoint center = dragPos - QPoint(0, radius);
center = center + QPoint(0, radius / 2);
QPoint corner = center - QPoint(radius, radius);
QPoint xy = center * 2 - QPoint(radius, radius);
if (zoomPixmap.size() != box) {
zoomPixmap = QPixmap(box);
zoomPixmap.fill(Qt::lightGray);
}
if (true) {
QPainter p(&zoomPixmap);
p.translate(-xy);
QWebFrame *frame = zoomPage->mainFrame();
frame->render(&p, QRect(xy, box));
p.end();
}
QPainterPath clipPath;
clipPath.addEllipse(center, ring, ring);
QPainter p(this);
p.setRenderHint(QPainter::Antialiasing);
p.setClipPath(clipPath);
p.drawPixmap(corner, zoomPixmap);
p.setClipping(false);
p.drawPixmap(corner, maskPixmap);
p.setPen(Qt::gray);
p.drawPath(clipPath);
p.end();
}
}
};
class MapZoom : public QMainWindow
{
Q_OBJECT
private:
GMaps *map;
public:
MapZoom(): QMainWindow(0) {
map = new GMaps(this);
setCentralWidget(map);
QMenu *menu = menuBar()->addMenu("&Place");
menu->addAction("&Oslo", this, SLOT(chooseOslo()));
menu->addAction("&Berlin", this, SLOT(chooseBerlin()));
menu->addAction("&Jakarta", this, SLOT(chooseJakarta()));
}
private slots:
void chooseOslo() {
map->setCenter(59.9138204, 10.7387413);
}
void chooseBerlin() {
map->setCenter(52.5056819, 13.3232027);
}
void chooseJakarta() {
map->setCenter(-6.211544, 106.845172);
}
};
#include "mapzoom.moc"
int main(int argc, char **argv)
{
#if defined(Q_WS_X11)
QApplication::setGraphicsSystem("raster");
#endif
QApplication app(argc, argv);
MapZoom w;
w.setWindowTitle("Maps (powered by Google)");
w.resize(600, 450);
w.show();
return app.exec();
}