allprojectsfind.cpp 5.63 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
con's avatar
con committed
2
**
3
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
con's avatar
con committed
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
con's avatar
con committed
7
**
hjk's avatar
hjk committed
8
9
10
11
12
13
14
** 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.
15
**
16
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17
18
19
20
21
22
23
24
25
** 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
con's avatar
con committed
26
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
hjk's avatar
hjk committed
29

con's avatar
con committed
30
#include "allprojectsfind.h"
hjk's avatar
hjk committed
31

con's avatar
con committed
32
#include "project.h"
33
#include "session.h"
hjk's avatar
hjk committed
34
#include "projectexplorer.h"
35
#include "editorconfiguration.h"
hjk's avatar
hjk committed
36

37
#include <texteditor/itexteditor.h>
38
#include <utils/filesearch.h>
con's avatar
con committed
39

40
41
#include <QSettings>
#include <QRegExp>
42

43
44
#include <QGridLayout>
#include <QLabel>
con's avatar
con committed
45
46
47
48
49

using namespace ProjectExplorer;
using namespace ProjectExplorer::Internal;
using namespace TextEditor;

hjk's avatar
hjk committed
50
51
AllProjectsFind::AllProjectsFind()
    :  m_configWidget(0)
con's avatar
con committed
52
{
hjk's avatar
hjk committed
53
    connect(ProjectExplorerPlugin::instance(), SIGNAL(fileListChanged()), this, SLOT(handleFileListChanged()));
con's avatar
con committed
54
55
}

56
57
QString AllProjectsFind::id() const
{
58
    return QLatin1String("All Projects");
59
60
}

61
QString AllProjectsFind::displayName() const
con's avatar
con committed
62
63
64
65
66
67
{
    return tr("All Projects");
}

bool AllProjectsFind::isEnabled() const
{
hjk's avatar
hjk committed
68
    return BaseFileFind::isEnabled() && SessionManager::hasProjects();
con's avatar
con committed
69
70
}

71
72
Utils::FileIterator *AllProjectsFind::files(const QStringList &nameFilters,
                                            const QVariant &additionalParameters) const
con's avatar
con committed
73
{
74
    Q_UNUSED(additionalParameters)
hjk's avatar
hjk committed
75
    return filesForProjects(nameFilters, SessionManager::projects());
76
77
}

78
79
Utils::FileIterator *AllProjectsFind::filesForProjects(const QStringList &nameFilters,
                                            const QList<Project *> &projects) const
80
{
con's avatar
con committed
81
82
83
84
    QList<QRegExp> filterRegs;
    foreach (const QString &filter, nameFilters) {
        filterRegs << QRegExp(filter, Qt::CaseInsensitive, QRegExp::Wildcard);
    }
85
86
    QMap<QString, QTextCodec *> openEditorEncodings
            = TextEditor::ITextEditorDocument::openedTextDocumentEncodings();
87
    QMap<QString, QTextCodec *> encodings;
88
    foreach (const Project *project, projects) {
89
90
        QStringList projectFiles = project->files(Project::AllFiles);
        QStringList filteredFiles;
con's avatar
con committed
91
92
        if (!filterRegs.isEmpty()) {
            foreach (const QString &file, projectFiles) {
93
                foreach (QRegExp reg, filterRegs) {
94
                    if (reg.exactMatch(file) || reg.exactMatch(QFileInfo(file).fileName())) {
95
                        filteredFiles.append(file);
con's avatar
con committed
96
97
98
99
100
                        break;
                    }
                }
            }
        } else {
101
102
103
104
105
            filteredFiles = projectFiles;
        }
        foreach (const QString &fileName, filteredFiles) {
            QTextCodec *codec = openEditorEncodings.value(fileName);
            if (!codec)
106
                codec = project->editorConfiguration()->textCodec();
107
            encodings.insert(fileName, codec);
con's avatar
con committed
108
109
        }
    }
110
    return new Utils::FileIterator(encodings.keys(), encodings.values());
con's avatar
con committed
111
112
}

113
114
115
116
117
QVariant AllProjectsFind::additionalParameters() const
{
    return QVariant();
}

118
119
120
121
122
123
124
QString AllProjectsFind::label() const
{
    return tr("All Projects:");
}

QString AllProjectsFind::toolTip() const
{
125
    // %2 is filled by BaseFileFind::runNewSearch
126
127
128
    return tr("Filter: %1\n%2").arg(fileNameFilters().join(QLatin1String(",")));
}

129
130
131
132
133
void AllProjectsFind::handleFileListChanged()
{
    emit enabledChanged(isEnabled());
}

con's avatar
con committed
134
135
136
137
138
139
140
QWidget *AllProjectsFind::createConfigWidget()
{
    if (!m_configWidget) {
        m_configWidget = new QWidget;
        QGridLayout * const gridLayout = new QGridLayout(m_configWidget);
        gridLayout->setMargin(0);
        m_configWidget->setLayout(gridLayout);
141
        QLabel * const filePatternLabel = new QLabel(tr("Fi&le pattern:"));
con's avatar
con committed
142
143
144
        filePatternLabel->setMinimumWidth(80);
        filePatternLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);
        filePatternLabel->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
145
146
        QWidget *patternWidget = createPatternWidget();
        filePatternLabel->setBuddy(patternWidget);
147
148
        gridLayout->addWidget(filePatternLabel, 0, 0, Qt::AlignRight);
        gridLayout->addWidget(patternWidget, 0, 1);
con's avatar
con committed
149
150
151
152
153
154
155
        m_configWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
    }
    return m_configWidget;
}

void AllProjectsFind::writeSettings(QSettings *settings)
{
156
    settings->beginGroup(QLatin1String("AllProjectsFind"));
con's avatar
con committed
157
158
159
160
161
162
    writeCommonSettings(settings);
    settings->endGroup();
}

void AllProjectsFind::readSettings(QSettings *settings)
{
163
164
    settings->beginGroup(QLatin1String("AllProjectsFind"));
    readCommonSettings(settings, QString(QLatin1Char('*')));
con's avatar
con committed
165
166
    settings->endGroup();
}