forked from qt-creator/qt-creator
On new generic project, show progress and dialog
Task-Nr: QTCREATORBUG-2226 Change-Id: I62e1c740a008663396960cfc12a05202f8800892 Reviewed-on: http://codereview.qt.nokia.com/594 Reviewed-by: Qt Sanity Bot <qt_sanity_bot@ovi.com> Reviewed-by: Thorbjørn Lindeijer <thorbjorn@lindeijer.nl>
This commit is contained in:
@@ -32,6 +32,9 @@
|
||||
|
||||
#include "selectablefilesmodel.h"
|
||||
|
||||
#include <coreplugin/fileiconprovider.h>
|
||||
|
||||
#include <qtconcurrent/QtConcurrentTools>
|
||||
#include <QtGui/QHBoxLayout>
|
||||
#include <QtGui/QDialogButtonBox>
|
||||
#include <QtGui/QTreeView>
|
||||
@@ -39,21 +42,68 @@
|
||||
using namespace GenericProjectManager;
|
||||
using namespace GenericProjectManager::Internal;
|
||||
|
||||
SelectableFilesModel::SelectableFilesModel(const QString &baseDir, const QStringList &files, const QSet<QString> &suffixes, QObject *parent)
|
||||
: QAbstractItemModel(parent), m_baseDir(baseDir), m_suffixes(suffixes), m_root(0)
|
||||
SelectableFilesModel::SelectableFilesModel(const QString &baseDir, QObject *parent)
|
||||
: QAbstractItemModel(parent), m_root(0), m_baseDir(baseDir), m_allFiles(true)
|
||||
{
|
||||
m_files = files.toSet();
|
||||
|
||||
// Build a tree
|
||||
// Dummy tree
|
||||
m_root = new Tree;
|
||||
m_root->name = "/";
|
||||
m_root->parent = 0;
|
||||
m_root->isDir = true;
|
||||
|
||||
buildTree(baseDir, m_root);
|
||||
}
|
||||
|
||||
void SelectableFilesModel::buildTree(const QString &baseDir, Tree *tree)
|
||||
void SelectableFilesModel::setInitialMarkedFiles(const QStringList &files)
|
||||
{
|
||||
m_files = files.toSet();
|
||||
m_allFiles = false;
|
||||
}
|
||||
|
||||
void SelectableFilesModel::setSuffixes(QSet<QString> suffixes)
|
||||
{
|
||||
m_suffixes = suffixes;
|
||||
}
|
||||
|
||||
void SelectableFilesModel::init()
|
||||
{
|
||||
}
|
||||
|
||||
void SelectableFilesModel::startParsing()
|
||||
{
|
||||
// Build a tree in a future
|
||||
m_rootForFuture = new Tree;
|
||||
m_rootForFuture->name = "/";
|
||||
m_rootForFuture->parent = 0;
|
||||
|
||||
connect(&m_watcher, SIGNAL(finished()), this, SLOT(buildTreeFinished()));
|
||||
m_watcher.setFuture(QtConcurrent::run(&SelectableFilesModel::run, this));
|
||||
}
|
||||
|
||||
void SelectableFilesModel::run(QFutureInterface<void> &fi)
|
||||
{
|
||||
m_futureCount = 0;
|
||||
buildTree(m_baseDir, m_rootForFuture, fi);
|
||||
}
|
||||
|
||||
void SelectableFilesModel::buildTreeFinished()
|
||||
{
|
||||
beginResetModel();
|
||||
deleteTree(m_root);
|
||||
m_root = m_rootForFuture;
|
||||
m_rootForFuture = 0;
|
||||
endResetModel();
|
||||
emit parsingFinished();
|
||||
}
|
||||
|
||||
void SelectableFilesModel::waitForFinished()
|
||||
{
|
||||
m_watcher.waitForFinished();
|
||||
}
|
||||
|
||||
void SelectableFilesModel::cancel()
|
||||
{
|
||||
m_watcher.cancel();
|
||||
}
|
||||
|
||||
void SelectableFilesModel::buildTree(const QString &baseDir, Tree *tree, QFutureInterface<void> &fi)
|
||||
{
|
||||
const QFileInfoList fileInfoList = QDir(baseDir).entryInfoList(QDir::Files |
|
||||
QDir::Dirs |
|
||||
@@ -62,12 +112,18 @@ void SelectableFilesModel::buildTree(const QString &baseDir, Tree *tree)
|
||||
bool allChecked = true;
|
||||
bool allUnchecked = true;
|
||||
foreach (const QFileInfo &fileInfo, fileInfoList) {
|
||||
if (m_futureCount % 100) {
|
||||
emit parsingProgress(fileInfo.absoluteFilePath());
|
||||
if (fi.isCanceled())
|
||||
return;
|
||||
}
|
||||
++m_futureCount;
|
||||
if (fileInfo.isDir()) {
|
||||
Tree *t = new Tree;
|
||||
t->parent = tree;
|
||||
t->name = fileInfo.fileName();
|
||||
t->isDir = true;
|
||||
buildTree(fileInfo.filePath(), t);
|
||||
t->fullPath = fileInfo.filePath();
|
||||
buildTree(fileInfo.filePath(), t, fi);
|
||||
allChecked &= t->checked == Qt::Checked;
|
||||
allUnchecked &= t->checked == Qt::Unchecked;
|
||||
tree->childDirectories.append(t);
|
||||
@@ -75,16 +131,16 @@ void SelectableFilesModel::buildTree(const QString &baseDir, Tree *tree)
|
||||
Tree *t = new Tree;
|
||||
t->parent = tree;
|
||||
t->name = fileInfo.fileName();
|
||||
t->isDir = false;
|
||||
t->icon = m_iconProvider.icon(fileInfo);
|
||||
t->checked = m_files.contains(fileInfo.absoluteFilePath()) ? Qt::Checked : Qt::Unchecked;
|
||||
t->checked = m_allFiles || m_files.contains(fileInfo.absoluteFilePath()) ? Qt::Checked : Qt::Unchecked;
|
||||
t->fullPath = fileInfo.filePath();
|
||||
allChecked &= t->checked == Qt::Checked;
|
||||
allUnchecked &= t->checked == Qt::Unchecked;
|
||||
tree->files.append(t);
|
||||
}
|
||||
}
|
||||
if (allChecked)
|
||||
if (tree->childDirectories.isEmpty() && tree->files.isEmpty())
|
||||
tree->checked = Qt::Unchecked;
|
||||
else if (allChecked)
|
||||
tree->checked = Qt::Checked;
|
||||
else if (allUnchecked)
|
||||
tree->checked = Qt::Unchecked;
|
||||
@@ -157,10 +213,9 @@ QVariant SelectableFilesModel::data(const QModelIndex &index, int role) const
|
||||
if (role == Qt::CheckStateRole)
|
||||
return t->checked;
|
||||
if (role == Qt::DecorationRole) {
|
||||
if (t->isDir)
|
||||
return m_iconProvider.icon(QFileIconProvider::Folder);
|
||||
else
|
||||
return t->icon;
|
||||
if (t->icon.isNull())
|
||||
t->icon = Core::FileIconProvider::instance()->icon(QFileInfo(t->fullPath));
|
||||
return t->icon;
|
||||
}
|
||||
return QVariant();
|
||||
}
|
||||
@@ -229,6 +284,22 @@ Qt::ItemFlags SelectableFilesModel::flags(const QModelIndex &index) const
|
||||
return Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsUserCheckable;
|
||||
}
|
||||
|
||||
QStringList SelectableFilesModel::selectedPaths() const
|
||||
{
|
||||
QStringList result;
|
||||
collectPaths(m_root, &result);
|
||||
return result;
|
||||
}
|
||||
|
||||
void SelectableFilesModel::collectPaths(Tree *root, QStringList *result) const
|
||||
{
|
||||
if (root->checked == Qt::Unchecked)
|
||||
return;
|
||||
result->append(root->fullPath);
|
||||
foreach (Tree *t, root->childDirectories)
|
||||
collectPaths(t, result);
|
||||
}
|
||||
|
||||
QStringList SelectableFilesModel::selectedFiles() const
|
||||
{
|
||||
QStringList result;
|
||||
@@ -255,8 +326,21 @@ SelectableFilesDialog::SelectableFilesDialog(const QString &path, const QStringL
|
||||
setLayout(layout);
|
||||
setWindowTitle(tr("Edit Files"));
|
||||
|
||||
QTreeView *view = new QTreeView(this);
|
||||
layout->addWidget(view);
|
||||
m_view = new QTreeView(this);
|
||||
|
||||
m_selectableFilesModel = new SelectableFilesModel(path, this);
|
||||
m_selectableFilesModel->setInitialMarkedFiles(files);
|
||||
m_selectableFilesModel->setSuffixes(suffixes);
|
||||
m_view->setModel(m_selectableFilesModel);
|
||||
m_view->setMinimumSize(500, 400);
|
||||
m_view->setHeaderHidden(true);
|
||||
m_view->hide();
|
||||
layout->addWidget(m_view);
|
||||
|
||||
m_progressLabel = new QLabel(this);
|
||||
m_progressLabel->setMaximumWidth(500);
|
||||
layout->addWidget(m_progressLabel);
|
||||
|
||||
QDialogButtonBox *buttonBox = new QDialogButtonBox(Qt::Horizontal, this);
|
||||
buttonBox->setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
|
||||
connect(buttonBox, SIGNAL(accepted()),
|
||||
@@ -265,21 +349,40 @@ SelectableFilesDialog::SelectableFilesDialog(const QString &path, const QStringL
|
||||
this, SLOT(reject()));
|
||||
layout->addWidget(buttonBox);
|
||||
|
||||
m_selectableFilesModel = new SelectableFilesModel(path, files, suffixes, this);
|
||||
view->setModel(m_selectableFilesModel);
|
||||
view->setMinimumSize(400, 300);
|
||||
view->setHeaderHidden(true);
|
||||
view->expand(QModelIndex());
|
||||
smartExpand(view, m_selectableFilesModel->index(0,0, QModelIndex()));
|
||||
connect(m_selectableFilesModel, SIGNAL(parsingProgress(QString)),
|
||||
this, SLOT(parsingProgress(QString)));
|
||||
connect(m_selectableFilesModel, SIGNAL(parsingFinished()),
|
||||
this, SLOT(parsingFinished()));
|
||||
|
||||
m_selectableFilesModel->startParsing();
|
||||
}
|
||||
|
||||
void SelectableFilesDialog::smartExpand(QTreeView *view, const QModelIndex &index)
|
||||
SelectableFilesDialog::~SelectableFilesDialog()
|
||||
{
|
||||
if (view->model()->data(index, Qt::CheckStateRole) == Qt::PartiallyChecked) {
|
||||
view->expand(index);
|
||||
int rows = view->model()->rowCount(index);
|
||||
m_selectableFilesModel->cancel();
|
||||
m_selectableFilesModel->waitForFinished();
|
||||
}
|
||||
|
||||
void SelectableFilesDialog::parsingProgress(const QString &fileName)
|
||||
{
|
||||
m_progressLabel->setText(tr("Generating file list...\n\n%1").arg(fileName));
|
||||
}
|
||||
|
||||
void SelectableFilesDialog::parsingFinished()
|
||||
{
|
||||
m_view->show();
|
||||
m_progressLabel->hide();
|
||||
m_view->expand(QModelIndex());
|
||||
smartExpand(m_selectableFilesModel->index(0,0, QModelIndex()));
|
||||
}
|
||||
|
||||
void SelectableFilesDialog::smartExpand(const QModelIndex &index)
|
||||
{
|
||||
if (m_view->model()->data(index, Qt::CheckStateRole) == Qt::PartiallyChecked) {
|
||||
m_view->expand(index);
|
||||
int rows = m_view->model()->rowCount(index);
|
||||
for (int i = 0; i < rows; ++i)
|
||||
smartExpand(view, index.child(i, 0));
|
||||
smartExpand(index.child(i, 0));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user