2009-02-25 09:15:00 +01:00
|
|
|
/**************************************************************************
|
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
|
|
|
**
|
2009-06-17 00:01:27 +10:00
|
|
|
** Contact: Nokia Corporation (qt-info@nokia.com)
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** Commercial Usage
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** 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.
|
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
|
|
|
**
|
2009-02-25 09:15:00 +01: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.
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** 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
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
**************************************************************************/
|
2008-12-09 15:25:01 +01:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
#include "findplugin.h"
|
|
|
|
|
|
|
|
|
|
#include "textfindconstants.h"
|
|
|
|
|
#include "currentdocumentfind.h"
|
2010-01-21 17:23:31 +01:00
|
|
|
#include "findtoolbar.h"
|
2008-12-02 12:01:29 +01:00
|
|
|
#include "findtoolwindow.h"
|
|
|
|
|
#include "searchresultwindow.h"
|
|
|
|
|
|
2009-01-13 13:39:31 +01:00
|
|
|
#include <coreplugin/actionmanager/actionmanager.h>
|
2009-01-14 12:39:59 +01:00
|
|
|
#include <coreplugin/actionmanager/actioncontainer.h>
|
2009-01-14 13:17:53 +01:00
|
|
|
#include <coreplugin/actionmanager/command.h>
|
2008-12-02 12:01:29 +01:00
|
|
|
#include <coreplugin/coreconstants.h>
|
2009-01-21 18:12:38 +01:00
|
|
|
#include <coreplugin/icore.h>
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2009-01-19 12:39:20 +01:00
|
|
|
#include <extensionsystem/pluginmanager.h>
|
|
|
|
|
|
2008-12-09 15:25:01 +01:00
|
|
|
#include <utils/qtcassert.h>
|
|
|
|
|
|
2010-03-18 10:59:06 +01:00
|
|
|
#include <QtGui/QMenu>
|
|
|
|
|
|
2009-01-19 12:39:20 +01:00
|
|
|
#include <QtCore/QtPlugin>
|
2008-12-02 12:01:29 +01:00
|
|
|
#include <QtCore/QSettings>
|
|
|
|
|
|
2009-04-24 16:00:19 +02:00
|
|
|
/*!
|
|
|
|
|
\namespace Find
|
|
|
|
|
The Find namespace provides everything that has to do with search term based searches.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
\namespace Find::Internal
|
|
|
|
|
\internal
|
|
|
|
|
*/
|
|
|
|
|
/*!
|
|
|
|
|
\namespace Find::Internal::ItemDataRoles
|
|
|
|
|
\internal
|
|
|
|
|
*/
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
Q_DECLARE_METATYPE(Find::IFindFilter*)
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
const int MAX_COMPLETIONS = 50;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using namespace Find;
|
|
|
|
|
using namespace Find::Internal;
|
|
|
|
|
|
2010-01-21 17:23:31 +01:00
|
|
|
FindPlugin *FindPlugin::m_instance = 0;
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
FindPlugin::FindPlugin()
|
2009-01-20 11:52:04 +01:00
|
|
|
: m_currentDocumentFind(0),
|
2008-12-02 12:01:29 +01:00
|
|
|
m_findToolBar(0),
|
|
|
|
|
m_findDialog(0),
|
|
|
|
|
m_findCompletionModel(new QStringListModel(this)),
|
|
|
|
|
m_replaceCompletionModel(new QStringListModel(this))
|
|
|
|
|
{
|
2010-01-21 17:23:31 +01:00
|
|
|
QTC_ASSERT(!m_instance, return);
|
|
|
|
|
m_instance = this;
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FindPlugin::~FindPlugin()
|
|
|
|
|
{
|
2010-01-21 17:23:31 +01:00
|
|
|
m_instance = 0;
|
2008-12-02 12:01:29 +01:00
|
|
|
delete m_currentDocumentFind;
|
|
|
|
|
delete m_findToolBar;
|
|
|
|
|
delete m_findDialog;
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-21 17:23:31 +01:00
|
|
|
FindPlugin *FindPlugin::instance()
|
|
|
|
|
{
|
|
|
|
|
return m_instance;
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
bool FindPlugin::initialize(const QStringList &, QString *)
|
|
|
|
|
{
|
|
|
|
|
setupMenu();
|
|
|
|
|
|
2009-01-21 18:09:37 +01:00
|
|
|
m_currentDocumentFind = new CurrentDocumentFind;
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
m_findToolBar = new FindToolBar(this, m_currentDocumentFind);
|
|
|
|
|
m_findDialog = new FindToolWindow(this);
|
2009-01-21 18:09:37 +01:00
|
|
|
SearchResultWindow *searchResultWindow = new SearchResultWindow;
|
2008-12-02 12:01:29 +01:00
|
|
|
addAutoReleasedObject(searchResultWindow);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FindPlugin::extensionsInitialized()
|
|
|
|
|
{
|
|
|
|
|
setupFilterMenuItems();
|
|
|
|
|
readSettings();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FindPlugin::shutdown()
|
|
|
|
|
{
|
2009-07-21 17:48:24 +02:00
|
|
|
m_findToolBar->setVisible(false);
|
2008-12-02 12:01:29 +01:00
|
|
|
m_findToolBar->setParent(0);
|
|
|
|
|
m_currentDocumentFind->removeConnections();
|
|
|
|
|
writeSettings();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FindPlugin::filterChanged()
|
|
|
|
|
{
|
|
|
|
|
IFindFilter *changedFilter = qobject_cast<IFindFilter *>(sender());
|
|
|
|
|
QAction *action = m_filterActions.value(changedFilter);
|
2008-12-09 15:25:01 +01:00
|
|
|
QTC_ASSERT(changedFilter, return);
|
|
|
|
|
QTC_ASSERT(action, return);
|
2008-12-02 12:01:29 +01:00
|
|
|
action->setEnabled(changedFilter->isEnabled());
|
2009-03-26 16:13:01 +01:00
|
|
|
bool haveEnabledFilters = false;
|
|
|
|
|
foreach (IFindFilter *filter, m_filterActions.keys()) {
|
|
|
|
|
if (filter->isEnabled()) {
|
|
|
|
|
haveEnabledFilters = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
m_openFindDialog->setEnabled(haveEnabledFilters);
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FindPlugin::openFindFilter()
|
|
|
|
|
{
|
|
|
|
|
QAction *action = qobject_cast<QAction*>(sender());
|
2008-12-09 15:25:01 +01:00
|
|
|
QTC_ASSERT(action, return);
|
2008-12-02 12:01:29 +01:00
|
|
|
IFindFilter *filter = action->data().value<IFindFilter *>();
|
2009-10-21 15:10:27 +02:00
|
|
|
if (m_currentDocumentFind->candidateIsEnabled())
|
|
|
|
|
m_currentDocumentFind->acceptCandidate();
|
2008-12-02 12:01:29 +01:00
|
|
|
QString currentFindString = (m_currentDocumentFind->isEnabled() ? m_currentDocumentFind->currentFindString() : "");
|
|
|
|
|
if (!currentFindString.isEmpty())
|
|
|
|
|
m_findDialog->setFindText(currentFindString);
|
|
|
|
|
m_findDialog->open(filter);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FindPlugin::setupMenu()
|
|
|
|
|
{
|
2009-01-21 18:09:37 +01:00
|
|
|
Core::ActionManager *am = Core::ICore::instance()->actionManager();
|
2009-01-14 12:39:59 +01:00
|
|
|
Core::ActionContainer *medit = am->actionContainer(Core::Constants::M_EDIT);
|
|
|
|
|
Core::ActionContainer *mfind = am->createMenu(Constants::M_FIND);
|
2008-12-02 12:01:29 +01:00
|
|
|
medit->addMenu(mfind, Core::Constants::G_EDIT_FIND);
|
|
|
|
|
mfind->menu()->setTitle(tr("&Find/Replace"));
|
2009-03-26 16:13:01 +01:00
|
|
|
mfind->appendGroup(Constants::G_FIND_CURRENTDOCUMENT);
|
2008-12-02 12:01:29 +01:00
|
|
|
mfind->appendGroup(Constants::G_FIND_FILTERS);
|
|
|
|
|
mfind->appendGroup(Constants::G_FIND_FLAGS);
|
|
|
|
|
mfind->appendGroup(Constants::G_FIND_ACTIONS);
|
|
|
|
|
QList<int> globalcontext = QList<int>() << Core::Constants::C_GLOBAL_ID;
|
2009-01-14 13:17:53 +01:00
|
|
|
Core::Command *cmd;
|
2008-12-02 12:01:29 +01:00
|
|
|
QAction *separator;
|
|
|
|
|
separator = new QAction(this);
|
|
|
|
|
separator->setSeparator(true);
|
|
|
|
|
cmd = am->registerAction(separator, QLatin1String("Find.Sep.Flags"), globalcontext);
|
|
|
|
|
mfind->addAction(cmd, Constants::G_FIND_FLAGS);
|
|
|
|
|
separator = new QAction(this);
|
|
|
|
|
separator->setSeparator(true);
|
|
|
|
|
cmd = am->registerAction(separator, QLatin1String("Find.Sep.Actions"), globalcontext);
|
|
|
|
|
mfind->addAction(cmd, Constants::G_FIND_ACTIONS);
|
2009-03-26 16:13:01 +01:00
|
|
|
|
2009-12-14 15:41:43 +01:00
|
|
|
Core::ActionContainer *mfindadvanced = am->createMenu(Constants::M_FIND_ADVANCED);
|
|
|
|
|
mfindadvanced->menu()->setTitle(tr("Advanced Find"));
|
|
|
|
|
mfind->addMenu(mfindadvanced, Constants::G_FIND_FILTERS);
|
|
|
|
|
m_openFindDialog = new QAction(tr("Open Advanced Find..."), this);
|
2009-03-26 16:13:01 +01:00
|
|
|
cmd = am->registerAction(m_openFindDialog, QLatin1String("Find.Dialog"), globalcontext);
|
|
|
|
|
cmd->setDefaultKeySequence(QKeySequence(tr("Ctrl+Shift+F")));
|
2009-12-14 15:41:43 +01:00
|
|
|
mfindadvanced->addAction(cmd);
|
2009-03-26 16:13:01 +01:00
|
|
|
connect(m_openFindDialog, SIGNAL(triggered()), this, SLOT(openFindFilter()));
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FindPlugin::setupFilterMenuItems()
|
|
|
|
|
{
|
2009-01-21 18:09:37 +01:00
|
|
|
Core::ActionManager *am = Core::ICore::instance()->actionManager();
|
2008-12-02 12:01:29 +01:00
|
|
|
QList<IFindFilter*> findInterfaces =
|
|
|
|
|
ExtensionSystem::PluginManager::instance()->getObjects<IFindFilter>();
|
2009-01-14 13:17:53 +01:00
|
|
|
Core::Command *cmd;
|
2008-12-02 12:01:29 +01:00
|
|
|
QList<int> globalcontext = QList<int>() << Core::Constants::C_GLOBAL_ID;
|
|
|
|
|
|
2009-12-14 15:41:43 +01:00
|
|
|
Core::ActionContainer *mfindadvanced = am->actionContainer(Constants::M_FIND_ADVANCED);
|
2008-12-02 12:01:29 +01:00
|
|
|
m_filterActions.clear();
|
2009-03-26 16:13:01 +01:00
|
|
|
bool haveEnabledFilters = false;
|
2008-12-02 12:01:29 +01:00
|
|
|
foreach (IFindFilter *filter, findInterfaces) {
|
2009-03-26 16:13:01 +01:00
|
|
|
QAction *action = new QAction(QString(" %1").arg(filter->name()), this);
|
|
|
|
|
bool isEnabled = filter->isEnabled();
|
|
|
|
|
if (isEnabled)
|
|
|
|
|
haveEnabledFilters = true;
|
|
|
|
|
action->setEnabled(isEnabled);
|
2008-12-02 12:01:29 +01:00
|
|
|
action->setData(qVariantFromValue(filter));
|
2009-10-29 17:12:57 +01:00
|
|
|
cmd = am->registerAction(action, QLatin1String("FindFilter.")+filter->id(), globalcontext);
|
2008-12-02 12:01:29 +01:00
|
|
|
cmd->setDefaultKeySequence(filter->defaultShortcut());
|
2009-12-14 17:20:14 +01:00
|
|
|
mfindadvanced->addAction(cmd);
|
2008-12-02 12:01:29 +01:00
|
|
|
m_filterActions.insert(filter, action);
|
|
|
|
|
connect(action, SIGNAL(triggered(bool)), this, SLOT(openFindFilter()));
|
|
|
|
|
connect(filter, SIGNAL(changed()), this, SLOT(filterChanged()));
|
|
|
|
|
}
|
|
|
|
|
m_findDialog->setFindFilters(findInterfaces);
|
2009-03-26 16:13:01 +01:00
|
|
|
m_openFindDialog->setEnabled(haveEnabledFilters);
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QTextDocument::FindFlags FindPlugin::findFlags() const
|
|
|
|
|
{
|
|
|
|
|
return m_findFlags;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FindPlugin::setCaseSensitive(bool sensitive)
|
|
|
|
|
{
|
|
|
|
|
setFindFlag(QTextDocument::FindCaseSensitively, sensitive);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FindPlugin::setWholeWord(bool wholeOnly)
|
|
|
|
|
{
|
|
|
|
|
setFindFlag(QTextDocument::FindWholeWords, wholeOnly);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FindPlugin::setBackward(bool backward)
|
|
|
|
|
{
|
|
|
|
|
setFindFlag(QTextDocument::FindBackward, backward);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FindPlugin::setFindFlag(QTextDocument::FindFlag flag, bool enabled)
|
|
|
|
|
{
|
|
|
|
|
bool hasFlag = hasFindFlag(flag);
|
|
|
|
|
if ((hasFlag && enabled) || (!hasFlag && !enabled))
|
|
|
|
|
return;
|
|
|
|
|
if (enabled)
|
|
|
|
|
m_findFlags |= flag;
|
|
|
|
|
else
|
|
|
|
|
m_findFlags &= ~flag;
|
|
|
|
|
if (flag != QTextDocument::FindBackward)
|
|
|
|
|
emit findFlagsChanged();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool FindPlugin::hasFindFlag(QTextDocument::FindFlag flag)
|
|
|
|
|
{
|
|
|
|
|
return m_findFlags & flag;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FindPlugin::writeSettings()
|
|
|
|
|
{
|
2009-01-20 11:52:04 +01:00
|
|
|
QSettings *settings = Core::ICore::instance()->settings();
|
2008-12-02 12:01:29 +01:00
|
|
|
settings->beginGroup("Find");
|
|
|
|
|
settings->setValue("Backward", QVariant((m_findFlags & QTextDocument::FindBackward) != 0));
|
|
|
|
|
settings->setValue("CaseSensitively", QVariant((m_findFlags & QTextDocument::FindCaseSensitively) != 0));
|
|
|
|
|
settings->setValue("WholeWords", QVariant((m_findFlags & QTextDocument::FindWholeWords) != 0));
|
|
|
|
|
settings->setValue("FindStrings", m_findCompletions);
|
|
|
|
|
settings->setValue("ReplaceStrings", m_replaceCompletions);
|
|
|
|
|
settings->endGroup();
|
2009-05-29 16:12:19 +02:00
|
|
|
m_findToolBar->writeSettings();
|
2008-12-02 12:01:29 +01:00
|
|
|
m_findDialog->writeSettings();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FindPlugin::readSettings()
|
|
|
|
|
{
|
2009-01-20 11:52:04 +01:00
|
|
|
QSettings *settings = Core::ICore::instance()->settings();
|
2008-12-02 12:01:29 +01:00
|
|
|
settings->beginGroup("Find");
|
|
|
|
|
bool block = blockSignals(true);
|
|
|
|
|
setBackward(settings->value("Backward", false).toBool());
|
|
|
|
|
setCaseSensitive(settings->value("CaseSensitively", false).toBool());
|
|
|
|
|
setWholeWord(settings->value("WholeWords", false).toBool());
|
|
|
|
|
blockSignals(block);
|
|
|
|
|
m_findCompletions = settings->value("FindStrings").toStringList();
|
|
|
|
|
m_replaceCompletions = settings->value("ReplaceStrings").toStringList();
|
|
|
|
|
m_findCompletionModel->setStringList(m_findCompletions);
|
|
|
|
|
m_replaceCompletionModel->setStringList(m_replaceCompletions);
|
|
|
|
|
settings->endGroup();
|
2009-05-29 16:12:19 +02:00
|
|
|
m_findToolBar->readSettings();
|
2008-12-02 12:01:29 +01:00
|
|
|
m_findDialog->readSettings();
|
|
|
|
|
emit findFlagsChanged(); // would have been done in the setXXX methods above
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FindPlugin::updateFindCompletion(const QString &text)
|
|
|
|
|
{
|
|
|
|
|
updateCompletion(text, m_findCompletions, m_findCompletionModel);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FindPlugin::updateReplaceCompletion(const QString &text)
|
|
|
|
|
{
|
|
|
|
|
updateCompletion(text, m_replaceCompletions, m_replaceCompletionModel);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FindPlugin::updateCompletion(const QString &text, QStringList &completions, QStringListModel *model)
|
|
|
|
|
{
|
|
|
|
|
if (text.isEmpty())
|
|
|
|
|
return;
|
|
|
|
|
completions.removeAll(text);
|
|
|
|
|
completions.prepend(text);
|
|
|
|
|
while (completions.size() > MAX_COMPLETIONS)
|
|
|
|
|
completions.removeLast();
|
|
|
|
|
model->setStringList(completions);
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-21 17:23:31 +01:00
|
|
|
void FindPlugin::setUseFakeVim(bool on)
|
|
|
|
|
{
|
|
|
|
|
if (m_findToolBar)
|
|
|
|
|
m_findToolBar->setUseFakeVim(on);
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-21 17:23:31 +01:00
|
|
|
void FindPlugin::openFindToolBar(FindDirection direction)
|
|
|
|
|
{
|
|
|
|
|
if (m_findToolBar) {
|
|
|
|
|
m_findToolBar->setBackward(direction == FindBackward);
|
|
|
|
|
m_findToolBar->openFindToolBar();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
Q_EXPORT_PLUGIN(FindPlugin)
|