]> Shamusworld >> Repos - virtualjaguar/blob - src/gui/keygrabber.cpp
7efe537b1f9a0ecb8e0b12aab446b7a6b00aee3d
[virtualjaguar] / src / gui / keygrabber.cpp
1 //
2 // keygrabber.cpp - Widget to grab a key and dismiss itself
3 //
4 // by James Hammons
5 // (C) 2011 Underground Software
6 //
7 // JLH = James Hammons <jlhamm@acm.org>
8 //
9 // Who  When        What
10 // ---  ----------  -------------------------------------------------------------
11 // JLH  07/18/2011  Created this file
12 //
13
14 #include "keygrabber.h"
15 #include "gamepad.h"
16
17
18 KeyGrabber::KeyGrabber(QWidget * parent/*= 0*/): QDialog(parent),
19         label(new QLabel), timer(new QTimer), buttonDown(false)
20 {
21 //      label = new QLabel(this);
22         QVBoxLayout * mainLayout = new QVBoxLayout;
23         mainLayout->addWidget(label);
24         setLayout(mainLayout);
25         setWindowTitle(tr("Grab"));
26         connect(timer, SIGNAL(timeout()), this, SLOT(CheckGamepad()));
27         timer->setInterval(100);
28         timer->start();
29
30         // Will this make Mac OSX work???
31         setFocusPolicy(Qt::StrongFocus);
32 }
33
34
35 KeyGrabber::~KeyGrabber()
36 {
37         timer->stop();
38 }
39
40
41 void KeyGrabber::SetKeyText(int keyNum)
42 {
43         char jagButtonName[21][10] = { "Up", "Down", "Left", "Right",
44                 "*", "7", "4", "1", "0", "8", "5", "2", "#", "9", "6", "3",
45                 "A", "B", "C", "Option", "Pause" };
46
47         QString text = QString(tr("Press key for \"%1\"<br>(ESC to cancel)"))
48                 .arg(QString(jagButtonName[keyNum]));
49         label->setText(text);
50 }
51
52
53 void KeyGrabber::keyPressEvent(QKeyEvent * e)
54 {
55         key = e->key();
56
57         // Since this is problematic, we don't allow this key...
58         if (key != Qt::Key_Alt)
59                 accept();
60 }
61
62
63 void KeyGrabber::CheckGamepad(void)
64 {
65         // How do we determine which joystick it is, if more than one?
66         // Possibly by a combobox selecting the stick you want to configure...
67         Gamepad::Update();
68
69         if (!buttonDown)
70         {
71                 button = Gamepad::CheckButtonPressed();
72
73                 if  (button == -1)
74                         return;
75
76                 buttonDown = true;
77         }
78         else
79         {
80                 if (Gamepad::CheckButtonPressed() == button)
81                         return;
82
83                 key = button;
84                 accept();
85                 buttonDown = false;
86         }
87 }
88