1 | /**************************************************************************** |
---|---|
2 | ** |
3 | ** Copyright (C) 2017 The Qt Company Ltd. |
4 | ** Contact: http://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the Qt Quick Controls 2 module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL3$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see http://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at http://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPLv3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or later as published by the Free |
28 | ** Software Foundation and appearing in the file LICENSE.GPL included in |
29 | ** the packaging of this file. Please review the following information to |
30 | ** ensure the GNU General Public License version 2.0 requirements will be |
31 | ** met: http://www.gnu.org/licenses/gpl-2.0.html. |
32 | ** |
33 | ** $QT_END_LICENSE$ |
34 | ** |
35 | ****************************************************************************/ |
36 | |
37 | #include "qquickmnemoniclabel_p.h" |
38 | |
39 | #include <QtQuick/private/qquicktext_p_p.h> |
40 | |
41 | QT_BEGIN_NAMESPACE |
42 | |
43 | QQuickMnemonicLabel::QQuickMnemonicLabel(QQuickItem *parent) |
44 | : QQuickText(parent) |
45 | { |
46 | } |
47 | |
48 | QString QQuickMnemonicLabel::text() const |
49 | { |
50 | return m_fullText; |
51 | } |
52 | |
53 | void QQuickMnemonicLabel::setText(const QString &text) |
54 | { |
55 | if (m_fullText == text) |
56 | return; |
57 | |
58 | m_fullText = text; |
59 | updateMnemonic(); |
60 | } |
61 | |
62 | bool QQuickMnemonicLabel::isMnemonicVisible() const |
63 | { |
64 | return m_mnemonicVisible; |
65 | } |
66 | |
67 | void QQuickMnemonicLabel::setMnemonicVisible(bool visible) |
68 | { |
69 | if (m_mnemonicVisible == visible) |
70 | return; |
71 | |
72 | m_mnemonicVisible = visible; |
73 | updateMnemonic(); |
74 | |
75 | if (isComponentComplete()) |
76 | forceLayout(); |
77 | } |
78 | |
79 | static QTextLayout::FormatRange underlineRange(int start, int length = 1) |
80 | { |
81 | QTextLayout::FormatRange range; |
82 | range.start = start; |
83 | range.length = length; |
84 | range.format.setFontUnderline(true); |
85 | return range; |
86 | } |
87 | |
88 | // based on QPlatformTheme::removeMnemonics() |
89 | void QQuickMnemonicLabel::updateMnemonic() |
90 | { |
91 | QString text(m_fullText.size(), QChar::Null); |
92 | int idx = 0; |
93 | int pos = 0; |
94 | int len = m_fullText.length(); |
95 | QVector<QTextLayout::FormatRange> formats; |
96 | while (len) { |
97 | if (m_fullText.at(i: pos) == QLatin1Char('&') && (len == 1 || m_fullText.at(i: pos + 1) != QLatin1Char('&'))) { |
98 | if (m_mnemonicVisible && (pos == 0 || m_fullText.at(i: pos - 1) != QLatin1Char('&'))) |
99 | formats += underlineRange(start: pos); |
100 | ++pos; |
101 | --len; |
102 | if (len == 0) |
103 | break; |
104 | } else if (m_fullText.at(i: pos) == QLatin1Char('(') && len >= 4 && |
105 | m_fullText.at(i: pos + 1) == QLatin1Char('&') && |
106 | m_fullText.at(i: pos + 2) != QLatin1Char('&') && |
107 | m_fullText.at(i: pos + 3) == QLatin1Char(')')) { |
108 | // a mnemonic with format "\s*(&X)" |
109 | if (m_mnemonicVisible) { |
110 | formats += underlineRange(start: pos + 1); |
111 | } else { |
112 | int n = 0; |
113 | while (idx > n && text.at(i: idx - n - 1).isSpace()) |
114 | ++n; |
115 | idx -= n; |
116 | pos += 4; |
117 | len -= 4; |
118 | continue; |
119 | } |
120 | } |
121 | text[idx] = m_fullText.at(i: pos); |
122 | ++pos; |
123 | ++idx; |
124 | --len; |
125 | } |
126 | text.truncate(pos: idx); |
127 | |
128 | QQuickTextPrivate::get(t: this)->layout.setFormats(formats); |
129 | QQuickText::setText(text); |
130 | } |
131 | |
132 | QT_END_NAMESPACE |
133 |