findtoolwindow.cpp 9.78 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
con's avatar
con committed
2
**
3
4
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
con's avatar
con committed
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
con's avatar
con committed
7
**
hjk's avatar
hjk committed
8
9
10
11
** 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
12
13
14
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
15
**
16
17
18
19
20
21
22
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
con's avatar
con committed
23
**
hjk's avatar
hjk committed
24
****************************************************************************/
hjk's avatar
hjk committed
25

con's avatar
con committed
26
#include "findtoolwindow.h"
27
#include "ifindfilter.h"
con's avatar
con committed
28
29
30
#include "findplugin.h"

#include <coreplugin/icore.h>
31
#include <utils/qtcassert.h>
32
#include <utils/algorithm.h>
con's avatar
con committed
33

34
35
36
37
38
#include <QSettings>
#include <QStringListModel>
#include <QCompleter>
#include <QKeyEvent>
#include <QScrollArea>
con's avatar
con committed
39

40
41
using namespace Core;
using namespace Core::Internal;
con's avatar
con committed
42

43
44
static FindToolWindow *m_instance = 0;

45
FindToolWindow::FindToolWindow(QWidget *parent)
46
    : QWidget(parent),
47
    m_findCompleter(new QCompleter(this)),
48
49
    m_currentFilter(0),
    m_configWidget(0)
con's avatar
con committed
50
{
51
    m_instance = this;
con's avatar
con committed
52
    m_ui.setupUi(this);
Montel Laurent's avatar
Montel Laurent committed
53
    m_ui.searchTerm->setFiltering(true);
54
    m_ui.searchTerm->setPlaceholderText(QString());
55
56
    setFocusProxy(m_ui.searchTerm);

Orgad Shaneh's avatar
Orgad Shaneh committed
57
58
    connect(m_ui.searchButton, &QAbstractButton::clicked, this, &FindToolWindow::search);
    connect(m_ui.replaceButton, &QAbstractButton::clicked, this, &FindToolWindow::replace);
59
60
61
    connect(m_ui.matchCase, &QAbstractButton::toggled, Find::instance(), &Find::setCaseSensitive);
    connect(m_ui.wholeWords, &QAbstractButton::toggled, Find::instance(), &Find::setWholeWord);
    connect(m_ui.regExp, &QAbstractButton::toggled, Find::instance(), &Find::setRegularExpression);
Orgad Shaneh's avatar
Orgad Shaneh committed
62
63
64
    connect(m_ui.filterList, static_cast<void (QComboBox::*)(int)>(&QComboBox::activated),
            this, static_cast<void (FindToolWindow::*)(int)>(&FindToolWindow::setCurrentFilter));
    connect(m_ui.searchTerm, &QLineEdit::textChanged, this, &FindToolWindow::updateButtonStates);
dt's avatar
dt committed
65

66
    m_findCompleter->setModel(Find::findCompletionModel());
67
    m_ui.searchTerm->setSpecialCompleter(m_findCompleter);
68
    m_ui.searchTerm->installEventFilter(this);
con's avatar
con committed
69
70
71
72
    QVBoxLayout *layout = new QVBoxLayout;
    layout->setMargin(0);
    layout->setSpacing(0);
    m_ui.configWidget->setLayout(layout);
con's avatar
con committed
73
    updateButtonStates();
74

75
    connect(Find::instance(), &Find::findFlagsChanged, this, &FindToolWindow::updateFindFlags);
con's avatar
con committed
76
77
78
79
80
81
82
}

FindToolWindow::~FindToolWindow()
{
    qDeleteAll(m_configWidgets);
}

83
84
85
86
87
FindToolWindow *FindToolWindow::instance()
{
    return m_instance;
}

88
89
90
91
92
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)
93
                && (ke->modifiers() == Qt::NoModifier || ke->modifiers() == Qt::KeypadModifier)) {
94
            ke->accept();
95
96
            if (m_ui.searchButton->isEnabled())
                search();
97
98
99
100
101
102
            return true;
        }
    }
    return QWidget::event(event);
}

103
104
105
106
107
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) {
108
109
            if (m_ui.searchTerm->text().isEmpty())
                m_findCompleter->setCompletionPrefix(QString());
110
111
112
            m_findCompleter->complete();
        }
    }
113
    return QWidget::eventFilter(obj, event);
114
115
}

con's avatar
con committed
116
117
void FindToolWindow::updateButtonStates()
{
118
    bool filterEnabled = m_currentFilter && m_currentFilter->isEnabled();
119
    bool enabled = !m_ui.searchTerm->text().isEmpty() && filterEnabled && m_currentFilter->isValid();
con's avatar
con committed
120
121
122
    m_ui.searchButton->setEnabled(enabled);
    m_ui.replaceButton->setEnabled(m_currentFilter
                                   && m_currentFilter->isReplaceSupported() && enabled);
123
124
125
126
    if (m_configWidget)
        m_configWidget->setEnabled(filterEnabled);

    m_ui.matchCase->setEnabled(filterEnabled
127
                               && (m_currentFilter->supportedFindFlags() & FindCaseSensitively));
128
    m_ui.wholeWords->setEnabled(filterEnabled
129
                                && (m_currentFilter->supportedFindFlags() & FindWholeWords));
130
    m_ui.regExp->setEnabled(filterEnabled
131
                            && (m_currentFilter->supportedFindFlags() & FindRegularExpression));
132
    m_ui.searchTerm->setEnabled(filterEnabled);
con's avatar
con committed
133
134
}

135
136
void FindToolWindow::updateFindFlags()
{
137
138
139
    m_ui.matchCase->setChecked(Find::hasFindFlag(FindCaseSensitively));
    m_ui.wholeWords->setChecked(Find::hasFindFlag(FindWholeWords));
    m_ui.regExp->setChecked(Find::hasFindFlag(FindRegularExpression));
140
141
142
}


con's avatar
con committed
143
144
145
146
void FindToolWindow::setFindFilters(const QList<IFindFilter *> &filters)
{
    qDeleteAll(m_configWidgets);
    m_configWidgets.clear();
147
148
    foreach (IFindFilter *filter, m_filters)
        filter->disconnect(this);
con's avatar
con committed
149
150
151
152
    m_filters = filters;
    m_ui.filterList->clear();
    QStringList names;
    foreach (IFindFilter *filter, filters) {
153
        names << filter->displayName();
con's avatar
con committed
154
        m_configWidgets.append(filter->createConfigWidget());
155
156
        connect(filter, &IFindFilter::displayNameChanged,
                this, [this, filter]() { updateFindFilterName(filter); });
con's avatar
con committed
157
158
    }
    m_ui.filterList->addItems(names);
159
160
    if (m_filters.size() > 0)
        setCurrentFilter(0);
con's avatar
con committed
161
162
}

163
QList<IFindFilter *> FindToolWindow::findFilters() const
164
{
165
166
167
168
169
170
171
172
    return m_filters;
}

void FindToolWindow::updateFindFilterName(IFindFilter *filter)
{
    int index = m_filters.indexOf(filter);
    if (QTC_GUARD(index >= 0))
        m_ui.filterList->setItemText(index, filter->displayName());
173
174
}

con's avatar
con committed
175
176
177
178
179
void FindToolWindow::setFindText(const QString &text)
{
    m_ui.searchTerm->setText(text);
}

180
void FindToolWindow::setCurrentFilter(IFindFilter *filter)
con's avatar
con committed
181
{
182
183
    if (!filter)
        filter = m_currentFilter;
con's avatar
con committed
184
    int index = m_filters.indexOf(filter);
185
    if (index >= 0)
186
        setCurrentFilter(index);
187
    updateFindFlags();
con's avatar
con committed
188
189
190
191
192
193
    m_ui.searchTerm->setFocus();
    m_ui.searchTerm->selectAll();
}

void FindToolWindow::setCurrentFilter(int index)
{
194
    m_ui.filterList->setCurrentIndex(index);
con's avatar
con committed
195
196
    for (int i = 0; i < m_configWidgets.size(); ++i) {
        QWidget *configWidget = m_configWidgets.at(i);
197
        if (i == index) {
198
            m_configWidget = configWidget;
199
            if (m_currentFilter) {
Orgad Shaneh's avatar
Orgad Shaneh committed
200
201
                disconnect(m_currentFilter, &IFindFilter::enabledChanged,
                           this, &FindToolWindow::updateButtonStates);
202
203
204
                disconnect(m_currentFilter, &IFindFilter::validChanged,
                           this, &FindToolWindow::updateButtonStates);
            }
con's avatar
con committed
205
            m_currentFilter = m_filters.at(i);
Orgad Shaneh's avatar
Orgad Shaneh committed
206
207
            connect(m_currentFilter, &IFindFilter::enabledChanged,
                    this, &FindToolWindow::updateButtonStates);
208
209
            connect(m_currentFilter, &IFindFilter::validChanged,
                    this, &FindToolWindow::updateButtonStates);
con's avatar
con committed
210
            updateButtonStates();
211
            if (m_configWidget)
212
                m_ui.configWidget->layout()->addWidget(m_configWidget);
213
        } else {
con's avatar
con committed
214
215
            if (configWidget)
                configWidget->setParent(0);
216
        }
con's avatar
con committed
217
    }
218
219
220
221
222
223
224
225
226
227
228
229
230
    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();
    }
con's avatar
con committed
231
232
}

233
void FindToolWindow::acceptAndGetParameters(QString *term, IFindFilter **filter)
con's avatar
con committed
234
{
235
236
    if (filter)
        *filter = 0;
237
    Find::updateFindCompletion(m_ui.searchTerm->text());
con's avatar
con committed
238
    int index = m_ui.filterList->currentIndex();
239
240
241
242
    QString searchTerm = m_ui.searchTerm->text();
    if (term)
        *term = searchTerm;
    if (searchTerm.isEmpty() || index < 0)
con's avatar
con committed
243
        return;
244
245
246
247
248
249
250
    if (filter)
        *filter = m_filters.at(index);
}

void FindToolWindow::search()
{
    QString term;
251
    IFindFilter *filter = 0;
252
    acceptAndGetParameters(&term, &filter);
253
    QTC_ASSERT(filter, return);
254
    filter->findAll(term, Find::findFlags());
con's avatar
con committed
255
256
}

257
258
259
void FindToolWindow::replace()
{
    QString term;
260
    IFindFilter *filter = 0;
261
    acceptAndGetParameters(&term, &filter);
262
    QTC_ASSERT(filter, return);
263
    filter->replaceAll(term, Find::findFlags());
264
265
}

con's avatar
con committed
266
267
void FindToolWindow::writeSettings()
{
Orgad Shaneh's avatar
Orgad Shaneh committed
268
    QSettings *settings = ICore::settings();
269
    settings->beginGroup(QLatin1String("Find"));
hjk's avatar
hjk committed
270
    settings->setValue(QLatin1String("CurrentFilter"), m_currentFilter ?  m_currentFilter->id() : QString());
con's avatar
con committed
271
272
273
274
275
276
277
    foreach (IFindFilter *filter, m_filters)
        filter->writeSettings(settings);
    settings->endGroup();
}

void FindToolWindow::readSettings()
{
Orgad Shaneh's avatar
Orgad Shaneh committed
278
    QSettings *settings = ICore::settings();
279
280
    settings->beginGroup(QLatin1String("Find"));
    const QString currentFilter = settings->value(QLatin1String("CurrentFilter")).toString();
281
282
    for (int i = 0; i < m_filters.size(); ++i) {
        IFindFilter *filter = m_filters.at(i);
con's avatar
con committed
283
        filter->readSettings(settings);
284
        if (filter->id() == currentFilter)
285
286
            setCurrentFilter(i);
    }
con's avatar
con committed
287
288
    settings->endGroup();
}