Changeset View
Changeset View
Standalone View
Standalone View
src/mnemonicattached.h
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * Copyright (C) 2017 by Marco Martin <mart@kde.org> | ||||
3 | * | ||||
4 | * This program is free software; you can redistribute it and/or modify | ||||
5 | * it under the terms of the GNU Library General Public License as | ||||
6 | * published by the Free Software Foundation; either version 2, or | ||||
7 | * (at your option) any later version. | ||||
8 | * | ||||
9 | * This program is distributed in the hope that it will be useful, | ||||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
12 | * GNU Library General Public License for more details | ||||
13 | * | ||||
14 | * You should have received a copy of the GNU Library General Public | ||||
15 | * License along with this program; if not, write to the | ||||
16 | * Free Software Foundation, Inc., | ||||
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||||
18 | */ | ||||
19 | | ||||
20 | #ifndef MNEMONICATTACHED_H | ||||
21 | #define MNEMONICATTACHED_H | ||||
22 | | ||||
23 | #include <QtQml> | ||||
24 | #include <QObject> | ||||
25 | #include <QQuickWindow> | ||||
26 | | ||||
27 | class QQuickItem; | ||||
28 | | ||||
29 | /** | ||||
30 | * This Attached property is used to calculate automated keyboard sequences | ||||
31 | * to trigger actions based upon their text: if an "&" mnemonic is | ||||
32 | * used (ie "&Ok"), the system will attempt to assign the desired letter giving | ||||
33 | * it priority, otherwise a letter among the ones in the label will be used if | ||||
34 | * possible and not conflicting. | ||||
35 | * Different kinds of controls will have different priorities in assigning the | ||||
36 | * shortcut: for instance the "Ok/Cancel" buttons in a dialog will have priority | ||||
37 | * over fields of a FormLayout. | ||||
38 | * @see ControlType | ||||
39 | * | ||||
broulik: Use `Q_ENUM` | |||||
40 | * Usually the developer shouldn't use this directly as base components | ||||
41 | * already use this, but only when implementing a custom graphical Control. | ||||
42 | * @since 2.3 | ||||
43 | */ | ||||
44 | class MnemonicAttached : public QObject | ||||
45 | { | ||||
46 | Q_OBJECT | ||||
47 | /** | ||||
48 | * The label of the control we want to compute a mnemonic for, instance | ||||
49 | * "Label:" or "&Ok" | ||||
broulik: `nullptr` | |||||
50 | */ | ||||
51 | Q_PROPERTY(QString label READ label WRITE setLabel NOTIFY labelChanged) | ||||
52 | | ||||
53 | /** | ||||
54 | * The user-visible final label, which will have the shortcut letter underlined, | ||||
55 | * such as "<u>O</u>k" | ||||
56 | */ | ||||
57 | Q_PROPERTY(QString richTextLabel READ richTextLabel NOTIFY richTextLabelChanged) | ||||
58 | | ||||
59 | /** | ||||
60 | * The label with an "&" mnemonic in the place which will have the shortcut | ||||
61 | * assigned, regardless the & wasassigned by the user or automatically generated. | ||||
62 | */ | ||||
63 | Q_PROPERTY(QString mnemonicLabel READ mnemonicLabel NOTIFY mnemonicLabelChanged) | ||||
64 | | ||||
65 | /** | ||||
66 | * Only if true this mnemonic will be considered for the global assignment | ||||
67 | * default: true | ||||
68 | */ | ||||
69 | Q_PROPERTY(bool enabled READ enabled WRITE setEnabled NOTIFY enabledChanged) | ||||
70 | | ||||
71 | /** | ||||
72 | * the type of control this mnemonic is attached: different types of controls have different importance and priority for shortcut assignment. | ||||
73 | * @see ControlType | ||||
74 | */ | ||||
75 | Q_PROPERTY(MnemonicAttached::ControlType controlType READ controlType WRITE setControlType NOTIFY controlTypeChanged) | ||||
76 | | ||||
77 | /** | ||||
78 | * The final key sequence assigned, if any: it will be Alt+alphanumeric char | ||||
79 | */ | ||||
80 | Q_PROPERTY(QKeySequence sequence READ sequence NOTIFY sequenceChanged) | ||||
81 | | ||||
82 | public: | ||||
83 | enum ControlType { | ||||
84 | ActionElement, /** pushbuttons, checkboxes etc */ | ||||
85 | DialogButton, /** buttons for dialogs */ | ||||
86 | MenuItem, /** Menu items */ | ||||
87 | FormLabel, /** Buddy label in a FormLayout*/ | ||||
88 | SecondaryControl /** Other controls that are considered not much important and low priority for shortcuts */ | ||||
89 | }; | ||||
90 | Q_ENUM(ControlType) | ||||
91 | | ||||
92 | explicit MnemonicAttached(QObject *parent = 0); | ||||
93 | ~MnemonicAttached(); | ||||
94 | | ||||
95 | void setLabel(const QString &text); | ||||
96 | QString label() const; | ||||
97 | | ||||
98 | QString richTextLabel() const; | ||||
99 | QString mnemonicLabel() const; | ||||
100 | | ||||
101 | void setEnabled(bool enabled); | ||||
102 | bool enabled() const; | ||||
103 | | ||||
104 | void setControlType(MnemonicAttached::ControlType controlType); | ||||
105 | ControlType controlType() const; | ||||
106 | | ||||
107 | QKeySequence sequence(); | ||||
108 | | ||||
109 | //QML attached property | ||||
110 | static MnemonicAttached *qmlAttachedProperties(QObject *object); | ||||
111 | | ||||
112 | protected: | ||||
113 | bool eventFilter(QObject *watched, QEvent *e); | ||||
114 | void updateSequence(); | ||||
115 | | ||||
116 | Q_SIGNALS: | ||||
117 | void labelChanged(); | ||||
118 | void enabledChanged(); | ||||
119 | void sequenceChanged(); | ||||
120 | void richTextLabelChanged(); | ||||
121 | void mnemonicLabelChanged(); | ||||
122 | void controlTypeChanged(); | ||||
123 | | ||||
124 | private: | ||||
125 | void calculateWeights(); | ||||
126 | | ||||
127 | //TODO: to have support for DIALOG_BUTTON_EXTRA_WEIGHT etc, a type enum should be exported | ||||
128 | enum { | ||||
129 | // Additional weight for first character in string | ||||
130 | FIRST_CHARACTER_EXTRA_WEIGHT = 50, | ||||
131 | // Additional weight for the beginning of a word | ||||
132 | WORD_BEGINNING_EXTRA_WEIGHT = 50, | ||||
133 | // Additional weight for a 'wanted' accelerator ie string with '&' | ||||
134 | WANTED_ACCEL_EXTRA_WEIGHT = 150, | ||||
135 | // Default weight for an 'action' widget (ie, pushbuttons) | ||||
136 | ACTION_ELEMENT_WEIGHT = 50, | ||||
137 | // Additional weight for the dialog buttons (large, we basically never want these reassigned) | ||||
138 | DIALOG_BUTTON_EXTRA_WEIGHT = 300, | ||||
139 | // Weight for FormLayout labels (low) | ||||
140 | FORM_LABEL_WEIGHT = 20, | ||||
141 | // Weight for Secondary controls which are considered less important (low) | ||||
142 | SECONDARY_CONTROL_WEIGHT = 10, | ||||
143 | // Default weight for menu items | ||||
144 | MENU_ITEM_WEIGHT = 250 | ||||
145 | }; | ||||
146 | | ||||
147 | //order word letters by weight | ||||
148 | int m_weight = 0; | ||||
149 | int m_baseWeight = 0; | ||||
150 | ControlType m_controlType = SecondaryControl; | ||||
151 | QMap<int, QChar> m_weights; | ||||
152 | | ||||
153 | QString m_label; | ||||
154 | QString m_actualRichTextLabel; | ||||
155 | QString m_richTextLabel; | ||||
156 | QString m_mnemonicLabel; | ||||
157 | bool m_enabled = true; | ||||
158 | | ||||
159 | QPointer<QQuickWindow> m_window; | ||||
160 | | ||||
161 | //global mapping of mnemonics | ||||
162 | //TODO: map by QWindow | ||||
163 | static QHash<QKeySequence, MnemonicAttached *> s_sequenceToObject; | ||||
This can go with D8827, right? hein: This can go with D8827, right? | |||||
no, mnemonics support is here, D8827 is the part in qqc2-desktop-style, another repo mart: no, mnemonics support is here, D8827 is the part in qqc2-desktop-style, another repo | |||||
hein: Ah, ok, I guess I thought window-level stuff might happen there. | |||||
yeah, i plan to add another granularity level to separe mnemonics per-window but since adds more complexity, i agreed beforehand to not make the diff grow more, so i'll push this beforehand and then give it the per-window support mart: yeah, i plan to add another granularity level to separe mnemonics per-window but since adds… | |||||
164 | static QHash<MnemonicAttached *, QKeySequence> s_objectToSequence; | ||||
165 | }; | ||||
166 | | ||||
167 | QML_DECLARE_TYPEINFO(MnemonicAttached, QML_HAS_ATTACHED_PROPERTIES) | ||||
168 | | ||||
169 | #endif // MnemonicATTACHED_H |
Use Q_ENUM