author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Tue, 02 Feb 2010 00:43:10 +0200 | |
changeset 3 | 41300fa6a67c |
parent 0 | 1918ee327afb |
child 4 | 3b1da2848fc7 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
3 |
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
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 <QtCore/QDir> |
|
43 |
#include <QtGui/QLayout> |
|
44 |
#include <QtGui/QComboBox> |
|
45 |
#include <QtGui/QTreeWidget> |
|
46 |
#include <QtGui/QLayout> |
|
47 |
#include <QtGui/QFileDialog> |
|
48 |
#include <QtGui/QDialogButtonBox> |
|
49 |
#include <QtGui/QToolButton> |
|
50 |
#include <QtGui/QPushButton> |
|
51 |
#include <QtGui/QLabel> |
|
52 |
||
53 |
#include "findfiledialog.h" |
|
54 |
#include "assistant.h" |
|
55 |
#include "textedit.h" |
|
56 |
||
57 |
//! [0] |
|
58 |
FindFileDialog::FindFileDialog(TextEdit *editor, Assistant *assistant) |
|
59 |
: QDialog(editor) |
|
60 |
{ |
|
61 |
currentAssistant = assistant; |
|
62 |
currentEditor = editor; |
|
63 |
//! [0] |
|
64 |
||
65 |
createButtons(); |
|
66 |
createComboBoxes(); |
|
67 |
createFilesTree(); |
|
68 |
createLabels(); |
|
69 |
createLayout(); |
|
70 |
||
71 |
directoryComboBox->addItem(QDir::toNativeSeparators(QDir::currentPath())); |
|
72 |
fileNameComboBox->addItem("*"); |
|
73 |
findFiles(); |
|
74 |
||
75 |
setWindowTitle(tr("Find File")); |
|
76 |
//! [1] |
|
77 |
} |
|
78 |
//! [1] |
|
79 |
||
80 |
void FindFileDialog::browse() |
|
81 |
{ |
|
82 |
QString currentDirectory = directoryComboBox->currentText(); |
|
83 |
QString newDirectory = QFileDialog::getExistingDirectory(this, |
|
84 |
tr("Select Directory"), currentDirectory); |
|
85 |
if (!newDirectory.isEmpty()) { |
|
86 |
directoryComboBox->addItem(QDir::toNativeSeparators(newDirectory)); |
|
87 |
directoryComboBox->setCurrentIndex(directoryComboBox->count() - 1); |
|
88 |
update(); |
|
89 |
} |
|
90 |
} |
|
91 |
||
92 |
//! [2] |
|
93 |
void FindFileDialog::help() |
|
94 |
{ |
|
95 |
currentAssistant->showDocumentation("filedialog.html"); |
|
96 |
} |
|
97 |
//! [2] |
|
98 |
||
99 |
void FindFileDialog::openFile(QTreeWidgetItem *item) |
|
100 |
{ |
|
101 |
if (!item) { |
|
102 |
item = foundFilesTree->currentItem(); |
|
103 |
if (!item) |
|
104 |
return; |
|
105 |
} |
|
106 |
||
107 |
QString fileName = item->text(0); |
|
108 |
QString path = directoryComboBox->currentText() + QDir::separator(); |
|
109 |
||
110 |
currentEditor->setContents(path + fileName); |
|
111 |
close(); |
|
112 |
} |
|
113 |
||
114 |
void FindFileDialog::update() |
|
115 |
{ |
|
116 |
findFiles(); |
|
117 |
buttonBox->button(QDialogButtonBox::Open)->setEnabled( |
|
118 |
foundFilesTree->topLevelItemCount() > 0); |
|
119 |
} |
|
120 |
||
121 |
void FindFileDialog::findFiles() |
|
122 |
{ |
|
123 |
QRegExp filePattern(fileNameComboBox->currentText() + "*"); |
|
124 |
filePattern.setPatternSyntax(QRegExp::Wildcard); |
|
125 |
||
126 |
QDir directory(directoryComboBox->currentText()); |
|
127 |
||
128 |
QStringList allFiles = directory.entryList(QDir::Files | QDir::NoSymLinks); |
|
129 |
QStringList matchingFiles; |
|
130 |
||
131 |
foreach (QString file, allFiles) { |
|
132 |
if (filePattern.exactMatch(file)) |
|
133 |
matchingFiles << file; |
|
134 |
} |
|
135 |
showFiles(matchingFiles); |
|
136 |
} |
|
137 |
||
138 |
void FindFileDialog::showFiles(const QStringList &files) |
|
139 |
{ |
|
140 |
foundFilesTree->clear(); |
|
141 |
||
142 |
for (int i = 0; i < files.count(); ++i) { |
|
143 |
QTreeWidgetItem *item = new QTreeWidgetItem(foundFilesTree); |
|
144 |
item->setText(0, files[i]); |
|
145 |
} |
|
146 |
||
147 |
if (files.count() > 0) |
|
148 |
foundFilesTree->setCurrentItem(foundFilesTree->topLevelItem(0)); |
|
149 |
} |
|
150 |
||
151 |
void FindFileDialog::createButtons() |
|
152 |
{ |
|
153 |
browseButton = new QToolButton; |
|
154 |
browseButton->setText(tr("...")); |
|
155 |
connect(browseButton, SIGNAL(clicked()), this, SLOT(browse())); |
|
156 |
||
157 |
buttonBox = new QDialogButtonBox(QDialogButtonBox::Open |
|
158 |
| QDialogButtonBox::Cancel |
|
159 |
| QDialogButtonBox::Help); |
|
160 |
connect(buttonBox, SIGNAL(accepted()), this, SLOT(openFile())); |
|
161 |
connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject())); |
|
162 |
connect(buttonBox, SIGNAL(helpRequested()), this, SLOT(help())); |
|
163 |
} |
|
164 |
||
165 |
void FindFileDialog::createComboBoxes() |
|
166 |
{ |
|
167 |
directoryComboBox = new QComboBox; |
|
168 |
fileNameComboBox = new QComboBox; |
|
169 |
||
170 |
fileNameComboBox->setEditable(true); |
|
171 |
fileNameComboBox->setSizePolicy(QSizePolicy::Expanding, |
|
172 |
QSizePolicy::Preferred); |
|
173 |
||
174 |
directoryComboBox->setMinimumContentsLength(30); |
|
175 |
directoryComboBox->setSizeAdjustPolicy( |
|
176 |
QComboBox::AdjustToMinimumContentsLength); |
|
177 |
directoryComboBox->setSizePolicy(QSizePolicy::Expanding, |
|
178 |
QSizePolicy::Preferred); |
|
179 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
180 |
connect(fileNameComboBox, SIGNAL(editTextChanged(QString)), |
0 | 181 |
this, SLOT(update())); |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
182 |
connect(directoryComboBox, SIGNAL(currentIndexChanged(QString)), |
0 | 183 |
this, SLOT(update())); |
184 |
} |
|
185 |
||
186 |
void FindFileDialog::createFilesTree() |
|
187 |
{ |
|
188 |
foundFilesTree = new QTreeWidget; |
|
189 |
foundFilesTree->setColumnCount(1); |
|
190 |
foundFilesTree->setHeaderLabels(QStringList(tr("Matching Files"))); |
|
191 |
foundFilesTree->setRootIsDecorated(false); |
|
192 |
foundFilesTree->setSelectionMode(QAbstractItemView::SingleSelection); |
|
193 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
194 |
connect(foundFilesTree, SIGNAL(itemActivated(QTreeWidgetItem*,int)), |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
195 |
this, SLOT(openFile(QTreeWidgetItem*))); |
0 | 196 |
} |
197 |
||
198 |
void FindFileDialog::createLabels() |
|
199 |
{ |
|
200 |
directoryLabel = new QLabel(tr("Search in:")); |
|
201 |
fileNameLabel = new QLabel(tr("File name (including wildcards):")); |
|
202 |
} |
|
203 |
||
204 |
void FindFileDialog::createLayout() |
|
205 |
{ |
|
206 |
QHBoxLayout *fileLayout = new QHBoxLayout; |
|
207 |
fileLayout->addWidget(fileNameLabel); |
|
208 |
fileLayout->addWidget(fileNameComboBox); |
|
209 |
||
210 |
QHBoxLayout *directoryLayout = new QHBoxLayout; |
|
211 |
directoryLayout->addWidget(directoryLabel); |
|
212 |
directoryLayout->addWidget(directoryComboBox); |
|
213 |
directoryLayout->addWidget(browseButton); |
|
214 |
||
215 |
QVBoxLayout *mainLayout = new QVBoxLayout; |
|
216 |
mainLayout->addLayout(fileLayout); |
|
217 |
mainLayout->addLayout(directoryLayout); |
|
218 |
mainLayout->addWidget(foundFilesTree); |
|
219 |
mainLayout->addStretch(); |
|
220 |
mainLayout->addWidget(buttonBox); |
|
221 |
setLayout(mainLayout); |
|
222 |
} |