1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of Qt Creator.
7 **
8 ** Commercial License Usage
9 ** Licensees holding valid commercial Qt licenses may use this file in
10 ** accordance with the commercial license agreement provided with the
11 ** Software or, alternatively, in accordance with the terms contained in
12 ** a written agreement between you and The Qt Company. For licensing terms
13 ** and conditions see https://www.qt.io/terms-conditions. For further
14 ** information use the contact form at https://www.qt.io/contact-us.
15 **
16 ** GNU General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU
18 ** General Public License version 3 as published by the Free Software
19 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
20 ** included in the packaging of this file. Please review the following
21 ** information to ensure the GNU General Public License requirements will
22 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
23 **
24 ****************************************************************************/
25 
26 #include "qmlprojectrunconfiguration.h"
27 #include "qmlproject.h"
28 #include "qmlprojectmanagerconstants.h"
29 #include "qmlmainfileaspect.h"
30 #include "qmlmultilanguageaspect.h"
31 
32 #include <coreplugin/editormanager/editormanager.h>
33 #include <coreplugin/editormanager/ieditor.h>
34 #include <coreplugin/icore.h>
35 #include <coreplugin/idocument.h>
36 
37 #include <projectexplorer/environmentaspect.h>
38 #include <projectexplorer/kitinformation.h>
39 #include <projectexplorer/kitmanager.h>
40 #include <projectexplorer/projectexplorer.h>
41 #include <projectexplorer/runconfigurationaspects.h>
42 #include <projectexplorer/runcontrol.h>
43 #include <projectexplorer/session.h>
44 #include <projectexplorer/target.h>
45 
46 #include <qtsupport/qtkitinformation.h>
47 #include <qtsupport/qtsupportconstants.h>
48 
49 #include <utils/aspects.h>
50 #include <utils/environment.h>
51 #include <utils/fileutils.h>
52 #include <utils/mimetypes/mimedatabase.h>
53 #include <utils/qtcprocess.h>
54 #include <utils/winutils.h>
55 
56 #include <qmljstools/qmljstoolsconstants.h>
57 
58 using namespace Core;
59 using namespace ProjectExplorer;
60 using namespace QtSupport;
61 using namespace Utils;
62 
63 namespace QmlProjectManager {
64 class QmlMultiLanguageAspect;
65 namespace Internal {
66 
67 // QmlProjectRunConfiguration
68 
isQtDesignStudio()69 static bool isQtDesignStudio()
70 {
71     QSettings *settings = Core::ICore::settings();
72     const QString qdsStandaloneEntry = "QML/Designer/StandAloneMode"; //entry from qml settings
73 
74     return settings->value(qdsStandaloneEntry, false).toBool();
75 }
76 
77 class QmlProjectRunConfiguration final : public RunConfiguration
78 {
79     Q_DECLARE_TR_FUNCTIONS(QmlProjectManager::QmlProjectRunConfiguration)
80 
81 public:
82     QmlProjectRunConfiguration(Target *target, Id id);
83 
84 private:
85     QString disabledReason() const final;
86     bool isEnabled() const final;
87 
88     QString mainScript() const;
89     FilePath qmlRuntimeFilePath() const;
90     QString commandLineArguments() const;
91     void createQtVersionAspect();
92 
93     StringAspect *m_qmlViewerAspect = nullptr;
94     QmlMainFileAspect *m_qmlMainFileAspect = nullptr;
95     QmlMultiLanguageAspect *m_multiLanguageAspect = nullptr;
96     SelectionAspect *m_qtversionAspect = nullptr;
97 };
98 
QmlProjectRunConfiguration(Target * target,Id id)99 QmlProjectRunConfiguration::QmlProjectRunConfiguration(Target *target, Id id)
100     : RunConfiguration(target, id)
101 {
102     m_qmlViewerAspect = addAspect<StringAspect>();
103     m_qmlViewerAspect->setLabelText(tr("QML Viewer:"));
104     m_qmlViewerAspect->setPlaceHolderText(commandLine().executable().toString());
105     m_qmlViewerAspect->setDisplayStyle(StringAspect::LineEditDisplay);
106     m_qmlViewerAspect->setHistoryCompleter("QmlProjectManager.viewer.history");
107 
108     auto argumentAspect = addAspect<ArgumentsAspect>();
109     argumentAspect->setSettingsKey(Constants::QML_VIEWER_ARGUMENTS_KEY);
110 
111     setCommandLineGetter([this] {
112         return CommandLine(qmlRuntimeFilePath(), commandLineArguments(), CommandLine::Raw);
113     });
114 
115     m_qmlMainFileAspect = addAspect<QmlMainFileAspect>(target);
116     connect(m_qmlMainFileAspect, &QmlMainFileAspect::changed, this, &RunConfiguration::update);
117 
118     createQtVersionAspect();
119 
120     connect(target, &Target::kitChanged, this, &RunConfiguration::update);
121 
122     m_multiLanguageAspect = addAspect<QmlMultiLanguageAspect>(target);
123 
124     auto envAspect = addAspect<EnvironmentAspect>();
125     connect(m_multiLanguageAspect, &QmlMultiLanguageAspect::changed, envAspect, &EnvironmentAspect::environmentChanged);
126 
127     auto envModifier = [this](Environment env) {
128         if (auto bs = dynamic_cast<const QmlBuildSystem *>(activeBuildSystem()))
129             env.modify(bs->environment());
130 
131         if (m_multiLanguageAspect && m_multiLanguageAspect->value() && !m_multiLanguageAspect->databaseFilePath().isEmpty()) {
132             env.set("QT_MULTILANGUAGE_DATABASE", m_multiLanguageAspect->databaseFilePath().toString());
133             env.set("QT_MULTILANGUAGE_LANGUAGE", m_multiLanguageAspect->currentLocale());
134         } else {
135             env.unset("QT_MULTILANGUAGE_DATABASE");
136             env.unset("QT_MULTILANGUAGE_LANGUAGE");
137         }
138         return env;
139     };
140 
141     const Id deviceTypeId = DeviceTypeKitAspect::deviceTypeId(target->kit());
142     if (deviceTypeId == ProjectExplorer::Constants::DESKTOP_DEVICE_TYPE) {
143         envAspect->addPreferredBaseEnvironment(tr("System Environment"), [envModifier] {
144             return envModifier(Environment::systemEnvironment());
145         });
146     }
147 
148     envAspect->addSupportedBaseEnvironment(tr("Clean Environment"), [envModifier] {
149         Environment environment;
150         return envModifier(environment);
151     });
152 
153     setRunnableModifier([this](Runnable &r) {
154         const QmlBuildSystem *bs = static_cast<QmlBuildSystem *>(activeBuildSystem());
155         r.workingDirectory = bs->targetDirectory().toString();
156     });
157 
158     setDisplayName(tr("QML Utility", "QMLRunConfiguration display name."));
159     update();
160 }
161 
disabledReason() const162 QString QmlProjectRunConfiguration::disabledReason() const
163 {
164     if (mainScript().isEmpty())
165         return tr("No script file to execute.");
166 
167     const FilePath viewer = qmlRuntimeFilePath();
168     if (DeviceTypeKitAspect::deviceTypeId(kit())
169             == ProjectExplorer::Constants::DESKTOP_DEVICE_TYPE
170             && !viewer.exists()) {
171         return tr("No QML utility found.");
172     }
173     if (viewer.isEmpty())
174         return tr("No QML utility specified for target device.");
175     return RunConfiguration::disabledReason();
176 }
177 
qmlRuntimeFilePath() const178 FilePath QmlProjectRunConfiguration::qmlRuntimeFilePath() const
179 {
180     const QString qmlViewer = m_qmlViewerAspect->value();
181     if (!qmlViewer.isEmpty())
182         return FilePath::fromString(qmlViewer);
183 
184     Kit *kit = target()->kit();
185     BaseQtVersion *version = QtKitAspect::qtVersion(kit);
186     if (!version) // No Qt version in Kit. Don't try to run QML runtime.
187         return {};
188 
189     const Id deviceType = DeviceTypeKitAspect::deviceTypeId(kit);
190     if (deviceType == ProjectExplorer::Constants::DESKTOP_DEVICE_TYPE) {
191         // If not given explicitly by Qt Version, try to pick it from $PATH.
192         const bool isDesktop = version->type() == QtSupport::Constants::DESKTOPQT;
193         return isDesktop ? version->qmlRuntimeFilePath() : FilePath::fromString("qmlscene");
194     }
195 
196     IDevice::ConstPtr dev = DeviceKitAspect::device(kit);
197     if (dev.isNull()) // No device set. We don't know where a QML utility is.
198         return {};
199 
200     const QString qmlRuntime = dev->qmlRunCommand();
201     // If not given explicitly by device, try to pick it from $PATH.
202     return FilePath::fromString(qmlRuntime.isEmpty() ? QString("qmlscene") : qmlRuntime);
203 }
204 
commandLineArguments() const205 QString QmlProjectRunConfiguration::commandLineArguments() const
206 {
207     // arguments in .user file
208     QString args = aspect<ArgumentsAspect>()->arguments(macroExpander());
209     const IDevice::ConstPtr device = DeviceKitAspect::device(kit());
210     const OsType osType = device ? device->osType() : HostOsInfo::hostOs();
211 
212     // arguments from .qmlproject file
213     const QmlBuildSystem *bs = qobject_cast<QmlBuildSystem *>(target()->buildSystem());
214     foreach (const QString &importPath,
215              QmlBuildSystem::makeAbsolute(bs->targetDirectory(), bs->customImportPaths())) {
216         ProcessArgs::addArg(&args, "-I", osType);
217         ProcessArgs::addArg(&args, importPath, osType);
218     }
219 
220     for (const QString &fileSelector : bs->customFileSelectors()) {
221         ProcessArgs::addArg(&args, "-S", osType);
222         ProcessArgs::addArg(&args, fileSelector, osType);
223     }
224 
225     if (HostOsInfo::isWindowsHost() && bs->forceFreeType()) {
226         ProcessArgs::addArg(&args, "-platform", osType);
227         ProcessArgs::addArg(&args, "windows:fontengine=freetype", osType);
228     }
229 
230     const QString main = bs->targetFile(FilePath::fromString(mainScript())).toString();
231     if (!main.isEmpty())
232         ProcessArgs::addArg(&args, main, osType);
233 
234     if (m_multiLanguageAspect && m_multiLanguageAspect->value())
235         ProcessArgs::addArg(&args, "-qmljsdebugger=file:unused_if_debugger_arguments_added,services:DebugTranslation", osType);
236 
237     return args;
238 }
239 
createQtVersionAspect()240 void QmlProjectRunConfiguration::createQtVersionAspect()
241 {
242     if (!isQtDesignStudio())
243         return;
244 
245     m_qtversionAspect = addAspect<SelectionAspect>();
246     m_qtversionAspect->setDisplayStyle(SelectionAspect::DisplayStyle::ComboBox);
247     m_qtversionAspect->setLabelText(tr("Qt Version:"));
248     m_qtversionAspect->setSettingsKey("QmlProjectManager.kit");
249 
250     Kit *kit = target()->kit();
251     BaseQtVersion *version = QtKitAspect::qtVersion(kit);
252 
253     if (version) {
254         const QmlBuildSystem *buildSystem = qobject_cast<QmlBuildSystem *>(target()->buildSystem());
255         const bool isQt6Project = buildSystem && buildSystem->qt6Project();
256 
257         if (isQt6Project) {
258             m_qtversionAspect->addOption(tr("Qt 6"));
259             m_qtversionAspect->setReadOnly(true);
260         } else { /* Only if this is not a Qt 6 project changing kits makes sense */
261             m_qtversionAspect->addOption(tr("Qt 5"));
262             m_qtversionAspect->addOption(tr("Qt 6"));
263 
264             const int valueForVersion = version->qtVersion().majorVersion == 6 ? 1 : 0;
265 
266             m_qtversionAspect->setValue(valueForVersion);
267 
268             connect(m_qtversionAspect, &SelectionAspect::changed, this, [&]() {
269                 QTC_ASSERT(target(), return );
270                 auto project = target()->project();
271                 QTC_ASSERT(project, return );
272 
273                 int oldValue = !m_qtversionAspect->value();
274                 const int preferedQtVersion = m_qtversionAspect->value() > 0 ? 6 : 5;
275                 Kit *currentKit = target()->kit();
276 
277                 const QList<Kit *> kits = Utils::filtered(KitManager::kits(), [&](const Kit *k) {
278                     QtSupport::BaseQtVersion *version = QtSupport::QtKitAspect::qtVersion(k);
279                     return (version && version->qtVersion().majorVersion == preferedQtVersion)
280                            && DeviceTypeKitAspect::deviceTypeId(k)
281                                   == ProjectExplorer::Constants::DESKTOP_DEVICE_TYPE;
282                 });
283 
284                 if (kits.contains(currentKit))
285                     return;
286 
287                 if (!kits.isEmpty()) {
288                     auto newTarget = target()->project()->target(kits.first());
289                     if (!newTarget)
290                         newTarget = project->addTargetForKit(kits.first());
291 
292                     SessionManager::setActiveTarget(project, newTarget, SetActive::Cascade);
293 
294                     /* Reset the aspect. We changed the target and this aspect should not change. */
295                     m_qtversionAspect->blockSignals(true);
296                     m_qtversionAspect->setValue(oldValue);
297                     m_qtversionAspect->blockSignals(false);
298                 }
299             });
300         }
301     }
302 }
303 
isEnabled() const304 bool QmlProjectRunConfiguration::isEnabled() const
305 {
306     return m_qmlMainFileAspect->isQmlFilePresent() && !commandLine().executable().isEmpty()
307             && activeBuildSystem()->hasParsingData();
308 }
309 
mainScript() const310 QString QmlProjectRunConfiguration::mainScript() const
311 {
312     return m_qmlMainFileAspect->mainScript();
313 }
314 
315 // QmlProjectRunConfigurationFactory
316 
QmlProjectRunConfigurationFactory()317 QmlProjectRunConfigurationFactory::QmlProjectRunConfigurationFactory()
318     : FixedRunConfigurationFactory(QmlProjectRunConfiguration::tr("QML Runtime"), false)
319 {
320     registerRunConfiguration<QmlProjectRunConfiguration>
321             ("QmlProjectManager.QmlRunConfiguration.Qml");
322     addSupportedProjectType(QmlProjectManager::Constants::QML_PROJECT_ID);
323 }
324 
325 } // namespace Internal
326 } // namespace QmlProjectManager
327