环境:源代码用的是qt-creator-opensource-src-3.4.1.tar.gz
此篇文章说明的的是在QT creator中,Ctrl+Shift+F,然后点击search后的动作
Ctrl+Shift+F,输入内容
点击search
在FindToolWindow的构造函数中
connect(m_ui.searchButton, SIGNAL(clicked()), this, SLOT(search()));
void FindToolWindow::search()
{
QString term;
IFindFilter *filter = 0;
acceptAndGetParameters(&term, &filter);
QTC_ASSERT(filter, return);
filter->findAll(term, m_plugin->findFlags());//BaseFileFind继承自IFindFilter
}
void BaseFileFind::findAll(const QString &txt, FindFlags findFlags)
{
runNewSearch(txt, findFlags, SearchResultWindow::SearchOnly);
}
void BaseFileFind::runNewSearch(const QString &txt, FindFlags findFlags,
SearchResultWindow::SearchMode searchMode)
{
d->m_currentFindSupport = 0;
if (d->m_filterCombo)
updateComboEntries(d->m_filterCombo, true);
SearchResult *search = SearchResultWindow::instance()->startNewSearch(label(),
toolTip().arg(IFindFilter::descriptionForFindFlags(findFlags)),
txt, searchMode, SearchResultWindow::PreserveCaseEnabled,
QString::fromLatin1("TextEditor")); //获取搜索框
search->setTextToReplace(txt);
search->setSearchAgainSupported(true);
FileFindParameters parameters;
parameters.text = txt;
parameters.flags = findFlags;
parameters.nameFilters = fileNameFilters();
parameters.additionalParameters = additionalParameters();
search->setUserData(qVariantFromValue(parameters));
connect(search, SIGNAL(activated(Core::SearchResultItem)), this, SLOT(openEditor(Core::SearchResultItem)));//用编辑器打开
if (searchMode == SearchResultWindow::SearchAndReplace) {
connect(search, SIGNAL(replaceButtonClicked(QString,QList<Core::SearchResultItem>,bool)),
this, SLOT(doReplace(QString,QList<Core::SearchResultItem>,bool)));//文本替换
}
connect(search, SIGNAL(visibilityChanged(bool)), this, SLOT(hideHighlightAll(bool)));
connect(search, SIGNAL(cancelled()), this, SLOT(cancel()));
connect(search, SIGNAL(paused(bool)), this, SLOT(setPaused(bool)));
connect(search, SIGNAL(searchAgainRequested()), this, SLOT(searchAgain()));//再次搜索
connect(this, SIGNAL(enabledChanged(bool)), search, SIGNAL(requestEnabledCheck()));
connect(search, SIGNAL(requestEnabledCheck()), this, SLOT(recheckEnabled()));
runSearch(search);
}
void BaseFileFind::runSearch(SearchResult *search)
{
FileFindParameters parameters = search->userData().value<FileFindParameters>();
CountingLabel *label = new CountingLabel;
connect(search, SIGNAL(countChanged(int)), label, SLOT(updateCount(int)));
CountingLabel *statusLabel = new CountingLabel;
connect(search, SIGNAL(countChanged(int)), statusLabel, SLOT(updateCount(int)));//找到了新的搜索内容时更新条目信息
SearchResultWindow::instance()->popup(IOutputPane::Flags(IOutputPane::ModeSwitch|IOutputPane::WithFocus));
QFutureWatcher<FileSearchResultList> *watcher = new QFutureWatcher<FileSearchResultList>();
d->m_watchers.insert(watcher, search);
watcher->setPendingResultsLimit(1);
connect(watcher, SIGNAL(resultReadyAt(int)), this, SLOT(displayResult(int)));//显示搜索结果
connect(watcher, SIGNAL(finished()), this, SLOT(searchFinished()));
if (parameters.flags & FindRegularExpression) {
watcher->setFuture(Utils::findInFilesRegExp(parameters.text,
files(parameters.nameFilters, parameters.additionalParameters),
textDocumentFlagsForFindFlags(parameters.flags),
TextDocument::openedTextDocumentContents()));
} else {
watcher->setFuture(Utils::findInFiles(parameters.text,
files(parameters.nameFilters, parameters.additionalParameters),
textDocumentFlagsForFindFlags(parameters.flags),
TextDocument::openedTextDocumentContents()));
}
FutureProgress *progress =
ProgressManager::addTask(watcher->future(), tr("Searching"), Constants::TASK_SEARCH);
progress->setWidget(label);
progress->setStatusBarWidget(statusLabel);
connect(progress, SIGNAL(clicked()), search, SLOT(popup()));
}
void BaseFileFind::displayResult(int index) {
QFutureWatcher<FileSearchResultList> *watcher =
static_cast<QFutureWatcher<FileSearchResultList> *>(sender());
SearchResult *search = d->m_watchers.value(watcher);
if (!search) {
// search was removed from search history while the search is running
watcher->cancel();
return;
}
FileSearchResultList results = watcher->resultAt(index);
QList<SearchResultItem> items;
foreach (const FileSearchResult &result, results) {
SearchResultItem item;
item.path = QStringList() << QDir::toNativeSeparators(result.fileName);
item.lineNumber = result.lineNumber;
item.text = result.matchingLine;
item.textMarkLength = result.matchLength;
item.textMarkPos = result.matchStart;
item.useTextEditorFont = true;
item.userData = result.regexpCapturedTexts;
items << item;
}
search->addResults(items, SearchResult::AddOrdered);//增加搜索结果
}
void SearchResult::addResults(const QList<SearchResultItem> &items, AddMode mode)
{
m_widget->addResults(items, mode);
emit countChanged(m_widget->count());//发送信号变更统计的搜索条目
}
↓↓双击打开编辑
void BaseFileFind::openEditor(const SearchResultItem &item)
{
SearchResult *result = qobject_cast<SearchResult *>(sender());
IEditor *openedEditor = 0;
if (item.path.size() > 0) {
openedEditor = EditorManager::openEditorAt(QDir::fromNativeSeparators(item.path.first()),
item.lineNumber,
item.textMarkPos, Id(),
EditorManager::DoNotSwitchToDesignMode);
} else {
openedEditor = EditorManager::openEditor(QDir::fromNativeSeparators(item.text));
}
if (d->m_currentFindSupport)
d->m_currentFindSupport->clearHighlights();
d->m_currentFindSupport = 0;
if (!openedEditor)
return;
// highlight results
if (IFindSupport *findSupport = Aggregation::query<IFindSupport>(openedEditor->widget())) {
if (result) {
FileFindParameters parameters = result->userData().value<FileFindParameters>();
d->m_currentFindSupport = findSupport;
d->m_currentFindSupport->highlightAll(parameters.text, parameters.flags);//在编辑框中高亮搜索内容
}
}
}
void BaseTextFind::highlightAll(const QString &txt, FindFlags findFlags)
{
emit highlightAllRequested(txt, findFlags);
}
在TextEditorWidgetPrivate::TextEditorWidgetPrivate(TextEditorWidget *parent)中
connect(baseTextFind, &BaseTextFind::highlightAllRequested,
this, &TextEditorWidgetPrivate::highlightSearchResultsSlot);
void TextEditorWidgetPrivate::highlightSearchResultsSlot(const QString &txt, FindFlags findFlags)
{
if (m_searchExpr.pattern() == txt)
return;
m_searchExpr.setPattern(txt);
m_searchExpr.setPatternSyntax((findFlags & FindRegularExpression) ?
QRegExp::RegExp : QRegExp::FixedString);
m_searchExpr.setCaseSensitivity((findFlags & FindCaseSensitively) ?
Qt::CaseSensitive : Qt::CaseInsensitive);
m_findFlags = findFlags;
m_delayedUpdateTimer.start(50);//刷新
}