forked from UIDO/UMapControl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Layer.cpp
292 lines (266 loc) · 8.13 KB
/
Layer.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
#include "Layer.h"
Layer::Layer(UMapControl *parent, QString name, QList<UM::Format> *typeList) : uMap(parent),
layerLabel(name),visible(true),selectable(true),sqlExcute(&parent->sqlExcute)
{
/*
* 这是直接创建新图层并写入数据库
* */
QUuid id = QUuid::createUuid();
layerID = QString("UISDO%1").arg(id.data1);
UM::Format f;
f.name = "UMID";
f.type = UM::UMapN;
headType.append(f);
sqlExcute->initLayer(layerID,layerLabel,typeList, &headType);
connect(this, SIGNAL(addGeoToScene(Geometry*)), uMap, SLOT(addGeoToScene(Geometry*)));
}
Layer::Layer(UMapControl *parent, QString id, QString name, bool visible, bool selectable):
uMap(parent),layerLabel(name),layerID(id),sqlExcute(&parent->sqlExcute)
{
/*
* 这得从数据库里读取表加载成图层
* */
this->visible = visible;
this->selectable = selectable;
QSqlQuery * query = sqlExcute->checkType(layerID);
while(query->next())
{
QString value = query->value(1).toString();
QString type = query->value(2).toString();
UM::Format t;
t.name = value;
t.type = type == "TEXT" ? UM::UMapT : UM::UMapN;
headType.append(t);
}
delete query;
query = 0;
connect(this, SIGNAL(addGeoToScene(Geometry*)), uMap, SLOT(addGeoToScene(Geometry*)));
}
Layer::~Layer()
{
sqlExcute->removeLayer(layerID);
}
QSqlQuery *Layer::searchInfo(QString field, QString text)
{
UM::DataType t = UM::UMapT;
for(int i=0; i<headType.size();i++)
{
if(headType.at(i).name == field)
{
t = headType.at(i).type;
break;
}
}
if(t == UM::UMapN)
{
bool ok;
text.toDouble(&ok);
if(!ok)
return nullptr;
}
return sqlExcute->searchInfo(layerID,field,t,text);
}
void Layer::setViewToItem(QString itemID)
{
QSqlQuery * query = sqlExcute->setViewToItem(getLayerID(),itemID);
while(query->next())
{
bool ok;
double x = query->value(0).toDouble(&ok);
if(!ok)
break;
double y = query->value(1).toDouble(&ok);
if(!ok)
break;
uMap->zoomTo(QPointF(x,y),uMap->zoomLevel());
break;
}
delete query;
query = 0;
}
void Layer::addGeo(Geometry::DataType data)
{
QList<Geometry::DataType> l;
l.append(data);
addGeos(&l);
}
void Layer::addGeos(QList<Geometry::DataType> *dataList)
{
sqlExcute->addItems(dataList,layerID, &headType);
emit uMap->updateMap();
}
QList<Geometry *> * Layer::getItems()
{
return &list;
}
void Layer::updatLayer(bool *isUpdate)
{
list.clear();
if(!*isUpdate)
return;
QSqlQuery * query =sqlExcute->updateLayer(layerID);
while(query->next() && *isUpdate)
{
int type = query->value(1).toInt();
Geometry * g = nullptr;
ILongInfo itemInfo = getInfo(query);
switch (type) {
case UM::iGeoCircle:
g = new GeoCircle(itemInfo.center,itemInfo.size,itemInfo.pen,itemInfo.brush);
break;
case UM::iGeoRect:
g = new GeoRect(itemInfo.center,itemInfo.size,itemInfo.pen,itemInfo.brush);
break;
case UM::iGeoPie:
g = new GeoPie(itemInfo.center,itemInfo.size,itemInfo.flags,itemInfo.pen,itemInfo.brush);
break;
case UM::iGeoStar:
g = new GeoStar(itemInfo.center,itemInfo.size,itemInfo.pen,itemInfo.brush);
break;
case UM::iGeoTri:
g = new GeoTri(itemInfo.center,itemInfo.size,itemInfo.pen,itemInfo.brush);
break;
case UM::iGeoPolygon:
g = new GeoPolygon(uMap,&itemInfo.list,itemInfo.flags,itemInfo.size,itemInfo.pen,itemInfo.brush);
break;
default:
break;
}
if(g && *isUpdate)
{
/*
* 图层顺序不能影响临时图层放在第二层,第三层用来保存GPS位置,
* 所有图层放到第一层,受到刷新顺序的影响
* 第0层是背景层
* */
g->setZValue(getLayerName() == "UISDO" ? 2 : 1);
if(type == UM::iGeoPolygon)
{
g->setPos(uMap->worldToScene(QPointF(g->getRect().minX,g->getRect().maxY)));
//g->setFlag(QGraphicsItem::ItemIsSelectable);
}
else
{
g->setPos(uMap->worldToScene(itemInfo.center));
g->setScale(uMap->itemScale);
g->rotate(itemInfo.flags);
}
if(itemInfo.label != "ILONGNULL")
g->setLabel(itemInfo.label);
g->setObjectName(QString("%1_%2").arg(layerID).arg(itemInfo.id));
emit addGeoToScene(g);
list.append(g);
}
}
delete query;
query = 0;
}
void Layer::setLabel(QString field)
{
sqlExcute->setLabel(layerID, field);
uMap->updateMap();
}
void Layer::updateGeoPenColor(quint32 geoID, QColor c)
{
sqlExcute->updateGeoColor(this->getLayerID(),geoID,"PEN",c);
uMap->updateMap();
}
void Layer::updateGeoBrushColor(quint32 geoID, QColor c)
{
sqlExcute->updateGeoColor(this->getLayerID(),geoID,"BRUSH",c);
uMap->updateMap();
}
void Layer::removeGeo(QString itemID)
{
sqlExcute->removeItem(getLayerID(), itemID);
uMap->updateMap();
}
QString Layer::getLayerName()
{
return layerLabel;
}
QString Layer::getLayerID()
{
return layerID;
}
QList<UM::Format> *Layer::getLayerHead()
{
return &headType;
}
void Layer::setVisible(bool b)
{
visible = b;
sqlExcute->setLayerVisible(layerID, b);
uMap->updateMap();
}
bool Layer::isVisible()
{
return visible;
}
void Layer::setSelectable(bool b)
{
selectable = b;
sqlExcute->setLayerSelectable(layerID, b);
uMap->updateMap();
}
bool Layer::isSelectable()
{
return selectable;
}
QPointF Layer::getItemPosByID(QString itemID)
{
QSqlQuery * query = sqlExcute->getPosByItemID(getLayerID(),itemID);
if(query->next())
{
return QPointF(query->value(0).toDouble(),query->value(1).toDouble());
}
return QPointF(0,0);
delete query;
}
Layer::ILongInfo Layer::getInfo(QSqlQuery *query)
{
/*
* 主要信息有:
* @UMID 与数据的ID关联;
* @TYPE ILongGeoType 枚举图元类型
* @CenterX 图元wgs CenterX 坐标
* @CenterY 图元wgs CenterX 坐标
* @MINX 图元最小wgs X坐标 (点类图元写CenterX相同)
* @MINY 图元最小wgs X坐标 (点类图元写CenterY相同)
* @MAXX 图元最大wgs X坐标 (点类图元写CenterX相同)
* @MAXY 图元最大wgs Y坐标 (点类图元写CenterY相同) 设计两个坐标点只为了非点类图元需要计算边界问题,比如线
* @LABEL 用来显示图标注的, 如果设置显示标注,就从数据表里面把标注内容填充到该字段 默认 ILONGNULL
* @INFO 保存图元GIS信息
* 格式: WGSx1,WGSy1_WGSx2,WGSy2_..._WGSxN,WGSyN
* @FLAGS 点类旋转角度或面类图元闭环(FLAGS==0 线条, FLAGS!=0 多边形)
* @SIZE 多边形或线条线宽或点类图元大小
* @PEN 画笔(R_G_B)
* @BRUSH 画刷(R_G_B)
*
*/
ILongInfo info;
info.id = query->value(0).toDouble();
info.center = QPointF(query->value(2).toDouble(),query->value(3).toDouble());
info.label = query->value(8).toString();
info.list = getGisList(query->value(9).toString());
info.flags = query->value(10).toInt();
info.size = query->value(11).toInt();
QStringList iPen = query->value(12).toString().split('_');
info.pen = QColor(iPen.at(0).toInt(),iPen.at(1).toInt(),iPen.at(2).toInt());
iPen = query->value(13).toString().split('_');
info.brush = QColor(iPen.at(0).toInt(),iPen.at(1).toInt(),iPen.at(2).toInt());
return info;
}
QList<QPointF> Layer::getGisList(QString gis)
{
QList<QPointF> l;
QStringList tl = gis.split('_');
while (!tl.isEmpty())
{
QString str = tl.first();
QStringList tmp = str.split(',');
l.append(QPointF(tmp.at(0).toDouble(),tmp.at(1).toDouble()));
tl.removeFirst();
}
return l;
}