Files
qt-creator/src/plugins/qmleditor/qmlmodelmanager.cpp

167 lines
5.0 KiB
C++
Raw Normal View History

2009-09-04 16:51:11 +02:00
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** 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.
**
** GNU Lesser General Public License Usage
**
** 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.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/
#include <QFile>
#include <QtConcurrentRun>
#include <qtconcurrent/runextensions.h>
#include <QTextStream>
#include <coreplugin/icore.h>
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/progressmanager/progressmanager.h>
#include <texteditor/itexteditor.h>
2009-09-30 17:43:21 +02:00
#include "qmleditorconstants.h"
#include "qmlmodelmanager.h"
2009-09-04 16:51:11 +02:00
#include <QtCore/QMetaType>
2009-09-30 17:43:21 +02:00
using namespace QmlEditor;
using namespace QmlEditor::Internal;
2009-09-04 16:51:11 +02:00
2009-09-30 17:43:21 +02:00
QmlModelManager::QmlModelManager(QObject *parent):
QmlModelManagerInterface(parent),
2009-09-04 16:51:11 +02:00
m_core(Core::ICore::instance())
{
m_synchronizer.setCancelOnWait(true);
qRegisterMetaType<QmlEditor::QmlDocument::Ptr>("QmlEditor::QmlDocument::Ptr");
2009-09-04 16:51:11 +02:00
connect(this, SIGNAL(documentUpdated(QmlEditor::QmlDocument::Ptr)),
this, SLOT(onDocumentUpdated(QmlEditor::QmlDocument::Ptr)));
2009-09-04 16:51:11 +02:00
}
2009-09-30 17:43:21 +02:00
Snapshot QmlModelManager::snapshot() const
2009-09-04 16:51:11 +02:00
{
QMutexLocker locker(&m_mutex);
2009-09-04 16:51:11 +02:00
return _snapshot;
}
2009-09-30 17:43:21 +02:00
void QmlModelManager::updateSourceFiles(const QStringList &files)
2009-09-04 16:51:11 +02:00
{
refreshSourceFiles(files);
}
2009-09-30 17:43:21 +02:00
QFuture<void> QmlModelManager::refreshSourceFiles(const QStringList &sourceFiles)
2009-09-04 16:51:11 +02:00
{
2009-09-21 18:27:15 +02:00
if (sourceFiles.isEmpty()) {
return QFuture<void>();
}
const QMap<QString, QString> workingCopy = buildWorkingCopyList();
2009-09-04 16:51:11 +02:00
2009-09-30 17:43:21 +02:00
QFuture<void> result = QtConcurrent::run(&QmlModelManager::parse,
2009-09-21 18:27:15 +02:00
workingCopy, sourceFiles,
this);
2009-09-04 16:51:11 +02:00
2009-09-21 18:27:15 +02:00
if (m_synchronizer.futures().size() > 10) {
QList<QFuture<void> > futures = m_synchronizer.futures();
2009-09-04 16:51:11 +02:00
2009-09-21 18:27:15 +02:00
m_synchronizer.clearFutures();
2009-09-04 16:51:11 +02:00
2009-09-21 18:27:15 +02:00
foreach (QFuture<void> future, futures) {
if (! (future.isFinished() || future.isCanceled()))
m_synchronizer.addFuture(future);
2009-09-04 16:51:11 +02:00
}
2009-09-21 18:27:15 +02:00
}
2009-09-04 16:51:11 +02:00
2009-09-21 18:27:15 +02:00
m_synchronizer.addFuture(result);
2009-09-04 16:51:11 +02:00
2009-09-21 18:27:15 +02:00
if (sourceFiles.count() > 1) {
m_core->progressManager()->addTask(result, tr("Indexing"),
2009-09-30 17:43:21 +02:00
QmlEditor::Constants::TASK_INDEX,
2009-09-21 18:27:15 +02:00
Core::ProgressManager::CloseOnSuccess);
2009-09-04 16:51:11 +02:00
}
2009-09-21 18:27:15 +02:00
return result;
2009-09-04 16:51:11 +02:00
}
2009-09-30 17:43:21 +02:00
QMap<QString, QString> QmlModelManager::buildWorkingCopyList()
2009-09-04 16:51:11 +02:00
{
QMap<QString, QString> workingCopy;
Core::EditorManager *editorManager = m_core->editorManager();
foreach (Core::IEditor *editor, editorManager->openedEditors()) {
const QString key = editor->file()->fileName();
if (TextEditor::ITextEditor *textEditor = qobject_cast<TextEditor::ITextEditor*>(editor)) {
workingCopy[key] = textEditor->contents();
}
}
return workingCopy;
}
2009-09-30 17:43:21 +02:00
void QmlModelManager::emitDocumentUpdated(QmlDocument::Ptr doc)
2009-09-04 16:51:11 +02:00
{ emit documentUpdated(doc); }
void QmlModelManager::onDocumentUpdated(QmlEditor::QmlDocument::Ptr doc)
2009-09-04 16:51:11 +02:00
{
QMutexLocker locker(&m_mutex);
2009-09-04 16:51:11 +02:00
_snapshot.insert(doc);
}
2009-09-30 17:43:21 +02:00
void QmlModelManager::parse(QFutureInterface<void> &future,
2009-09-04 16:51:11 +02:00
QMap<QString, QString> workingCopy,
QStringList files,
2009-09-30 17:43:21 +02:00
QmlModelManager *modelManager)
2009-09-04 16:51:11 +02:00
{
2009-09-04 18:01:46 +02:00
future.setProgressRange(0, files.size());
2009-09-04 16:51:11 +02:00
for (int i = 0; i < files.size(); ++i) {
future.setProgressValue(i);
const QString fileName = files.at(i);
QString contents;
if (workingCopy.contains(fileName)) {
contents = workingCopy.value(fileName);
} else {
QFile inFile(fileName);
if (inFile.open(QIODevice::ReadOnly)) {
QTextStream ins(&inFile);
contents = ins.readAll();
inFile.close();
}
}
2009-09-30 17:43:21 +02:00
QmlDocument::Ptr doc = QmlDocument::create(fileName);
2009-09-04 16:51:11 +02:00
doc->setSource(contents);
doc->parse();
modelManager->emitDocumentUpdated(doc);
}
2009-09-04 18:01:46 +02:00
future.setProgressValue(files.size());
2009-09-04 16:51:11 +02:00
}