2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2009-05-04 12:19:22 +02:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2009-05-04 12:19:22 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2009-05-04 12:19:22 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** Commercial License Usage
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
2016-01-15 14:57:40 +01:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2009-05-04 12:19:22 +02:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** GNU General Public License Usage
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
2010-12-17 16:01:08 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2009-05-04 12:19:22 +02:00
|
|
|
|
2022-05-06 14:27:58 +03:00
|
|
|
#include "qdslandingpage.h"
|
2009-05-04 12:19:22 +02:00
|
|
|
#include "qmlprojectplugin.h"
|
2009-05-07 12:27:52 +02:00
|
|
|
#include "qmlproject.h"
|
2022-02-22 16:33:53 +01:00
|
|
|
#include "qmlprojectconstants.h"
|
2019-03-13 12:22:44 +01:00
|
|
|
#include "qmlprojectrunconfiguration.h"
|
2022-05-12 12:33:12 +03:00
|
|
|
#include "projectfilecontenttools.h"
|
2009-11-11 10:10:00 +01:00
|
|
|
|
2022-05-06 14:27:58 +03:00
|
|
|
#include <coreplugin/coreconstants.h>
|
|
|
|
#include <coreplugin/designmode.h>
|
2010-04-01 17:21:18 +02:00
|
|
|
#include <coreplugin/fileiconprovider.h>
|
2009-05-04 12:19:22 +02:00
|
|
|
#include <coreplugin/icore.h>
|
2021-09-21 12:31:19 +02:00
|
|
|
#include <coreplugin/messagebox.h>
|
2022-05-06 14:27:58 +03:00
|
|
|
#include <coreplugin/modemanager.h>
|
2009-05-04 12:19:22 +02:00
|
|
|
|
2017-03-03 18:16:34 +01:00
|
|
|
#include <projectexplorer/projectmanager.h>
|
2019-03-13 12:22:44 +01:00
|
|
|
#include <projectexplorer/runcontrol.h>
|
2021-09-21 12:31:19 +02:00
|
|
|
#include <projectexplorer/session.h>
|
|
|
|
|
|
|
|
#include <qmljs/qmljsmodelmanagerinterface.h>
|
2017-03-03 18:16:34 +01:00
|
|
|
|
2022-05-06 14:27:58 +03:00
|
|
|
#include <qmljseditor/qmljseditor.h>
|
|
|
|
#include <qmljseditor/qmljseditorconstants.h>
|
|
|
|
|
2017-03-03 18:16:34 +01:00
|
|
|
#include <qmljstools/qmljstoolsconstants.h>
|
2011-07-21 10:40:56 +02:00
|
|
|
|
2021-09-21 12:31:19 +02:00
|
|
|
#include <extensionsystem/pluginmanager.h>
|
|
|
|
#include <extensionsystem/pluginspec.h>
|
|
|
|
|
2022-05-06 14:27:58 +03:00
|
|
|
#include <utils/fileutils.h>
|
2021-11-17 10:38:52 +01:00
|
|
|
#include <utils/qtcprocess.h>
|
2021-09-21 12:31:19 +02:00
|
|
|
|
2022-05-06 14:27:58 +03:00
|
|
|
#include <QDesktopServices>
|
2021-09-21 12:31:19 +02:00
|
|
|
#include <QMessageBox>
|
|
|
|
#include <QPushButton>
|
|
|
|
#include <QTimer>
|
|
|
|
#include <QPointer>
|
|
|
|
|
2017-03-03 18:16:34 +01:00
|
|
|
using namespace ProjectExplorer;
|
|
|
|
|
2010-02-16 13:39:13 +01:00
|
|
|
namespace QmlProjectManager {
|
2018-02-15 14:51:31 +01:00
|
|
|
namespace Internal {
|
2009-05-04 12:19:22 +02:00
|
|
|
|
2022-05-06 14:27:58 +03:00
|
|
|
const char alwaysOpenUiQmlMode[] = "J.QtQuick/QmlJSEditor.openUiQmlMode";
|
|
|
|
const char installQdsUrl[] = "https://www.qt.io/product/ui-design-tools";
|
2021-09-21 12:31:19 +02:00
|
|
|
|
|
|
|
static bool isQmlDesigner(const ExtensionSystem::PluginSpec *spec)
|
|
|
|
{
|
|
|
|
if (!spec)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return spec->name().contains("QmlDesigner");
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool qmlDesignerEnabled()
|
|
|
|
{
|
|
|
|
const auto plugins = ExtensionSystem::PluginManager::plugins();
|
|
|
|
const auto it = std::find_if(plugins.begin(), plugins.end(), &isQmlDesigner);
|
|
|
|
return it != plugins.end() && (*it)->plugin();
|
|
|
|
}
|
|
|
|
|
2022-05-06 14:27:58 +03:00
|
|
|
static QString alwaysOpenWithMode()
|
|
|
|
{
|
|
|
|
return Core::ICore::settings()->value(alwaysOpenUiQmlMode, "").toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void setAlwaysOpenWithMode(const QString &mode)
|
2022-01-21 20:08:28 +01:00
|
|
|
{
|
2022-05-06 14:27:58 +03:00
|
|
|
Core::ICore::settings()->setValue(alwaysOpenUiQmlMode, mode);
|
2022-01-21 20:08:28 +01:00
|
|
|
}
|
|
|
|
|
2022-05-06 14:27:58 +03:00
|
|
|
static void clearAlwaysOpenWithMode()
|
2022-01-21 20:08:28 +01:00
|
|
|
{
|
2022-05-06 14:27:58 +03:00
|
|
|
Core::ICore::settings()->remove(alwaysOpenUiQmlMode);
|
2022-01-21 20:08:28 +01:00
|
|
|
}
|
|
|
|
|
2019-03-13 12:22:44 +01:00
|
|
|
class QmlProjectPluginPrivate
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
QmlProjectRunConfigurationFactory runConfigFactory;
|
2021-09-21 12:31:19 +02:00
|
|
|
RunWorkerFactory runWorkerFactory{RunWorkerFactory::make<SimpleTargetRunner>(),
|
|
|
|
{ProjectExplorer::Constants::NORMAL_RUN_MODE},
|
|
|
|
{runConfigFactory.runConfigurationId()}};
|
|
|
|
QPointer<QMessageBox> lastMessageBox;
|
2022-05-06 14:27:58 +03:00
|
|
|
QdsLandingPage *landingPage = nullptr;
|
2019-03-13 12:22:44 +01:00
|
|
|
};
|
|
|
|
|
2009-05-04 12:19:22 +02:00
|
|
|
QmlProjectPlugin::~QmlProjectPlugin()
|
|
|
|
{
|
2021-09-21 12:31:19 +02:00
|
|
|
if (d->lastMessageBox)
|
|
|
|
d->lastMessageBox->deleteLater();
|
2022-05-06 14:27:58 +03:00
|
|
|
if (d->landingPage)
|
|
|
|
d->landingPage->deleteLater();
|
2019-03-13 12:22:44 +01:00
|
|
|
delete d;
|
2009-05-04 12:19:22 +02:00
|
|
|
}
|
|
|
|
|
2021-09-21 12:31:19 +02:00
|
|
|
void QmlProjectPlugin::openQDS(const Utils::FilePath &fileName)
|
|
|
|
{
|
2021-11-17 10:38:52 +01:00
|
|
|
const Utils::FilePath &qdsPath = QmlProjectPlugin::qdsInstallationEntry();
|
2021-09-21 12:31:19 +02:00
|
|
|
bool qdsStarted = false;
|
2022-02-22 16:33:53 +01:00
|
|
|
qputenv(Constants::enviromentLaunchedQDS, "true");
|
2021-09-21 12:31:19 +02:00
|
|
|
//-a and -client arguments help to append project to open design studio application
|
|
|
|
if (Utils::HostOsInfo::isMacHost())
|
2021-11-17 10:38:52 +01:00
|
|
|
qdsStarted = Utils::QtcProcess::startDetached(
|
|
|
|
{"/usr/bin/open", {"-a", qdsPath.path(), fileName.toString()}});
|
2021-09-21 12:31:19 +02:00
|
|
|
else
|
2021-11-17 10:38:52 +01:00
|
|
|
qdsStarted = Utils::QtcProcess::startDetached({qdsPath, {"-client", fileName.toString()}});
|
2021-09-21 12:31:19 +02:00
|
|
|
|
|
|
|
if (!qdsStarted) {
|
|
|
|
QMessageBox::warning(Core::ICore::dialogParent(),
|
|
|
|
fileName.fileName(),
|
|
|
|
QObject::tr("Failed to start Qt Design Studio."));
|
2022-05-06 14:27:58 +03:00
|
|
|
if (alwaysOpenWithMode() == Core::Constants::MODE_DESIGN)
|
|
|
|
clearAlwaysOpenWithMode();
|
2021-09-21 12:31:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-17 10:03:50 +01:00
|
|
|
Utils::FilePath QmlProjectPlugin::qdsInstallationEntry()
|
2021-09-21 12:31:19 +02:00
|
|
|
{
|
|
|
|
QSettings *settings = Core::ICore::settings();
|
|
|
|
const QString qdsInstallationEntry = "QML/Designer/DesignStudioInstallation"; //set in installer
|
|
|
|
|
2021-11-17 10:03:50 +01:00
|
|
|
return Utils::FilePath::fromUserInput(settings->value(qdsInstallationEntry).toString());
|
2021-09-21 12:31:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool QmlProjectPlugin::qdsInstallationExists()
|
|
|
|
{
|
2021-11-17 10:03:50 +01:00
|
|
|
return qdsInstallationEntry().exists();
|
2021-09-21 12:31:19 +02:00
|
|
|
}
|
|
|
|
|
2022-05-06 14:27:58 +03:00
|
|
|
bool QmlProjectPlugin::checkIfEditorIsuiQml(Core::IEditor *editor)
|
2021-09-21 12:31:19 +02:00
|
|
|
{
|
2022-05-06 14:27:58 +03:00
|
|
|
if (editor
|
|
|
|
&& (editor->document()->id() == QmlJSEditor::Constants::C_QMLJSEDITOR_ID
|
|
|
|
|| editor->document()->id() == QmlJSEditor::Constants::C_QTQUICKDESIGNEREDITOR_ID)) {
|
|
|
|
QmlJS::ModelManagerInterface *modelManager = QmlJS::ModelManagerInterface::instance();
|
|
|
|
QmlJS::Document::Ptr document =
|
|
|
|
modelManager->ensuredGetDocumentForPath(editor->document()->filePath().toString());
|
|
|
|
if (!document.isNull())
|
|
|
|
return document->language() == QmlJS::Dialect::QmlQtQuick2Ui;
|
|
|
|
}
|
|
|
|
return false;
|
2021-09-21 12:31:19 +02:00
|
|
|
}
|
|
|
|
|
2022-05-06 14:27:58 +03:00
|
|
|
const Utils::FilePath findQmlProject(const Utils::FilePath &folder)
|
|
|
|
{
|
|
|
|
const Utils::FilePaths files = folder.dirEntries({QStringList("*.qmlproject"), QDir::Files});
|
|
|
|
if (files.isEmpty())
|
|
|
|
return {};
|
|
|
|
|
|
|
|
return files.constFirst();
|
|
|
|
}
|
|
|
|
|
|
|
|
const Utils::FilePath findQmlProjectUpwards(const Utils::FilePath &folder)
|
2021-09-21 12:31:19 +02:00
|
|
|
{
|
|
|
|
auto ret = findQmlProject(folder);
|
|
|
|
if (ret.exists())
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
QDir dir = folder.toDir();
|
|
|
|
if (dir.cdUp())
|
|
|
|
return findQmlProjectUpwards(Utils::FilePath::fromString(dir.absolutePath()));
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool findAndOpenProject(const Utils::FilePath &filePath)
|
|
|
|
{
|
|
|
|
|
|
|
|
ProjectExplorer::Project *project
|
|
|
|
= ProjectExplorer::SessionManager::projectForFile(filePath);
|
|
|
|
|
|
|
|
if (project) {
|
|
|
|
if (project->projectFilePath().suffix() == "qmlproject") {
|
|
|
|
QmlProjectPlugin::openQDS(project->projectFilePath());
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
auto projectFolder = project->rootProjectDirectory();
|
|
|
|
auto qmlProjectFile = findQmlProject(projectFolder);
|
|
|
|
if (qmlProjectFile.exists()) {
|
|
|
|
QmlProjectPlugin::openQDS(qmlProjectFile);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto qmlProjectFile = findQmlProjectUpwards(filePath);
|
|
|
|
if (qmlProjectFile.exists()) {
|
|
|
|
QmlProjectPlugin::openQDS(qmlProjectFile);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-01-21 20:08:28 +01:00
|
|
|
void QmlProjectPlugin::openInQDSWithProject(const Utils::FilePath &filePath)
|
|
|
|
{
|
|
|
|
if (findAndOpenProject(filePath)) {
|
|
|
|
openQDS(filePath);
|
|
|
|
//The first one might be ignored when QDS is starting up
|
|
|
|
QTimer::singleShot(4000, [filePath] { openQDS(filePath); });
|
|
|
|
} else {
|
|
|
|
Core::AsynchronousMessageBox::warning(
|
|
|
|
tr("Qt Design Studio"),
|
|
|
|
tr("No project file (*.qmlproject) found for Qt Design "
|
|
|
|
"Studio.\n Qt Design Studio requires a .qmlproject "
|
|
|
|
"based project to open the .ui.qml file."));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-04 12:19:22 +02:00
|
|
|
bool QmlProjectPlugin::initialize(const QStringList &, QString *errorMessage)
|
|
|
|
{
|
2015-02-04 09:32:46 +01:00
|
|
|
Q_UNUSED(errorMessage)
|
2009-05-04 12:19:22 +02:00
|
|
|
|
2019-03-13 12:22:44 +01:00
|
|
|
d = new QmlProjectPluginPrivate;
|
2010-02-26 14:46:04 +01:00
|
|
|
|
2021-09-21 12:31:19 +02:00
|
|
|
if (!qmlDesignerEnabled()) {
|
2022-05-06 14:27:58 +03:00
|
|
|
initializeQmlLandingPage();
|
2021-09-21 12:31:19 +02:00
|
|
|
}
|
|
|
|
|
2017-03-03 18:16:34 +01:00
|
|
|
ProjectManager::registerProjectType<QmlProject>(QmlJSTools::Constants::QMLPROJECT_MIMETYPE);
|
2021-09-21 12:31:19 +02:00
|
|
|
Core::FileIconProvider::registerIconOverlayForSuffix(":/qmlproject/images/qmlproject.png",
|
|
|
|
"qmlproject");
|
2009-05-04 12:19:22 +02:00
|
|
|
return true;
|
2022-05-12 12:33:12 +03:00
|
|
|
}
|
2009-05-04 12:19:22 +02:00
|
|
|
|
2022-05-06 14:27:58 +03:00
|
|
|
void QmlProjectPlugin::initializeQmlLandingPage()
|
|
|
|
{
|
|
|
|
d->landingPage = new QdsLandingPage();
|
|
|
|
connect(d->landingPage, &QdsLandingPage::openCreator, this, &QmlProjectPlugin::openQtc);
|
|
|
|
connect(d->landingPage, &QdsLandingPage::openDesigner, this, &QmlProjectPlugin::openQds);
|
|
|
|
connect(d->landingPage, &QdsLandingPage::installDesigner, this, &QmlProjectPlugin::installQds);
|
|
|
|
connect(d->landingPage, &QdsLandingPage::generateCmake, this, &QmlProjectPlugin::generateCmake);
|
|
|
|
connect(d->landingPage, &QdsLandingPage::generateProjectFile, this, &QmlProjectPlugin::generateProjectFile);
|
|
|
|
|
|
|
|
auto dialog = d->landingPage->dialog();
|
|
|
|
|
|
|
|
const QStringList mimeTypes = {QmlJSTools::Constants::QMLUI_MIMETYPE};
|
|
|
|
auto context = new Internal::DesignModeContext(dialog);
|
|
|
|
Core::ICore::addContextObject(context);
|
|
|
|
|
|
|
|
Core::DesignMode::registerDesignWidget(dialog, mimeTypes, context->context());
|
|
|
|
|
|
|
|
connect(Core::ModeManager::instance(), &Core::ModeManager::currentModeChanged,
|
|
|
|
this, &QmlProjectPlugin::editorModeChanged);
|
|
|
|
}
|
|
|
|
|
|
|
|
void QmlProjectPlugin::displayQmlLandingPage()
|
|
|
|
{
|
|
|
|
const QString qtVersionString = ProjectFileContentTools::qtVersion(projectFilePath());
|
|
|
|
const QString qdsVersionString = ProjectFileContentTools::qdsVersion(projectFilePath());
|
|
|
|
|
|
|
|
d->landingPage->setQdsInstalled(qdsInstallationExists());
|
|
|
|
d->landingPage->setProjectFileExists(projectFilePath().exists());
|
|
|
|
d->landingPage->setCmakeResources(ProjectFileContentTools::rootCmakeFiles());
|
|
|
|
d->landingPage->setQtVersion(qtVersionString);
|
|
|
|
d->landingPage->setQdsVersion(qdsVersionString);
|
|
|
|
d->landingPage->show();
|
|
|
|
}
|
|
|
|
|
|
|
|
void QmlProjectPlugin::hideQmlLandingPage()
|
|
|
|
{
|
|
|
|
d->landingPage->hide();
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool isDesignerMode(Utils::Id mode)
|
|
|
|
{
|
|
|
|
return mode == Core::Constants::MODE_DESIGN;
|
|
|
|
}
|
|
|
|
|
|
|
|
void QmlProjectPlugin::editorModeChanged(Utils::Id newMode, Utils::Id oldMode)
|
|
|
|
{
|
|
|
|
Core::IEditor *currentEditor = Core::EditorManager::currentEditor();
|
|
|
|
if (checkIfEditorIsuiQml(currentEditor)) {
|
|
|
|
if (isDesignerMode(newMode)) {
|
|
|
|
if (alwaysOpenWithMode() == Core::Constants::MODE_DESIGN)
|
|
|
|
openQds();
|
|
|
|
else if (alwaysOpenWithMode() == Core::Constants::MODE_EDIT)
|
|
|
|
openQtc();
|
|
|
|
else
|
|
|
|
displayQmlLandingPage();
|
|
|
|
} else if (isDesignerMode(oldMode)) {
|
|
|
|
hideQmlLandingPage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void QmlProjectPlugin::openQtc(bool permanent)
|
|
|
|
{
|
|
|
|
if (permanent)
|
|
|
|
setAlwaysOpenWithMode(Core::Constants::MODE_EDIT);
|
|
|
|
|
|
|
|
hideQmlLandingPage();
|
|
|
|
Core::ModeManager::activateMode(Core::Constants::MODE_EDIT);
|
|
|
|
}
|
|
|
|
|
|
|
|
void QmlProjectPlugin::openQds(bool permanent)
|
|
|
|
{
|
|
|
|
if (permanent)
|
|
|
|
setAlwaysOpenWithMode(Core::Constants::MODE_DESIGN);
|
|
|
|
|
|
|
|
hideQmlLandingPage();
|
|
|
|
auto editor = Core::EditorManager::currentEditor();
|
|
|
|
if (editor)
|
|
|
|
openInQDSWithProject(editor->document()->filePath());
|
|
|
|
}
|
|
|
|
|
|
|
|
void QmlProjectPlugin::installQds()
|
|
|
|
{
|
|
|
|
QDesktopServices::openUrl(QUrl(installQdsUrl));
|
|
|
|
hideQmlLandingPage();
|
|
|
|
}
|
|
|
|
|
|
|
|
void QmlProjectPlugin::generateCmake()
|
|
|
|
{
|
|
|
|
qWarning() << "TODO generate cmake";
|
|
|
|
}
|
|
|
|
|
|
|
|
void QmlProjectPlugin::generateProjectFile()
|
|
|
|
{
|
|
|
|
qWarning() << "TODO generate .qmlproject";
|
|
|
|
}
|
|
|
|
|
|
|
|
Utils::FilePath QmlProjectPlugin::projectFilePath()
|
|
|
|
{
|
|
|
|
auto project = ProjectExplorer::SessionManager::startupProject();
|
|
|
|
const QmlProjectManager::QmlProject *qmlProject = qobject_cast<const QmlProjectManager::QmlProject*>(project);
|
|
|
|
if (qmlProject)
|
|
|
|
return qmlProject->projectFilePath();
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2018-02-15 14:51:31 +01:00
|
|
|
} // namespace Internal
|
2010-02-16 13:39:13 +01:00
|
|
|
} // namespace QmlProjectManager
|