File: uclabel.cpp

package info (click to toggle)
lomiri-ui-toolkit 1.3.5010%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 25,900 kB
  • sloc: cpp: 85,772; python: 5,528; sh: 1,364; javascript: 919; ansic: 573; makefile: 204
file content (290 lines) | stat: -rw-r--r-- 8,161 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
 * Copyright 2016 Canonical Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; version 3.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "label_p.h"

#include "quickutils_p.h"
#include "ucfontutils_p.h"
#include "uctheme_p.h"
#include "ucunits_p.h"

UT_NAMESPACE_BEGIN

UCLabelPrivate::UCLabelPrivate(UCLabel *qq)
    : q_ptr(qq)
    , defaultColor(getDefaultColor)
    , textSize(UCLabel::Medium)
    , flags(0)
{
}

UCLabelPrivate::UCLabelPrivate(UCLabel *qq, UCLabel::ColorProviderFunc func)
    : q_ptr(qq)
    , defaultColor(func)
    , textSize(UCLabel::Medium)
    , flags(0)
{
}

void UCLabelPrivate::updatePixelSize()
{
    if (flags & PixelSizeSet) {
        return;
    }

    Q_Q(UCLabel);
    const float sizes[] = {
        UCFontUtils::xxSmallScale, UCFontUtils::xSmallScale, UCFontUtils::smallScale,
        UCFontUtils::mediumScale, UCFontUtils::largeScale, UCFontUtils::xLargeScale
    };
    QFont textFont = q->font();
    textFont.setPixelSize(
        qRound(sizes[textSize] * UCUnits::instance()->dp(UCFontUtils::fontUnits)));
    q->setFont(textFont);
}

void UCLabelPrivate::updateRenderType()
{
    Q_Q(UCLabel);
    QQuickText *qtext = static_cast<QQuickText*>(q);
    if (UCUnits::instance()->gridUnit() <= 10) {
        qtext->setRenderType(QQuickText::NativeRendering);
    } else {
        qtext->setRenderType(QQuickText::QtRendering);
    }
}

/*!
 * \qmltype Label
 * \qmlabstract
 * \instantiates UCLabel
 * \inherits Text
 * \inqmlmodule Lomiri.Components 1.3
 * \ingroup lomiri
 * \brief Extended Text item with Lomiri styling.
 *
 * Label is an extended Text item with Lomiri styling. It exposes an additional property that
 * provides adaptive resizing based on the measurement unit.
 *
 * Example:
 * \qml
 * Rectangle {
 *     color: LomiriColors.warmGrey
 *     width: units.gu(30)
 *     height: units.gu(30)
 *
 *     Label {
 *         anchors.centerIn: parent
 *         text: "Hello world!"
 *         textSize: Label.Large
 *     }
 * }
 * \endqml
 */
UCLabel::UCLabel(QQuickItem* parent)
    : QQuickText(parent)
    , UCThemingExtension(this)
    , d_ptr(new UCLabelPrivate(this))
{
}

UCLabel::UCLabel(ColorProviderFunc defaultColor, QQuickItem *parent)
    : QQuickText(parent)
    , UCThemingExtension(this)
    , d_ptr(new UCLabelPrivate(this, defaultColor))
{
}
UCLabel::~UCLabel()
{
    // disconnect functor, so QQuickItem's enabledChanged won't call into the invalid functor
    disconnect(this, &UCLabel::enabledChanged, this, &UCLabel::postThemeChanged);
}

QColor UCLabelPrivate::getDefaultColor(QQuickItem *item, UCTheme *theme)
{
    // FIXME: replace the code below with automatic color
    // change detection based on the item's state
    const char *valueSet = item->isEnabled() ? "normal" : "disabled";
    return theme ? theme->getPaletteColor(valueSet, "backgroundText") : QColor();
}

void UCLabel::classBegin()
{
    Q_D(UCLabel);
    QQuickText::classBegin();
    d->init();
}

void UCLabelPrivate::init()
{
    Q_Q(UCLabel);
    q->postThemeChanged();

    updatePixelSize();
    QFont defaultFont = q->font();
    defaultFont.setFamily(QStringLiteral("Noto Sans"));
    defaultFont.setWeight(QFont::Light);
    q->setFont(defaultFont);
    updateRenderType();

    QObject::connect(UCUnits::instance(), SIGNAL(gridUnitChanged()), q, SLOT(updateRenderType()));
    QObject::connect(UCUnits::instance(), SIGNAL(gridUnitChanged()), q, SLOT(updatePixelSize()));

    QObject::connect(q, &UCLabel::enabledChanged, q, &UCLabel::postThemeChanged, Qt::DirectConnection);

    QObject::connect(q, &UCLabel::fontChanged, q, &UCLabel::fontChanged2, Qt::DirectConnection);
    QObject::connect(q, &UCLabel::colorChanged, q, &UCLabel::colorChanged2, Qt::DirectConnection);
}

void UCLabel::postThemeChanged()
{
    Q_D(UCLabel);
    if (d->flags & UCLabelPrivate::ColorSet) {
        return;
    }
    UCTheme *theme = getTheme();
    if (theme) {
        setColor(d->defaultColor(this, theme));
    }
}

/*!
 * \qmlproperty enumeration Label::textSize
 * \since Lomiri.Components 1.3
 *
 * This property holds an abstract size that allows adaptive resizing based on the measurement unit
 * (see Units). The default value is \c Label.Medium.
 *
 * \note Setting this disables support for the deprecated \l fontSize property.
 *
 * \list
 *  \li \b Label.XxSmall - extremely small font size
 *  \li \b Label.XSmall - very small font size
 *  \li \b Label.Small - small font size
 *  \li \b Label.Medium - medium font size
 *  \li \b Label.Large - large font size
 *  \li \b Label.XLarge - very large font size
 *  \endlist
 */
UCLabel::TextSize UCLabel::textSize() const
{
    Q_D(const UCLabel);
    return d->textSize;
}
void UCLabel::setTextSize(TextSize size)
{
    Q_D(UCLabel);
    if (!(d->flags & UCLabelPrivate::TextSizeSet)) {
        Q_EMIT fontSizeChanged();
        d->flags |= UCLabelPrivate::TextSizeSet;
    }

    if (d->textSize != size) {
        d->textSize = size;
        d->updatePixelSize();
        Q_EMIT textSizeChanged();
    }
}

void UCLabel::setFont2(const QFont &font)
{
    Q_D(UCLabel);
    // we must restrict ourself to the pixelSize change as any font property change will
    // lead to the setter call.
    if (this->font().pixelSize() != font.pixelSize()) {
        d->flags |= UCLabelPrivate::PixelSizeSet;
    }
    QQuickText::setFont(font);
}

void UCLabel::setColor2(const QColor &color)
{
    Q_D(UCLabel);
    d->flags |= UCLabelPrivate::ColorSet;
    QQuickText::setColor(color);
}

void UCLabel::setRenderType(RenderType renderType)
{
    disconnect(UCUnits::instance(), SIGNAL(gridUnitChanged()),
               this, SLOT(updateRenderType()));
    QQuickText::setRenderType(renderType);
}

/*!
 * \qmlproperty string Label::fontSize
 * \deprecated
 *
 * This property holds an abstract size represented as a string that allows adaptive resizing based
 * on the measurement unit (see Units). The default value is \c "medium".
 *
 * \note Use \l textSize instead.
 *
 * Here is the list of allowed strings from the smallest to the largest:
 * \list
 *  \li \b "xx-small" - extremely small font size
 *  \li \b "x-small" - very small font size
 *  \li \b "small" - small font size
 *  \li \b "medium" - medium font size
 *  \li \b "large" - large font size
 *  \li \b "x-large" - very large font size
 *  \endlist
 */
QString UCLabel::fontSize() const
{
    Q_D(const UCLabel);
    if (d->flags & UCLabelPrivate::TextSizeSet) {
        return QStringLiteral("");
    }
    static const QString sizes[] = {
        QStringLiteral("xx-small"), QStringLiteral("x-small"), QStringLiteral("small"),
        QStringLiteral("medium"), QStringLiteral("large"), QStringLiteral("x-large")
    };
    return sizes[d->textSize];
}
void UCLabel::setFontSize(const QString& fontSize)
{
    Q_D(UCLabel);
    if (d->flags & UCLabelPrivate::TextSizeSet) {
        return;
    }
    if (fontSize.size() < 4) {
        return;
    }

    UC_QML_DEPRECATION_WARNING("'fontSize' is deprecated, use 'textSize' property instead.");

    TextSize textSize;
    switch (SCALE_CODE(fontSize)) {
        case SCALE_MEDIUM: { textSize = Medium; break; }
        case SCALE_LARGE: { textSize = Large; break; }
        case SCALE_SMALL: { textSize = Small; break; }
        case SCALE_XLARGE: { textSize = XLarge; break; }
        case SCALE_XSMALL: { textSize = XSmall; break; }
        case SCALE_XXSMALL: { textSize = XxSmall; break; }
        default: { return; }
    }

    if (d->textSize != textSize) {
        d->textSize = textSize;
        d->updatePixelSize();
        Q_EMIT fontSizeChanged();
    }
}

UT_NAMESPACE_END

#include "moc_uclabel_p.cpp"