Kirigami2

mnemonicattached.h
1/*
2 * SPDX-FileCopyrightText: 2017 Marco Martin <mart@kde.org>
3 *
4 * SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6
7#ifndef MNEMONICATTACHED_H
8#define MNEMONICATTACHED_H
9
10#include <QObject>
11#include <QQuickWindow>
12
13#include <QQmlEngine>
14
15/**
16 * This Attached property is used to calculate automated keyboard sequences
17 * to trigger actions based upon their text: if an "&" mnemonic is
18 * used (ie "&Ok"), the system will attempt to assign the desired letter giving
19 * it priority, otherwise a letter among the ones in the label will be used if
20 * possible and not conflicting.
21 * Different kinds of controls will have different priorities in assigning the
22 * shortcut: for instance the "Ok/Cancel" buttons in a dialog will have priority
23 * over fields of a FormLayout.
24 * @see ControlType
25 *
26 * Usually the developer shouldn't use this directly as base components
27 * already use this, but only when implementing a custom graphical Control.
28 * @since 2.3
29 */
30class MnemonicAttached : public QObject
31{
33 QML_NAMED_ELEMENT(MnemonicData)
34 QML_ATTACHED(MnemonicAttached)
35 QML_UNCREATABLE("Cannot create objects of type MnemonicData, use it as an attached property")
36 /**
37 * The label of the control we want to compute a mnemonic for, instance
38 * "Label:" or "&Ok"
39 */
40 Q_PROPERTY(QString label READ label WRITE setLabel NOTIFY labelChanged FINAL)
41
42 /**
43 * The user-visible final label, which will have the shortcut letter underlined,
44 * such as "&lt;u&gt;O&lt;/u&gt;k"
45 */
46 Q_PROPERTY(QString richTextLabel READ richTextLabel NOTIFY richTextLabelChanged FINAL)
47
48 /**
49 * The label with an "&" mnemonic in the place which will have the shortcut
50 * assigned, regardless of whether the & was assigned by the user or automatically generated.
51 */
52 Q_PROPERTY(QString mnemonicLabel READ mnemonicLabel NOTIFY mnemonicLabelChanged FINAL)
53
54 /**
55 * The label in plain text with no markup nor & markers.
56 */
57 Q_PROPERTY(QString plainTextLabel READ plainTextLabel NOTIFY plainTextLabelChanged FINAL)
58
59 /**
60 * Only if true this mnemonic will be considered for the global assignment
61 * default: true
62 */
63 Q_PROPERTY(bool enabled READ enabled WRITE setEnabled NOTIFY enabledChanged FINAL)
64
65 /**
66 * The type of control this mnemonic is attached: different types of controls have different importance and priority for shortcut assignment.
67 * @see ControlType
68 */
69 Q_PROPERTY(MnemonicAttached::ControlType controlType READ controlType WRITE setControlType NOTIFY controlTypeChanged FINAL)
70
71 /**
72 * The final key sequence assigned, if any: it will be Alt+alphanumeric char
73 */
74 Q_PROPERTY(QKeySequence sequence READ sequence NOTIFY sequenceChanged FINAL)
75
76 /**
77 * True when the user is pressing alt and the accelerators should be shown
78 *
79 * @since 5.72
80 * @since 2.15
81 */
82 Q_PROPERTY(bool active READ active WRITE setActive NOTIFY activeChanged FINAL)
83
84public:
86 ActionElement, /**< pushbuttons, checkboxes etc */
87 DialogButton, /**< buttons for dialogs */
88 MenuItem, /**< Menu items */
89 FormLabel, /**< Buddy label in a FormLayout*/
90 SecondaryControl, /**< Other controls that are considered not much important and low priority for shortcuts */
91 };
92 Q_ENUM(ControlType)
93
94 explicit MnemonicAttached(QObject *parent = nullptr);
95 ~MnemonicAttached() override;
96
97 void setLabel(const QString &text);
98 QString label() const;
99
100 QString richTextLabel() const;
101 QString mnemonicLabel() const;
102 QString plainTextLabel() const;
103
104 void setEnabled(bool enabled);
105 bool enabled() const;
106
107 void setControlType(MnemonicAttached::ControlType controlType);
108 ControlType controlType() const;
109
111
112 void setActive(bool active);
113 bool active() const;
114
115 // QML attached property
116 static MnemonicAttached *qmlAttachedProperties(QObject *object);
117
118protected:
119 void updateSequence();
120
122 void labelChanged();
123 void enabledChanged();
124 void sequenceChanged();
125 void richTextLabelChanged();
126 void mnemonicLabelChanged();
127 void plainTextLabelChanged();
128 void controlTypeChanged();
129 void activeChanged();
130
131private:
132 QWindow *window() const;
133
134 void onAltPressed();
135 void onAltReleased();
136
137 void calculateWeights();
138
139 // TODO: to have support for DIALOG_BUTTON_EXTRA_WEIGHT etc, a type enum should be exported
140 enum {
141 // Additional weight for first character in string
142 FIRST_CHARACTER_EXTRA_WEIGHT = 50,
143 // Additional weight for the beginning of a word
144 WORD_BEGINNING_EXTRA_WEIGHT = 50,
145 // Additional weight for a 'wanted' accelerator ie string with '&'
146 WANTED_ACCEL_EXTRA_WEIGHT = 150,
147 // Default weight for an 'action' widget (ie, pushbuttons)
148 ACTION_ELEMENT_WEIGHT = 50,
149 // Additional weight for the dialog buttons (large, we basically never want these reassigned)
150 DIALOG_BUTTON_EXTRA_WEIGHT = 300,
151 // Weight for FormLayout labels (low)
152 FORM_LABEL_WEIGHT = 20,
153 // Weight for Secondary controls which are considered less important (low)
154 SECONDARY_CONTROL_WEIGHT = 10,
155 // Default weight for menu items
156 MENU_ITEM_WEIGHT = 250,
157 };
158
159 // order word letters by weight
160 int m_weight = 0;
161 int m_baseWeight = 0;
162 ControlType m_controlType = SecondaryControl;
163 QMap<int, QChar> m_weights;
164
165 QString m_label;
166 QString m_actualRichTextLabel;
167 QString m_richTextLabel;
168 QString m_mnemonicLabel;
169 QKeySequence m_sequence;
170 bool m_enabled = true;
171 bool m_active = false;
172
173 QPointer<QQuickWindow> m_window;
174
175 // global mapping of mnemonics
176 // TODO: map by QWindow
177 static QHash<QKeySequence, MnemonicAttached *> s_sequenceToObject;
178};
179
180QML_DECLARE_TYPEINFO(MnemonicAttached, QML_HAS_ATTACHED_PROPERTIES)
181
182#endif // MnemonicATTACHED_H
This Attached property is used to calculate automated keyboard sequences to trigger actions based upo...
QString richTextLabel
The user-visible final label, which will have the shortcut letter underlined, such as "<u>O</u>k".
QString label
The label of the control we want to compute a mnemonic for, instance "Label:" or "&Ok".
@ MenuItem
Menu items.
@ SecondaryControl
Other controls that are considered not much important and low priority for shortcuts.
@ DialogButton
buttons for dialogs
@ ActionElement
pushbuttons, checkboxes etc
@ FormLabel
Buddy label in a FormLayout.
bool enabled
Only if true this mnemonic will be considered for the global assignment default: true.
bool active
True when the user is pressing alt and the accelerators should be shown.
QString mnemonicLabel
The label with an "&" mnemonic in the place which will have the shortcut assigned,...
MnemonicAttached::ControlType controlType
The type of control this mnemonic is attached: different types of controls have different importance ...
QString plainTextLabel
The label in plain text with no markup nor & markers.
QKeySequence sequence
The final key sequence assigned, if any: it will be Alt+alphanumeric char.
QObject(QObject *parent)
Q_ENUM(...)
Q_OBJECTQ_OBJECT
Q_PROPERTY(...)
Q_SIGNALSQ_SIGNALS
QObject * parent() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Feb 21 2025 11:47:53 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.