Skip to content

Commit

Permalink
cache config.units to reduce number of calls to python
Browse files Browse the repository at this point in the history
  • Loading branch information
rinigus committed Sep 8, 2018
1 parent eff0de2 commit e8d5caa
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 18 deletions.
8 changes: 8 additions & 0 deletions poor/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import poor
import sys
import traceback
import pyotherside

__all__ = ("ConfigurationStore",)

Expand Down Expand Up @@ -72,6 +73,7 @@ def add(self, option, item):
root, name = self._split_option(option)
if item in root[name]: return
root[name].append(copy.deepcopy(item))
self._emit()

def _coerce(self, value, ref):
"""Coerce type of `value` to match `ref`."""
Expand All @@ -84,6 +86,9 @@ def contains(self, option, item):
root, name = self._split_option(option)
return item in root[name]

def _emit(self):
pyotherside.send('config.changed')

def get(self, option):
"""Return the value of `option`."""
root = self
Expand Down Expand Up @@ -152,11 +157,13 @@ def remove(self, option, item):
root, name = self._split_option(option)
if item not in root[name]: return
root[name].remove(item)
self._emit()

def set(self, option, value):
"""Set the value of `option`."""
root, name = self._split_option(option, create=True)
root[name] = copy.deepcopy(value)
self._emit()

def _split_option(self, option, create=False):
"""Split dotted option to dictionary and option name."""
Expand Down Expand Up @@ -190,6 +197,7 @@ def _update(self, values, root=None, defaults=None, path=()):
print("Discarding bad option-value pair {}, {}: {}"
.format(repr(full_name), repr(value), str(error)),
file=sys.stderr)
self._emit()

def write(self, path=None):
"""Write values of options to JSON file at `path`."""
Expand Down
20 changes: 19 additions & 1 deletion qml/Config.qml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,19 @@

import QtQuick 2.0

QtObject {
Item {
id: conf

// cache certain frequently used properties locally
property string units

Component.onCompleted: _update()

Connections {
target: py
onConfigurationChanged: conf._update()
onReadyChanged: conf._update()
}

function add(option, item) {
// Add item to the value of option.
Expand Down Expand Up @@ -50,4 +62,10 @@ QtObject {
return py.call_sync("poor.conf.set", [option, value]);
}

function _update() {
if (!py.ready) return;
conf.units = get("units");
console.log("Config updated");
}

}
9 changes: 3 additions & 6 deletions qml/Meters.qml
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,9 @@ Item {

function update() {
// Update speed and positioning accuracy values in user's preferred units.
if (!py.ready) return;
if (app.conf.get("units") === "american") {
var lines = ["", ""];
if (app.conf.units === "american") {
labels.text = " %1\n %2".arg(app.tr("mph")).arg(app.tr("ft"))
var lines = ["", ""];
if (gps.position.speedValid)
lines[0] = Math.round(gps.position.speed * 2.23694);
if (gps.position.horizontalAccuracyValid)
Expand All @@ -79,9 +78,8 @@ Item {
values.text = lines.join("\n");
values.doLayout();

} else if (app.conf.get("units") === "british") {
} else if (app.conf.units === "british") {
labels.text = " %1\n %2".arg(app.tr("mph")).arg(app.tr("yd"))
var lines = ["", ""];
if (gps.position.speedValid)
lines[0] = Math.round(gps.position.speed * 2.23694);
if (gps.position.horizontalAccuracyValid)
Expand All @@ -92,7 +90,6 @@ Item {

} else {
labels.text = " %1\n %2".arg(app.tr("km/h")).arg(app.tr("m"))
var lines = ["", ""];
if (gps.position.speedValid)
lines[0] = Math.round(gps.position.speed * 3.6);
if (gps.position.horizontalAccuracyValid)
Expand Down
15 changes: 10 additions & 5 deletions qml/NavigationInfoBlock.qml
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ Rectangle {
return;
}

if (app.conf.get("units") === "american") {
if (app.conf.units === "american") {
text = "%1".arg(Math.round(gps.position.speed * 2.23694))
} else if (app.conf.get("units") === "british") {
} else if (app.conf.units === "british") {
text = "%1".arg(Math.round(gps.position.speed * 2.23694))
} else {
text = "%1".arg(Math.round(gps.position.speed * 3.6))
Expand All @@ -79,9 +79,9 @@ Rectangle {

function update() {
if (!py.ready || !app.navigationActive) return;
if (app.conf.get("units") === "american") {
if (app.conf.units === "american") {
text = app.tr("mph")
} else if (app.conf.get("units") === "british") {
} else if (app.conf.units === "british") {
text = app.tr("mph")
} else {
text = app.tr("km/h")
Expand Down Expand Up @@ -145,9 +145,14 @@ Rectangle {
onScreenHeightChanged: block.checkIfBusy();
}

Connections {
target: app.conf
onUnitsChanged: block.update()
}

Connections {
target: gps
onPositionChanged: block.update()
onPositionChanged: speed.update()
}

Connections {
Expand Down
4 changes: 2 additions & 2 deletions qml/NearbyPage.qml
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ Page {
// Offer a different selection of radii depending on the user's
// preferred length units, but keep values as meters.

property var radiusLabels: app.conf.get("units") === "metric" ?
property var radiusLabels: app.conf.units === "metric" ?
["500 m", "1 km", "2 km", "5 km", "10 km", "20 km", "50 km", "100 km"] :
[ "¼ mi", "½ mi", "1 mi", "2 mi", "5 mi", "10 mi", "20 mi", "40 mi"]

property var radiusValues: app.conf.get("units") === "metric" ?
property var radiusValues: app.conf.units === "metric" ?
[500, 1000, 2000, 5000, 10000, 20000, 50000, 100000] :
[402, 805, 1609, 3219, 8047, 16093, 32187, 64374]

Expand Down
6 changes: 6 additions & 0 deletions qml/Python.qml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ Python {

property bool ready: false

signal configurationChanged

Component.onCompleted: {
addImportPath(Qt.resolvedUrl(".."));
importModule("poor", function() {
Expand All @@ -35,4 +37,8 @@ Python {

onError: console.log("Error: %1".arg(traceback));

onReceived: {
if (!data.length) return;
if (data[0] === "config.changed") py.configurationChanged();
}
}
12 changes: 10 additions & 2 deletions qml/ScaleBar.qml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ Item {
origin.y: scaleBar.height/2
}

property real _prevDist: 0
property int scaleBarMaxLengthDefault: Math.min(map.height,map.width) / 4
property int scaleBarMaxLength: scaleBarMaxLengthDefault
property real scaleWidth: 0
Expand Down Expand Up @@ -111,6 +112,11 @@ Item {
text: scaleBar.text
}

Connections {
target: app.conf
onUnitsChanged: scaleBar.update()
}

Connections {
target: map
onMetersPerPixelChanged: scaleBar.update();
Expand All @@ -126,12 +132,12 @@ Item {
function roundedDistace(dist) {
// Return dist rounded to an even amount of user-visible units,
// but keeping the value as meters.
if (app.conf.get("units") === "american")
if (app.conf.units === "american")
// Round to an even amount of miles or feet.
return dist >= 1609.34 ?
Util.siground(dist / 1609.34, 1) * 1609.34 :
Util.siground(dist * 3.28084, 1) / 3.28084;
if (app.conf.get("units") === "british")
if (app.conf.units === "british")
// Round to an even amount of miles or yards.
return dist >= 1609.34 ?
Util.siground(dist / 1609.34, 1) * 1609.34 :
Expand All @@ -145,7 +151,9 @@ Item {
var dist = map.metersPerPixel * scaleBarMaxLength;
dist = scaleBar.roundedDistace(dist);
scaleBar.scaleWidth = dist / map.metersPerPixel;
if (Math.abs(dist - _prevDist) < 1e-1) return;
scaleBar.text = py.call_sync("poor.util.format_distance", [dist, 1]);
_prevDist = dist;
}

}
Expand Down
4 changes: 2 additions & 2 deletions qml/SpeedLimit.qml
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ Rectangle {
}

// speed limit in m/s
if (app.conf.get("units") === "american") {
if (app.conf.units === "american") {
text = "%1".arg(Math.round(gps.streetSpeedLimit * 2.23694))
} else if (app.conf.get("units") === "british") {
} else if (app.conf.units === "british") {
text = "%1".arg(Math.round(gps.streetSpeedLimit * 2.23694))
} else {
text = "%1".arg(gps.streetSpeedLimit * 3.6)
Expand Down

0 comments on commit e8d5caa

Please sign in to comment.