| 1 | /* | 
|---|
| 2 | orxonox - the future of 3D-vertical-scrollers | 
|---|
| 3 |  | 
|---|
| 4 | Copyright (C) 2004 orx | 
|---|
| 5 |  | 
|---|
| 6 | This program is free software; you can redistribute it and/or modify | 
|---|
| 7 | it under the terms of the GNU General Public License as published by | 
|---|
| 8 | the Free Software Foundation; either version 2, or (at your option) | 
|---|
| 9 | any later version. | 
|---|
| 10 |  | 
|---|
| 11 | This program is distributed in the hope that it will be useful, | 
|---|
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 14 | GNU General Public License for more details. | 
|---|
| 15 |  | 
|---|
| 16 | You should have received a copy of the GNU General Public License | 
|---|
| 17 | along with this program; if not, write to the Free Software Foundation, | 
|---|
| 18 | Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | 
|---|
| 19 |  | 
|---|
| 20 |  | 
|---|
| 21 | ### File Specific: | 
|---|
| 22 | main-programmer: Benjamin Grauer | 
|---|
| 23 |  | 
|---|
| 24 | */ | 
|---|
| 25 |  | 
|---|
| 26 |  | 
|---|
| 27 | #include "gui_control.h" | 
|---|
| 28 |  | 
|---|
| 29 | #include <QtGui/QLayout> | 
|---|
| 30 | #include <QtCore/QEvent> | 
|---|
| 31 | #include <QtGui/QKeyEvent> | 
|---|
| 32 | #include <QtGui/QMouseEvent> | 
|---|
| 33 |  | 
|---|
| 34 | #include "sdlincl.h" | 
|---|
| 35 | #include "lib/event/key_names.h" | 
|---|
| 36 |  | 
|---|
| 37 | #include "globals.h" | 
|---|
| 38 | #include "debug.h" | 
|---|
| 39 | #include "qt_gui_elements.h" | 
|---|
| 40 |  | 
|---|
| 41 | #include "lib/event/key_mapper.h" | 
|---|
| 42 | #include "lib/event/key_names.h" | 
|---|
| 43 |  | 
|---|
| 44 | namespace OrxGui | 
|---|
| 45 | { | 
|---|
| 46 | /** | 
|---|
| 47 | *  Creates the Control-Option-Frame | 
|---|
| 48 | */ | 
|---|
| 49 | GuiControl::GuiControl(OrxGui::Gui* gui) | 
|---|
| 50 | : Element(CONFIG_SECTION_CONTROL, gui), QGroupBox() | 
|---|
| 51 | { | 
|---|
| 52 | QGridLayout* layout = new QGridLayout(this); | 
|---|
| 53 |  | 
|---|
| 54 | { | 
|---|
| 55 | //QLabel* keyLabel = new QLabel; | 
|---|
| 56 |  | 
|---|
| 57 | const KeyMapper::KeyMapping* map = KeyMapper::getKeyMapping(); | 
|---|
| 58 | unsigned int i = 0; | 
|---|
| 59 | while(!map->pName.empty()) | 
|---|
| 60 | { | 
|---|
| 61 | QLabel* label = new QLabel(QString().fromStdString(map->pName)); | 
|---|
| 62 | layout->addWidget(label, i, 0); | 
|---|
| 63 |  | 
|---|
| 64 | GuiControlInput* input = new GuiControlInput(map->pName, this, EVToKeyName(map->defaultValue)); | 
|---|
| 65 | layout->addWidget(input, i, 1); | 
|---|
| 66 |  | 
|---|
| 67 | ++map; | 
|---|
| 68 | ++i; | 
|---|
| 69 | } | 
|---|
| 70 | } | 
|---|
| 71 |  | 
|---|
| 72 |  | 
|---|
| 73 | } | 
|---|
| 74 |  | 
|---|
| 75 | /** | 
|---|
| 76 | *  Destructs the Control-stuff | 
|---|
| 77 | */ | 
|---|
| 78 | GuiControl::~GuiControl() | 
|---|
| 79 | { | 
|---|
| 80 | // nothing to do here. | 
|---|
| 81 | } | 
|---|
| 82 |  | 
|---|
| 83 | GuiControlInput::GuiControlInput(const std::string& name, SaveableGroup* group, const std::string& defaultValue) | 
|---|
| 84 | : QPushButton(QString().fromStdString(name)), Saveable(name, group, defaultValue) | 
|---|
| 85 | { | 
|---|
| 86 | this->bListening = false; | 
|---|
| 87 |  | 
|---|
| 88 | connect(this, SIGNAL(released()), this, SLOT(listen())); | 
|---|
| 89 |  | 
|---|
| 90 | } | 
|---|
| 91 |  | 
|---|
| 92 | void GuiControlInput::load() | 
|---|
| 93 | { | 
|---|
| 94 | Saveable::load(); | 
|---|
| 95 | this->setText(QString().fromStdString(this->value().getString())); | 
|---|
| 96 | } | 
|---|
| 97 |  | 
|---|
| 98 | void GuiControlInput::save() | 
|---|
| 99 | { | 
|---|
| 100 | this->value() = this->text().toStdString(); | 
|---|
| 101 | Saveable::save(); | 
|---|
| 102 | } | 
|---|
| 103 |  | 
|---|
| 104 |  | 
|---|
| 105 | bool GuiControlInput::event ( QEvent * e ) | 
|---|
| 106 | { | 
|---|
| 107 | if (this->bListening && (e->type() == QEvent::KeyPress || e->type() == QEvent::MouseButtonPress)) | 
|---|
| 108 | { | 
|---|
| 109 | this->bListening = false; | 
|---|
| 110 | this->releaseKeyboard(); | 
|---|
| 111 | this->releaseMouse(); | 
|---|
| 112 | /// KEY IS PRESSED: | 
|---|
| 113 | if (e->type() == QEvent::KeyPress) | 
|---|
| 114 | { | 
|---|
| 115 | QKeyEvent* event = dynamic_cast<QKeyEvent*>(e); | 
|---|
| 116 |  | 
|---|
| 117 | int ev = QtKToSDLK(event->key()); | 
|---|
| 118 | if (ev != -1) | 
|---|
| 119 | { | 
|---|
| 120 | this->setText(QString().fromStdString(SDLKToKeyname(ev))); | 
|---|
| 121 | } | 
|---|
| 122 | return true; | 
|---|
| 123 | } | 
|---|
| 124 | /// MOUSE BUTTON PRESSED: | 
|---|
| 125 | if (e->type() == QEvent::MouseButtonPress) | 
|---|
| 126 | { | 
|---|
| 127 | QMouseEvent* event = dynamic_cast<QMouseEvent*>(e); | 
|---|
| 128 | if (event->button() != Qt::NoButton) | 
|---|
| 129 | { | 
|---|
| 130 | this->setText(QString().fromStdString(QtKToString(event->button()))); | 
|---|
| 131 | } | 
|---|
| 132 |  | 
|---|
| 133 | } | 
|---|
| 134 |  | 
|---|
| 135 | } | 
|---|
| 136 |  | 
|---|
| 137 | return QPushButton::event(e); | 
|---|
| 138 | } | 
|---|
| 139 |  | 
|---|
| 140 | void GuiControlInput::listen() | 
|---|
| 141 | { | 
|---|
| 142 | this->bListening = true; | 
|---|
| 143 | this->grabKeyboard(); | 
|---|
| 144 | this->grabMouse(); | 
|---|
| 145 | } | 
|---|
| 146 |  | 
|---|
| 147 | int GuiControlInput::QtKToSDLK(int key) | 
|---|
| 148 | { | 
|---|
| 149 |  | 
|---|
| 150 | switch(key) | 
|---|
| 151 | { | 
|---|
| 152 | case Qt::Key_Backspace: return SDLK_BACKSPACE; | 
|---|
| 153 | case Qt::Key_Tab: return SDLK_TAB; | 
|---|
| 154 | case Qt::Key_Clear: return SDLK_CLEAR; | 
|---|
| 155 | case Qt::Key_Return: return SDLK_RETURN; | 
|---|
| 156 | case Qt::Key_Escape: return SDLK_ESCAPE; | 
|---|
| 157 | case Qt::Key_Space: return SDLK_SPACE; | 
|---|
| 158 | case Qt::Key_exclamdown: return SDLK_EXCLAIM; | 
|---|
| 159 | case Qt::Key_QuoteDbl: return SDLK_QUOTEDBL; | 
|---|
| 160 | //case Qt::Key_Hash: return SDLK_HASH; | 
|---|
| 161 | case Qt::Key_Pause: return SDLK_PAUSE; | 
|---|
| 162 | case Qt::Key_Dollar: return SDLK_DOLLAR; | 
|---|
| 163 | case Qt::Key_Ampersand: return SDLK_AMPERSAND; | 
|---|
| 164 | case Qt::Key_QuoteLeft: return SDLK_QUOTE; /// TODO check if correct | 
|---|
| 165 | case Qt::Key_ParenLeft: return SDLK_LEFTPAREN; | 
|---|
| 166 | case Qt::Key_ParenRight: return SDLK_RIGHTPAREN; | 
|---|
| 167 | case Qt::Key_Asterisk: return SDLK_ASTERISK; | 
|---|
| 168 | case Qt::Key_Plus: return SDLK_PLUS; | 
|---|
| 169 | case Qt::Key_Comma: return SDLK_COMMA; | 
|---|
| 170 | case Qt::Key_Minus: return SDLK_MINUS; | 
|---|
| 171 | case Qt::Key_Period: return SDLK_PERIOD; | 
|---|
| 172 | case Qt::Key_Slash: return SDLK_SLASH; | 
|---|
| 173 | case Qt::Key_0: return SDLK_0; | 
|---|
| 174 | case Qt::Key_1: return SDLK_1; | 
|---|
| 175 | case Qt::Key_2: return SDLK_2; | 
|---|
| 176 | case Qt::Key_3: return SDLK_3; | 
|---|
| 177 | case Qt::Key_4: return SDLK_4; | 
|---|
| 178 | case Qt::Key_5: return SDLK_5; | 
|---|
| 179 | case Qt::Key_6: return SDLK_6; | 
|---|
| 180 | case Qt::Key_7: return SDLK_7; | 
|---|
| 181 | case Qt::Key_8: return SDLK_8; | 
|---|
| 182 | case Qt::Key_9: return SDLK_9; | 
|---|
| 183 | case Qt::Key_Colon: return SDLK_COLON; | 
|---|
| 184 | case Qt::Key_Semicolon: return SDLK_SEMICOLON; | 
|---|
| 185 | case Qt::Key_Less: return SDLK_LESS; | 
|---|
| 186 | case Qt::Key_Equal: return SDLK_EQUALS; | 
|---|
| 187 | case Qt::Key_Greater: return SDLK_GREATER; | 
|---|
| 188 | case Qt::Key_Question: return SDLK_QUESTION; | 
|---|
| 189 | case Qt::Key_At: return SDLK_AT; | 
|---|
| 190 | case Qt::Key_BracketLeft: return SDLK_LEFTBRACKET; | 
|---|
| 191 | case Qt::Key_Backslash: return SDLK_BACKSLASH; | 
|---|
| 192 | case Qt::Key_BracketRight: return SDLK_RIGHTBRACKET; | 
|---|
| 193 | ///case Qt::Key_Caret: return SDLK_CARET; | 
|---|
| 194 | case Qt::Key_Underscore: return SDLK_UNDERSCORE; | 
|---|
| 195 | //case Qt::Key_Backquote: return SDLK_BACKQUOTE; | 
|---|
| 196 | case Qt::Key_A: return SDLK_a; | 
|---|
| 197 | case Qt::Key_B: return SDLK_b; | 
|---|
| 198 | case Qt::Key_C: return SDLK_c; | 
|---|
| 199 | case Qt::Key_D: return SDLK_d; | 
|---|
| 200 | case Qt::Key_E: return SDLK_e; | 
|---|
| 201 | case Qt::Key_F: return SDLK_f; | 
|---|
| 202 | case Qt::Key_G: return SDLK_g; | 
|---|
| 203 | case Qt::Key_H: return SDLK_h; | 
|---|
| 204 | case Qt::Key_I: return SDLK_i; | 
|---|
| 205 | case Qt::Key_J: return SDLK_j; | 
|---|
| 206 | case Qt::Key_K: return SDLK_k; | 
|---|
| 207 | case Qt::Key_L: return SDLK_l; | 
|---|
| 208 | case Qt::Key_M: return SDLK_m; | 
|---|
| 209 | case Qt::Key_N: return SDLK_n; | 
|---|
| 210 | case Qt::Key_O: return SDLK_o; | 
|---|
| 211 | case Qt::Key_P: return SDLK_p; | 
|---|
| 212 | case Qt::Key_Q: return SDLK_q; | 
|---|
| 213 | case Qt::Key_R: return SDLK_r; | 
|---|
| 214 | case Qt::Key_S: return SDLK_s; | 
|---|
| 215 | case Qt::Key_T: return SDLK_t; | 
|---|
| 216 | case Qt::Key_U: return SDLK_u; | 
|---|
| 217 | case Qt::Key_V: return SDLK_v; | 
|---|
| 218 | case Qt::Key_W: return SDLK_w; | 
|---|
| 219 | case Qt::Key_X: return SDLK_x; | 
|---|
| 220 | case Qt::Key_Y: return SDLK_y; | 
|---|
| 221 | case Qt::Key_Z: return SDLK_z; | 
|---|
| 222 | case Qt::Key_Delete: return SDLK_DELETE; | 
|---|
| 223 | /*      case Qt::Key_KP0: return SDLK_KP0; | 
|---|
| 224 | case Qt::Key_KP1: return SDLK_KP1; | 
|---|
| 225 | case Qt::Key_KP2: return SDLK_KP2; | 
|---|
| 226 | case Qt::Key_KP3: return SDLK_KP3; | 
|---|
| 227 | case Qt::Key_KP4: return SDLK_KP4; | 
|---|
| 228 | case Qt::Key_KP5: return SDLK_KP5; | 
|---|
| 229 | case Qt::Key_KP6: return SDLK_KP6; | 
|---|
| 230 | case Qt::Key_KP7: return SDLK_KP7; | 
|---|
| 231 | case Qt::Key_KP8: return SDLK_KP8; | 
|---|
| 232 | case Qt::Key_KP9: return SDLK_KP9; */ | 
|---|
| 233 | /*      case Qt::Key_KP_Period: return SDLK_KP_PERIOD; | 
|---|
| 234 | case Qt::Key_KP_Divide: return SDLK_KP_DIVIDE; | 
|---|
| 235 | case Qt::Key_KP_Multiply: return SDLK_KP_MULTIPLY; | 
|---|
| 236 | case Qt::Key_KP_Minus: return SDLK_KP_MINUS; | 
|---|
| 237 | case Qt::Key_KP_PLUS: return SDLK_KP_PLUS; | 
|---|
| 238 | case Qt::Key_KP_ENTER: return SDLK_KP_ENTER; | 
|---|
| 239 | case Qt::Key_KP_EQUALS: return SDLK_KP_EQUALS;*/ | 
|---|
| 240 | case Qt::Key_Up: return SDLK_UP; | 
|---|
| 241 | case Qt::Key_Down: return SDLK_DOWN; | 
|---|
| 242 | case Qt::Key_Right: return SDLK_RIGHT; | 
|---|
| 243 | case Qt::Key_Left: return SDLK_LEFT; | 
|---|
| 244 | case Qt::Key_Insert: return SDLK_INSERT; | 
|---|
| 245 | case Qt::Key_Home: return SDLK_HOME; | 
|---|
| 246 | case Qt::Key_End: return SDLK_END; | 
|---|
| 247 | case Qt::Key_PageUp: return SDLK_PAGEUP; | 
|---|
| 248 | case Qt::Key_PageDown: return SDLK_PAGEDOWN; | 
|---|
| 249 | case Qt::Key_F1: return SDLK_F1; | 
|---|
| 250 | case Qt::Key_F2: return SDLK_F2; | 
|---|
| 251 | case Qt::Key_F3: return SDLK_F3; | 
|---|
| 252 | case Qt::Key_F4: return SDLK_F4; | 
|---|
| 253 | case Qt::Key_F5: return SDLK_F5; | 
|---|
| 254 | case Qt::Key_F6: return SDLK_F6; | 
|---|
| 255 | case Qt::Key_F7: return SDLK_F7; | 
|---|
| 256 | case Qt::Key_F8: return SDLK_F8; | 
|---|
| 257 | case Qt::Key_F9: return SDLK_F9; | 
|---|
| 258 | case Qt::Key_F10: return SDLK_F10; | 
|---|
| 259 | case Qt::Key_F11: return SDLK_F11; | 
|---|
| 260 | case Qt::Key_F12: return SDLK_F12; | 
|---|
| 261 | case Qt::Key_F13: return SDLK_F13; | 
|---|
| 262 | case Qt::Key_F14: return SDLK_F14; | 
|---|
| 263 | case Qt::Key_F15: return SDLK_F15; | 
|---|
| 264 | case Qt::Key_NumLock: return SDLK_NUMLOCK; | 
|---|
| 265 | case Qt::Key_CapsLock: return SDLK_CAPSLOCK; | 
|---|
| 266 | case Qt::Key_ScrollLock: return SDLK_SCROLLOCK; | 
|---|
| 267 | ///case Qt::Key_RSHIFT: return SDLK_RSHIFT; | 
|---|
| 268 | case Qt::Key_Shift: return SDLK_LSHIFT; | 
|---|
| 269 | ///case Qt::Key_RCTRL: return SDLK_RCTRL; | 
|---|
| 270 | case Qt::Key_Control: return SDLK_LCTRL; | 
|---|
| 271 | ///case Qt::Key_RALT: return SDLK_RALT; | 
|---|
| 272 | case Qt::Key_Alt: return SDLK_LALT; | 
|---|
| 273 | //case Qt::Key_RMETA: return SDLK_RMETA; | 
|---|
| 274 | case Qt::Key_Meta: return SDLK_LMETA; | 
|---|
| 275 | case Qt::Key_Super_L: return SDLK_LSUPER; | 
|---|
| 276 | case Qt::Key_Super_R: return SDLK_RSUPER; | 
|---|
| 277 | case Qt::Key_Mode_switch: return SDLK_MODE; | 
|---|
| 278 | case Qt::Key_Help: return SDLK_HELP; | 
|---|
| 279 | case Qt::Key_Print: return SDLK_PRINT; | 
|---|
| 280 | case Qt::Key_SysReq: return SDLK_SYSREQ; | 
|---|
| 281 | ///case Qt::Key_Break: return SDLK_BREAK; | 
|---|
| 282 | ///case Qt::Key_Menu: return SDLK_MENU; | 
|---|
| 283 | ///case Qt::Key_Power: return SDLK_POWER; | 
|---|
| 284 | ///case Qt::Key_Euro: return SDLK_EURO; | 
|---|
| 285 | return -1; | 
|---|
| 286 | } | 
|---|
| 287 | } | 
|---|
| 288 |  | 
|---|
| 289 | std::string GuiControlInput::QtKToString(int button) | 
|---|
| 290 | { | 
|---|
| 291 | if( button == Qt::LeftButton) return "BUTTON_LEFT"; | 
|---|
| 292 | if( button == Qt::MidButton) return "BUTTON_MIDDLE"; | 
|---|
| 293 | if( button == Qt::RightButton) return "BUTTON_RIGHT"; | 
|---|
| 294 | //if( button == Qt::) return "BUTTON_WHEELUP"; | 
|---|
| 295 | //if( button == EV_MOUSE_BUTTON_WHEELDOWN) return "BUTTON_WHEELDOWN"; | 
|---|
| 296 | return "UNKNOWN"; | 
|---|
| 297 | } | 
|---|
| 298 |  | 
|---|
| 299 | } | 
|---|