OSDN Git Service

fac2037c306f46001f5f8ab4b649456d6ac1e865
[qt-creator-jp/qt-creator-jp.git] / doc / pluginhowto / examples / headerfilter / headerfilter.cpp
1 /***************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the documentation of Qt Creator.
8 **
9 ** You may use this file under the terms of the BSD license as follows:
10 **
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
13 ** met:
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
19 **     distribution.
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
23 **     permission.
24 **
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."
36 **
37 ****************************************************************************/
38
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>
47 #include<QLabel>
48 #include <find/searchresultwindow.h>
49 #include <texteditor/basetexteditor.h>
50
51
52
53 using namespace Core;
54 using namespace Utils;
55
56
57 struct HeaderFilterData
58  {
59     HeaderFilterData() : m_projectPlugin(0), m_searchResultWindow(0){}
60     QFutureWatcher<FileSearchResult> watcher;
61
62     ProjectExplorer::ProjectExplorerPlugin* projectExplorer()
63      {
64          if(m_projectPlugin)
65              return m_projectPlugin;
66
67          ExtensionSystem::PluginManager* pm = ExtensionSystem::PluginManager::instance();
68          m_projectPlugin = pm->getObject<ProjectExplorer::ProjectExplorerPlugin>();
69          return m_projectPlugin;
70      }
71
72     // Method to search and return the search window
73
74     Find::SearchResultWindow* searchResultWindow()
75     {
76         if(m_searchResultWindow)
77             return m_searchResultWindow;
78
79         ExtensionSystem::PluginManager* pm = ExtensionSystem::PluginManager::instance();
80         m_searchResultWindow = pm->getObject<Find::SearchResultWindow>();
81         return m_searchResultWindow;
82     }
83
84  private:
85      ProjectExplorer::ProjectExplorerPlugin* m_projectPlugin;
86      Find::SearchResultWindow *m_searchResultWindow;
87
88 };
89
90 HeaderFilter::HeaderFilter()
91 {
92     d = new HeaderFilterData;
93     d->watcher.setPendingResultsLimit(1);
94
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)));
98 }
99
100 HeaderFilter::~HeaderFilter()
101 {
102     delete d;
103 }
104
105 QString HeaderFilter::id() const
106 {
107     return "HeaderFilter";
108 }
109
110 QString HeaderFilter::name() const
111 {
112     return tr("Header Filter");
113 }
114
115 bool HeaderFilter::isEnabled() const
116 {
117     QList<ProjectExplorer::Project*> projects = d->projectExplorer()->session()->projects();
118     if(projects.count())
119         return true;
120
121     return false;
122 }
123
124 QKeySequence HeaderFilter::defaultShortcut() const
125 {
126     return QKeySequence();
127 }
128
129 QWidget *HeaderFilter::createConfigWidget()
130 {
131     return (new QLabel("This is a header filter"));
132 }
133
134
135 void HeaderFilter::findAll(const QString &text,QTextDocument::FindFlags findFlags)
136  {
137     // Fetch a list of all open projects
138     QList<ProjectExplorer::Project*> projects = d->projectExplorer()->session()->projects();
139
140     // Make a list of files in each project
141     QStringList files;
142     Q_FOREACH(ProjectExplorer::Project* project, projects)
143             files += project->files(ProjectExplorer::Project::AllFiles);
144
145     // Remove duplicates
146     files.removeDuplicates();
147
148     //------------------------------------------------------------
149     // Begin searching
150     QString includeline = "#include <" + text + ">";
151     Find::SearchResult *result = d->searchResultWindow()->startNewSearch();
152
153     d->watcher.setFuture(QFuture<FileSearchResult>());
154
155    //When result gets activated it invokes the openEditor function
156     connect(result, SIGNAL(activated(Find::SearchResultItem)),
157             this, SLOT(openEditor(Find::SearchResultItem)));
158
159     d->searchResultWindow()->popup(true);
160
161     // Let the watcher monitor the search results
162     d->watcher.setFuture(Utils::findInFiles(includeline, files, findFlags));
163 }
164
165 void HeaderFilter::displayResult(int index)
166 {
167     FileSearchResult result = d->watcher.future().resultAt(index);
168
169     d->searchResultWindow()->addResult(result.fileName,
170                                        result.lineNumber,
171                                        result.matchingLine,
172                                        result.matchStart,
173                                        result.matchLength);
174 }
175
176 void HeaderFilter::openEditor(const Find::SearchResultItem &item)
177 {
178     TextEditor::BaseTextEditor::openEditorAt(item.fileName, item.lineNumber, item.index);
179 }
180