-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmaptilesprovider.cpp
More file actions
262 lines (225 loc) · 7.09 KB
/
maptilesprovider.cpp
File metadata and controls
262 lines (225 loc) · 7.09 KB
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
/*============================================================
* 地图瓦片提供模块
* 功能 : 根据地图显示模块的需求,从网络上获取瓦片数据,供其使用,
* 并具备缓存机制,自动将获取到的数据保存在缓存中,下次取用时无需再次联网获取
* 模块重要性: 重要
*
* 作者 : 刘梓懿
*
* -----------------------------------------------------------
* 变更履历 :
*
* 2013.3.14 从 MapTilesFrame 中独立出该模块
* 2013.3.16 修正几个内存泄漏问题
*
============================================================*/
#include "maptilesprovider.h"
/*============================================================
* MapTilesProvider 构造函数
* 功能说明 : 初始化所需的变量,并连接下载完成的信号到我们自定义的槽函数
*
* 函数重要性: 重要
*
* 参数说明 : 指向父对象的指针(Qt 默认参数)
* 返回值 : 无
* 作者 : 刘梓懿
* -----------------------------------------------------------
* 变更履历 :
*
============================================================*/
MapTilesProvider::MapTilesProvider(QObject *parent) :
QObject(parent)
{
setMapType("/NormalMap/");
setDataSource(LocalData);
setServerAddress(QCoreApplication::applicationDirPath() + "/离线地图包/");
setTileFileType(".png");
connect(&nam,SIGNAL(finished(QNetworkReply*)),this,SLOT(slotFinished(QNetworkReply*)));
}
/*============================================================
* 瓦片调取函数
* 功能说明 : 尝试从缓存中加载地图显示模块所需的瓦片,并返回其指针
* 如果不存在
* 函数重要性: 重要
*
* 参数说明 : 新的视口坐标
* (视口坐标为视口的左上角点坐标,以服务器坐标系为基准)
* 返回值 : 无
* 作者 : 刘梓懿
* -----------------------------------------------------------
* 变更履历 :
*
============================================================*/
QImage *MapTilesProvider::getTile(const QString &tileName, bool getDirectPath)
{
//Note: QImage 使用了Implicit Data Sharing,拷贝一份QImage对象速度很快
QString path;
if(getDirectPath)
{
path = getServerAddress() + tileName;
}
else
{
path = getServerAddress() + getMapType() + tileName + getTileFileType();
}
qDebug() << "path: " << path;
if(imgCache[path] != NULL)
{
qDebug() << "tile is CACHED" << path;
return new QImage(*imgCache[path]);
}
else
{
if(getDataSource() == NetworkData)
{
QNetworkRequest request;
request.setUrl(QUrl(path));
request.setAttribute(QNetworkRequest::User, path);
request.setAttribute(QNetworkRequest::UserMax, getDirectPath);
qDebug() << "tile is NOT CACHED, sending request: " << request.url().toString();
nam.get(request);
return NULL;
}
else
{
qDebug() << "reading local tile file " << path;
QImage *img = new QImage(path);
if(img->isNull())
{
qDebug() << "file not exist or corrupted";
return NULL;
}
else
{
if(!imgCache.insert(path, img))
{
qDebug() << "ERROR!!! failed to insert local file " << path << " into cache";
qDebug() << "Cache size at this time" << imgCache.size();
}
return new QImage(*img);
}
}
}
}
//QImage *MapTilesProvider::getImage(const QString &imagePath)
//{
// //Note: QImage 使用了Implicit Data Sharing,拷贝一份QImage对象速度很快
// QString path(getServerAddress() + imagePath);
// qDebug() << "path: " << path;
// if(imgCache[path] != NULL)
// {
// qDebug() << "tile is CACHED" << path;
// return new QImage(*imgCache[path]);
// }
// else
// {
// if(getDataSource() == NetworkData)
// {
// QNetworkRequest request;
// request.setUrl(QUrl(path));
// request.setAttribute(QNetworkRequest::User, path);
// qDebug() << "tile is NOT CACHED, sending request: " << request.url().toString();
// nam.get(request);
// return NULL;
// }
// else
// {
// qDebug() << "reading local tile file " << path;
// QImage *img = new QImage(path);
// if(img->isNull())
// {
// qDebug() << "file not exist or corrupted";
// return NULL;
// }
// else
// {
// if(!imgCache.insert(path, img))
// {
// qDebug() << "ERROR!!! failed to insert local file " << path << " into cache";
// qDebug() << "Cache size at this time" << imgCache.size();
// }
// return new QImage(*img);
// }
// }
// }
//}
QString MapTilesProvider::getMapType() const
{
return m_mapType;
}
void MapTilesProvider::setMapType(QString mapType)
{
m_mapType = mapType;
emit mapTypeChanged(mapType);
}
MapTilesProvider::DataSource MapTilesProvider::getDataSource() const
{
return m_dataSource;
}
void MapTilesProvider::setDataSource(MapTilesProvider::DataSource dataSource)
{
m_dataSource = dataSource;
emit dataSourceChanged(dataSource);
}
QString MapTilesProvider::getServerAddress() const
{
return QString(m_serverAddress);
}
void MapTilesProvider::setServerAddress(const QString &newServerAddress)
{
if(!QUrl(newServerAddress).isValid())
{
//qDebug() << "NOT A VALID newServerAddress!";
return;
}
else
{
m_serverAddress = newServerAddress;
emit serverAddressChanged(m_serverAddress);
}
}
QString MapTilesProvider::getTileFileType() const
{
return QString(m_tileFileType);
}
void MapTilesProvider::setTileFileType(const QString &newTileFileType)
{
m_tileFileType = newTileFileType;
emit tileFileTypeChanged(m_tileFileType);
}
void MapTilesProvider::slotFinished(QNetworkReply *reply)
{
QString path = reply->request().attribute(QNetworkRequest::User).toString();
bool getDirectPath = reply->request().attribute(QNetworkRequest::UserMax).toBool();
qDebug() << "slotFinished called for tile: " << path;
if(reply->error())
{
qDebug() << "network reply ERROR for tile: " << path;
return;
}
QImage *replyImg = new QImage(QImage::fromData(reply->readAll()));
//判断网络传回的图片是否损坏
if(replyImg->isNull())
{
qDebug() << "reply data is NOT A VALID IMAGE for tile: " << path;
return;
}
else
{
if(!imgCache.insert(path, replyImg))
{
qDebug() << "ERROR!!! failed to insert " << path << " into cache";
qDebug() << "Cache size at this time" << imgCache.size();
return;
}
}
//QImage *tileImg = new QImage(*replyImg);
if(getDirectPath)
{
emit readyImage(path);
}
else
{
emit readyTile(path);
}
}