Merge lp:~zsombi/ubuntu-ui-toolkit/native-fontutils into lp:ubuntu-ui-toolkit

Proposed by Zsombor Egri
Status: Merged
Approved by: Florian Boucault
Approved revision: 500
Merged at revision: 502
Proposed branch: lp:~zsombi/ubuntu-ui-toolkit/native-fontutils
Merge into: lp:ubuntu-ui-toolkit
Diff against target: 333 lines (+203/-37)
9 files modified
modules/Ubuntu/Components/Label.qml (+0/-1)
modules/Ubuntu/Components/TextField.qml (+0/-1)
modules/Ubuntu/Components/fontUtils.js (+0/-31)
modules/Ubuntu/Components/plugin/plugin.cpp (+8/-1)
modules/Ubuntu/Components/plugin/plugin.pro (+4/-2)
modules/Ubuntu/Components/plugin/ucfontutils.cpp (+99/-0)
modules/Ubuntu/Components/plugin/ucfontutils.h (+40/-0)
modules/Ubuntu/Components/qmldir (+0/-1)
tests/unit/tst_components/tst_fontutils.qml (+52/-0)
To merge this branch: bzr merge lp:~zsombi/ubuntu-ui-toolkit/native-fontutils
Reviewer Review Type Date Requested Status
PS Jenkins bot continuous-integration Approve
Florian Boucault (community) Needs Fixing
Review via email: mp+164682@code.launchpad.net

Commit message

[theming] FontUtils converted from stateful JS library to native module due to its dependency to "units" context property.

To post a comment you must log in.
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
Florian Boucault (fboucault) wrote :

In ucfontutils.h:

* unnecessary includes:
#include <QtQml/QQmlEngine>
#include <QtQml/QJSEngine>

* copyright year should be 2013

review: Needs Fixing
Revision history for this message
Florian Boucault (fboucault) wrote :

In ucfontutils.cpp:

* unnecessary includes:
#include <QtCore/QMetaEnum>
#include <QtQml/QQmlInfo>

* copyright year should be 2013

* documentation generation fails; also I wonder how to document a context property

review: Needs Fixing
499. By Zsombor Egri

comments applied, more documentation added

500. By Zsombor Egri

documentation update

Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'modules/Ubuntu/Components/Label.qml'
--- modules/Ubuntu/Components/Label.qml 2013-04-11 18:00:34 +0000
+++ modules/Ubuntu/Components/Label.qml 2013-05-20 12:15:41 +0000
@@ -15,7 +15,6 @@
15 */15 */
1616
17import QtQuick 2.017import QtQuick 2.0
18import "fontUtils.js" as FontUtils
19// FIXME: When a module contains QML, C++ and JavaScript elements exported,18// FIXME: When a module contains QML, C++ and JavaScript elements exported,
20// we need to use named imports otherwise namespace collision is reported19// we need to use named imports otherwise namespace collision is reported
21// by the QML engine. As workaround, we use Theming named import.20// by the QML engine. As workaround, we use Theming named import.
2221
=== modified file 'modules/Ubuntu/Components/TextField.qml'
--- modules/Ubuntu/Components/TextField.qml 2013-05-19 19:23:40 +0000
+++ modules/Ubuntu/Components/TextField.qml 2013-05-20 12:15:41 +0000
@@ -15,7 +15,6 @@
15 */15 */
1616
17import QtQuick 2.017import QtQuick 2.0
18import "fontUtils.js" as FontUtils
19// FIXME: When a module contains QML, C++ and JavaScript elements exported,18// FIXME: When a module contains QML, C++ and JavaScript elements exported,
20// we need to use named imports otherwise namespace collision is reported19// we need to use named imports otherwise namespace collision is reported
21// by the QML engine. As workaround, we use Theming named import.20// by the QML engine. As workaround, we use Theming named import.
2221
=== removed file 'modules/Ubuntu/Components/fontUtils.js'
--- modules/Ubuntu/Components/fontUtils.js 2012-11-01 11:01:28 +0000
+++ modules/Ubuntu/Components/fontUtils.js 1970-01-01 00:00:00 +0000
@@ -1,31 +0,0 @@
1/*
2 * Copyright 2012 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
17function sizeToPixels(size) {
18 var baseSize = units.dp(14)
19 return Math.round(modularScale(size) * baseSize)
20}
21
22function modularScale(size) {
23 switch (size) {
24 case "xx-small": return 0.606
25 case "x-small": return 0.707
26 case "small": return 0.857
27 case "medium": return 1.0
28 case "large": return 1.414
29 case "x-large": return 2.423
30 }
31}
320
=== modified file 'modules/Ubuntu/Components/plugin/plugin.cpp'
--- modules/Ubuntu/Components/plugin/plugin.cpp 2013-05-09 19:04:28 +0000
+++ modules/Ubuntu/Components/plugin/plugin.cpp 2013-05-20 12:15:41 +0000
@@ -41,6 +41,7 @@
41#include "bottombarvisibilitycommunicator.h"41#include "bottombarvisibilitycommunicator.h"
42#include "ucstyle.h"42#include "ucstyle.h"
43#include "ucubuntuanimation.h"43#include "ucubuntuanimation.h"
44#include "ucfontutils.h"
4445
45#include <sys/types.h>46#include <sys/types.h>
46#include <unistd.h>47#include <unistd.h>
@@ -96,12 +97,18 @@
96 QObject::connect(&UCUnits::instance(), SIGNAL(gridUnitChanged()),97 QObject::connect(&UCUnits::instance(), SIGNAL(gridUnitChanged()),
97 &unitsChangeListener, SLOT(updateContextProperty()));98 &unitsChangeListener, SLOT(updateContextProperty()));
9899
100 // register FontUtils
101 context->setContextProperty("FontUtils", &UCFontUtils::instance());
102 static ContextPropertyChangeListener fontUtilsListener(context, "FontUtils");
103 QObject::connect(&UCUnits::instance(), SIGNAL(gridUnitChanged()),
104 &fontUtilsListener, SLOT(updateContextProperty()));
105
99 context->setContextProperty("bottomBarVisibilityCommunicator", &BottomBarVisibilityCommunicator::instance());106 context->setContextProperty("bottomBarVisibilityCommunicator", &BottomBarVisibilityCommunicator::instance());
100107
101 engine->addImageProvider(QLatin1String("scaling"), new UCScalingImageProvider);108 engine->addImageProvider(QLatin1String("scaling"), new UCScalingImageProvider);
102109
103 // register gicon provider110 // register gicon provider
104 engine->addImageProvider(QLatin1String("gicon"), new GIconProvider);111 engine->addImageProvider(QLatin1String("gicon"), new GIconProvider);
105112
106 // Necessary for Screen.orientation (from import QtQuick.Window 2.0) to work113 // Necessary for Screen.orientation (from import QtQuick.Window 2.0) to work
107 QGuiApplication::primaryScreen()->setOrientationUpdateMask(114 QGuiApplication::primaryScreen()->setOrientationUpdateMask(
108115
=== modified file 'modules/Ubuntu/Components/plugin/plugin.pro'
--- modules/Ubuntu/Components/plugin/plugin.pro 2013-05-09 19:04:28 +0000
+++ modules/Ubuntu/Components/plugin/plugin.pro 2013-05-20 12:15:41 +0000
@@ -44,7 +44,8 @@
44 qquickclipboard_p.h \44 qquickclipboard_p.h \
45 selector_p.h \45 selector_p.h \
46 ucstyle.h \46 ucstyle.h \
47 ucubuntuanimation.h47 ucubuntuanimation.h \
48 ucfontutils.h
4849
49SOURCES += plugin.cpp \50SOURCES += plugin.cpp \
50 themeengine.cpp \51 themeengine.cpp \
@@ -66,7 +67,8 @@
66 qquickmimedata.cpp \67 qquickmimedata.cpp \
67 selector.cpp \68 selector.cpp \
68 ucstyle.cpp \69 ucstyle.cpp \
69 ucubuntuanimation.cpp70 ucubuntuanimation.cpp \
71 ucfontutils.cpp
7072
71# deployment rules for the plugin73# deployment rules for the plugin
72installPath = $$[QT_INSTALL_QML]/$$replace(uri, \\., /)74installPath = $$[QT_INSTALL_QML]/$$replace(uri, \\., /)
7375
=== added file 'modules/Ubuntu/Components/plugin/ucfontutils.cpp'
--- modules/Ubuntu/Components/plugin/ucfontutils.cpp 1970-01-01 00:00:00 +0000
+++ modules/Ubuntu/Components/plugin/ucfontutils.cpp 2013-05-20 12:15:41 +0000
@@ -0,0 +1,99 @@
1/*
2 * Copyright 2013 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 */
17
18#include "ucfontutils.h"
19#include "ucunits.h"
20
21/*!
22 * \qmltype FontUtils
23 * \instantiates UCFontUtils
24 * \inqmlmodule Ubuntu.Components 0.1
25 * \ingroup ubuntu
26 * \brief FontUtils is a \l {http://qt-project.org/doc/qt-5.0/qtqml/qqmlcontext.html#details} {context property},
27 * which provides utility functions for font manipulations.
28 *
29 * FontUtils cannot be instantiated and it is already available as a context
30 * property, i.e. 'FontUtils' is an instance that can be used from anywhere in
31 * the code.
32 *
33 * Example of a Label implementation which uses the default base scale for the font
34 * size:
35 * \qml
36 * import QtQuick 2.0
37 * import Ubuntu.Components 0.1
38 *
39 * Text {
40 * property string fontSize: "medium"
41 * font.pixelSize: FontUtils.sizeToPixels(fontSize)
42 * }
43 * \endqml
44 *
45 * Another example of a custom text input component exposing a fontSize property and a base
46 * font unit size to scale its font:
47 * \qml
48 * import QtQuick 2.0
49 * import Ubuntu.Components 0.1
50 *
51 * TextInput {
52 * property string fontSize: "small"
53 * property int baseFontUnits: units.dp(20)
54 * font.pixelSize: FontUtils.modularScale(fontSize) * units.dp(baseFontUnits)
55 * }
56 * \endqml
57 *
58 */
59
60/*!
61 * \qmlmethod real FontUtils::sizeToPixels(string size)
62 * The function calculates the pixel size of a given scale. The size scale can be
63 * one of the strings specified at modularScale function. On failure returns 0.
64 */
65qreal UCFontUtils::sizeToPixels(const QString &size)
66{
67 return modularScale(size) * UCUnits::instance().dp(14);
68}
69
70/*!
71 * \qmlmethod real FontUtils::modularScale(string size)
72 * The function returns the number interpretation of a given font scale. The scale
73 * can have one of the following values:
74 * \list
75 * \li \b{xx-small}
76 * \li \b{x-small}
77 * \li \b{small}
78 * \li \b{medium}
79 * \li \b{large}
80 * \li \b{x-large}
81 * \endlist
82 */
83qreal UCFontUtils::modularScale(const QString &size)
84{
85 if (size == "xx-small") {
86 return 0.606;
87 } else if (size == "x-small") {
88 return 0.707;
89 } else if (size == "small") {
90 return 0.857;
91 } else if (size == "medium") {
92 return 1.0;
93 } else if (size == "large") {
94 return 1.414;
95 } else if (size == "x-large") {
96 return 2.423;
97 }
98 return 0.0;
99}
0100
=== added file 'modules/Ubuntu/Components/plugin/ucfontutils.h'
--- modules/Ubuntu/Components/plugin/ucfontutils.h 1970-01-01 00:00:00 +0000
+++ modules/Ubuntu/Components/plugin/ucfontutils.h 2013-05-20 12:15:41 +0000
@@ -0,0 +1,40 @@
1/*
2 * Copyright 2013 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 */
17
18#ifndef UCFONTUTILS_H
19#define UCFONTUTILS_H
20
21#include <QtCore/QObject>
22
23class UCFontUtils : public QObject
24{
25 Q_OBJECT
26public:
27
28 static UCFontUtils& instance()
29 {
30 static UCFontUtils instance;
31 return instance;
32 }
33
34 explicit UCFontUtils(QObject *parent = 0) : QObject(parent) {}
35
36 Q_INVOKABLE qreal sizeToPixels(const QString &size);
37 Q_INVOKABLE qreal modularScale(const QString &size);
38};
39
40#endif // UCFONTUTILS_H
041
=== modified file 'modules/Ubuntu/Components/qmldir'
--- modules/Ubuntu/Components/qmldir 2013-05-20 09:06:47 +0000
+++ modules/Ubuntu/Components/qmldir 2013-05-20 12:15:41 +0000
@@ -33,7 +33,6 @@
33UbuntuShape 0.1 UbuntuShape.qml33UbuntuShape 0.1 UbuntuShape.qml
34Icon 0.1 Icon.qml34Icon 0.1 Icon.qml
35OrientationHelper 0.1 OrientationHelper.qml35OrientationHelper 0.1 OrientationHelper.qml
36FontUtils 0.1 fontUtils.js
37MathUtils 0.1 mathUtils.js36MathUtils 0.1 mathUtils.js
38SliderUtils 0.1 sliderUtils.js37SliderUtils 0.1 sliderUtils.js
39ScrollbarUtils 0.1 scrollbarUtils.js38ScrollbarUtils 0.1 scrollbarUtils.js
4039
=== added file 'tests/unit/tst_components/tst_fontutils.qml'
--- tests/unit/tst_components/tst_fontutils.qml 1970-01-01 00:00:00 +0000
+++ tests/unit/tst_components/tst_fontutils.qml 2013-05-20 12:15:41 +0000
@@ -0,0 +1,52 @@
1/*
2 * Copyright 2012 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
17import QtQuick 2.0
18import QtTest 1.0
19import Ubuntu.Components 0.1
20
21TestCase {
22 name: "FontUtilsAPI"
23
24 function test_modularScale() {
25 compare(FontUtils.modularScale("xx-small"), 0.606, "xx-small scale");
26 compare(FontUtils.modularScale("x-small"), 0.707, "x-small scale");
27 compare(FontUtils.modularScale("small"), 0.857, "small scale");
28 compare(FontUtils.modularScale("medium"), 1.0, "medium scale");
29 compare(FontUtils.modularScale("large"), 1.414, "large scale");
30 compare(FontUtils.modularScale("x-large"), 2.423, "x-large scale");
31 }
32
33 function test_modularScale_failures() {
34 compare(FontUtils.modularScale("xxsmall"), 0.0, "xxsmall not defined");
35 compare(FontUtils.modularScale("undefined"), 0.0, "undefined scale");
36 }
37
38 function test_sizeToPixels() {
39 compare(FontUtils.sizeToPixels("xx-small"), 0.606 * units.dp(14), "xx-small scale");
40 compare(FontUtils.sizeToPixels("x-small"), 0.707 * units.dp(14), "x-small scale");
41 compare(FontUtils.sizeToPixels("small"), 0.857 * units.dp(14), "small scale");
42 compare(FontUtils.sizeToPixels("medium"), 1.0 * units.dp(14), "medium scale");
43 compare(FontUtils.sizeToPixels("large"), 1.414 * units.dp(14), "large scale");
44 compare(FontUtils.sizeToPixels("x-large"), 2.423 * units.dp(14), "x-large scale");
45 }
46
47 function test_sizeToPixels_failures() {
48 compare(FontUtils.sizeToPixels("xxsmall"), 0.0, "xxsmall not defined");
49 compare(FontUtils.sizeToPixels("undefined"), 0.0, "undefined scale");
50 }
51
52}

Subscribers

People subscribed via source and target branches

to status/vote changes: