From 7f9732c6297ebffd1a3c9ff33a3a3cf372487eb6 Mon Sep 17 00:00:00 2001 From: Viktor Govako Date: Tue, 6 Sep 2022 14:13:18 +0300 Subject: [PATCH] [desktop] Added "ru" locale and category checkbox. Signed-off-by: Viktor Govako --- qt/search_panel.cpp | 80 ++++++++++++++++++++++++++------------------- qt/search_panel.hpp | 17 ++++++---- 2 files changed, 57 insertions(+), 40 deletions(-) diff --git a/qt/search_panel.cpp b/qt/search_panel.cpp index 4ef63d16bb..42716da7d4 100644 --- a/qt/search_panel.cpp +++ b/qt/search_panel.cpp @@ -5,17 +5,15 @@ #include "map/bookmark_manager.hpp" #include "map/framework.hpp" -#include "map/user_mark_layer.hpp" #include "platform/platform.hpp" #include "base/assert.hpp" -#include - #include -#include +#include #include +#include #include #include #include @@ -31,6 +29,7 @@ namespace qt SearchPanel::SearchPanel(DrawWidget * drawWidget, QWidget * parent) : QWidget(parent) , m_pDrawWidget(drawWidget) + , m_clearIcon(":/ui/x.png") , m_busyIcon(":/ui/busy.png") , m_mode(search::Mode::Everywhere) , m_timestamp(0) @@ -56,25 +55,30 @@ SearchPanel::SearchPanel(DrawWidget * drawWidget, QWidget * parent) m_pAnimationTimer = new QTimer(this); connect(m_pAnimationTimer, &QTimer::timeout, this, &SearchPanel::OnAnimationTimer); - m_pSearchModeButtons = new QButtonGroup(this); + QButtonGroup * searchModeButtons = new QButtonGroup(this); QGroupBox * groupBox = new QGroupBox(); QHBoxLayout * modeLayout = new QHBoxLayout(); - QRadioButton * buttonE = new QRadioButton("Everywhere"); + QRadioButton * buttonE = new QRadioButton(tr("Everywhere")); modeLayout->addWidget(buttonE); - m_pSearchModeButtons->addButton(buttonE, static_cast(search::Mode::Everywhere)); - QRadioButton * buttonV = new QRadioButton("Viewport"); + searchModeButtons->addButton(buttonE, static_cast(search::Mode::Everywhere)); + QRadioButton * buttonV = new QRadioButton(tr("Viewport")); modeLayout->addWidget(buttonV); - m_pSearchModeButtons->addButton(buttonV, static_cast(search::Mode::Viewport)); + searchModeButtons->addButton(buttonV, static_cast(search::Mode::Viewport)); groupBox->setLayout(modeLayout); groupBox->setFlat(true); - m_pSearchModeButtons->button(static_cast(search::Mode::Everywhere))->setChecked(true); - connect(m_pSearchModeButtons, SIGNAL(buttonClicked(int)), this, SLOT(OnSearchModeChanged(int))); + searchModeButtons->button(static_cast(search::Mode::Everywhere))->setChecked(true); + connect(searchModeButtons, SIGNAL(buttonClicked(int)), this, SLOT(OnSearchModeChanged(int))); + + m_isCategory = new QCheckBox(tr("Category request")); + m_isCategory->setCheckState(Qt::Unchecked); + connect(m_isCategory, &QCheckBox::stateChanged, std::bind(&SearchPanel::RunSearch, this)); QHBoxLayout * requestLayout = new QHBoxLayout(); requestLayout->addWidget(m_pEditor); requestLayout->addWidget(m_pClearButton); QVBoxLayout * verticalLayout = new QVBoxLayout(); verticalLayout->addWidget(groupBox); + verticalLayout->addWidget(m_isCategory); verticalLayout->addLayout(requestLayout); verticalLayout->addWidget(m_pTable); setLayout(verticalLayout); @@ -110,7 +114,7 @@ void SearchPanel::StartBusyIndicator() void SearchPanel::StopBusyIndicator() { m_pAnimationTimer->stop(); - m_pClearButton->setIcon(QIcon(":/ui/x.png")); + m_pClearButton->setIcon(m_clearIcon); } void SearchPanel::OnEverywhereSearchResults(uint64_t timestamp, search::Results results) @@ -165,7 +169,7 @@ void SearchPanel::OnEverywhereSearchResults(uint64_t timestamp, search::Results StopBusyIndicator(); } -bool SearchPanel::Try3dModeCmd(QString const & str) +bool SearchPanel::Try3dModeCmd(std::string const & str) { bool const is3dModeOn = (str == "?3d"); bool const is3dBuildingsOn = (str == "?b3d"); @@ -180,7 +184,7 @@ bool SearchPanel::Try3dModeCmd(QString const & str) return true; } -bool SearchPanel::TryTrafficSimplifiedColorsCmd(QString const & str) +bool SearchPanel::TryTrafficSimplifiedColorsCmd(std::string const & str) { bool const simplifiedMode = (str == "?tc:simp"); bool const normalMode = (str == "?tc:norm"); @@ -195,11 +199,22 @@ bool SearchPanel::TryTrafficSimplifiedColorsCmd(QString const & str) return true; } -void SearchPanel::OnSearchTextChanged(QString const & normalized) +std::string SearchPanel::GetCurrentInputLocale() +{ + QString loc = QGuiApplication::inputMethod()->locale().name(); + loc.replace('_', '-'); + auto res = loc.toStdString(); + if (CategoriesHolder::MapLocaleToInteger(res) < 0) + res = "en"; + return res; +} + +void SearchPanel::OnSearchTextChanged(QString const & str) { // Pass input query as-is without any normalization. // Otherwise № -> No, and it's unexpectable for the search index. //QString const normalized = str.normalized(QString::NormalizationForm_KC); + std::string const normalized = str.toStdString(); if (Try3dModeCmd(normalized)) return; @@ -208,7 +223,7 @@ void SearchPanel::OnSearchTextChanged(QString const & normalized) ClearResults(); - if (normalized.isEmpty()) + if (normalized.empty()) { m_pDrawWidget->GetFramework().GetSearchAPI().CancelAllSearches(); @@ -217,16 +232,17 @@ void SearchPanel::OnSearchTextChanged(QString const & normalized) return; } + bool const isCategory = m_isCategory->isChecked(); + bool started = false; auto const timestamp = ++m_timestamp; using namespace search; - switch (m_mode) + if (m_mode == Mode::Everywhere) { - case Mode::Everywhere: - { - EverywhereSearchParams params{ - normalized.toUtf8().constData(), {} /* locale */, {} /* timeout */, false /* isCategory */, + EverywhereSearchParams params + { + normalized, GetCurrentInputLocale(), {} /* timeout */, isCategory, // m_onResults [this, timestamp](Results results, std::vector /* productInfo */) { @@ -236,12 +252,11 @@ void SearchPanel::OnSearchTextChanged(QString const & normalized) started = m_pDrawWidget->GetFramework().GetSearchAPI().SearchEverywhere(std::move(params)); } - break; - - case Mode::Viewport: + else if (m_mode == Mode::Viewport) { - ViewportSearchParams params{ - normalized.toUtf8().constData(), {} /* locale*/, {} /* timeout */, false /* isCategory */, + ViewportSearchParams params + { + normalized, GetCurrentInputLocale(), {} /* timeout */, isCategory, // m_onStarted {}, // m_onCompleted @@ -260,12 +275,6 @@ void SearchPanel::OnSearchTextChanged(QString const & normalized) started = m_pDrawWidget->GetFramework().GetSearchAPI().SearchInViewport(std::move(params)); } - break; - - default: - started = false; - break; - } if (started) StartBusyIndicator(); @@ -285,10 +294,13 @@ void SearchPanel::OnSearchModeChanged(int mode) if (m_mode == newMode) return; - m_mode = newMode; - // Run this query in the new mode. + RunSearch(); +} + +void SearchPanel::RunSearch() +{ auto const text = m_pEditor->text(); m_pEditor->setText(QString()); m_pEditor->setText(text); diff --git a/qt/search_panel.hpp b/qt/search_panel.hpp index 4375adee58..9025e1f442 100644 --- a/qt/search_panel.hpp +++ b/qt/search_panel.hpp @@ -7,13 +7,14 @@ #include +#include #include #include -class QTableWidget; +class QCheckBox; class QLineEdit; class QPushButton; -class QButtonGroup; +class QTableWidget; class QTimer; namespace qt @@ -27,8 +28,9 @@ class SearchPanel : public QWidget QLineEdit * m_pEditor; QPushButton * m_pClearButton; QTimer * m_pAnimationTimer; - QButtonGroup * m_pSearchModeButtons; + QCheckBox * m_isCategory; + QIcon m_clearIcon; QPixmap m_busyIcon; std::vector m_results; @@ -43,9 +45,12 @@ class SearchPanel : public QWidget public: SearchPanel(DrawWidget * drawWidget, QWidget * parent); + static std::string GetCurrentInputLocale(); + private: virtual void hideEvent(QHideEvent *); + void RunSearch(); void ClearResults(); void StartBusyIndicator(); @@ -54,13 +59,13 @@ private: private slots: void OnSearchModeChanged(int mode); void OnSearchPanelItemClicked(int row, int column); - void OnSearchTextChanged(QString const &); + void OnSearchTextChanged(QString const & str); void OnEverywhereSearchResults(uint64_t timestamp, search::Results results); void OnAnimationTimer(); void OnClearButton(); - bool Try3dModeCmd(QString const & str); - bool TryTrafficSimplifiedColorsCmd(QString const & str); + bool Try3dModeCmd(std::string const & str); + bool TryTrafficSimplifiedColorsCmd(std::string const & str); }; } // namespace qt