1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
11 ** Licensees holding valid Qt Commercial licenses may use this file in
12 ** accordance with the Qt Commercial License Agreement provided with the
13 ** Software or, alternatively, in accordance with the terms contained in
14 ** a written agreement between you and Nokia.
16 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** If you are unsure which license is appropriate for your use, please
26 ** contact the sales department at http://qt.nokia.com/contact.
28 **************************************************************************/
30 #include "qmlproject.h"
31 #include "qmlprojectfile.h"
32 #include "qmlprojectmanagerconstants.h"
33 #include "fileformat/qmlprojectitem.h"
34 #include "qmlprojectrunconfiguration.h"
36 #include <coreplugin/icore.h>
37 #include <coreplugin/messagemanager.h>
38 #include <extensionsystem/pluginmanager.h>
39 #include <projectexplorer/filewatcher.h>
40 #include <qt4projectmanager/qmldumptool.h>
41 #include <qmljs/qmljsmodelmanagerinterface.h>
43 #include <QTextStream>
44 #include <QDeclarativeComponent>
47 namespace QmlProjectManager {
49 QmlProject::QmlProject(Internal::Manager *manager, const QString &fileName)
52 m_modelManager(ExtensionSystem::PluginManager::instance()->getObject<QmlJS::ModelManagerInterface>()),
53 m_fileWatcher(new ProjectExplorer::FileWatcher(this)),
54 m_targetFactory(new Internal::QmlProjectTargetFactory(this))
56 setSupportedTargetIds(QSet<QString>() << QLatin1String(Constants::QML_VIEWER_TARGET_ID));
57 QFileInfo fileInfo(m_fileName);
58 m_projectName = fileInfo.completeBaseName();
60 m_file = new Internal::QmlProjectFile(this, fileName);
61 m_rootNode = new Internal::QmlProjectNode(this, m_file);
63 m_fileWatcher->addFile(fileName),
64 connect(m_fileWatcher, SIGNAL(fileChanged(QString)),
65 this, SLOT(refreshProjectFile()));
67 m_manager->registerProject(this);
70 QmlProject::~QmlProject()
72 m_manager->unregisterProject(this);
74 delete m_projectItem.data();
78 QDir QmlProject::projectDir() const
80 return QFileInfo(file()->fileName()).dir();
83 QString QmlProject::filesFileName() const
84 { return m_fileName; }
86 void QmlProject::parseProject(RefreshOptions options)
88 if (options & Files) {
89 if (options & ProjectFile)
90 delete m_projectItem.data();
92 QFile file(m_fileName);
93 if (file.open(QFile::ReadOnly)) {
94 QDeclarativeComponent *component = new QDeclarativeComponent(&m_engine, this);
95 component->setData(file.readAll(), QUrl::fromLocalFile(m_fileName));
96 if (component->isReady()
97 && qobject_cast<QmlProjectItem*>(component->create())) {
98 m_projectItem = qobject_cast<QmlProjectItem*>(component->create());
99 connect(m_projectItem.data(), SIGNAL(qmlFilesChanged(QSet<QString>, QSet<QString>)),
100 this, SLOT(refreshFiles(QSet<QString>, QSet<QString>)));
102 Core::MessageManager *messageManager = Core::ICore::instance()->messageManager();
103 messageManager->printToOutputPane(tr("Error while loading project file!"));
104 messageManager->printToOutputPane(component->errorString(), true);
109 m_projectItem.data()->setSourceDirectory(projectDir().path());
110 m_modelManager->updateSourceFiles(m_projectItem.data()->files(), true);
112 m_rootNode->refresh();
115 if (options & Configuration) {
116 // update configuration
120 emit fileListChanged();
123 void QmlProject::refresh(RefreshOptions options)
125 parseProject(options);
128 m_rootNode->refresh();
130 QmlJS::ModelManagerInterface::ProjectInfo pinfo(this);
131 pinfo.sourceFiles = files();
132 pinfo.importPaths = importPaths();
134 if (pinfo.qmlDumpPath.isNull()) {
135 pinfo.qmlDumpPath = Qt4ProjectManager::QmlDumpTool::qmlDumpPath();
138 m_modelManager->updateProjectInfo(pinfo);
141 QStringList QmlProject::convertToAbsoluteFiles(const QStringList &paths) const
143 const QDir projectDir(QFileInfo(m_fileName).dir());
144 QStringList absolutePaths;
145 foreach (const QString &file, paths) {
146 QFileInfo fileInfo(projectDir, file);
147 absolutePaths.append(fileInfo.absoluteFilePath());
149 absolutePaths.removeDuplicates();
150 return absolutePaths;
153 QStringList QmlProject::files() const
157 files = m_projectItem.data()->files();
164 bool QmlProject::validProjectFile() const
166 return !m_projectItem.isNull();
169 QStringList QmlProject::importPaths() const
171 QStringList importPaths;
173 importPaths = m_projectItem.data()->importPaths();
177 bool QmlProject::addFiles(const QStringList &filePaths)
180 foreach (const QString &filePath, filePaths) {
181 if (!m_projectItem.data()->matchesFile(filePath))
184 return toAdd.isEmpty();
187 void QmlProject::refreshProjectFile()
189 refresh(QmlProject::ProjectFile | Files);
192 void QmlProject::refreshFiles(const QSet<QString> &/*added*/, const QSet<QString> &removed)
195 if (!removed.isEmpty())
196 m_modelManager->removeFiles(removed.toList());
199 QString QmlProject::displayName() const
201 return m_projectName;
204 QString QmlProject::id() const
206 return QLatin1String("QmlProjectManager.QmlProject");
209 Core::IFile *QmlProject::file() const
214 Internal::Manager *QmlProject::projectManager() const
219 QList<ProjectExplorer::Project *> QmlProject::dependsOn()
221 return QList<Project *>();
224 ProjectExplorer::BuildConfigWidget *QmlProject::createConfigWidget()
229 QList<ProjectExplorer::BuildConfigWidget*> QmlProject::subConfigWidgets()
231 return QList<ProjectExplorer::BuildConfigWidget*>();
234 Internal::QmlProjectTargetFactory *QmlProject::targetFactory() const
236 return m_targetFactory;
239 Internal::QmlProjectTarget *QmlProject::activeTarget() const
241 return static_cast<Internal::QmlProjectTarget *>(Project::activeTarget());
244 Internal::QmlProjectNode *QmlProject::rootProjectNode() const
249 QStringList QmlProject::files(FilesMode) const
254 bool QmlProject::fromMap(const QVariantMap &map)
256 if (!Project::fromMap(map))
259 if (targets().isEmpty()) {
260 Internal::QmlProjectTarget *target(targetFactory()->create(this, QLatin1String(Constants::QML_VIEWER_TARGET_ID)));
265 // FIXME workaround to guarantee that run/debug actions are enabled if a valid file exists
266 QmlProjectRunConfiguration *runConfig = qobject_cast<QmlProjectRunConfiguration*>(activeTarget()->activeRunConfiguration());
268 runConfig->changeCurrentFile(0);
273 } // namespace QmlProjectManager