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