Commit 5b3aa933 authored by Eike Ziller's avatar Eike Ziller

CppFindReferences: Simplify code

Get rid of the need to manage a map of QFutureWatcher -> SearchResult

Change-Id: I1a87eccfff9149f2ddfdd2f72bb5e4b07ee81ce3
Reviewed-by: Nikolai Kosjar's avatarNikolai Kosjar <nikolai.kosjar@qt.io>
parent a8456c16
......@@ -331,7 +331,6 @@ void CppFindReferences::findUsages(Symbol *symbol,
search->setTextToReplace(replacement);
connect(search, &SearchResult::replaceButtonClicked,
this, &CppFindReferences::onReplaceButtonClicked);
connect(search, &SearchResult::paused, this, &CppFindReferences::setPaused);
search->setSearchAgainSupported(true);
connect(search, &SearchResult::searchAgainRequested, this, &CppFindReferences::searchAgain);
CppFindReferencesParameters parameters;
......@@ -358,7 +357,6 @@ void CppFindReferences::findAll_helper(SearchResult *search, Symbol *symbol,
search->finishSearch(false);
return;
}
connect(search, &SearchResult::cancelled, this, &CppFindReferences::cancel);
connect(search, &SearchResult::activated,
this, &CppFindReferences::openEditor);
......@@ -466,15 +464,9 @@ Symbol *CppFindReferences::findSymbol(const CppFindReferencesParameters &paramet
return 0;
}
void CppFindReferences::displayResults(int first, int last)
static void displayResults(SearchResult *search, QFutureWatcher<Usage> *watcher,
int first, int last)
{
QFutureWatcher<Usage> *watcher = static_cast<QFutureWatcher<Usage> *>(sender());
SearchResult *search = m_watchers.value(watcher);
if (!search) {
// search was deleted while it was running
watcher->cancel();
return;
}
for (int index = first; index != last; ++index) {
Usage result = watcher->future().resultAt(index);
search->addResult(result.path,
......@@ -485,35 +477,6 @@ void CppFindReferences::displayResults(int first, int last)
}
}
void CppFindReferences::searchFinished()
{
QFutureWatcher<Usage> *watcher = static_cast<QFutureWatcher<Usage> *>(sender());
SearchResult *search = m_watchers.value(watcher);
if (search)
search->finishSearch(watcher->isCanceled());
m_watchers.remove(watcher);
watcher->deleteLater();
}
void CppFindReferences::cancel()
{
SearchResult *search = qobject_cast<SearchResult *>(sender());
QTC_ASSERT(search, return);
QFutureWatcher<Usage> *watcher = m_watchers.key(search);
QTC_ASSERT(watcher, return);
watcher->cancel();
}
void CppFindReferences::setPaused(bool paused)
{
SearchResult *search = qobject_cast<SearchResult *>(sender());
QTC_ASSERT(search, return);
QFutureWatcher<Usage> *watcher = m_watchers.key(search);
QTC_ASSERT(watcher, return);
if (!paused || watcher->isRunning()) // guard against pausing when the search is finished
watcher->setPaused(paused);
}
void CppFindReferences::openEditor(const SearchResultItem &item)
{
if (item.path.size() > 0) {
......@@ -652,8 +615,6 @@ void CppFindReferences::findMacroUses(const Macro &macro, const QString &replace
connect(search, &SearchResult::activated,
this, &CppFindReferences::openEditor);
connect(search, &SearchResult::cancelled, this, &CppFindReferences::cancel);
connect(search, &SearchResult::paused, this, &CppFindReferences::setPaused);
const Snapshot snapshot = m_modelManager->snapshot();
const WorkingCopy workingCopy = m_modelManager->workingCopy();
......@@ -688,9 +649,21 @@ void CppFindReferences::renameMacroUses(const Macro &macro, const QString &repla
void CppFindReferences::createWatcher(const QFuture<Usage> &future, SearchResult *search)
{
QFutureWatcher<Usage> *watcher = new QFutureWatcher<Usage>();
// auto-delete:
connect(watcher, &QFutureWatcherBase::finished, watcher, &QObject::deleteLater);
connect(watcher, &QFutureWatcherBase::resultsReadyAt, search,
[search, watcher](int first, int last) {
displayResults(search, watcher, first, last);
});
connect(watcher, &QFutureWatcherBase::finished, search, [search, watcher]() {
search->finishSearch(watcher->isCanceled());
});
connect(search, &SearchResult::cancelled, watcher, [watcher]() { watcher->cancel(); });
connect(search, &SearchResult::paused, watcher, [watcher](bool paused) {
if (!paused || watcher->isRunning()) // guard against pausing when the search is finished
watcher->setPaused(paused);
});
watcher->setPendingResultsLimit(1);
connect(watcher, &QFutureWatcherBase::resultsReadyAt, this, &CppFindReferences::displayResults);
connect(watcher, &QFutureWatcherBase::finished, this, &CppFindReferences::searchFinished);
m_watchers.insert(watcher, search);
watcher->setFuture(future);
}
......@@ -71,10 +71,6 @@ public:
void renameMacroUses(const CPlusPlus::Macro &macro, const QString &replacement = QString());
private:
void displayResults(int first, int last);
void searchFinished();
void cancel();
void setPaused(bool paused);
void openEditor(const Core::SearchResultItem &item);
void onReplaceButtonClicked(const QString &text, const QList<Core::SearchResultItem> &items, bool preserveCase);
void searchAgain();
......@@ -91,7 +87,6 @@ private:
private:
QPointer<CppModelManager> m_modelManager;
QMap<QFutureWatcher<CPlusPlus::Usage> *, QPointer<Core::SearchResult> > m_watchers;
};
} // namespace Internal
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment