00001
00025
00026 #include <qstring.h>
00027 #include <qdeepcopy.h>
00028
00029
00030 #include <mythtv/mythcontext.h>
00031
00032
00033 #include "keygrabber.h"
00034
00035 KeyGrabPopupBox::KeyGrabPopupBox(MythMainWindow *window)
00036 : MythPopupBox(window, "keygrabber"),
00037 m_waitingForKeyRelease(false), m_keyReleaseSeen(false),
00038 m_capturedKey(QString::null),
00039 m_ok(NULL), m_cancel(NULL), m_label(NULL)
00040 {
00041 addLabel(tr("Press A Key"), Large, false);
00042 m_label = addLabel(tr("Waiting for key press"), Small, false);
00043 m_ok = addButton(QObject::tr("OK"), this, SLOT(accept()));
00044 m_cancel = addButton(QObject::tr("Cancel"), this, SLOT(reject()));
00045
00046 grabKeyboard();
00047 }
00048
00049 KeyGrabPopupBox::~KeyGrabPopupBox()
00050 {
00051 Teardown();
00052 }
00053
00054 void KeyGrabPopupBox::deleteLater(void)
00055 {
00056 Teardown();
00057 MythPopupBox::deleteLater();
00058 }
00059
00060 void KeyGrabPopupBox::Teardown(void)
00061 {
00062 m_capturedKey = QString::null;
00063 m_ok = NULL;
00064 m_cancel = NULL;
00065 m_label = NULL;
00066 releaseKeyboard();
00067 }
00068
00071 QString KeyGrabPopupBox::GetCapturedKey(void) const
00072 {
00073 return QDeepCopy<QString>(m_capturedKey);
00074 }
00075
00076 void KeyGrabPopupBox::keyReleaseEvent(QKeyEvent *e)
00077 {
00078 if (!m_ok || !m_cancel || !m_label)
00079 return;
00080
00081 if (!m_waitingForKeyRelease)
00082 return;
00083
00084 m_waitingForKeyRelease = false;
00085 m_keyReleaseSeen = true;
00086
00087 QString key_name = QString(QKeySequence(e->key()));
00088 if (!key_name.isEmpty() && !key_name.isNull())
00089 {
00090 QString modifiers = "";
00091
00092
00093 if (e->state() & Qt::ShiftButton)
00094 modifiers += "Shift+";
00095 if (e->state() & Qt::ControlButton)
00096 modifiers += "Ctrl+";
00097 if (e->state() & Qt::AltButton)
00098 modifiers += "Alt+";
00099 if (e->state() & Qt::MetaButton)
00100 modifiers += "Meta+";
00101
00102 key_name = modifiers + key_name;
00103 }
00104
00105 if (key_name.isEmpty())
00106 {
00107 m_label->setText(tr("Pressed key not recognized"));
00108 m_ok->setDisabled(true);
00109 m_cancel->setFocus();
00110 }
00111 else
00112 {
00113 m_capturedKey = key_name;
00114 m_label->setText(tr("Add key '%1'?").arg(key_name));
00115 m_ok->setFocus();
00116 }
00117
00118 releaseKeyboard();
00119 }
00120
00121 void KeyGrabPopupBox::keyPressEvent(QKeyEvent *e)
00122 {
00123
00124 m_waitingForKeyRelease |= !m_keyReleaseSeen;
00125
00126 if (m_waitingForKeyRelease)
00127 {
00128 e->accept();
00129 return;
00130 }
00131
00132 MythPopupBox::keyPressEvent(e);
00133 }