1 /***************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the documentation of Qt Creator.
9 ** You may use this file under the terms of the BSD license as follows:
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
14 ** * Redistributions of source code must retain the above copyright
15 ** notice, this list of conditions and the following disclaimer.
16 ** * Redistributions in binary form must reproduce the above copyright
17 ** notice, this list of conditions and the following disclaimer in
18 ** the documentation and/or other materials provided with the
20 ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
21 ** the names of its contributors may be used to endorse or promote
22 ** products derived from this software without specific prior written
25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
37 ****************************************************************************/
39 #include "headerfilter.h"
40 #include <projectexplorer/projectexplorer.h>
41 #include <projectexplorer/iprojectmanager.h>
42 #include <projectexplorer/project.h>
43 #include <projectexplorer/session.h>
44 #include <extensionsystem/pluginmanager.h>
45 #include <utils/filesearch.h>
46 #include<QFutureWatcher>
48 #include <find/searchresultwindow.h>
49 #include <texteditor/basetexteditor.h>
54 using namespace Utils;
57 struct HeaderFilterData
59 HeaderFilterData() : m_projectPlugin(0), m_searchResultWindow(0){}
60 QFutureWatcher<FileSearchResult> watcher;
62 ProjectExplorer::ProjectExplorerPlugin* projectExplorer()
65 return m_projectPlugin;
67 ExtensionSystem::PluginManager* pm = ExtensionSystem::PluginManager::instance();
68 m_projectPlugin = pm->getObject<ProjectExplorer::ProjectExplorerPlugin>();
69 return m_projectPlugin;
72 // Method to search and return the search window
74 Find::SearchResultWindow* searchResultWindow()
76 if(m_searchResultWindow)
77 return m_searchResultWindow;
79 ExtensionSystem::PluginManager* pm = ExtensionSystem::PluginManager::instance();
80 m_searchResultWindow = pm->getObject<Find::SearchResultWindow>();
81 return m_searchResultWindow;
85 ProjectExplorer::ProjectExplorerPlugin* m_projectPlugin;
86 Find::SearchResultWindow *m_searchResultWindow;
90 HeaderFilter::HeaderFilter()
92 d = new HeaderFilterData;
93 d->watcher.setPendingResultsLimit(1);
95 // displayResult(int) is called when every a new
96 // search result is generated
97 connect(&d->watcher, SIGNAL(resultReadyAt(int)),this, SLOT(displayResult(int)));
100 HeaderFilter::~HeaderFilter()
105 QString HeaderFilter::id() const
107 return "HeaderFilter";
110 QString HeaderFilter::name() const
112 return tr("Header Filter");
115 bool HeaderFilter::isEnabled() const
117 QList<ProjectExplorer::Project*> projects = d->projectExplorer()->session()->projects();
124 QKeySequence HeaderFilter::defaultShortcut() const
126 return QKeySequence();
129 QWidget *HeaderFilter::createConfigWidget()
131 return (new QLabel("This is a header filter"));
135 void HeaderFilter::findAll(const QString &text,QTextDocument::FindFlags findFlags)
137 // Fetch a list of all open projects
138 QList<ProjectExplorer::Project*> projects = d->projectExplorer()->session()->projects();
140 // Make a list of files in each project
142 Q_FOREACH(ProjectExplorer::Project* project, projects)
143 files += project->files(ProjectExplorer::Project::AllFiles);
146 files.removeDuplicates();
148 //------------------------------------------------------------
150 QString includeline = "#include <" + text + ">";
151 Find::SearchResult *result = d->searchResultWindow()->startNewSearch();
153 d->watcher.setFuture(QFuture<FileSearchResult>());
155 //When result gets activated it invokes the openEditor function
156 connect(result, SIGNAL(activated(Find::SearchResultItem)),
157 this, SLOT(openEditor(Find::SearchResultItem)));
159 d->searchResultWindow()->popup(true);
161 // Let the watcher monitor the search results
162 d->watcher.setFuture(Utils::findInFiles(includeline, files, findFlags));
165 void HeaderFilter::displayResult(int index)
167 FileSearchResult result = d->watcher.future().resultAt(index);
169 d->searchResultWindow()->addResult(result.fileName,
176 void HeaderFilter::openEditor(const Find::SearchResultItem &item)
178 TextEditor::BaseTextEditor::openEditorAt(item.fileName, item.lineNumber, item.index);