1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
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
16 ** GNU Lesser General Public License Usage
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.
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.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
34 #include "currentdocumentfind.h"
36 #include <aggregation/aggregate.h>
37 #include <coreplugin/coreconstants.h>
38 #include <utils/qtcassert.h>
40 #include <QtCore/QDebug>
41 #include <QtGui/QApplication>
42 #include <QtGui/QWidget>
46 using namespace Find::Internal;
48 CurrentDocumentFind::CurrentDocumentFind()
51 connect(qApp, SIGNAL(focusChanged(QWidget*, QWidget*)),
52 this, SLOT(updateCandidateFindFilter(QWidget*,QWidget*)));
55 void CurrentDocumentFind::removeConnections()
57 disconnect(qApp, 0, this, 0);
58 removeFindSupportConnections();
61 void CurrentDocumentFind::resetIncrementalSearch()
63 QTC_ASSERT(m_currentFind, return);
64 m_currentFind->resetIncrementalSearch();
67 void CurrentDocumentFind::clearResults()
69 QTC_ASSERT(m_currentFind, return);
70 m_currentFind->clearResults();
73 bool CurrentDocumentFind::isEnabled() const
75 return m_currentFind && (!m_currentWidget || m_currentWidget->isVisible());
78 bool CurrentDocumentFind::candidateIsEnabled() const
80 return (m_candidateFind != 0);
83 bool CurrentDocumentFind::supportsReplace() const
85 QTC_ASSERT(m_currentFind, return false);
86 return m_currentFind->supportsReplace();
89 Find::FindFlags CurrentDocumentFind::supportedFindFlags() const
91 QTC_ASSERT(m_currentFind, return 0);
92 return m_currentFind->supportedFindFlags();
95 QString CurrentDocumentFind::currentFindString() const
97 QTC_ASSERT(m_currentFind, return QString());
98 return m_currentFind->currentFindString();
101 QString CurrentDocumentFind::completedFindString() const
103 QTC_ASSERT(m_currentFind, return QString());
104 return m_currentFind->completedFindString();
107 void CurrentDocumentFind::highlightAll(const QString &txt, Find::FindFlags findFlags)
109 QTC_ASSERT(m_currentFind, return);
110 m_currentFind->highlightAll(txt, findFlags);
113 IFindSupport::Result CurrentDocumentFind::findIncremental(const QString &txt, Find::FindFlags findFlags)
115 QTC_ASSERT(m_currentFind, return IFindSupport::NotFound);
116 return m_currentFind->findIncremental(txt, findFlags);
119 IFindSupport::Result CurrentDocumentFind::findStep(const QString &txt, Find::FindFlags findFlags)
121 QTC_ASSERT(m_currentFind, return IFindSupport::NotFound);
122 return m_currentFind->findStep(txt, findFlags);
125 void CurrentDocumentFind::replace(const QString &before, const QString &after,
126 Find::FindFlags findFlags)
128 QTC_ASSERT(m_currentFind, return);
129 m_currentFind->replace(before, after, findFlags);
132 bool CurrentDocumentFind::replaceStep(const QString &before, const QString &after,
133 Find::FindFlags findFlags)
135 QTC_ASSERT(m_currentFind, return false);
136 return m_currentFind->replaceStep(before, after, findFlags);
139 int CurrentDocumentFind::replaceAll(const QString &before, const QString &after,
140 Find::FindFlags findFlags)
142 QTC_ASSERT(m_currentFind, return 0);
143 return m_currentFind->replaceAll(before, after, findFlags);
146 void CurrentDocumentFind::defineFindScope()
148 QTC_ASSERT(m_currentFind, return);
149 m_currentFind->defineFindScope();
152 void CurrentDocumentFind::clearFindScope()
154 QTC_ASSERT(m_currentFind, return);
155 m_currentFind->clearFindScope();
158 void CurrentDocumentFind::updateCandidateFindFilter(QWidget *old, QWidget *now)
161 QWidget *candidate = now;
162 QPointer<IFindSupport> impl = 0;
163 while (!impl && candidate) {
164 impl = Aggregation::query<IFindSupport>(candidate);
166 candidate = candidate->parentWidget();
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();
179 void CurrentDocumentFind::acceptCandidate()
181 if (!m_candidateFind || m_candidateFind == m_currentFind)
183 removeFindSupportConnections();
185 m_currentFind->highlightAll(QString(), 0);
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()));
194 m_currentFind = m_candidateFind;
196 connect(m_currentFind, SIGNAL(changed()), this, SIGNAL(changed()));
197 connect(m_currentFind, SIGNAL(destroyed(QObject*)), SLOT(clearFindSupport()));
200 m_currentWidget->installEventFilter(this);
204 void CurrentDocumentFind::removeFindSupportConnections()
207 disconnect(m_currentFind, SIGNAL(changed()), this, SIGNAL(changed()));
208 disconnect(m_currentFind, SIGNAL(destroyed(QObject*)), this, SLOT(clearFindSupport()));
211 m_currentWidget->removeEventFilter(this);
214 void CurrentDocumentFind::clearFindSupport()
216 removeFindSupportConnections();
222 bool CurrentDocumentFind::setFocusToCurrentFindSupport()
224 if (m_currentFind && m_currentWidget) {
225 m_currentWidget->setFocus();
231 bool CurrentDocumentFind::eventFilter(QObject *obj, QEvent *event)
233 if (m_currentWidget && obj == m_currentWidget) {
234 if (event->type() == QEvent::Hide || event->type() == QEvent::Show) {
238 return QObject::eventFilter(obj, event);
241 void CurrentDocumentFind::aggregationChanged()
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
248 m_candidateWidget = m_currentWidget;
249 m_candidateFind = currentFind;
259 void CurrentDocumentFind::candidateAggregationChanged()
261 if (m_candidateWidget && m_candidateWidget != m_currentWidget) {
262 m_candidateFind = Aggregation::query<IFindSupport>(m_candidateWidget);
263 emit candidateChanged();