source: trunk/examples/dialogs/findfiles/window.cpp@ 275

Last change on this file since 275 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 7.8 KB
Line 
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 "window.h"
45
46//! [0]
47Window::Window(QWidget *parent)
48 : QDialog(parent)
49{
50 browseButton = createButton(tr("&Browse..."), SLOT(browse()));
51 findButton = createButton(tr("&Find"), SLOT(find()));
52
53 fileComboBox = createComboBox(tr("*"));
54 textComboBox = createComboBox();
55 directoryComboBox = createComboBox(QDir::currentPath());
56
57 fileLabel = new QLabel(tr("Named:"));
58 textLabel = new QLabel(tr("Containing text:"));
59 directoryLabel = new QLabel(tr("In directory:"));
60 filesFoundLabel = new QLabel;
61
62 createFilesTable();
63//! [0]
64
65//! [1]
66 QHBoxLayout *buttonsLayout = new QHBoxLayout;
67 buttonsLayout->addStretch();
68 buttonsLayout->addWidget(findButton);
69
70 QGridLayout *mainLayout = new QGridLayout;
71 mainLayout->addWidget(fileLabel, 0, 0);
72 mainLayout->addWidget(fileComboBox, 0, 1, 1, 2);
73 mainLayout->addWidget(textLabel, 1, 0);
74 mainLayout->addWidget(textComboBox, 1, 1, 1, 2);
75 mainLayout->addWidget(directoryLabel, 2, 0);
76 mainLayout->addWidget(directoryComboBox, 2, 1);
77 mainLayout->addWidget(browseButton, 2, 2);
78 mainLayout->addWidget(filesTable, 3, 0, 1, 3);
79 mainLayout->addWidget(filesFoundLabel, 4, 0);
80 mainLayout->addLayout(buttonsLayout, 5, 0, 1, 3);
81 setLayout(mainLayout);
82
83 setWindowTitle(tr("Find Files"));
84 resize(700, 300);
85}
86//! [1]
87
88//! [2]
89void Window::browse()
90{
91 QString directory = QFileDialog::getExistingDirectory(this,
92 tr("Find Files"), QDir::currentPath());
93
94 if (!directory.isEmpty()) {
95 if (directoryComboBox->findText(directory) == -1)
96 directoryComboBox->addItem(directory);
97 directoryComboBox->setCurrentIndex(directoryComboBox->findText(directory));
98 }
99}
100//! [2]
101
102static void updateComboBox(QComboBox *comboBox)
103{
104 if (comboBox->findText(comboBox->currentText()) == -1)
105 comboBox->addItem(comboBox->currentText());
106}
107
108//! [3]
109void Window::find()
110{
111 filesTable->setRowCount(0);
112
113 QString fileName = fileComboBox->currentText();
114 QString text = textComboBox->currentText();
115 QString path = directoryComboBox->currentText();
116//! [3]
117
118 updateComboBox(fileComboBox);
119 updateComboBox(textComboBox);
120 updateComboBox(directoryComboBox);
121
122//! [4]
123 currentDir = QDir(path);
124 QStringList files;
125 if (fileName.isEmpty())
126 fileName = "*";
127 files = currentDir.entryList(QStringList(fileName),
128 QDir::Files | QDir::NoSymLinks);
129
130 if (!text.isEmpty())
131 files = findFiles(files, text);
132 showFiles(files);
133}
134//! [4]
135
136//! [5]
137QStringList Window::findFiles(const QStringList &files, const QString &text)
138{
139 QProgressDialog progressDialog(this);
140 progressDialog.setCancelButtonText(tr("&Cancel"));
141 progressDialog.setRange(0, files.size());
142 progressDialog.setWindowTitle(tr("Find Files"));