1
/****************************************************************************
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4
** Contact: http://www.qt-project.org/legal
6
** This file is part of the examples of the Qt Toolkit.
8
** $QT_BEGIN_LICENSE:BSD$
9
** You may use this file under the terms of the BSD license as follows:
11
** "Redistribution and use in source and binary forms, with or without
12
** modification, are permitted provided that the following conditions are
14
** * Redistributions of source code must retain the above copyright
15
** notice, this list of conditions and the following disclaimer.
16
** * Redistributions in binary form must reproduce the above copyright
17
** notice, this list of conditions and the following disclaimer in
18
** the documentation and/or other materials provided with the
20
** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
21
** of its contributors may be used to endorse or promote products derived
22
** from this software without specific prior written permission.
25
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
39
****************************************************************************/
46
Window::Window(QWidget *parent)
49
browseButton = createButton(tr("&Browse..."), SLOT(browse()));
50
findButton = createButton(tr("&Find"), SLOT(find()));
52
fileComboBox = createComboBox(tr("*"));
53
textComboBox = createComboBox();
54
directoryComboBox = createComboBox(QDir::currentPath());
56
fileLabel = new QLabel(tr("Named:"));
57
textLabel = new QLabel(tr("Containing text:"));
58
directoryLabel = new QLabel(tr("In directory:"));
59
filesFoundLabel = new QLabel;
65
QGridLayout *mainLayout = new QGridLayout;
66
mainLayout->setSizeConstraint(QLayout::SetNoConstraint);
67
mainLayout->addWidget(fileLabel, 0, 0);
68
mainLayout->addWidget(fileComboBox, 0, 1, 1, 2);
69
mainLayout->addWidget(textLabel, 1, 0);
70
mainLayout->addWidget(textComboBox, 1, 1, 1, 2);
71
mainLayout->addWidget(directoryLabel, 2, 0);
72
mainLayout->addWidget(directoryComboBox, 2, 1);
73
mainLayout->addWidget(browseButton, 2, 2);
74
mainLayout->addWidget(filesTable, 3, 0, 1, 3);
75
mainLayout->addWidget(filesFoundLabel, 4, 0, 1, 2);
76
mainLayout->addWidget(findButton, 4, 2);
77
setLayout(mainLayout);
79
setWindowTitle(tr("Find Files"));
87
QString directory = QFileDialog::getExistingDirectory(this,
88
tr("Find Files"), QDir::currentPath());
90
if (!directory.isEmpty()) {
91
if (directoryComboBox->findText(directory) == -1)
92
directoryComboBox->addItem(directory);
93
directoryComboBox->setCurrentIndex(directoryComboBox->findText(directory));
98
static void updateComboBox(QComboBox *comboBox)
100
if (comboBox->findText(comboBox->currentText()) == -1)
101
comboBox->addItem(comboBox->currentText());
107
filesTable->setRowCount(0);
109
QString fileName = fileComboBox->currentText();
110
QString text = textComboBox->currentText();
111
QString path = directoryComboBox->currentText();
114
updateComboBox(fileComboBox);
115
updateComboBox(textComboBox);
116
updateComboBox(directoryComboBox);
119
currentDir = QDir(path);
121
if (fileName.isEmpty())
123
files = currentDir.entryList(QStringList(fileName),
124
QDir::Files | QDir::NoSymLinks);
127
files = findFiles(files, text);
133
QStringList Window::findFiles(const QStringList &files, const QString &text)
135
QProgressDialog progressDialog(this);
136
progressDialog.setCancelButtonText(tr("&Cancel"));
137
progressDialog.setRange(0, files.size());
138
progressDialog.setWindowTitle(tr("Find Files"));
141
QStringList foundFiles;
143
for (int i = 0; i < files.size(); ++i) {
144
progressDialog.setValue(i);
145
progressDialog.setLabelText(tr("Searching file number %1 of %2...")
146
.arg(i).arg(files.size()));
147
qApp->processEvents();
150
if (progressDialog.wasCanceled())
154
QFile file(currentDir.absoluteFilePath(files[i]));
156
if (file.open(QIODevice::ReadOnly)) {
158
QTextStream in(&file);
159
while (!in.atEnd()) {
160
if (progressDialog.wasCanceled())
162
line = in.readLine();
163
if (line.contains(text)) {
164
foundFiles << files[i];
175
void Window::showFiles(const QStringList &files)
177
for (int i = 0; i < files.size(); ++i) {
178
QFile file(currentDir.absoluteFilePath(files[i]));
179
qint64 size = QFileInfo(file).size();
181
QTableWidgetItem *fileNameItem = new QTableWidgetItem(files[i]);
182
fileNameItem->setFlags(fileNameItem->flags() ^ Qt::ItemIsEditable);
183
QTableWidgetItem *sizeItem = new QTableWidgetItem(tr("%1 KB")
184
.arg(int((size + 1023) / 1024)));
185
sizeItem->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter);
186
sizeItem->setFlags(sizeItem->flags() ^ Qt::ItemIsEditable);
188
int row = filesTable->rowCount();
189
filesTable->insertRow(row);
190
filesTable->setItem(row, 0, fileNameItem);
191
filesTable->setItem(row, 1, sizeItem);
193
filesFoundLabel->setText(tr("%1 file(s) found").arg(files.size()) +
194
(" (Double click on a file to open it)"));
195
filesFoundLabel->setWordWrap(true);
200
QPushButton *Window::createButton(const QString &text, const char *member)
202
QPushButton *button = new QPushButton(text);
203
connect(button, SIGNAL(clicked()), this, member);
209
QComboBox *Window::createComboBox(const QString &text)
211
QComboBox *comboBox = new QComboBox;
212
comboBox->setEditable(true);
213
comboBox->addItem(text);
214
comboBox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
220
void Window::createFilesTable()
222
filesTable = new QTableWidget(0, 2);
223
filesTable->setSelectionBehavior(QAbstractItemView::SelectRows);
226
labels << tr("Filename") << tr("Size");
227
filesTable->setHorizontalHeaderLabels(labels);
228
filesTable->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch);
229
filesTable->verticalHeader()->hide();
230
filesTable->setShowGrid(false);
232
connect(filesTable, SIGNAL(cellActivated(int,int)),
233
this, SLOT(openFileOfItem(int,int)));
239
void Window::openFileOfItem(int row, int /* column */)
241
QTableWidgetItem *item = filesTable->item(row, 0);
243
QDesktopServices::openUrl(QUrl::fromLocalFile(currentDir.absoluteFilePath(item->text())));