aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/qmlpreview/qmlpreviewruncontrol.cpp
blob: dfa4871de5f89f4e8cbede4d09853f2a04b51722 (plain)
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Copyright (C) 2019 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "qmlpreviewruncontrol.h"

#include "projectexplorer/runcontrol.h"
#include "qmlpreviewconnectionmanager.h"

#include <projectexplorer/buildconfiguration.h>
#include <projectexplorer/projectexplorer.h>
#include <projectexplorer/qmldebugcommandlinearguments.h>
#include <projectexplorer/target.h>

#include <qmlprojectmanager/buildsystem/qmlbuildsystem.h>
#include <qmlprojectmanager/qmlmainfileaspect.h>
#include <qmlprojectmanager/qmlmultilanguageaspect.h>

#include <utils/async.h>
#include <utils/filepath.h>
#include <utils/qtcprocess.h>
#include <utils/url.h>

using namespace ProjectExplorer;
using namespace Tasking;
using namespace Utils;

namespace QmlPreview {

static Group qmlPreviewRecipe(RunControl *runControl)
{
    const auto onTranslationSetup = [](Async<void> &task) {
        if (!QmlPreviewPlugin::settings().refreshTranslationsFunction)
            return SetupResult::StopWithSuccess;

        task.setConcurrentCallData(QmlPreviewPlugin::settings().refreshTranslationsFunction);
        // Cancel and blocking wait for finished when deleting the translation task.
        task.setFutureSynchronizer(nullptr);
        return SetupResult::Continue;
    };

    const auto onPreviewSetup = [runControl](QmlPreviewConnectionManager &task) {
        QmlPreviewPlugin *plugin = QmlPreviewPlugin::instance();
        const QmlPreviewRunnerSetting &settings = QmlPreviewPlugin::settings();
        task.setFileLoader(settings.fileLoader);
        task.setFileClassifier(settings.fileClassifier);
        task.setFpsHandler(settings.fpsHandler);
        task.setQmlDebugTranslationClientCreator(settings.createDebugTranslationClientMethod);

        QObject::connect(plugin, &QmlPreviewPlugin::updatePreviews,
                         &task, &QmlPreviewConnectionManager::loadFile);
        QObject::connect(plugin, &QmlPreviewPlugin::rerunPreviews,
                         &task, &QmlPreviewConnectionManager::rerun);
        QObject::connect(plugin, &QmlPreviewPlugin::zoomFactorChanged,
                         &task, &QmlPreviewConnectionManager::zoom);
        QObject::connect(plugin, &QmlPreviewPlugin::localeIsoCodeChanged,
                         &task, &QmlPreviewConnectionManager::language);

        QObject::connect(&task, &QmlPreviewConnectionManager::connectionOpened,
                         &task, [task = &task, plugin, settings] {
            if (settings.zoomFactor > 0)
                task->zoom(settings.zoomFactor);
            if (auto multiLanguageAspect = QmlProjectManager::QmlMultiLanguageAspect::current()) {
                if (!multiLanguageAspect->currentLocale().isEmpty())
                    task->language(multiLanguageAspect->currentLocale());
            }
            plugin->previewCurrentFile();
        });

        QObject::connect(&task, &QmlPreviewConnectionManager::restart, runControl, [runControl] {
            if (!runControl->isRunning())
                return;

            QObject::connect(runControl, &RunControl::stopped,
                             ProjectExplorerPlugin::instance(), [runControl] {
                auto rc = new RunControl(ProjectExplorer::Constants::QML_PREVIEW_RUN_MODE);
                rc->copyDataFromRunControl(runControl);
                rc->start();
            });

            runControl->initiateStop();
        });

        task.setBuildConfiguration(runControl->buildConfiguration());
        task.setServer(runControl->qmlChannel());
        plugin->addPreview(runControl);
    };

    const auto onDone = [runControl] {
        QmlPreviewPlugin::instance()->removePreview(runControl);
    };

    return Group {
        parallel,
        onGroupSetup([] { emit runStorage()->started(); }),
        AsyncTask<void>(onTranslationSetup),
        QmlPreviewConnectionManagerTask(onPreviewSetup),
        onGroupDone(onDone)
    }.withCancel(canceler());
}

QmlPreviewRunWorkerFactory::QmlPreviewRunWorkerFactory()
{
    setRecipeProducer(qmlPreviewRecipe);
    addSupportedRunMode(Constants::QML_PREVIEW_RUNNER);
}

LocalQmlPreviewSupportFactory::LocalQmlPreviewSupportFactory()
{
    setId(ProjectExplorer::Constants::QML_PREVIEW_RUN_FACTORY);
    setProducer([](RunControl *runControl) {
        runControl->setQmlChannel(Utils::urlFromLocalSocket());

        // Create QmlPreviewRunner
        RunWorker *preview = runControl->createWorker(ProjectExplorer::Constants::QML_PREVIEW_RUNNER);

        const auto modifier = [runControl](Process &process) {
            CommandLine cmd = runControl->commandLine();

            if (const auto aspect = runControl->aspectData<QmlProjectManager::QmlMainFileAspect>()) {
                const auto qmlBuildSystem = qobject_cast<QmlProjectManager::QmlBuildSystem *>(
                    runControl->buildConfiguration()->buildSystem());
                QTC_ASSERT(qmlBuildSystem, return SetupResult::StopWithError);

                const FilePath mainScript = aspect->mainScript;
                const FilePath currentFile = aspect->currentFile;

                const QString mainScriptFromProject = qmlBuildSystem->targetFile(mainScript).path();

                QStringList qmlProjectRunConfigurationArguments = cmd.splitArguments();

                if (!currentFile.isEmpty() && qmlProjectRunConfigurationArguments.last().contains(mainScriptFromProject)) {
                    qmlProjectRunConfigurationArguments.removeLast();
                    cmd = CommandLine(cmd.executable(), qmlProjectRunConfigurationArguments);
                    cmd.addArg(currentFile.path());
                }
            }

            cmd.addArg(qmlDebugLocalArguments(QmlPreviewServices, runControl->qmlChannel().path()));
            process.setCommand(cmd.toLocal());
            return SetupResult::Continue;
        };
        auto worker = createProcessWorker(runControl, modifier);
        worker->addStopDependency(preview);
        worker->addStartDependency(preview);
        return worker;
    });
    addSupportedRunMode(ProjectExplorer::Constants::QML_PREVIEW_RUN_MODE);
    addSupportedDeviceType(ProjectExplorer::Constants::DESKTOP_DEVICE_TYPE);

    addSupportForLocalRunConfigs();
}

} // QmlPreview