author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Mon, 04 Oct 2010 01:19:32 +0300 | |
changeset 37 | 758a864f9613 |
parent 30 | 5dc02b23752f |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
18
2f34d5167611
Revision: 201011
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 |
** |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
9 |
** $QT_BEGIN_LICENSE:BSD$ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
10 |
** You may use this file under the terms of the BSD license as follows: |
0 | 11 |
** |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
12 |
** "Redistribution and use in source and binary forms, with or without |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
13 |
** modification, are permitted provided that the following conditions are |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
14 |
** met: |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
15 |
** * Redistributions of source code must retain the above copyright |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
16 |
** notice, this list of conditions and the following disclaimer. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
17 |
** * Redistributions in binary form must reproduce the above copyright |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
18 |
** notice, this list of conditions and the following disclaimer in |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
19 |
** the documentation and/or other materials provided with the |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
20 |
** distribution. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
21 |
** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
22 |
** the names of its contributors may be used to endorse or promote |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
23 |
** products derived from this software without specific prior written |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
24 |
** permission. |
0 | 25 |
** |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
26 |
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
27 |
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
28 |
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
29 |
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
30 |
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
31 |
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
32 |
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
33 |
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
34 |
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
35 |
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
36 |
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
0 | 37 |
** $QT_END_LICENSE$ |
38 |
** |
|
39 |
****************************************************************************/ |
|
40 |
||
41 |
#include <QtCore/QDir> |
|
42 |
#include <QtGui/QLayout> |
|
43 |
#include <QtGui/QComboBox> |
|
44 |
#include <QtGui/QTreeWidget> |
|
45 |
#include <QtGui/QLayout> |
|
46 |
#include <QtGui/QFileDialog> |
|
47 |
#include <QtGui/QDialogButtonBox> |
|
48 |
#include <QtGui/QToolButton> |
|
49 |
#include <QtGui/QPushButton> |
|
50 |
#include <QtGui/QLabel> |
|
51 |
||
52 |
#include "findfiledialog.h" |
|
53 |
#include "assistant.h" |
|
54 |
#include "textedit.h" |
|
55 |
||
56 |
//! [0] |
|
57 |
FindFileDialog::FindFileDialog(TextEdit *editor, Assistant *assistant) |
|
58 |
: QDialog(editor) |
|
59 |
{ |
|
60 |
currentAssistant = assistant; |
|
61 |
currentEditor = editor; |
|
62 |
//! [0] |
|
63 |
||
64 |
createButtons(); |
|
65 |
createComboBoxes(); |
|
66 |
createFilesTree(); |
|
67 |
createLabels(); |
|
68 |
createLayout(); |
|
69 |
||
70 |
directoryComboBox->addItem(QDir::toNativeSeparators(QDir::currentPath())); |
|
71 |
fileNameComboBox->addItem("*"); |
|
72 |
findFiles(); |
|
73 |
||
74 |
setWindowTitle(tr("Find File")); |
|
75 |
//! [1] |
|
76 |
} |
|
77 |
//! [1] |
|
78 |
||
79 |
void FindFileDialog::browse() |
|
80 |
{ |
|
81 |
QString currentDirectory = directoryComboBox->currentText(); |
|
82 |
QString newDirectory = QFileDialog::getExistingDirectory(this, |
|
83 |
tr("Select Directory"), currentDirectory); |
|
84 |
if (!newDirectory.isEmpty()) { |
|
85 |
directoryComboBox->addItem(QDir::toNativeSeparators(newDirectory)); |
|
86 |
directoryComboBox->setCurrentIndex(directoryComboBox->count() - 1); |
|
87 |
update(); |
|
88 |
} |
|
89 |
} |
|
90 |
||
91 |
//! [2] |
|
92 |
void FindFileDialog::help() |
|
93 |
{ |
|
94 |
currentAssistant->showDocumentation("filedialog.html"); |
|
95 |
} |
|
96 |
//! [2] |
|
97 |
||
98 |
void FindFileDialog::openFile(QTreeWidgetItem *item) |
|
99 |
{ |
|
100 |
if (!item) { |
|
101 |
item = foundFilesTree->currentItem(); |
|
102 |
if (!item) |
|
103 |
return; |
|
104 |
} |
|
105 |
||
106 |
QString fileName = item->text(0); |
|
107 |
QString path = directoryComboBox->currentText() + QDir::separator(); |
|
108 |
||
109 |
currentEditor->setContents(path + fileName); |
|
110 |
close(); |
|
111 |
} |
|
112 |
||
113 |
void FindFileDialog::update() |
|
114 |
{ |
|
115 |
findFiles(); |
|
116 |
buttonBox->button(QDialogButtonBox::Open)->setEnabled( |
|
117 |
foundFilesTree->topLevelItemCount() > 0); |
|
118 |
} |
|
119 |
||
120 |
void FindFileDialog::findFiles() |
|
121 |
{ |
|
122 |
QRegExp filePattern(fileNameComboBox->currentText() + "*"); |
|
123 |
filePattern.setPatternSyntax(QRegExp::Wildcard); |
|
124 |
||
125 |
QDir directory(directoryComboBox->currentText()); |
|
126 |
||
127 |
QStringList allFiles = directory.entryList(QDir::Files | QDir::NoSymLinks); |
|
128 |
QStringList matchingFiles; |
|
129 |
||
130 |
foreach (QString file, allFiles) { |
|
131 |
if (filePattern.exactMatch(file)) |
|
132 |
matchingFiles << file; |
|
133 |
} |
|
134 |
showFiles(matchingFiles); |
|
135 |
} |
|
136 |
||
137 |
void FindFileDialog::showFiles(const QStringList &files) |
|
138 |
{ |
|
139 |
foundFilesTree->clear(); |
|
140 |
||
141 |
for (int i = 0; i < files.count(); ++i) { |
|
142 |
QTreeWidgetItem *item = new QTreeWidgetItem(foundFilesTree); |
|
143 |
item->setText(0, files[i]); |
|
144 |
} |
|
145 |
||
146 |
if (files.count() > 0) |
|
147 |
foundFilesTree->setCurrentItem(foundFilesTree->topLevelItem(0)); |
|
148 |
} |
|
149 |
||
150 |
void FindFileDialog::createButtons() |
|
151 |
{ |
|
152 |
browseButton = new QToolButton; |
|
153 |
browseButton->setText(tr("...")); |
|
154 |
connect(browseButton, SIGNAL(clicked()), this, SLOT(browse())); |
|
155 |
||
156 |
buttonBox = new QDialogButtonBox(QDialogButtonBox::Open |
|
157 |
| QDialogButtonBox::Cancel |
|
158 |
| QDialogButtonBox::Help); |
|
159 |
connect(buttonBox, SIGNAL(accepted()), this, SLOT(openFile())); |
|
160 |
connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject())); |
|
161 |
connect(buttonBox, SIGNAL(helpRequested()), this, SLOT(help())); |
|
162 |
} |
|
163 |
||
164 |
void FindFileDialog::createComboBoxes() |
|
165 |
{ |
|
166 |
directoryComboBox = new QComboBox; |
|
167 |
fileNameComboBox = new QComboBox; |
|
168 |
||
169 |
fileNameComboBox->setEditable(true); |
|
170 |
fileNameComboBox->setSizePolicy(QSizePolicy::Expanding, |
|
171 |
QSizePolicy::Preferred); |
|
172 |
||
173 |
directoryComboBox->setMinimumContentsLength(30); |
|
174 |
directoryComboBox->setSizeAdjustPolicy( |
|
175 |
QComboBox::AdjustToMinimumContentsLength); |
|
176 |
directoryComboBox->setSizePolicy(QSizePolicy::Expanding, |
|
177 |
QSizePolicy::Preferred); |
|
178 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
179 |
connect(fileNameComboBox, SIGNAL(editTextChanged(QString)), |
0 | 180 |
this, SLOT(update())); |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
181 |
connect(directoryComboBox, SIGNAL(currentIndexChanged(QString)), |
0 | 182 |
this, SLOT(update())); |
183 |
} |
|
184 |
||
185 |
void FindFileDialog::createFilesTree() |
|
186 |
{ |
|
187 |
foundFilesTree = new QTreeWidget; |
|
188 |
foundFilesTree->setColumnCount(1); |
|
189 |
foundFilesTree->setHeaderLabels(QStringList(tr("Matching Files"))); |
|
190 |
foundFilesTree->setRootIsDecorated(false); |
|
191 |
foundFilesTree->setSelectionMode(QAbstractItemView::SingleSelection); |
|
192 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
193 |
connect(foundFilesTree, SIGNAL(itemActivated(QTreeWidgetItem*,int)), |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
194 |
this, SLOT(openFile(QTreeWidgetItem*))); |
0 | 195 |
} |
196 |
||
197 |
void FindFileDialog::createLabels() |
|
198 |
{ |
|
199 |
directoryLabel = new QLabel(tr("Search in:")); |
|
200 |
fileNameLabel = new QLabel(tr("File name (including wildcards):")); |
|
201 |
} |
|
202 |
||
203 |
void FindFileDialog::createLayout() |
|
204 |
{ |
|
205 |
QHBoxLayout *fileLayout = new QHBoxLayout; |
|
206 |
fileLayout->addWidget(fileNameLabel); |
|
207 |
fileLayout->addWidget(fileNameComboBox); |
|
208 |
||
209 |
QHBoxLayout *directoryLayout = new QHBoxLayout; |
|
210 |
directoryLayout->addWidget(directoryLabel); |
|
211 |
directoryLayout->addWidget(directoryComboBox); |
|
212 |
directoryLayout->addWidget(browseButton); |
|
213 |
||
214 |
QVBoxLayout *mainLayout = new QVBoxLayout; |
|
215 |
mainLayout->addLayout(fileLayout); |
|
216 |
mainLayout->addLayout(directoryLayout); |
|
217 |
mainLayout->addWidget(foundFilesTree); |
|
218 |
mainLayout->addStretch(); |
|
219 |
mainLayout->addWidget(buttonBox); |
|
220 |
setLayout(mainLayout); |
|
221 |
} |