From 250a681ef4a629fb996770e15daa7714d4617587 Mon Sep 17 00:00:00 2001 From: vng Date: Sun, 12 Jun 2011 16:42:12 +0300 Subject: [PATCH] Add "System of measurement". Use this settings in search distance. --- map/settings.cpp | 20 +++++++ map/settings.hpp | 2 + qt/preferences_dialog.cpp | 109 ++++++++++++++++++++++++++++---------- qt/preferences_dialog.hpp | 5 ++ qt/search_panel.cpp | 28 ++++++++-- 5 files changed, 131 insertions(+), 33 deletions(-) diff --git a/map/settings.cpp b/map/settings.cpp index 1ba2f84cf1..fc3e277a89 100644 --- a/map/settings.cpp +++ b/map/settings.cpp @@ -138,4 +138,24 @@ namespace Settings } return false; } + + template <> string ToString(Units const & v) + { + switch (v) + { + case Yard: return "Y"; + case Foot: return "F"; + default: return "M"; + } + } + + template <> bool FromString(string const & s, Units & v) + { + if (s == "M") v = Metric; + else if (s == "Y") v = Yard; + else if (s == "F") v = Foot; + else return false; + + return true; + } } diff --git a/map/settings.hpp b/map/settings.hpp index 0c0d5ae0c9..e3471633cb 100644 --- a/map/settings.hpp +++ b/map/settings.hpp @@ -38,4 +38,6 @@ namespace Settings { StringStorage::Instance().SetValue(key, ToString(value)); } + + enum Units { Metric, Yard, Foot }; }; diff --git a/qt/preferences_dialog.cpp b/qt/preferences_dialog.cpp index 9df9671ad5..0585ef216a 100644 --- a/qt/preferences_dialog.cpp +++ b/qt/preferences_dialog.cpp @@ -1,6 +1,7 @@ #include "preferences_dialog.hpp" #include "../map/languages.hpp" +#include "../map/settings.hpp" #include #include @@ -9,6 +10,10 @@ #include #include #include +#include +#include +#include + namespace qt { @@ -41,40 +46,73 @@ namespace qt m_pTable->setItem(i, 1, c2); } - QPushButton * upButton = new QPushButton(); - upButton->setIcon(QIcon(":/navig64/up.png")); - upButton->setToolTip(tr("Move up")); - upButton->setDefault(false); - connect(upButton, SIGNAL(clicked()), this, SLOT(OnUpClick())); + m_pUnits = new QButtonGroup(this); + QGroupBox * radioBox = new QGroupBox("System of measurement"); + { + QHBoxLayout * pLayout = new QHBoxLayout(); - QPushButton * downButton = new QPushButton(); - downButton->setIcon(QIcon(":/navig64/down.png")); - downButton->setToolTip(tr("Move down")); - downButton->setDefault(false); - connect(downButton, SIGNAL(clicked()), this, SLOT(OnDownClick())); + using namespace Settings; - QPushButton * closeButton = new QPushButton(tr("Close")); - closeButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); - closeButton->setDefault(true); - connect(closeButton, SIGNAL(clicked()), this, SLOT(OnCloseClick())); + QRadioButton * p = new QRadioButton("Metric"); + pLayout->addWidget(p); + m_pUnits->addButton(p, Metric); - QVBoxLayout * vBox = new QVBoxLayout(); - vBox->addWidget(upButton); - vBox->addWidget(downButton); + p = new QRadioButton("Imperial (yard)"); + pLayout->addWidget(p); + m_pUnits->addButton(p, Yard); - QHBoxLayout * hBox = new QHBoxLayout(); - hBox->addLayout(vBox); - hBox->addWidget(m_pTable); + p = new QRadioButton("Imperial (foot)"); + pLayout->addWidget(p); + m_pUnits->addButton(p, Foot); + + radioBox->setLayout(pLayout); + + Units u; + if (!Settings::Get("Units", u)) u = Metric; + m_pUnits->button(static_cast(u))->setChecked(true); + + connect(m_pUnits, SIGNAL(buttonClicked(int)), this, SLOT(OnUnitsChanged(int))); + } + + QHBoxLayout * tableLayout = new QHBoxLayout(); + { + QPushButton * upButton = new QPushButton(); + upButton->setIcon(QIcon(":/navig64/up.png")); + upButton->setToolTip(tr("Move up")); + upButton->setDefault(false); + connect(upButton, SIGNAL(clicked()), this, SLOT(OnUpClick())); + + QPushButton * downButton = new QPushButton(); + downButton->setIcon(QIcon(":/navig64/down.png")); + downButton->setToolTip(tr("Move down")); + downButton->setDefault(false); + connect(downButton, SIGNAL(clicked()), this, SLOT(OnDownClick())); + + QVBoxLayout * vBox = new QVBoxLayout(); + vBox->addWidget(upButton); + vBox->addWidget(downButton); + + tableLayout->addLayout(vBox); + tableLayout->addWidget(m_pTable); + } QHBoxLayout * bottomLayout = new QHBoxLayout(); - bottomLayout->addStretch(1); - bottomLayout->setSpacing(0); - bottomLayout->addWidget(closeButton); + { + QPushButton * closeButton = new QPushButton(tr("Close")); + closeButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); + closeButton->setDefault(true); + connect(closeButton, SIGNAL(clicked()), this, SLOT(OnCloseClick())); - QVBoxLayout * finalBox = new QVBoxLayout(); - finalBox->addLayout(hBox); - finalBox->addLayout(bottomLayout); - setLayout(finalBox); + bottomLayout->addStretch(1); + bottomLayout->setSpacing(0); + bottomLayout->addWidget(closeButton); + } + + QVBoxLayout * finalLayout = new QVBoxLayout(); + finalLayout->addWidget(radioBox); + finalLayout->addLayout(tableLayout); + finalLayout->addLayout(bottomLayout); + setLayout(finalLayout); if (m_pTable->rowCount() > 0) m_pTable->selectRow(0); @@ -152,6 +190,21 @@ namespace qt langCodes.push_back(m_pTable->item(i, 0)->text().toUtf8().constData()); languages::SaveSettings(langCodes); - QDialog::done(code); + base_t::done(code); + } + + void PreferencesDialog::OnUnitsChanged(int i) + { + using namespace Settings; + + Units u; + switch (i) + { + case 0: u = Metric; break; + case 1: u = Yard; break; + case 2: u = Foot; break; + } + + Settings::Set("Units", u); } } diff --git a/qt/preferences_dialog.hpp b/qt/preferences_dialog.hpp index 8e0e560a02..b7c97b0065 100644 --- a/qt/preferences_dialog.hpp +++ b/qt/preferences_dialog.hpp @@ -3,11 +3,14 @@ #include class QTableWidget; +class QButtonGroup; namespace qt { class PreferencesDialog : public QDialog { + typedef QDialog base_t; + Q_OBJECT virtual QSize sizeHint () const { return QSize(400, 400); } @@ -20,9 +23,11 @@ namespace qt void OnCloseClick(); void OnUpClick(); void OnDownClick(); + void OnUnitsChanged(int i); private: bool & m_autoUpdatesEnabled; QTableWidget * m_pTable; + QButtonGroup * m_pUnits; }; } // namespace qt diff --git a/qt/search_panel.cpp b/qt/search_panel.cpp index e947c958aa..493a6efe52 100644 --- a/qt/search_panel.cpp +++ b/qt/search_panel.cpp @@ -1,6 +1,8 @@ #include "search_panel.hpp" #include "draw_widget.hpp" +#include "../map/settings.hpp" + #include "../std/bind.hpp" #include @@ -83,17 +85,33 @@ namespace return item; } - QString format_distance(double m, bool & drawDir) + 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 (m < 1.0) + if (lowV < 1.0) { drawDir = false; - return QString::fromAscii("0 m."); + return (QString::fromAscii("0") + QString::fromAscii(low)); } - if (m >= 1.0E3) return QString("%1 km.").arg(m * 1.0E-3, 0, 'f', 1); - else return QString("%1 m.").arg(m, 0, 'f', 0); + 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)) u = Metric; + + 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)