MythTV
master
programs
mythfrontend
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
16
bool
KeyGrabPopupBox::Create
(
void
)
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
27
if
(!
m_messageText
|| !
m_okButton
|| !
m_cancelButton
)
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
, &
MythUIButton::Clicked
,
this
, &
KeyGrabPopupBox::SendResult
);
39
connect(
m_cancelButton
, &
MythUIButton::Clicked
,
this
, &
MythScreenType::Close
);
40
41
m_okButton
->
SetEnabled
(
false
);
42
m_cancelButton
->
SetEnabled
(
false
);
43
44
BuildFocusList
();
45
46
SetFocusWidget
(
m_okButton
);
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
54
m_waitingForKeyRelease
|= !
m_keyReleaseSeen
;
55
56
bool
handled =
false
;
57
58
if
(!
m_waitingForKeyRelease
)
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
121
void
KeyGrabPopupBox::SendResult
()
122
{
123
emit
HaveResult
(
m_capturedKey
);
124
Close
();
125
}
MythUIButton::Clicked
void Clicked()
KeyGrabPopupBox::SendResult
void SendResult()
Definition:
keygrabber.cpp:121
KeyGrabPopupBox::keyPressEvent
bool keyPressEvent(QKeyEvent *event) override
Key event handler.
Definition:
keygrabber.cpp:51
mythuitext.h
MythScreenType::Close
virtual void Close()
Definition:
mythscreentype.cpp:402
MythUIType::GetChild
MythUIType * GetChild(const QString &name) const
Get a named child of this UIType.
Definition:
mythuitype.cpp:131
KeyGrabPopupBox::m_okButton
MythUIButton * m_okButton
Definition:
keygrabber.h:39
KeyGrabPopupBox::m_keyReleaseSeen
bool m_keyReleaseSeen
Definition:
keygrabber.h:35
arg
arg(title).arg(filename).arg(doDelete))
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition:
mythlogging.h:23
KeyGrabPopupBox::m_messageText
MythUIText * m_messageText
Definition:
keygrabber.h:38
MythScreenType::GetFocusWidget
MythUIType * GetFocusWidget(void) const
Definition:
mythscreentype.cpp:112
KeyGrabPopupBox::m_cancelButton
MythUIButton * m_cancelButton
Definition:
keygrabber.h:40
mythlogging.h
MythScreenType::SetFocusWidget
bool SetFocusWidget(MythUIType *widget=nullptr)
Definition:
mythscreentype.cpp:117
MythScreenType::BuildFocusList
void BuildFocusList(void)
Definition:
mythscreentype.cpp:222
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:1109
KeyGrabPopupBox::Create
bool Create(void) override
Definition:
keygrabber.cpp:16
MythUIText
All purpose text widget, displays a text string.
Definition:
mythuitext.h:30
MythScreenType::keyPressEvent
bool keyPressEvent(QKeyEvent *event) override
Key event handler.
Definition:
mythscreentype.cpp:414
XMLParseBase::LoadWindowFromXML
static bool LoadWindowFromXML(const QString &xmlfile, const QString &windowname, MythUIType *parent)
Definition:
xmlparsebase.cpp:692
MythUIText::SetText
virtual void SetText(const QString &text)
Definition:
mythuitext.cpp:133
mythuibutton.h
KeyGrabPopupBox::m_capturedKey
QString m_capturedKey
Definition:
keygrabber.h:36
keygrabber.h
Generated on Sat Jan 16 2021 03:18:12 for MythTV by
1.8.17