summaryrefslogtreecommitdiff
path: root/src/plugins/qmlprojectmanager/qdslandingpage.cpp
blob: d710afc4b90a950983ae1f5a634e549386794c6b (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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0

#include "qdslandingpage.h"
#include "projectfilecontenttools.h"
#include "qdslandingpagetheme.h"
#include "qmlprojectconstants.h"
#include "qmlprojectgen/qmlprojectgenerator.h"
#include "qmlprojectplugin.h"
#include "utils/algorithm.h"

#include <coreplugin/coreconstants.h>
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/icore.h>
#include <coreplugin/modemanager.h>

#include <QDesktopServices>
#include <QHBoxLayout>
#include <QQuickItem>
#include <QtQml/QQmlEngine>

namespace QmlProjectManager {
namespace Internal {

const char INSTALL_QDS_URL[] = "https://www.qt.io/product/ui-design-tools";

QdsLandingPageWidget::QdsLandingPageWidget(QWidget* parent)
    : QWidget(parent)
{
    setLayout(new QHBoxLayout());
    layout()->setContentsMargins(0, 0, 0, 0);
}

QdsLandingPageWidget::~QdsLandingPageWidget()
{
    if (m_widget)
        m_widget->deleteLater();
}

QQuickWidget *QdsLandingPageWidget::widget()
{
    if (!m_widget) {
        m_widget = new QQuickWidget();

        const QString resourcePath
            = Core::ICore::resourcePath(QmlProjectManager::Constants::QML_RESOURCE_PATH).toString();
        const QString landingPath
            = Core::ICore::resourcePath(QmlProjectManager::Constants::LANDING_PAGE_PATH).toString();

        QdsLandingPageTheme::setupTheme(m_widget->engine());

        m_widget->setResizeMode(QQuickWidget::SizeRootObjectToView);
        m_widget->engine()->addImportPath(landingPath + "/imports");
        m_widget->engine()->addImportPath(resourcePath);
        m_widget->engine()->addImportPath("qrc:/studiofonts");
        m_widget->setSource(QUrl::fromLocalFile(landingPath + "/main.qml"));
        m_widget->hide();

        layout()->addWidget(m_widget);
    }

    return m_widget;
}

QdsLandingPage::QdsLandingPage()
    : m_widget{nullptr}
{}

void QdsLandingPage::openQtc(bool rememberSelection)
{
    if (rememberSelection)
        Core::ICore::settings()->setValue(QmlProjectManager::Constants::ALWAYS_OPEN_UI_MODE,
                                          Core::Constants::MODE_EDIT);

    hide();

    Core::ModeManager::activateMode(Core::Constants::MODE_EDIT);
}

void QdsLandingPage::openQds(bool rememberSelection)
{
    if (rememberSelection)
        Core::ICore::settings()->setValue(QmlProjectManager::Constants::ALWAYS_OPEN_UI_MODE,
                                          Core::Constants::MODE_DESIGN);

    auto editor = Core::EditorManager::currentEditor();
    if (editor)
        QmlProjectPlugin::openInQDSWithProject(editor->document()->filePath());
}

void QdsLandingPage::installQds()
{
    QDesktopServices::openUrl(QUrl(INSTALL_QDS_URL));
}

void QdsLandingPage::generateProjectFile()
{
    GenerateQmlProject::QmlProjectFileGenerator generator;

    Core::IEditor *editor = Core::EditorManager::currentEditor();
    if (!editor)
        return;

    if (generator.prepareForUiQmlFile(editor->document()->filePath())) {
        if (generator.execute()) {
            const QString qtVersion = ProjectFileContentTools::qtVersion(generator.targetFile());
            const QString qdsVersion = ProjectFileContentTools::qdsVersion(generator.targetFile());
            setProjectFileExists(generator.targetFile().exists());
            setQtVersion(qtVersion);
            setQdsVersion(qdsVersion);
        }
    }
}

void QdsLandingPage::setWidget(QWidget *widget)
{
    m_widget = widget;
}

QWidget *QdsLandingPage::widget()
{
    return m_widget;
}

void QdsLandingPage::show()
{
    if (!m_widget)
        return;

    m_widget->show();
}

void QdsLandingPage::hide()
{
    if (!m_widget)
        return;

    m_widget->hide();
}

bool QdsLandingPage::qdsInstalled() const
{
    return m_qdsInstalled;
}

void QdsLandingPage::setQdsInstalled(bool installed)
{
    if (m_qdsInstalled != installed) {
        m_qdsInstalled = installed;
        emit qdsInstalledChanged();
    }
}

bool QdsLandingPage::projectFileExists() const
{
    return m_projectFileExists;
}

void QdsLandingPage::setProjectFileExists(bool exists)
{
    if (m_projectFileExists != exists) {
        m_projectFileExists = exists;
        emit projectFileExistshanged();
    }
}

const QString QdsLandingPage::qtVersion() const
{
    return m_qtVersion;
}

void QdsLandingPage::setQtVersion(const QString &version)
{
    if (m_qtVersion != version) {
        m_qtVersion = version;
        emit qtVersionChanged();
    }
}

const QString QdsLandingPage::qdsVersion() const
{
    return m_qdsVersion;
}

void QdsLandingPage::setQdsVersion(const QString &version)
{
    if (m_qdsVersion != version) {
        m_qdsVersion = version;
        emit qdsVersionChanged();
    }
}

const QStringList QdsLandingPage::cmakeResources() const
{
    return m_cmakeResources;
}

void QdsLandingPage::setCmakeResources(const Utils::FilePaths &resources)
{
    QStringList strings = Utils::transform(resources,
                                           [](const Utils::FilePath &path)
                                                { return path.fileName(); });
    setCmakeResources(strings);
}

void QdsLandingPage::setCmakeResources(const QStringList &resources)
{
    m_cmakeResources = resources;
}

} // namespace Internal
} // namespace QmlProjectManager