Skip to content

Commit

Permalink
Allow to reuse existing search term
Browse files Browse the repository at this point in the history
PR #22148.
  • Loading branch information
glassez authored Jan 13, 2025
1 parent c622d50 commit 99adb16
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 40 deletions.
1 change: 1 addition & 0 deletions src/gui/search/searchjobwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ void SearchJobWidget::cancelSearch()
return;

m_searchHandler->cancelSearch();
setStatus(Status::Aborted);
}

void SearchJobWidget::downloadTorrents(const AddTorrentOption option)
Expand Down
102 changes: 64 additions & 38 deletions src/gui/search/searchwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<SearchJobWidget *>(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]()
Expand Down Expand Up @@ -162,16 +178,19 @@ bool SearchWidget::eventFilter(QObject *object, QEvent *event)

const auto *mouseEvent = static_cast<QMouseEvent *>(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;
Expand Down Expand Up @@ -266,27 +285,21 @@ 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
m_currentSearchTab = (index >= 0)
? static_cast<SearchJobWidget *>(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)
Expand All @@ -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);
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
}
5 changes: 3 additions & 2 deletions src/gui/search/searchwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@ class SearchWidget : public GUIApplicationComponent<QWidget>
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);
Expand All @@ -76,11 +77,11 @@ class SearchWidget : public GUIApplicationComponent<QWidget>

void selectMultipleBox(int index);
void toggleFocusBetweenLineEdits();
void adjustSearchButton();

void fillCatCombobox();
void fillPluginComboBox();
void selectActivePage();
void searchTextEdited(const QString &);

QString selectedCategory() const;
QStringList selectedPlugins() const;
Expand Down

0 comments on commit 99adb16

Please sign in to comment.