1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** Contact: Qt Software Information ([email protected])
|
---|
5 | **
|
---|
6 | ** This file is part of the examples of the Qt Toolkit.
|
---|
7 | **
|
---|
8 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
9 | ** Commercial Usage
|
---|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
11 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
12 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
13 | ** a written agreement between you and Nokia.
|
---|
14 | **
|
---|
15 | ** GNU Lesser General Public License Usage
|
---|
16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
17 | ** General Public License version 2.1 as published by the Free Software
|
---|
18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
19 | ** packaging of this file. Please review the following information to
|
---|
20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
22 | **
|
---|
23 | ** In addition, as a special exception, Nokia gives you certain
|
---|
24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
---|
25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
---|
26 | ** package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you are unsure which license is appropriate for your use, please
|
---|
37 | ** contact the sales department at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | #include <QtGui>
|
---|
43 |
|
---|
44 | #include "findfiledialog.h"
|
---|
45 |
|
---|
46 | //! [0]
|
---|
47 | FindFileDialog::FindFileDialog(QTextEdit *editor, QAssistantClient *assistant,
|
---|
48 | QWidget *parent)
|
---|
49 | : QDialog(parent)
|
---|
50 | {
|
---|
51 | currentAssistantClient = assistant;
|
---|
52 | currentEditor = editor;
|
---|
53 | //! [0]
|
---|
54 |
|
---|
55 | createButtons();
|
---|
56 | createComboBoxes();
|
---|
57 | createFilesTree();
|
---|
58 | createLabels();
|
---|
59 | createLayout();
|
---|
60 |
|
---|
61 | directoryComboBox->addItem(QDir::toNativeSeparators(QDir::currentPath()));
|
---|
62 | fileNameComboBox->addItem("*");
|
---|
63 | findFiles();
|
---|
64 |
|
---|
65 | setWindowTitle(tr("Find File"));
|
---|
66 | //! [1]
|
---|
67 | }
|
---|
68 | //! [1]
|
---|
69 |
|
---|
70 | void FindFileDialog::browse()
|
---|
71 | {
|
---|
72 | QString currentDirectory = directoryComboBox->currentText();
|
---|
73 | QString newDirectory = QFileDialog::getExistingDirectory(this,
|
---|
74 | tr("Select Directory"), currentDirectory);
|
---|
75 | if (!newDirectory.isEmpty()) {
|
---|
76 | directoryComboBox->addItem(QDir::toNativeSeparators(newDirectory));
|
---|
77 | directoryComboBox->setCurrentIndex(directoryComboBox->count() - 1);
|
---|
78 | update();
|
---|
79 | }
|
---|
80 | }
|
---|
81 |
|
---|
82 | //! [2]
|
---|
83 | void FindFileDialog::help()
|
---|
84 | {
|
---|
85 | currentAssistantClient->showPage(QLibraryInfo::location(QLibraryInfo::ExamplesPath) +
|
---|
86 | QDir::separator() + "assistant/simpletextviewer/documentation/filedialog.html");
|
---|
87 | }
|
---|
88 | //! [2]
|
---|
89 |
|
---|
90 | void FindFileDialog::openFile(QTreeWidgetItem *item)
|
---|
91 | {
|
---|
92 | if (!item) {
|
---|
93 | item = foundFilesTree->currentItem();
|
---|
94 | if (!item)
|
---|
95 | return;
|
---|
96 | }
|
---|
97 |
|
---|
98 | QString fileName = item->text(0);
|
---|
99 | QString path = directoryComboBox->currentText() + QDir::separator();
|
---|
100 |
|
---|
101 | QFile file(path + fileName);
|
---|
102 | if (file.open(QIODevice::ReadOnly)) {
|
---|
103 | QString data(file.readAll());
|
---|
104 |
|
---|
105 | if (fileName.endsWith(".html"))
|
---|
106 | currentEditor->setHtml(data);
|
---|
107 | else
|
---|
108 | currentEditor->setPlainText(data);
|
---|
109 | }
|
---|
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 |
|
---|
179 | connect(fileNameComboBox, SIGNAL(editTextChanged(const QString &)),
|
---|
180 | this, SLOT(update()));
|
---|
181 | connect(directoryComboBox, SIGNAL(currentIndexChanged(const QString &)),
|
---|
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 |
|
---|
193 | connect(foundFilesTree, SIGNAL(itemActivated(QTreeWidgetItem *, int)),
|
---|
194 | this, SLOT(openFile(QTreeWidgetItem *)));
|
---|
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 | }
|
---|