Skip to content

Commit

Permalink
registry: Allow to cancel current searches
Browse files Browse the repository at this point in the history
When user types into the search box the previous search is canceled.
This improves the speed with which the results can be obtained.

Based on #460 plus cosmetic changes.
Related to #265, #523.
  • Loading branch information
drognanar authored and trollixx committed Jul 20, 2016
1 parent fd11923 commit c8464b6
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 11 deletions.
40 changes: 40 additions & 0 deletions src/registry/cancellationtoken.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/****************************************************************************
**
** Copyright (C) 2015 Artur Spychaj
** Contact: http://zealdocs.org/contact.html
**
** This file is part of Zeal.
**
** Zeal is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** Zeal is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with Zeal. If not, see <http://www.gnu.org/licenses/>.
**
****************************************************************************/

#include "cancellationtoken.h"

using namespace Zeal;

CancellationToken::CancellationToken()
{
m_cancelled = QSharedPointer<bool>(new bool(false));
}

void CancellationToken::cancel()
{
*m_cancelled = true;
}

bool CancellationToken::isCanceled() const
{
return *m_cancelled;
}
47 changes: 47 additions & 0 deletions src/registry/cancellationtoken.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/****************************************************************************
**
** Copyright (C) 2015 Artur Spychaj
** Contact: http://zealdocs.org/contact.html
**
** This file is part of Zeal.
**
** Zeal is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** Zeal is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with Zeal. If not, see <http://www.gnu.org/licenses/>.
**
****************************************************************************/

#ifndef CANCELLATIONTOKEN_H
#define CANCELLATIONTOKEN_H

#include <QSharedPointer>

namespace Zeal {

/// Token that stores whether cancel was called on it.
/// In async code can be used to check if another thread called cancel.
struct CancellationToken
{
public:
CancellationToken();
bool isCanceled() const;
void cancel();

private:
QSharedPointer<bool> m_cancelled;
};

}

Q_DECLARE_METATYPE(Zeal::CancellationToken)

#endif // CANCELLATIONTOKEN_H
18 changes: 10 additions & 8 deletions src/registry/docsetregistry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#include "docsetregistry.h"

#include "cancellationtoken.h"
#include "searchresult.h"

#include <QDir>
Expand All @@ -35,6 +36,7 @@ DocsetRegistry::DocsetRegistry(QObject *parent) :
m_thread(new QThread(this))
{
// Register for use in signal connections.
qRegisterMetaType<CancellationToken>("CancellationToken");
qRegisterMetaType<QList<Zeal::SearchResult>>("QList<SearchResult>");

// FIXME: Only search should be performed in a separate thread
Expand Down Expand Up @@ -122,21 +124,21 @@ void DocsetRegistry::_addDocset(const QString &path)
emit docsetAdded(name);
}

void DocsetRegistry::search(const QString &query)
void DocsetRegistry::search(const QString &query, const CancellationToken &token)
{
// Only invalidate queries
if (query.isEmpty())
return;

QMetaObject::invokeMethod(this, "_runQuery", Qt::QueuedConnection, Q_ARG(QString, query));
QMetaObject::invokeMethod(this, "_runQuery", Qt::QueuedConnection,
Q_ARG(QString, query), Q_ARG(CancellationToken, token));
}

void DocsetRegistry::_runQuery(const QString &query)
void DocsetRegistry::_runQuery(const QString &query, const CancellationToken &token)
{
QList<SearchResult> results;

for (Docset *docset : docsets())
for (Docset *docset : docsets()) {
if (token.isCanceled())
return;
results << docset->search(query);
}

std::sort(results.begin(), results.end());

Expand Down
6 changes: 4 additions & 2 deletions src/registry/docsetregistry.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class QThread;

namespace Zeal {

struct CancellationToken;
struct SearchResult;

class DocsetRegistry : public QObject
Expand All @@ -52,7 +53,8 @@ class DocsetRegistry : public QObject
Docset *docset(int index) const;
QList<Docset *> docsets() const;

void search(const QString &query);
void search(const QString &query, const CancellationToken &token);
const QList<SearchResult> &queryResults();

public slots:
void addDocset(const QString &path);
Expand All @@ -65,7 +67,7 @@ public slots:

private slots:
void _addDocset(const QString &path);
void _runQuery(const QString &query);
void _runQuery(const QString &query, const CancellationToken &token);

private:
void addDocsetsFromFolder(const QString &path);
Expand Down
4 changes: 3 additions & 1 deletion src/ui/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,9 @@ MainWindow::MainWindow(Core::Application *app, QWidget *parent) :
return;

currentTabState()->searchQuery = text;
m_application->docsetRegistry()->search(text);
m_cancelSearch.cancel();
m_cancelSearch = CancellationToken();
m_application->docsetRegistry()->search(text, m_cancelSearch);
if (text.isEmpty()) {
currentTabState()->tocModel->setResults();
syncTreeView();
Expand Down
3 changes: 3 additions & 0 deletions src/ui/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#define MAINWINDOW_H

#include "registry/searchquery.h"
#include "registry/cancellationtoken.h"

#include <QMainWindow>

Expand Down Expand Up @@ -123,6 +124,8 @@ private slots:
QMenu *m_backMenu = nullptr;
QMenu *m_forwardMenu = nullptr;

Zeal::CancellationToken m_cancelSearch;

QxtGlobalShortcut *m_globalShortcut = nullptr;

QTabBar *m_tabBar = nullptr;
Expand Down

0 comments on commit c8464b6

Please sign in to comment.