summaryrefslogtreecommitdiff
path: root/src/plugins/find/findplugin.cpp
blob: 92c52212b62babc8092256689b8717c01c0018dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/***************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact:  Qt Software Information (qt-info@nokia.com)
**
** 
** Non-Open Source Usage  
** 
** Licensees may use this file in accordance with the Qt Beta Version
** License Agreement, Agreement version 2.2 provided with the Software or,
** alternatively, in accordance with the terms contained in a written
** agreement between you and Nokia.  
** 
** GNU General Public License Usage 
** 
** Alternatively, this file may be used under the terms of the GNU General
** Public License versions 2.0 or 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the packaging
** of this file.  Please review the following information to ensure GNU
** General Public Licensing requirements will be met:
**
** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
** http://www.gnu.org/copyleft/gpl.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt GPL Exception version
** 1.2, included in the file GPL_EXCEPTION.txt in this package.  
** 
***************************************************************************/
#include "findplugin.h"

#include "textfindconstants.h"
#include "currentdocumentfind.h"
#include "findtoolwindow.h"
#include "searchresultwindow.h"

#include <coreplugin/actionmanager/actionmanagerinterface.h>
#include <coreplugin/actionmanager/iactioncontainer.h>
#include <coreplugin/actionmanager/icommand.h>
#include <coreplugin/coreconstants.h>

#include <QtCore/qplugin.h>
#include <QtCore/QSettings>

Q_DECLARE_METATYPE(Find::IFindFilter*)

namespace {
    const int MAX_COMPLETIONS = 50;
}

using namespace Find;
using namespace Find::Internal;

FindPlugin::FindPlugin()
    : m_core(0),
    m_currentDocumentFind(0),
    m_findToolBar(0),
    m_findDialog(0),
    m_findCompletionModel(new QStringListModel(this)),
    m_replaceCompletionModel(new QStringListModel(this))
{
}

FindPlugin::~FindPlugin()
{
    delete m_currentDocumentFind;
    delete m_findToolBar;
    delete m_findDialog;
}

bool FindPlugin::initialize(const QStringList &, QString *)
{
    m_core = ExtensionSystem::PluginManager::instance()->getObject<Core::ICore>();
    setupMenu();

    m_currentDocumentFind = new CurrentDocumentFind(m_core);

    m_findToolBar = new FindToolBar(this, m_currentDocumentFind);
    m_findDialog = new FindToolWindow(this);
    SearchResultWindow *searchResultWindow = new SearchResultWindow(m_core);
    addAutoReleasedObject(searchResultWindow);
    return true;
}

void FindPlugin::extensionsInitialized()
{
    setupFilterMenuItems();
    readSettings();
}

void FindPlugin::shutdown()
{
    m_findToolBar->setParent(0);
    m_currentDocumentFind->removeConnections();
    writeSettings();
}

void FindPlugin::filterChanged()
{
    IFindFilter *changedFilter = qobject_cast<IFindFilter *>(sender());
    QAction *action = m_filterActions.value(changedFilter);
    Q_ASSERT(changedFilter);
    Q_ASSERT(action);
    if (!changedFilter || !action)
        return;
    action->setEnabled(changedFilter->isEnabled());
}

void FindPlugin::openFindFilter()
{
    QAction *action = qobject_cast<QAction*>(sender());
    Q_ASSERT(action);
    if (!action)
        return;
    IFindFilter *filter = action->data().value<IFindFilter *>();
    Q_ASSERT(filter);
    Q_ASSERT(filter->isEnabled());
    if (!filter || !filter->isEnabled())
        return;
    QString currentFindString = (m_currentDocumentFind->isEnabled() ? m_currentDocumentFind->currentFindString() : "");
    if (!currentFindString.isEmpty())
        m_findDialog->setFindText(currentFindString);
    m_findDialog->open(filter);
}


void FindPlugin::setupMenu()
{
    Core::ActionManagerInterface *am = m_core->actionManager();
    Core::IActionContainer *medit = am->actionContainer(Core::Constants::M_EDIT);
    Core::IActionContainer *mfind = am->createMenu(Constants::M_FIND);
    medit->addMenu(mfind, Core::Constants::G_EDIT_FIND);
    mfind->menu()->setTitle(tr("&Find/Replace"));
    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;
    Core::ICommand *cmd;
    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);
}

void FindPlugin::setupFilterMenuItems()
{
    Core::ActionManagerInterface *am = m_core->actionManager();
    QList<IFindFilter*> findInterfaces =
        ExtensionSystem::PluginManager::instance()->getObjects<IFindFilter>();
    Core::ICommand *cmd;
    QList<int> globalcontext = QList<int>() << Core::Constants::C_GLOBAL_ID;

    Core::IActionContainer *mfind = am->actionContainer(Constants::M_FIND);
    m_filterActions.clear();
    foreach (IFindFilter *filter, findInterfaces) {
        QAction *action = new QAction(filter->name(), this);
        action->setEnabled(filter->isEnabled());
        action->setData(qVariantFromValue(filter));
        cmd = am->registerAction(action, QLatin1String("FindFilter.")+filter->name(), globalcontext);
        cmd->setDefaultKeySequence(filter->defaultShortcut());
        mfind->addAction(cmd, Constants::G_FIND_FILTERS);
        m_filterActions.insert(filter, action);
        connect(action, SIGNAL(triggered(bool)), this, SLOT(openFindFilter()));
        connect(filter, SIGNAL(changed()), this, SLOT(filterChanged()));
    }
    m_findDialog->setFindFilters(findInterfaces);
}

Core::ICore *FindPlugin::core()
{
    return m_core;
}

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()
{
    QSettings *settings = ExtensionSystem::PluginManager::instance()->getObject<Core::ICore>()->settings();
    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();
    m_findDialog->writeSettings();
}

void FindPlugin::readSettings()
{
    QSettings *settings = ExtensionSystem::PluginManager::instance()->getObject<Core::ICore>()->settings();
    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();
    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);
}

Q_EXPORT_PLUGIN(FindPlugin)