source: smplayer/trunk/src/shortcutgetter.cpp@ 170

Last change on this file since 170 was 165, checked in by Silvan Scherrer, 11 years ago

SMPlayer: update trunk to latest 0.8.7

  • Property svn:eol-style set to LF
File size: 10.7 KB
Line 
1/* smplayer, GUI front-end for mplayer.
2 Copyright (C) 2006-2014 Ricardo Villalba <rvm@users.sourceforge.net>
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, 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 General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17*/
18
19/*
20 Note: The ShortcutGetter class is taken from the source code of Edyuk
21 (http://www.edyuk.org/), from file 3rdparty/qcumber/qshortcutdialog.cpp
22
23 Copyright (C) 2006 FullMetalCoder
24 License: GPL
25
26 I've just made a little few changes on it.
27*/
28
29
30/****************************************************************************
31**
32** Copyright (C) 2006 FullMetalCoder
33**
34** This file is part of the Edyuk project (beta version)
35**
36** This file may be used under the terms of the GNU General Public License
37** version 2 as published by the Free Software Foundation and appearing in the
38** file GPL.txt included in the packaging of this file.
39**
40** Notes : Parts of the project are derivative work of Trolltech's QSA library
41** or Trolltech's Qt4 framework but, unless notified, every single line of code
42** is the work of the Edyuk team or a contributor.
43**
44** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
45** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
46**
47****************************************************************************/
48
49
50#include "shortcutgetter.h"
51
52#include <QLayout>
53#include <QHash>
54#include <QLabel>
55#include <QString>
56#include <QShortcut>
57#include <QLineEdit>
58#include <QKeyEvent>
59#include <QPushButton>
60#include <QDialogButtonBox>
61
62#if 1
63
64static QHash<int, const char*> keyMap;
65
66static void initKeyMap()
67{
68 if ( !keyMap.isEmpty() )
69 return;
70
71 /*
72 I'm a bit unsure about these one...
73 */
74 keyMap[Qt::Key_Escape] = "Escape";
75 keyMap[Qt::Key_Return] = "Return";
76 keyMap[Qt::Key_Enter] = "Enter";
77 keyMap[Qt::Key_Insert] = "Ins";
78 keyMap[Qt::Key_Delete] = "Delete";
79 keyMap[Qt::Key_Home] = "Home";
80 keyMap[Qt::Key_End] = "End";
81 keyMap[Qt::Key_Left] = "Left";
82 keyMap[Qt::Key_Up] = "Up";
83 keyMap[Qt::Key_Right] = "Right";
84 keyMap[Qt::Key_Down] = "Down";
85 keyMap[Qt::Key_PageUp] = "PgUp";
86 keyMap[Qt::Key_PageDown] = "PgDown";
87 keyMap[Qt::Key_CapsLock] = "CapsLock";
88 keyMap[Qt::Key_NumLock] = "NumLock";
89 keyMap[Qt::Key_ScrollLock] = "ScrollLock";
90
91 /*
92 These one are quite sure...
93 */
94 keyMap[Qt::Key_F1] = "F1";
95 keyMap[Qt::Key_F2] = "F2";
96 keyMap[Qt::Key_F3] = "F3";
97 keyMap[Qt::Key_F4] = "F4";
98 keyMap[Qt::Key_F5] = "F5";
99 keyMap[Qt::Key_F6] = "F6";
100 keyMap[Qt::Key_F7] = "F7";
101 keyMap[Qt::Key_F8] = "F8";
102 keyMap[Qt::Key_F9] = "F9";
103 keyMap[Qt::Key_F10] = "F10";
104 keyMap[Qt::Key_F11] = "F11";
105 keyMap[Qt::Key_F12] = "F12";
106 keyMap[Qt::Key_F13] = "F13";
107 keyMap[Qt::Key_F14] = "F14";
108 keyMap[Qt::Key_F15] = "F15";
109 keyMap[Qt::Key_F16] = "F16";
110 keyMap[Qt::Key_F17] = "F17";
111 keyMap[Qt::Key_F18] = "F18";
112 keyMap[Qt::Key_F19] = "F19";
113 keyMap[Qt::Key_F20] = "F20";
114 keyMap[Qt::Key_F21] = "F21";
115 keyMap[Qt::Key_F22] = "F22";
116 keyMap[Qt::Key_F23] = "F23";
117 keyMap[Qt::Key_F24] = "F24";
118 keyMap[Qt::Key_F25] = "F25";
119 keyMap[Qt::Key_F26] = "F26";
120 keyMap[Qt::Key_F27] = "F27";
121 keyMap[Qt::Key_F28] = "F28";
122 keyMap[Qt::Key_F29] = "F29";
123 keyMap[Qt::Key_F30] = "F30";
124 keyMap[Qt::Key_F31] = "F31";
125 keyMap[Qt::Key_F32] = "F32";
126 keyMap[Qt::Key_F33] = "F33";
127 keyMap[Qt::Key_F34] = "F34";
128 keyMap[Qt::Key_F35] = "F35";
129
130 keyMap[Qt::Key_Exclam] = "!";
131 keyMap[Qt::Key_QuoteDbl] = "\"";
132 keyMap[Qt::Key_NumberSign] = "-";
133 keyMap[Qt::Key_Dollar] = "$";
134 keyMap[Qt::Key_Percent] = "%";
135 keyMap[Qt::Key_Ampersand] = "&amp;";
136 keyMap[Qt::Key_Apostrophe] = "\'";
137 keyMap[Qt::Key_ParenLeft] = "(";
138 keyMap[Qt::Key_ParenRight] = ")";
139 keyMap[Qt::Key_Asterisk] = "*";
140 keyMap[Qt::Key_Plus] = "+";
141 keyMap[Qt::Key_Comma] = ",";
142 keyMap[Qt::Key_Minus] = "-";
143 keyMap[Qt::Key_Period] = "Period";
144 keyMap[Qt::Key_Slash] = "/";
145
146 keyMap[Qt::Key_0] = "0";
147 keyMap[Qt::Key_1] = "1";
148 keyMap[Qt::Key_2] = "2";
149 keyMap[Qt::Key_3] = "3";
150 keyMap[Qt::Key_4] = "4";
151 keyMap[Qt::Key_5] = "5";
152 keyMap[Qt::Key_6] = "6";
153 keyMap[Qt::Key_7] = "7";
154 keyMap[Qt::Key_8] = "8";
155 keyMap[Qt::Key_9] = "9";
156
157 keyMap[Qt::Key_Colon] = ":";
158 keyMap[Qt::Key_Semicolon] = ";";
159 keyMap[Qt::Key_Less] = "<";
160 keyMap[Qt::Key_Equal] = "=";
161 keyMap[Qt::Key_Greater] = ">";
162 keyMap[Qt::Key_Question] = "?";
163 keyMap[Qt::Key_At] = "@";
164
165 keyMap[Qt::Key_A] = "A";
166 keyMap[Qt::Key_B] = "B";
167 keyMap[Qt::Key_C] = "C";
168 keyMap[Qt::Key_D] = "D";
169 keyMap[Qt::Key_E] = "E";
170 keyMap[Qt::Key_F] = "F";
171 keyMap[Qt::Key_G] = "G";
172 keyMap[Qt::Key_H] = "H";
173 keyMap[Qt::Key_I] = "I";
174 keyMap[Qt::Key_J] = "J";
175 keyMap[Qt::Key_K] = "K";
176 keyMap[Qt::Key_L] = "L";
177 keyMap[Qt::Key_M] = "M";
178 keyMap[Qt::Key_N] = "N";
179 keyMap[Qt::Key_O] = "O";
180 keyMap[Qt::Key_P] = "P";
181 keyMap[Qt::Key_Q] = "Q";
182 keyMap[Qt::Key_R] = "R";
183 keyMap[Qt::Key_S] = "S";
184 keyMap[Qt::Key_T] = "T";
185 keyMap[Qt::Key_U] = "U";
186 keyMap[Qt::Key_V] = "V";
187 keyMap[Qt::Key_W] = "W";
188 keyMap[Qt::Key_X] = "X";
189 keyMap[Qt::Key_Y] = "Y";
190 keyMap[Qt::Key_Z] = "Z";
191
192 keyMap[Qt::Key_BracketLeft] = "[";
193 keyMap[Qt::Key_Backslash] = "\\";
194 keyMap[Qt::Key_BracketRight] = "]";
195
196 keyMap[Qt::Key_Underscore] = "_";
197 keyMap[Qt::Key_BraceLeft] = "{";
198 keyMap[Qt::Key_Bar] = "|";
199 keyMap[Qt::Key_BraceRight] = "}";
200 keyMap[Qt::Key_AsciiTilde] = "~";
201
202 // Added by rvm:
203 keyMap[Qt::Key_Space] = "Space";
204 keyMap[Qt::Key_Backspace] = "Backspace";
205 keyMap[Qt::Key_MediaPlay] = "Media Play";
206 keyMap[Qt::Key_MediaStop] = "Media Stop";
207 keyMap[Qt::Key_MediaPrevious] = "Media Previous";
208 keyMap[Qt::Key_MediaNext] = "Media Next";
209 keyMap[Qt::Key_MediaRecord] = "Media Record";
210 keyMap[Qt::Key_MediaLast] = "Media Last"; // doesn't work?
211 keyMap[Qt::Key_VolumeUp] = "Volume Up";
212 keyMap[Qt::Key_VolumeDown] = "Volume Down";
213 keyMap[Qt::Key_VolumeMute] = "Volume Mute";
214 keyMap[Qt::Key_Back] = "Back";
215 keyMap[Qt::Key_Forward] = "Forward";
216 keyMap[Qt::Key_Stop] = "Stop";
217}
218
219static QString keyToString(int k)
220{
221 if ( k == Qt::Key_Shift || k == Qt::Key_Control || k == Qt::Key_Meta ||
222 k == Qt::Key_Alt || k == Qt::Key_AltGr )
223 return QString::null;
224
225 initKeyMap();
226
227 return keyMap[k];
228}
229
230#else
231
232static QString keyToString(int k)
233{
234 if ( k == Qt::Key_Shift || k == Qt::Key_Control || k == Qt::Key_Meta ||
235 k == Qt::Key_Alt || k == Qt::Key_AltGr )
236 return QString::null;
237
238 return QKeySequence(k).toString();
239}
240
241#endif
242
243static QStringList modToString(Qt::KeyboardModifiers k)
244{
245 //qDebug("modToString: k: %x", (int) k);
246
247 QStringList l;
248
249 if ( k & Qt::ShiftModifier )
250 l << "Shift";
251 if ( k & Qt::ControlModifier )
252 l << "Ctrl";
253 if ( k & Qt::AltModifier )
254 l << "Alt";
255 if ( k & Qt::MetaModifier )
256 l << "Meta";
257 if ( k & Qt::GroupSwitchModifier )
258 ;
259 if ( k & Qt::KeypadModifier )
260 ;
261
262 return l;
263}
264
265
266ShortcutGetter::ShortcutGetter(QWidget *parent) : QDialog(parent)
267{
268 setWindowTitle(tr("Modify shortcut"));
269
270
271 QVBoxLayout *vbox = new QVBoxLayout(this);
272 vbox->setMargin(2);
273 vbox->setSpacing(4);
274
275 QLabel *l = new QLabel(this);
276 l->setText(tr("Press the key combination you want to assign"));
277 vbox->addWidget(l);
278
279 leKey = new QLineEdit(this);
280
281 leKey->installEventFilter(this);
282 vbox->addWidget(leKey);
283
284 // Change by rvm: use a QDialogButtonBox instead of QPushButtons
285 // and add a clear button
286 setCaptureKeyboard(true);
287 QDialogButtonBox * buttonbox = new QDialogButtonBox(QDialogButtonBox::Ok |
288 QDialogButtonBox::Cancel |
289 QDialogButtonBox::Reset );
290 QPushButton * clearbutton = buttonbox->button(QDialogButtonBox::Reset);
291 clearbutton->setText( tr("Clear") );
292
293 QPushButton * captureButton = new QPushButton(tr("Capture"), this);
294 captureButton->setToolTip( tr("Capture keystrokes") );
295 captureButton->setCheckable( captureKeyboard() );
296 captureButton->setChecked( captureKeyboard() );
297 connect(captureButton, SIGNAL(toggled(bool)),
298 this, SLOT(setCaptureKeyboard(bool)));
299
300
301 buttonbox->addButton(captureButton, QDialogButtonBox::ActionRole);
302
303 connect( buttonbox, SIGNAL(accepted()), this, SLOT(accept()) );
304 connect( buttonbox, SIGNAL(rejected()), this, SLOT(reject()) );
305 connect( clearbutton, SIGNAL(clicked()), leKey, SLOT(clear()) );
306 vbox->addWidget(buttonbox);
307}
308
309void ShortcutGetter::setCaptureKeyboard(bool b) {
310 capture = b;
311 leKey->setReadOnly(b);
312 leKey->setFocus();
313}
314
315
316QString ShortcutGetter::exec(const QString& s)
317{
318 bStop = false;
319 leKey->setText(s);
320
321 if ( QDialog::exec() == QDialog::Accepted )
322 return leKey->text();
323
324 return QString();
325}
326
327bool ShortcutGetter::event(QEvent *e)
328{
329 if (!capture) return QDialog::event(e);
330
331
332 QString key;
333 QStringList mods;
334 QKeyEvent *k = static_cast<QKeyEvent*>(e);
335
336 switch ( e->type() )
337 {
338 case QEvent::KeyPress :
339
340 if ( bStop )
341 {
342 lKeys.clear();
343 bStop = false;
344 }
345
346 key = keyToString(k->key());
347 mods = modToString(k->modifiers());
348
349 //qDebug("event: key.count: %d, mods.count: %d", key.count(), mods.count());
350
351 if ( key.count() || mods.count() )
352 {
353
354 if ( key.count() && !lKeys.contains(key) )
355 lKeys << key;
356
357 foreach ( key, mods )
358 if ( !lKeys.contains(key) )
359 lKeys << key;
360
361 } else {
362 key = k->text();
363
364 if ( !lKeys.contains(key) )
365 lKeys << key;
366 }
367
368 setText();
369 break;
370
371 case QEvent::KeyRelease :
372
373 bStop = true;
374 break;
375
376 /*
377 case QEvent::ShortcutOverride :
378 leKey->setText("Shortcut override");
379 break;
380 */
381
382 default:
383 return QDialog::event(e);
384 break;
385 }
386
387 return true;
388}
389
390bool ShortcutGetter::eventFilter(QObject *o, QEvent *e)
391{
392 if (!capture) return QDialog::eventFilter(o, e);
393
394 if ( e->type() == QEvent::KeyPress ||
395 e->type() ==QEvent::KeyRelease )
396 return event(e);
397 else
398 return QDialog::eventFilter(o, e);
399}
400
401void ShortcutGetter::setText()
402{
403 QStringList seq;
404
405 if ( lKeys.contains("Shift") )
406 seq << "Shift";
407
408 if ( lKeys.contains("Ctrl") )
409 seq << "Ctrl";
410
411 if ( lKeys.contains("Alt") )
412 seq << "Alt";
413
414 if ( lKeys.contains("Meta") )
415 seq << "Meta";
416
417 foreach ( QString s, lKeys ) {
418 //qDebug("setText: s: '%s'", s.toUtf8().data());
419 if ( s != "Shift" && s != "Ctrl"
420 && s != "Alt" && s != "Meta" )
421 seq << s;
422 }
423
424 leKey->setText(seq.join("+"));
425 //leKey->selectAll();
426}
427
428#include "moc_shortcutgetter.cpp"
Note: See TracBrowser for help on using the repository browser.