From 99adb1609011ddaccef67edacd87ac3b7eaf11a4 Mon Sep 17 00:00:00 2001 From: Vladimir Golovnev Date: Mon, 13 Jan 2025 10:21:40 +0300 Subject: [PATCH] Allow to reuse existing search term PR #22148. --- src/gui/search/searchjobwidget.cpp | 1 + src/gui/search/searchwidget.cpp | 102 ++++++++++++++++++----------- src/gui/search/searchwidget.h | 5 +- 3 files changed, 68 insertions(+), 40 deletions(-) diff --git a/src/gui/search/searchjobwidget.cpp b/src/gui/search/searchjobwidget.cpp index 4e75ed98883..75a9bdbc282 100644 --- a/src/gui/search/searchjobwidget.cpp +++ b/src/gui/search/searchjobwidget.cpp @@ -286,6 +286,7 @@ void SearchJobWidget::cancelSearch() return; m_searchHandler->cancelSearch(); + setStatus(Status::Aborted); } void SearchJobWidget::downloadTorrents(const AddTorrentOption option) diff --git a/src/gui/search/searchwidget.cpp b/src/gui/search/searchwidget.cpp index 6784a324050..7aa678b2d7e 100644 --- a/src/gui/search/searchwidget.cpp +++ b/src/gui/search/searchwidget.cpp @@ -119,7 +119,23 @@ SearchWidget::SearchWidget(IGUIApplication *app, QWidget *parent) m_ui->tabWidget->setIconSize(iconSize); #endif connect(m_ui->tabWidget, &QTabWidget::tabCloseRequested, this, &SearchWidget::closeTab); - connect(m_ui->tabWidget, &QTabWidget::currentChanged, this, &SearchWidget::tabChanged); + connect(m_ui->tabWidget, &QTabWidget::currentChanged, this, &SearchWidget::currentTabChanged); + + connect(m_ui->tabWidget, &QTabWidget::tabBarDoubleClicked, this, [this](const int tabIndex) + { + if (tabIndex < 0) + return; + + // Reset current search pattern + auto *searchJobWidget = static_cast(m_ui->tabWidget->widget(tabIndex)); + const QString pattern = searchJobWidget->searchPattern(); + if (pattern != m_ui->lineEditSearchPattern->text()) + { + m_ui->lineEditSearchPattern->setText(pattern); + m_isNewQueryString = false; + adjustSearchButton(); + } + }); const auto *searchManager = SearchPluginManager::instance(); const auto onPluginChanged = [this]() @@ -162,16 +178,19 @@ bool SearchWidget::eventFilter(QObject *object, QEvent *event) const auto *mouseEvent = static_cast(event); const int tabIndex = m_ui->tabWidget->tabBar()->tabAt(mouseEvent->pos()); - if ((mouseEvent->button() == Qt::MiddleButton) && (tabIndex >= 0)) - { - closeTab(tabIndex); - return true; - } - - if (mouseEvent->button() == Qt::RightButton) + if (tabIndex >= 0) { - showTabMenu(tabIndex); - return true; + if (mouseEvent->button() == Qt::MiddleButton) + { + closeTab(tabIndex); + return true; + } + + if (mouseEvent->button() == Qt::RightButton) + { + showTabMenu(tabIndex); + return true; + } } return false; @@ -266,7 +285,7 @@ SearchWidget::~SearchWidget() delete m_ui; } -void SearchWidget::tabChanged(const int index) +void SearchWidget::currentTabChanged(const int index) { // when we switch from a tab that is not empty to another that is empty // the download button doesn't have to be available @@ -274,19 +293,13 @@ void SearchWidget::tabChanged(const int index) ? static_cast(m_ui->tabWidget->widget(index)) : nullptr; + if (!m_currentSearchTab) + m_isNewQueryString = true; + if (!m_isNewQueryString) - { - if (m_currentSearchTab && (m_currentSearchTab->status() == SearchJobWidget::Status::Ongoing)) - { - m_ui->searchButton->hide(); - m_ui->stopButton->show(); - } - else - { - m_ui->stopButton->hide(); - m_ui->searchButton->show(); - } - } + m_ui->lineEditSearchPattern->setText(m_currentSearchTab->searchPattern()); + + adjustSearchButton(); } void SearchWidget::selectMultipleBox([[maybe_unused]] const int index) @@ -310,6 +323,27 @@ void SearchWidget::toggleFocusBetweenLineEdits() } } +void SearchWidget::adjustSearchButton() +{ + if (!m_isNewQueryString + && (m_currentSearchTab && (m_currentSearchTab->status() == SearchJobWidget::Status::Ongoing))) + { + if (m_ui->searchButton->isVisible()) + { + m_ui->searchButton->hide(); + m_ui->stopButton->show(); + } + } + else + { + if (m_ui->stopButton->isVisible()) + { + m_ui->stopButton->hide(); + m_ui->searchButton->show(); + } + } +} + void SearchWidget::showTabMenu(const int index) { QMenu *menu = new QMenu(this); @@ -339,11 +373,13 @@ void SearchWidget::pluginsButtonClicked() dlg->show(); } -void SearchWidget::searchTextEdited(const QString &) +void SearchWidget::searchTextEdited(const QString &text) { - // Enable search button - m_ui->searchButton->setText(tr("Search")); - m_isNewQueryString = true; + if (m_currentSearchTab) + { + m_isNewQueryString = m_currentSearchTab->searchPattern() != text; + adjustSearchButton(); + } } void SearchWidget::giveFocusToSearchInput() @@ -391,8 +427,6 @@ void SearchWidget::searchButtonClicked() void SearchWidget::stopButtonClicked() { m_currentSearchTab->cancelSearch(); - m_ui->stopButton->hide(); - m_ui->searchButton->show(); } void SearchWidget::tabStatusChanged(SearchJobWidget *tab) @@ -405,10 +439,7 @@ void SearchWidget::tabStatusChanged(SearchJobWidget *tab) if (tab->status() != SearchJobWidget::Status::Ongoing) { if (tab == m_currentSearchTab) - { - m_ui->stopButton->hide(); - m_ui->searchButton->show(); - } + adjustSearchButton(); emit searchFinished(tab->status() == SearchJobWidget::Status::Error); } @@ -437,10 +468,5 @@ void SearchWidget::refreshTab(SearchJobWidget *searchJobWidget) // Re-launch search auto *searchHandler = SearchPluginManager::instance()->startSearch(searchJobWidget->searchPattern(), selectedCategory(), selectedPlugins()); searchJobWidget->assignSearchHandler(searchHandler); - if (!m_isNewQueryString && (m_ui->tabWidget->currentWidget() == searchJobWidget)) - { - m_ui->searchButton->hide(); - m_ui->stopButton->show(); - } tabStatusChanged(searchJobWidget); } diff --git a/src/gui/search/searchwidget.h b/src/gui/search/searchwidget.h index 8e076008bec..9eca603ebe0 100644 --- a/src/gui/search/searchwidget.h +++ b/src/gui/search/searchwidget.h @@ -65,8 +65,9 @@ class SearchWidget : public GUIApplicationComponent void pluginsButtonClicked(); void searchButtonClicked(); void stopButtonClicked(); + void searchTextEdited(const QString &text); + void currentTabChanged(int index); - void tabChanged(int index); void tabStatusChanged(SearchJobWidget *tab); void closeTab(int index); @@ -76,11 +77,11 @@ class SearchWidget : public GUIApplicationComponent void selectMultipleBox(int index); void toggleFocusBetweenLineEdits(); + void adjustSearchButton(); void fillCatCombobox(); void fillPluginComboBox(); void selectActivePage(); - void searchTextEdited(const QString &); QString selectedCategory() const; QStringList selectedPlugins() const;