Merge lp:~loic.molinari/ubuntu-ui-toolkit/ubuntu-ui-toolkit-c++-label-conversion into lp:ubuntu-ui-toolkit/staging

Proposed by Loïc Molinari
Status: Work in progress
Proposed branch: lp:~loic.molinari/ubuntu-ui-toolkit/ubuntu-ui-toolkit-c++-label-conversion
Merge into: lp:ubuntu-ui-toolkit/staging
Diff against target: 519 lines (+251/-87)
13 files modified
components.api (+7/-3)
modules/Ubuntu/Components/Label.qml (+0/-59)
modules/Ubuntu/Components/OptionSelector.qml (+1/-1)
modules/Ubuntu/Components/OptionSelectorDelegate.qml (+2/-2)
modules/Ubuntu/Components/TextArea.qml (+2/-2)
modules/Ubuntu/Components/TextField.qml (+1/-1)
modules/Ubuntu/Components/plugin/plugin.cpp (+3/-0)
modules/Ubuntu/Components/plugin/plugin.pro (+2/-0)
modules/Ubuntu/Components/plugin/ucfontutils.cpp (+15/-16)
modules/Ubuntu/Components/plugin/ucfontutils.h (+8/-1)
modules/Ubuntu/Components/plugin/uclabel.cpp (+141/-0)
modules/Ubuntu/Components/plugin/uclabel.h (+69/-0)
modules/Ubuntu/Components/qmldir (+0/-2)
To merge this branch: bzr merge lp:~loic.molinari/ubuntu-ui-toolkit/ubuntu-ui-toolkit-c++-label-conversion
Reviewer Review Type Date Requested Status
Zsombor Egri Needs Fixing
Review via email: mp+253163@code.launchpad.net

Commit message

Coverted Label item from QML to C++ for performance reasons.

Description of the change

Coverted Label item from QML to C++ for performance reasons.

To post a comment you must log in.
Revision history for this message
Zsombor Egri (zsombi) wrote :

You cannot hardcode the color. See old Label "color: Theme.palette.selected.backgroundText" binding is broken now, you got to fix that.

Also would be nice to see some performance measurements to see what is the gain.

Comments inline

review: Needs Fixing
Revision history for this message
Loïc Molinari (loic.molinari) wrote :

> You cannot hardcode the color. See old Label "color:
> Theme.palette.selected.backgroundText" binding is broken now, you got to fix
> that.

Any idea on how to do that?

Revision history for this message
Loïc Molinari (loic.molinari) wrote :

What about the naming of the new property adaptiveSize, good enough?

Unmerged revisions

1451. By Loïc Molinari

Converted Label from QML to C++.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'components.api'
2--- components.api 2015-03-10 13:58:52 +0000
3+++ components.api 2015-03-17 09:10:46 +0000
4@@ -83,9 +83,6 @@
5 Icon 1.1
6 Icon10
7 property url source
8-Label 0.1 1.0
9-Text
10- property string fontSize
11 Base 0.1 1.0
12 Empty
13 property variant icon
14@@ -925,6 +922,13 @@
15 name: "UCInverseMouse"
16 prototype: "UCMouse"
17 exports: ["InverseMouse 0.1", "InverseMouse 1.0"]
18+ name: "UCLabel"
19+ prototype: "QQuickText"
20+ exports: ["Label 0.1", "Label 1.0", "Label 1.2"]
21+ name: "AdaptiveSize"
22+ Property { name: "adaptiveSize"; revision: 1; type: "AdaptiveSize" }
23+ Property { name: "fontSize"; type: "string" }
24+ Signal { name: "adaptiveSizeChanged"; revision: 1 }
25 name: "UCListItem"
26 prototype: "UCStyledItemBase"
27 exports: ["ListItem 1.2"]
28
29=== removed file 'modules/Ubuntu/Components/Label.qml'
30--- modules/Ubuntu/Components/Label.qml 2015-03-03 13:47:48 +0000
31+++ modules/Ubuntu/Components/Label.qml 1970-01-01 00:00:00 +0000
32@@ -1,59 +0,0 @@
33-/*
34- * Copyright 2012 Canonical Ltd.
35- *
36- * This program is free software; you can redistribute it and/or modify
37- * it under the terms of the GNU Lesser General Public License as published by
38- * the Free Software Foundation; version 3.
39- *
40- * This program is distributed in the hope that it will be useful,
41- * but WITHOUT ANY WARRANTY; without even the implied warranty of
42- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
43- * GNU Lesser General Public License for more details.
44- *
45- * You should have received a copy of the GNU Lesser General Public License
46- * along with this program. If not, see <http://www.gnu.org/licenses/>.
47- */
48-
49-import QtQuick 2.4
50-
51-/*!
52- \qmltype Label
53- \inqmlmodule Ubuntu.Components 1.1
54- \ingroup ubuntu
55- \brief Text with Ubuntu styling.
56-
57- Example:
58- \qml
59- Rectangle {
60- color: UbuntuColors.coolGrey
61- width: units.gu(30)
62- height: units.gu(30)
63-
64- Label {
65- anchors.centerIn: parent
66- text: "Hello, world!"
67- fontSize: "large"
68- }
69- }
70- \endqml
71-*/
72-Text {
73- id: label
74-
75- /*!
76- The size of the text. One of the following strings (from smallest to largest):
77- \list
78- \li "xx-small"
79- \li "x-small"
80- \li "small"
81- \li "medium"
82- \li "large"
83- \li "x-large"
84- \endlist
85- Default value is "medium".
86- */
87- property string fontSize: "medium"
88-
89- font.pixelSize: FontUtils.sizeToPixels(fontSize)
90- color: Theme.palette.selected.backgroundText
91-}
92
93=== modified file 'modules/Ubuntu/Components/OptionSelector.qml'
94--- modules/Ubuntu/Components/OptionSelector.qml 2015-03-03 13:47:48 +0000
95+++ modules/Ubuntu/Components/OptionSelector.qml 2015-03-17 09:10:46 +0000
96@@ -202,7 +202,7 @@
97 right: parent.right
98 }
99
100- Label {
101+ Toolkit.Label {
102 id : label
103
104 text: optionSelector.text
105
106=== modified file 'modules/Ubuntu/Components/OptionSelectorDelegate.qml'
107--- modules/Ubuntu/Components/OptionSelectorDelegate.qml 2015-03-03 13:47:48 +0000
108+++ modules/Ubuntu/Components/OptionSelectorDelegate.qml 2015-03-17 09:10:46 +0000
109@@ -318,10 +318,10 @@
110 anchors {
111 verticalCenter: parent.verticalCenter
112 }
113- Label {
114+ Toolkit.Label {
115 text: option.text === "" ? modelData : option.text
116 }
117- Label {
118+ Toolkit.Label {
119 text: option.subText
120 visible: option.subText !== "" ? true : false
121 fontSize: "small"
122
123=== modified file 'modules/Ubuntu/Components/TextArea.qml'
124--- modules/Ubuntu/Components/TextArea.qml 2015-03-10 11:49:27 +0000
125+++ modules/Ubuntu/Components/TextArea.qml 2015-03-17 09:10:46 +0000
126@@ -792,10 +792,10 @@
127 Keys.onReleased: event.accepted = (event.key === Qt.Key_Enter) || (event.key === Qt.Key_Return)
128
129 // holding default values
130- Label { id: fontHolder }
131+ Ubuntu.Label { id: fontHolder }
132
133 //hint
134- Label {
135+ Ubuntu.Label {
136 id: hint
137 anchors {
138 fill: parent
139
140=== modified file 'modules/Ubuntu/Components/TextField.qml'
141--- modules/Ubuntu/Components/TextField.qml 2015-03-10 11:49:27 +0000
142+++ modules/Ubuntu/Components/TextField.qml 2015-03-17 09:10:46 +0000
143@@ -936,7 +936,7 @@
144 }
145
146 // hint text
147- Label {
148+ Ubuntu.Label {
149 id: hint
150 verticalAlignment: Text.AlignVCenter
151 horizontalAlignment: editor.horizontalAlignment
152
153=== modified file 'modules/Ubuntu/Components/plugin/plugin.cpp'
154--- modules/Ubuntu/Components/plugin/plugin.cpp 2015-03-04 19:07:58 +0000
155+++ modules/Ubuntu/Components/plugin/plugin.cpp 2015-03-17 09:10:46 +0000
156@@ -58,6 +58,7 @@
157 #include "uclistitemactions.h"
158 #include "uclistitemstyle.h"
159 #include "ucserviceproperties.h"
160+#include "uclabel.h"
161 #include "ucnamespace.h"
162
163 #include <sys/types.h>
164@@ -156,6 +157,7 @@
165 qmlRegisterSingletonType<UCUriHandler>(uri, major, minor, "UriHandler", registerUriHandler);
166 qmlRegisterType<UCMouse>(uri, major, minor, "Mouse");
167 qmlRegisterType<UCInverseMouse>(uri, major, minor, "InverseMouse");
168+ qmlRegisterType<UCLabel>(uri, major, minor, "Label");
169 }
170
171 void UbuntuComponentsPlugin::registerTypes(const char *uri)
172@@ -189,6 +191,7 @@
173 qmlRegisterSingletonType<UCNamespace>(uri, 1, 2, "Ubuntu", registerUbuntuNamespace);
174 qmlRegisterType<UCUbuntuShape, 1>(uri, 1, 2, "UbuntuShape");
175 qmlRegisterType<UCUbuntuShapeOverlay>(uri, 1, 2, "UbuntuShapeOverlay");
176+ qmlRegisterType<UCLabel, 1>(uri, 1, 2, "Label");
177 }
178
179 void UbuntuComponentsPlugin::initializeEngine(QQmlEngine *engine, const char *uri)
180
181=== modified file 'modules/Ubuntu/Components/plugin/plugin.pro'
182--- modules/Ubuntu/Components/plugin/plugin.pro 2015-03-04 19:07:58 +0000
183+++ modules/Ubuntu/Components/plugin/plugin.pro 2015-03-17 09:10:46 +0000
184@@ -77,6 +77,7 @@
185 uclistitemstyle.h \
186 ucserviceproperties.h \
187 ucserviceproperties_p.h \
188+ uclabel.h \
189 privates/listitemdragarea.h \
190 privates/listitemdraghandler.h \
191 ucnamespace.h
192@@ -124,6 +125,7 @@
193 uclistitemstyle.cpp \
194 ucviewitemsattached.cpp \
195 ucserviceproperties.cpp \
196+ uclabel.cpp \
197 privates/listitemdragarea.cpp \
198 privates/listitemdraghandler.cpp \
199 ucnamespace.cpp
200
201=== modified file 'modules/Ubuntu/Components/plugin/ucfontutils.cpp'
202--- modules/Ubuntu/Components/plugin/ucfontutils.cpp 2015-03-03 13:47:48 +0000
203+++ modules/Ubuntu/Components/plugin/ucfontutils.cpp 2015-03-17 09:10:46 +0000
204@@ -1,5 +1,5 @@
205 /*
206- * Copyright 2013 Canonical Ltd.
207+ * Copyright 2013-2015 Canonical Ltd.
208 *
209 * This program is free software; you can redistribute it and/or modify
210 * it under the terms of the GNU Lesser General Public License as published by
211@@ -64,7 +64,7 @@
212 */
213 qreal UCFontUtils::sizeToPixels(const QString &size)
214 {
215- return modularScale(size) * UCUnits::instance().dp(14);
216+ return modularScale(size) * UCUnits::instance().dp(fontUnits);
217 }
218
219 /*!
220@@ -82,18 +82,17 @@
221 */
222 qreal UCFontUtils::modularScale(const QString &size)
223 {
224- if (size == "xx-small") {
225- return 0.677;
226- } else if (size == "x-small") {
227- return 0.804;
228- } else if (size == "small") {
229- return 0.931;
230- } else if (size == "medium") {
231- return 1.079;
232- } else if (size == "large") {
233- return 1.291;
234- } else if (size == "x-large") {
235- return 1.714;
236- }
237- return 0.0;
238+ if (size.size() < 4) {
239+ return 0.0;
240+ }
241+
242+ switch (reinterpret_cast<int*>(size.toLatin1().data())[0]) {
243+ case /*"medi"*/0x6964656d: { return mediumScale; }
244+ case /*"larg"*/0x6772616c: { return largeScale; }
245+ case /*"smal"*/0x6c616d73: { return smallScale; }
246+ case /*"x-la"*/0x616c2d78: { return xLargeScale; }
247+ case /*"x-sm"*/0x6d732d78: { return xSmallScale; }
248+ case /*"xx-s"*/0x732d7878: { return xxSmallScale; }
249+ default: { return 0.0; }
250+ }
251 }
252
253=== modified file 'modules/Ubuntu/Components/plugin/ucfontutils.h'
254--- modules/Ubuntu/Components/plugin/ucfontutils.h 2013-05-20 11:38:32 +0000
255+++ modules/Ubuntu/Components/plugin/ucfontutils.h 2015-03-17 09:10:46 +0000
256@@ -12,7 +12,6 @@
257 *
258 * You should have received a copy of the GNU Lesser General Public License
259 * along with this program. If not, see <http://www.gnu.org/licenses/>.
260- *
261 */
262
263 #ifndef UCFONTUTILS_H
264@@ -23,7 +22,15 @@
265 class UCFontUtils : public QObject
266 {
267 Q_OBJECT
268+
269 public:
270+ static const float fontUnits = 14.0f;
271+ static const float xxSmallScale = 0.677f;
272+ static const float xSmallScale = 0.804f;
273+ static const float smallScale = 0.931f;
274+ static const float mediumScale = 1.079f;
275+ static const float largeScale = 1.291f;
276+ static const float xLargeScale = 1.714f;
277
278 static UCFontUtils& instance()
279 {
280
281=== added file 'modules/Ubuntu/Components/plugin/uclabel.cpp'
282--- modules/Ubuntu/Components/plugin/uclabel.cpp 1970-01-01 00:00:00 +0000
283+++ modules/Ubuntu/Components/plugin/uclabel.cpp 2015-03-17 09:10:46 +0000
284@@ -0,0 +1,141 @@
285+/*
286+ * Copyright 2015 Canonical Ltd.
287+ *
288+ * This program is free software; you can redistribute it and/or modify
289+ * it under the terms of the GNU Lesser General Public License as published by
290+ * the Free Software Foundation; version 3.
291+ *
292+ * This program is distributed in the hope that it will be useful,
293+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
294+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
295+ * GNU Lesser General Public License for more details.
296+ *
297+ * You should have received a copy of the GNU Lesser General Public License
298+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
299+ */
300+
301+#include "uclabel.h"
302+#include "ucfontutils.h"
303+#include "ucunits.h"
304+
305+void UCLabel::updatePixelSize()
306+{
307+ const float sizes[] = {
308+ UCFontUtils::xxSmallScale, UCFontUtils::xSmallScale, UCFontUtils::smallScale,
309+ UCFontUtils::mediumScale, UCFontUtils::largeScale, UCFontUtils::xLargeScale
310+ };
311+ QFont textFont = font();
312+ textFont.setPixelSize(
313+ qRound(sizes[m_adaptiveSize] * UCUnits::instance().dp(UCFontUtils::fontUnits)));
314+ setFont(textFont);
315+}
316+
317+/*! \qmltype Label
318+ \instantiates UCLabel
319+ \inqmlmodule Ubuntu.Components 1.2
320+ \ingroup ubuntu
321+ \brief Extended Text item with Ubuntu styling.
322+
323+ Label is an extended Text item with Ubuntu styling. It exposes an additional property that
324+ provides adaptive resizing based on the measurement unit.
325+
326+ Example:
327+ \qml
328+ Rectangle {
329+ color: UbuntuColors.warmGrey
330+ width: units.gu(30)
331+ height: units.gu(30)
332+
333+ Label {
334+ anchors.centerIn: parent
335+ text: "Hello world!"
336+ adaptiveSize: "large"
337+ }
338+ }
339+ \endqml
340+*/
341+UCLabel::UCLabel(QQuickItem* parent)
342+ : QQuickText(parent)
343+ , m_adaptiveSize(Medium)
344+ , m_flags(0)
345+{
346+ // FIXME(loicm) Hard-coded selected background text from Ambiance palette.
347+ setColor(QColor(qRgb(0x5d, 0x5d, 0x5d)));
348+ updatePixelSize();
349+}
350+
351+/*! \qmlproperty enumeration Label::adaptiveSize
352+ \since Ubuntu.Components 1.2
353+
354+ This property holds an abstract size that allows adaptive resizing based on the measurement unit
355+ (see Units). The default value is \c Label.Medium.
356+
357+ \note Setting this disables support for the deprecated \l fontSize property.
358+
359+ \list
360+ \li \b Label.XxSmall - extremely small font size
361+ \li \b Label.XSmall - very small font size
362+ \li \b Label.Small - small font size
363+ \li \b Label.Medium - medium font size
364+ \li \b Label.Large - large font size
365+ \li \b Label.XLarge - very large font size
366+ \endlist
367+*/
368+void UCLabel::setAdaptiveSize(AdaptiveSize adaptiveSize)
369+{
370+ if (!(m_flags & AdaptiveSizeSet)) {
371+ Q_EMIT fontSizeChanged();
372+ m_flags |= AdaptiveSizeSet;
373+ }
374+
375+ if (m_adaptiveSize != adaptiveSize) {
376+ m_adaptiveSize = adaptiveSize;
377+ updatePixelSize();
378+ Q_EMIT adaptiveSizeChanged();
379+ }
380+}
381+
382+/*! \qmlproperty string Label::fontSize
383+ \deprecated
384+
385+ This property holds an abstract size represented as a string that allows adaptive resizing based
386+ on the measurement unit (see Units). The default value is \c "medium".
387+
388+ \note Use \l adaptiveSize instead.
389+
390+ Here is the list of allowed strings from the smallest to the largest:
391+ \list
392+ \li \b "xx-small" - extremely small font size
393+ \li \b "x-small" - very small font size
394+ \li \b "small" - small font size
395+ \li \b "medium" - medium font size
396+ \li \b "large" - large font size
397+ \li \b "x-large" - very large font size
398+ \endlist
399+*/
400+void UCLabel::setFontSize(const QString& fontSize)
401+{
402+ if (m_flags & AdaptiveSizeSet) {
403+ return;
404+ }
405+ if (fontSize.size() < 4) {
406+ return;
407+ }
408+
409+ AdaptiveSize adaptiveSize;
410+ switch (reinterpret_cast<int*>(fontSize.toLatin1().data())[0]) {
411+ case /*"medi"*/0x6964656d: { adaptiveSize = Medium; break; }
412+ case /*"larg"*/0x6772616c: { adaptiveSize = Large; break; }
413+ case /*"smal"*/0x6c616d73: { adaptiveSize = Small; break; }
414+ case /*"x-la"*/0x616c2d78: { adaptiveSize = XLarge; break; }
415+ case /*"x-sm"*/0x6d732d78: { adaptiveSize = XSmall; break; }
416+ case /*"xx-s"*/0x732d7878: { adaptiveSize = XxSmall; break; }
417+ default: { return; }
418+ }
419+
420+ if (m_adaptiveSize != adaptiveSize) {
421+ m_adaptiveSize = adaptiveSize;
422+ updatePixelSize();
423+ Q_EMIT fontSizeChanged();
424+ }
425+}
426
427=== added file 'modules/Ubuntu/Components/plugin/uclabel.h'
428--- modules/Ubuntu/Components/plugin/uclabel.h 1970-01-01 00:00:00 +0000
429+++ modules/Ubuntu/Components/plugin/uclabel.h 2015-03-17 09:10:46 +0000
430@@ -0,0 +1,69 @@
431+/*
432+ * Copyright 2015 Canonical Ltd.
433+ *
434+ * This program is free software; you can redistribute it and/or modify
435+ * it under the terms of the GNU Lesser General Public License as published by
436+ * the Free Software Foundation; version 3.
437+ *
438+ * This program is distributed in the hope that it will be useful,
439+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
440+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
441+ * GNU Lesser General Public License for more details.
442+ *
443+ * You should have received a copy of the GNU Lesser General Public License
444+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
445+ */
446+
447+#ifndef UCLABEL_H
448+#define UCLABEL_H
449+
450+#include <private/qquicktext_p.h>
451+
452+class UCLabel : public QQuickText
453+{
454+ Q_OBJECT
455+
456+ Q_ENUMS(AdaptiveSize)
457+ Q_PROPERTY(AdaptiveSize adaptiveSize READ adaptiveSize WRITE setAdaptiveSize
458+ NOTIFY adaptiveSizeChanged REVISION 1)
459+
460+ // Deprecated.
461+ Q_PROPERTY(QString fontSize READ fontSize WRITE setFontSize NOTIFY fontSizeChanged)
462+
463+public:
464+ UCLabel(QQuickItem* parent=0);
465+
466+ enum AdaptiveSize { XxSmall = 0, XSmall = 1, Small = 2, Medium = 3, Large = 4, XLarge = 5 };
467+
468+ AdaptiveSize adaptiveSize() const { return m_adaptiveSize; }
469+ void setAdaptiveSize(AdaptiveSize adaptiveSize);
470+
471+ // Deprecated.
472+ QString fontSize() const {
473+ if (m_flags & AdaptiveSizeSet) { return ""; }
474+ const char* const sizes[] =
475+ { "xx-small", "x-small", "small", "medium", "large", "x-large" };
476+ return QString(sizes[m_adaptiveSize]);
477+ }
478+ void setFontSize(const QString& fontSize);
479+
480+Q_SIGNALS:
481+ Q_REVISION(1) void adaptiveSizeChanged();
482+
483+ // Deprecated.
484+ void fontSizeChanged();
485+
486+private:
487+ void updatePixelSize();
488+
489+ enum { AdaptiveSizeSet = 1 };
490+
491+ AdaptiveSize m_adaptiveSize;
492+ quint8 m_flags;
493+
494+ Q_DISABLE_COPY(UCLabel)
495+};
496+
497+QML_DECLARE_TYPE(UCLabel)
498+
499+#endif // UCLABEL_H
500
501=== modified file 'modules/Ubuntu/Components/qmldir'
502--- modules/Ubuntu/Components/qmldir 2015-03-02 13:39:03 +0000
503+++ modules/Ubuntu/Components/qmldir 2015-03-17 09:10:46 +0000
504@@ -11,7 +11,6 @@
505 Tab 0.1 Tab.qml
506 TabBar 0.1 TabBar.qml
507 Tabs 0.1 Tabs.qml
508-Label 0.1 Label.qml
509 AbstractButton 0.1 AbstractButton.qml
510 ActivityIndicator 0.1 ActivityIndicator.qml
511 ProgressBar 0.1 ProgressBar10.qml
512@@ -60,7 +59,6 @@
513 Tab 1.0 Tab.qml
514 TabBar 1.0 TabBar.qml
515 Tabs 1.0 Tabs.qml
516-Label 1.0 Label.qml
517 AbstractButton 1.0 AbstractButton.qml
518 ActivityIndicator 1.0 ActivityIndicator.qml
519 ProgressBar 1.0 ProgressBar10.qml

Subscribers

People subscribed via source and target branches