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 | class MnemonicAttached : public QObject | ||||
30 | { | ||||
31 | Q_OBJECT | ||||
32 | Q_PROPERTY(QString label READ label WRITE setLabel NOTIFY labelChanged) | ||||
33 | Q_PROPERTY(QString richTextLabel READ richTextLabel NOTIFY richTextLabelChanged) | ||||
34 | Q_PROPERTY(QString mnemonicLabel READ mnemonicLabel NOTIFY mnemonicLabelChanged) | ||||
35 | Q_PROPERTY(bool enabled READ enabled WRITE setEnabled NOTIFY enabledChanged) | ||||
36 | Q_PROPERTY(MnemonicAttached::ControlType controlType READ controlType WRITE setControlType NOTIFY controlTypeChanged) | ||||
37 | Q_PROPERTY(QKeySequence sequence READ sequence NOTIFY sequenceChanged) | ||||
38 | | ||||
39 | public: | ||||
broulik: Use `Q_ENUM` | |||||
40 | enum ControlType { | ||||
41 | ActionElement, /** pushbuttons, checkboxes etc */ | ||||
42 | DialogButton, /** buttons for dialogs */ | ||||
43 | MenuItem, /** Menu items */ | ||||
44 | FormLabel, /** Buddy label in a FormLayout*/ | ||||
45 | SecondaryControl /** Other controls that are considered not much important and low priority for shortcuts */ | ||||
46 | }; | ||||
47 | Q_ENUM(ControlType) | ||||
48 | | ||||
49 | explicit MnemonicAttached(QObject *parent = 0); | ||||
broulik: `nullptr` | |||||
50 | ~MnemonicAttached(); | ||||
51 | | ||||
52 | void setLabel(const QString &text); | ||||
53 | QString label() const; | ||||
54 | | ||||
55 | QString richTextLabel() const; | ||||
56 | QString mnemonicLabel() const; | ||||
57 | | ||||
58 | void setEnabled(bool enabled); | ||||
59 | bool enabled() const; | ||||
60 | | ||||
61 | void setControlType(MnemonicAttached::ControlType controlType); | ||||
62 | ControlType controlType() const; | ||||
63 | | ||||
64 | QKeySequence sequence(); | ||||
65 | | ||||
66 | //QML attached property | ||||
67 | static MnemonicAttached *qmlAttachedProperties(QObject *object); | ||||
68 | | ||||
69 | protected: | ||||
70 | bool eventFilter(QObject *watched, QEvent *e); | ||||
71 | void updateSequence(); | ||||
72 | | ||||
73 | Q_SIGNALS: | ||||
74 | void labelChanged(); | ||||
75 | void enabledChanged(); | ||||
76 | void sequenceChanged(); | ||||
77 | void richTextLabelChanged(); | ||||
78 | void mnemonicLabelChanged(); | ||||
79 | void controlTypeChanged(); | ||||
80 | | ||||
81 | private: | ||||
82 | void calculateWeights(); | ||||
83 | | ||||
84 | //TODO: to have support for DIALOG_BUTTON_EXTRA_WEIGHT etc, a type enum should be exported | ||||
85 | enum { | ||||
86 | // Additional weight for first character in string | ||||
87 | FIRST_CHARACTER_EXTRA_WEIGHT = 50, | ||||
88 | // Additional weight for the beginning of a word | ||||
89 | WORD_BEGINNING_EXTRA_WEIGHT = 50, | ||||
90 | // Additional weight for a 'wanted' accelerator ie string with '&' | ||||
91 | WANTED_ACCEL_EXTRA_WEIGHT = 150, | ||||
92 | // Default weight for an 'action' widget (ie, pushbuttons) | ||||
93 | ACTION_ELEMENT_WEIGHT = 50, | ||||
94 | // Additional weight for the dialog buttons (large, we basically never want these reassigned) | ||||
95 | DIALOG_BUTTON_EXTRA_WEIGHT = 300, | ||||
96 | // Weight for FormLayout labels (low) | ||||
97 | FORM_LABEL_WEIGHT = 20, | ||||
98 | // Weight for Secondary controls which are considered less important (low) | ||||
99 | SECONDARY_CONTROL_WEIGHT = 10, | ||||
100 | // Default weight for menu items | ||||
101 | MENU_ITEM_WEIGHT = 250 | ||||
102 | }; | ||||
103 | | ||||
104 | //order word letters by weight | ||||
105 | int m_weight = 0; | ||||
106 | int m_baseWeight = 0; | ||||
107 | ControlType m_controlType = SecondaryControl; | ||||
108 | QMap<int, QChar> m_weights; | ||||
109 | | ||||
110 | QString m_label; | ||||
111 | QString m_actualRichTextLabel; | ||||
112 | QString m_richTextLabel; | ||||
113 | QString m_mnemonicLabel; | ||||
114 | bool m_enabled = true; | ||||
115 | | ||||
116 | QPointer<QQuickWindow> m_window; | ||||
117 | | ||||
118 | //global mapping of mnemonics | ||||
119 | //TODO: map by QWindow | ||||
120 | static QHash<QKeySequence, MnemonicAttached *> s_sequenceToObject; | ||||
121 | static QHash<MnemonicAttached *, QKeySequence> s_objectToSequence; | ||||
122 | }; | ||||
123 | | ||||
124 | QML_DECLARE_TYPEINFO(MnemonicAttached, QML_HAS_ATTACHED_PROPERTIES) | ||||
125 | | ||||
126 | #endif // MnemonicATTACHED_H | ||||
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… |
Use Q_ENUM