~ubuntu-sdk-team/ubuntu-ui-toolkit/trunk

« back to all changes in this revision

Viewing changes to src/Ubuntu/UbuntuToolkit/ubuntutoolkitmodule.cpp

  • Committer: Bileto Bot
  • Date: 2016-07-26 08:39:51 UTC
  • mfrom: (1000.974.22 OTA12-2016-06-27)
  • Revision ID: ci-train-bot@canonical.com-20160726083951-jdqr0xksf0yd421l
* Make qml-module-ubuntu-components-gles depend on 
  qml-module-ubuntu-performancemetrics-gles.
* Set default for TextArea.persistentSelection and correct type. 
  Fixes LP: #1594400
* Add version suffix to all unit tests in visual.
* Stop using macros for namespaces in headers. Fixes LP: #1596536.
* More robust list and namespace handling in apicheck. Fixes LP: #1595461
* Separate plugin for Ubuntu.Components.Labs.
* Create separate plugin for Ubuntu.Components.Styles.
* Move C++ components into UbuntuToolkit library.
* Remove fw-headers from UbuntuGestures and UbuntuToolkit. We are no longer
  supporting them as public C++ API.
* Break unity8 with lower version 8.13.
* Improve the i18n documentation for plurals. Fixes LP: #1184810
* Don't get APL subheader colors from MainView AppHeader when there is no 
  MainView. Fixes LP: #1588172
* Add Header.automaticHeight property. Fixes LP: #1540240
* UbuntuShape - Use reference counting to handle shape textures.
  The current shape textures handling system requires to search for the 
  textures associated to the current graphics context by doing a search in an
  array for each item at each updatePaintNode() call. It also deletes the 
  textures on OpenGLContext::aboutToBeDestroyed() signal emission, which 
  appears in some cases to not have any OpenGL context bound.
  We propose an alternative to create and destroy shape textures per material 
  creation and destruction with a reference counting mechanism to minimise 
  costly calls. We search for the shape textures (hash lookup with an OpenGL 
  context pointer as the key) only when a texture material is created or 
  deleted, which is much more occasional compared to updatePaintNode() calls.
  That also ensures textures are deleted with the proper OpenGL context bound.
  The only drawback is that shape textures are deleted when there is no shape 
  items anymore in a scene and recreated when there is a new one appearing. 
  These operations being quite uncommon and relatively fast, the advantages 
  seem to be higher than the drawbacks.
* Expansion of LD_LIBRARY_PATH is broken since it doesn't include a ":", which
  means PerformanceMetrics path, which is at the end, is actually not 
  available, nor the current content of LD_LIBRARY_PATH.
  That fixes it using the proper shell expansion syntax.
* Fix packaging sorting bzr hook to work on bzr branches that are local only.
* Disable documentation building for GLES builds
* ProgressionVisual: do not load the icon until the progression is made 
  visible.
* Scrollbar: do not load the stepper icon until it is visible.
* Scrollbar: load stepper icon asynchronously.
* Icon: only load when completed and therefore avoids multiple reloads caused
  by size changes.
                                                              

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2016 Canonical Ltd.
 
3
 *
 
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.
 
7
 *
 
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.
 
12
 *
 
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/>.
 
15
 *
 
16
 * Author: Zsombor Egri <zsombor.egri@canonical.com>
 
17
 */
 
18
 
 
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>
 
27
 
 
28
#include <sys/types.h>
 
29
#include <unistd.h>
 
30
#include <stdexcept>
 
31
 
 
32
#include <ucunits_p.h>
 
33
#include <quickutils_p.h>
 
34
#include <i18n_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>
 
83
#include <tree_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>
 
91
 
 
92
// styles
 
93
#include <ucbottomedgestyle_p.h>
 
94
 
 
95
UT_NAMESPACE_BEGIN
 
96
 
 
97
const char *EngineProperty("__ubuntu_toolkit_plugin_data");
 
98
 
 
99
/******************************************************************************
 
100
 * UbuntuToolkitModule
 
101
 */
 
102
UbuntuToolkitModule* UbuntuToolkitModule::create(QQmlEngine *engine, const QUrl &baseUrl)
 
103
{
 
104
    if (!engine) {
 
105
        return Q_NULLPTR;
 
106
    }
 
107
    UbuntuToolkitModule *data = new UbuntuToolkitModule(engine);
 
108
    data->m_baseUrl = QUrl(baseUrl.toString() + '/');
 
109
    engine->setProperty(EngineProperty, QVariant::fromValue(data));
 
110
    return data;
 
111
}
 
112
 
 
113
void UbuntuToolkitModule::registerWindowContextProperty()
 
114
{
 
115
    setWindowContextProperty(QGuiApplication::focusWindow());
 
116
 
 
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
 
121
    */
 
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));
 
126
 
 
127
}
 
128
 
 
129
void UbuntuToolkitModule::setWindowContextProperty(QWindow* focusWindow)
 
130
{
 
131
    QQuickView* view = qobject_cast<QQuickView*>(focusWindow);
 
132
 
 
133
    if (view != NULL) {
 
134
        view->rootContext()->setContextProperty("window", view);
 
135
    }
 
136
}
 
137
 
 
138
void UbuntuToolkitModule::initializeContextProperties(QQmlEngine *engine)
 
139
{
 
140
    UCUnits::instance(engine);
 
141
    QuickUtils::instance(engine);
 
142
    UbuntuI18n::instance(engine);
 
143
    UCApplication::instance(engine);
 
144
    UCFontUtils::instance(engine);
 
145
    UCTheme::defaultTheme(engine);
 
146
 
 
147
    QQmlContext* context = engine->rootContext();
 
148
 
 
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());
 
152
 
 
153
    UCDeprecatedTheme::registerToContext(context);
 
154
 
 
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()));
 
162
 
 
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);
 
171
 
 
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()));
 
177
 
 
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()));
 
184
}
 
185
 
 
186
void UbuntuToolkitModule::registerTypesToVersion(const char *uri, int major, int minor)
 
187
{
 
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");
 
218
}
 
219
 
 
220
/*
 
221
 * public API
 
222
 */
 
223
UbuntuToolkitModule::UbuntuToolkitModule(QObject *parent)
 
224
    : QObject(parent)
 
225
{
 
226
}
 
227
 
 
228
QUrl UbuntuToolkitModule::baseUrl(QQmlEngine *engine)
 
229
{
 
230
    if (!engine) {
 
231
        return QUrl();
 
232
    }
 
233
    UbuntuToolkitModule *data = engine->property(EngineProperty).value<UbuntuToolkitModule*>();
 
234
    return !data ? QUrl() : data->m_baseUrl;
 
235
}
 
236
 
 
237
void UbuntuToolkitModule::initializeModule(QQmlEngine *engine, const QUrl &pluginBaseUrl)
 
238
{
 
239
    UbuntuToolkitModule *module = create(engine, pluginBaseUrl);
 
240
 
 
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");
 
247
 
 
248
    // allocate all context property objects prior we register them
 
249
    initializeContextProperties(engine);
 
250
 
 
251
    HapticsProxy::instance(engine);
 
252
 
 
253
    engine->addImageProvider(QLatin1String("scaling"), new UCScalingImageProvider);
 
254
 
 
255
    // register icon provider
 
256
    engine->addImageProvider(QLatin1String("theme"), new UnityThemeIconProvider);
 
257
 
 
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));
 
264
 
 
265
    module->registerWindowContextProperty();
 
266
 
 
267
    // register performance monitor
 
268
    engine->rootContext()->setContextProperty("performanceMonitor", new UCPerformanceMonitor(engine));
 
269
}
 
270
 
 
271
void UbuntuToolkitModule::defineModule()
 
272
{
 
273
    const char *uri = "Ubuntu.Components";
 
274
    // register 0.1 for backward compatibility
 
275
    registerTypesToVersion(uri, 0, 1);
 
276
    registerTypesToVersion(uri, 1, 0);
 
277
 
 
278
    // register custom event
 
279
    ForwardedEvent::registerForwardedEvent();
 
280
 
 
281
    // register parent type so that properties can get/ set it
 
282
    qmlRegisterUncreatableType<QAbstractItemModel>(uri, 1, 1, "QAbstractItemModel", "Not instantiable");
 
283
 
 
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");
 
290
 
 
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");
 
300
 
 
301
    // register 1.3 API
 
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");
 
326
}
 
327
 
 
328
void UbuntuToolkitModule::undefineModule()
 
329
{
 
330
    // nothing yet
 
331
}
 
332
 
 
333
/******************************************************************************
 
334
 * Styles module
 
335
 */
 
336
void UbuntuStylesModule::defineModule(const char *uri)
 
337
{
 
338
    // 1.2 styles
 
339
    qmlRegisterType<UCListItemStyle>(uri, 1, 2, "ListItemStyle");
 
340
 
 
341
    // 1.3 styles
 
342
    qmlRegisterType<UCListItemStyle, 1>(uri, 1, 3, "ListItemStyle");
 
343
    qmlRegisterType<UCBottomEdgeStyle>(uri, 1, 3, "BottomEdgeStyle");
 
344
}
 
345
 
 
346
void UbuntuStylesModule::undefineModule()
 
347
{
 
348
    // nothing yet
 
349
}
 
350
 
 
351
/******************************************************************************
 
352
 * Labs module
 
353
 */
 
354
void UbuntuLabsModule::initializeModule(QQmlEngine *engine, QQmlExtensionPlugin *plugin)
 
355
{
 
356
    Q_UNUSED(engine);
 
357
    Q_UNUSED(plugin);
 
358
}
 
359
 
 
360
void UbuntuLabsModule::defineModule(const char *uri)
 
361
{
 
362
    Q_UNUSED(uri);
 
363
    // a fake component so we can have the module types file created
 
364
    qmlRegisterType<QObject>(uri, 1, 0, "ZiObject");
 
365
}
 
366
 
 
367
void UbuntuLabsModule::undefineModule()
 
368
{
 
369
    // nothing yet
 
370
}
 
371
 
 
372
UT_NAMESPACE_END