2009-02-25 09:15:00 +01:00
|
|
|
/**************************************************************************
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator
|
|
|
|
|
**
|
2011-01-11 16:28:15 +01:00
|
|
|
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2011-11-02 15:59:12 +01:00
|
|
|
** Contact: Nokia Corporation (qt-info@nokia.com)
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** GNU Lesser General Public License Usage
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2011-04-13 08:42:33 +02:00
|
|
|
** 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.
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2010-12-17 16:01:08 +01:00
|
|
|
** In addition, as a special exception, Nokia gives you certain additional
|
2011-04-13 08:42:33 +02:00
|
|
|
** rights. These rights are described in the Nokia Qt LGPL Exception
|
2010-12-17 16:01:08 +01:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
2011-04-13 08:42:33 +02:00
|
|
|
** Other Usage
|
|
|
|
|
**
|
|
|
|
|
** Alternatively, this file may be used in accordance with the terms and
|
|
|
|
|
** conditions contained in a signed written agreement between you and Nokia.
|
|
|
|
|
**
|
2010-12-17 16:01:08 +01:00
|
|
|
** If you have questions regarding the use of this file, please contact
|
2011-11-02 15:59:12 +01:00
|
|
|
** Nokia at qt-info@nokia.com.
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
**************************************************************************/
|
2008-12-02 15:08:31 +01:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
#include "findtoolwindow.h"
|
|
|
|
|
#include "findplugin.h"
|
|
|
|
|
|
|
|
|
|
#include <coreplugin/icore.h>
|
|
|
|
|
|
2009-10-01 16:38:08 +02:00
|
|
|
#include <QtCore/QSettings>
|
2008-12-02 12:01:29 +01:00
|
|
|
#include <QtGui/QMainWindow>
|
2010-03-24 10:43:01 +01:00
|
|
|
#include <QtGui/QStringListModel>
|
|
|
|
|
#include <QtGui/QCompleter>
|
2010-07-06 11:21:19 +02:00
|
|
|
#include <QtGui/QKeyEvent>
|
2011-12-06 16:57:09 +01:00
|
|
|
#include <QtGui/QScrollArea>
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
using namespace Find;
|
|
|
|
|
using namespace Find::Internal;
|
|
|
|
|
|
2011-09-09 08:46:51 +02:00
|
|
|
static FindToolWindow *m_instance = 0;
|
|
|
|
|
|
|
|
|
|
FindToolWindow::FindToolWindow(FindPlugin *plugin, QWidget *parent)
|
|
|
|
|
: QWidget(parent),
|
2008-12-02 12:01:29 +01:00
|
|
|
m_plugin(plugin),
|
2009-04-03 15:53:29 +02:00
|
|
|
m_findCompleter(new QCompleter(this)),
|
2010-09-02 15:53:01 +02:00
|
|
|
m_currentFilter(0),
|
|
|
|
|
m_configWidget(0)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2011-09-09 08:46:51 +02:00
|
|
|
m_instance = this;
|
2008-12-02 12:01:29 +01:00
|
|
|
m_ui.setupUi(this);
|
2011-09-09 08:46:51 +02:00
|
|
|
setFocusProxy(m_ui.searchTerm);
|
|
|
|
|
|
2009-12-21 11:08:20 +01:00
|
|
|
connect(m_ui.searchButton, SIGNAL(clicked()), this, SLOT(search()));
|
|
|
|
|
connect(m_ui.replaceButton, SIGNAL(clicked()), this, SLOT(replace()));
|
2008-12-02 12:01:29 +01:00
|
|
|
connect(m_ui.matchCase, SIGNAL(toggled(bool)), m_plugin, SLOT(setCaseSensitive(bool)));
|
|
|
|
|
connect(m_ui.wholeWords, SIGNAL(toggled(bool)), m_plugin, SLOT(setWholeWord(bool)));
|
2010-07-15 16:40:05 +02:00
|
|
|
connect(m_ui.regExp, SIGNAL(toggled(bool)), m_plugin, SLOT(setRegularExpression(bool)));
|
2009-04-03 15:53:29 +02:00
|
|
|
connect(m_ui.filterList, SIGNAL(activated(int)), this, SLOT(setCurrentFilter(int)));
|
2010-02-26 15:45:48 +01:00
|
|
|
connect(m_ui.searchTerm, SIGNAL(textChanged(QString)), this, SLOT(updateButtonStates()));
|
2010-09-02 16:13:33 +02:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
m_findCompleter->setModel(m_plugin->findCompletionModel());
|
|
|
|
|
m_ui.searchTerm->setCompleter(m_findCompleter);
|
2010-07-06 11:21:19 +02:00
|
|
|
m_ui.searchTerm->installEventFilter(this);
|
2008-12-02 12:01:29 +01:00
|
|
|
QVBoxLayout *layout = new QVBoxLayout;
|
|
|
|
|
layout->setMargin(0);
|
|
|
|
|
layout->setSpacing(0);
|
|
|
|
|
m_ui.configWidget->setLayout(layout);
|
2010-02-26 15:45:48 +01:00
|
|
|
updateButtonStates();
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FindToolWindow::~FindToolWindow()
|
|
|
|
|
{
|
|
|
|
|
qDeleteAll(m_configWidgets);
|
|
|
|
|
}
|
|
|
|
|
|
2011-09-09 08:46:51 +02:00
|
|
|
FindToolWindow *FindToolWindow::instance()
|
|
|
|
|
{
|
|
|
|
|
return m_instance;
|
|
|
|
|
}
|
|
|
|
|
|
2011-09-20 17:23:05 +02:00
|
|
|
bool FindToolWindow::event(QEvent *event)
|
|
|
|
|
{
|
|
|
|
|
if (event->type() == QEvent::KeyPress) {
|
|
|
|
|
QKeyEvent *ke = static_cast<QKeyEvent *>(event);
|
|
|
|
|
if ((ke->key() == Qt::Key_Return || ke->key() == Qt::Key_Enter)
|
2011-09-26 14:49:27 +02:00
|
|
|
&& (ke->modifiers() == Qt::NoModifier || ke->modifiers() == Qt::KeypadModifier)) {
|
2011-09-20 17:23:05 +02:00
|
|
|
ke->accept();
|
|
|
|
|
search();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return QWidget::event(event);
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-06 11:21:19 +02:00
|
|
|
bool FindToolWindow::eventFilter(QObject *obj, QEvent *event)
|
|
|
|
|
{
|
|
|
|
|
if (obj == m_ui.searchTerm && event->type() == QEvent::KeyPress) {
|
|
|
|
|
QKeyEvent *ke = static_cast<QKeyEvent *>(event);
|
|
|
|
|
if (ke->key() == Qt::Key_Down) {
|
|
|
|
|
m_findCompleter->complete();
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-09-09 08:46:51 +02:00
|
|
|
return QWidget::eventFilter(obj, event);
|
2010-07-06 11:21:19 +02:00
|
|
|
}
|
|
|
|
|
|
2010-02-26 15:45:48 +01:00
|
|
|
void FindToolWindow::updateButtonStates()
|
|
|
|
|
{
|
2010-09-02 15:53:01 +02:00
|
|
|
bool filterEnabled = m_currentFilter && m_currentFilter->isEnabled();
|
|
|
|
|
bool enabled = !m_ui.searchTerm->text().isEmpty() && filterEnabled;
|
2010-02-26 15:45:48 +01:00
|
|
|
m_ui.searchButton->setEnabled(enabled);
|
|
|
|
|
m_ui.replaceButton->setEnabled(m_currentFilter
|
|
|
|
|
&& m_currentFilter->isReplaceSupported() && enabled);
|
2010-09-02 15:53:01 +02:00
|
|
|
if (m_configWidget)
|
|
|
|
|
m_configWidget->setEnabled(filterEnabled);
|
|
|
|
|
|
|
|
|
|
m_ui.matchCase->setEnabled(filterEnabled
|
|
|
|
|
&& (m_currentFilter->supportedFindFlags() & Find::FindCaseSensitively));
|
|
|
|
|
m_ui.wholeWords->setEnabled(filterEnabled
|
|
|
|
|
&& (m_currentFilter->supportedFindFlags() & Find::FindWholeWords));
|
|
|
|
|
m_ui.regExp->setEnabled(filterEnabled
|
|
|
|
|
&& (m_currentFilter->supportedFindFlags() & Find::FindRegularExpression));
|
|
|
|
|
m_ui.searchTerm->setEnabled(filterEnabled);
|
2010-02-26 15:45:48 +01:00
|
|
|
}
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
void FindToolWindow::setFindFilters(const QList<IFindFilter *> &filters)
|
|
|
|
|
{
|
|
|
|
|
qDeleteAll(m_configWidgets);
|
|
|
|
|
m_configWidgets.clear();
|
|
|
|
|
m_filters = filters;
|
|
|
|
|
m_ui.filterList->clear();
|
|
|
|
|
QStringList names;
|
|
|
|
|
foreach (IFindFilter *filter, filters) {
|
2010-07-12 12:06:15 +02:00
|
|
|
names << filter->displayName();
|
2008-12-02 12:01:29 +01:00
|
|
|
m_configWidgets.append(filter->createConfigWidget());
|
|
|
|
|
}
|
|
|
|
|
m_ui.filterList->addItems(names);
|
2009-04-03 15:53:29 +02:00
|
|
|
if (m_filters.size() > 0)
|
|
|
|
|
setCurrentFilter(0);
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FindToolWindow::setFindText(const QString &text)
|
|
|
|
|
{
|
|
|
|
|
m_ui.searchTerm->setText(text);
|
|
|
|
|
}
|
|
|
|
|
|
2011-09-09 08:46:51 +02:00
|
|
|
void FindToolWindow::setCurrentFilter(IFindFilter *filter)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2009-04-03 15:53:29 +02:00
|
|
|
if (!filter)
|
|
|
|
|
filter = m_currentFilter;
|
2008-12-02 12:01:29 +01:00
|
|
|
int index = m_filters.indexOf(filter);
|
|
|
|
|
if (index >= 0) {
|
2009-04-03 15:53:29 +02:00
|
|
|
setCurrentFilter(index);
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
2010-07-15 16:40:05 +02:00
|
|
|
m_ui.matchCase->setChecked(m_plugin->hasFindFlag(Find::FindCaseSensitively));
|
|
|
|
|
m_ui.wholeWords->setChecked(m_plugin->hasFindFlag(Find::FindWholeWords));
|
|
|
|
|
m_ui.regExp->setChecked(m_plugin->hasFindFlag(Find::FindRegularExpression));
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
m_ui.searchTerm->setFocus();
|
|
|
|
|
m_ui.searchTerm->selectAll();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FindToolWindow::setCurrentFilter(int index)
|
|
|
|
|
{
|
2009-04-03 15:53:29 +02:00
|
|
|
m_ui.filterList->setCurrentIndex(index);
|
2008-12-02 12:01:29 +01:00
|
|
|
for (int i = 0; i < m_configWidgets.size(); ++i) {
|
|
|
|
|
QWidget *configWidget = m_configWidgets.at(i);
|
2009-02-10 12:13:45 +01:00
|
|
|
if (i == index) {
|
2010-09-02 15:53:01 +02:00
|
|
|
m_configWidget = configWidget;
|
|
|
|
|
if (m_currentFilter)
|
2011-12-14 15:55:05 +01:00
|
|
|
disconnect(m_currentFilter, SIGNAL(enabledChanged(bool)), this, SLOT(updateButtonStates()));
|
2010-02-26 15:45:48 +01:00
|
|
|
m_currentFilter = m_filters.at(i);
|
2011-12-14 15:55:05 +01:00
|
|
|
connect(m_currentFilter, SIGNAL(enabledChanged(bool)), this, SLOT(updateButtonStates()));
|
2010-02-26 15:45:48 +01:00
|
|
|
updateButtonStates();
|
2010-09-02 15:53:01 +02:00
|
|
|
if (m_configWidget) {
|
|
|
|
|
m_ui.configWidget->layout()->addWidget(m_configWidget);
|
2010-02-26 15:45:48 +01:00
|
|
|
}
|
2009-02-10 12:13:45 +01:00
|
|
|
} else {
|
2010-02-26 15:45:48 +01:00
|
|
|
if (configWidget)
|
|
|
|
|
configWidget->setParent(0);
|
2009-02-10 12:13:45 +01:00
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
2011-12-06 16:57:09 +01:00
|
|
|
QWidget *w = m_ui.configWidget;
|
|
|
|
|
while (w) {
|
|
|
|
|
QScrollArea *sa = qobject_cast<QScrollArea *>(w);
|
|
|
|
|
if (sa) {
|
|
|
|
|
sa->updateGeometry();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
w = w->parentWidget();
|
|
|
|
|
}
|
|
|
|
|
for (w = m_configWidget ? m_configWidget : m_ui.configWidget; w; w = w->parentWidget()) {
|
|
|
|
|
if (w->layout())
|
|
|
|
|
w->layout()->activate();
|
|
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2009-12-21 11:08:20 +01:00
|
|
|
void FindToolWindow::acceptAndGetParameters(QString *term, IFindFilter **filter)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2009-12-21 11:08:20 +01:00
|
|
|
if (filter)
|
|
|
|
|
*filter = 0;
|
2008-12-02 12:01:29 +01:00
|
|
|
m_plugin->updateFindCompletion(m_ui.searchTerm->text());
|
|
|
|
|
int index = m_ui.filterList->currentIndex();
|
2009-12-21 11:08:20 +01:00
|
|
|
QString searchTerm = m_ui.searchTerm->text();
|
|
|
|
|
if (term)
|
|
|
|
|
*term = searchTerm;
|
|
|
|
|
if (searchTerm.isEmpty() || index < 0)
|
2008-12-02 12:01:29 +01:00
|
|
|
return;
|
2009-12-21 11:08:20 +01:00
|
|
|
if (filter)
|
|
|
|
|
*filter = m_filters.at(index);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FindToolWindow::search()
|
|
|
|
|
{
|
|
|
|
|
QString term;
|
|
|
|
|
IFindFilter *filter;
|
|
|
|
|
acceptAndGetParameters(&term, &filter);
|
2010-02-26 15:45:48 +01:00
|
|
|
if (filter)
|
|
|
|
|
filter->findAll(term, m_plugin->findFlags());
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2009-12-21 11:08:20 +01:00
|
|
|
void FindToolWindow::replace()
|
|
|
|
|
{
|
|
|
|
|
QString term;
|
|
|
|
|
IFindFilter *filter;
|
|
|
|
|
acceptAndGetParameters(&term, &filter);
|
|
|
|
|
filter->replaceAll(term, m_plugin->findFlags());
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
void FindToolWindow::writeSettings()
|
|
|
|
|
{
|
2009-01-20 11:52:04 +01:00
|
|
|
QSettings *settings = Core::ICore::instance()->settings();
|
2012-01-20 15:33:18 +01:00
|
|
|
settings->beginGroup(QLatin1String("Find"));
|
|
|
|
|
settings->setValue(QLatin1String("CurrentFilter"), m_currentFilter ? m_currentFilter->id() : QString());
|
2008-12-02 12:01:29 +01:00
|
|
|
foreach (IFindFilter *filter, m_filters)
|
|
|
|
|
filter->writeSettings(settings);
|
|
|
|
|
settings->endGroup();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FindToolWindow::readSettings()
|
|
|
|
|
{
|
2009-01-20 11:52:04 +01:00
|
|
|
QSettings *settings = Core::ICore::instance()->settings();
|
2012-01-20 15:33:18 +01:00
|
|
|
settings->beginGroup(QLatin1String("Find"));
|
|
|
|
|
const QString currentFilter = settings->value(QLatin1String("CurrentFilter")).toString();
|
2009-04-03 15:53:29 +02:00
|
|
|
for (int i = 0; i < m_filters.size(); ++i) {
|
|
|
|
|
IFindFilter *filter = m_filters.at(i);
|
2008-12-02 12:01:29 +01:00
|
|
|
filter->readSettings(settings);
|
2009-04-03 15:53:29 +02:00
|
|
|
if (filter->id() == currentFilter) {
|
|
|
|
|
setCurrentFilter(i);
|
|
|
|
|
}
|
|
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
settings->endGroup();
|
|
|
|
|
}
|