Skip to content

Commit

Permalink
Allow to reuse existing search term
Browse files Browse the repository at this point in the history
  • Loading branch information
glassez committed Jan 11, 2025
1 parent 2e21cf7 commit c8395c1
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 31 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
81 changes: 52 additions & 29 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 @@ -266,27 +282,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 +320,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 +370,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 +424,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 +436,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 +465,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);
}
6 changes: 4 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,12 @@ 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 c8395c1

Please sign in to comment.