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
|
#include "drape_surface.hpp"
#include "../drape_frontend/viewport.hpp"
#include "../drape_frontend/map_data_provider.hpp"
#include "../platform/platform.hpp"
#include "../drape/shader_def.hpp"
#include "../base/stl_add.hpp"
#include "../base/logging.hpp"
#include "../std/bind.hpp"
#include "../std/cmath.hpp"
#include <QtGui/QMouseEvent>
DrapeSurface::DrapeSurface()
: m_dragState(false)
, m_navigator(m_scales)
, m_contextFactory(NULL)
{
setSurfaceType(QSurface::OpenGLSurface);
QObject::connect(this, SIGNAL(heightChanged(int)), this, SLOT(sizeChanged(int)));
QObject::connect(this, SIGNAL(widthChanged(int)), this, SLOT(sizeChanged(int)));
///{ Temporary initialization
m_model.InitClassificator();
Platform::FilesList maps;
Platform & pl = GetPlatform();
pl.GetFilesByExt(pl.WritableDir(), DATA_FILE_EXTENSION, maps);
for_each(maps.begin(), maps.end(), bind(&model::FeaturesFetcher::AddMap, &m_model, _1));
///}
///
m_navigator.LoadState();
}
DrapeSurface::~DrapeSurface()
{
m_navigator.SaveState();
m_drapeEngine.Destroy();
m_contextFactory.Destroy();
}
void DrapeSurface::exposeEvent(QExposeEvent *e)
{
Q_UNUSED(e);
if (isExposed())
{
if (m_contextFactory.IsNull())
{
dp::ThreadSafeFactory * factory = new dp::ThreadSafeFactory(new QtOGLContextFactory(this));
m_contextFactory = dp::MasterPointer<dp::OGLContextFactory>(factory);
CreateEngine();
UpdateCoverage();
}
}
}
void DrapeSurface::mousePressEvent(QMouseEvent * e)
{
QWindow::mousePressEvent(e);
if (!isExposed())
return;
if (e->button() == Qt::LeftButton)
{
m2::PointF p = GetDevicePosition(e->pos());
m_navigator.StartDrag(p, 0);
UpdateCoverage();
m_dragState = true;
}
}
void DrapeSurface::mouseMoveEvent(QMouseEvent * e)
{
QWindow::mouseMoveEvent(e);
if (!isExposed())
return;
if (m_dragState)
{
m2::PointF p = GetDevicePosition(e->pos());
m_navigator.DoDrag(p, 0);
UpdateCoverage();
}
}
void DrapeSurface::mouseReleaseEvent(QMouseEvent * e)
{
QWindow::mouseReleaseEvent(e);
if (!isExposed())
return;
if (m_dragState)
{
m2::PointF p = GetDevicePosition(e->pos());
m_navigator.StopDrag(p, 0, false);
UpdateCoverage();
m_dragState = false;
}
}
void DrapeSurface::wheelEvent(QWheelEvent * e)
{
if (!m_dragState)
{
m_navigator.ScaleToPoint(GetDevicePosition(e->pos()), exp(e->delta() / 360.0), 0);
UpdateCoverage();
}
}
void DrapeSurface::CreateEngine()
{
dp::RefPointer<dp::OGLContextFactory> f(m_contextFactory.GetRefPointer());
float pixelRatio = devicePixelRatio();
typedef df::MapDataProvider::TReadIDsFn TReadIDsFn;
typedef df::MapDataProvider::TReadFeaturesFn TReadFeaturesFn;
typedef df::MapDataProvider::TReadIdCallback TReadIdCallback;
typedef df::MapDataProvider::TReadFeatureCallback TReadFeatureCallback;
TReadIDsFn idReadFn = [this](TReadIdCallback const & fn, m2::RectD const & r, int scale)
{
m_model.ForEachFeatureID(r, fn, scale);
};
TReadFeaturesFn featureReadFn = [this](TReadFeatureCallback const & fn, vector<FeatureID> const & ids)
{
m_model.ReadFeatures(fn, ids);
};
m_drapeEngine = TEnginePrt(new df::DrapeEngine(f, df::Viewport(pixelRatio, 0, 0, width(), height()),
df::MapDataProvider(idReadFn, featureReadFn)));
}
void DrapeSurface::UpdateCoverage()
{
m_drapeEngine->UpdateCoverage(m_navigator.Screen());
}
void DrapeSurface::sizeChanged(int)
{
if (!m_drapeEngine.IsNull())
{
float vs = devicePixelRatio();
int w = width() * vs;
int h = height() * vs;
m_navigator.OnSize(0, 0, w, h);
m_drapeEngine->Resize(width(), height());
UpdateCoverage();
}
}
m2::PointF DrapeSurface::GetDevicePosition(QPoint const & p)
{
qreal const ratio = devicePixelRatio();
return m2::PointF(p.x() * ratio, p.y() * ratio);
}
|