2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** 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
|
2016-01-15 14:57:40 +01:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** GNU General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
2010-12-17 16:01:08 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2008-12-02 16:19:05 +01:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
#include "findinfiles.h"
|
|
|
|
|
|
2010-10-11 11:34:17 +02:00
|
|
|
#include <coreplugin/editormanager/editormanager.h>
|
2014-01-13 16:17:34 +01:00
|
|
|
#include <coreplugin/find/findplugin.h>
|
2015-12-26 21:47:16 +02:00
|
|
|
#include <coreplugin/icore.h>
|
2013-09-24 12:10:43 +03:00
|
|
|
#include <utils/filesearch.h>
|
2013-10-23 12:37:10 +03:00
|
|
|
#include <utils/fileutils.h>
|
2015-12-26 21:47:16 +02:00
|
|
|
#include <utils/historycompleter.h>
|
2015-01-14 14:58:24 +01:00
|
|
|
#include <utils/pathchooser.h>
|
2013-09-24 12:10:43 +03:00
|
|
|
#include <utils/qtcassert.h>
|
2010-10-11 11:34:17 +02:00
|
|
|
|
2012-08-06 13:42:46 +02:00
|
|
|
#include <QDebug>
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QSettings>
|
|
|
|
|
#include <QDir>
|
|
|
|
|
#include <QPushButton>
|
|
|
|
|
#include <QFileDialog>
|
2013-03-26 12:32:52 +01:00
|
|
|
#include <QLabel>
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QHBoxLayout>
|
2016-03-16 22:37:24 +01:00
|
|
|
#include <QStackedWidget>
|
|
|
|
|
#include <QComboBox>
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2014-01-13 16:17:34 +01:00
|
|
|
using namespace Core;
|
2011-09-05 10:19:49 +02:00
|
|
|
using namespace TextEditor;
|
2016-01-18 23:01:00 +02:00
|
|
|
using namespace Utils;
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2018-09-20 01:16:01 +03:00
|
|
|
static FindInFiles *m_instance = nullptr;
|
2015-12-26 21:47:16 +02:00
|
|
|
static const char HistoryKey[] = "FindInFiles.Directories.History";
|
2013-09-24 12:10:43 +03:00
|
|
|
|
2011-09-02 11:51:31 +02:00
|
|
|
FindInFiles::FindInFiles()
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2013-09-24 12:10:43 +03:00
|
|
|
m_instance = this;
|
2015-12-13 01:18:33 +02:00
|
|
|
connect(EditorManager::instance(), &EditorManager::findOnFileSystemRequest,
|
|
|
|
|
this, &FindInFiles::findOnFileSystem);
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2018-11-25 18:52:41 +01:00
|
|
|
FindInFiles::~FindInFiles() = default;
|
2013-03-26 12:32:52 +01:00
|
|
|
|
2016-01-21 12:24:52 +02:00
|
|
|
bool FindInFiles::isValid() const
|
|
|
|
|
{
|
2016-11-21 10:58:19 +01:00
|
|
|
return m_isValid;
|
2016-01-21 12:24:52 +02:00
|
|
|
}
|
|
|
|
|
|
2009-04-03 15:53:29 +02:00
|
|
|
QString FindInFiles::id() const
|
|
|
|
|
{
|
2012-01-05 11:05:28 +01:00
|
|
|
return QLatin1String("Files on Disk");
|
2009-04-03 15:53:29 +02:00
|
|
|
}
|
|
|
|
|
|
2010-07-12 12:06:15 +02:00
|
|
|
QString FindInFiles::displayName() const
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2015-11-10 14:24:51 +01:00
|
|
|
return tr("Files in File System");
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2016-01-18 23:01:00 +02:00
|
|
|
FileIterator *FindInFiles::files(const QStringList &nameFilters,
|
2016-12-09 13:33:12 +01:00
|
|
|
const QStringList &exclusionFilters,
|
2016-01-18 23:01:00 +02:00
|
|
|
const QVariant &additionalParameters) const
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2019-01-18 20:25:30 +01:00
|
|
|
return new SubDirFileIterator({additionalParameters.toString()},
|
2016-01-18 23:01:00 +02:00
|
|
|
nameFilters,
|
2016-12-09 13:33:12 +01:00
|
|
|
exclusionFilters,
|
2016-01-18 23:01:00 +02:00
|
|
|
EditorManager::defaultTextCodec());
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2011-12-13 10:50:57 +01:00
|
|
|
QVariant FindInFiles::additionalParameters() const
|
|
|
|
|
{
|
2019-05-27 13:32:20 +02:00
|
|
|
return QVariant::fromValue(path().toString());
|
2011-12-13 10:50:57 +01:00
|
|
|
}
|
|
|
|
|
|
2011-09-07 20:28:04 +02:00
|
|
|
QString FindInFiles::label() const
|
|
|
|
|
{
|
2016-03-16 22:37:24 +01:00
|
|
|
QString title = currentSearchEngine()->title();
|
|
|
|
|
|
2012-01-05 11:05:28 +01:00
|
|
|
const QChar slash = QLatin1Char('/');
|
2014-02-13 15:57:43 +01:00
|
|
|
const QStringList &nonEmptyComponents = path().toFileInfo().absoluteFilePath()
|
2012-01-05 11:05:28 +01:00
|
|
|
.split(slash, QString::SkipEmptyParts);
|
2016-01-28 23:37:10 +02:00
|
|
|
return tr("%1 \"%2\":")
|
|
|
|
|
.arg(title)
|
|
|
|
|
.arg(nonEmptyComponents.isEmpty() ? QString(slash) : nonEmptyComponents.last());
|
2011-09-07 20:28:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString FindInFiles::toolTip() const
|
|
|
|
|
{
|
2016-03-16 22:37:24 +01:00
|
|
|
//: the last arg is filled by BaseFileFind::runNewSearch
|
2016-12-09 13:33:12 +01:00
|
|
|
QString tooltip = tr("Path: %1\nFilter: %2\nExcluding: %3\n%4")
|
2014-02-13 15:57:43 +01:00
|
|
|
.arg(path().toUserOutput())
|
2016-12-09 13:33:12 +01:00
|
|
|
.arg(fileNameFilters().join(','))
|
|
|
|
|
.arg(fileExclusionFilters().join(','));
|
2016-03-16 22:37:24 +01:00
|
|
|
|
|
|
|
|
const QString searchEngineToolTip = currentSearchEngine()->toolTip();
|
|
|
|
|
if (!searchEngineToolTip.isEmpty())
|
|
|
|
|
tooltip = tooltip.arg(searchEngineToolTip);
|
|
|
|
|
|
|
|
|
|
return tooltip;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FindInFiles::syncSearchEngineCombo(int selectedSearchEngineIndex)
|
|
|
|
|
{
|
|
|
|
|
QTC_ASSERT(m_searchEngineCombo && selectedSearchEngineIndex >= 0
|
|
|
|
|
&& selectedSearchEngineIndex < searchEngines().size(), return);
|
|
|
|
|
|
|
|
|
|
m_searchEngineCombo->setCurrentIndex(selectedSearchEngineIndex);
|
2016-11-21 10:58:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FindInFiles::setValid(bool valid)
|
|
|
|
|
{
|
|
|
|
|
if (valid == m_isValid)
|
|
|
|
|
return;
|
|
|
|
|
m_isValid = valid;
|
|
|
|
|
emit validChanged(m_isValid);
|
2016-03-16 22:37:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FindInFiles::searchEnginesSelectionChanged(int index)
|
|
|
|
|
{
|
|
|
|
|
setCurrentSearchEngine(index);
|
|
|
|
|
m_searchEngineWidget->setCurrentIndex(index);
|
2011-09-07 20:28:04 +02:00
|
|
|
}
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
QWidget *FindInFiles::createConfigWidget()
|
|
|
|
|
{
|
|
|
|
|
if (!m_configWidget) {
|
|
|
|
|
m_configWidget = new QWidget;
|
2018-09-20 01:16:01 +03:00
|
|
|
auto gridLayout = new QGridLayout(m_configWidget);
|
2008-12-02 12:01:29 +01:00
|
|
|
gridLayout->setMargin(0);
|
|
|
|
|
m_configWidget->setLayout(gridLayout);
|
|
|
|
|
|
2016-01-28 23:37:10 +02:00
|
|
|
int row = 0;
|
2016-03-16 22:37:24 +01:00
|
|
|
auto searchEngineLabel = new QLabel(tr("Search engine:"));
|
|
|
|
|
gridLayout->addWidget(searchEngineLabel, row, 0, Qt::AlignRight);
|
|
|
|
|
m_searchEngineCombo = new QComboBox;
|
2019-02-26 09:40:49 +01:00
|
|
|
auto cc = QOverload<int>::of(&QComboBox::currentIndexChanged);
|
2016-03-16 22:37:24 +01:00
|
|
|
connect(m_searchEngineCombo, cc, this, &FindInFiles::searchEnginesSelectionChanged);
|
|
|
|
|
searchEngineLabel->setBuddy(m_searchEngineCombo);
|
|
|
|
|
gridLayout->addWidget(m_searchEngineCombo, row, 1);
|
|
|
|
|
|
|
|
|
|
m_searchEngineWidget = new QStackedWidget(m_configWidget);
|
|
|
|
|
foreach (SearchEngine *searchEngine, searchEngines()) {
|
|
|
|
|
m_searchEngineWidget->addWidget(searchEngine->widget());
|
|
|
|
|
m_searchEngineCombo->addItem(searchEngine->title());
|
|
|
|
|
}
|
|
|
|
|
gridLayout->addWidget(m_searchEngineWidget, row++, 2);
|
2016-01-28 23:37:10 +02:00
|
|
|
|
2011-09-19 14:39:30 +02:00
|
|
|
QLabel *dirLabel = new QLabel(tr("Director&y:"));
|
2016-01-28 23:37:10 +02:00
|
|
|
gridLayout->addWidget(dirLabel, row, 0, Qt::AlignRight);
|
2015-12-26 21:47:16 +02:00
|
|
|
m_directory = new PathChooser;
|
|
|
|
|
m_directory->setExpectedKind(PathChooser::ExistingDirectory);
|
|
|
|
|
m_directory->setPromptDialogTitle(tr("Directory to Search"));
|
2016-01-28 23:37:10 +02:00
|
|
|
connect(m_directory.data(), &PathChooser::pathChanged,
|
2016-11-21 10:58:19 +01:00
|
|
|
this, &FindInFiles::pathChanged);
|
2016-01-28 23:37:10 +02:00
|
|
|
m_directory->setHistoryCompleter(QLatin1String(HistoryKey),
|
|
|
|
|
/*restoreLastItemFromHistory=*/ true);
|
2015-12-26 21:47:16 +02:00
|
|
|
if (!HistoryCompleter::historyExistsFor(QLatin1String(HistoryKey))) {
|
|
|
|
|
auto completer = static_cast<HistoryCompleter *>(m_directory->lineEdit()->completer());
|
|
|
|
|
const QStringList legacyHistory = Core::ICore::settings()->value(
|
|
|
|
|
QLatin1String("Find/FindInFiles/directories")).toStringList();
|
|
|
|
|
for (const QString &dir: legacyHistory)
|
|
|
|
|
completer->addEntry(dir);
|
|
|
|
|
}
|
2008-12-02 16:44:32 +01:00
|
|
|
dirLabel->setBuddy(m_directory);
|
2016-01-28 23:37:10 +02:00
|
|
|
gridLayout->addWidget(m_directory, row++, 1, 1, 2);
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2016-12-09 13:33:12 +01:00
|
|
|
const QList<QPair<QWidget *, QWidget *>> patternWidgets = createPatternWidgets();
|
|
|
|
|
for (const QPair<QWidget *, QWidget *> &p : patternWidgets) {
|
|
|
|
|
gridLayout->addWidget(p.first, row, 0, Qt::AlignRight);
|
|
|
|
|
gridLayout->addWidget(p.second, row, 1, 1, 2);
|
|
|
|
|
++row;
|
|
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
m_configWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
|
2016-11-21 10:58:19 +01:00
|
|
|
|
|
|
|
|
// validity
|
|
|
|
|
auto updateValidity = [this]() {
|
|
|
|
|
setValid(currentSearchEngine()->isEnabled() && m_directory->isValid());
|
|
|
|
|
};
|
|
|
|
|
connect(this, &BaseFileFind::currentSearchEngineChanged, this, updateValidity);
|
|
|
|
|
foreach (SearchEngine *searchEngine, searchEngines())
|
|
|
|
|
connect(searchEngine, &SearchEngine::enabledChanged, this, updateValidity);
|
|
|
|
|
connect(m_directory.data(), &PathChooser::validChanged, this, updateValidity);
|
|
|
|
|
updateValidity();
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
return m_configWidget;
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-18 23:01:00 +02:00
|
|
|
FileName FindInFiles::path() const
|
2013-10-23 12:37:10 +03:00
|
|
|
{
|
2015-12-26 21:47:16 +02:00
|
|
|
return m_directory->fileName();
|
2013-10-23 12:37:10 +03:00
|
|
|
}
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
void FindInFiles::writeSettings(QSettings *settings)
|
|
|
|
|
{
|
2012-01-05 11:05:28 +01:00
|
|
|
settings->beginGroup(QLatin1String("FindInFiles"));
|
2008-12-02 12:01:29 +01:00
|
|
|
writeCommonSettings(settings);
|
|
|
|
|
settings->endGroup();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FindInFiles::readSettings(QSettings *settings)
|
|
|
|
|
{
|
2012-01-05 11:05:28 +01:00
|
|
|
settings->beginGroup(QLatin1String("FindInFiles"));
|
2018-11-23 11:36:42 +01:00
|
|
|
readCommonSettings(settings, "*.cpp,*.h", "*/.git/*,*/.cvs/*,*/.svn/*,*.autosave");
|
2008-12-02 12:01:29 +01:00
|
|
|
settings->endGroup();
|
|
|
|
|
}
|
2011-09-05 10:19:49 +02:00
|
|
|
|
2016-01-18 23:01:00 +02:00
|
|
|
void FindInFiles::setDirectory(const FileName &directory)
|
2011-09-05 10:19:49 +02:00
|
|
|
{
|
2015-12-26 21:47:16 +02:00
|
|
|
m_directory->setFileName(directory);
|
2011-09-05 10:19:49 +02:00
|
|
|
}
|
|
|
|
|
|
2017-05-18 22:57:19 +03:00
|
|
|
void FindInFiles::setBaseDirectory(const FileName &directory)
|
|
|
|
|
{
|
|
|
|
|
m_directory->setBaseFileName(directory);
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-28 23:37:10 +02:00
|
|
|
FileName FindInFiles::directory() const
|
|
|
|
|
{
|
|
|
|
|
return m_directory->fileName();
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-24 12:10:43 +03:00
|
|
|
void FindInFiles::findOnFileSystem(const QString &path)
|
|
|
|
|
{
|
|
|
|
|
QTC_ASSERT(m_instance, return);
|
|
|
|
|
const QFileInfo fi(path);
|
|
|
|
|
const QString folder = fi.isDir() ? fi.absoluteFilePath() : fi.absolutePath();
|
2016-01-18 23:01:00 +02:00
|
|
|
m_instance->setDirectory(FileName::fromString(folder));
|
2016-03-11 19:12:07 +01:00
|
|
|
Find::openFindDialog(m_instance);
|
2013-09-24 12:10:43 +03:00
|
|
|
}
|
2016-01-28 23:37:10 +02:00
|
|
|
|
|
|
|
|
FindInFiles *FindInFiles::instance()
|
|
|
|
|
{
|
|
|
|
|
return m_instance;
|
|
|
|
|
}
|