source: trunk/examples/help/simpletextviewer/findfiledialog.cpp@ 1147

Last change on this file since 1147 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.2 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 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: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 <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]
57FindFileDialog::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
79void 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]
92void FindFileDialog::help()
93{
94 currentAssistant->showDocumentation("filedialog.html");
95}
96//! [2]
97
98void 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
113void FindFileDialog::update()
114{
115 findFiles();
116 buttonBox->button(QDialogButtonBox::Open)->setEnabled(
117 foundFilesTree->topLevelItemCount() > 0);
118}
119
120void 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
137void 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
150void 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
164void 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(QString)),
180 this, SLOT(update()));
181 connect(directoryComboBox, SIGNAL(currentIndexChanged(QString)),
182 this, SLOT(update()));
183}
184
185void 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
197void FindFileDialog::createLabels()
198{
199 directoryLabel = new QLabel(tr("Search in:"));
200 fileNameLabel = new QLabel(tr("File name (including wildcards):"));
201}
202
203void 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}
Note: See TracBrowser for help on using the repository browser.