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
shortcutcatcher.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/shortcutcatcher.h"
30 
31 #include "dynamic-shortcuts/shortcutbutton.h"
32 #include "gui/plaintoolbutton.h"
33 #include "miscellaneous/iconfactory.h"
34 
35 #include <QHBoxLayout>
36 
37 
39  : QWidget(parent) {
40  // Setup layout of the control
41  m_layout = new QHBoxLayout(this);
42  m_layout->setMargin(0);
43  m_layout->setSpacing(1);
44 
45  // Create reset button.
46  m_btnReset = new PlainToolButton(this);
47  m_btnReset->setIcon(IconFactory::instance()->fromTheme("edit-revert"));
48  m_btnReset->setFocusPolicy(Qt::NoFocus);
49  m_btnReset->setToolTip(tr("Reset to original shortcut."));
50 
51  // Create clear button.
52  m_btnClear = new PlainToolButton(this);
53  m_btnClear->setIcon(IconFactory::instance()->fromTheme("item-remove"));
54  m_btnClear->setFocusPolicy(Qt::NoFocus);
55  m_btnClear->setToolTip(tr("Clear current shortcut."));
56 
57  // Clear main shortcut catching button.
58  m_btnChange = new ShortcutButton(this);
59  m_btnChange->setFocusPolicy(Qt::StrongFocus);
60  m_btnChange->setToolTip(tr("Click and hit new shortcut."));
61 
62  // Add both buttons to the layout.
63  m_layout->addWidget(m_btnChange);
64  m_layout->addWidget(m_btnReset);
65  m_layout->addWidget(m_btnClear);
66 
67  // Establish needed connections.
68  connect(m_btnReset, SIGNAL(clicked()), this, SLOT(resetShortcut()));
69  connect(m_btnClear, SIGNAL(clicked()), this, SLOT(clearShortcut()));
70  connect(m_btnChange, SIGNAL(clicked()), this, SLOT(startRecording()));
71 
72  // Prepare initial state of the control.
74 }
75 
76 ShortcutCatcher::~ShortcutCatcher() {
77  delete m_btnReset;
78  delete m_btnChange;
79  delete m_btnClear;
80  delete m_layout;
81 }
82 
83 void ShortcutCatcher::startRecording() {
84  m_numKey = 0;
85  m_modifierKeys = 0;
86  m_currentSequence = QKeySequence();
87  m_isRecording = true;
88  m_btnChange->setDown(true);
89  m_btnChange->grabKeyboard();
90 
92 }
93 
94 void ShortcutCatcher::doneRecording() {
95  m_isRecording = false;
96  m_btnChange->releaseKeyboard();
97  m_btnChange->setDown(false);
98 
100 
101  emit shortcutChanged(m_currentSequence);
102 }
103 
105  if (m_numKey && !m_modifierKeys) {
106  doneRecording();
107  }
108 }
109 
111  QString str = m_currentSequence.toString(QKeySequence::NativeText);
112  str.replace('&', QLatin1String("&&"));
113 
114  if (m_isRecording) {
115  if (m_modifierKeys) {
116  if (!str.isEmpty()) {
117  str.append(",");
118  }
119  if (m_modifierKeys & Qt::META) {
120  str += "Meta + ";
121  }
122  if (m_modifierKeys & Qt::CTRL) {
123  str += "Ctrl + ";
124  }
125  if (m_modifierKeys & Qt::ALT) {
126  str += "Alt + ";
127  }
128  if (m_modifierKeys & Qt::SHIFT) {
129  str += "Shift + ";
130  }
131  }
132  }
133 
134  m_btnChange->setText(str);
135 }
ShortcutCatcher(QWidget *parent=0)
Constructor.
void updateDisplayShortcut()
Updates text displayed in catcher according to active shortcut.
void resetShortcut()
Resets active shortcut to default shortcut.
Tool button without frame.
void clearShortcut()
Clears current active shortcut.
Represents single button for changing keyboard shortcut.
void controlModifierlessTimeout()
Performs time-dependent check of current shortcut check and stops the recording if time is up...
static IconFactory * instance()
Singleton getter.
Definition: iconfactory.cpp:48