qmlmodelmanager.cpp 4.89 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
/**************************************************************************
**
** 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>

41 42
#include "qmleditorconstants.h"
#include "qmlmodelmanager.h"
43

44 45
#include <QtCore/QMetaType>

46 47
using namespace QmlEditor;
using namespace QmlEditor::Internal;
48

49 50
QmlModelManager::QmlModelManager(QObject *parent):
        QmlModelManagerInterface(parent),
51 52 53 54
        m_core(Core::ICore::instance())
{
    m_synchronizer.setCancelOnWait(true);

55
    qRegisterMetaType<QmlEditor::QmlDocument::Ptr>("QmlEditor::QmlDocument::Ptr");
56

57 58
    connect(this, SIGNAL(documentUpdated(QmlEditor::QmlDocument::Ptr)),
            this, SLOT(onDocumentUpdated(QmlEditor::QmlDocument::Ptr)));
59 60
}

61
Snapshot QmlModelManager::snapshot() const
62
{
63 64
    QMutexLocker locker(&m_mutex);

65 66 67
    return _snapshot;
}

68
void QmlModelManager::updateSourceFiles(const QStringList &files)
69 70 71 72
{
    refreshSourceFiles(files);
}

73
QFuture<void> QmlModelManager::refreshSourceFiles(const QStringList &sourceFiles)
74
{
75 76 77 78 79
    if (sourceFiles.isEmpty()) {
        return QFuture<void>();
    }

    const QMap<QString, QString> workingCopy = buildWorkingCopyList();
80

81
    QFuture<void> result = QtConcurrent::run(&QmlModelManager::parse,
82 83
                                              workingCopy, sourceFiles,
                                              this);
84

85 86
    if (m_synchronizer.futures().size() > 10) {
        QList<QFuture<void> > futures = m_synchronizer.futures();
87

88
        m_synchronizer.clearFutures();
89

90 91 92
        foreach (QFuture<void> future, futures) {
            if (! (future.isFinished() || future.isCanceled()))
                m_synchronizer.addFuture(future);
93
        }
94
    }
95

96
    m_synchronizer.addFuture(result);
97

98 99
    if (sourceFiles.count() > 1) {
        m_core->progressManager()->addTask(result, tr("Indexing"),
100
                        QmlEditor::Constants::TASK_INDEX);
101
    }
102 103

    return result;
104 105
}

106
QMap<QString, QString> QmlModelManager::buildWorkingCopyList()
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
{
    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;
}

122
void QmlModelManager::emitDocumentUpdated(QmlDocument::Ptr doc)
123 124
{ emit documentUpdated(doc); }

125
void QmlModelManager::onDocumentUpdated(QmlEditor::QmlDocument::Ptr doc)
126
{
127 128
    QMutexLocker locker(&m_mutex);

129 130 131
    _snapshot.insert(doc);
}

132
void QmlModelManager::parse(QFutureInterface<void> &future,
133 134
                            QMap<QString, QString> workingCopy,
                            QStringList files,
135
                            QmlModelManager *modelManager)
136
{
Erik Verbruggen's avatar
Erik Verbruggen committed
137
    future.setProgressRange(0, files.size());
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156

    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();
            }
        }

157
        QmlDocument::Ptr doc = QmlDocument::create(fileName);
158 159 160 161 162 163
        doc->setSource(contents);
        doc->parse();

        modelManager->emitDocumentUpdated(doc);
    }

Erik Verbruggen's avatar
Erik Verbruggen committed
164
    future.setProgressValue(files.size());
165
}