Line data Source code
1 : /*
2 : * SPDX-License-Identifier: GPL-3.0-only
3 : * MuseScore-CLA-applies
4 : *
5 : * MuseScore Studio
6 : * Music Composition & Notation
7 : *
8 : * Copyright (C) 2021 MuseScore Limited and others
9 : *
10 : * This program is free software: you can redistribute it and/or modify
11 : * it under the terms of the GNU General Public License version 3 as
12 : * published by the Free Software Foundation.
13 : *
14 : * This program is distributed in the hope that it will be useful,
15 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 : * GNU General Public License for more details.
18 : *
19 : * You should have received a copy of the GNU General Public License
20 : * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 : */
22 :
23 : #include "uiengine.h"
24 :
25 : #include <QQmlApplicationEngine>
26 : #include <QDir>
27 : #include <QQmlContext>
28 : #include <QEventLoop>
29 : #include <QFontDatabase>
30 : #include <QTimer>
31 :
32 : #include "global/types/color.h"
33 : #include "graphicsapiprovider.h"
34 :
35 : using namespace muse::ui;
36 :
37 0 : UiEngine::UiEngine(const modularity::ContextPtr& iocCtx)
38 0 : : Contextable(iocCtx)
39 : {
40 0 : m_engine = new QQmlApplicationEngine(this);
41 0 : m_apiEngine = new muse::api::JsApiEngine(m_engine, iocContext());
42 0 : m_translation = new QmlTranslation(this);
43 0 : m_api = new QmlApi(this, iocContext());
44 0 : m_tooltip = new QmlToolTip(this, iocContext());
45 0 : m_dataFormatter = new QmlDataFormatter(this);
46 0 : }
47 :
48 0 : UiEngine::~UiEngine()
49 : {
50 0 : delete m_translation;
51 0 : }
52 :
53 0 : void UiEngine::init()
54 : {
55 0 : QmlIoCContext* qmlIoc = new QmlIoCContext(this);
56 0 : qmlIoc->ctx = iocContext();
57 :
58 0 : QQmlContext* rootContext = m_engine->rootContext();
59 0 : rootContext->setObjectName(QString("Root QQmlContext: %1").arg(qmlIoc->ctx->id));
60 0 : rootContext->setContextProperty("ioc_context", QVariant::fromValue(qmlIoc));
61 :
62 0 : rootContext->setContextProperty("ui", this);
63 0 : rootContext->setContextProperty("api", m_api);
64 :
65 0 : QJSValue translator = m_engine->newQObject(m_translation);
66 0 : QJSValue translateFn = translator.property("translate");
67 0 : m_engine->globalObject().setProperty("qsTrc", translateFn);
68 :
69 0 : m_networkManagerFactory = new QmlNetworkAccessManagerFactory();
70 0 : m_engine->setNetworkAccessManagerFactory(m_networkManagerFactory);
71 :
72 : #ifdef Q_OS_WIN
73 : QDir dir(QCoreApplication::applicationDirPath() + QString("/../qml"));
74 : m_engine->addImportPath(dir.absolutePath());
75 : #endif
76 :
77 0 : m_engine->addImportPath(":/qml");
78 :
79 : #ifdef MUSE_LOAD_QML_FROM_SOURCE
80 : for (const QString& path : m_sourceImportPaths) {
81 : m_engine->addImportPath(path);
82 : }
83 : #endif
84 0 : }
85 :
86 0 : void UiEngine::quit()
87 : {
88 0 : TRACEFUNC;
89 :
90 0 : if (!m_engine) {
91 0 : return;
92 : }
93 :
94 0 : m_engine->exit(0);
95 :
96 0 : delete m_engine;
97 0 : m_engine = nullptr;
98 0 : }
99 :
100 0 : QmlDataFormatter* UiEngine::df() const
101 : {
102 0 : return m_dataFormatter;
103 : }
104 :
105 0 : QQuickItem* UiEngine::rootItem() const
106 : {
107 0 : return m_rootItem;
108 : }
109 :
110 0 : void UiEngine::setRootItem(QQuickItem* rootItem)
111 : {
112 0 : if (m_rootItem == rootItem) {
113 0 : return;
114 : }
115 :
116 0 : m_rootItem = rootItem;
117 0 : emit rootItemChanged(m_rootItem);
118 : }
119 :
120 0 : bool UiEngine::isEffectsAllowed() const
121 : {
122 0 : if (m_isEffectsAllowed == -1) {
123 0 : m_isEffectsAllowed = GraphicsApiProvider::graphicsApi() != GraphicsApi::Software;
124 : }
125 0 : return m_isEffectsAllowed;
126 : }
127 :
128 0 : bool UiEngine::isSystemDragSupported() const
129 : {
130 0 : return configuration()->isSystemDragSupported();
131 : }
132 :
133 0 : void UiEngine::addSourceImportPath(const QString& path)
134 : {
135 : #ifdef MUSE_LOAD_QML_FROM_SOURCE
136 : LOGD() << path;
137 : m_sourceImportPaths << path;
138 : if (m_engine) {
139 : m_engine->addImportPath(path);
140 : }
141 : #else
142 : Q_UNUSED(path);
143 : #endif
144 0 : }
145 :
146 0 : QmlApi* UiEngine::api() const
147 : {
148 0 : return m_api;
149 : }
150 :
151 0 : void UiEngine::setTheme(api::ThemeApi* theme)
152 : {
153 0 : m_theme = theme;
154 0 : }
155 :
156 0 : muse::api::ThemeApi* UiEngine::theme() const
157 : {
158 0 : return m_theme;
159 : }
160 :
161 0 : QmlToolTip* UiEngine::tooltip() const
162 : {
163 0 : return m_tooltip;
164 : }
165 :
166 0 : Qt::KeyboardModifiers UiEngine::keyboardModifiers() const
167 : {
168 0 : return QGuiApplication::keyboardModifiers();
169 : }
170 :
171 0 : Qt::LayoutDirection UiEngine::currentLanguageLayoutDirection() const
172 : {
173 0 : return languagesService()->currentLanguage().direction;
174 : }
175 :
176 0 : QColor UiEngine::blendColors(const QColor& c1, const QColor& c2) const
177 : {
178 0 : return muse::blendQColors(c1, c2);
179 : }
180 :
181 0 : QColor UiEngine::blendColors(const QColor& c1, const QColor& c2, float alpha) const
182 : {
183 0 : return muse::blendQColors(c1, c2, alpha);
184 : }
185 :
186 0 : QColor UiEngine::colorWithAlphaF(const QColor& src, float alpha) const
187 : {
188 0 : QColor c = src;
189 0 : c.setAlphaF(alpha);
190 0 : return c;
191 : }
192 :
193 0 : QStringList UiEngine::allTextFonts() const
194 : {
195 0 : QStringList allFonts = QFontDatabase::families();
196 0 : for (const QString& nonTextFont : configuration()->nonTextFonts()) {
197 0 : if (!nonTextFont.endsWith(" Text")) {
198 0 : allFonts.removeAll(nonTextFont);
199 : }
200 0 : }
201 0 : return allFonts;
202 0 : }
203 :
204 0 : QQmlApplicationEngine* UiEngine::qmlAppEngine() const
205 : {
206 0 : return m_engine;
207 : }
208 :
209 0 : QQmlEngine* UiEngine::qmlEngine() const
210 : {
211 0 : return qmlAppEngine();
212 : }
213 :
214 0 : void UiEngine::clearComponentCache()
215 : {
216 0 : m_engine->clearComponentCache();
217 0 : }
218 :
219 0 : GraphicsApi UiEngine::graphicsApi() const
220 : {
221 0 : return GraphicsApiProvider::graphicsApi();
222 : }
223 :
224 0 : QString UiEngine::graphicsApiName() const
225 : {
226 0 : return GraphicsApiProvider::graphicsApiName();
227 : }
228 :
229 0 : void UiEngine::sleep(int msec)
230 : {
231 0 : QEventLoop loop;
232 0 : QTimer::singleShot(msec, [&loop]() {
233 0 : loop.quit();
234 0 : });
235 0 : loop.exec();
236 0 : }
|