~ubuntu-branches/ubuntu/wily/qtbase-opensource-src/wily

« back to all changes in this revision

Viewing changes to examples/widgets/dialogs/findfiles/window.cpp

  • Committer: Package Import Robot
  • Author(s): Timo Jyrinki
  • Date: 2013-02-05 12:46:17 UTC
  • Revision ID: package-import@ubuntu.com-20130205124617-c8jouts182j002fx
Tags: upstream-5.0.1+dfsg
ImportĀ upstreamĀ versionĀ 5.0.1+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
 
4
** Contact: http://www.qt-project.org/legal
 
5
**
 
6
** This file is part of the examples of the Qt Toolkit.
 
7
**
 
8
** $QT_BEGIN_LICENSE:BSD$
 
9
** You may use this file under the terms of the BSD license as follows:
 
10
**
 
11
** "Redistribution and use in source and binary forms, with or without
 
12
** modification, are permitted provided that the following conditions are
 
13
** met:
 
14
**   * Redistributions of source code must retain the above copyright
 
15
**     notice, this list of conditions and the following disclaimer.
 
16
**   * Redistributions in binary form must reproduce the above copyright
 
17
**     notice, this list of conditions and the following disclaimer in
 
18
**     the documentation and/or other materials provided with the
 
19
**     distribution.
 
20
**   * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
 
21
**     of its contributors may be used to endorse or promote products derived
 
22
**     from this software without specific prior written permission.
 
23
**
 
24
**
 
25
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 
26
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 
27
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 
28
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 
29
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 
30
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 
31
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 
32
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 
33
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
34
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 
35
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
 
36
**
 
37
** $QT_END_LICENSE$
 
38
**
 
39
****************************************************************************/
 
40
 
 
41
#include <QtWidgets>
 
42
 
 
43
#include "window.h"
 
44
 
 
45
//! [0]
 
46
Window::Window(QWidget *parent)
 
47
    : QWidget(parent)
 
48
{
 
49
    browseButton = createButton(tr("&Browse..."), SLOT(browse()));
 
50
    findButton = createButton(tr("&Find"), SLOT(find()));
 
51
 
 
52
    fileComboBox = createComboBox(tr("*"));
 
53
    textComboBox = createComboBox();
 
54
    directoryComboBox = createComboBox(QDir::currentPath());
 
55
 
 
56
    fileLabel = new QLabel(tr("Named:"));
 
57
    textLabel = new QLabel(tr("Containing text:"));
 
58
    directoryLabel = new QLabel(tr("In directory:"));
 
59
    filesFoundLabel = new QLabel;
 
60
 
 
61
    createFilesTable();
 
62
//! [0]
 
63
 
 
64
//! [1]
 
65
    QGridLayout *mainLayout = new QGridLayout;
 
66
    mainLayout->setSizeConstraint(QLayout::SetNoConstraint);
 
67
    mainLayout->addWidget(fileLabel, 0, 0);
 
68
    mainLayout->addWidget(fileComboBox, 0, 1, 1, 2);
 
69
    mainLayout->addWidget(textLabel, 1, 0);
 
70
    mainLayout->addWidget(textComboBox, 1, 1, 1, 2);
 
71
    mainLayout->addWidget(directoryLabel, 2, 0);
 
72
    mainLayout->addWidget(directoryComboBox, 2, 1);
 
73
    mainLayout->addWidget(browseButton, 2, 2);
 
74
    mainLayout->addWidget(filesTable, 3, 0, 1, 3);
 
75
    mainLayout->addWidget(filesFoundLabel, 4, 0, 1, 2);
 
76
    mainLayout->addWidget(findButton, 4, 2);
 
77
    setLayout(mainLayout);
 
78
 
 
79
    setWindowTitle(tr("Find Files"));
 
80
    resize(700, 300);
 
81
}
 
82
//! [1]
 
83
 
 
84
//! [2]
 
85
void Window::browse()
 
86
{
 
87
    QString directory = QFileDialog::getExistingDirectory(this,
 
88
                               tr("Find Files"), QDir::currentPath());
 
89
 
 
90
    if (!directory.isEmpty()) {
 
91
        if (directoryComboBox->findText(directory) == -1)
 
92
            directoryComboBox->addItem(directory);
 
93
        directoryComboBox->setCurrentIndex(directoryComboBox->findText(directory));
 
94
    }
 
95
}
 
96
//! [2]
 
97
 
 
98
static void updateComboBox(QComboBox *comboBox)
 
99
{
 
100
    if (comboBox->findText(comboBox->currentText()) == -1)
 
101
        comboBox->addItem(comboBox->currentText());
 
102
}
 
103
 
 
104
//! [3]
 
105
void Window::find()
 
106
{
 
107
    filesTable->setRowCount(0);
 
108
 
 
109
    QString fileName = fileComboBox->currentText();
 
110
    QString text = textComboBox->currentText();
 
111
    QString path = directoryComboBox->currentText();
 
112
//! [3]
 
113
 
 
114
    updateComboBox(fileComboBox);
 
115
    updateComboBox(textComboBox);
 
116
    updateComboBox(directoryComboBox);
 
117
 
 
118
//! [4]
 
119
    currentDir = QDir(path);
 
120
    QStringList files;
 
121
    if (fileName.isEmpty())
 
122
        fileName = "*";
 
123
    files = currentDir.entryList(QStringList(fileName),
 
124
                                 QDir::Files | QDir::NoSymLinks);
 
125
 
 
126
    if (!text.isEmpty())
 
127
        files = findFiles(files, text);
 
128
    showFiles(files);
 
129
}
 
130
//! [4]
 
131
 
 
132
//! [5]
 
133
QStringList Window::findFiles(const QStringList &files, const QString &text)
 
134
{
 
135
    QProgressDialog progressDialog(this);
 
136
    progressDialog.setCancelButtonText(tr("&Cancel"));
 
137
    progressDialog.setRange(0, files.size());
 
138
    progressDialog.setWindowTitle(tr("Find Files"));
 
139
 
 
140
//! [5] //! [6]
 
141
    QStringList foundFiles;
 
142
 
 
143
    for (int i = 0; i < files.size(); ++i) {
 
144
        progressDialog.setValue(i);
 
145
        progressDialog.setLabelText(tr("Searching file number %1 of %2...")
 
146
                                    .arg(i).arg(files.size()));
 
147
        qApp->processEvents();
 
148
//! [6]
 
149
 
 
150
        if (progressDialog.wasCanceled())
 
151
            break;
 
152
 
 
153
//! [7]
 
154
        QFile file(currentDir.absoluteFilePath(files[i]));
 
155
 
 
156
        if (file.open(QIODevice::ReadOnly)) {
 
157
            QString line;
 
158
            QTextStream in(&file);
 
159
            while (!in.atEnd()) {
 
160
                if (progressDialog.wasCanceled())
 
161
                    break;
 
162
                line = in.readLine();
 
163
                if (line.contains(text)) {
 
164
                    foundFiles << files[i];
 
165
                    break;
 
166
                }
 
167
            }
 
168
        }
 
169
    }
 
170
    return foundFiles;
 
171
}
 
172
//! [7]
 
173
 
 
174
//! [8]
 
175
void Window::showFiles(const QStringList &files)
 
176
{
 
177
    for (int i = 0; i < files.size(); ++i) {
 
178
        QFile file(currentDir.absoluteFilePath(files[i]));
 
179
        qint64 size = QFileInfo(file).size();
 
180
 
 
181
        QTableWidgetItem *fileNameItem = new QTableWidgetItem(files[i]);
 
182
        fileNameItem->setFlags(fileNameItem->flags() ^ Qt::ItemIsEditable);
 
183
        QTableWidgetItem *sizeItem = new QTableWidgetItem(tr("%1 KB")
 
184
                                             .arg(int((size + 1023) / 1024)));
 
185
        sizeItem->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter);
 
186
        sizeItem->setFlags(sizeItem->flags() ^ Qt::ItemIsEditable);
 
187
 
 
188
        int row = filesTable->rowCount();
 
189
        filesTable->insertRow(row);
 
190
        filesTable->setItem(row, 0, fileNameItem);
 
191
        filesTable->setItem(row, 1, sizeItem);
 
192
    }
 
193
    filesFoundLabel->setText(tr("%1 file(s) found").arg(files.size()) +
 
194
                             (" (Double click on a file to open it)"));
 
195
    filesFoundLabel->setWordWrap(true);
 
196
}
 
197
//! [8]
 
198
 
 
199
//! [9]
 
200
QPushButton *Window::createButton(const QString &text, const char *member)
 
201
{
 
202
    QPushButton *button = new QPushButton(text);
 
203
    connect(button, SIGNAL(clicked()), this, member);
 
204
    return button;
 
205
}
 
206
//! [9]
 
207
 
 
208
//! [10]
 
209
QComboBox *Window::createComboBox(const QString &text)
 
210
{
 
211
    QComboBox *comboBox = new QComboBox;
 
212
    comboBox->setEditable(true);
 
213
    comboBox->addItem(text);
 
214
    comboBox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
 
215
    return comboBox;
 
216
}
 
217
//! [10]
 
218
 
 
219
//! [11]
 
220
void Window::createFilesTable()
 
221
{
 
222
    filesTable = new QTableWidget(0, 2);
 
223
    filesTable->setSelectionBehavior(QAbstractItemView::SelectRows);
 
224
 
 
225
    QStringList labels;
 
226
    labels << tr("Filename") << tr("Size");
 
227
    filesTable->setHorizontalHeaderLabels(labels);
 
228
    filesTable->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch);
 
229
    filesTable->verticalHeader()->hide();
 
230
    filesTable->setShowGrid(false);
 
231
 
 
232
    connect(filesTable, SIGNAL(cellActivated(int,int)),
 
233
            this, SLOT(openFileOfItem(int,int)));
 
234
}
 
235
//! [11]
 
236
 
 
237
//! [12]
 
238
 
 
239
void Window::openFileOfItem(int row, int /* column */)
 
240
{
 
241
    QTableWidgetItem *item = filesTable->item(row, 0);
 
242
 
 
243
    QDesktopServices::openUrl(QUrl::fromLocalFile(currentDir.absoluteFilePath(item->text())));
 
244
}
 
245
 
 
246
//! [12]
 
247