Commit 44e2704b authored by con's avatar con
Browse files

Refactor search result window for use of many items and checked state.

parent 406d35ac
...@@ -493,7 +493,10 @@ static void find_helper(QFutureInterface<Core::Utils::FileSearchResult> &future, ...@@ -493,7 +493,10 @@ static void find_helper(QFutureInterface<Core::Utils::FileSearchResult> &future,
void CppFindReferences::findAll(Symbol *symbol) void CppFindReferences::findAll(Symbol *symbol)
{ {
_resultWindow->clearContents(); Find::SearchResult *search = _resultWindow->startNewSearch();
connect(search, SIGNAL(activated(Find::SearchResultItem)),
this, SLOT(openEditor(Find::SearchResultItem)));
_resultWindow->setShowReplaceUI(true); _resultWindow->setShowReplaceUI(true);
_resultWindow->popup(true); _resultWindow->popup(true);
...@@ -515,14 +518,11 @@ void CppFindReferences::findAll(Symbol *symbol) ...@@ -515,14 +518,11 @@ void CppFindReferences::findAll(Symbol *symbol)
void CppFindReferences::displayResult(int index) void CppFindReferences::displayResult(int index)
{ {
Core::Utils::FileSearchResult result = m_watcher.future().resultAt(index); Core::Utils::FileSearchResult result = m_watcher.future().resultAt(index);
Find::ResultWindowItem *item = _resultWindow->addResult(result.fileName, _resultWindow->addResult(result.fileName,
result.lineNumber, result.lineNumber,
result.matchingLine, result.matchingLine,
result.matchStart, result.matchStart,
result.matchLength); result.matchLength);
if (item)
connect(item, SIGNAL(activated(const QString&,int,int)),
this, SLOT(openEditor(const QString&,int,int)));
} }
void CppFindReferences::searchFinished() void CppFindReferences::searchFinished()
...@@ -530,8 +530,8 @@ void CppFindReferences::searchFinished() ...@@ -530,8 +530,8 @@ void CppFindReferences::searchFinished()
emit changed(); emit changed();
} }
void CppFindReferences::openEditor(const QString &fileName, int line, int column) void CppFindReferences::openEditor(const Find::SearchResultItem &item)
{ {
TextEditor::BaseTextEditor::openEditorAt(fileName, line, column); TextEditor::BaseTextEditor::openEditorAt(item.fileName, item.lineNumber, item.searchTermStart);
} }
...@@ -39,6 +39,7 @@ ...@@ -39,6 +39,7 @@
namespace Find { namespace Find {
class SearchResultWindow; class SearchResultWindow;
struct SearchResultItem;
} // end of namespace Find } // end of namespace Find
namespace CppTools { namespace CppTools {
...@@ -67,7 +68,7 @@ public: ...@@ -67,7 +68,7 @@ public:
private Q_SLOTS: private Q_SLOTS:
void displayResult(int); void displayResult(int);
void searchFinished(); void searchFinished();
void openEditor(const QString&, int, int); void openEditor(const Find::SearchResultItem &item);
private: private:
QPointer<CppModelManager> _modelManager; QPointer<CppModelManager> _modelManager;
......
...@@ -86,7 +86,10 @@ FindClassDeclarations::FindClassDeclarations(CppModelManager *modelManager) ...@@ -86,7 +86,10 @@ FindClassDeclarations::FindClassDeclarations(CppModelManager *modelManager)
void FindClassDeclarations::findAll(const QString &text, QTextDocument::FindFlags findFlags) void FindClassDeclarations::findAll(const QString &text, QTextDocument::FindFlags findFlags)
{ {
_resultWindow->clearContents(); Find::SearchResult *search = _resultWindow->startNewSearch();
connect(search, SIGNAL(activated(Find::SearchResultItem)),
this, SLOT(openEditor(Find::SearchResultItem)));
_resultWindow->popup(true); _resultWindow->popup(true);
Core::ProgressManager *progressManager = Core::ICore::instance()->progressManager(); Core::ProgressManager *progressManager = Core::ICore::instance()->progressManager();
...@@ -107,14 +110,11 @@ void FindClassDeclarations::findAll(const QString &text, QTextDocument::FindFlag ...@@ -107,14 +110,11 @@ void FindClassDeclarations::findAll(const QString &text, QTextDocument::FindFlag
void FindClassDeclarations::displayResult(int index) void FindClassDeclarations::displayResult(int index)
{ {
Core::Utils::FileSearchResult result = m_watcher.future().resultAt(index); Core::Utils::FileSearchResult result = m_watcher.future().resultAt(index);
Find::ResultWindowItem *item = _resultWindow->addResult(result.fileName, _resultWindow->addResult(result.fileName,
result.lineNumber, result.lineNumber,
result.matchingLine, result.matchingLine,
result.matchStart, result.matchStart,
result.matchLength); result.matchLength);
if (item)
connect(item, SIGNAL(activated(const QString&,int,int)),
this, SLOT(openEditor(const QString&,int,int)));
} }
void FindClassDeclarations::searchFinished() void FindClassDeclarations::searchFinished()
...@@ -122,9 +122,9 @@ void FindClassDeclarations::searchFinished() ...@@ -122,9 +122,9 @@ void FindClassDeclarations::searchFinished()
emit changed(); emit changed();
} }
void FindClassDeclarations::openEditor(const QString &fileName, int line, int column) void FindClassDeclarations::openEditor(const Find::SearchResultItem &item)
{ {
TextEditor::BaseTextEditor::openEditorAt(fileName, line, column); TextEditor::BaseTextEditor::openEditorAt(item.fileName, item.lineNumber, item.searchTermStart);
} }
////// //////
...@@ -139,7 +139,10 @@ FindFunctionCalls::FindFunctionCalls(CppModelManager *modelManager) ...@@ -139,7 +139,10 @@ FindFunctionCalls::FindFunctionCalls(CppModelManager *modelManager)
void FindFunctionCalls::findAll(const QString &text, QTextDocument::FindFlags findFlags) void FindFunctionCalls::findAll(const QString &text, QTextDocument::FindFlags findFlags)
{ {
_resultWindow->clearContents(); Find::SearchResult *search = _resultWindow->startNewSearch();
connect(search, SIGNAL(activated(Find::SearchResultItem)),
this, SLOT(openEditor(Find::SearchResultItem)));
_resultWindow->popup(true); _resultWindow->popup(true);
Core::ProgressManager *progressManager = Core::ICore::instance()->progressManager(); Core::ProgressManager *progressManager = Core::ICore::instance()->progressManager();
...@@ -160,14 +163,11 @@ void FindFunctionCalls::findAll(const QString &text, QTextDocument::FindFlags fi ...@@ -160,14 +163,11 @@ void FindFunctionCalls::findAll(const QString &text, QTextDocument::FindFlags fi
void FindFunctionCalls::displayResult(int index) void FindFunctionCalls::displayResult(int index)
{ {
Core::Utils::FileSearchResult result = m_watcher.future().resultAt(index); Core::Utils::FileSearchResult result = m_watcher.future().resultAt(index);
Find::ResultWindowItem *item = _resultWindow->addResult(result.fileName, _resultWindow->addResult(result.fileName,
result.lineNumber, result.lineNumber,
result.matchingLine, result.matchingLine,
result.matchStart, result.matchStart,
result.matchLength); result.matchLength);
if (item)
connect(item, SIGNAL(activated(const QString&,int,int)),
this, SLOT(openEditor(const QString&,int,int)));
} }
void FindFunctionCalls::searchFinished() void FindFunctionCalls::searchFinished()
...@@ -175,9 +175,9 @@ void FindFunctionCalls::searchFinished() ...@@ -175,9 +175,9 @@ void FindFunctionCalls::searchFinished()
emit changed(); emit changed();
} }
void FindFunctionCalls::openEditor(const QString &fileName, int line, int column) void FindFunctionCalls::openEditor(const Find::SearchResultItem &item)
{ {
TextEditor::BaseTextEditor::openEditorAt(fileName, line, column); TextEditor::BaseTextEditor::openEditorAt(item.fileName, item.lineNumber, item.searchTermStart);
} }
......
...@@ -53,6 +53,7 @@ class Snapshot; ...@@ -53,6 +53,7 @@ class Snapshot;
namespace Find { namespace Find {
class SearchResultWindow; class SearchResultWindow;
struct SearchResultItem;
} }
namespace CppTools { namespace CppTools {
...@@ -79,7 +80,7 @@ public: ...@@ -79,7 +80,7 @@ public:
protected Q_SLOTS: protected Q_SLOTS:
void displayResult(int); void displayResult(int);
void searchFinished(); void searchFinished();
void openEditor(const QString&, int, int); void openEditor(const Find::SearchResultItem &item);
private: private:
QPointer<CppModelManager> _modelManager; QPointer<CppModelManager> _modelManager;
...@@ -104,7 +105,7 @@ public: ...@@ -104,7 +105,7 @@ public:
protected Q_SLOTS: protected Q_SLOTS:
void displayResult(int); void displayResult(int);
void searchFinished(); void searchFinished();
void openEditor(const QString&, int, int); void openEditor(const Find::SearchResultItem &item);
private: private:
QPointer<CppModelManager> _modelManager; QPointer<CppModelManager> _modelManager;
......
...@@ -81,13 +81,10 @@ void SearchResultTreeView::emitJumpToSearchResult(const QModelIndex &index) ...@@ -81,13 +81,10 @@ void SearchResultTreeView::emitJumpToSearchResult(const QModelIndex &index)
if (model()->data(index, ItemDataRoles::TypeRole).toString().compare("row") != 0) if (model()->data(index, ItemDataRoles::TypeRole).toString().compare("row") != 0)
return; return;
QString fileName = model()->data(index, ItemDataRoles::FileNameRole).toString();
int position = model()->data(index, ItemDataRoles::ResultIndexRole).toInt(); int position = model()->data(index, ItemDataRoles::ResultIndexRole).toInt();
int lineNumber = model()->data(index, ItemDataRoles::ResultLineNumberRole).toInt(); int checked = model()->data(index, Qt::CheckStateRole).toBool();
int searchTermStart = model()->data(index, ItemDataRoles::SearchTermStartRole).toInt();
int searchTermLength = model()->data(index, ItemDataRoles::SearchTermLengthRole).toInt();
emit jumpToSearchResult(position, fileName, lineNumber, searchTermStart, searchTermLength); emit jumpToSearchResult(position, checked);
} }
void SearchResultTreeView::keyPressEvent(QKeyEvent *e) void SearchResultTreeView::keyPressEvent(QKeyEvent *e)
......
...@@ -51,8 +51,7 @@ public: ...@@ -51,8 +51,7 @@ public:
SearchResultTreeModel *model() const; SearchResultTreeModel *model() const;
signals: signals:
void jumpToSearchResult(int index, const QString &fileName, int lineNumber, void jumpToSearchResult(int index, bool checked);
int searchTermStart, int searchTermLength);
public slots: public slots:
void clear(); void clear();
......
...@@ -29,8 +29,10 @@ ...@@ -29,8 +29,10 @@
#include "searchresultwindow.h" #include "searchresultwindow.h"
#include "searchresulttreemodel.h" #include "searchresulttreemodel.h"
#include "searchresulttreeitems.h"
#include <coreplugin/icore.h> #include <coreplugin/icore.h>
#include <utils/qtcassert.h>
#include <QtCore/QFile> #include <QtCore/QFile>
#include <QtCore/QTextStream> #include <QtCore/QTextStream>
...@@ -49,18 +51,9 @@ static const QString SETTINGSKEYSECTIONNAME("SearchResults"); ...@@ -49,18 +51,9 @@ static const QString SETTINGSKEYSECTIONNAME("SearchResults");
static const QString SETTINGSKEYEXPANDRESULTS("ExpandResults"); static const QString SETTINGSKEYEXPANDRESULTS("ExpandResults");
void ResultWindowItem::setData(const QVariant &data)
{
m_data = data;
}
QVariant ResultWindowItem::data() const
{
return m_data;
}
SearchResultWindow::SearchResultWindow() SearchResultWindow::SearchResultWindow()
: m_isShowingReplaceUI(false) : m_currentSearch(0),
m_isShowingReplaceUI(false)
{ {
m_widget = new QStackedWidget; m_widget = new QStackedWidget;
m_widget->setWindowTitle(name()); m_widget->setWindowTitle(name());
...@@ -91,8 +84,8 @@ SearchResultWindow::SearchResultWindow() ...@@ -91,8 +84,8 @@ SearchResultWindow::SearchResultWindow()
m_replaceButton->setAutoRaise(true); m_replaceButton->setAutoRaise(true);
m_replaceTextEdit->setTabOrder(m_replaceTextEdit, m_searchResultTreeView); m_replaceTextEdit->setTabOrder(m_replaceTextEdit, m_searchResultTreeView);
connect(m_searchResultTreeView, SIGNAL(jumpToSearchResult(int,const QString&,int,int,int)), connect(m_searchResultTreeView, SIGNAL(jumpToSearchResult(int,bool)),
this, SLOT(handleJumpToSearchResult(int,const QString&,int,int,int))); this, SLOT(handleJumpToSearchResult(int,bool)));
connect(m_expandCollapseToolButton, SIGNAL(toggled(bool)), this, SLOT(handleExpandCollapseToolButton(bool))); connect(m_expandCollapseToolButton, SIGNAL(toggled(bool)), this, SLOT(handleExpandCollapseToolButton(bool)));
connect(m_replaceButton, SIGNAL(clicked()), this, SLOT(handleReplaceButton())); connect(m_replaceButton, SIGNAL(clicked()), this, SLOT(handleReplaceButton()));
...@@ -103,9 +96,10 @@ SearchResultWindow::SearchResultWindow() ...@@ -103,9 +96,10 @@ SearchResultWindow::SearchResultWindow()
SearchResultWindow::~SearchResultWindow() SearchResultWindow::~SearchResultWindow()
{ {
writeSettings(); writeSettings();
delete m_currentSearch;
m_currentSearch = 0;
delete m_widget; delete m_widget;
m_widget = 0; m_widget = 0;
qDeleteAll(m_items);
m_items.clear(); m_items.clear();
} }
...@@ -115,7 +109,6 @@ void SearchResultWindow::setShowReplaceUI(bool show) ...@@ -115,7 +109,6 @@ void SearchResultWindow::setShowReplaceUI(bool show)
m_replaceLabel->setVisible(show); m_replaceLabel->setVisible(show);
m_replaceTextEdit->setVisible(show); m_replaceTextEdit->setVisible(show);
m_replaceButton->setVisible(show); m_replaceButton->setVisible(show);
// TODO m_searchResultTreeView->setShowCheckboxes(show);
m_isShowingReplaceUI = show; m_isShowingReplaceUI = show;
} }
...@@ -126,18 +119,27 @@ bool SearchResultWindow::isShowingReplaceUI() const ...@@ -126,18 +119,27 @@ bool SearchResultWindow::isShowingReplaceUI() const
void SearchResultWindow::handleReplaceButton() void SearchResultWindow::handleReplaceButton()
{ {
emit replaceButtonClicked(m_replaceTextEdit->text()); QTC_ASSERT(m_currentSearch, return);
m_currentSearch->replaceButtonClicked(m_replaceTextEdit->text(), checkedItems());
} }
QList<ResultWindowItem *> SearchResultWindow::selectedItems() const QList<SearchResultItem> SearchResultWindow::checkedItems() const
{ {
QList<ResultWindowItem *> items; QList<SearchResultItem> result;
// TODO SearchResultTreeModel *model = m_searchResultTreeView->model();
// foreach (ResultWindowItem *item, m_items) { const int fileCount = model->rowCount(QModelIndex());
// if (item->isSelected) for (int i = 0; i < fileCount; ++i) {
// items << item; QModelIndex fileIndex = model->index(i, 0, QModelIndex());
// } SearchResultFile *fileItem = static_cast<SearchResultFile *>(fileIndex.internalPointer());
return items; Q_ASSERT(fileItem != 0);
for (int rowIndex = 0; rowIndex < fileItem->childrenCount(); ++rowIndex) {
QModelIndex textIndex = model->index(rowIndex, 0, fileIndex);
SearchResultTextRow *rowItem = static_cast<SearchResultTextRow *>(textIndex.internalPointer());
if (rowItem->checkState())
result << m_items.at(rowItem->index());
}
}
return result;
} }
void SearchResultWindow::visibilityChanged(bool /*visible*/) void SearchResultWindow::visibilityChanged(bool /*visible*/)
...@@ -154,18 +156,27 @@ QList<QWidget*> SearchResultWindow::toolBarWidgets() const ...@@ -154,18 +156,27 @@ QList<QWidget*> SearchResultWindow::toolBarWidgets() const
return QList<QWidget*>() << m_expandCollapseToolButton << m_replaceLabel << m_replaceTextEdit << m_replaceButton; return QList<QWidget*>() << m_expandCollapseToolButton << m_replaceLabel << m_replaceTextEdit << m_replaceButton;
} }
SearchResult *SearchResultWindow::startNewSearch(SearchMode searchOrSearchAndReplace)
{
clearContents();
setShowReplaceUI(searchOrSearchAndReplace != SearchOnly);
delete m_currentSearch;
m_currentSearch = new SearchResult;
return m_currentSearch;
}
void SearchResultWindow::clearContents() void SearchResultWindow::clearContents()
{ {
setShowReplaceUI(false); setReplaceUIEnabled(false);
m_widget->setCurrentWidget(m_searchResultTreeView);
m_searchResultTreeView->clear(); m_searchResultTreeView->clear();
qDeleteAll(m_items);
m_items.clear(); m_items.clear();
m_widget->setCurrentWidget(m_searchResultTreeView);
navigateStateChanged(); navigateStateChanged();
} }
void SearchResultWindow::showNoMatchesFound() void SearchResultWindow::showNoMatchesFound()
{ {
setReplaceUIEnabled(false);
m_widget->setCurrentWidget(m_noMatchesFoundDisplay); m_widget->setCurrentWidget(m_noMatchesFoundDisplay);
} }
...@@ -176,7 +187,7 @@ bool SearchResultWindow::isEmpty() const ...@@ -176,7 +187,7 @@ bool SearchResultWindow::isEmpty() const
int SearchResultWindow::numberOfResults() const int SearchResultWindow::numberOfResults() const
{ {
return m_searchResultTreeView->model()->rowCount(); return m_items.count();
} }
bool SearchResultWindow::hasFocus() bool SearchResultWindow::hasFocus()
...@@ -210,30 +221,41 @@ void SearchResultWindow::setTextEditorFont(const QFont &font) ...@@ -210,30 +221,41 @@ void SearchResultWindow::setTextEditorFont(const QFont &font)
m_searchResultTreeView->setTextEditorFont(font); m_searchResultTreeView->setTextEditorFont(font);
} }
void SearchResultWindow::handleJumpToSearchResult(int index, const QString &fileName, int lineNumber, void SearchResultWindow::handleJumpToSearchResult(int index, bool checked)
int searchTermStart, int searchTermLength)
{ {
Q_UNUSED(searchTermLength) QTC_ASSERT(m_currentSearch, return);
ResultWindowItem *item = m_items.at(index); m_currentSearch->activated(m_items.at(index));
emit item->activated(fileName, lineNumber, searchTermStart);
} }
ResultWindowItem *SearchResultWindow::addResult(const QString &fileName, int lineNumber, const QString &rowText, void SearchResultWindow::addResult(const QString &fileName, int lineNumber, const QString &rowText,
int searchTermStart, int searchTermLength) int searchTermStart, int searchTermLength, const QVariant &userData)
{ {
//qDebug()<<"###"<<fileName; //qDebug()<<"###"<<fileName;
m_widget->setCurrentWidget(m_searchResultTreeView); m_widget->setCurrentWidget(m_searchResultTreeView);
int index = m_items.size(); int index = m_items.size();
ResultWindowItem *item = new ResultWindowItem; SearchResultItem item;
item.fileName = fileName;
item.lineNumber = lineNumber;
item.lineText = rowText;
item.searchTermStart = searchTermStart;
item.searchTermLength = searchTermLength;
item.userData = userData;
item.index = index;
m_items.append(item); m_items.append(item);
m_searchResultTreeView->appendResultLine(index, fileName, lineNumber, rowText, searchTermStart, searchTermLength); m_searchResultTreeView->appendResultLine(index, fileName, lineNumber, rowText, searchTermStart, searchTermLength);
if (index == 0) { if (index == 0) {
setReplaceUIEnabled(true);
// We didn't have an item before, set the focus to the m_searchResultTreeView // We didn't have an item before, set the focus to the m_searchResultTreeView
setFocus(); setFocus();
m_searchResultTreeView->selectionModel()->select(m_searchResultTreeView->model()->index(0, 0, QModelIndex()), QItemSelectionModel::Select); m_searchResultTreeView->selectionModel()->select(m_searchResultTreeView->model()->index(0, 0, QModelIndex()), QItemSelectionModel::Select);
emit navigateStateChanged(); emit navigateStateChanged();
} }
return item; }
void SearchResultWindow::setReplaceUIEnabled(bool enabled)
{
m_replaceTextEdit->setEnabled(enabled);
m_replaceButton->setEnabled(enabled);
} }
void SearchResultWindow::handleExpandCollapseToolButton(bool checked) void SearchResultWindow::handleExpandCollapseToolButton(bool checked)
...@@ -272,17 +294,17 @@ int SearchResultWindow::priorityInStatusBar() const ...@@ -272,17 +294,17 @@ int SearchResultWindow::priorityInStatusBar() const
bool SearchResultWindow::canNext() bool SearchResultWindow::canNext()
{ {
return m_searchResultTreeView->model()->rowCount(); return m_items.count() > 0;
} }
bool SearchResultWindow::canPrevious() bool SearchResultWindow::canPrevious()
{ {
return m_searchResultTreeView->model()->rowCount(); return m_items.count() > 0;
} }
void SearchResultWindow::goToNext() void SearchResultWindow::goToNext()
{ {
if (!m_searchResultTreeView->model()->rowCount()) if (m_items.count() == 0)
return; return;
QModelIndex idx = m_searchResultTreeView->model()->next(m_searchResultTreeView->currentIndex()); QModelIndex idx = m_searchResultTreeView->model()->next(m_searchResultTreeView->currentIndex());
if (idx.isValid()) { if (idx.isValid()) {
......
...@@ -47,19 +47,25 @@ namespace Find { ...@@ -47,19 +47,25 @@ namespace Find {
class SearchResultWindow; class SearchResultWindow;
class FIND_EXPORT ResultWindowItem : public QObject struct FIND_EXPORT SearchResultItem
{ {
Q_OBJECT QString fileName;
int lineNumber;
QString lineText;
int searchTermStart;
int searchTermLength;
int index;
QVariant userData;
// whatever information we also need here
};
public: class FIND_EXPORT SearchResult : public QObject
void setData(const QVariant &data); {
QVariant data() const; Q_OBJECT
signals: signals:
void activated(const QString &fileName, int lineNumber, int column); void activated(const Find::SearchResultItem &item);
void replaceButtonClicked(const QString &replaceText, const QList<Find::SearchResultItem> &checkedItems);
private:
QVariant m_data;