currentdocumentfind.cpp 8.51 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
con's avatar
con committed
2
**
Eike Ziller's avatar
Eike Ziller committed
3
4
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://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
Eike Ziller's avatar
Eike Ziller committed
12
13
** a written agreement between you and The Qt Company.  For licensing terms and
** conditions see http://www.qt.io/terms-conditions.  For further information
Eike Ziller's avatar
Eike Ziller committed
14
** use the contact form at http://www.qt.io/contact-us.
15
**
16
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17
** Alternatively, this file may be used under the terms of the GNU Lesser
Eike Ziller's avatar
Eike Ziller committed
18
19
20
21
22
23
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file.  Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
hjk's avatar
hjk committed
24
**
Eike Ziller's avatar
Eike Ziller committed
25
26
** In addition, as a special exception, The Qt Company gives you certain additional
** rights.  These rights are described in The Qt Company LGPL Exception
con's avatar
con committed
27
28
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
29
****************************************************************************/
hjk's avatar
hjk committed
30

con's avatar
con committed
31
#include "currentdocumentfind.h"
hjk's avatar
hjk committed
32

con's avatar
con committed
33
34
#include <aggregation/aggregate.h>
#include <coreplugin/coreconstants.h>
35
36

#include <utils/fadingindicator.h>
hjk's avatar
hjk committed
37
#include <utils/qtcassert.h>
con's avatar
con committed
38

39
40
41
#include <QDebug>
#include <QApplication>
#include <QWidget>
con's avatar
con committed
42
43

using namespace Core;
44
45
using namespace Core;
using namespace Core::Internal;
con's avatar
con committed
46

47
48
CurrentDocumentFind::CurrentDocumentFind()
  : m_currentFind(0)
con's avatar
con committed
49
{
Montel Laurent's avatar
Montel Laurent committed
50
51
    connect(qApp, &QApplication::focusChanged,
            this, &CurrentDocumentFind::updateCandidateFindFilter);
con's avatar
con committed
52
53
54
55
56
57
58
59
60
61
}

void CurrentDocumentFind::removeConnections()
{
    disconnect(qApp, 0, this, 0);
    removeFindSupportConnections();
}

void CurrentDocumentFind::resetIncrementalSearch()
{
hjk's avatar
hjk committed
62
63
    QTC_ASSERT(m_currentFind, return);
    m_currentFind->resetIncrementalSearch();
con's avatar
con committed
64
65
}

66
void CurrentDocumentFind::clearHighlights()
con's avatar
con committed
67
{
hjk's avatar
hjk committed
68
    QTC_ASSERT(m_currentFind, return);
69
    m_currentFind->clearHighlights();
con's avatar
con committed
70
71
72
73
74
75
76
}

bool CurrentDocumentFind::isEnabled() const
{
    return m_currentFind && (!m_currentWidget || m_currentWidget->isVisible());
}

77
IFindSupport *CurrentDocumentFind::candidate() const
78
{
79
    return m_candidateFind;
80
81
}

con's avatar
con committed
82
83
bool CurrentDocumentFind::supportsReplace() const
{
hjk's avatar
hjk committed
84
85
    QTC_ASSERT(m_currentFind, return false);
    return m_currentFind->supportsReplace();
con's avatar
con committed
86
87
}

88
FindFlags CurrentDocumentFind::supportedFindFlags() const
89
90
91
92
93
{
    QTC_ASSERT(m_currentFind, return 0);
    return m_currentFind->supportedFindFlags();
}

con's avatar
con committed
94
95
QString CurrentDocumentFind::currentFindString() const
{
hjk's avatar
hjk committed
96
97
    QTC_ASSERT(m_currentFind, return QString());
    return m_currentFind->currentFindString();
con's avatar
con committed
98
99
100
101
}

QString CurrentDocumentFind::completedFindString() const
{
hjk's avatar
hjk committed
102
103
    QTC_ASSERT(m_currentFind, return QString());
    return m_currentFind->completedFindString();
con's avatar
con committed
104
105
}

106
void CurrentDocumentFind::highlightAll(const QString &txt, FindFlags findFlags)
con's avatar
con committed
107
{
hjk's avatar
hjk committed
108
109
    QTC_ASSERT(m_currentFind, return);
    m_currentFind->highlightAll(txt, findFlags);
con's avatar
con committed
110
111
}

112
IFindSupport::Result CurrentDocumentFind::findIncremental(const QString &txt, FindFlags findFlags)
con's avatar
con committed
113
{
ck's avatar
ck committed
114
    QTC_ASSERT(m_currentFind, return IFindSupport::NotFound);
hjk's avatar
hjk committed
115
    return m_currentFind->findIncremental(txt, findFlags);
con's avatar
con committed
116
117
}

118
IFindSupport::Result CurrentDocumentFind::findStep(const QString &txt, FindFlags findFlags)
con's avatar
con committed
119
{
ck's avatar
ck committed
120
    QTC_ASSERT(m_currentFind, return IFindSupport::NotFound);
hjk's avatar
hjk committed
121
    return m_currentFind->findStep(txt, findFlags);
con's avatar
con committed
122
123
}

124
void CurrentDocumentFind::replace(const QString &before, const QString &after, FindFlags findFlags)
125
126
127
128
129
{
    QTC_ASSERT(m_currentFind, return);
    m_currentFind->replace(before, after, findFlags);
}

130
bool CurrentDocumentFind::replaceStep(const QString &before, const QString &after, FindFlags findFlags)
con's avatar
con committed
131
{
hjk's avatar
hjk committed
132
133
    QTC_ASSERT(m_currentFind, return false);
    return m_currentFind->replaceStep(before, after, findFlags);
con's avatar
con committed
134
135
}

136
int CurrentDocumentFind::replaceAll(const QString &before, const QString &after, FindFlags findFlags)
con's avatar
con committed
137
{
hjk's avatar
hjk committed
138
    QTC_ASSERT(m_currentFind, return 0);
139
140
141
142
143
144
    QTC_CHECK(m_currentWidget);
    int count = m_currentFind->replaceAll(before, after, findFlags);
    Utils::FadingIndicator::showText(m_currentWidget,
                                     tr("%1 occurrences replaced.").arg(count),
                                     Utils::FadingIndicator::SmallText);
    return count;
con's avatar
con committed
145
146
147
148
}

void CurrentDocumentFind::defineFindScope()
{
hjk's avatar
hjk committed
149
150
    QTC_ASSERT(m_currentFind, return);
    m_currentFind->defineFindScope();
con's avatar
con committed
151
152
153
154
}

void CurrentDocumentFind::clearFindScope()
{
hjk's avatar
hjk committed
155
156
    QTC_ASSERT(m_currentFind, return);
    m_currentFind->clearFindScope();
con's avatar
con committed
157
158
}

159
void CurrentDocumentFind::updateCandidateFindFilter(QWidget *old, QWidget *now)
con's avatar
con committed
160
{
161
    Q_UNUSED(old)
con's avatar
con committed
162
163
164
165
166
167
168
    QWidget *candidate = now;
    QPointer<IFindSupport> impl = 0;
    while (!impl && candidate) {
        impl = Aggregation::query<IFindSupport>(candidate);
        if (!impl)
            candidate = candidate->parentWidget();
    }
Eike Ziller's avatar
Eike Ziller committed
169
170
    if (candidate == m_candidateWidget && impl == m_candidateFind)
        return;
171
172
173
    if (m_candidateWidget)
        disconnect(Aggregation::Aggregate::parentAggregate(m_candidateWidget), SIGNAL(changed()),
                   this, SLOT(candidateAggregationChanged()));
174
175
    m_candidateWidget = candidate;
    m_candidateFind = impl;
176
177
178
    if (m_candidateWidget)
        connect(Aggregation::Aggregate::parentAggregate(m_candidateWidget), SIGNAL(changed()),
                this, SLOT(candidateAggregationChanged()));
179
180
181
182
183
184
    emit candidateChanged();
}

void CurrentDocumentFind::acceptCandidate()
{
    if (!m_candidateFind || m_candidateFind == m_currentFind)
con's avatar
con committed
185
186
        return;
    removeFindSupportConnections();
187
    if (m_currentFind)
188
        m_currentFind->clearHighlights();
189
190
191
192

    if (m_currentWidget)
        disconnect(Aggregation::Aggregate::parentAggregate(m_currentWidget), SIGNAL(changed()),
                   this, SLOT(aggregationChanged()));
193
    m_currentWidget = m_candidateWidget;
194
195
196
    connect(Aggregation::Aggregate::parentAggregate(m_currentWidget), SIGNAL(changed()),
            this, SLOT(aggregationChanged()));

197
    m_currentFind = m_candidateFind;
con's avatar
con committed
198
    if (m_currentFind) {
Montel Laurent's avatar
Montel Laurent committed
199
200
        connect(m_currentFind.data(), &IFindSupport::changed,
                this, &CurrentDocumentFind::changed);
201
        connect(m_currentFind, SIGNAL(destroyed(QObject*)), SLOT(clearFindSupport()));
con's avatar
con committed
202
203
204
205
206
207
208
209
210
    }
    if (m_currentWidget)
        m_currentWidget->installEventFilter(this);
    emit changed();
}

void CurrentDocumentFind::removeFindSupportConnections()
{
    if (m_currentFind) {
Montel Laurent's avatar
Montel Laurent committed
211
212
213
214
        disconnect(m_currentFind.data(), &IFindSupport::changed,
                   this, &CurrentDocumentFind::changed);
        disconnect(m_currentFind.data(), &IFindSupport::destroyed,
                   this, &CurrentDocumentFind::clearFindSupport);
con's avatar
con committed
215
216
217
218
219
    }
    if (m_currentWidget)
        m_currentWidget->removeEventFilter(this);
}

220
void CurrentDocumentFind::clearFindSupport()
con's avatar
con committed
221
222
223
224
225
226
227
228
229
230
{
    removeFindSupportConnections();
    m_currentWidget = 0;
    m_currentFind = 0;
    emit changed();
}

bool CurrentDocumentFind::setFocusToCurrentFindSupport()
{
    if (m_currentFind && m_currentWidget) {
Eike Ziller's avatar
Eike Ziller committed
231
232
233
234
        QWidget *w = m_currentWidget->focusWidget();
        if (!w)
            w = m_currentWidget;
        w->setFocus();
con's avatar
con committed
235
236
237
238
239
240
241
242
        return true;
    }
    return false;
}

bool CurrentDocumentFind::eventFilter(QObject *obj, QEvent *event)
{
    if (m_currentWidget && obj == m_currentWidget) {
243
        if (event->type() == QEvent::Hide || event->type() == QEvent::Show)
con's avatar
con committed
244
245
246
247
            emit changed();
    }
    return QObject::eventFilter(obj, event);
}
248
249
250
251
252
253
254
255
256
257
258

void CurrentDocumentFind::aggregationChanged()
{
    if (m_currentWidget) {
        QPointer<IFindSupport> currentFind = Aggregation::query<IFindSupport>(m_currentWidget);
        if (currentFind != m_currentFind) {
            // There's a change in the find support
            if (currentFind) {
                m_candidateWidget = m_currentWidget;
                m_candidateFind = currentFind;
                acceptCandidate();
259
            } else {
260
261
262
263
264
265
266
267
                clearFindSupport();
            }
        }
    }
}

void CurrentDocumentFind::candidateAggregationChanged()
{
268
    if (m_candidateWidget && m_candidateWidget != m_currentWidget) {
269
270
271
272
        m_candidateFind = Aggregation::query<IFindSupport>(m_candidateWidget);
        emit candidateChanged();
    }
}