author | Eckhart Koeppen <eckhart.koppen@nokia.com> |
Wed, 21 Apr 2010 12:15:23 +0300 | |
branch | RCL_3 |
changeset 12 | cc75c76972ee |
parent 4 | 3b1da2848fc7 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the examples of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
15 |
** |
|
16 |
** GNU Lesser General Public License Usage |
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 |
** General Public License version 2.1 as published by the Free Software |
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 |
** packaging of this file. Please review the following information to |
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 |
** |
|
24 |
** In addition, as a special exception, Nokia gives you certain additional |
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
||
42 |
#include <QtGui> |
|
43 |
||
44 |
#include "window.h" |
|
45 |
||
46 |
//! [0] |
|
47 |
Window::Window(QWidget *parent) |
|
48 |
: QDialog(parent) |
|
49 |
{ |
|
50 |
browseButton = createButton(tr("&Browse..."), SLOT(browse())); |
|
51 |
findButton = createButton(tr("&Find"), SLOT(find())); |
|
52 |
||
53 |
fileComboBox = createComboBox(tr("*")); |
|
54 |
textComboBox = createComboBox(); |
|
55 |
directoryComboBox = createComboBox(QDir::currentPath()); |
|
56 |
||
57 |
fileLabel = new QLabel(tr("Named:")); |
|
58 |
textLabel = new QLabel(tr("Containing text:")); |
|
59 |
directoryLabel = new QLabel(tr("In directory:")); |
|
60 |
filesFoundLabel = new QLabel; |
|
61 |
||
62 |
createFilesTable(); |
|
63 |
//! [0] |
|
64 |
||
65 |
//! [1] |
|
66 |
QHBoxLayout *buttonsLayout = new QHBoxLayout; |
|
67 |
buttonsLayout->addStretch(); |
|
68 |
buttonsLayout->addWidget(findButton); |
|
69 |
||
70 |
QGridLayout *mainLayout = new QGridLayout; |
|
71 |
mainLayout->addWidget(fileLabel, 0, 0); |
|
72 |
mainLayout->addWidget(fileComboBox, 0, 1, 1, 2); |
|
73 |
mainLayout->addWidget(textLabel, 1, 0); |
|
74 |
mainLayout->addWidget(textComboBox, 1, 1, 1, 2); |
|
75 |
mainLayout->addWidget(directoryLabel, 2, 0); |
|
76 |
mainLayout->addWidget(directoryComboBox, 2, 1); |
|
77 |
mainLayout->addWidget(browseButton, 2, 2); |
|
78 |
mainLayout->addWidget(filesTable, 3, 0, 1, 3); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
79 |
mainLayout->addWidget(filesFoundLabel, 4, 0, 1, 3); |
0 | 80 |
mainLayout->addLayout(buttonsLayout, 5, 0, 1, 3); |
81 |
setLayout(mainLayout); |
|
82 |
||
83 |
setWindowTitle(tr("Find Files")); |
|
84 |
resize(700, 300); |
|
85 |
} |
|
86 |
//! [1] |
|
87 |
||
88 |
//! [2] |
|
89 |
void Window::browse() |
|
90 |
{ |
|
91 |
QString directory = QFileDialog::getExistingDirectory(this, |
|
92 |
tr("Find Files"), QDir::currentPath()); |
|
93 |
||
94 |
if (!directory.isEmpty()) { |
|
95 |
if (directoryComboBox->findText(directory) == -1) |
|
96 |
directoryComboBox->addItem(directory); |
|
97 |
directoryComboBox->setCurrentIndex(directoryComboBox->findText(directory)); |
|
98 |
} |
|
99 |
} |
|
100 |
//! [2] |
|
101 |
||
102 |
static void updateComboBox(QComboBox *comboBox) |
|
103 |
{ |
|
104 |
if (comboBox->findText(comboBox->currentText()) == -1) |
|
105 |
comboBox->addItem(comboBox->currentText()); |
|
106 |
} |
|
107 |
||
108 |
//! [3] |
|
109 |
void Window::find() |
|
110 |
{ |
|
111 |
filesTable->setRowCount(0); |
|
112 |
||
113 |
QString fileName = fileComboBox->currentText(); |
|
114 |
QString text = textComboBox->currentText(); |
|
115 |
QString path = directoryComboBox->currentText(); |
|
116 |
//! [3] |
|
117 |
||
118 |
updateComboBox(fileComboBox); |
|
119 |
updateComboBox(textComboBox); |
|
120 |
updateComboBox(directoryComboBox); |
|
121 |
||
122 |
//! [4] |
|
123 |
currentDir = QDir(path); |
|
124 |
QStringList files; |
|
125 |
if (fileName.isEmpty()) |
|
126 |
fileName = "*"; |
|
127 |
files = currentDir.entryList(QStringList(fileName), |
|
128 |
QDir::Files | QDir::NoSymLinks); |
|
129 |
||
130 |
if (!text.isEmpty()) |
|
131 |
files = findFiles(files, text); |
|
132 |
showFiles(files); |
|
133 |
} |
|
134 |
//! [4] |
|
135 |
||
136 |
//! [5] |
|
137 |
QStringList Window::findFiles(const QStringList &files, const QString &text) |
|
138 |
{ |
|
139 |
QProgressDialog progressDialog(this); |
|
140 |
progressDialog.setCancelButtonText(tr("&Cancel")); |
|
141 |
progressDialog.setRange(0, files.size()); |
|
142 |
progressDialog.setWindowTitle(tr("Find Files")); |
|
143 |
||
144 |
//! [5] //! [6] |
|
145 |
QStringList foundFiles; |
|
146 |
||
147 |
for (int i = 0; i < files.size(); ++i) { |
|
148 |
progressDialog.setValue(i); |
|
149 |
progressDialog.setLabelText(tr("Searching file number %1 of %2...") |
|
150 |
.arg(i).arg(files.size())); |
|
151 |
qApp->processEvents(); |
|
152 |
//! [6] |
|
153 |
||
154 |
if (progressDialog.wasCanceled()) |
|
155 |
break; |
|
156 |
||
157 |
//! [7] |
|
158 |
QFile file(currentDir.absoluteFilePath(files[i])); |
|
159 |
||
160 |
if (file.open(QIODevice::ReadOnly)) { |
|
161 |
QString line; |
|
162 |
QTextStream in(&file); |
|
163 |
while (!in.atEnd()) { |
|
164 |
if (progressDialog.wasCanceled()) |
|
165 |
break; |
|
166 |
line = in.readLine(); |
|
167 |
if (line.contains(text)) { |
|
168 |
foundFiles << files[i]; |
|
169 |
break; |
|
170 |
} |
|
171 |
} |
|
172 |
} |
|
173 |
} |
|
174 |
return foundFiles; |
|
175 |
} |
|
176 |
//! [7] |
|
177 |
||
178 |
//! [8] |
|
179 |
void Window::showFiles(const QStringList &files) |
|
180 |
{ |
|
181 |
for (int i = 0; i < files.size(); ++i) { |
|
182 |
QFile file(currentDir.absoluteFilePath(files[i])); |
|
183 |
qint64 size = QFileInfo(file).size(); |
|
184 |
||
185 |
QTableWidgetItem *fileNameItem = new QTableWidgetItem(files[i]); |
|
186 |
fileNameItem->setFlags(fileNameItem->flags() ^ Qt::ItemIsEditable); |
|
187 |
QTableWidgetItem *sizeItem = new QTableWidgetItem(tr("%1 KB") |
|
188 |
.arg(int((size + 1023) / 1024))); |
|
189 |
sizeItem->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter); |
|
190 |
sizeItem->setFlags(sizeItem->flags() ^ Qt::ItemIsEditable); |
|
191 |
||
192 |
int row = filesTable->rowCount(); |
|
193 |
filesTable->insertRow(row); |
|
194 |
filesTable->setItem(row, 0, fileNameItem); |
|
195 |
filesTable->setItem(row, 1, sizeItem); |
|
196 |
} |
|
197 |
filesFoundLabel->setText(tr("%1 file(s) found").arg(files.size()) + |
|
198 |
(" (Double click on a file to open it)")); |
|
199 |
} |
|
200 |
//! [8] |
|
201 |
||
202 |
//! [9] |
|
203 |
QPushButton *Window::createButton(const QString &text, const char *member) |
|
204 |
{ |
|
205 |
QPushButton *button = new QPushButton(text); |
|
206 |
connect(button, SIGNAL(clicked()), this, member); |
|
207 |
return button; |
|
208 |
} |
|
209 |
//! [9] |
|
210 |
||
211 |
//! [10] |
|
212 |
QComboBox *Window::createComboBox(const QString &text) |
|
213 |
{ |
|
214 |
QComboBox *comboBox = new QComboBox; |
|
215 |
comboBox->setEditable(true); |
|
216 |
comboBox->addItem(text); |
|
217 |
comboBox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred); |
|
218 |
return comboBox; |
|
219 |
} |
|
220 |
//! [10] |
|
221 |
||
222 |
//! [11] |
|
223 |
void Window::createFilesTable() |
|
224 |
{ |
|
225 |
filesTable = new QTableWidget(0, 2); |
|
226 |
filesTable->setSelectionBehavior(QAbstractItemView::SelectRows); |
|
227 |
||
228 |
QStringList labels; |
|
229 |
labels << tr("File Name") << tr("Size"); |
|
230 |
filesTable->setHorizontalHeaderLabels(labels); |
|
231 |
filesTable->horizontalHeader()->setResizeMode(0, QHeaderView::Stretch); |
|
232 |
filesTable->verticalHeader()->hide(); |
|
233 |
filesTable->setShowGrid(false); |
|
234 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
235 |
connect(filesTable, SIGNAL(cellActivated(int,int)), |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
236 |
this, SLOT(openFileOfItem(int,int))); |
0 | 237 |
} |
238 |
//! [11] |
|
239 |
||
240 |
//! [12] |
|
241 |
||
242 |
void Window::openFileOfItem(int row, int /* column */) |
|
243 |
{ |
|
244 |
QTableWidgetItem *item = filesTable->item(row, 0); |
|
245 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
246 |
QDesktopServices::openUrl(QUrl::fromLocalFile(currentDir.absoluteFilePath(item->text()))); |
0 | 247 |
} |
248 |
||
249 |
//! [12] |
|
250 |