aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/qmlprojectmanager/qmlprojectexporter/pythongenerator.cpp
blob: 145bce29a2fd11724a5eceec03619087cc473ce8 (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
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "pythongenerator.h"

#include "cmakewriter.h"
#include "resourcegenerator.h"
#include "../qmlproject.h"

#include <projectexplorer/projectmanager.h>

#include <QMenu>

namespace QmlProjectManager::QmlProjectExporter {

void PythonGenerator::createMenuAction(QObject *parent)
{
    QAction *action = FileGenerator::createMenuAction(parent,
                                                      "Enable Python Generator",
                                                      "QmlProject.EnablePythonGenerator");

    QObject::connect(ProjectExplorer::ProjectManager::instance(),
                     &ProjectExplorer::ProjectManager::startupProjectChanged,
                     [action]() {
                         if (auto buildSystem = QmlBuildSystem::getStartupBuildSystem()) {
                             action->setEnabled(!buildSystem->qtForMCUs());
                             action->setChecked(buildSystem->enablePythonGeneration());
                         }
                     });

    QObject::connect(action, &QAction::toggled, [](bool checked) {
        if (auto buildSystem = QmlBuildSystem::getStartupBuildSystem())
            buildSystem->setEnablePythonGeneration(checked);
    });
}

PythonGenerator::PythonGenerator(QmlBuildSystem *bs)
    : FileGenerator(bs)
{}

void PythonGenerator::updateMenuAction()
{
    FileGenerator::updateMenuAction("QmlProject.EnablePythonGenerator",
                                    [this]() { return buildSystem()->enablePythonGeneration(); });
}

void PythonGenerator::updateProject(QmlProject *project)
{
    if (!isEnabled())
        return;

    Utils::FilePath projectPath = project->rootProjectDirectory();
    Utils::FilePath pythonFolderPath = projectPath.pathAppended("Python");
    if (!pythonFolderPath.exists())
        pythonFolderPath.createDir();

    Utils::FilePath mainFilePath = pythonFolderPath.pathAppended("main.py");
    if (!mainFilePath.exists()) {
        const QString mainFileTemplate = CMakeWriter::readTemplate(
            ":/templates/python_generator_main");
        CMakeWriter::writeFile(mainFilePath, mainFileTemplate);
    }

    Utils::FilePath pyprojectFilePath = pythonFolderPath.pathAppended("pyproject.toml");
    if (!pyprojectFilePath.exists()) {
        const QString pyprojectFileTemplate = CMakeWriter::readTemplate(
            ":/templates/python_pyproject_toml");
        const QString pyprojectFileContent = pyprojectFileTemplate.arg(project->displayName());
        CMakeWriter::writeFile(pyprojectFilePath, pyprojectFileContent);
    }

    Utils::FilePath autogenFolderPath = pythonFolderPath.pathAppended("autogen");
    if (!autogenFolderPath.exists())
        autogenFolderPath.createDir();

    Utils::FilePath settingsFilePath = autogenFolderPath.pathAppended("settings.py");
    const QString settingsFileTemplate = CMakeWriter::readTemplate(
        ":/templates/python_generator_settings");
    const QString settingsFileContent = settingsFileTemplate.arg(buildSystem()->mainFile());
    CMakeWriter::writeFile(settingsFilePath, settingsFileContent);

    // Python code uses the Qt resources collection file (.qrc)
    ResourceGenerator::createQrc(project);
}

/*!
    Regenerates the .qrc resources file
*/
void PythonGenerator::update(const QSet<QString> &added, const QSet<QString> &removed) {
    Q_UNUSED(added)
    Q_UNUSED(removed)
    ResourceGenerator::createQrc(qmlProject());
    // Generated Python code does not need to be updated
};

} // namespace QmlProjectExporter::QmlProjectManager.