MythTV  master
keygrabber.cpp
Go to the documentation of this file.
1 // -*- Mode: c++ -*-
2 // Qt headers
3 #include <QKeyEvent>
4 #include <QString>
5 
6 // MythTV headers
7 #include "mythlogging.h"
8 
9 // MythUI headers
10 #include "mythuitext.h"
11 #include "mythuibutton.h"
12 
13 // MythControls headers
14 #include "keygrabber.h"
15 
17 {
18  // Load the theme for this screen
19  bool foundtheme = LoadWindowFromXML("controls-ui.xml", "keygrabpopup", this);
20  if (!foundtheme)
21  return false;
22 
23  m_messageText = dynamic_cast<MythUIText *> (GetChild("message"));
24  m_okButton = dynamic_cast<MythUIButton *> (GetChild("ok"));
25  m_cancelButton = dynamic_cast<MythUIButton *> (GetChild("cancel"));
26 
28  {
29  LOG(VB_GENERAL, LOG_ERR, "Theme is missing critical elements.");
30  return false;
31  }
32 
33  QString label = QString("%1\n\n%2").arg(tr("Press A Key"))
34  .arg(tr("Waiting for key press"));
35 
36  m_messageText->SetText(label);
37 
38  connect(m_okButton, SIGNAL(Clicked()), SLOT(SendResult()));
39  connect(m_cancelButton, SIGNAL(Clicked()), SLOT(Close()));
40 
41  m_okButton->SetEnabled(false);
42  m_cancelButton->SetEnabled(false);
43 
45 
47 
48  return true;
49 }
50 
51 bool KeyGrabPopupBox::keyPressEvent(QKeyEvent *event)
52 {
53  // If no capturing has occurred yet, then start waiting for key release
55 
56  bool handled = false;
57 
59  {
60  if (GetFocusWidget()->keyPressEvent(event))
61  handled = true;
62  }
63  else
64  {
65  int keycode = event->key();
66 
67  // Modifier keypress, ignore until we see the complete combo
68  if ((keycode == Qt::Key_Shift ) || (keycode == Qt::Key_Control) ||
69  (keycode == Qt::Key_Meta ) || (keycode == Qt::Key_Alt ) ||
70  (keycode == Qt::Key_Super_L ) || (keycode == Qt::Key_Super_R) ||
71  (keycode == Qt::Key_Hyper_L ) || (keycode == Qt::Key_Hyper_R) ||
72  (keycode == Qt::Key_AltGr ))
73  return true;
74 
75  m_waitingForKeyRelease = false;
76  m_keyReleaseSeen = true;
77 
78  QString key_name = QKeySequence(keycode).toString();
79  if (!key_name.isEmpty())
80  {
81  QString modifiers;
82 
83  /* key modifier strings as defined by the QT docs */
84  if (((event->modifiers() & Qt::ShiftModifier) != 0U)
85  && keycode > 0x7f
86  && keycode != Qt::Key_Backtab)
87  modifiers += "Shift+";
88  if ((event->modifiers() & Qt::ControlModifier) != 0U)
89  modifiers += "Ctrl+";
90  if ((event->modifiers() & Qt::AltModifier) != 0U)
91  modifiers += "Alt+";
92  if ((event->modifiers() & Qt::MetaModifier) != 0U)
93  modifiers += "Meta+";
94 
95  key_name = modifiers + key_name;
96  }
97 
98  if (key_name.isEmpty())
99  {
100  m_messageText->SetText(tr("Pressed key not recognized"));
101  LOG(VB_GENERAL, LOG_ERR, QString("Pressed key not recognized: '%1' (keycode %2)").arg(event->text()).arg(event->nativeScanCode()) );
102  }
103  else
104  {
105  m_capturedKey = key_name;
106  m_messageText->SetText(tr("Add key '%1'?").arg(key_name));
107  }
108 
109  m_okButton->SetEnabled(true);
110  m_cancelButton->SetEnabled(true);
111 
112  handled = true;
113  }
114 
115  if (!handled && MythScreenType::keyPressEvent(event))
116  handled = true;
117 
118  return handled;
119 }
120 
122 {
124  Close();
125 }
bool m_waitingForKeyRelease
Definition: keygrabber.h:34
QString m_capturedKey
Definition: keygrabber.h:36
void SetEnabled(bool enable)
bool m_keyReleaseSeen
Definition: keygrabber.h:35
bool Create(void) override
Definition: keygrabber.cpp:16
virtual void SetText(const QString &text)
Definition: mythuitext.cpp:135
MythUIButton * m_cancelButton
Definition: keygrabber.h:40
MythUIButton * m_okButton
Definition: keygrabber.h:39
void BuildFocusList(void)
virtual void Close()
bool keyPressEvent(QKeyEvent *) override
Key event handler.
Definition: keygrabber.cpp:51
static bool LoadWindowFromXML(const QString &xmlfile, const QString &windowname, MythUIType *parent)
MythUIType * GetFocusWidget(void) const
bool keyPressEvent(QKeyEvent *) override
Key event handler.
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: mythlogging.h:41
MythUIText * m_messageText
Definition: keygrabber.h:38
bool SetFocusWidget(MythUIType *widget=nullptr)
MythUIType * GetChild(const QString &name) const
Get a named child of this UIType.
Definition: mythuitype.cpp:132
void HaveResult(QString)