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 <QtGui/private/qguiapplication_p.h>
8#include <QtGui/qpa/qplatformtheme.h>
9#include <QtQuick/private/qquicktext_p_p.h>
10
12
13QQuickMnemonicLabel::QQuickMnemonicLabel(QQuickItem *parent)
14 : QQuickText(parent)
15{
16}
17
18QString QQuickMnemonicLabel::text() const
19{
20 return m_fullText;
21}
22
23void QQuickMnemonicLabel::setText(const QString &text)
24{
25 if (m_fullText == text)
26 return;
27
28 m_fullText = text;
29 updateMnemonic();
30}
31
32/*!
33 \internal
34
35 This property determines whether \c "&" in \l text is treated as a
36 mnemonic marker (\c true) or displayed literally (\c false).
37
38 If \c true, QQuickMnemonicLabel parses \c "&" as a mnemonic marker and
39 removes it from the displayed text, underlining the character that
40 follows it (see \c updateMnemonic()). The character is not underlined when
41 \c QPlatformTheme::UnderlineShortcut is \c false for the current platform,
42 but the \c "&" is still removed.
43
44 If \c false, \c "&" has no special meaning and is displayed literally,
45 which allows it to be used in text such as \c "Cats & Dogs".
46
47 For example, given \l text of \c "M&nemonic":
48 \list
49 \li \c true: the displayed text is \c "Mnemonic", with the \c "n" underlined
50 (depending on \c QPlatformTheme::UnderlineShortcut).
51 \li \c false: the displayed text is \c "M&nemonic" (unchanged).
52 \endlist
53
54 The default value is \c true.
55*/
56bool QQuickMnemonicLabel::isMnemonicEnabled() const
57{
58 return m_mnemonicEnabled;
59}
60
61void QQuickMnemonicLabel::setMnemonicEnabled(bool enabled)
62{
63 if (m_mnemonicEnabled == enabled)
64 return;
65
66 m_mnemonicEnabled = enabled;
67 updateMnemonic();
68
69 if (isComponentComplete())
70 forceLayout();
71}
72
73static QTextLayout::FormatRange underlineRange(int start, int length = 1)
74{
75 QTextLayout::FormatRange range;
76 range.start = start;
77 range.length = length;
78 range.format.setFontUnderline(true);
79 return range;
80}
81
82// based on QPlatformTheme::removeMnemonics()
83void QQuickMnemonicLabel::updateMnemonic()
84{
85 if (!m_mnemonicEnabled) {
86 // "&" has no special meaning; display the text as-is.
87 QQuickTextPrivate::get(this)->layout.setFormats({});
88 QQuickText::setText(m_fullText);
89 return;
90 }
91
92 const bool showUnderline = QGuiApplicationPrivate::platformTheme()->themeHint(
93 QPlatformTheme::UnderlineShortcut).toBool();
94
95 QString text(m_fullText.size(), QChar::Null);
96 int idx = 0;
97 int pos = 0;
98 int len = m_fullText.size();
99 QList<QTextLayout::FormatRange> formats;
100 while (len) {
101 if (m_fullText.at(pos) == QLatin1Char('&') && (len == 1 || m_fullText.at(pos + 1) != QLatin1Char('&'))) {
102 // A plain mnemonic marker, e.g. "M&nemonic": drop the "&" and
103 // underline the character that follows it (if the platform supports it).
104 if (showUnderline && (pos == 0 || m_fullText.at(pos - 1) != QLatin1Char('&')))
105 formats += underlineRange(pos);
106 ++pos;
107 --len;
108 if (len == 0)
109 break;
110 } else if (m_fullText.at(pos) == QLatin1Char('(') && len >= 4 &&
111 m_fullText.at(pos + 1) == QLatin1Char('&') &&
112 m_fullText.at(pos + 2) != QLatin1Char('&') &&
113 m_fullText.at(pos + 3) == QLatin1Char(')')) {
114 // A mnemonic with format "\s*(&X)", used when the label itself has
115 // no natural character to underline (e.g. non-Latin scripts). Keep
116 // "X" in the text, and underline it if the platform supports it.
117 if (showUnderline)
118 formats += underlineRange(pos + 1);
119 }
120 text[idx] = m_fullText.at(pos);
121 ++pos;
122 ++idx;
123 --len;
124 }
125 text.truncate(idx);
126
127 QQuickTextPrivate::get(this)->layout.setFormats(formats);
128 QQuickText::setText(text);
129}
130
131QT_END_NAMESPACE
132
133#include "moc_qquickmnemoniclabel_p.cpp"
Combined button and popup list for selecting options.
static QTextLayout::FormatRange underlineRange(int start, int length=1)