directoryfilter.cpp 7.15 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
con's avatar
con committed
2
**
3
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
con's avatar
con committed
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
con's avatar
con committed
7
**
hjk's avatar
hjk committed
8 9 10 11 12 13 14
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
15
**
16
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17 18 19 20 21 22 23 24 25
** 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.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
con's avatar
con committed
26 27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
hjk's avatar
hjk committed
29

con's avatar
con committed
30 31
#include "directoryfilter.h"

32
#include <QFileDialog>
33
#include <utils/filesearch.h>
34

35 36
using namespace Core;
using namespace Core::Internal;
con's avatar
con committed
37

38 39
DirectoryFilter::DirectoryFilter(Id id)
    : m_dialog(0)
con's avatar
con committed
40
{
41
    setId(id);
con's avatar
con committed
42
    setIncludedByDefault(true);
43
    setDisplayName(tr("Generic Directory Filter"));
hjk's avatar
hjk committed
44 45 46 47 48

    m_filters.append(QLatin1String("*.h"));
    m_filters.append(QLatin1String("*.cpp"));
    m_filters.append(QLatin1String("*.ui"));
    m_filters.append(QLatin1String("*.qrc"));
con's avatar
con committed
49 50 51 52 53 54 55
}

QByteArray DirectoryFilter::saveState() const
{
    QMutexLocker locker(&m_lock);
    QByteArray value;
    QDataStream out(&value, QIODevice::WriteOnly);
56
    out << displayName();
con's avatar
con committed
57 58 59
    out << m_directories;
    out << m_filters;
    out << shortcutString();
60
    out << isIncludedByDefault();
61
    out << files();
con's avatar
con committed
62 63 64 65 66 67 68
    return value;
}

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

69
    QString name;
con's avatar
con committed
70 71 72 73
    QString shortcut;
    bool defaultFilter;

    QDataStream in(state);
74
    in >> name;
75
    in >> m_directories;
con's avatar
con committed
76 77 78
    in >> m_filters;
    in >> shortcut;
    in >> defaultFilter;
79
    in >> files();
con's avatar
con committed
80

81
    setDisplayName(name);
con's avatar
con committed
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
    setShortcutString(shortcut);
    setIncludedByDefault(defaultFilter);

    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);
104
    m_ui.nameEdit->setText(displayName());
con's avatar
con committed
105 106 107
    m_ui.nameEdit->selectAll();
    m_ui.directoryList->clear();
    m_ui.directoryList->addItems(m_directories);
hjk's avatar
hjk committed
108
    m_ui.fileTypeEdit->setText(m_filters.join(QLatin1Char(',')));
con's avatar
con committed
109
    m_ui.shortcutEdit->setText(shortcutString());
110
    m_ui.defaultFlag->setChecked(!isIncludedByDefault());
con's avatar
con committed
111 112 113 114 115 116
    updateOptionButtons();
    if (dialog.exec() == QDialog::Accepted) {
        QMutexLocker locker(&m_lock);
        bool directoriesChanged = false;
        QStringList oldDirectories = m_directories;
        QStringList oldFilters = m_filters;
117
        setDisplayName(m_ui.nameEdit->text().trimmed());
con's avatar
con committed
118 119 120 121 122 123 124 125 126 127
        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;
        }
128
        m_filters = m_ui.fileTypeEdit->text().trimmed().split(QLatin1Char(','));
con's avatar
con committed
129 130 131 132 133 134 135 136 137 138 139
        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()
{
140
    QString dir = QFileDialog::getExistingDirectory(m_dialog, tr("Select Directory"));
141
    if (!dir.isEmpty())
con's avatar
con committed
142 143 144 145 146 147 148 149
        m_ui.directoryList->addItem(dir);
}

void DirectoryFilter::editDirectory()
{
    if (m_ui.directoryList->selectedItems().count() < 1)
        return;
    QListWidgetItem *currentItem = m_ui.directoryList->selectedItems().at(0);
150
    QString dir = QFileDialog::getExistingDirectory(m_dialog, tr("Select Directory"),
con's avatar
con committed
151
                                                    currentItem->text());
152
    if (!dir.isEmpty())
con's avatar
con committed
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
        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)
{
173 174
    QStringList directories;
    {
con's avatar
con committed
175
        QMutexLocker locker(&m_lock);
176 177 178 179
        if (m_directories.count() < 1) {
            files().clear();
            generateFileNames();
            future.setProgressRange(0, 1);
180
            future.setProgressValueAndText(1, tr("%1 filter update: 0 files").arg(displayName()));
181
            return;
con's avatar
con committed
182
        }
183
        directories = m_directories;
con's avatar
con committed
184
    }
185 186 187 188 189
    Utils::SubDirFileIterator it(directories, m_filters);
    future.setProgressRange(0, it.maxProgress());
    QStringList filesFound;
    while (!future.isCanceled() && it.hasNext()) {
        filesFound << it.next();
con's avatar
con committed
190 191
        if (future.isProgressUpdateNeeded()
                || future.progressValue() == 0 /*workaround for regression in Qt*/) {
192
            future.setProgressValueAndText(it.currentProgress(),
193
                                           tr("%1 filter update: %n files", 0, filesFound.size()).arg(displayName()));
con's avatar
con committed
194 195 196 197 198
        }
    }

    if (!future.isCanceled()) {
        QMutexLocker locker(&m_lock);
199
        files() = filesFound;
con's avatar
con committed
200
        generateFileNames();
201
        future.setProgressValue(it.maxProgress());
con's avatar
con committed
202
    } else {
203
        future.setProgressValueAndText(it.currentProgress(), tr("%1 filter update: canceled").arg(displayName()));
con's avatar
con committed
204 205
    }
}