OSDN Git Service

f2e74cffda56de21fc296cc2ec4c38e8bd1d1225
[qt-creator-jp/qt-creator-jp.git] / src / plugins / projectexplorer / allprojectsfind.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 "allprojectsfind.h"
35
36 #include "project.h"
37 #include "session.h"
38 #include "projectexplorer.h"
39 #include "editorconfiguration.h"
40
41 #include <utils/qtcassert.h>
42 #include <texteditor/itexteditor.h>
43 #include <coreplugin/editormanager/editormanager.h>
44
45 #include <QtCore/QSettings>
46 #include <QtCore/QRegExp>
47
48 #include <QtGui/QGridLayout>
49 #include <QtGui/QLabel>
50
51 using namespace Find;
52 using namespace ProjectExplorer;
53 using namespace ProjectExplorer::Internal;
54 using namespace TextEditor;
55
56 AllProjectsFind::AllProjectsFind(ProjectExplorerPlugin *plugin, SearchResultWindow *resultWindow)
57     : BaseFileFind(resultWindow),
58     m_plugin(plugin),
59     m_configWidget(0)
60 {
61     connect(m_plugin, SIGNAL(fileListChanged()), this, SIGNAL(changed()));
62 }
63
64 QString AllProjectsFind::id() const
65 {
66     return QLatin1String("All Projects");
67 }
68
69 QString AllProjectsFind::displayName() const
70 {
71     return tr("All Projects");
72 }
73
74 bool AllProjectsFind::isEnabled() const
75 {
76     return BaseFileFind::isEnabled()
77             && m_plugin->session() != 0
78             && m_plugin->session()->projects().count() > 0;
79 }
80
81 QList<Project *> AllProjectsFind::projects() const
82 {
83     Q_ASSERT(m_plugin->session());
84     return m_plugin->session()->projects();
85 }
86
87 Utils::FileIterator *AllProjectsFind::files() const
88 {
89     QList<QRegExp> filterRegs;
90     QStringList nameFilters = fileNameFilters();
91     foreach (const QString &filter, nameFilters) {
92         filterRegs << QRegExp(filter, Qt::CaseInsensitive, QRegExp::Wildcard);
93     }
94     QMap<QString, QTextCodec *> openEditorEncodings = TextEditor::ITextEditor::openedTextEditorsEncodings();
95     QMap<QString, QTextCodec *> encodings;
96     foreach (const Project *project, projects()) {
97         QStringList projectFiles = project->files(Project::AllFiles);
98         QStringList filteredFiles;
99         if (!filterRegs.isEmpty()) {
100             foreach (const QString &file, projectFiles) {
101                 foreach (const QRegExp &reg, filterRegs) {
102                     if (reg.exactMatch(file) || reg.exactMatch(QFileInfo(file).fileName())) {
103                         filteredFiles.append(file);
104                         break;
105                     }
106                 }
107             }
108         } else {
109             filteredFiles = projectFiles;
110         }
111         foreach (const QString &fileName, filteredFiles) {
112             QTextCodec *codec = openEditorEncodings.value(fileName);
113             if (!codec)
114                 codec = project->editorConfiguration()->textCodec();
115             encodings.insert(fileName, codec);
116         }
117     }
118     return new Utils::FileIterator(encodings.keys(), encodings.values());
119 }
120
121 QWidget *AllProjectsFind::createConfigWidget()
122 {
123     if (!m_configWidget) {
124         m_configWidget = new QWidget;
125         QGridLayout * const gridLayout = new QGridLayout(m_configWidget);
126         gridLayout->setMargin(0);
127         m_configWidget->setLayout(gridLayout);
128         QLabel * const filePatternLabel = new QLabel(tr("File &pattern:"));
129         filePatternLabel->setMinimumWidth(80);
130         filePatternLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);
131         filePatternLabel->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
132         QWidget *patternWidget = createPatternWidget();
133         filePatternLabel->setBuddy(patternWidget);
134         gridLayout->addWidget(filePatternLabel, 0, 0, Qt::AlignRight);
135         gridLayout->addWidget(patternWidget, 0, 1);
136         m_configWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
137     }
138     return m_configWidget;
139 }
140
141 void AllProjectsFind::writeSettings(QSettings *settings)
142 {
143     settings->beginGroup(QLatin1String("AllProjectsFind"));
144     writeCommonSettings(settings);
145     settings->endGroup();
146 }
147
148 void AllProjectsFind::readSettings(QSettings *settings)
149 {
150     settings->beginGroup(QLatin1String("AllProjectsFind"));
151     readCommonSettings(settings, QString(QLatin1Char('*')));
152     settings->endGroup();
153 }