2011-04-27 10:05:43 +00:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
2016-01-22 12:24:00 +00:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2011-04-27 10:05:43 +00:00
|
|
|
**
|
|
|
|
** This file is part of the examples of the Qt Toolkit.
|
|
|
|
**
|
|
|
|
** $QT_BEGIN_LICENSE:BSD$
|
2016-01-22 12:24:00 +00: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
|
|
|
|
** 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.
|
|
|
|
**
|
|
|
|
** BSD License Usage
|
|
|
|
** Alternatively, you may use this file under the terms of the BSD license
|
|
|
|
** as follows:
|
2011-04-27 10:05:43 +00:00
|
|
|
**
|
|
|
|
** "Redistribution and use in source and binary forms, with or without
|
|
|
|
** modification, are permitted provided that the following conditions are
|
|
|
|
** met:
|
|
|
|
** * Redistributions of source code must retain the above copyright
|
|
|
|
** notice, this list of conditions and the following disclaimer.
|
|
|
|
** * Redistributions in binary form must reproduce the above copyright
|
|
|
|
** notice, this list of conditions and the following disclaimer in
|
|
|
|
** the documentation and/or other materials provided with the
|
|
|
|
** distribution.
|
2015-02-13 11:15:33 +00:00
|
|
|
** * Neither the name of The Qt Company Ltd nor the names of its
|
|
|
|
** contributors may be used to endorse or promote products derived
|
|
|
|
** from this software without specific prior written permission.
|
2012-09-19 12:28:29 +00:00
|
|
|
**
|
2011-04-27 10:05:43 +00:00
|
|
|
**
|
|
|
|
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
2012-01-24 06:17:24 +00:00
|
|
|
**
|
2011-04-27 10:05:43 +00:00
|
|
|
** $QT_END_LICENSE$
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-05-07 20:57:49 +00:00
|
|
|
#include <QtWidgets>
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
#include "window.h"
|
|
|
|
|
2016-07-05 11:26:53 +00:00
|
|
|
//! [17]
|
|
|
|
enum { absoluteFileNameRole = Qt::UserRole + 1 };
|
|
|
|
//! [17]
|
|
|
|
|
|
|
|
//! [18]
|
|
|
|
static inline QString fileNameOfItem(const QTableWidgetItem *item)
|
|
|
|
{
|
|
|
|
return item->data(absoluteFileNameRole).toString();
|
|
|
|
}
|
|
|
|
//! [18]
|
|
|
|
|
|
|
|
//! [14]
|
|
|
|
static inline void openFile(const QString &fileName)
|
|
|
|
{
|
|
|
|
QDesktopServices::openUrl(QUrl::fromLocalFile(fileName));
|
|
|
|
}
|
|
|
|
//! [14]
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
//! [0]
|
|
|
|
Window::Window(QWidget *parent)
|
2011-04-27 17:16:41 +00:00
|
|
|
: QWidget(parent)
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
2016-07-05 11:26:53 +00:00
|
|
|
QPushButton *browseButton = new QPushButton(tr("&Browse..."), this);
|
2015-07-21 12:41:36 +00:00
|
|
|
connect(browseButton, &QAbstractButton::clicked, this, &Window::browse);
|
|
|
|
findButton = new QPushButton(tr("&Find"), this);
|
|
|
|
connect(findButton, &QAbstractButton::clicked, this, &Window::find);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
fileComboBox = createComboBox(tr("*"));
|
2016-07-05 11:26:53 +00:00
|
|
|
connect(fileComboBox->lineEdit(), &QLineEdit::returnPressed,
|
|
|
|
this, &Window::animateFindClick);
|
2011-04-27 10:05:43 +00:00
|
|
|
textComboBox = createComboBox();
|
2016-07-05 11:26:53 +00:00
|
|
|
connect(textComboBox->lineEdit(), &QLineEdit::returnPressed,
|
|
|
|
this, &Window::animateFindClick);
|
|
|
|
directoryComboBox = createComboBox(QDir::toNativeSeparators(QDir::currentPath()));
|
|
|
|
connect(directoryComboBox->lineEdit(), &QLineEdit::returnPressed,
|
|
|
|
this, &Window::animateFindClick);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
filesFoundLabel = new QLabel;
|
|
|
|
|
|
|
|
createFilesTable();
|
|
|
|
//! [0]
|
|
|
|
|
|
|
|
//! [1]
|
2016-07-05 11:26:53 +00:00
|
|
|
QGridLayout *mainLayout = new QGridLayout(this);
|
|
|
|
mainLayout->addWidget(new QLabel(tr("Named:")), 0, 0);
|
2011-04-27 10:05:43 +00:00
|
|
|
mainLayout->addWidget(fileComboBox, 0, 1, 1, 2);
|
2016-07-05 11:26:53 +00:00
|
|
|
mainLayout->addWidget(new QLabel(tr("Containing text:")), 1, 0);
|
2011-04-27 10:05:43 +00:00
|
|
|
mainLayout->addWidget(textComboBox, 1, 1, 1, 2);
|
2016-07-05 11:26:53 +00:00
|
|
|
mainLayout->addWidget(new QLabel(tr("In directory:")), 2, 0);
|
2011-04-27 10:05:43 +00:00
|
|
|
mainLayout->addWidget(directoryComboBox, 2, 1);
|
|
|
|
mainLayout->addWidget(browseButton, 2, 2);
|
|
|
|
mainLayout->addWidget(filesTable, 3, 0, 1, 3);
|
2011-04-27 17:16:41 +00:00
|
|
|
mainLayout->addWidget(filesFoundLabel, 4, 0, 1, 2);
|
|
|
|
mainLayout->addWidget(findButton, 4, 2);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
setWindowTitle(tr("Find Files"));
|
2016-07-05 11:26:53 +00:00
|
|
|
const QRect screenGeometry = QApplication::desktop()->screenGeometry(this);
|
|
|
|
resize(screenGeometry.width() / 2, screenGeometry.height() / 3);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
//! [1]
|
|
|
|
|
|
|
|
//! [2]
|
|
|
|
void Window::browse()
|
|
|
|
{
|
2016-07-05 11:26:53 +00:00
|
|
|
QString directory =
|
|
|
|
QDir::toNativeSeparators(QFileDialog::getExistingDirectory(this, tr("Find Files"), QDir::currentPath()));
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
if (!directory.isEmpty()) {
|
|
|
|
if (directoryComboBox->findText(directory) == -1)
|
|
|
|
directoryComboBox->addItem(directory);
|
|
|
|
directoryComboBox->setCurrentIndex(directoryComboBox->findText(directory));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//! [2]
|
|
|
|
|
|
|
|
static void updateComboBox(QComboBox *comboBox)
|
|
|
|
{
|
|
|
|
if (comboBox->findText(comboBox->currentText()) == -1)
|
|
|
|
comboBox->addItem(comboBox->currentText());
|
|
|
|
}
|
|
|
|
|
2016-07-05 11:26:53 +00:00
|
|
|
//! [13]
|
|
|
|
|
|
|
|
static void findRecursion(const QString &path, const QString &pattern, QStringList *result)
|
|
|
|
{
|
|
|
|
QDir currentDir(path);
|
|
|
|
const QString prefix = path + QLatin1Char('/');
|
|
|
|
foreach (const QString &match, currentDir.entryList(QStringList(pattern), QDir::Files | QDir::NoSymLinks))
|
|
|
|
result->append(prefix + match);
|
|
|
|
foreach (const QString &dir, currentDir.entryList(QDir::Dirs | QDir::NoSymLinks | QDir::NoDotAndDotDot))
|
|
|
|
findRecursion(prefix + dir, pattern, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
//! [13]
|
2011-04-27 10:05:43 +00:00
|
|
|
//! [3]
|
2016-07-05 11:26:53 +00:00
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
void Window::find()
|
|
|
|
{
|
|
|
|
filesTable->setRowCount(0);
|
|
|
|
|
|
|
|
QString fileName = fileComboBox->currentText();
|
|
|
|
QString text = textComboBox->currentText();
|
2016-07-05 11:26:53 +00:00
|
|
|
QString path = QDir::cleanPath(directoryComboBox->currentText());
|
2011-04-27 10:05:43 +00:00
|
|
|
//! [3]
|
|
|
|
|
|
|
|
updateComboBox(fileComboBox);
|
|
|
|
updateComboBox(textComboBox);
|
|
|
|
updateComboBox(directoryComboBox);
|
|
|
|
|
|
|
|
//! [4]
|
2016-07-05 11:26:53 +00:00
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
currentDir = QDir(path);
|
|
|
|
QStringList files;
|
2016-07-05 11:26:53 +00:00
|
|
|
findRecursion(path, fileName.isEmpty() ? QStringLiteral("*") : fileName, &files);
|
2011-04-27 10:05:43 +00:00
|
|
|
if (!text.isEmpty())
|
|
|
|
files = findFiles(files, text);
|
|
|
|
showFiles(files);
|
|
|
|
}
|
|
|
|
//! [4]
|
|
|
|
|
2016-07-05 11:26:53 +00:00
|
|
|
void Window::animateFindClick()
|
|
|
|
{
|
|
|
|
findButton->animateClick();
|
|
|
|
}
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
//! [5]
|
|
|
|
QStringList Window::findFiles(const QStringList &files, const QString &text)
|
|
|
|
{
|
|
|
|
QProgressDialog progressDialog(this);
|
|
|
|
progressDialog.setCancelButtonText(tr("&Cancel"));
|
|
|
|
progressDialog.setRange(0, files.size());
|
|
|
|
progressDialog.setWindowTitle(tr("Find Files"));
|
|
|
|
|
|
|
|
//! [5] //! [6]
|
2016-07-05 11:26:53 +00:00
|
|
|
QMimeDatabase mimeDatabase;
|
2011-04-27 10:05:43 +00:00
|
|
|
QStringList foundFiles;
|
|
|
|
|
|
|
|
for (int i = 0; i < files.size(); ++i) {
|
|
|
|
progressDialog.setValue(i);
|
2016-07-05 11:26:53 +00:00
|
|
|
progressDialog.setLabelText(tr("Searching file number %1 of %n...", 0, files.size()).arg(i));
|
|
|
|
QCoreApplication::processEvents();
|
2011-04-27 10:05:43 +00:00
|
|
|
//! [6]
|
|
|
|
|
|
|
|
if (progressDialog.wasCanceled())
|
|
|
|
break;
|
|
|
|
|
|
|
|
//! [7]
|
2016-07-05 11:26:53 +00:00
|
|
|
const QString fileName = files.at(i);
|
|
|
|
const QMimeType mimeType = mimeDatabase.mimeTypeForFile(fileName);
|
|
|
|
if (mimeType.isValid() && !mimeType.inherits(QStringLiteral("text/plain"))) {
|
|
|
|
qWarning() << "Not searching binary file " << QDir::toNativeSeparators(fileName);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
QFile file(fileName);
|
2011-04-27 10:05:43 +00:00
|
|
|
if (file.open(QIODevice::ReadOnly)) {
|
|
|
|
QString line;
|
|
|
|
QTextStream in(&file);
|
|
|
|
while (!in.atEnd()) {
|
|
|
|
if (progressDialog.wasCanceled())
|
|
|
|
break;
|
|
|
|
line = in.readLine();
|
2016-07-05 11:26:53 +00:00
|
|
|
if (line.contains(text, Qt::CaseInsensitive)) {
|
2011-04-27 10:05:43 +00:00
|
|
|
foundFiles << files[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return foundFiles;
|
|
|
|
}
|
|
|
|
//! [7]
|
|
|
|
|
|
|
|
//! [8]
|
|
|
|
void Window::showFiles(const QStringList &files)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < files.size(); ++i) {
|
2016-07-05 11:26:53 +00:00
|
|
|
const QString &fileName = files.at(i);
|
|
|
|
const QString toolTip = QDir::toNativeSeparators(fileName);
|
|
|
|
const QString relativePath = QDir::toNativeSeparators(currentDir.relativeFilePath(fileName));
|
|
|
|
const qint64 size = QFileInfo(fileName).size();
|
|
|
|
QTableWidgetItem *fileNameItem = new QTableWidgetItem(relativePath);
|
|
|
|
fileNameItem->setData(absoluteFileNameRole, QVariant(fileName));
|
|
|
|
fileNameItem->setToolTip(toolTip);
|
2011-04-27 10:05:43 +00:00
|
|
|
fileNameItem->setFlags(fileNameItem->flags() ^ Qt::ItemIsEditable);
|
|
|
|
QTableWidgetItem *sizeItem = new QTableWidgetItem(tr("%1 KB")
|
|
|
|
.arg(int((size + 1023) / 1024)));
|
2016-07-05 11:26:53 +00:00
|
|
|
sizeItem->setData(absoluteFileNameRole, QVariant(fileName));
|
|
|
|
sizeItem->setToolTip(toolTip);
|
2011-04-27 10:05:43 +00:00
|
|
|
sizeItem->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter);
|
|
|
|
sizeItem->setFlags(sizeItem->flags() ^ Qt::ItemIsEditable);
|
|
|
|
|
|
|
|
int row = filesTable->rowCount();
|
|
|
|
filesTable->insertRow(row);
|
|
|
|
filesTable->setItem(row, 0, fileNameItem);
|
|
|
|
filesTable->setItem(row, 1, sizeItem);
|
|
|
|
}
|
2016-07-05 11:26:53 +00:00
|
|
|
filesFoundLabel->setText(tr("%n file(s) found (Double click on a file to open it)", 0, files.size()));
|
2011-04-27 17:16:41 +00:00
|
|
|
filesFoundLabel->setWordWrap(true);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
//! [8]
|
|
|
|
|
|
|
|
//! [10]
|
|
|
|
QComboBox *Window::createComboBox(const QString &text)
|
|
|
|
{
|
|
|
|
QComboBox *comboBox = new QComboBox;
|
|
|
|
comboBox->setEditable(true);
|
|
|
|
comboBox->addItem(text);
|
|
|
|
comboBox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
|
|
|
|
return comboBox;
|
|
|
|
}
|
|
|
|
//! [10]
|
|
|
|
|
|
|
|
//! [11]
|
|
|
|
void Window::createFilesTable()
|
|
|
|
{
|
|
|
|
filesTable = new QTableWidget(0, 2);
|
|
|
|
filesTable->setSelectionBehavior(QAbstractItemView::SelectRows);
|
|
|
|
|
|
|
|
QStringList labels;
|
2011-04-27 17:16:41 +00:00
|
|
|
labels << tr("Filename") << tr("Size");
|
2011-04-27 10:05:43 +00:00
|
|
|
filesTable->setHorizontalHeaderLabels(labels);
|
2012-04-20 14:55:59 +00:00
|
|
|
filesTable->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch);
|
2011-04-27 10:05:43 +00:00
|
|
|
filesTable->verticalHeader()->hide();
|
|
|
|
filesTable->setShowGrid(false);
|
2016-07-05 11:26:53 +00:00
|
|
|
//! [15]
|
|
|
|
filesTable->setContextMenuPolicy(Qt::CustomContextMenu);
|
|
|
|
connect(filesTable, &QTableWidget::customContextMenuRequested,
|
|
|
|
this, &Window::contextMenu);
|
2015-07-21 12:41:36 +00:00
|
|
|
connect(filesTable, &QTableWidget::cellActivated,
|
|
|
|
this, &Window::openFileOfItem);
|
2016-07-05 11:26:53 +00:00
|
|
|
//! [15]
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
//! [11]
|
|
|
|
|
2016-07-05 11:26:53 +00:00
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
//! [12]
|
|
|
|
|
|
|
|
void Window::openFileOfItem(int row, int /* column */)
|
|
|
|
{
|
2016-07-05 11:26:53 +00:00
|
|
|
const QTableWidgetItem *item = filesTable->item(row, 0);
|
|
|
|
openFile(fileNameOfItem(item));
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//! [12]
|
|
|
|
|
2016-07-05 11:26:53 +00:00
|
|
|
//! [16]
|
|
|
|
void Window::contextMenu(const QPoint &pos)
|
|
|
|
{
|
|
|
|
const QTableWidgetItem *item = filesTable->itemAt(pos);
|
|
|
|
if (!item)
|
|
|
|
return;
|
|
|
|
QMenu menu;
|
2016-09-02 13:42:53 +00:00
|
|
|
#ifndef QT_NO_CLIPBOARD
|
2016-07-05 11:26:53 +00:00
|
|
|
QAction *copyAction = menu.addAction("Copy Name");
|
2016-09-02 13:42:53 +00:00
|
|
|
#endif
|
2016-07-05 11:26:53 +00:00
|
|
|
QAction *openAction = menu.addAction("Open");
|
|
|
|
QAction *action = menu.exec(filesTable->mapToGlobal(pos));
|
|
|
|
if (!action)
|
|
|
|
return;
|
|
|
|
const QString fileName = fileNameOfItem(item);
|
2016-09-02 13:42:53 +00:00
|
|
|
if (action == openAction)
|
2016-07-05 11:26:53 +00:00
|
|
|
openFile(fileName);
|
2016-09-02 13:42:53 +00:00
|
|
|
#ifndef QT_NO_CLIPBOARD
|
|
|
|
else if (action == copyAction)
|
|
|
|
QGuiApplication::clipboard()->setText(QDir::toNativeSeparators(fileName));
|
|
|
|
#endif
|
2016-07-05 11:26:53 +00:00
|
|
|
}
|
|
|
|
//! [16]
|