|
3 | 3 |
|
4 | 4 | #include "DigitalClockLabel_p.h" |
5 | 5 |
|
| 6 | +#include <algorithm> |
| 7 | + |
| 8 | +#include <QFontMetricsF> |
| 9 | +#include <QTextLayout> |
| 10 | + |
6 | 11 | #include <QtQuick/private/qquicktext_p_p.h> |
7 | 12 |
|
8 | 13 | namespace UIShell { |
9 | 14 |
|
10 | 15 | DigitalClockLabel::DigitalClockLabel(QQuickItem *parent) : QQuickText(parent) { |
11 | 16 | } |
| 17 | + |
12 | 18 | DigitalClockLabel::~DigitalClockLabel() = default; |
13 | 19 |
|
14 | 20 | QString DigitalClockLabel::text() const { |
15 | | - return m_fullText; |
| 21 | + return m_text; |
16 | 22 | } |
17 | 23 |
|
18 | 24 | void DigitalClockLabel::setText(const QString &text) { |
19 | | - if (m_fullText == text) |
| 25 | + if (text == m_text) { |
20 | 26 | return; |
21 | | - |
22 | | - m_fullText = text; |
| 27 | + } |
| 28 | + m_text = text; |
23 | 29 | updateDigitalFormat(); |
24 | | - Q_EMIT textChanged(); |
25 | 30 | } |
26 | 31 |
|
27 | | - bool DigitalClockLabel::fineTuneEnabled() const { |
28 | | - return m_fineTuneEnabled; |
| 32 | + QFont DigitalClockLabel::font() const { |
| 33 | + return m_font; |
29 | 34 | } |
30 | 35 |
|
31 | | - void DigitalClockLabel::setFineTuneEnabled(bool enabled) { |
32 | | - if (m_fineTuneEnabled == enabled) |
| 36 | + void DigitalClockLabel::setFont(const QFont &font) { |
| 37 | + if (font == m_font) { |
33 | 38 | return; |
34 | | - |
35 | | - m_fineTuneEnabled = enabled; |
| 39 | + } |
| 40 | + m_font = font; |
36 | 41 | updateDigitalFormat(); |
| 42 | + Q_EMIT fontChanged(); |
| 43 | + } |
37 | 44 |
|
38 | | - if (isComponentComplete()) |
39 | | - forceLayout(); |
40 | | - |
41 | | - Q_EMIT fineTuneEnabledChanged(); |
| 45 | + DigitalClockLabel::FineTuneMode DigitalClockLabel::fineTuneMode() const { |
| 46 | + return m_fineTuneMode; |
42 | 47 | } |
43 | 48 |
|
44 | | - static inline QTextLayout::FormatRange fixedPitchRange(int start, int length) { |
45 | | - QTextLayout::FormatRange range; |
46 | | - range.start = start; |
47 | | - range.length = length; |
48 | | - range.format.setFontFixedPitch(true); |
49 | | - return range; |
| 49 | + void DigitalClockLabel::setFineTuneMode(FineTuneMode mode) { |
| 50 | + if (m_fineTuneMode == mode) { |
| 51 | + return; |
| 52 | + } |
| 53 | + m_fineTuneMode = mode; |
| 54 | + updateDigitalFormat(); |
| 55 | + Q_EMIT fineTuneModeChanged(); |
50 | 56 | } |
51 | 57 |
|
52 | | - static constexpr bool isIncludedChar(QChar c) { |
53 | | - return c.isDigit(); |
| 58 | + static QList<QTextLayout::FormatRange> makeTabularDigitFormats(const QString &text, const QFont &font) { |
| 59 | + QFontMetricsF fm(font); |
| 60 | + |
| 61 | + qreal digitWidth = 0.0; |
| 62 | + for (int i = 0; i <= 9; i++) { |
| 63 | + const QChar ch(static_cast<uchar>('0' + i)); |
| 64 | + digitWidth = std::max(digitWidth, fm.horizontalAdvance(ch)); |
| 65 | + } |
| 66 | + |
| 67 | + QList<QTextLayout::FormatRange> formats; |
| 68 | + formats.reserve(text.size()); |
| 69 | + |
| 70 | + for (qsizetype i = 0; i < text.size(); ++i) { |
| 71 | + const QChar ch = text[i]; |
| 72 | + |
| 73 | + if (!ch.isDigit()) |
| 74 | + continue; |
| 75 | + |
| 76 | + const qreal width = fm.horizontalAdvance(ch); |
| 77 | + const qreal extraSpacing = digitWidth - width; |
| 78 | + |
| 79 | + QTextCharFormat format; |
| 80 | + |
| 81 | + format.setFontKerning(false); |
| 82 | + |
| 83 | + format.setFontLetterSpacingType(QFont::AbsoluteSpacing); |
| 84 | + format.setFontLetterSpacing(extraSpacing); |
| 85 | + |
| 86 | + QTextLayout::FormatRange range; |
| 87 | + range.start = static_cast<int>(i); |
| 88 | + range.length = 1; |
| 89 | + range.format = format; |
| 90 | + |
| 91 | + formats.append(range); |
| 92 | + } |
| 93 | + |
| 94 | + return formats; |
54 | 95 | } |
55 | 96 |
|
56 | 97 | void DigitalClockLabel::updateDigitalFormat() { |
57 | | - // FIXME |
58 | | - QString text = m_fullText; |
| 98 | + auto font = m_font; |
| 99 | + switch (m_fineTuneMode) { |
| 100 | + case None: |
| 101 | + font.unsetFeature("tnum"); |
| 102 | + break; |
| 103 | + case TabularFiguresFontFeature: |
| 104 | + font.setFeature("tnum", 1); |
| 105 | + break; |
| 106 | + case LayoutSimulation: |
| 107 | + font.unsetFeature("tnum"); |
| 108 | + break; |
| 109 | + } |
| 110 | + QQuickText::setFont(font); |
| 111 | + QQuickText::setText(m_text); |
59 | 112 | QList<QTextLayout::FormatRange> formats; |
60 | | - if (m_fineTuneEnabled) { |
61 | | - formats += fixedPitchRange(0, m_fullText.length()); |
| 113 | + if (m_fineTuneMode == LayoutSimulation) { |
| 114 | + formats = makeTabularDigitFormats(m_text, font); |
62 | 115 | } |
63 | 116 | QQuickTextPrivate::get(this)->layout.setFormats(formats); |
64 | | - QQuickText::setText(text); |
| 117 | + if (isComponentComplete()) { |
| 118 | + forceLayout(); |
| 119 | + } |
65 | 120 | } |
66 | 121 |
|
67 | 122 | } |
|
0 commit comments