| 1 | /**************************************************************************** | 
|---|
| 2 | ** | 
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). | 
|---|
| 4 | ** Contact: Qt Software Information (qt-info@nokia.com) | 
|---|
| 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 qt-sales@nokia.com. | 
|---|
| 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 |  | 
|---|
| 180 | connect(fileNameComboBox, SIGNAL(editTextChanged(const QString &)), | 
|---|
| 181 | this, SLOT(update())); | 
|---|
| 182 | connect(directoryComboBox, SIGNAL(currentIndexChanged(const QString &)), | 
|---|
| 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 |  | 
|---|
| 194 | connect(foundFilesTree, SIGNAL(itemActivated(QTreeWidgetItem *, int)), | 
|---|
| 195 | this, SLOT(openFile(QTreeWidgetItem *))); | 
|---|
| 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 | } | 
|---|