Files
qt-creator/src/plugins/find/searchresultwindow.cpp

350 lines
11 KiB
C++
Raw Normal View History

/**************************************************************************
2008-12-02 12:01:29 +01:00
**
** This file is part of Qt Creator
**
2010-03-05 11:25:49 +01:00
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
2008-12-02 12:01:29 +01:00
**
** Contact: Nokia Corporation (qt-info@nokia.com)
2008-12-02 12:01:29 +01:00
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** If you are unsure which license is appropriate for your use, please
2009-08-14 09:30:56 +02:00
** contact the sales department at http://qt.nokia.com/contact.
2008-12-02 12:01:29 +01:00
**
**************************************************************************/
2008-12-02 15:08:31 +01:00
2008-12-02 12:01:29 +01:00
#include "searchresultwindow.h"
#include "searchresulttreemodel.h"
#include "searchresulttreeitems.h"
2008-12-02 12:01:29 +01:00
#include <coreplugin/icore.h>
#include <utils/qtcassert.h>
2008-12-02 12:01:29 +01:00
#include <QtCore/QFile>
#include <QtCore/QTextStream>
#include <QtCore/QSettings>
#include <QtCore/QDebug>
2008-12-02 12:01:29 +01:00
#include <QtGui/QListWidget>
#include <QtGui/QToolButton>
#include <QtGui/QLineEdit>
#include <QtGui/QStackedWidget>
#include <QtGui/QLabel>
2008-12-02 12:01:29 +01:00
using namespace Find;
using namespace Find::Internal;
static const QString SETTINGSKEYSECTIONNAME("SearchResults");
static const QString SETTINGSKEYEXPANDRESULTS("ExpandResults");
2009-09-29 16:59:19 +02:00
SearchResultWindow::SearchResultWindow()
: m_currentSearch(0),
m_isShowingReplaceUI(false),
m_focusReplaceEdit(false)
2008-12-02 12:01:29 +01:00
{
m_widget = new QStackedWidget;
2008-12-02 12:01:29 +01:00
m_widget->setWindowTitle(name());
m_searchResultTreeView = new SearchResultTreeView(m_widget);
m_searchResultTreeView->setFrameStyle(QFrame::NoFrame);
2009-01-30 17:09:09 +01:00
m_searchResultTreeView->setAttribute(Qt::WA_MacShowFocusRect, false);
2008-12-02 12:01:29 +01:00
m_widget->addWidget(m_searchResultTreeView);
m_noMatchesFoundDisplay = new QListWidget(m_widget);
m_noMatchesFoundDisplay->addItem(tr("No matches found!"));
m_noMatchesFoundDisplay->setFrameStyle(QFrame::NoFrame);
m_widget->addWidget(m_noMatchesFoundDisplay);
m_expandCollapseToolButton = new QToolButton(m_widget);
m_expandCollapseToolButton->setAutoRaise(true);
m_expandCollapseToolButton->setCheckable(true);
m_expandCollapseToolButton->setIcon(QIcon(":/find/images/expand.png"));
m_expandCollapseToolButton->setToolTip(tr("Expand All"));
m_replaceLabel = new QLabel(tr("Replace with:"), m_widget);
m_replaceLabel->setContentsMargins(12, 0, 5, 0);
m_replaceTextEdit = new QLineEdit(m_widget);
m_replaceButton = new QToolButton(m_widget);
2009-10-12 12:33:12 +02:00
m_replaceButton->setToolTip(tr("Replace all occurrences"));
m_replaceButton->setText(tr("Replace"));
m_replaceButton->setToolButtonStyle(Qt::ToolButtonTextOnly);
m_replaceButton->setAutoRaise(true);
m_replaceTextEdit->setTabOrder(m_replaceTextEdit, m_searchResultTreeView);
connect(m_searchResultTreeView, SIGNAL(jumpToSearchResult(int,bool)),
this, SLOT(handleJumpToSearchResult(int,bool)));
2008-12-02 12:01:29 +01:00
connect(m_expandCollapseToolButton, SIGNAL(toggled(bool)), this, SLOT(handleExpandCollapseToolButton(bool)));
2009-10-05 18:02:46 +02:00
connect(m_replaceTextEdit, SIGNAL(returnPressed()), this, SLOT(handleReplaceButton()));
connect(m_replaceButton, SIGNAL(clicked()), this, SLOT(handleReplaceButton()));
2008-12-02 12:01:29 +01:00
readSettings();
setShowReplaceUI(false);
2008-12-02 12:01:29 +01:00
}
SearchResultWindow::~SearchResultWindow()
{
writeSettings();
delete m_currentSearch;
m_currentSearch = 0;
2008-12-02 12:01:29 +01:00
delete m_widget;
m_widget = 0;
m_items.clear();
}
void SearchResultWindow::setTextToReplace(const QString &textToReplace)
{
m_replaceTextEdit->setText(textToReplace);
}
QString SearchResultWindow::textToReplace() const
{
return m_replaceTextEdit->text();
}
void SearchResultWindow::setShowReplaceUI(bool show)
{
2009-09-29 18:16:13 +02:00
m_searchResultTreeView->model()->setShowReplaceUI(show);
m_replaceLabel->setVisible(show);
m_replaceTextEdit->setVisible(show);
m_replaceButton->setVisible(show);
m_isShowingReplaceUI = show;
}
void SearchResultWindow::handleReplaceButton()
{
QTC_ASSERT(m_currentSearch, return);
// check if button is actually enabled, because this is also triggered
// by pressing return in replace line edit
if (m_replaceButton->isEnabled())
m_currentSearch->replaceButtonClicked(m_replaceTextEdit->text(), checkedItems());
}
QList<SearchResultItem> SearchResultWindow::checkedItems() const
{
QList<SearchResultItem> result;
SearchResultTreeModel *model = m_searchResultTreeView->model();
const int fileCount = model->rowCount(QModelIndex());
for (int i = 0; i < fileCount; ++i) {
QModelIndex fileIndex = model->index(i, 0, QModelIndex());
SearchResultFile *fileItem = static_cast<SearchResultFile *>(fileIndex.internalPointer());
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;
}
2008-12-02 12:01:29 +01:00
void SearchResultWindow::visibilityChanged(bool /*visible*/)
{
}
QWidget *SearchResultWindow::outputWidget(QWidget *)
{
return m_widget;
}
QList<QWidget*> SearchResultWindow::toolBarWidgets() const
2008-12-02 12:01:29 +01:00
{
return QList<QWidget*>() << m_expandCollapseToolButton << m_replaceLabel << m_replaceTextEdit << m_replaceButton;
2008-12-02 12:01:29 +01:00
}
SearchResult *SearchResultWindow::startNewSearch(SearchMode searchOrSearchAndReplace)
{
clearContents();
setShowReplaceUI(searchOrSearchAndReplace != SearchOnly);
delete m_currentSearch;
m_currentSearch = new SearchResult;
return m_currentSearch;
}
void SearchResultWindow::finishSearch()
{
if (m_items.count()) {
m_replaceButton->setEnabled(true);
} else {
showNoMatchesFound();
}
}
2008-12-02 12:01:29 +01:00
void SearchResultWindow::clearContents()
{
m_replaceTextEdit->setEnabled(false);
m_replaceButton->setEnabled(false);
m_replaceTextEdit->clear();
2008-12-02 12:01:29 +01:00
m_searchResultTreeView->clear();
m_items.clear();
m_widget->setCurrentWidget(m_searchResultTreeView);
navigateStateChanged();
2008-12-02 12:01:29 +01:00
}
void SearchResultWindow::showNoMatchesFound()
2008-12-02 12:01:29 +01:00
{
m_replaceTextEdit->setEnabled(false);
m_replaceButton->setEnabled(false);
2008-12-02 12:01:29 +01:00
m_widget->setCurrentWidget(m_noMatchesFoundDisplay);
}
bool SearchResultWindow::isEmpty() const
{
return (m_searchResultTreeView->model()->rowCount() < 1);
}
int SearchResultWindow::numberOfResults() const
{
return m_items.count();
2008-12-02 12:01:29 +01:00
}
bool SearchResultWindow::hasFocus()
{
return m_searchResultTreeView->hasFocus() || (m_isShowingReplaceUI && m_replaceTextEdit->hasFocus());
}
bool SearchResultWindow::canFocus()
{
return !m_items.isEmpty();
}
void SearchResultWindow::setFocus()
{
if (!m_items.isEmpty()) {
if (!m_isShowingReplaceUI) {
m_searchResultTreeView->setFocus();
} else {
if (!m_widget->focusWidget()
|| m_widget->focusWidget() == m_replaceTextEdit
|| m_focusReplaceEdit) {
m_replaceTextEdit->setFocus();
m_replaceTextEdit->selectAll();
} else {
m_searchResultTreeView->setFocus();
}
}
}
}
void SearchResultWindow::setTextEditorFont(const QFont &font)
{
m_searchResultTreeView->setTextEditorFont(font);
}
2009-10-12 12:33:12 +02:00
void SearchResultWindow::handleJumpToSearchResult(int index, bool /* checked */)
2008-12-02 12:01:29 +01:00
{
QTC_ASSERT(m_currentSearch, return);
m_currentSearch->activated(m_items.at(index));
2008-12-02 12:01:29 +01:00
}
void SearchResultWindow::addResult(const QString &fileName, int lineNumber, const QString &rowText,
int searchTermStart, int searchTermLength, const QVariant &userData)
2008-12-02 12:01:29 +01:00
{
2009-07-24 15:47:22 +02:00
//qDebug()<<"###"<<fileName;
2008-12-02 12:01:29 +01:00
m_widget->setCurrentWidget(m_searchResultTreeView);
int index = m_items.size();
SearchResultItem item;
item.fileName = fileName;
item.lineNumber = lineNumber;
item.lineText = rowText;
item.searchTermStart = searchTermStart;
item.searchTermLength = searchTermLength;
item.userData = userData;
item.index = index;
2008-12-02 12:01:29 +01:00
m_items.append(item);
m_searchResultTreeView->appendResultLine(index, fileName, lineNumber, rowText, searchTermStart, searchTermLength);
if (index == 0) {
m_replaceTextEdit->setEnabled(true);
// We didn't have an item before, set the focus to the search widget
m_focusReplaceEdit = true;
setFocus();
m_focusReplaceEdit = false;
2008-12-02 12:01:29 +01:00
m_searchResultTreeView->selectionModel()->select(m_searchResultTreeView->model()->index(0, 0, QModelIndex()), QItemSelectionModel::Select);
emit navigateStateChanged();
2008-12-02 12:01:29 +01:00
}
}
2008-12-02 12:01:29 +01:00
void SearchResultWindow::handleExpandCollapseToolButton(bool checked)
{
m_searchResultTreeView->setAutoExpandResults(checked);
if (checked)
m_searchResultTreeView->expandAll();
else
m_searchResultTreeView->collapseAll();
}
void SearchResultWindow::readSettings()
2008-12-02 12:01:29 +01:00
{
QSettings *s = Core::ICore::instance()->settings();
if (s) {
2008-12-02 12:01:29 +01:00
s->beginGroup(SETTINGSKEYSECTIONNAME);
m_expandCollapseToolButton->setChecked(s->value(SETTINGSKEYEXPANDRESULTS, m_initiallyExpand).toBool());
s->endGroup();
}
}
void SearchResultWindow::writeSettings()
2008-12-02 12:01:29 +01:00
{
QSettings *s = Core::ICore::instance()->settings();
if (s) {
2008-12-02 12:01:29 +01:00
s->beginGroup(SETTINGSKEYSECTIONNAME);
s->setValue(SETTINGSKEYEXPANDRESULTS, m_expandCollapseToolButton->isChecked());
s->endGroup();
}
}
int SearchResultWindow::priorityInStatusBar() const
{
return 80;
}
bool SearchResultWindow::canNext()
{
return m_items.count() > 0;
}
bool SearchResultWindow::canPrevious()
{
return m_items.count() > 0;
}
void SearchResultWindow::goToNext()
{
if (m_items.count() == 0)
return;
QModelIndex idx = m_searchResultTreeView->model()->next(m_searchResultTreeView->currentIndex());
if (idx.isValid()) {
m_searchResultTreeView->setCurrentIndex(idx);
m_searchResultTreeView->emitJumpToSearchResult(idx);
}
}
void SearchResultWindow::goToPrev()
{
if (!m_searchResultTreeView->model()->rowCount())
return;
QModelIndex idx = m_searchResultTreeView->model()->prev(m_searchResultTreeView->currentIndex());
if (idx.isValid()) {
m_searchResultTreeView->setCurrentIndex(idx);
m_searchResultTreeView->emitJumpToSearchResult(idx);
}
}
bool SearchResultWindow::canNavigate()
{
return true;
}