directoryfilter.cpp 8.84 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2
3
4
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
5
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
6
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
con's avatar
con committed
8
**
9
** Commercial Usage
10
**
11
12
13
14
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
15
**
16
** GNU Lesser General Public License Usage
17
**
18
19
20
21
22
23
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24
**
25
** If you are unsure which license is appropriate for your use, please
hjk's avatar
hjk committed
26
** contact the sales department at http://qt.nokia.com/contact.
con's avatar
con committed
27
**
28
**************************************************************************/
hjk's avatar
hjk committed
29

con's avatar
con committed
30
31
32
33
34
35
36
37
#include "directoryfilter.h"

#include <QtCore/QDir>
#include <QtCore/QStack>
#include <QtGui/QCompleter>
#include <QtGui/QFileDialog>
#include <QtGui/QMessageBox>

38
39
#include <qtconcurrent/QtConcurrentTools>

con's avatar
con committed
40
41
using namespace Locator;
using namespace Locator::Internal;
con's avatar
con committed
42

43
44
DirectoryFilter::DirectoryFilter()
  : m_name(tr("Generic Directory Filter")),
45
    m_filters(QStringList() << QLatin1String("*.h") << QLatin1String("*.cpp")
dt's avatar
dt committed
46
47
                            << QLatin1String("*.ui") << QLatin1String("*.qrc")),
    m_dialog(0)
con's avatar
con committed
48
49
50
51
52
53
54
55
56
57
58
59
60
{
    setIncludedByDefault(true);
}

QByteArray DirectoryFilter::saveState() const
{
    QMutexLocker locker(&m_lock);
    QByteArray value;
    QDataStream out(&value, QIODevice::WriteOnly);
    out << m_name;
    out << m_directories;
    out << m_filters;
    out << shortcutString();
61
    out << isIncludedByDefault();
62
    out << files();
con's avatar
con committed
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
    return value;
}

bool DirectoryFilter::restoreState(const QByteArray &state)
{
    QMutexLocker locker(&m_lock);

    QStringList dirEntries;
    QString shortcut;
    bool defaultFilter;

    QDataStream in(state);
    in >> m_name;
    in >> dirEntries;
    in >> m_filters;
    in >> shortcut;
    in >> defaultFilter;
80
    in >> files();
con's avatar
con committed
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116

    setShortcutString(shortcut);
    setIncludedByDefault(defaultFilter);

    // ### TODO throw that out again:
    // clear the list of directories of empty entries (which might at least happen at a format change)
    m_directories.clear();
    foreach (const QString &dir, dirEntries) {
        if (dir.isEmpty())
            continue;
        m_directories.append(dir);
    }

    generateFileNames();
    return true;
}

bool DirectoryFilter::openConfigDialog(QWidget *parent, bool &needsRefresh)
{
    bool success = false;
    QDialog dialog(parent);
    m_dialog = &dialog;
    m_ui.setupUi(&dialog);
    dialog.setWindowTitle(tr("Filter Configuration"));
    connect(m_ui.addButton, SIGNAL(clicked()),
            this, SLOT(addDirectory()), Qt::DirectConnection);
    connect(m_ui.editButton, SIGNAL(clicked()),
            this, SLOT(editDirectory()), Qt::DirectConnection);
    connect(m_ui.removeButton, SIGNAL(clicked()),
            this, SLOT(removeDirectory()), Qt::DirectConnection);
    connect(m_ui.directoryList, SIGNAL(itemSelectionChanged()),
            this, SLOT(updateOptionButtons()), Qt::DirectConnection);
    m_ui.nameEdit->setText(m_name);
    m_ui.nameEdit->selectAll();
    m_ui.directoryList->clear();
    m_ui.directoryList->addItems(m_directories);
117
    m_ui.fileTypeEdit->setText(m_filters.join(QString(QLatin1Char(','))));
con's avatar
con committed
118
    m_ui.shortcutEdit->setText(shortcutString());
119
    m_ui.defaultFlag->setChecked(!isIncludedByDefault());
con's avatar
con committed
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
    updateOptionButtons();
    if (dialog.exec() == QDialog::Accepted) {
        QMutexLocker locker(&m_lock);
        bool directoriesChanged = false;
        QStringList oldDirectories = m_directories;
        QStringList oldFilters = m_filters;
        m_name = m_ui.nameEdit->text().trimmed();
        m_directories.clear();
        int oldCount = oldDirectories.count();
        int newCount = m_ui.directoryList->count();
        if (oldCount != newCount)
            directoriesChanged = true;
        for (int i = 0; i < newCount; ++i) {
            m_directories.append(m_ui.directoryList->item(i)->text());
            if (!directoriesChanged && m_directories.at(i) != oldDirectories.at(i))
                directoriesChanged = true;
        }
137
        m_filters = m_ui.fileTypeEdit->text().trimmed().split(QLatin1Char(','));
con's avatar
con committed
138
139
140
141
142
143
144
145
146
147
148
        setShortcutString(m_ui.shortcutEdit->text().trimmed());
        setIncludedByDefault(!m_ui.defaultFlag->isChecked());
        if (directoriesChanged || oldFilters != m_filters)
            needsRefresh = true;
        success = true;
    }
    return success;
}

void DirectoryFilter::addDirectory()
{
149
    QString dir = QFileDialog::getExistingDirectory(m_dialog, tr("Select Directory"));
con's avatar
con committed
150
151
152
153
154
155
156
157
158
159
    if (!dir.isEmpty()) {
        m_ui.directoryList->addItem(dir);
    }
}

void DirectoryFilter::editDirectory()
{
    if (m_ui.directoryList->selectedItems().count() < 1)
        return;
    QListWidgetItem *currentItem = m_ui.directoryList->selectedItems().at(0);
160
    QString dir = QFileDialog::getExistingDirectory(m_dialog, tr("Select Directory"),
con's avatar
con committed
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
                                                    currentItem->text());
    if (!dir.isEmpty()) {
        currentItem->setText(dir);
    }
}

void DirectoryFilter::removeDirectory()
{
    if (m_ui.directoryList->selectedItems().count() < 1)
        return;
    QListWidgetItem *currentItem = m_ui.directoryList->selectedItems().at(0);
    delete m_ui.directoryList->takeItem(m_ui.directoryList->row(currentItem));
}

void DirectoryFilter::updateOptionButtons()
{
    bool haveSelectedItem = (m_ui.directoryList->selectedItems().count() > 0);
    m_ui.editButton->setEnabled(haveSelectedItem);
    m_ui.removeButton->setEnabled(haveSelectedItem);
}

void DirectoryFilter::refresh(QFutureInterface<void> &future)
{
    const int MAX = 360;
    future.setProgressRange(0, MAX);
    if (m_directories.count() < 1) {
        QMutexLocker locker(&m_lock);
188
        files().clear();
con's avatar
con committed
189
190
191
192
193
194
        generateFileNames();
        future.setProgressValueAndText(MAX, tr("%1 filter update: 0 files").arg(m_name));
        return;
    }
    int progress = 0;
    int MAX_PER = MAX;
195
    QStringList filesFound;
con's avatar
con committed
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
    QStack<QDir> dirs;
    QStack<int> progressValues;
    QStack<bool> processedValues;
    { // initialize
        QMutexLocker locker(&m_lock);
        MAX_PER = MAX/m_directories.count();
        foreach (const QString &directoryEntry, m_directories) {
            if (!directoryEntry.isEmpty()) {
                dirs.push(QDir(directoryEntry));
                progressValues.push(MAX_PER);
                processedValues.push(false);
            }
        }
    }
    while (!dirs.isEmpty() && !future.isCanceled()) {
        if (future.isProgressUpdateNeeded()) {
212
            future.setProgressValueAndText(progress,
213
                                           tr("%1 filter update: %n files", 0, filesFound.size()).arg(m_name));
con's avatar
con committed
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
        }
        QDir dir = dirs.pop();
        int dirProgressMax = progressValues.pop();
        bool processed = processedValues.pop();
        if (dir.exists()) {
            QStringList subDirs;
            if (!processed) {
                subDirs = dir.entryList(QDir::Dirs|QDir::Hidden|QDir::NoDotAndDotDot,
                    QDir::Name|QDir::IgnoreCase|QDir::LocaleAware);
            }
            if (subDirs.isEmpty()) {
                QStringList fileEntries = dir.entryList(m_filters,
                    QDir::Files|QDir::Hidden,
                    QDir::Name|QDir::IgnoreCase|QDir::LocaleAware);
                foreach (const QString &file, fileEntries)
229
                    filesFound.append(dir.path()+ QLatin1Char('/') +file);
con's avatar
con committed
230
231
232
233
234
235
236
237
                progress += dirProgressMax;
            } else {
                int subProgress = dirProgressMax/(subDirs.size()+1);
                int selfProgress = subProgress + dirProgressMax%(subDirs.size()+1);
                dirs.push(dir);
                progressValues.push(selfProgress);
                processedValues.push(true);
                foreach (const QString &directory, subDirs) {
238
                    dirs.push(QDir(dir.path()+ QLatin1Char('/') + directory));
con's avatar
con committed
239
240
241
242
243
244
245
246
247
248
249
                    progressValues.push(subProgress);
                    processedValues.push(false);
                }
            }
        } else {
            progress += dirProgressMax;
        }
    }

    if (!future.isCanceled()) {
        QMutexLocker locker(&m_lock);
250
        files() = filesFound;
con's avatar
con committed
251
252
253
254
255
256
        generateFileNames();
        future.setProgressValue(MAX);
    } else {
        future.setProgressValueAndText(progress, tr("%1 filter update: canceled").arg(m_name));
    }
}