on xenial help sign in
expose the lat,lon coords from the model
commitc2977cb006dfe3680fb32f408ebc1e8d4f6ef8cb authorLukáš Tinkl <lukas.tinkl@canonical.com> dateThu 24 Dec 2015, 00:43 (11 years ago) parent65f91df
3 files changed+119−8
plugins/Wizard/timezonemodel.cpp+12 plugins/Wizard/timezonemodel.h+5−2 qml/Wizard/Pages/50-timezone.qml+102−6
plugins/Wizard/timezonemodel.cpp+12
@@ -35,6 +35,8 @@ TimeZoneLocationModel::TimeZoneLocationModel(QObject *parent):
3535 m_roleNames[CityRole] = "city";
3636 m_roleNames[CountryRole] = "country";
3737 m_roleNames[OffsetRole] = "offset";
38+ m_roleNames[LatitudeRole] = "latitude";
39+ m_roleNames[LongitudeRole] = "longitude";
3840
3941 QObject::connect(m_workerThread,
4042 &TimeZonePopulateWorker::resultReady,
@@ -103,6 +105,10 @@ QVariant TimeZoneLocationModel::data(const QModelIndex &index, int role) const
103105 QTimeZone tmp(tz.timezone.toLatin1());
104106 return static_cast<double>(tmp.standardTimeOffset(QDateTime::currentDateTime())) / 3600;
105107 }
108+ case LatitudeRole:
109+ return tz.latitude;
110+ case LongitudeRole:
111+ return tz.longitude;
106112 default:
107113 qWarning() << Q_FUNC_INFO << "Unknown role";
108114 return QVariant();
@@ -129,11 +135,15 @@ void TimeZonePopulateWorker::buildCityMap()
129135 for (guint i = 0; i < tz_locations->len; ++i) {
130136 auto tmp = static_cast<CcTimezoneLocation *>(g_ptr_array_index(tz_locations, i));
131137 gchar *en_name, *country, *zone, *state, *full_country;
138+ gdouble latitude;
139+ gdouble longitude;
132140 g_object_get (tmp, "en_name", &en_name,
133141 "country", &country,
134142 "zone", &zone,
135143 "state", &state,
136144 "full_country", &full_country,
145+ "latitude", &latitude,
146+ "longitude", &longitude,
137147 nullptr);
138148 // There are empty entries in the DB
139149 if (g_strcmp0(en_name, "") != 0) {
@@ -142,6 +152,8 @@ void TimeZonePopulateWorker::buildCityMap()
142152 tmpTz.timezone = zone;
143153 tmpTz.state = state;
144154 tmpTz.full_country = full_country;
155+ tmpTz.latitude = latitude;
156+ tmpTz.longitude = longitude;
145157
146158 Q_EMIT (resultReady(tmpTz));
147159 }
plugins/Wizard/timezonemodel.h+5−2
@@ -37,7 +37,9 @@ public:
3737 CityRole,
3838 CountryRole,
3939 SimpleRole,
40− OffsetRole
40+ OffsetRole,
41+ LatitudeRole,
42+ LongitudeRole
4143 };
4244
4345 struct TzLocation {
@@ -46,7 +48,8 @@ public:
4648 QString timezone;
4749 QString state;
4850 QString full_country;
49− double offset;
51+ double latitude;
52+ double longitude;
5053 };
5154
5255 int rowCount(const QModelIndex &parent = QModelIndex()) const override;
qml/Wizard/Pages/50-timezone.qml+102−6
@@ -15,6 +15,7 @@
1515 */
1616
1717import QtQuick 2.4
18+import QtGraphicalEffects 1.0
1819import Ubuntu.Components 1.3
1920import Wizard 0.1
2021import Ubuntu.SystemSettings.TimeDate 1.0
@@ -33,6 +34,36 @@ LocalComponents.Page {
3334 // for testing
3435 readonly property alias tdModule: timeDatePanel
3536
37+ function highlightTimezone(offset) {
38+ highlightImage.source = "file:/usr/share/libtimezonemap/ui/timezone_" + offset + ".png";
39+ }
40+
41+ // geo coords conversion functions (adapted from libtimezonemap)
42+ function radians (degrees) {
43+ return degrees * Math.PI / 180;
44+ }
45+
46+ function longitudeToX(longitude, map_width) {
47+ var xdeg_offset = -6;
48+ var x = (map_width * (180.0 + longitude) / 360.0) + (map_width * xdeg_offset / 180.0);
49+ return x;
50+ }
51+
52+ function latitudeToY(latitude, map_height) {
53+ var bottom_lat = -59;
54+ var top_lat = 81;
55+
56+ var top_per = top_lat / 180.0;
57+ var y = 1.25 * Math.log(Math.tan(0.25*Math.PI + 0.4 * radians(latitude)));
58+ var full_range = 4.6068250867599998;
59+ var top_offset = full_range * top_per;
60+ var map_range = Math.abs(1.25 * Math.log(Math.tan(0.25*Math.PI + 0.4 * radians(bottom_lat))) - top_offset);
61+ y = Math.abs(y - top_offset);
62+ y = y / map_range;
63+ y = y * map_height;
64+ return y;
65+ }
66+
3667 UbuntuTimeDatePanel {
3768 id: timeDatePanel
3869 }
@@ -110,8 +141,13 @@ LocalComponents.Page {
110141 }
111142
112143 onClicked: {
144+ highlightTimezone(offset);
113145 ListView.view.currentIndex = index;
114146 selectedTimeZone = timeZone;
147+ print("Clicked at city with coords:", longitude, latitude);
148+ print("Highlight at (x,y):", longitudeToX(longitude, map.width), latitudeToY(latitude, map.height));
149+ pinImage.x = longitudeToX(longitude, map.width) - 8;
150+ pinImage.y = latitudeToY(latitude, map.height) - 16;
115151 }
116152 }
117153 }
@@ -120,16 +156,16 @@ LocalComponents.Page {
120156 id: column
121157 anchors {
122158 fill: content
123− topMargin: customMargin
124− leftMargin: desktopLook ? parent.leftMargin : 0
125− rightMargin: desktopLook ? parent.rightMargin : 0
159+ topMargin: units.gu(4)
160+ leftMargin: desktopLook ? staticMargin : 0
161+ rightMargin: desktopLook ? staticMargin : 0
126162 }
127163
128164 LocalComponents.WizardTextField {
129165 id: searchField
130166 objectName: "tzFilter"
131167 anchors.left: parent.left
132− anchors.right: parent.right
168+ anchors.right: !desktopLook ? parent.right : undefined
133169 anchors.leftMargin: column.anchors.leftMargin == 0 ? staticMargin : 0
134170 anchors.rightMargin: column.anchors.rightMargin == 0 ? staticMargin : 0
135171 placeholderText: i18n.tr("Enter your city")
@@ -138,13 +174,14 @@ LocalComponents.Page {
138174 // reset when switching between filter modes (text/country)
139175 selectedTimeZone = ""
140176 tzList.currentIndex = -1
177+ highlightImage.source = ""
141178 }
142179 }
143180
144181 Rectangle {
145182 id: divider
146183 anchors.left: parent.left
147− anchors.right: parent.right
184+ anchors.right: !desktopLook ? parent.right : undefined
148185 anchors.top: searchField.bottom
149186 anchors.topMargin: units.gu(3)
150187 height: units.dp(1)
@@ -161,14 +198,73 @@ LocalComponents.Page {
161198
162199 anchors {
163200 left: parent.left
164− right: parent.right
201+ right: !desktopLook ? parent.right : undefined
165202 top: divider.bottom
166203 }
167204
205+ width: desktopLook ? searchField.width : undefined
168206 height: column.height - searchField.height - column.anchors.topMargin - divider.height
169207 model: tzFilterModel
170208 delegate: tzComponent
171209 }
210+
211+ Item {
212+ id: mapContainer
213+ visible: desktopLook
214+ anchors {
215+ left: tzList.right
216+ right: parent.right
217+ top: parent.top
218+ bottom: parent.bottom
219+ }
220+
221+ Item {
222+ id: map
223+ width: units.dp(800)
224+ height: units.dp(410)
225+ anchors {
226+ centerIn: parent
227+ }
228+
229+ Image {
230+ id: backgroundImage
231+ source: "file:/usr/share/libtimezonemap/ui/bg.png"
232+ sourceSize: Qt.size(map.width, map.height)
233+ fillMode: Image.PreserveAspectFit
234+ smooth: true
235+ visible: false
236+ }
237+
238+ Image {
239+ id: highlightImage
240+ sourceSize: Qt.size(map.width, map.height)
241+ fillMode: Image.PreserveAspectFit
242+ smooth: true
243+ visible: false
244+ }
245+
246+ Blend {
247+ anchors.fill: map
248+ cached: true
249+ source: backgroundImage
250+ foregroundSource: highlightImage
251+ }
252+
253+ MouseArea {
254+ anchors.fill: parent
255+ onClicked: {
256+ print("Clicked map at:", mouseX, mouseY)
257+ }
258+ }
259+
260+ Image {
261+ id: pinImage
262+ source: "file:/usr/share/libtimezonemap/ui/pin.png"
263+ visible: selectedTimeZone != ""
264+ z: map.z + 1
265+ }
266+ }
267+ }
172268 }
173269
174270 Component {