forked from qt-creator/qt-creator
Allow multiple file searches at the same time.
I.e. All Projects, Current Project, Files on File System, Current File. Task-number: QTCREATORBUG-6101 Change-Id: Ib2ee95c5eca1ecc3ef3bf707705651471db6324a Reviewed-by: Leandro Melo <leandro.melo@nokia.com>
This commit is contained in:
@@ -31,6 +31,7 @@
|
||||
**************************************************************************/
|
||||
|
||||
#include "basefilefind.h"
|
||||
#include "basefilefind_p.h"
|
||||
|
||||
#include <aggregation/aggregate.h>
|
||||
#include <coreplugin/icore.h>
|
||||
@@ -64,13 +65,10 @@
|
||||
using namespace Utils;
|
||||
using namespace Find;
|
||||
using namespace TextEditor;
|
||||
using namespace TextEditor::Internal;
|
||||
|
||||
BaseFileFind::BaseFileFind()
|
||||
: m_currentSearch(0),
|
||||
m_currentSearchCount(0),
|
||||
m_watcher(0),
|
||||
m_isSearching(false),
|
||||
m_resultLabel(0),
|
||||
: m_resultLabel(0),
|
||||
m_filterCombo(0)
|
||||
{
|
||||
}
|
||||
@@ -81,13 +79,16 @@ BaseFileFind::~BaseFileFind()
|
||||
|
||||
bool BaseFileFind::isEnabled() const
|
||||
{
|
||||
return !m_isSearching;
|
||||
return true;
|
||||
}
|
||||
|
||||
void BaseFileFind::cancel()
|
||||
{
|
||||
QTC_ASSERT(m_watcher, return);
|
||||
m_watcher->cancel();
|
||||
SearchResult *search = qobject_cast<SearchResult *>(sender());
|
||||
QTC_ASSERT(search, return);
|
||||
QFutureWatcher<FileSearchResultList> *watcher = watcherForSearch(search);
|
||||
QTC_ASSERT(watcher, return);
|
||||
watcher->cancel();
|
||||
}
|
||||
|
||||
QStringList BaseFileFind::fileNameFilters() const
|
||||
@@ -108,47 +109,60 @@ QStringList BaseFileFind::fileNameFilters() const
|
||||
void BaseFileFind::runNewSearch(const QString &txt, Find::FindFlags findFlags,
|
||||
SearchResultWindow::SearchMode searchMode)
|
||||
{
|
||||
m_isSearching = true;
|
||||
m_currentFindSupport = 0;
|
||||
emit changed();
|
||||
if (m_filterCombo)
|
||||
updateComboEntries(m_filterCombo, true);
|
||||
delete m_watcher;
|
||||
m_watcher = new QFutureWatcher<FileSearchResultList>();
|
||||
m_watcher->setPendingResultsLimit(1);
|
||||
connect(m_watcher, SIGNAL(resultReadyAt(int)), this, SLOT(displayResult(int)));
|
||||
connect(m_watcher, SIGNAL(finished()), this, SLOT(searchFinished()));
|
||||
m_currentSearchCount = 0;
|
||||
m_currentSearch = Find::SearchResultWindow::instance()->startNewSearch(label(),
|
||||
QFutureWatcher<FileSearchResultList> *watcher = new QFutureWatcher<FileSearchResultList>();
|
||||
watcher->setPendingResultsLimit(1);
|
||||
connect(watcher, SIGNAL(resultReadyAt(int)), this, SLOT(displayResult(int)));
|
||||
connect(watcher, SIGNAL(finished()), this, SLOT(searchFinished()));
|
||||
SearchResult *search = Find::SearchResultWindow::instance()->startNewSearch(label(),
|
||||
toolTip().arg(Find::IFindFilter::descriptionForFindFlags(findFlags)),
|
||||
txt, searchMode, QString::fromLatin1("TextEditor"));
|
||||
m_currentSearch->setTextToReplace(txt);
|
||||
m_watchers.insert(watcher, search);
|
||||
search->setTextToReplace(txt);
|
||||
QVariantList searchParameters;
|
||||
searchParameters << qVariantFromValue(txt) << qVariantFromValue(findFlags);
|
||||
m_currentSearch->setUserData(searchParameters);
|
||||
connect(m_currentSearch, SIGNAL(activated(Find::SearchResultItem)), this, SLOT(openEditor(Find::SearchResultItem)));
|
||||
search->setUserData(searchParameters);
|
||||
connect(search, SIGNAL(activated(Find::SearchResultItem)), this, SLOT(openEditor(Find::SearchResultItem)));
|
||||
if (searchMode == SearchResultWindow::SearchAndReplace) {
|
||||
connect(m_currentSearch, SIGNAL(replaceButtonClicked(QString,QList<Find::SearchResultItem>)),
|
||||
connect(search, SIGNAL(replaceButtonClicked(QString,QList<Find::SearchResultItem>)),
|
||||
this, SLOT(doReplace(QString,QList<Find::SearchResultItem>)));
|
||||
}
|
||||
connect(m_currentSearch, SIGNAL(visibilityChanged(bool)), this, SLOT(hideHighlightAll(bool)));
|
||||
connect(search, SIGNAL(visibilityChanged(bool)), this, SLOT(hideHighlightAll(bool)));
|
||||
CountingLabel *label = new CountingLabel;
|
||||
connect(search, SIGNAL(countChanged(int)), label, SLOT(updateCount(int)));
|
||||
Find::SearchResultWindow::instance()->popup(true);
|
||||
if (findFlags & Find::FindRegularExpression) {
|
||||
m_watcher->setFuture(Utils::findInFilesRegExp(txt, files(),
|
||||
watcher->setFuture(Utils::findInFilesRegExp(txt, files(),
|
||||
textDocumentFlagsForFindFlags(findFlags), ITextEditor::openedTextEditorsContents()));
|
||||
} else {
|
||||
m_watcher->setFuture(Utils::findInFiles(txt, files(),
|
||||
watcher->setFuture(Utils::findInFiles(txt, files(),
|
||||
textDocumentFlagsForFindFlags(findFlags), ITextEditor::openedTextEditorsContents()));
|
||||
}
|
||||
connect(m_currentSearch, SIGNAL(cancelled()), this, SLOT(cancel()));
|
||||
connect(search, SIGNAL(cancelled()), this, SLOT(cancel()));
|
||||
Core::FutureProgress *progress =
|
||||
Core::ICore::instance()->progressManager()->addTask(m_watcher->future(),
|
||||
Core::ICore::instance()->progressManager()->addTask(watcher->future(),
|
||||
tr("Search"),
|
||||
Constants::TASK_SEARCH);
|
||||
progress->setWidget(createProgressWidget());
|
||||
|
||||
progress->setWidget(label);
|
||||
connect(progress, SIGNAL(clicked()), Find::SearchResultWindow::instance(), SLOT(popup()));
|
||||
}
|
||||
|
||||
QFutureWatcher<FileSearchResultList> *BaseFileFind::watcherForSearch(SearchResult *search)
|
||||
{
|
||||
if (!search)
|
||||
return 0;
|
||||
QMapIterator<QFutureWatcher<Utils::FileSearchResultList> *, QPointer<Find::SearchResult> > it(m_watchers);
|
||||
while (it.hasNext()) {
|
||||
it.next();
|
||||
if (it.value() == search)
|
||||
return it.key();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void BaseFileFind::findAll(const QString &txt, Find::FindFlags findFlags)
|
||||
{
|
||||
runNewSearch(txt, findFlags, SearchResultWindow::SearchOnly);
|
||||
@@ -171,11 +185,15 @@ void BaseFileFind::doReplace(const QString &text,
|
||||
}
|
||||
|
||||
void BaseFileFind::displayResult(int index) {
|
||||
if (!m_currentSearch) {
|
||||
m_watcher->cancel();
|
||||
QFutureWatcher<FileSearchResultList> *watcher =
|
||||
static_cast<QFutureWatcher<FileSearchResultList> *>(sender());
|
||||
SearchResult *search = m_watchers.value(watcher);
|
||||
if (!search) {
|
||||
// search was removed from search history while the search is running
|
||||
watcher->cancel();
|
||||
return;
|
||||
}
|
||||
Utils::FileSearchResultList results = m_watcher->resultAt(index);
|
||||
Utils::FileSearchResultList results = watcher->resultAt(index);
|
||||
QList<Find::SearchResultItem> items;
|
||||
foreach (const Utils::FileSearchResult &result, results) {
|
||||
Find::SearchResultItem item;
|
||||
@@ -188,36 +206,18 @@ void BaseFileFind::displayResult(int index) {
|
||||
item.userData = result.regexpCapturedTexts;
|
||||
items << item;
|
||||
}
|
||||
m_currentSearch->addResults(items, Find::SearchResult::AddOrdered);
|
||||
m_currentSearchCount += items.count();
|
||||
if (m_resultLabel)
|
||||
m_resultLabel->setText(tr("%1 found").arg(m_currentSearchCount));
|
||||
search->addResults(items, Find::SearchResult::AddOrdered);
|
||||
}
|
||||
|
||||
void BaseFileFind::searchFinished()
|
||||
{
|
||||
if (m_currentSearch)
|
||||
m_currentSearch->finishSearch();
|
||||
m_currentSearch = 0;
|
||||
m_isSearching = false;
|
||||
m_resultLabel = 0;
|
||||
m_watcher->deleteLater();
|
||||
m_watcher = 0;
|
||||
emit changed();
|
||||
}
|
||||
|
||||
QWidget *BaseFileFind::createProgressWidget()
|
||||
{
|
||||
m_resultLabel = new QLabel;
|
||||
m_resultLabel->setAlignment(Qt::AlignCenter);
|
||||
// ### TODO this setup should be done by style
|
||||
QFont f = m_resultLabel->font();
|
||||
f.setBold(true);
|
||||
f.setPointSizeF(StyleHelper::sidebarFontSize());
|
||||
m_resultLabel->setFont(f);
|
||||
m_resultLabel->setPalette(StyleHelper::sidebarFontPalette(m_resultLabel->palette()));
|
||||
m_resultLabel->setText(tr("%1 found").arg(m_currentSearchCount));
|
||||
return m_resultLabel;
|
||||
QFutureWatcher<FileSearchResultList> *watcher =
|
||||
static_cast<QFutureWatcher<FileSearchResultList> *>(sender());
|
||||
SearchResult *search = m_watchers.value(watcher);
|
||||
if (search)
|
||||
search->finishSearch();
|
||||
m_watchers.remove(watcher);
|
||||
watcher->deleteLater();
|
||||
}
|
||||
|
||||
QWidget *BaseFileFind::createPatternWidget()
|
||||
@@ -360,3 +360,20 @@ QStringList BaseFileFind::replaceAll(const QString &text,
|
||||
|
||||
return changes.keys();
|
||||
}
|
||||
|
||||
CountingLabel::CountingLabel()
|
||||
{
|
||||
setAlignment(Qt::AlignCenter);
|
||||
// ### TODO this setup should be done by style
|
||||
QFont f = font();
|
||||
f.setBold(true);
|
||||
f.setPointSizeF(StyleHelper::sidebarFontSize());
|
||||
setFont(f);
|
||||
setPalette(StyleHelper::sidebarFontPalette(palette()));
|
||||
updateCount(0);
|
||||
}
|
||||
|
||||
void CountingLabel::updateCount(int count)
|
||||
{
|
||||
setText(tr("%1 found").arg(count));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user