Skip to content
This repository was archived by the owner on Aug 8, 2023. It is now read-only.

Commit a03e6c1

Browse files
committed
[Qt] Add a more compelling runtime style example
1 parent 8f965b2 commit a03e6c1

File tree

7 files changed

+1404
-272
lines changed

7 files changed

+1404
-272
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<RCC>
22
<qresource prefix="/">
33
<file>source.geojson</file>
4+
<file>label-arrow.svg</file>
5+
<file>label-background.svg</file>
46
</qresource>
57
</RCC>

platform/qt/app/label-arrow.svg

+97
Loading

platform/qt/app/label-background.svg

+25
Loading

platform/qt/app/mapwindow.cpp

+106-19
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ void MapWindow::changeStyle()
7171
if (++currentStyleIndex == styles.size()) {
7272
currentStyleIndex = 0;
7373
}
74+
75+
m_sourceAdded = false;
7476
}
7577

7678
void MapWindow::keyPressEvent(QKeyEvent *ev)
@@ -82,30 +84,115 @@ void MapWindow::keyPressEvent(QKeyEvent *ev)
8284
changeStyle();
8385
break;
8486
case Qt::Key_L: {
85-
m_map.setPaintProperty("water", "fill-color", QColor(255, 0, 0));
86-
m_map.setPaintProperty("building", "fill-color", "red");
87-
m_map.setPaintProperty("road-secondary-tertiary", "line-color", "red");
87+
if (m_sourceAdded) {
88+
return;
89+
}
8890

89-
m_map.setLayoutProperty("road-label-small", "symbol-placement", "point");
90-
m_map.setLayoutProperty("road-label-medium", "symbol-placement", "point");
91-
m_map.setLayoutProperty("road-label-large", "symbol-placement", "point");
91+
m_sourceAdded = true;
9292

9393
QFile geojson(":source.geojson");
9494
geojson.open(QIODevice::ReadOnly);
9595

96-
QVariantMap testSource;
97-
testSource["type"] = "geojson";
98-
testSource["data"] = geojson.readAll();
99-
100-
m_map.addSource("testSource", testSource);
101-
102-
QVariantMap testLayer;
103-
testLayer["id"] = "testLayer";
104-
testLayer["type"] = "fill";
105-
testLayer["source"] = "testSource";
106-
107-
m_map.addLayer(testLayer);
108-
m_map.setPaintProperty("testLayer", "fill-color", QColor("blue"));
96+
// The data source for the route line and markers
97+
QVariantMap routeSource;
98+
routeSource["type"] = "geojson";
99+
routeSource["data"] = geojson.readAll();
100+
m_map.addSource("routeSource", routeSource);
101+
102+
// The route case, painted before the route
103+
QVariantMap routeCase;
104+
routeCase["id"] = "routeCase";
105+
routeCase["type"] = "line";
106+
routeCase["source"] = "routeSource";
107+
m_map.addLayer(routeCase);
108+
109+
m_map.setPaintProperty("routeCase", "line-color", QColor("white"));
110+
m_map.setPaintProperty("routeCase", "line-width", 20.0);
111+
m_map.setLayoutProperty("routeCase", "line-join", "round");
112+
m_map.setLayoutProperty("routeCase", "line-cap", "round");
113+
114+
// The route, painted on top of the route case
115+
QVariantMap route;
116+
route["id"] = "route";
117+
route["type"] = "line";
118+
route["source"] = "routeSource";
119+
m_map.addLayer(route);
120+
121+
m_map.setPaintProperty("route", "line-color", QColor("blue"));
122+
m_map.setPaintProperty("route", "line-width", 8.0);
123+
m_map.setLayoutProperty("route", "line-join", "round");
124+
m_map.setLayoutProperty("route", "line-cap", "round");
125+
126+
// Markers at the beginning and end of the route
127+
m_map.addImage("label-arrow", QImage(":label-arrow.svg"));
128+
m_map.addImage("label-background", QImage(":label-background.svg"));
129+
130+
QVariantMap makerArrow;
131+
makerArrow["id"] = "makerArrow";
132+
makerArrow["type"] = "symbol";
133+
makerArrow["source"] = "routeSource";
134+
m_map.addLayer(makerArrow);
135+
136+
m_map.setLayoutProperty("makerArrow", "icon-image", "label-arrow");
137+
m_map.setLayoutProperty("makerArrow", "icon-size", 0.5);
138+
m_map.setLayoutProperty("makerArrow", "icon-ignore-placement", true);
139+
140+
QVariantList arrowOffset;
141+
arrowOffset.append(0.0);
142+
arrowOffset.append(-15.0);
143+
m_map.setLayoutProperty("makerArrow", "icon-offset", arrowOffset);
144+
145+
QVariantMap makerBackground;
146+
makerBackground["id"] = "makerBackground";
147+
makerBackground["type"] = "symbol";
148+
makerBackground["source"] = "routeSource";
149+
m_map.addLayer(makerBackground);
150+
151+
m_map.setLayoutProperty("makerBackground", "icon-image", "label-background");
152+
m_map.setLayoutProperty("makerBackground", "text-field", "{name}");
153+
m_map.setLayoutProperty("makerBackground", "icon-text-fit", "both");
154+
m_map.setLayoutProperty("makerBackground", "icon-ignore-placement", true);
155+
m_map.setLayoutProperty("makerBackground", "text-ignore-placement", true);
156+
m_map.setLayoutProperty("makerBackground", "text-anchor", "left");
157+
m_map.setLayoutProperty("makerBackground", "text-size", 16.0);
158+
m_map.setLayoutProperty("makerBackground", "text-padding", 0.0);
159+
m_map.setLayoutProperty("makerBackground", "text-line-height", 1.0);
160+
m_map.setLayoutProperty("makerBackground", "text-max-width", 8.0);
161+
162+
QVariantList iconTextFitPadding;
163+
iconTextFitPadding.append(15.0);
164+
iconTextFitPadding.append(10.0);
165+
iconTextFitPadding.append(15.0);
166+
iconTextFitPadding.append(10.0);
167+
m_map.setLayoutProperty("makerBackground", "icon-text-fit-padding", iconTextFitPadding);
168+
169+
QVariantList backgroundOffset;
170+
backgroundOffset.append(-0.5);
171+
backgroundOffset.append(-1.5);
172+
m_map.setLayoutProperty("makerBackground", "text-offset", backgroundOffset);
173+
174+
m_map.setPaintProperty("makerBackground", "text-color", QColor("white"));
175+
176+
QVariantList filterExpression;
177+
filterExpression.append("==");
178+
filterExpression.append("$type");
179+
filterExpression.append("Point");
180+
181+
QVariantList filter;
182+
filter.append(filterExpression);
183+
184+
m_map.setFilter("makerArrow", filter);
185+
m_map.setFilter("makerBackground", filter);
186+
187+
// Tilt the labels when tilting the map and make them larger
188+
m_map.setLayoutProperty("road-label-large", "text-size", 30.0);
189+
m_map.setLayoutProperty("road-label-large", "text-pitch-alignment", "viewport");
190+
191+
m_map.setLayoutProperty("road-label-medium", "text-size", 30.0);
192+
m_map.setLayoutProperty("road-label-medium", "text-pitch-alignment", "viewport");
193+
194+
m_map.setLayoutProperty("road-label-small", "text-pitch-alignment", "viewport");
195+
m_map.setLayoutProperty("road-label-small", "text-size", 30.0);
109196
}
110197
break;
111198
case Qt::Key_Tab:

platform/qt/app/mapwindow.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ protected slots:
4444

4545
unsigned m_animationTicks = 0;
4646
unsigned m_frameDraws = 0;
47+
48+
bool m_sourceAdded = false;
4749
};
4850

4951
#endif

0 commit comments

Comments
 (0)