Separated formatting for measurement

This commit is contained in:
Alex Zolotarev 2011-09-15 15:29:31 +03:00 committed by Alex Zolotarev
parent 9c18b20ae3
commit 0f9786151f
4 changed files with 65 additions and 39 deletions

View file

@ -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

40
map/measurement_utils.cpp Normal file
View file

@ -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

21
map/measurement_utils.hpp Normal file
View file

@ -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);
}

View file

@ -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)
{