Changeset View
Changeset View
Standalone View
Standalone View
src/mnemonicattached.cpp
Show All 18 Lines | |||||
19 | 19 | | |||
20 | #include "mnemonicattached.h" | 20 | #include "mnemonicattached.h" | ||
21 | #include <QQuickItem> | 21 | #include <QQuickItem> | ||
22 | #include <QQuickWindow> | 22 | #include <QQuickWindow> | ||
23 | #include <QQuickRenderControl> | 23 | #include <QQuickRenderControl> | ||
24 | #include <QDebug> | 24 | #include <QDebug> | ||
25 | 25 | | |||
26 | QHash<QKeySequence, MnemonicAttached *> MnemonicAttached::s_sequenceToObject = QHash<QKeySequence, MnemonicAttached *>(); | 26 | QHash<QKeySequence, MnemonicAttached *> MnemonicAttached::s_sequenceToObject = QHash<QKeySequence, MnemonicAttached *>(); | ||
27 | QHash<MnemonicAttached *, QKeySequence> MnemonicAttached::s_objectToSequence = QHash<MnemonicAttached *, QKeySequence>(); | | |||
28 | 27 | | |||
29 | MnemonicAttached::MnemonicAttached(QObject *parent) | 28 | MnemonicAttached::MnemonicAttached(QObject *parent) | ||
30 | : QObject(parent) | 29 | : QObject(parent) | ||
31 | { | 30 | { | ||
32 | QQuickItem *parentItem = qobject_cast<QQuickItem *>(parent); | 31 | QQuickItem *parentItem = qobject_cast<QQuickItem *>(parent); | ||
33 | if (parentItem) { | 32 | if (parentItem) { | ||
34 | if (parentItem->window()) { | 33 | if (parentItem->window()) { | ||
35 | m_window = parentItem->window(); | 34 | m_window = parentItem->window(); | ||
Show All 20 Lines | 48 | if (m_window) { | |||
56 | } | 55 | } | ||
57 | } | 56 | } | ||
58 | }); | 57 | }); | ||
59 | } | 58 | } | ||
60 | } | 59 | } | ||
61 | 60 | | |||
62 | MnemonicAttached::~MnemonicAttached() | 61 | MnemonicAttached::~MnemonicAttached() | ||
63 | { | 62 | { | ||
64 | QHash<MnemonicAttached *, QKeySequence>::iterator i = s_objectToSequence.find(this); | 63 | s_sequenceToObject.remove(m_sequence); | ||
65 | if (i != s_objectToSequence.end()) { | | |||
66 | s_sequenceToObject.remove(i.value()); | | |||
67 | s_objectToSequence.erase(i); | | |||
68 | } | | |||
69 | } | 64 | } | ||
70 | 65 | | |||
71 | bool MnemonicAttached::eventFilter(QObject *watched, QEvent *e) | 66 | bool MnemonicAttached::eventFilter(QObject *watched, QEvent *e) | ||
72 | { | 67 | { | ||
73 | Q_UNUSED(watched) | 68 | Q_UNUSED(watched) | ||
74 | 69 | | |||
75 | if (m_richTextLabel.isEmpty()) { | 70 | if (m_richTextLabel.isEmpty()) { | ||
76 | return false; | 71 | return false; | ||
▲ Show 20 Lines • Show All 81 Lines • ▼ Show 20 Line(s) | 152 | if (m_weights.isEmpty()) { | |||
158 | m_weight = m_baseWeight; | 153 | m_weight = m_baseWeight; | ||
159 | } else { | 154 | } else { | ||
160 | m_weight = m_baseWeight + m_weights.keys().last(); | 155 | m_weight = m_baseWeight + m_weights.keys().last(); | ||
161 | } | 156 | } | ||
162 | } | 157 | } | ||
163 | 158 | | |||
164 | void MnemonicAttached::updateSequence() | 159 | void MnemonicAttached::updateSequence() | ||
165 | { | 160 | { | ||
166 | //forget about old association | 161 | if (!m_sequence.isEmpty()) { | ||
167 | QHash<MnemonicAttached *, QKeySequence>::iterator objIt = s_objectToSequence.find(this); | 162 | s_sequenceToObject.remove(m_sequence); | ||
168 | if (objIt != s_objectToSequence.end()) { | 163 | m_sequence = {}; | ||
169 | s_sequenceToObject.remove(objIt.value()); | | |||
170 | s_objectToSequence.erase(objIt); | | |||
171 | } | 164 | } | ||
172 | 165 | | |||
173 | calculateWeights(); | 166 | calculateWeights(); | ||
174 | 167 | | |||
175 | const QString text = label(); | 168 | const QString text = label(); | ||
176 | 169 | | |||
177 | if (!m_enabled) { | 170 | if (!m_enabled) { | ||
178 | m_actualRichTextLabel = text; | 171 | m_actualRichTextLabel = text; | ||
Show All 17 Lines | 187 | do { | |||
196 | QChar c = i.value(); | 189 | QChar c = i.value(); | ||
197 | QKeySequence ks(QStringLiteral("Alt+") % c); | 190 | QKeySequence ks(QStringLiteral("Alt+") % c); | ||
198 | MnemonicAttached *otherMa = s_sequenceToObject.value(ks); | 191 | MnemonicAttached *otherMa = s_sequenceToObject.value(ks); | ||
199 | Q_ASSERT(otherMa != this); | 192 | Q_ASSERT(otherMa != this); | ||
200 | if (!otherMa || otherMa->m_weight < m_weight) { | 193 | if (!otherMa || otherMa->m_weight < m_weight) { | ||
201 | //the old shortcut is less valuable than the current: remove it | 194 | //the old shortcut is less valuable than the current: remove it | ||
202 | if (otherMa) { | 195 | if (otherMa) { | ||
203 | s_sequenceToObject.remove(otherMa->sequence()); | 196 | s_sequenceToObject.remove(otherMa->sequence()); | ||
204 | s_objectToSequence.remove(otherMa); | 197 | otherMa->m_sequence = {}; | ||
205 | } | 198 | } | ||
206 | 199 | | |||
207 | s_sequenceToObject[ks] = this; | 200 | s_sequenceToObject[ks] = this; | ||
208 | s_objectToSequence[this] = ks; | 201 | m_sequence = ks; | ||
209 | m_richTextLabel = text; | 202 | m_richTextLabel = text; | ||
210 | m_richTextLabel.replace(QRegularExpression(QLatin1String("\\&([^\\&])")), QStringLiteral("\\1")); | 203 | m_richTextLabel.replace(QRegularExpression(QLatin1String("\\&([^\\&])")), QStringLiteral("\\1")); | ||
211 | m_actualRichTextLabel = m_richTextLabel; | 204 | m_actualRichTextLabel = m_richTextLabel; | ||
212 | m_mnemonicLabel = m_richTextLabel; | 205 | m_mnemonicLabel = m_richTextLabel; | ||
213 | m_mnemonicLabel.replace(c, QLatin1String("&") % c); | 206 | m_mnemonicLabel.replace(c, QLatin1String("&") % c); | ||
214 | m_richTextLabel.replace(QString(c), QLatin1String("<u>") % c % QLatin1String("</u>")); | 207 | m_richTextLabel.replace(QString(c), QLatin1String("<u>") % c % QLatin1String("</u>")); | ||
215 | 208 | | |||
216 | //remap the sequence of the previous shortcut | 209 | //remap the sequence of the previous shortcut | ||
217 | if (otherMa) { | 210 | if (otherMa) { | ||
218 | otherMa->updateSequence(); | 211 | otherMa->updateSequence(); | ||
219 | } | 212 | } | ||
220 | 213 | | |||
221 | break; | 214 | break; | ||
222 | } | 215 | } | ||
223 | } while (i != m_weights.constBegin()); | 216 | } while (i != m_weights.constBegin()); | ||
224 | 217 | | |||
225 | if (s_objectToSequence.contains(this)) { | 218 | if (!m_sequence.isEmpty()) { | ||
226 | emit sequenceChanged(); | 219 | emit sequenceChanged(); | ||
227 | } else { | 220 | } else { | ||
228 | m_actualRichTextLabel = text; | 221 | m_actualRichTextLabel = text; | ||
229 | m_actualRichTextLabel.replace(QRegularExpression(QStringLiteral("\\&([^\\&])")), QStringLiteral("\\1")); | 222 | m_actualRichTextLabel.replace(QRegularExpression(QStringLiteral("\\&([^\\&])")), QStringLiteral("\\1")); | ||
230 | m_mnemonicLabel = m_actualRichTextLabel; | 223 | m_mnemonicLabel = m_actualRichTextLabel; | ||
231 | } | 224 | } | ||
232 | 225 | | |||
233 | emit richTextLabelChanged(); | 226 | emit richTextLabelChanged(); | ||
▲ Show 20 Lines • Show All 78 Lines • ▼ Show 20 Line(s) | |||||
312 | 305 | | |||
313 | MnemonicAttached::ControlType MnemonicAttached::controlType() const | 306 | MnemonicAttached::ControlType MnemonicAttached::controlType() const | ||
314 | { | 307 | { | ||
315 | return m_controlType; | 308 | return m_controlType; | ||
316 | } | 309 | } | ||
317 | 310 | | |||
318 | QKeySequence MnemonicAttached::sequence() | 311 | QKeySequence MnemonicAttached::sequence() | ||
319 | { | 312 | { | ||
320 | return s_objectToSequence.value(this); | 313 | return m_sequence; | ||
321 | } | 314 | } | ||
322 | 315 | | |||
323 | MnemonicAttached *MnemonicAttached::qmlAttachedProperties(QObject *object) | 316 | MnemonicAttached *MnemonicAttached::qmlAttachedProperties(QObject *object) | ||
324 | { | 317 | { | ||
325 | return new MnemonicAttached(object); | 318 | return new MnemonicAttached(object); | ||
326 | } | 319 | } | ||
327 | 320 | | |||
328 | #include "moc_mnemonicattached.cpp" | 321 | #include "moc_mnemonicattached.cpp" |