Commit b1c881ce authored by Eike Ziller's avatar Eike Ziller

Highlight matches when opening editor from advanced text search.

When you close the search pane it will remove the highlight, even if the
find tool bar is open with a different search, though.

Task-number: QTCREATORBUG-2606
Change-Id: I7defe15c844d37ae80ab66c6b9e68e1ef1afdf92
Reviewed-on: http://codereview.qt.nokia.com/2786Reviewed-by: default avatarEike Ziller <eike.ziller@nokia.com>
parent 86e2b724
......@@ -93,6 +93,11 @@ public:
m_incrementalStartPos = m_contPos = -1;
}
virtual void highlightAll(const QString &txt, Find::FindFlags findFlags)
{
m_editor->highlightSearchResults(txt.toLatin1(), Find::textDocumentFlagsForFindFlags(findFlags));
}
void clearResults() { m_editor->highlightSearchResults(QByteArray()); }
QString currentFindString() const { return QString(); }
QString completedFindString() const { return QString(); }
......
......@@ -486,8 +486,10 @@ Bookmark *BookmarkManager::bookmarkForIndex(const QModelIndex &index)
bool BookmarkManager::gotoBookmark(Bookmark *bookmark)
{
using namespace TextEditor;
if (ITextEditor *editor = BaseTextEditorWidget::openEditorAt(bookmark->filePath(), bookmark->lineNumber()))
if (ITextEditor *editor = qobject_cast<ITextEditor *>(BaseTextEditorWidget::openEditorAt(bookmark->filePath(),
bookmark->lineNumber()))) {
return (editor->currentLine() == bookmark->lineNumber());
}
return false;
}
......
......@@ -181,7 +181,7 @@ void CurrentDocumentFind::acceptCandidate()
return;
removeFindSupportConnections();
if (m_currentFind)
m_currentFind->highlightAll(QString(), 0);
m_currentFind->clearResults();
if (m_currentWidget)
disconnect(Aggregation::Aggregate::parentAggregate(m_currentWidget), SIGNAL(changed()),
......
......@@ -486,8 +486,10 @@ QList<SearchResultItem> SearchResultWindow::checkedItems() const
\fn void SearchResultWindow::visibilityChanged(bool)
\internal
*/
void SearchResultWindow::visibilityChanged(bool /*visible*/)
void SearchResultWindow::visibilityChanged(bool visible)
{
if (d->m_currentSearch)
d->m_currentSearch->visibilityChanged(visible);
}
/*!
......
......@@ -88,11 +88,19 @@ class FIND_EXPORT SearchResult : public QObject
{
Q_OBJECT
public:
void setUserData(const QVariant &data) { m_userData = data; }
QVariant userData() const { return m_userData; }
signals:
void activated(const Find::SearchResultItem &item);
void replaceButtonClicked(const QString &replaceText, const QList<Find::SearchResultItem> &checkedItems);
void visibilityChanged(bool visible);
friend class SearchResultWindow;
private:
QVariant m_userData;
};
class FIND_EXPORT SearchResultWindow : public Core::IOutputPane
......
......@@ -35,6 +35,7 @@
#include "find_global.h"
#include <QtCore/QMetaType>
#include <QtCore/QFlags>
#include <QtGui/QTextDocument>
......@@ -77,5 +78,6 @@ QTextDocument::FindFlags FIND_EXPORT textDocumentFlagsForFindFlags(Find::FindFla
} // namespace Find
Q_DECLARE_OPERATORS_FOR_FLAGS(Find::FindFlags)
Q_DECLARE_METATYPE(Find::FindFlags)
#endif // TEXTFINDCONSTANTS_H
......@@ -32,17 +32,19 @@
#include "basefilefind.h"
#include <aggregation/aggregate.h>
#include <coreplugin/icore.h>
#include <coreplugin/progressmanager/progressmanager.h>
#include <coreplugin/progressmanager/futureprogress.h>
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/editormanager/ieditor.h>
#include <coreplugin/filemanager.h>
#include <find/textfindconstants.h>
#include <find/searchresultwindow.h>
#include <texteditor/itexteditor.h>
#include <texteditor/basetexteditor.h>
#include <utils/stylehelper.h>
#include <utils/fileutils.h>
#include <utils/qtcassert.h>
#include <QtCore/QDebug>
#include <QtCore/QDirIterator>
......@@ -100,15 +102,28 @@ QStringList BaseFileFind::fileNameFilters() const
return filters;
}
void BaseFileFind::findAll(const QString &txt, Find::FindFlags findFlags)
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);
m_watcher.setFuture(QFuture<FileSearchResultList>());
SearchResult *result = m_resultWindow->startNewSearch();
SearchResult *result = m_resultWindow->startNewSearch(searchMode,
searchMode == SearchResultWindow::SearchAndReplace
? QString::fromLatin1("TextEditor")
: QString());
QVariantList searchParameters;
searchParameters << qVariantFromValue(txt) << qVariantFromValue(findFlags);
result->setUserData(searchParameters);
connect(result, SIGNAL(activated(Find::SearchResultItem)), this, SLOT(openEditor(Find::SearchResultItem)));
if (searchMode == SearchResultWindow::SearchAndReplace) {
connect(result, SIGNAL(replaceButtonClicked(QString,QList<Find::SearchResultItem>)),
this, SLOT(doReplace(QString,QList<Find::SearchResultItem>)));
}
connect(result, SIGNAL(visibilityChanged(bool)), this, SLOT(hideHighlightAll(bool)));
m_resultWindow->popup(true);
if (findFlags & Find::FindRegularExpression) {
m_watcher.setFuture(Utils::findInFilesRegExp(txt, files(),
......@@ -125,32 +140,14 @@ void BaseFileFind::findAll(const QString &txt, Find::FindFlags findFlags)
connect(progress, SIGNAL(clicked()), m_resultWindow, SLOT(popup()));
}
void BaseFileFind::findAll(const QString &txt, Find::FindFlags findFlags)
{
runNewSearch(txt, findFlags, SearchResultWindow::SearchOnly);
}
void BaseFileFind::replaceAll(const QString &txt, Find::FindFlags findFlags)
{
m_isSearching = true;
emit changed();
if (m_filterCombo)
updateComboEntries(m_filterCombo, true);
m_watcher.setFuture(QFuture<FileSearchResultList>());
SearchResult *result = m_resultWindow->startNewSearch(
SearchResultWindow::SearchAndReplace, QLatin1String("TextEditor"));
connect(result, SIGNAL(activated(Find::SearchResultItem)), this, SLOT(openEditor(Find::SearchResultItem)));
connect(result, SIGNAL(replaceButtonClicked(QString,QList<Find::SearchResultItem>)),
this, SLOT(doReplace(QString,QList<Find::SearchResultItem>)));
m_resultWindow->popup(true);
if (findFlags & Find::FindRegularExpression) {
m_watcher.setFuture(Utils::findInFilesRegExp(txt, files(),
textDocumentFlagsForFindFlags(findFlags), ITextEditor::openedTextEditorsContents()));
} else {
m_watcher.setFuture(Utils::findInFiles(txt, files(),
textDocumentFlagsForFindFlags(findFlags), ITextEditor::openedTextEditorsContents()));
}
Core::FutureProgress *progress =
Core::ICore::instance()->progressManager()->addTask(m_watcher.future(),
tr("Search"),
Constants::TASK_SEARCH);
progress->setWidget(createProgressWidget());
connect(progress, SIGNAL(clicked()), m_resultWindow, SLOT(popup()));
runNewSearch(txt, findFlags, SearchResultWindow::SearchAndReplace);
}
void BaseFileFind::doReplace(const QString &text,
......@@ -166,7 +163,7 @@ void BaseFileFind::doReplace(const QString &text,
void BaseFileFind::displayResult(int index) {
Utils::FileSearchResultList results = m_watcher.future().resultAt(index);
QList<Find::SearchResultItem> items; // this conversion is stupid...
QList<Find::SearchResultItem> items;
foreach (const Utils::FileSearchResult &result, results) {
Find::SearchResultItem item;
item.path = QStringList() << QDir::toNativeSeparators(result.fileName);
......@@ -267,14 +264,40 @@ void BaseFileFind::updateComboEntries(QComboBox *combo, bool onTop)
void BaseFileFind::openEditor(const Find::SearchResultItem &item)
{
SearchResult *result = qobject_cast<SearchResult *>(sender());
Core::IEditor *openedEditor = 0;
if (item.path.size() > 0) {
TextEditor::BaseTextEditorWidget::openEditorAt(QDir::fromNativeSeparators(item.path.first()), item.lineNumber, item.textMarkPos,
QString(), Core::EditorManager::ModeSwitch);
openedEditor = TextEditor::BaseTextEditorWidget::openEditorAt(QDir::fromNativeSeparators(item.path.first()),
item.lineNumber,
item.textMarkPos,
QString(),
Core::EditorManager::ModeSwitch);
} else {
Core::EditorManager::instance()->openEditor(item.text, QString(), Core::EditorManager::ModeSwitch);
openedEditor = Core::EditorManager::instance()->openEditor(item.text, QString(),
Core::EditorManager::ModeSwitch);
}
if (m_currentFindSupport)
m_currentFindSupport->clearResults();
m_currentFindSupport = 0;
if (!openedEditor)
return;
// highlight results
if (IFindSupport *findSupport = Aggregation::query<IFindSupport>(openedEditor->widget())) {
if (result) {
QVariantList userData = result->userData().value<QVariantList>();
QTC_ASSERT(userData.size() != 0, return);
m_currentFindSupport = findSupport;
m_currentFindSupport->highlightAll(userData.at(0).toString(), userData.at(1).value<FindFlags>());
}
}
}
void BaseFileFind::hideHighlightAll(bool visible)
{
if (!visible && m_currentFindSupport)
m_currentFindSupport->clearResults();
}
// #pragma mark Static methods
static void applyChanges(QTextDocument *doc, const QString &text, const QList<Find::SearchResultItem> &items)
......
......@@ -36,6 +36,8 @@
#include "texteditor_global.h"
#include <find/ifindfilter.h>
#include <find/ifindsupport.h>
#include <find/searchresultwindow.h>
#include <utils/filesearch.h>
#include <QtCore/QFutureWatcher>
......@@ -88,13 +90,19 @@ private slots:
void openEditor(const Find::SearchResultItem &item);
void doReplace(const QString &txt,
const QList<Find::SearchResultItem> &items);
void hideHighlightAll(bool visible);
private:
QWidget *createProgressWidget();
void runNewSearch(const QString &txt, Find::FindFlags findFlags,
Find::SearchResultWindow::SearchMode searchMode);
Find::SearchResultWindow *m_resultWindow;
QFutureWatcher<Utils::FileSearchResultList> m_watcher;
bool m_isSearching;
QPointer<Find::IFindSupport> m_currentFindSupport;
QLabel *m_resultLabel;
QStringListModel m_filterStrings;
QString m_filterSetting;
......
......@@ -154,7 +154,7 @@ protected:
} // namespace Internal
} // namespace TextEditor
ITextEditor *BaseTextEditorWidget::openEditorAt(const QString &fileName, int line, int column,
Core::IEditor *BaseTextEditorWidget::openEditorAt(const QString &fileName, int line, int column,
const QString &editorKind,
Core::EditorManager::OpenEditorFlags flags,
bool *newEditor)
......@@ -170,7 +170,7 @@ ITextEditor *BaseTextEditorWidget::openEditorAt(const QString &fileName, int lin
return texteditor;
}
return 0;
return editor;
}
static void convertToPlainText(QString &txt)
......
......@@ -132,7 +132,7 @@ public:
BaseTextEditorWidget(QWidget *parent);
~BaseTextEditorWidget();
static ITextEditor *openEditorAt(const QString &fileName, int line, int column = 0,
static Core::IEditor *openEditorAt(const QString &fileName, int line, int column = 0,
const QString &editorId = QString(),
Core::EditorManager::OpenEditorFlags flags = Core::EditorManager::IgnoreNavigationHistory,
bool *newEditor = 0);
......
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