findinfiles.cpp 7.17 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
con's avatar
con committed
2
**
Eike Ziller's avatar
Eike Ziller committed
3
4
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing
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
** 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
Eike Ziller's avatar
Eike Ziller committed
12
13
** a written agreement between you and The Qt Company.  For licensing terms and
** conditions see http://www.qt.io/terms-conditions.  For further information
Eike Ziller's avatar
Eike Ziller committed
14
** use the contact form at http://www.qt.io/contact-us.
15
**
16
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17
** Alternatively, this file may be used under the terms of the GNU Lesser
Eike Ziller's avatar
Eike Ziller committed
18
19
20
21
22
23
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file.  Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
hjk's avatar
hjk committed
24
**
Eike Ziller's avatar
Eike Ziller committed
25
26
** In addition, as a special exception, The Qt Company gives you certain additional
** rights.  These rights are described in The Qt Company LGPL Exception
con's avatar
con committed
27
28
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
29
****************************************************************************/
hjk's avatar
hjk committed
30

con's avatar
con committed
31
32
#include "findinfiles.h"

33
#include <coreplugin/editormanager/editormanager.h>
34
#include <coreplugin/find/findplugin.h>
35
#include <utils/filesearch.h>
36
#include <utils/fileutils.h>
37
#include <utils/pathchooser.h>
38
#include <utils/qtcassert.h>
39

hjk's avatar
hjk committed
40
#include <QDebug>
41
42
43
44
#include <QSettings>
#include <QDir>
#include <QPushButton>
#include <QFileDialog>
45
46
#include <QLabel>
#include <QComboBox>
47
#include <QHBoxLayout>
con's avatar
con committed
48

49
using namespace Core;
50
using namespace TextEditor;
51
using namespace Utils;
con's avatar
con committed
52

53
54
static FindInFiles *m_instance = 0;

55
56
FindInFiles::FindInFiles()
  : m_configWidget(0),
con's avatar
con committed
57
58
    m_directory(0)
{
59
    m_instance = this;
60
61
    connect(EditorManager::instance(), &EditorManager::findOnFileSystemRequest,
            this, &FindInFiles::findOnFileSystem);
con's avatar
con committed
62
63
}

64
65
66
67
FindInFiles::~FindInFiles()
{
}

68
69
QString FindInFiles::id() const
{
70
    return QLatin1String("Files on Disk");
71
72
}

73
QString FindInFiles::displayName() const
con's avatar
con committed
74
{
75
    return tr("Files in File System");
con's avatar
con committed
76
77
}

78
void FindInFiles::findAll(const QString &txt, FindFlags findFlags)
con's avatar
con committed
79
80
81
82
83
{
    updateComboEntries(m_directory, true);
    BaseFileFind::findAll(txt, findFlags);
}

84
85
FileIterator *FindInFiles::files(const QStringList &nameFilters,
                                 const QVariant &additionalParameters) const
con's avatar
con committed
86
{
87
88
89
    return new SubDirFileIterator(QStringList() << additionalParameters.toString(),
                                  nameFilters,
                                  EditorManager::defaultTextCodec());
con's avatar
con committed
90
91
}

92
93
QVariant FindInFiles::additionalParameters() const
{
94
    return qVariantFromValue(path().toString());
95
96
}

97
98
QString FindInFiles::label() const
{
99
    const QChar slash = QLatin1Char('/');
100
    const QStringList &nonEmptyComponents = path().toFileInfo().absoluteFilePath()
101
            .split(slash, QString::SkipEmptyParts);
102
    return tr("Directory \"%1\":").arg(nonEmptyComponents.isEmpty() ? QString(slash) : nonEmptyComponents.last());
103
104
105
106
}

QString FindInFiles::toolTip() const
{
107
    //: %3 is filled by BaseFileFind::runNewSearch
108
    return tr("Path: %1\nFilter: %2\n%3")
109
            .arg(path().toUserOutput())
hjk's avatar
hjk committed
110
            .arg(fileNameFilters().join(QLatin1Char(',')));
111
112
}

con's avatar
con committed
113
114
115
116
117
118
119
120
QWidget *FindInFiles::createConfigWidget()
{
    if (!m_configWidget) {
        m_configWidget = new QWidget;
        QGridLayout * const gridLayout = new QGridLayout(m_configWidget);
        gridLayout->setMargin(0);
        m_configWidget->setLayout(gridLayout);

Leandro Melo's avatar
Leandro Melo committed
121
        QLabel *dirLabel = new QLabel(tr("Director&y:"));
122
        gridLayout->addWidget(dirLabel, 0, 0, Qt::AlignRight);
con's avatar
con committed
123
124
125
126
127
128
129
130
        m_directory = new QComboBox;
        m_directory->setEditable(true);
        m_directory->setMaxCount(30);
        m_directory->setMinimumContentsLength(10);
        m_directory->setSizeAdjustPolicy(QComboBox::AdjustToMinimumContentsLengthWithIcon);
        m_directory->setInsertPolicy(QComboBox::InsertAtTop);
        m_directory->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
        m_directory->setModel(&m_directoryStrings);
131
        syncComboWithSettings(m_directory, m_directorySetting.toUserOutput());
132
        dirLabel->setBuddy(m_directory);
133
        gridLayout->addWidget(m_directory, 0, 1);
134
        QPushButton *browseButton = new QPushButton(PathChooser::browseButtonLabel());
135
        gridLayout->addWidget(browseButton, 0, 2);
136
        connect(browseButton, &QAbstractButton::clicked, this, &FindInFiles::openFileBrowser);
con's avatar
con committed
137

Leandro Melo's avatar
Leandro Melo committed
138
        QLabel * const filePatternLabel = new QLabel(tr("Fi&le pattern:"));
con's avatar
con committed
139
140
141
        filePatternLabel->setMinimumWidth(80);
        filePatternLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);
        filePatternLabel->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
142
143
        QWidget *patternWidget = createPatternWidget();
        filePatternLabel->setBuddy(patternWidget);
144
145
        gridLayout->addWidget(filePatternLabel, 1, 0);
        gridLayout->addWidget(patternWidget, 1, 1, 1, 2);
con's avatar
con committed
146
147
148
149
150
151
152
153
154
        m_configWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
    }
    return m_configWidget;
}

void FindInFiles::openFileBrowser()
{
    if (!m_directory)
        return;
155
    QString oldDir = path().toString();
156
157
    if (!QDir(oldDir).exists())
        oldDir.clear();
con's avatar
con committed
158
    QString dir = QFileDialog::getExistingDirectory(m_configWidget,
159
        tr("Directory to search"), oldDir);
con's avatar
con committed
160
    if (!dir.isEmpty())
161
        m_directory->setEditText(QDir::toNativeSeparators(dir));
con's avatar
con committed
162
163
}

164
FileName FindInFiles::path() const
165
{
166
    return FileName::fromUserInput(FileUtils::normalizePathName(m_directory->currentText()));
167
168
}

con's avatar
con committed
169
170
void FindInFiles::writeSettings(QSettings *settings)
{
171
    settings->beginGroup(QLatin1String("FindInFiles"));
con's avatar
con committed
172
    writeCommonSettings(settings);
173
    settings->setValue(QLatin1String("directories"), m_directoryStrings.stringList());
con's avatar
con committed
174
    if (m_directory)
175
        settings->setValue(QLatin1String("currentDirectory"), path().toString());
con's avatar
con committed
176
177
178
179
180
    settings->endGroup();
}

void FindInFiles::readSettings(QSettings *settings)
{
181
182
183
    settings->beginGroup(QLatin1String("FindInFiles"));
    readCommonSettings(settings, QLatin1String("*.cpp,*.h"));
    m_directoryStrings.setStringList(settings->value(QLatin1String("directories")).toStringList());
184
    m_directorySetting = FileName::fromString(
185
                settings->value(QLatin1String("currentDirectory")).toString());
con's avatar
con committed
186
    settings->endGroup();
187
    syncComboWithSettings(m_directory, m_directorySetting.toUserOutput());
con's avatar
con committed
188
}
189

190
void FindInFiles::setDirectory(const FileName &directory)
191
{
192
    syncComboWithSettings(m_directory, directory.toUserOutput());
193
194
}

195
196
197
198
199
void FindInFiles::findOnFileSystem(const QString &path)
{
    QTC_ASSERT(m_instance, return);
    const QFileInfo fi(path);
    const QString folder = fi.isDir() ? fi.absoluteFilePath() : fi.absolutePath();
200
    m_instance->setDirectory(FileName::fromString(folder));
201
    FindPlugin::instance()->openFindDialog(m_instance);
202
}