From 0f9786151feaea765b7e507834117875e83c9ceb Mon Sep 17 00:00:00 2001 From: Alex Zolotarev Date: Thu, 15 Sep 2011 15:29:31 +0300 Subject: [PATCH] Separated formatting for measurement --- map/map.pro | 2 ++ map/measurement_utils.cpp | 40 ++++++++++++++++++++++++++++++++++++++ map/measurement_utils.hpp | 21 ++++++++++++++++++++ qt/search_panel.cpp | 41 ++------------------------------------- 4 files changed, 65 insertions(+), 39 deletions(-) create mode 100644 map/measurement_utils.cpp create mode 100644 map/measurement_utils.hpp diff --git a/map/map.pro b/map/map.pro index a2db0f06d3..a67e27aa80 100644 --- a/map/map.pro +++ b/map/map.pro @@ -40,6 +40,7 @@ HEADERS += \ render_queue_routine.hpp \ benchmark_render_policy_mt.hpp \ ruler.hpp \ + measurement_utils.hpp \ SOURCES += \ feature_vec_model.cpp \ @@ -69,6 +70,7 @@ SOURCES += \ render_queue.cpp \ benchmark_render_policy_mt.cpp \ ruler.cpp \ + measurement_utils.cpp \ !iphone*:!bada*:!android* { HEADERS += qgl_render_context.hpp diff --git a/map/measurement_utils.cpp b/map/measurement_utils.cpp new file mode 100644 index 0000000000..0693d794eb --- /dev/null +++ b/map/measurement_utils.cpp @@ -0,0 +1,40 @@ +#include "measurement_utils.hpp" + +#include "../base/string_utils.hpp" +#include "../platform/settings.hpp" + +namespace MeasurementUtils +{ + +string FormatDistanceImpl(double m, bool & drawDir, + char const * high, char const * low, double highF, double lowF) +{ + double const lowV = m / lowF; + drawDir = true; + if (lowV < 1.0) + { + drawDir = false; + return string("0") + low; + } + + if (m >= highF) + return strings::to_string(m / highF) + high; + else + return strings::to_string(lowV) + low; +} + +string FormatDistance(double m, bool & drawDir) +{ + using namespace Settings; + Units u = Metric; + Settings::Get("Units", u); + + switch (u) + { + case Yard: return FormatDistanceImpl(m, drawDir, " mi", " yd", 1609.344, 0.9144); + case Foot: return FormatDistanceImpl(m, drawDir, " mi", " ft", 1609.344, 0.3048); + default: return FormatDistanceImpl(m, drawDir, " km", " m", 1000.0, 1.0); + } +} + +} // namespace MeasurementUtils diff --git a/map/measurement_utils.hpp b/map/measurement_utils.hpp new file mode 100644 index 0000000000..f9ccd72b35 --- /dev/null +++ b/map/measurement_utils.hpp @@ -0,0 +1,21 @@ +#pragma once + +#include "../std/string.hpp" + +namespace MeasurementUtils +{ + +inline double MetersToMiles(double m) { return m * 0.000621371192; } +inline double MilesToMeters(double mi) { return mi * 1609.344; } +inline double MetersToYards(double m) { return m * 1.0936133; } +inline double YardsToMeters(double yd) { return yd * 0.9144; } +inline double MetersToFeet(double m) { return m * 3.2808399; } +inline double FeetToMeters(double ft) { return ft * 0.3048; } + +/// Takes into an account user settings [metric, imperial] +/// @param[in] m meters +/// @param[out] drawDir should be direction arrow drawed? false if distance is < 1.0 +/// @return formatted string for search +string FormatDistance(double m, bool & drawDir); + +} diff --git a/qt/search_panel.cpp b/qt/search_panel.cpp index cc13520da0..d279d7fe6d 100644 --- a/qt/search_panel.cpp +++ b/qt/search_panel.cpp @@ -1,7 +1,7 @@ #include "search_panel.hpp" #include "draw_widget.hpp" -#include "../platform/settings.hpp" +#include "../map/measurement_utils.hpp" #include "../std/bind.hpp" @@ -86,43 +86,6 @@ namespace return item; } - QString format_distance_impl(double m, bool & drawDir, - char const * high, char const * low, double highF, double lowF) - { - double const lowV = m / lowF; - drawDir = true; - if (lowV < 1.0) - { - drawDir = false; - return (QString::fromAscii("0") + QString::fromAscii(low)); - } - - if (m >= highF) return QString("%1").arg(m / highF, 0, 'f', 1) + QString::fromAscii(high); - else return QString("%1").arg(lowV, 0, 'f', 0) + QString::fromAscii(low); - } - - QString format_distance(double m, bool & drawDir) - { - using namespace Settings; - Units u; - if (!Settings::Get("Units", u)) - { - // set default measurement from system locale - if (QLocale::system().measurementSystem() == QLocale::MetricSystem) - u = Metric; - else - u = Foot; - Settings::Set("Units", u); - } - - switch (u) - { - case Yard: return format_distance_impl(m, drawDir, " mi", " yd", 1609.344, 0.9144); - case Foot: return format_distance_impl(m, drawDir, " mi", " ft", 1609.344, 0.3048); - default: return format_distance_impl(m, drawDir, " km", " m", 1000.0, 1.0); - } - } - QIcon draw_direction(double a) { int const dim = 64; @@ -175,7 +138,7 @@ void SearchPanel::OnSearchResult(ResultT * res, int queryId) bool drawDir; m_pTable->setItem(rowCount, 2, - create_item(format_distance(res->GetDistanceFromCenter(), drawDir))); + create_item(MeasurementUtils::FormatDistance(res->GetDistanceFromCenter(), drawDir).c_str())); if (drawDir) {