Commit a1ef043b authored by Kai Koehne's avatar Kai Koehne
Browse files

QmlProject: Detect import type from mainFile



Detect the default import (QtQuick 1.x vs QtQuick 2.x) from main
file, and use it to restrict kits + select the right runtime.

Task-number: QTCREATORBUG-8358
Change-Id: Ib8cfd3eb989953f607fee6043c43e588f798c774
Reviewed-by: default avatarFawzi Mohamed <fawzi.mohamed@digia.com>
Reviewed-by: default avatarDaniel Teske <daniel.teske@digia.com>
parent 459d471c
......@@ -60,6 +60,7 @@ namespace QmlProjectManager {
QmlProject::QmlProject(Internal::Manager *manager, const QString &fileName)
: m_manager(manager),
m_fileName(fileName),
m_defaultImport(UnknownImport),
m_modelManager(QmlJS::ModelManagerInterface::instance())
{
setProjectContext(Core::Context(QmlProjectManager::Constants::PROJECTCONTEXT));
......@@ -111,6 +112,18 @@ QDir QmlProject::projectDir() const
QString QmlProject::filesFileName() const
{ return m_fileName; }
static QmlProject::QmlImport detectImport(const QString &qml) {
static QRegExp qtQuick1RegExp(QLatin1String("import\\s+QtQuick\\s+1"));
static QRegExp qtQuick2RegExp(QLatin1String("import\\s+QtQuick\\s+2"));
if (qml.contains(qtQuick1RegExp))
return QmlProject::QtQuick1Import;
else if (qml.contains(qtQuick2RegExp))
return QmlProject::QtQuick2Import;
else
return QmlProject::UnknownImport;
}
void QmlProject::parseProject(RefreshOptions options)
{
Core::MessageManager *messageManager = Core::ICore::messageManager();
......@@ -142,11 +155,14 @@ void QmlProject::parseProject(RefreshOptions options)
QString mainFilePath = m_projectItem.data()->mainFile();
if (!mainFilePath.isEmpty()) {
mainFilePath = projectDir().absoluteFilePath(mainFilePath);
if (!QFileInfo(mainFilePath).isReadable()) {
Utils::FileReader reader;
QString errorMessage;
if (!reader.fetch(mainFilePath, &errorMessage)) {
messageManager->printToOutputPane(
tr("Warning while loading project file %1.").arg(m_fileName));
messageManager->printToOutputPane(
tr("File '%1' does not exist or is not readable.").arg(mainFilePath), true);
messageManager->printToOutputPane(errorMessage, true);
} else {
m_defaultImport = detectImport(QString::fromUtf8(reader.data()));
}
}
}
......@@ -255,6 +271,11 @@ void QmlProject::refreshProjectFile()
refresh(QmlProject::ProjectFile | Files);
}
QmlProject::QmlImport QmlProject::defaultImport() const
{
return m_defaultImport;
}
void QmlProject::refreshFiles(const QSet<QString> &/*added*/, const QSet<QString> &removed)
{
refresh(Files);
......@@ -291,11 +312,26 @@ bool QmlProject::supportsKit(ProjectExplorer::Kit *k, QString *errorMessage) con
return false;
}
// TODO: Limit supported versions?
QtSupport::BaseQtVersion *version = QtSupport::QtKitInformation::qtVersion(k);
if (!version && errorMessage)
*errorMessage = tr("No Qt version set in kit.");
return version;
if (!version) {
if (errorMessage)
*errorMessage = tr("No Qt version set in kit.");
return false;
}
if (version->qtVersion() < QtSupport::QtVersionNumber(4, 7, 0)) {
if (errorMessage)
*errorMessage = tr("Qt version is too old.");
return false;
}
if (version->qtVersion() < QtSupport::QtVersionNumber(5, 0, 0)
&& defaultImport() == QtQuick2Import) {
if (errorMessage)
*errorMessage = tr("Qt version is too old.");
return false;
}
return true;
}
ProjectExplorer::ProjectNode *QmlProject::rootProjectNode() const
......@@ -313,12 +349,13 @@ bool QmlProject::fromMap(const QVariantMap &map)
if (!Project::fromMap(map))
return false;
// refresh first - project information is used e.g. to decide the default RC's
refresh(Everything);
ProjectExplorer::Kit *defaultKit = ProjectExplorer::KitManager::instance()->defaultKit();
if (!activeTarget() && defaultKit)
addTarget(createTarget(defaultKit));
refresh(Everything);
// addedTarget calls updateEnabled on the runconfigurations
// which needs to happen after refresh
foreach (ProjectExplorer::Target *t, targets())
......
......@@ -92,6 +92,9 @@ public:
void refreshProjectFile();
enum QmlImport { UnknownImport, QtQuick1Import, QtQuick2Import };
QmlImport defaultImport() const;
private slots:
void refreshFiles(const QSet<QString> &added, const QSet<QString> &removed);
void addedTarget(ProjectExplorer::Target *target);
......@@ -109,6 +112,7 @@ private:
QString m_fileName;
Internal::QmlProjectFile *m_file;
QString m_projectName;
QmlImport m_defaultImport;
QmlJS::ModelManagerInterface *m_modelManager;
// plain format
......
......@@ -59,12 +59,26 @@ QList<Core::Id> QmlProjectRunConfigurationFactory::availableCreationIds(ProjectE
QtSupport::BaseQtVersion *version
= QtSupport::QtKitInformation::qtVersion(parent->kit());
// put qmlscene first (so that it is the default) for Qt 5.0.0
// First id will be the default run configuration
QList<Core::Id> list;
if (version && version->qtVersion() >= QtSupport::QtVersionNumber(5, 0, 0))
list << Core::Id(Constants::QML_SCENE_RC_ID);
list << Core::Id(Constants::QML_VIEWER_RC_ID);
if (version && version->qtVersion() >= QtSupport::QtVersionNumber(5, 0, 0)) {
QmlProject *project = static_cast<QmlProject*>(parent->project());
switch (project->defaultImport()) {
case QmlProject::QtQuick1Import:
list << Core::Id(Constants::QML_VIEWER_RC_ID);
break;
case QmlProject::QtQuick2Import:
list << Core::Id(Constants::QML_SCENE_RC_ID);
break;
case QmlProject::UnknownImport:
default:
list << Core::Id(Constants::QML_SCENE_RC_ID);
list << Core::Id(Constants::QML_VIEWER_RC_ID);
break;
}
} else {
list << Core::Id(Constants::QML_VIEWER_RC_ID);
}
return list;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment