BuildmLearn Toolkit  2.0.4
BuildmLearn Toolkit is an easy-to-use program that helps users make mobile apps without any knowledge of application development.
 All Classes Functions Enumerations Groups Pages
shortcutbutton.cpp
1 /******************************************************************************
2 Copyright (c) 2010, Artem Galichkin <doomer3d@gmail.com>
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7 
8  * Redistributions of source code must retain the above copyright
9  notice, this list of conditions and the following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright
11  notice, this list of conditions and the following disclaimer in the
12  documentation and/or other materials provided with the distribution.
13  * Neither the name of the <organization> nor the
14  names of its contributors may be used to endorse or promote products
15  derived from this software without specific prior written permission.
16 
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
21 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *******************************************************************************/
28 
29 #include "dynamic-shortcuts/shortcutbutton.h"
30 
31 #include "dynamic-shortcuts/shortcutcatcher.h"
32 
33 #include <QKeyEvent>
34 
35 
37  : QPushButton(parent), m_catcher(catcher) {
38 }
39 
40 ShortcutButton::~ShortcutButton() {
41 }
42 
43 void ShortcutButton::keyPressEvent(QKeyEvent *event) {
44  int pressed_key = event->key();
45 
46  if (pressed_key == -1) {
47  m_catcher->doneRecording();
48  }
49 
50  Qt::KeyboardModifiers new_modifiers = event->modifiers() &
51  (Qt::SHIFT | Qt::CTRL | Qt::ALT | Qt::META);
52 
53  if (!m_catcher->m_isRecording && (pressed_key == Qt::Key_Return || pressed_key == Qt::Key_Space)) {
54  return;
55  }
56 
57  if (!m_catcher->m_isRecording) {
58  return QPushButton::keyPressEvent(event);
59  }
60 
61  event->accept();
62  m_catcher->m_modifierKeys = new_modifiers;
63 
64  switch(pressed_key) {
65  case Qt::Key_AltGr:
66  return;
67  case Qt::Key_Shift:
68  case Qt::Key_Control:
69  case Qt::Key_Alt:
70  case Qt::Key_Meta:
71  case Qt::Key_Menu:
72  m_catcher->controlModifierlessTimeout();
73  m_catcher->updateDisplayShortcut();
74  break;
75  default: {
76  }
77 
78  // We now have a valid key press.
79  if (pressed_key) {
80  if ((pressed_key == Qt::Key_Backtab) && (m_catcher->m_modifierKeys & Qt::SHIFT)) {
81  pressed_key = Qt::Key_Tab | m_catcher->m_modifierKeys;
82  }
83  else {
84  pressed_key |= m_catcher->m_modifierKeys;
85  }
86 
87  if (m_catcher->m_numKey == 0) {
88  m_catcher->m_currentSequence = QKeySequence(pressed_key);
89  }
90 
91  m_catcher->m_numKey++;
92 
93  if (m_catcher->m_numKey >= 4) {
94  m_catcher->doneRecording();
95  return;
96  }
97 
98  m_catcher->controlModifierlessTimeout();
99  m_catcher->updateDisplayShortcut();
100  }
101  }
102 }
103 
104 void ShortcutButton::keyReleaseEvent(QKeyEvent *event) {
105  if (event->key() == -1){
106  return;
107  }
108 
109  if (!m_catcher->m_isRecording) {
110  return QPushButton::keyReleaseEvent(event);
111  }
112 
113  event->accept();
114 
115  Qt::KeyboardModifiers new_modifiers = event->modifiers() &
116  (Qt::SHIFT | Qt::CTRL | Qt::ALT | Qt::META);
117 
118  if (((uint) new_modifiers & m_catcher->m_modifierKeys) < m_catcher->m_modifierKeys) {
119  m_catcher->m_modifierKeys = new_modifiers;
120  m_catcher->controlModifierlessTimeout();
121  m_catcher->updateDisplayShortcut();
122  }
123 }
void updateDisplayShortcut()
Updates text displayed in catcher according to active shortcut.
ShortcutButton(ShortcutCatcher *catcher, QWidget *parent=0)
Constructor.
Represents extra widget for changing single keyboard shortcut.
void controlModifierlessTimeout()
Performs time-dependent check of current shortcut check and stops the recording if time is up...