source: trunk/examples/dialogs/findfiles/window.cpp@ 846

Last change on this file since 846 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 7.9 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the examples of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:BSD$
10** You may use this file under the terms of the BSD license as follows:
11**
12** "Redistribution and use in source and binary forms, with or without
13** modification, are permitted provided that the following conditions are
14** met:
15** * Redistributions of source code must retain the above copyright
16** notice, this list of conditions and the following disclaimer.
17** * Redistributions in binary form must reproduce the above copyright
18** notice, this list of conditions and the following disclaimer in
19** the documentation and/or other materials provided with the
20** distribution.
21** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
22** the names of its contributors may be used to endorse or promote
23** products derived from this software without specific prior written
24** permission.
25**
26** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
37** $QT_END_LICENSE$
38**
39****************************************************************************/
40
41#include <QtGui>
42
43#include "window.h"
44
45//! [0]
46Window::Window(QWidget *parent)
47 : QDialog(parent)
48{
49 browseButton = createButton(tr("&Browse..."), SLOT(browse()));
50 findButton = createButton(tr("&Find"), SLOT(find()));
51
52 fileComboBox = createComboBox(tr("*"));
53 textComboBox = createComboBox();
54 directoryComboBox = createComboBox(QDir::currentPath());
55
56 fileLabel = new QLabel(tr("Named:"));
57 textLabel = new QLabel(tr("Containing text:"));
58 directoryLabel = new QLabel(tr("In directory:"));
59 filesFoundLabel = new QLabel;
60
61 createFilesTable();
62//! [0]
63
64//! [1]
65 QHBoxLayout *buttonsLayout = new QHBoxLayout;
66 buttonsLayout->addStretch();
67 buttonsLayout->addWidget(findButton);
68
69 QGridLayout *mainLayout = new QGridLayout;
70 mainLayout->addWidget(fileLabel, 0, 0);
71 mainLayout->addWidget(fileComboBox, 0, 1, 1, 2);
72 mainLayout->addWidget(textLabel, 1, 0);
73 mainLayout->addWidget(textComboBox, 1, 1, 1, 2);
74 mainLayout->addWidget(directoryLabel, 2, 0);
75 mainLayout->addWidget(directoryComboBox, 2, 1);
76 mainLayout->addWidget(browseButton, 2, 2);
77 mainLayout->addWidget(filesTable, 3, 0, 1, 3);
78 mainLayout->addWidget(filesFoundLabel, 4, 0, 1, 3);
79 mainLayout->addLayout(buttonsLayout, 5, 0, 1, 3);
80 setLayout(mainLayout);
81
82 setWindowTitle(tr("Find Files"));
83 resize(700, 300);
84}
85//! [1]
86
87//! [2]
88void Window::browse()
89{
90 QString directory = QFileDialog::getExistingDirectory(this,
91 tr("Find Files"), QDir::currentPath());
92
93 if (!directory.isEmpty()) {
94 if (directoryComboBox->findText(directory) == -1)
95 directoryComboBox->addItem(directory);
96 directoryComboBox->setCurrentIndex(directoryComboBox->findText(directory));
97 }
98}
99//! [2]
100
101static void updateComboBox(QComboBox *comboBox)
102{
103 if (comboBox->findText(comboBox->currentText()) == -1)
104 comboBox->addItem(comboBox->currentText());
105}
106
107//! [3]
108void Window::find()
109{
110 filesTable->setRowCount(0);
111
112 QString fileName = fileComboBox->currentText();
113 QString text = textComboBox->currentText();
114 QString path = directoryComboBox->currentText();
115//! [3]
116
117 updateComboBox(fileComboBox);
118 updateComboBox(textComboBox);
119 updateComboBox(directoryComboBox);
120
121//! [4]
122 currentDir = QDir(path);
123 QStringList files;
124 if (fileName.isEmpty())
125 fileName = "*";
126 files = currentDir.entryList(QStringList(fileName),
127 QDir::Files | QDir::NoSymLinks);
128
129 if (!text.isEmpty())
130 files = findFiles(files, text);
131 showFiles(files);
132}
133//! [4]
134
135//! [5]
136QStringList Window::findFiles(const QStringList &files, const QString &text)
137{
138 QProgressDialog progressDialog(this);
139 progressDialog.setCancelButtonText(tr("&Cancel"));
140 progressDialog.setRange(0, files.size());
141 progressDialog.setWindowTitle(tr("Find Files"));
142
143//! [5] //! [6]
144 QStringList foundFiles;
145
146 for (int i = 0; i < files.size(); ++i) {
147 progressDialog.setValue(i);
148 progressDialog.setLabelText(tr("Searching file number %1 of %2...")
149 .arg(i).arg(files.size()));
150 qApp->processEvents();
151//! [6]
152
153 if (progressDialog.wasCanceled())
154 break;
155
156//! [7]
157 QFile file(currentDir.absoluteFilePath(files[i]));
158
159 if (file.open(QIODevice::ReadOnly)) {
160 QString line;
161 QTextStream in(&file);
162 while (!in.atEnd()) {
163 if (progressDialog.wasCanceled())
164 break;
165 line = in.readLine();
166 if (line.contains(text)) {
167 foundFiles << files[i];
168 break;
169 }
170 }
171 }
172 }
173 return foundFiles;
174}
175//! [7]
176
177//! [8]
178void Window::showFiles(const QStringList &files)
179{
180 for (int i = 0; i < files.size(); ++i) {
181 QFile file(currentDir.absoluteFilePath(files[i]));
182 qint64 size = QFileInfo(file).size();
183
184 QTableWidgetItem *fileNameItem = new QTableWidgetItem(files[i]);
185 fileNameItem->setFlags(fileNameItem->flags() ^ Qt::ItemIsEditable);
186 QTableWidgetItem *sizeItem = new QTableWidgetItem(tr("%1 KB")
187 .arg(int((size + 1023) / 1024)));
188 sizeItem->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter);
189 sizeItem->setFlags(sizeItem->flags() ^ Qt::ItemIsEditable);
190
191 int row = filesTable->rowCount();
192 filesTable->insertRow(row);
193 filesTable->setItem(row, 0, fileNameItem);
194 filesTable->setItem(row, 1, sizeItem);
195 }
196 filesFoundLabel->setText(tr("%1 file(s) found").arg(files.size()) +
197 (" (Double click on a file to open it)"));
198}
199//! [8]
200
201//! [9]
202QPushButton *Window::createButton(const QString &text, const char *member)
203{
204 QPushButton *button = new QPushButton(text);
205 connect(button, SIGNAL(clicked()), this, member);
206 return button;
207}
208//! [9]
209
210//! [10]
211QComboBox *Window::createComboBox(const QString &text)
212{
213 QComboBox *comboBox = new QComboBox;
214 comboBox->setEditable(true);
215 comboBox->addItem(text);
216 comboBox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
217 return comboBox;
218}
219//! [10]
220
221//! [11]
222void Window::createFilesTable()
223{
224 filesTable = new QTableWidget(0, 2);
225 filesTable->setSelectionBehavior(QAbstractItemView::SelectRows);
226
227 QStringList labels;
228 labels << tr("File Name") << tr("Size");
229 filesTable->setHorizontalHeaderLabels(labels);
230 filesTable->horizontalHeader()->setResizeMode(0, QHeaderView::Stretch);
231 filesTable->verticalHeader()->hide();
232 filesTable->setShowGrid(false);
233
234 connect(filesTable, SIGNAL(cellActivated(int,int)),
235 this, SLOT(openFileOfItem(int,int)));
236}
237//! [11]
238
239//! [12]
240
241void Window::openFileOfItem(int row, int /* column */)
242{
243 QTableWidgetItem *item = filesTable->item(row, 0);
244
245 QDesktopServices::openUrl(QUrl::fromLocalFile(currentDir.absoluteFilePath(item->text())));
246}
247
248//! [12]
249
Note: See TracBrowser for help on using the repository browser.