summaryrefslogtreecommitdiff
path: root/src/plugins/python/pythonwizardpage.cpp
blob: 1b5798f1733e81ca600c6d5c1c2f20f8d2f4871f (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
154
155
156
157
158
159
160
161
162
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "pythonwizardpage.h"

#include "pythonconstants.h"
#include "pythonsettings.h"
#include "pythontr.h"

#include <coreplugin/generatedfile.h>

#include <utils/algorithm.h>
#include <utils/layoutbuilder.h>
#include <utils/mimeutils.h>
#include <utils/qtcassert.h>

#include <projectexplorer/project.h>
#include <projectexplorer/projectmanager.h>
#include <projectexplorer/target.h>

using namespace ProjectExplorer;
using namespace Utils;

namespace Python::Internal {

PythonWizardPageFactory::PythonWizardPageFactory()
{
    setTypeIdsSuffix("PythonConfiguration");
}

WizardPage *PythonWizardPageFactory::create(JsonWizard *wizard, Id typeId, const QVariant &data)
{
    Q_UNUSED(wizard)

    QTC_ASSERT(canCreate(typeId), return nullptr);

    auto page = new PythonWizardPage;
    for (const QVariant &item : data.toMap().value("items").toList()) {
        const QMap<QString, QVariant> map = item.toMap();
        const QVariant name = map.value("trKey");
        if (name.isValid())
            page->addPySideVersions(name.toString(), map.value("value"));
    }
    bool validIndex = false;
    const int index = data.toMap().value("index").toInt(&validIndex);
    if (validIndex)
        page->setDefaultPySideVersions(index);
    return page;
}

static bool validItem(const QVariant &item)
{
    QMap<QString, QVariant> map = item.toMap();
    if (!map.value("trKey").canConvert<QString>())
        return false;
    map = map.value("value").toMap();
    return map.value("PySideVersion").canConvert<QString>();
}

bool PythonWizardPageFactory::validateData(Id typeId, const QVariant &data, QString *errorMessage)
{
    QTC_ASSERT(canCreate(typeId), return false);
    const QList<QVariant> items = data.toMap().value("items").toList();

    if (items.isEmpty()) {
        if (errorMessage) {
            *errorMessage = Tr::tr("'data' of a Python wizard page expects a map with 'items' "
                                   "containing a list of objects");
        }
        return false;
    }

    if (!Utils::allOf(items, &validItem)) {
        if (errorMessage) {
            *errorMessage = Tr::tr(
                "An item of Python wizard page data expects a 'trKey' field containing the ui "
                "visible string for that python version and an field 'value' containing an object "
                "with a 'PySideVersion' field used for import statements in the python files.");
        }
        return false;
    }
    return true;
}

PythonWizardPage::PythonWizardPage()
{
    m_interpreter.setSettingsDialogId(Constants::C_PYTHONOPTIONS_PAGE_ID);
    connect(PythonSettings::instance(),
            &PythonSettings::interpretersChanged,
            this,
            &PythonWizardPage::updateInterpreters);

    m_pySideVersion.setLabelText(Tr::tr("PySide version"));
    m_pySideVersion.setDisplayStyle(SelectionAspect::DisplayStyle::ComboBox);
}

void PythonWizardPage::initializePage()
{
    using namespace Utils::Layouting;

    auto wiz = qobject_cast<JsonWizard *>(wizard());
    QTC_ASSERT(wiz, return);

    updateInterpreters();

    connect(wiz, &JsonWizard::filesPolished, this, &PythonWizardPage::setupProject);

    Grid {
         m_pySideVersion, br,
         m_interpreter, br
    }.attachTo(this, WithoutMargins);
}

bool PythonWizardPage::validatePage()
{
    auto wiz = qobject_cast<JsonWizard *>(wizard());
    const QMap<QString, QVariant> data = m_pySideVersion.itemValue().toMap();
    for (auto it = data.begin(), end = data.end(); it != end; ++it)
        wiz->setValue(it.key(), it.value());
    return true;
}

void PythonWizardPage::addPySideVersions(const QString &name, const QVariant &data)
{
    m_pySideVersion.addOption(SelectionAspect::Option(name, {}, data));
}

void PythonWizardPage::setDefaultPySideVersions(int index)
{
    m_pySideVersion.setDefaultValue(index);
}

void PythonWizardPage::setupProject(const JsonWizard::GeneratorFiles &files)
{
    for (const JsonWizard::GeneratorFile &f : files) {
        if (f.file.attributes() & Core::GeneratedFile::OpenProjectAttribute) {
            Project *project = ProjectManager::openProject(Utils::mimeTypeForFile(f.file.filePath()),
                                                           f.file.filePath().absoluteFilePath());
            if (project) {
                project->addTargetForDefaultKit();
                if (Target *target = project->activeTarget()) {
                    if (RunConfiguration *rc = target->activeRunConfiguration()) {
                        if (auto interpreters = rc->aspect<InterpreterAspect>()) {
                            interpreters->setCurrentInterpreter(m_interpreter.currentInterpreter());
                            project->saveSettings();
                        }
                    }
                }
                delete project;
            }
        }
    }
}

void PythonWizardPage::updateInterpreters()
{
    m_interpreter.setDefaultInterpreter(PythonSettings::defaultInterpreter());
    m_interpreter.updateInterpreters(PythonSettings::interpreters());
}

} // namespace Python::Internal