OSDN Git Service

It's 2011 now.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / find / currentdocumentfind.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights.  These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include "currentdocumentfind.h"
35
36 #include <aggregation/aggregate.h>
37 #include <coreplugin/coreconstants.h>
38 #include <utils/qtcassert.h>
39
40 #include <QtCore/QDebug>
41 #include <QtGui/QApplication>
42 #include <QtGui/QWidget>
43
44 using namespace Core;
45 using namespace Find;
46 using namespace Find::Internal;
47
48 CurrentDocumentFind::CurrentDocumentFind()
49   : m_currentFind(0)
50 {
51     connect(qApp, SIGNAL(focusChanged(QWidget*, QWidget*)),
52             this, SLOT(updateCandidateFindFilter(QWidget*,QWidget*)));
53 }
54
55 void CurrentDocumentFind::removeConnections()
56 {
57     disconnect(qApp, 0, this, 0);
58     removeFindSupportConnections();
59 }
60
61 void CurrentDocumentFind::resetIncrementalSearch()
62 {
63     QTC_ASSERT(m_currentFind, return);
64     m_currentFind->resetIncrementalSearch();
65 }
66
67 void CurrentDocumentFind::clearResults()
68 {
69     QTC_ASSERT(m_currentFind, return);
70     m_currentFind->clearResults();
71 }
72
73 bool CurrentDocumentFind::isEnabled() const
74 {
75     return m_currentFind && (!m_currentWidget || m_currentWidget->isVisible());
76 }
77
78 bool CurrentDocumentFind::candidateIsEnabled() const
79 {
80     return (m_candidateFind != 0);
81 }
82
83 bool CurrentDocumentFind::supportsReplace() const
84 {
85     QTC_ASSERT(m_currentFind, return false);
86     return m_currentFind->supportsReplace();
87 }
88
89 Find::FindFlags CurrentDocumentFind::supportedFindFlags() const
90 {
91     QTC_ASSERT(m_currentFind, return 0);
92     return m_currentFind->supportedFindFlags();
93 }
94
95 QString CurrentDocumentFind::currentFindString() const
96 {
97     QTC_ASSERT(m_currentFind, return QString());
98     return m_currentFind->currentFindString();
99 }
100
101 QString CurrentDocumentFind::completedFindString() const
102 {
103     QTC_ASSERT(m_currentFind, return QString());
104     return m_currentFind->completedFindString();
105 }
106
107 void CurrentDocumentFind::highlightAll(const QString &txt, Find::FindFlags findFlags)
108 {
109     QTC_ASSERT(m_currentFind, return);
110     m_currentFind->highlightAll(txt, findFlags);
111 }
112
113 IFindSupport::Result CurrentDocumentFind::findIncremental(const QString &txt, Find::FindFlags findFlags)
114 {
115     QTC_ASSERT(m_currentFind, return IFindSupport::NotFound);
116     return m_currentFind->findIncremental(txt, findFlags);
117 }
118
119 IFindSupport::Result CurrentDocumentFind::findStep(const QString &txt, Find::FindFlags findFlags)
120 {
121     QTC_ASSERT(m_currentFind, return IFindSupport::NotFound);
122     return m_currentFind->findStep(txt, findFlags);
123 }
124
125 void CurrentDocumentFind::replace(const QString &before, const QString &after,
126     Find::FindFlags findFlags)
127 {
128     QTC_ASSERT(m_currentFind, return);
129     m_currentFind->replace(before, after, findFlags);
130 }
131
132 bool CurrentDocumentFind::replaceStep(const QString &before, const QString &after,
133     Find::FindFlags findFlags)
134 {
135     QTC_ASSERT(m_currentFind, return false);
136     return m_currentFind->replaceStep(before, after, findFlags);
137 }
138
139 int CurrentDocumentFind::replaceAll(const QString &before, const QString &after,
140     Find::FindFlags findFlags)
141 {
142     QTC_ASSERT(m_currentFind, return 0);
143     return m_currentFind->replaceAll(before, after, findFlags);
144 }
145
146 void CurrentDocumentFind::defineFindScope()
147 {
148     QTC_ASSERT(m_currentFind, return);
149     m_currentFind->defineFindScope();
150 }
151
152 void CurrentDocumentFind::clearFindScope()
153 {
154     QTC_ASSERT(m_currentFind, return);
155     m_currentFind->clearFindScope();
156 }
157
158 void CurrentDocumentFind::updateCandidateFindFilter(QWidget *old, QWidget *now)
159 {
160     Q_UNUSED(old)
161     QWidget *candidate = now;
162     QPointer<IFindSupport> impl = 0;
163     while (!impl && candidate) {
164         impl = Aggregation::query<IFindSupport>(candidate);
165         if (!impl)
166             candidate = candidate->parentWidget();
167     }
168     if (m_candidateWidget)
169         disconnect(Aggregation::Aggregate::parentAggregate(m_candidateWidget), SIGNAL(changed()),
170                    this, SLOT(candidateAggregationChanged()));
171     m_candidateWidget = candidate;
172     m_candidateFind = impl;
173     if (m_candidateWidget)
174         connect(Aggregation::Aggregate::parentAggregate(m_candidateWidget), SIGNAL(changed()),
175                 this, SLOT(candidateAggregationChanged()));
176     emit candidateChanged();
177 }
178
179 void CurrentDocumentFind::acceptCandidate()
180 {
181     if (!m_candidateFind || m_candidateFind == m_currentFind)
182         return;
183     removeFindSupportConnections();
184     if (m_currentFind)
185         m_currentFind->highlightAll(QString(), 0);
186
187     if (m_currentWidget)
188         disconnect(Aggregation::Aggregate::parentAggregate(m_currentWidget), SIGNAL(changed()),
189                    this, SLOT(aggregationChanged()));
190     m_currentWidget = m_candidateWidget;
191     connect(Aggregation::Aggregate::parentAggregate(m_currentWidget), SIGNAL(changed()),
192             this, SLOT(aggregationChanged()));
193
194     m_currentFind = m_candidateFind;
195     if (m_currentFind) {
196         connect(m_currentFind, SIGNAL(changed()), this, SIGNAL(changed()));
197         connect(m_currentFind, SIGNAL(destroyed(QObject*)), SLOT(clearFindSupport()));
198     }
199     if (m_currentWidget)
200         m_currentWidget->installEventFilter(this);
201     emit changed();
202 }
203
204 void CurrentDocumentFind::removeFindSupportConnections()
205 {
206     if (m_currentFind) {
207         disconnect(m_currentFind, SIGNAL(changed()), this, SIGNAL(changed()));
208         disconnect(m_currentFind, SIGNAL(destroyed(QObject*)), this, SLOT(clearFindSupport()));
209     }
210     if (m_currentWidget)
211         m_currentWidget->removeEventFilter(this);
212 }
213
214 void CurrentDocumentFind::clearFindSupport()
215 {
216     removeFindSupportConnections();
217     m_currentWidget = 0;
218     m_currentFind = 0;
219     emit changed();
220 }
221
222 bool CurrentDocumentFind::setFocusToCurrentFindSupport()
223 {
224     if (m_currentFind && m_currentWidget) {
225         m_currentWidget->setFocus();
226         return true;
227     }
228     return false;
229 }
230
231 bool CurrentDocumentFind::eventFilter(QObject *obj, QEvent *event)
232 {
233     if (m_currentWidget && obj == m_currentWidget) {
234         if (event->type() == QEvent::Hide || event->type() == QEvent::Show) {
235             emit changed();
236         }
237     }
238     return QObject::eventFilter(obj, event);
239 }
240
241 void CurrentDocumentFind::aggregationChanged()
242 {
243     if (m_currentWidget) {
244         QPointer<IFindSupport> currentFind = Aggregation::query<IFindSupport>(m_currentWidget);
245         if (currentFind != m_currentFind) {
246             // There's a change in the find support
247             if (currentFind) {
248                 m_candidateWidget = m_currentWidget;
249                 m_candidateFind = currentFind;
250                 acceptCandidate();
251             }
252             else {
253                 clearFindSupport();
254             }
255         }
256     }
257 }
258
259 void CurrentDocumentFind::candidateAggregationChanged()
260 {
261     if (m_candidateWidget && m_candidateWidget != m_currentWidget) {
262         m_candidateFind = Aggregation::query<IFindSupport>(m_candidateWidget);
263         emit candidateChanged();
264     }
265 }