2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
|
|
|
|
|
** Contact: http://www.qt-project.org/legal
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
|
** a written agreement between you and Digia. For licensing terms and
|
|
|
|
|
** conditions see http://qt.digia.com/licensing. For further information
|
|
|
|
|
** use the contact form at http://qt.digia.com/contact-us.
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** GNU Lesser General Public License Usage
|
2012-10-02 09:12:39 +02:00
|
|
|
** 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.
|
|
|
|
|
**
|
|
|
|
|
** In addition, as a special exception, Digia gives you certain additional
|
|
|
|
|
** rights. These rights are described in the Digia Qt LGPL Exception
|
2010-12-17 16:01:08 +01:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
2012-10-02 09:12:39 +02: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>
|
|
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QSettings>
|
|
|
|
|
#include <QStringListModel>
|
|
|
|
|
#include <QCompleter>
|
|
|
|
|
#include <QKeyEvent>
|
|
|
|
|
#include <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);
|
2012-04-26 18:51:59 +04:00
|
|
|
m_ui.searchTerm->setPlaceholderText(QString());
|
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());
|
2012-08-23 14:10:12 +02:00
|
|
|
m_ui.searchTerm->setSpecialCompleter(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();
|
2012-05-07 12:56:22 +04:00
|
|
|
|
|
|
|
|
connect(m_plugin, SIGNAL(findFlagsChanged()), this, SLOT(updateFindFlags()));
|
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) {
|
2012-09-09 09:28:18 +03:00
|
|
|
if (m_ui.searchTerm->text().isEmpty())
|
|
|
|
|
m_findCompleter->setCompletionPrefix(QString());
|
2010-07-06 11:21:19 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2012-05-07 12:56:22 +04:00
|
|
|
void FindToolWindow::updateFindFlags()
|
|
|
|
|
{
|
|
|
|
|
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
|
|
|
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
|
|
|
}
|
2012-05-07 12:56:22 +04:00
|
|
|
updateFindFlags();
|
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()
|
|
|
|
|
{
|
2012-01-24 15:36:40 +01:00
|
|
|
QSettings *settings = Core::ICore::settings();
|
2012-01-20 15:33:18 +01:00
|
|
|
settings->beginGroup(QLatin1String("Find"));
|
2012-01-24 15:36:40 +01:00
|
|
|
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()
|
|
|
|
|
{
|
2012-01-24 15:36:40 +01:00
|
|
|
QSettings *settings = Core::ICore::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();
|
|
|
|
|
}
|