Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qquickmnemoniclabel.cpp
Go to the documentation of this file.
1// Copyright (C) 2017 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
6
7#include <QtQuick/private/qquicktext_p_p.h>
8
10
11QQuickMnemonicLabel::QQuickMnemonicLabel(QQuickItem *parent)
12 : QQuickText(parent)
13{
14}
15
16QString QQuickMnemonicLabel::text() const
17{
18 return m_fullText;
19}
20
21void QQuickMnemonicLabel::setText(const QString &text)
22{
23 if (m_fullText == text)
24 return;
25
26 m_fullText = text;
27 updateMnemonic();
28}
29
30bool QQuickMnemonicLabel::isMnemonicVisible() const
31{
32 return m_mnemonicVisible;
33}
34
35void QQuickMnemonicLabel::setMnemonicVisible(bool visible)
36{
37 if (m_mnemonicVisible == visible)
38 return;
39
40 m_mnemonicVisible = visible;
41 updateMnemonic();
42
43 if (isComponentComplete())
44 forceLayout();
45}
46
47static QTextLayout::FormatRange underlineRange(int start, int length = 1)
48{
49 QTextLayout::FormatRange range;
50 range.start = start;
51 range.length = length;
52 range.format.setFontUnderline(true);
53 return range;
54}
55
56// based on QPlatformTheme::removeMnemonics()
57void QQuickMnemonicLabel::updateMnemonic()
58{
59 QString text(m_fullText.size(), QChar::Null);
60 int idx = 0;
61 int pos = 0;
62 int len = m_fullText.size();
63 QList<QTextLayout::FormatRange> formats;
64 while (len) {
65 if (m_fullText.at(pos) == QLatin1Char('&') && (len == 1 || m_fullText.at(pos + 1) != QLatin1Char('&'))) {
66 if (m_mnemonicVisible && (pos == 0 || m_fullText.at(pos - 1) != QLatin1Char('&')))
67 formats += underlineRange(pos);
68 ++pos;
69 --len;
70 if (len == 0)
71 break;
72 } else if (m_fullText.at(pos) == QLatin1Char('(') && len >= 4 &&
73 m_fullText.at(pos + 1) == QLatin1Char('&') &&
74 m_fullText.at(pos + 2) != QLatin1Char('&') &&
75 m_fullText.at(pos + 3) == QLatin1Char(')')) {
76 // a mnemonic with format "\s*(&X)"
77 if (m_mnemonicVisible) {
78 formats += underlineRange(pos + 1);
79 } else {
80 int n = 0;
81 while (idx > n && text.at(idx - n - 1).isSpace())
82 ++n;
83 idx -= n;
84 pos += 4;
85 len -= 4;
86 continue;
87 }
88 }
89 text[idx] = m_fullText.at(pos);
90 ++pos;
91 ++idx;
92 --len;
93 }
94 text.truncate(idx);
95
96 QQuickTextPrivate::get(this)->layout.setFormats(formats);
97 QQuickText::setText(text);
98}
99
100QT_END_NAMESPACE
101
102#include "moc_qquickmnemoniclabel_p.cpp"
static QTextLayout::FormatRange underlineRange(int start, int length=1)