2
* Copyright 2016 Canonical Ltd.
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU Lesser General Public License as published by
6
* the Free Software Foundation; version 3.
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU Lesser General Public License for more details.
13
* You should have received a copy of the GNU Lesser General Public License
14
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16
* Author: Zsombor Egri <zsombor.egri@canonical.com>
19
#include "ubuntutoolkitmodule.h"
20
#include <QtQml/QQmlEngine>
21
#include <QtQml/QQmlContext>
22
#include <QtQml/QQmlExtensionPlugin>
23
#include <QtQuick/private/qquickimagebase_p.h>
24
#include <QDBusConnection>
25
#include <QtGui/QGuiApplication>
26
#include <QtGui/QScreen>
28
#include <sys/types.h>
32
#include <ucunits_p.h>
33
#include <quickutils_p.h>
35
#include <ucapplication_p.h>
36
#include <ucfontutils_p.h>
37
#include <uctheme_p.h>
38
#include <ucdeprecatedtheme_p.h>
39
#include <listener_p.h>
40
#include <ucfontutils_p.h>
41
#include <ucmathutils_p.h>
42
#include <ucmargins_p.h>
43
#include <ucmainviewbase_p.h>
44
#include <ucabstractbutton_p.h>
45
#include <ucaction_p.h>
46
#include <ucactioncontext_p.h>
47
#include <ucactionitem_p.h>
48
#include <ucactionmanager_p.h>
49
#include <ucalarm_p.h>
50
#include <ucalarmmodel_p.h>
51
#include <ucarguments_p.h>
52
#include <ucargument_p.h>
53
#include <ucbottomedgehint_p.h>
54
#include <ucbottomedgeregion_p.h>
55
#include <ucbottomedge_p.h>
56
#include <uchaptics_p.h>
57
#include <ucheader_p.h>
58
#include <ucinversemouse_p.h>
59
#include <ucmouse_p.h>
60
#include <uclistitem_p.h>
61
#include <uclistitemactions_p.h>
62
#include <uclistitemlayout_p.h>
63
#include <uclabel_p.h>
64
#include <ucpagetreenode_p.h>
65
#include <ucperformancemonitor_p.h>
66
#include <ucproportionalshape_p.h>
67
#include <ucscalingimageprovider_p.h>
68
#include <ucserviceproperties_p.h>
69
#include <ucslotslayout_p.h>
70
#include <ucstatesaver_p.h>
71
#include <ucstyleditembase_p.h>
72
#include <ucstylehints_p.h>
73
#include <uctheme_p.h>
74
#include <ucubuntuanimation_p.h>
75
#include <ucubuntushapeoverlay_p.h>
76
#include <ucubuntushape_p.h>
77
#include <ucurihandler_p.h>
78
#include <ucqquickimageextension_p.h>
79
#include <inversemouseareatype_p.h>
80
#include <qquickclipboard_p.h>
81
#include <qquickmimedata_p.h>
82
#include <colorutils_p.h>
84
#include <unitythemeiconprovider_p.h>
85
#include <sortbehavior_p.h>
86
#include <sortfiltermodel_p.h>
87
#include <livetimer_p.h>
88
#include <privates/frame_p.h>
89
#include <privates/ucpagewrapper_p.h>
90
#include <privates/appheaderbase_p.h>
93
#include <ucbottomedgestyle_p.h>
97
const char *EngineProperty("__ubuntu_toolkit_plugin_data");
99
/******************************************************************************
100
* UbuntuToolkitModule
102
UbuntuToolkitModule* UbuntuToolkitModule::create(QQmlEngine *engine, const QUrl &baseUrl)
107
UbuntuToolkitModule *data = new UbuntuToolkitModule(engine);
108
data->m_baseUrl = QUrl(baseUrl.toString() + '/');
109
engine->setProperty(EngineProperty, QVariant::fromValue(data));
113
void UbuntuToolkitModule::registerWindowContextProperty()
115
setWindowContextProperty(QGuiApplication::focusWindow());
117
// listen to QGuiApplication::focusWindowChanged
118
/* Ensure that setWindowContextProperty is called in the same thread (the
119
main thread) otherwise it segfaults. Reference:
120
https://bugs.launchpad.net/ubuntu-ui-toolkit/+bug/1205556
122
QGuiApplication* application = static_cast<QGuiApplication*>(QCoreApplication::instance());
123
QObject::connect(application, SIGNAL(focusWindowChanged(QWindow*)),
124
this, SLOT(setWindowContextProperty(QWindow*)),
125
Qt::ConnectionType(Qt::DirectConnection | Qt::UniqueConnection));
129
void UbuntuToolkitModule::setWindowContextProperty(QWindow* focusWindow)
131
QQuickView* view = qobject_cast<QQuickView*>(focusWindow);
134
view->rootContext()->setContextProperty("window", view);
138
void UbuntuToolkitModule::initializeContextProperties(QQmlEngine *engine)
140
UCUnits::instance(engine);
141
QuickUtils::instance(engine);
142
UbuntuI18n::instance(engine);
143
UCApplication::instance(engine);
144
UCFontUtils::instance(engine);
145
UCTheme::defaultTheme(engine);
147
QQmlContext* context = engine->rootContext();
149
// register root object watcher that sets a global property with the root object
150
// that can be accessed from any object
151
context->setContextProperty("QuickUtils", QuickUtils::instance());
153
UCDeprecatedTheme::registerToContext(context);
155
context->setContextProperty("i18n", UbuntuI18n::instance());
156
ContextPropertyChangeListener *i18nChangeListener =
157
new ContextPropertyChangeListener(context, "i18n");
158
QObject::connect(UbuntuI18n::instance(), SIGNAL(domainChanged()),
159
i18nChangeListener, SLOT(updateContextProperty()));
160
QObject::connect(UbuntuI18n::instance(), SIGNAL(languageChanged()),
161
i18nChangeListener, SLOT(updateContextProperty()));
163
// We can't use 'Application' because it exists (undocumented)
164
context->setContextProperty("UbuntuApplication", UCApplication::instance());
165
ContextPropertyChangeListener *applicationChangeListener =
166
new ContextPropertyChangeListener(context, "UbuntuApplication");
167
QObject::connect(UCApplication::instance(), SIGNAL(applicationNameChanged()),
168
applicationChangeListener, SLOT(updateContextProperty()));
169
// Give the application object access to the engine
170
UCApplication::instance()->setContext(context);
172
context->setContextProperty("units", UCUnits::instance());
173
ContextPropertyChangeListener *unitsChangeListener =
174
new ContextPropertyChangeListener(context, "units");
175
QObject::connect(UCUnits::instance(), SIGNAL(gridUnitChanged()),
176
unitsChangeListener, SLOT(updateContextProperty()));
178
// register FontUtils
179
context->setContextProperty("FontUtils", UCFontUtils::instance());
180
ContextPropertyChangeListener *fontUtilsListener =
181
new ContextPropertyChangeListener(context, "FontUtils");
182
QObject::connect(UCUnits::instance(), SIGNAL(gridUnitChanged()),
183
fontUtilsListener, SLOT(updateContextProperty()));
186
void UbuntuToolkitModule::registerTypesToVersion(const char *uri, int major, int minor)
188
qmlRegisterType<UCAction>(uri, major, minor, "Action");
189
qmlRegisterType<UCActionContext>(uri, major, minor, "ActionContext");
190
qmlRegisterUncreatableType<UCApplication>(uri, major, minor, "UCApplication", "Not instantiable");
191
qmlRegisterType<UCActionManager>(uri, major, minor, "ActionManager");
192
qmlRegisterUncreatableType<UCFontUtils>(uri, major, minor, "UCFontUtils", "Not instantiable");
193
qmlRegisterType<UCStyledItemBase>(uri, major, minor, "StyledItem");
194
qmlRegisterUncreatableType<UbuntuI18n>(uri, major, minor, "i18n", "Singleton object");
195
qmlRegisterExtendedType<QQuickImageBase, UCQQuickImageExtension>(uri, major, minor, "QQuickImageBase");
196
qmlRegisterUncreatableType<UCUnits>(uri, major, minor, "UCUnits", "Not instantiable");
197
qmlRegisterType<UCUbuntuShape>(uri, major, minor, "UbuntuShape");
198
// FIXME/DEPRECATED: Shape is exported for backwards compatibility only
199
qmlRegisterType<UCUbuntuShape>(uri, major, minor, "Shape");
200
qmlRegisterType<InverseMouseAreaType>(uri, major, minor, "InverseMouseArea");
201
qmlRegisterType<QQuickMimeData>(uri, major, minor, "MimeData");
202
qmlRegisterSimpleSingletonType<QQuickClipboard>(uri, major, minor, "Clipboard");
203
qmlRegisterSimpleSingletonType<UCUbuntuAnimation>(uri, major, minor, "UbuntuAnimation");
204
qmlRegisterType<UCArguments>(uri, major, minor, "Arguments");
205
qmlRegisterType<UCArgument>(uri, major, minor, "Argument");
206
qmlRegisterType<QQmlPropertyMap>();
207
qmlRegisterType<UCAlarm>(uri, major, minor, "Alarm");
208
qmlRegisterType<UCAlarmModel>(uri, major, minor, "AlarmModel");
209
qmlRegisterType<UCStateSaver>(uri, major, minor, "StateSaver");
210
qmlRegisterType<UCStateSaverAttached>();
211
qmlRegisterSimpleSingletonType<UCUriHandler>(uri, major, minor, "UriHandler");
212
qmlRegisterType<UCMouse>(uri, major, minor, "Mouse");
213
qmlRegisterType<UCInverseMouse>(uri, major, minor, "InverseMouse");
214
qmlRegisterType<UCActionItem>(uri, major, minor, "ActionItem");
215
qmlRegisterSimpleSingletonType<UCHaptics>(uri, major, minor, "Haptics");
216
qmlRegisterSimpleSingletonType<UCMathUtils>(uri, major, minor, "MathUtils");
217
qmlRegisterSimpleSingletonType<ColorUtils>(uri, major, minor, "ColorUtils");
223
UbuntuToolkitModule::UbuntuToolkitModule(QObject *parent)
228
QUrl UbuntuToolkitModule::baseUrl(QQmlEngine *engine)
233
UbuntuToolkitModule *data = engine->property(EngineProperty).value<UbuntuToolkitModule*>();
234
return !data ? QUrl() : data->m_baseUrl;
237
void UbuntuToolkitModule::initializeModule(QQmlEngine *engine, const QUrl &pluginBaseUrl)
239
UbuntuToolkitModule *module = create(engine, pluginBaseUrl);
241
// Register private types.
242
const char *privateUri = "Ubuntu.Components.Private";
243
qmlRegisterType<UCFrame>(privateUri, 1, 3, "Frame");
244
qmlRegisterType<UCPageWrapper>(privateUri, 1, 3, "PageWrapper");
245
qmlRegisterType<UCAppHeaderBase>(privateUri, 1, 3, "AppHeaderBase");
246
qmlRegisterType<Tree>(privateUri, 1, 3, "Tree");
248
// allocate all context property objects prior we register them
249
initializeContextProperties(engine);
251
HapticsProxy::instance(engine);
253
engine->addImageProvider(QLatin1String("scaling"), new UCScalingImageProvider);
255
// register icon provider
256
engine->addImageProvider(QLatin1String("theme"), new UnityThemeIconProvider);
258
// Necessary for Screen.orientation (from import QtQuick.Window 2.0) to work
259
QGuiApplication::primaryScreen()->setOrientationUpdateMask( Qt::ScreenOrientations(
260
Qt::PortraitOrientation |
261
Qt::LandscapeOrientation |
262
Qt::InvertedPortraitOrientation |
263
Qt::InvertedLandscapeOrientation));
265
module->registerWindowContextProperty();
267
// register performance monitor
268
engine->rootContext()->setContextProperty("performanceMonitor", new UCPerformanceMonitor(engine));
271
void UbuntuToolkitModule::defineModule()
273
const char *uri = "Ubuntu.Components";
274
// register 0.1 for backward compatibility
275
registerTypesToVersion(uri, 0, 1);
276
registerTypesToVersion(uri, 1, 0);
278
// register custom event
279
ForwardedEvent::registerForwardedEvent();
281
// register parent type so that properties can get/ set it
282
qmlRegisterUncreatableType<QAbstractItemModel>(uri, 1, 1, "QAbstractItemModel", "Not instantiable");
284
// register 1.1 only API
285
qmlRegisterType<UCStyledItemBase, 1>(uri, 1, 1, "StyledItem");
286
qmlRegisterType<QSortFilterProxyModelQML>(uri, 1, 1, "SortFilterModel");
287
qmlRegisterUncreatableType<FilterBehavior>(uri, 1, 1, "FilterBehavior", "Not instantiable");
288
qmlRegisterUncreatableType<SortBehavior>(uri, 1, 1, "SortBehavior", "Not instantiable");
289
qmlRegisterType<UCServiceProperties, 1>(uri, 1, 1, "ServiceProperties");
291
// register 1.2 only API
292
qmlRegisterType<UCListItem>(uri, 1, 2, "ListItem");
293
qmlRegisterType<UCListItemDivider>();
294
qmlRegisterUncreatableType<UCSwipeEvent>(uri, 1, 2, "SwipeEvent", "This is an event object.");
295
qmlRegisterUncreatableType<UCDragEvent>(uri, 1, 2, "ListItemDrag", "This is an event object");
296
qmlRegisterType<UCListItemActions>(uri, 1, 2, "ListItemActions");
297
qmlRegisterUncreatableType<UCViewItemsAttached>(uri, 1, 2, "ViewItems", "Not instantiable");
298
qmlRegisterType<UCUbuntuShape, 1>(uri, 1, 2, "UbuntuShape");
299
qmlRegisterType<UCUbuntuShapeOverlay>(uri, 1, 2, "UbuntuShapeOverlay");
302
qmlRegisterType<UCListItem, 1>(uri, 1, 3, "ListItem");
303
qmlRegisterType<UCListItemExpansion>();
304
qmlRegisterType<UCTheme>(uri, 1, 3, "ThemeSettings");
305
qmlRegisterType<UCStyledItemBase, 2>(uri, 1, 3, "StyledItem");
306
qmlRegisterType<UCStyledItemBase, 2>(uri, 1, 3, "StyledItem");
307
qmlRegisterCustomType<UCStyleHints>(uri, 1, 3, "StyleHints", new UCStyleHintsParser);
308
qmlRegisterType<UCAction, 1>(uri, 1, 3, "Action");
309
qmlRegisterType<UCSlotsLayout>(uri, 1, 3, "SlotsLayout");
310
qmlRegisterType<UCUbuntuShape, 2>(uri, 1, 3, "UbuntuShape");
311
qmlRegisterType<UCProportionalShape>(uri, 1, 3, "ProportionalShape");
312
qmlRegisterType<LiveTimer>(uri, 1, 3, "LiveTimer");
313
qmlRegisterType<UCAbstractButton>(uri, 1, 3, "AbstractButton");
314
qmlRegisterType<UCMargins>();
315
qmlRegisterUncreatableType<UCSlotsAttached>(uri, 1, 3, "SlotsAttached", "Not instantiable");
316
qmlRegisterUncreatableType<UCSlotsLayoutPadding>(uri, 1, 3, "SlotsLayoutPadding", "Not instantiable");
317
qmlRegisterType<UCListItemLayout>(uri, 1, 3, "ListItemLayout");
318
qmlRegisterType<UCHeader>(uri, 1, 3, "Header");
319
qmlRegisterType<UCLabel>(uri, 1, 3, "Label");
320
qmlRegisterType<UCBottomEdgeHint>(uri, 1, 3, "BottomEdgeHint");
321
qmlRegisterType<UCBottomEdge>(uri, 1, 3, "BottomEdge");
322
qmlRegisterType<UCBottomEdgeRegion>(uri, 1, 3, "BottomEdgeRegion");
323
qmlRegisterType<UCPageTreeNode>(uri, 1, 3, "PageTreeNode");
324
qmlRegisterType<UCPopupContext>(uri, 1, 3, "PopupContext");
325
qmlRegisterType<UCMainViewBase>(uri, 1, 3, "MainViewBase");
328
void UbuntuToolkitModule::undefineModule()
333
/******************************************************************************
336
void UbuntuStylesModule::defineModule(const char *uri)
339
qmlRegisterType<UCListItemStyle>(uri, 1, 2, "ListItemStyle");
342
qmlRegisterType<UCListItemStyle, 1>(uri, 1, 3, "ListItemStyle");
343
qmlRegisterType<UCBottomEdgeStyle>(uri, 1, 3, "BottomEdgeStyle");
346
void UbuntuStylesModule::undefineModule()
351
/******************************************************************************
354
void UbuntuLabsModule::initializeModule(QQmlEngine *engine, QQmlExtensionPlugin *plugin)
360
void UbuntuLabsModule::defineModule(const char *uri)
363
// a fake component so we can have the module types file created
364
qmlRegisterType<QObject>(uri, 1, 0, "ZiObject");
367
void UbuntuLabsModule::undefineModule()