@@ -72,8 +72,11 @@ class ShortcutItem final : public QTreeWidgetItem {
72
72
// ShortcutViewer
73
73
// -----------------------------------------------------------------------------
74
74
75
- ShortcutViewer::ShortcutViewer (QWidget *parent) : QWidget(parent), m_action(0 ) {
75
+ ShortcutViewer::ShortcutViewer (QWidget *parent)
76
+ : QKeySequenceEdit(parent), m_action(0 ) {
77
+ setObjectName (" ShortcutViewer" );
76
78
setSizePolicy (QSizePolicy::Expanding, QSizePolicy::Expanding);
79
+ connect (this , SIGNAL (editingFinished ()), this , SLOT (onEditingFinished ()));
77
80
}
78
81
79
82
// -----------------------------------------------------------------------------
@@ -82,95 +85,64 @@ ShortcutViewer::~ShortcutViewer() {}
82
85
83
86
// -----------------------------------------------------------------------------
84
87
85
- void ShortcutViewer::paintEvent (QPaintEvent *) {
86
- QPainter p (this );
87
- // sfondo azzurro se il widget ha il focus (e quindi si accorge dei tasti
88
- // premuti)
89
- p.fillRect (1 , 1 , width () - 1 , height () - 1 ,
90
- QBrush (hasFocus () ? QColor (171 , 206 , 255 ) : Qt::white));
91
- // bordo
92
- p.setPen (QColor (184 , 188 , 127 ));
93
- p.drawRect (0 , 0 , width () - 1 , height () - 1 );
94
- if (m_action) {
95
- // lo shortcut corrente
96
- p.setPen (Qt::black);
97
- p.drawText (10 , 13 , m_action->shortcut ().toString ());
98
- }
99
- }
100
-
101
- // -----------------------------------------------------------------------------
102
-
103
88
void ShortcutViewer::setAction (QAction *action) {
104
89
m_action = action;
105
- update ( );
90
+ setKeySequence (m_action-> shortcut () );
106
91
setFocus ();
107
92
}
108
93
109
94
// -----------------------------------------------------------------------------
110
95
111
- bool ShortcutViewer::event (QEvent *event) {
112
- // quando si vuole assegnare una combinazione che gia' assegnata bisogna
113
- // evitare che lo shortcut relativo agisca.
114
- if (event->type () == QEvent::ShortcutOverride) {
115
- event->accept ();
116
- return true ;
117
- } else
118
- return QWidget::event (event);
119
- }
120
-
121
- // -----------------------------------------------------------------------------
122
-
123
96
void ShortcutViewer::keyPressEvent (QKeyEvent *event) {
124
- int key = event->key ();
125
- if (key == Qt::Key_Control || key == Qt::Key_Shift || key == Qt::Key_Alt) {
126
- event->ignore ();
127
- return ;
128
- }
97
+ int key = event->key ();
129
98
Qt::KeyboardModifiers modifiers = event->modifiers ();
130
-
131
- // Tasti che non possono essere utilizzati come shortcut
132
- if ((modifiers | (Qt::CTRL | Qt::SHIFT | Qt::ALT)) !=
133
- (Qt::CTRL | Qt::SHIFT | Qt::ALT) ||
134
- key == Qt::Key_Home || key == Qt::Key_End || key == Qt::Key_PageDown ||
99
+ if (key == Qt::Key_Home || key == Qt::Key_End || key == Qt::Key_PageDown ||
135
100
key == Qt::Key_PageUp || key == Qt::Key_Escape || key == Qt::Key_Print ||
136
101
key == Qt::Key_Pause || key == Qt::Key_ScrollLock) {
137
- if (key != Qt::Key_Plus && key != Qt::Key_Minus &&
138
- key != Qt::Key_Asterisk && key != Qt::Key_Slash) {
139
- event->ignore ();
140
- return ;
141
- } else
142
- modifiers = 0 ;
102
+ event->ignore ();
103
+ return ;
143
104
}
144
-
145
105
// If "Use Numpad and Tab keys for Switching Styles" option is activated,
146
106
// then prevent to assign such keys
147
107
if (Preferences::instance ()->isUseNumpadForSwitchingStylesEnabled () &&
148
108
modifiers == 0 && (key >= Qt::Key_0 && key <= Qt::Key_9)) {
149
109
event->ignore ();
150
110
return ;
151
111
}
112
+ QKeySequenceEdit::keyPressEvent (event);
113
+ }
114
+
115
+ // -----------------------------------------------------------------------------
116
+
117
+ void ShortcutViewer::onEditingFinished () {
118
+ // limit to one shortcut key input
119
+ QKeySequence keys = (keySequence ().isEmpty ())
120
+ ? QKeySequence ()
121
+ : QKeySequence (keySequence ()[0 ]);
152
122
153
123
if (m_action) {
154
- CommandManager *cm = CommandManager::instance ();
155
- QKeySequence keySequence (key + modifiers);
156
- std::string shortcutString = keySequence.toString ().toStdString ();
124
+ CommandManager *cm = CommandManager::instance ();
125
+ std::string shortcutString = keys.toString ().toStdString ();
157
126
QAction *oldAction =
158
- cm->getActionFromShortcut (keySequence .toString ().toStdString ());
127
+ cm->getActionFromShortcut (keys .toString ().toStdString ());
159
128
if (oldAction == m_action) return ;
160
129
if (oldAction) {
161
130
QString msg = tr (" %1 is already assigned to '%2'\n Assign to '%3'?" )
162
- .arg (keySequence .toString ())
131
+ .arg (keys .toString ())
163
132
.arg (oldAction->iconText ())
164
133
.arg (m_action->iconText ());
165
134
int ret = DVGui::MsgBox (msg, tr (" Yes" ), tr (" No" ), 1 );
166
135
activateWindow ();
167
- if (ret == 2 || ret == 0 ) return ;
136
+ if (ret == 2 || ret == 0 ) {
137
+ setKeySequence (m_action->shortcut ());
138
+ setFocus ();
139
+ return ;
140
+ }
168
141
}
169
142
CommandManager::instance ()->setShortcut (m_action, shortcutString);
170
143
emit shortcutChanged ();
171
144
}
172
- event->accept ();
173
- update ();
145
+ setKeySequence (keys);
174
146
}
175
147
176
148
// -----------------------------------------------------------------------------
@@ -179,7 +151,7 @@ void ShortcutViewer::removeShortcut() {
179
151
if (m_action) {
180
152
CommandManager::instance ()->setShortcut (m_action, " " , false );
181
153
emit shortcutChanged ();
182
- update ();
154
+ clear ();
183
155
}
184
156
}
185
157
0 commit comments