| 
									
										
										
										
											2012-10-02 09:12:39 +02:00
										 |  |  | /****************************************************************************
 | 
					
						
							| 
									
										
										
										
											2009-05-29 14:14:08 +02:00
										 |  |  | ** | 
					
						
							| 
									
										
										
										
											2013-01-28 17:12:19 +01:00
										 |  |  | ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). | 
					
						
							| 
									
										
										
										
											2012-10-02 09:12:39 +02:00
										 |  |  | ** Contact: http://www.qt-project.org/legal
 | 
					
						
							| 
									
										
										
										
											2009-05-29 14:14:08 +02:00
										 |  |  | ** | 
					
						
							| 
									
										
										
										
											2012-10-02 09:12:39 +02:00
										 |  |  | ** This file is part of Qt Creator. | 
					
						
							| 
									
										
										
										
											2009-05-29 14:14:08 +02:00
										 |  |  | ** | 
					
						
							| 
									
										
										
										
											2012-10-02 09:12:39 +02:00
										 |  |  | ** 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 | 
					
						
							|  |  |  | ** a written agreement between you and Digia.  For licensing terms and | 
					
						
							|  |  |  | ** conditions see http://qt.digia.com/licensing.  For further information
 | 
					
						
							|  |  |  | ** use the contact form at http://qt.digia.com/contact-us.
 | 
					
						
							| 
									
										
										
										
											2009-05-29 14:14:08 +02:00
										 |  |  | ** | 
					
						
							|  |  |  | ** GNU Lesser General Public License Usage | 
					
						
							| 
									
										
										
										
											2012-10-02 09:12:39 +02:00
										 |  |  | ** Alternatively, this file may be used under the terms of the GNU Lesser | 
					
						
							|  |  |  | ** General Public License version 2.1 as published by the Free Software | 
					
						
							|  |  |  | ** Foundation and appearing in the file LICENSE.LGPL included in the | 
					
						
							|  |  |  | ** packaging of this file.  Please review the following information to | 
					
						
							|  |  |  | ** ensure the GNU Lesser General Public License version 2.1 requirements | 
					
						
							|  |  |  | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
 | 
					
						
							|  |  |  | ** | 
					
						
							|  |  |  | ** In addition, as a special exception, Digia gives you certain additional | 
					
						
							|  |  |  | ** rights.  These rights are described in the Digia Qt LGPL Exception | 
					
						
							| 
									
										
										
										
											2010-12-17 16:01:08 +01:00
										 |  |  | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | 
					
						
							|  |  |  | ** | 
					
						
							| 
									
										
										
										
											2012-10-02 09:12:39 +02:00
										 |  |  | ****************************************************************************/ | 
					
						
							| 
									
										
										
										
											2009-05-29 14:14:08 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include "findincurrentfile.h"
 | 
					
						
							| 
									
										
										
										
											2010-10-11 11:34:17 +02:00
										 |  |  | #include "itexteditor.h"
 | 
					
						
							| 
									
										
										
										
											2009-05-29 14:14:08 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include <coreplugin/icore.h>
 | 
					
						
							|  |  |  | #include <coreplugin/editormanager/editormanager.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-02-15 10:42:41 +01:00
										 |  |  | #include <QSettings>
 | 
					
						
							| 
									
										
										
										
											2009-05-29 14:14:08 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | using namespace Find; | 
					
						
							| 
									
										
										
										
											2010-10-11 11:34:17 +02:00
										 |  |  | using namespace TextEditor; | 
					
						
							| 
									
										
										
										
											2009-05-29 14:14:08 +02:00
										 |  |  | using namespace TextEditor::Internal; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-02 11:51:31 +02:00
										 |  |  | FindInCurrentFile::FindInCurrentFile() | 
					
						
							| 
									
										
										
										
											2012-02-14 16:43:51 +01:00
										 |  |  |   : m_currentDocument(0) | 
					
						
							| 
									
										
										
										
											2009-05-29 14:14:08 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2012-01-24 15:36:40 +01:00
										 |  |  |     connect(Core::ICore::editorManager(), SIGNAL(currentEditorChanged(Core::IEditor*)), | 
					
						
							| 
									
										
										
										
											2009-08-25 12:30:47 +02:00
										 |  |  |             this, SLOT(handleFileChange(Core::IEditor*))); | 
					
						
							| 
									
										
										
										
											2012-05-08 09:43:14 +02:00
										 |  |  |     handleFileChange(Core::EditorManager::currentEditor()); | 
					
						
							| 
									
										
										
										
											2009-05-29 14:14:08 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | QString FindInCurrentFile::id() const | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2012-01-05 11:05:28 +01:00
										 |  |  |     return QLatin1String("Current File"); | 
					
						
							| 
									
										
										
										
											2009-05-29 14:14:08 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-12 12:06:15 +02:00
										 |  |  | QString FindInCurrentFile::displayName() const | 
					
						
							| 
									
										
										
										
											2009-05-29 14:14:08 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     return tr("Current File"); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-13 10:50:57 +01:00
										 |  |  | Utils::FileIterator *FindInCurrentFile::files(const QStringList &nameFilters, | 
					
						
							|  |  |  |                                               const QVariant &additionalParameters) const | 
					
						
							| 
									
										
										
										
											2009-05-29 14:14:08 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-12-13 10:50:57 +01:00
										 |  |  |     Q_UNUSED(nameFilters) | 
					
						
							|  |  |  |     QString fileName = additionalParameters.toString(); | 
					
						
							| 
									
										
										
										
											2010-10-11 11:34:17 +02:00
										 |  |  |     QMap<QString, QTextCodec *> openEditorEncodings = ITextEditor::openedTextEditorsEncodings(); | 
					
						
							|  |  |  |     QTextCodec *codec = openEditorEncodings.value(fileName); | 
					
						
							|  |  |  |     if (!codec) | 
					
						
							| 
									
										
										
										
											2011-02-01 14:13:54 +01:00
										 |  |  |         codec = Core::EditorManager::instance()->defaultTextCodec(); | 
					
						
							| 
									
										
										
										
											2010-10-11 11:34:17 +02:00
										 |  |  |     return new Utils::FileIterator(QStringList() << fileName, QList<QTextCodec *>() << codec); | 
					
						
							| 
									
										
										
										
											2009-05-29 14:14:08 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-13 10:50:57 +01:00
										 |  |  | QVariant FindInCurrentFile::additionalParameters() const | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2012-02-14 16:43:51 +01:00
										 |  |  |     return qVariantFromValue(m_currentDocument->fileName()); | 
					
						
							| 
									
										
										
										
											2011-12-13 10:50:57 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-07 20:28:04 +02:00
										 |  |  | QString FindInCurrentFile::label() const | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2012-02-14 16:43:51 +01:00
										 |  |  |     return tr("File '%1':").arg(QFileInfo(m_currentDocument->fileName()).fileName()); | 
					
						
							| 
									
										
										
										
											2011-09-07 20:28:04 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | QString FindInCurrentFile::toolTip() const | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-09-16 09:28:20 +02:00
										 |  |  |     // %2 is filled by BaseFileFind::runNewSearch
 | 
					
						
							| 
									
										
										
										
											2012-02-14 16:43:51 +01:00
										 |  |  |     return tr("File path: %1\n%2").arg(QDir::toNativeSeparators(m_currentDocument->fileName())); | 
					
						
							| 
									
										
										
										
											2011-09-07 20:28:04 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-29 14:14:08 +02:00
										 |  |  | bool FindInCurrentFile::isEnabled() const | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2012-02-14 16:43:51 +01:00
										 |  |  |     return m_currentDocument && !m_currentDocument->fileName().isEmpty(); | 
					
						
							| 
									
										
										
										
											2009-08-25 12:30:47 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void FindInCurrentFile::handleFileChange(Core::IEditor *editor) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (!editor) { | 
					
						
							| 
									
										
										
										
											2012-02-14 16:43:51 +01:00
										 |  |  |         if (m_currentDocument) { | 
					
						
							|  |  |  |             m_currentDocument = 0; | 
					
						
							| 
									
										
										
										
											2011-12-14 15:55:05 +01:00
										 |  |  |             emit enabledChanged(isEnabled()); | 
					
						
							| 
									
										
										
										
											2009-08-25 12:30:47 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2012-02-14 16:43:51 +01:00
										 |  |  |         Core::IDocument *document = editor->document(); | 
					
						
							|  |  |  |         if (document != m_currentDocument) { | 
					
						
							|  |  |  |             m_currentDocument = document; | 
					
						
							| 
									
										
										
										
											2011-12-14 15:55:05 +01:00
										 |  |  |             emit enabledChanged(isEnabled()); | 
					
						
							| 
									
										
										
										
											2009-08-25 12:30:47 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2009-05-29 14:14:08 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-25 12:30:47 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-29 14:14:08 +02:00
										 |  |  | void FindInCurrentFile::writeSettings(QSettings *settings) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2012-01-05 11:05:28 +01:00
										 |  |  |     settings->beginGroup(QLatin1String("FindInCurrentFile")); | 
					
						
							| 
									
										
										
										
											2009-05-29 14:14:08 +02:00
										 |  |  |     writeCommonSettings(settings); | 
					
						
							|  |  |  |     settings->endGroup(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void FindInCurrentFile::readSettings(QSettings *settings) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2012-01-05 11:05:28 +01:00
										 |  |  |     settings->beginGroup(QLatin1String("FindInCurrentFile")); | 
					
						
							| 
									
										
										
										
											2012-01-23 11:45:33 +01:00
										 |  |  |     readCommonSettings(settings, QLatin1String("*")); | 
					
						
							| 
									
										
										
										
											2009-05-29 14:14:08 +02:00
										 |  |  |     settings->endGroup(); | 
					
						
							|  |  |  | } |