From ebeb81adfd3deaea48b5abe622f3ef05ea065e43 Mon Sep 17 00:00:00 2001 From: Viktor Govako Date: Thu, 21 Apr 2022 11:42:18 +0300 Subject: [PATCH] [qt] Refactored RoutingSettings dialog. Signed-off-by: Viktor Govako --- qt/mainwindow.cpp | 5 +- qt/routing_settings_dialog.cpp | 205 +++++++++++++++------------------ qt/routing_settings_dialog.hpp | 26 +---- 3 files changed, 99 insertions(+), 137 deletions(-) diff --git a/qt/mainwindow.cpp b/qt/mainwindow.cpp index f8e1fe0142..f07a44a7bc 100644 --- a/qt/mainwindow.cpp +++ b/qt/mainwindow.cpp @@ -208,10 +208,7 @@ MainWindow::MainWindow(Framework & framework, bool apiOpenGLES3, m_pDrawWidget->UpdateAfterSettingsChanged(); - if (RoutingSettings::IsCacheEnabled()) - RoutingSettings::LoadSettings(m_pDrawWidget->GetFramework()); - else - RoutingSettings::ResetSettings(); + RoutingSettings::LoadSession(m_pDrawWidget->GetFramework()); } #if defined(Q_WS_WIN) diff --git a/qt/routing_settings_dialog.cpp b/qt/routing_settings_dialog.cpp index ef44b70164..0701265703 100644 --- a/qt/routing_settings_dialog.cpp +++ b/qt/routing_settings_dialog.cpp @@ -11,22 +11,27 @@ #include "base/string_utils.hpp" #include +#include #include -#include #include -namespace -{ -char constexpr kDelim[] = " ,\t"; -} // namespace - namespace qt { -std::string const RoutingSettings::kShowTurnsSettings = "show_turns_desktop"; -std::string const RoutingSettings::kUseCachedRoutingSettings = "use_cached_settings_desktop"; -std::string const RoutingSettings::kStartCoordsCachedSettings = "start_coords_desktop"; -std::string const RoutingSettings::kFinishCoordsCachedSettings = "finish_coords_desktop"; -std::string const RoutingSettings::kRouterTypeCachedSettings = "router_type_desktop"; +namespace +{ +char constexpr kDelim[] = " ,\t"; +std::string const kShowTurnsSettings = "show_turns_desktop"; +std::string const kUseCachedRoutingSettings = "use_cached_settings_desktop"; +std::string const kStartCoordsCachedSettings = "start_coords_desktop"; +std::string const kFinishCoordsCachedSettings = "finish_coords_desktop"; +std::string const kRouterTypeCachedSettings = "router_type_desktop"; + +int constexpr DefaultRouterIndex() +{ + // Car router by default. + return static_cast(routing::RouterType::Vehicle); +} +} // namespace // static bool RoutingSettings::TurnsEnabled() @@ -38,29 +43,10 @@ bool RoutingSettings::TurnsEnabled() return false; } -// static -bool RoutingSettings::IsCacheEnabled() -{ - bool enable = false; - if (settings::Get(kUseCachedRoutingSettings, enable)) - return enable; - - return false; -} - -// static -void RoutingSettings::ResetSettings() -{ - settings::Set(kUseCachedRoutingSettings, false); - settings::Set(kStartCoordsCachedSettings, std::string()); - settings::Set(kFinishCoordsCachedSettings, std::string()); - settings::Set(kRouterTypeCachedSettings, static_cast(routing::RouterType::Vehicle)); -} - // static std::optional RoutingSettings::GetCoordsFromString(std::string const & input) { - ms::LatLon coords{}; + ms::LatLon coords; strings::SimpleTokenizer iter(input, kDelim); if (iter && strings::to_double(*iter, coords.m_lat)) { @@ -80,78 +66,83 @@ std::optional RoutingSettings::GetCoords(bool start) } // static -void RoutingSettings::LoadSettings(Framework & framework) +void RoutingSettings::LoadSession(Framework & framework) { - int routerType = 0; - settings::TryGet(kRouterTypeCachedSettings, routerType); - - framework.GetRoutingManager().SetRouterImpl(static_cast(routerType)); + bool enable = false; + settings::TryGet(kUseCachedRoutingSettings, enable); + if (enable) + { + int routerType = DefaultRouterIndex(); + settings::TryGet(kRouterTypeCachedSettings, routerType); + framework.GetRoutingManager().SetRouterImpl(static_cast(routerType)); + } + else + { + settings::Delete(kUseCachedRoutingSettings); + settings::Delete(kShowTurnsSettings); + settings::Delete(kStartCoordsCachedSettings); + settings::Delete(kFinishCoordsCachedSettings); + settings::Delete(kRouterTypeCachedSettings); + } } RoutingSettings::RoutingSettings(QWidget * parent, Framework & framework) - : QDialog(parent) - , m_framework(framework) - , m_form(this) - , m_startInput(new QLineEdit(this)) - , m_finishInput(new QLineEdit(this)) - , m_routerType(new QComboBox(this)) - , m_showTurnsCheckbox(new QCheckBox("", this)) - , m_alwaysCheckbox(new QCheckBox("", this)) +: QDialog(parent) +, m_framework(framework) { setWindowTitle("Routing settings"); + QVBoxLayout * layout = new QVBoxLayout(this); - AddLineEdit("Start coords (lat, lon)", m_startInput); - AddLineEdit("Finish coords (lat, lon)", m_finishInput); - AddListWidgetWithRoutingTypes(); - AddCheckBox(); - AddButtonBox(); - - LoadSettings(); -} - -void RoutingSettings::AddLineEdit(std::string const & title, QLineEdit * lineEdit) -{ - std::string defaultText; - if (IsCacheEnabled()) { - bool const isStart = lineEdit == m_startInput; - std::string const settingsName = isStart ? kStartCoordsCachedSettings.c_str() - : kFinishCoordsCachedSettings.c_str(); - settings::TryGet(settingsName, defaultText); + QFrame * frame = new QFrame(this); + frame->setFrameShape(QFrame::Box); + QFormLayout * form = new QFormLayout(frame); + + auto const addCoordLine = [&](char const * title, bool isStart) + { + QLineEdit * lineEdit = new QLineEdit(frame); + if (isStart) + m_startInput = lineEdit; + else + m_finishInput = lineEdit; + + std::string defaultText; + settings::TryGet(isStart ? kStartCoordsCachedSettings : kFinishCoordsCachedSettings, defaultText); + + lineEdit->setText(defaultText.c_str()); + form->addRow(title, lineEdit); + }; + addCoordLine("Start coords (lat, lon)", true); + addCoordLine("Finish coords (lat, lon)", false); + + using namespace routing; + m_routerType = new QComboBox(frame); + m_routerType->insertItem(static_cast(RouterType::Vehicle), "car"); + m_routerType->insertItem(static_cast(RouterType::Pedestrian), "pedestrian"); + m_routerType->insertItem(static_cast(RouterType::Bicycle), "bicycle"); + m_routerType->insertItem(static_cast(RouterType::Transit), "transit"); + form->addRow("Choose router:", m_routerType); + + m_showTurnsCheckbox = new QCheckBox({}, frame); + form->addRow("Show turns:", m_showTurnsCheckbox); + + layout->addWidget(frame); } - lineEdit->setText(defaultText.c_str()); + { + m_saveSessionCheckbox = new QCheckBox("Save these settings for the next app session", this); + layout->addWidget(m_saveSessionCheckbox); + } - QString const label = QString(title.c_str()); - m_form.addRow(label, lineEdit); -} + { + auto * buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, this); + QObject::connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept); + QObject::connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject); -void RoutingSettings::AddCheckBox() -{ - m_form.addRow("Show turns:", m_showTurnsCheckbox); - m_form.addRow("Save for next sessions:", m_alwaysCheckbox); -} + layout->addWidget(buttonBox); + } -void RoutingSettings::AddButtonBox() -{ - auto * buttonBox = - new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, this); - - m_form.addRow(buttonBox); - - QObject::connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept); - QObject::connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject); -} - -void RoutingSettings::AddListWidgetWithRoutingTypes() -{ - using namespace routing; - m_routerType->insertItem(static_cast(RouterType::Vehicle), "car"); - m_routerType->insertItem(static_cast(RouterType::Pedestrian), "pedestrian"); - m_routerType->insertItem(static_cast(RouterType::Bicycle), "bicycle"); - m_routerType->insertItem(static_cast(RouterType::Transit), "transit"); - - m_form.addRow("Choose router:", m_routerType); + LoadSettings(); } void RoutingSettings::ShowMessage(std::string const & message) @@ -163,8 +154,10 @@ void RoutingSettings::ShowMessage(std::string const & message) bool RoutingSettings::ValidateAndSaveCoordsFromInput() { - std::string const & startText = m_startInput->text().toStdString(); - std::string const & finishText = m_finishInput->text().toStdString(); + std::string startText = m_startInput->text().toStdString(); + strings::Trim(startText); + std::string finishText = m_finishInput->text().toStdString(); + strings::Trim(finishText); if (!startText.empty() && !GetCoordsFromString(startText)) return false; @@ -174,16 +167,15 @@ bool RoutingSettings::ValidateAndSaveCoordsFromInput() settings::Set(kStartCoordsCachedSettings, startText); settings::Set(kFinishCoordsCachedSettings, finishText); - return true; } -void RoutingSettings::SaveSettings() +bool RoutingSettings::SaveSettings() { - settings::Set(kShowTurnsSettings, m_showTurnsCheckbox->checkState() == Qt::CheckState::Checked); - settings::Set(kUseCachedRoutingSettings, true); - ValidateAndSaveCoordsFromInput(); + settings::Set(kShowTurnsSettings, m_showTurnsCheckbox->isChecked()); + settings::Set(kUseCachedRoutingSettings, m_saveSessionCheckbox->isChecked()); settings::Set(kRouterTypeCachedSettings, m_routerType->currentIndex()); + return ValidateAndSaveCoordsFromInput(); } void RoutingSettings::LoadSettings() @@ -196,19 +188,17 @@ void RoutingSettings::LoadSettings() settings::TryGet(kFinishCoordsCachedSettings, finishCoordsText); m_finishInput->setText(finishCoordsText.c_str()); - int routerType = 0; + int routerType = DefaultRouterIndex(); settings::TryGet(kRouterTypeCachedSettings, routerType); m_routerType->setCurrentIndex(routerType); - m_framework.GetRoutingManager().SetRouterImpl(static_cast(routerType)); - bool showTurns = false; settings::TryGet(kShowTurnsSettings, showTurns); m_showTurnsCheckbox->setChecked(showTurns); bool setChecked = false; settings::TryGet(kUseCachedRoutingSettings, setChecked); - m_alwaysCheckbox->setChecked(setChecked); + m_saveSessionCheckbox->setChecked(setChecked); } void RoutingSettings::ShowModal() @@ -216,21 +206,14 @@ void RoutingSettings::ShowModal() if (exec() != QDialog::Accepted) return; - if (!ValidateAndSaveCoordsFromInput()) + if (!SaveSettings()) { ShowMessage("Bad start or finish coords input."); ShowModal(); return; } - int routerType = m_routerType->currentIndex(); - settings::Set(kRouterTypeCachedSettings, routerType); - m_framework.GetRoutingManager().SetRouterImpl( - static_cast(routerType)); - - if (m_alwaysCheckbox->isChecked()) - SaveSettings(); - else - settings::Set(kUseCachedRoutingSettings, false); + int const routerType = m_routerType->currentIndex(); + m_framework.GetRoutingManager().SetRouterImpl(static_cast(routerType)); } } // namespace qt diff --git a/qt/routing_settings_dialog.hpp b/qt/routing_settings_dialog.hpp index cf2f42d786..7d60be6aeb 100644 --- a/qt/routing_settings_dialog.hpp +++ b/qt/routing_settings_dialog.hpp @@ -2,11 +2,9 @@ #include "geometry/latlon.hpp" -#include #include #include #include -#include #include #include @@ -19,45 +17,29 @@ namespace qt class RoutingSettings : public QDialog { public: - static bool IsCacheEnabled(); - static bool TurnsEnabled(); + static void LoadSession(Framework & framework); + static bool TurnsEnabled(); static std::optional GetCoords(bool start); - static void LoadSettings(Framework & framework); - static void ResetSettings(); RoutingSettings(QWidget * parent, Framework & framework); - void ShowModal(); private: - static std::string const kShowTurnsSettings; - static std::string const kUseCachedRoutingSettings; - static std::string const kStartCoordsCachedSettings; - static std::string const kFinishCoordsCachedSettings; - static std::string const kRouterTypeCachedSettings; - static std::optional GetCoordsFromString(std::string const & input); - void AddLineEdit(std::string const & title, QLineEdit * lineEdit); - void AddCheckBox(); - void AddButtonBox(); - void AddListWidgetWithRoutingTypes(); - void ShowMessage(std::string const & message); bool ValidateAndSaveCoordsFromInput(); - void SaveSettings(); + bool SaveSettings(); void LoadSettings(); Framework & m_framework; - QDialog m_dialog; - QFormLayout m_form; QLineEdit * m_startInput; QLineEdit * m_finishInput; QComboBox * m_routerType; QCheckBox * m_showTurnsCheckbox; - QCheckBox * m_alwaysCheckbox; + QCheckBox * m_saveSessionCheckbox; }; } // namespace qt