Skip to content

Commit

Permalink
Refactor|libgui: KeyEvent class
Browse files Browse the repository at this point in the history
KeyEventSource now produces an instance of KeyEvent.
  • Loading branch information
skyjake committed Apr 13, 2013
1 parent e9a00db commit 4de6ad7
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 31 deletions.
1 change: 1 addition & 0 deletions doomsday/libgui/include/de/KeyEvent
@@ -0,0 +1 @@
#include "gui/keyevent.h"
72 changes: 72 additions & 0 deletions doomsday/libgui/include/de/gui/keyevent.h
@@ -0,0 +1,72 @@
/** @file keyevent.h Input event from a keyboard.
*
* @authors Copyright (c) 2013 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details. You should have received a copy of the GNU
* General Public License along with this program; if not, see:
* http://www.gnu.org/licenses</small>
*/

#ifndef LIBGUI_KEYEVENT_H
#define LIBGUI_KEYEVENT_H

#include "libgui.h"
#include "ddkey.h"
#include <de/Event>
#include <de/String>

namespace de {

/**
* Input event generated by a keyboard device. @ingroup ui
*/
class LIBGUI_PUBLIC KeyEvent : public de::Event
{
public:
enum State
{
Released, ///< Released key.
Pressed ///< Pressed key.
};

public:
KeyEvent();

KeyEvent(State keyState, int qtKeyCode, int ddKeyCode, int nativeKeyCode, de::String const &keyText);

State state() const;
inline int qtKey() const { return _qtKey; }
inline int ddKey() const { return _ddKey; }
inline int nativeCode() const { return _nativeCode; }
inline de::String const &text() const { return _text; }

/**
* Translates a Qt key code to a Doomsday key code (see ddkey.h).
*
* @param qtKey Qt key code.
* @param nativeVirtualKey Native virtual key code.
* @param nativeScanCode Native scan code.
*
* @return DDKEY code.
*/
static int ddKeyFromQt(int qtKey, int nativeVirtualKey, int nativeScanCode);

private:
int _qtKey;
int _ddKey;
int _nativeCode;
de::String _text;
};

} // namespace de

#endif // LIBGUI_KEYEVENT_H
21 changes: 2 additions & 19 deletions doomsday/libgui/include/de/gui/keyeventsource.h
Expand Up @@ -20,7 +20,7 @@
#define LIBGUI_KEYEVENTSOURCE_H

#include "libgui.h"
#include "ddkey.h"
#include "keyevent.h"
#include <de/Observers>
#include <de/String>

Expand All @@ -32,27 +32,10 @@ namespace de {
class LIBGUI_PUBLIC KeyEventSource
{
public:
enum KeyState
{
Released, ///< Released button.
Pressed ///< Pressed button.
};

DENG2_DEFINE_AUDIENCE(KeyEvent, void keyEvent(KeyState state, int ddKey, int nativeCode, String const &text))
DENG2_DEFINE_AUDIENCE(KeyEvent, void keyEvent(KeyEvent const &))

public:
virtual ~KeyEventSource() {}

/**
* Translates a Qt key code to a Doomsday key code (see ddkey.h).
*
* @param qtKey Qt key code.
* @param nativeVirtualKey Native virtual key code.
* @param nativeScanCode Native scan code.
*
* @return DDKEY code.
*/
static int ddKeyFromQt(int qtKey, int nativeVirtualKey, int nativeScanCode);
};

} // namespace de
Expand Down
4 changes: 3 additions & 1 deletion doomsday/libgui/libgui.pro
Expand Up @@ -48,6 +48,7 @@ HEADERS += \
include/de/Canvas \
include/de/CanvasWindow \
include/de/GuiApp \
include/de/KeyEvent \
include/de/KeyEventSource \
include/de/MouseEventSource \
include/de/PersistentCanvasWindow \
Expand All @@ -58,6 +59,7 @@ HEADERS += \
include/de/gui/displaymode.h \
include/de/gui/displaymode_native.h \
include/de/gui/guiapp.h \
include/de/gui/keyevent.h \
include/de/gui/keyeventsource.h \
include/de/gui/libgui.h \
include/de/gui/mouseeventsource.h \
Expand All @@ -69,7 +71,7 @@ SOURCES += \
src/canvaswindow.cpp \
src/displaymode.cpp \
src/guiapp.cpp \
src/keyeventsource.cpp \
src/keyevent.cpp \
src/persistentcanvaswindow.cpp

# DisplayMode
Expand Down
11 changes: 6 additions & 5 deletions doomsday/libgui/src/canvas.cpp
Expand Up @@ -152,11 +152,12 @@ DENG2_PIMPL(Canvas)

DENG2_FOR_PUBLIC_AUDIENCE(KeyEvent, i)
{
i->keyEvent(ev->type() == QEvent::KeyPress? KeyEventSource::Pressed :
KeyEventSource::Released,
ddKeyFromQt(ev->key(), ev->nativeVirtualKey(), ev->nativeScanCode()),
nativeCode(ev),
ev->text());
i->keyEvent(KeyEvent(ev->type() == QEvent::KeyPress? KeyEvent::Pressed :
KeyEvent::Released,
ev->key(),
KeyEvent::ddKeyFromQt(ev->key(), ev->nativeVirtualKey(), ev->nativeScanCode()),
nativeCode(ev),
ev->text()));
}
}
};
Expand Down
@@ -1,4 +1,4 @@
/** @file keyeventsource.cpp Object that produces key events.
/** @file keyevent.cpp Input event from a keyboard.
* @ingroup input
*
* Depends on Qt GUI.
Expand All @@ -25,7 +25,7 @@
# include <windows.h>
#endif

#include "de/KeyEventSource"
#include "de/KeyEvent"
#include <QKeyEvent>
#include <de/Log>

Expand Down Expand Up @@ -177,7 +177,7 @@ static void checkWin32Keymap()
}
#endif // WIN32

int de::KeyEventSource::ddKeyFromQt(int qtKey, int nativeVirtualKey, int nativeScanCode)
int de::KeyEvent::ddKeyFromQt(int qtKey, int nativeVirtualKey, int nativeScanCode)
{
#ifdef MACOSX
switch(qtKey)
Expand Down Expand Up @@ -396,3 +396,24 @@ static int x11ScancodeToDDKey(int scancode)
return 0;
}
#endif

namespace de {

KeyEvent::KeyEvent()
: Event(KeyPress), _qtKey(0), _ddKey(0), _nativeCode(0)
{}

KeyEvent::KeyEvent(State keyState, int qtKeyCode, int ddKeyCode, int nativeKeyCode, String const &keyText)
: Event(keyState == Pressed? KeyPress : KeyRelease),
_qtKey(qtKeyCode),
_ddKey(ddKeyCode),
_nativeCode(nativeKeyCode),
_text(keyText)
{}

KeyEvent::State KeyEvent::state() const
{
return type() == KeyPress? Pressed : Released;
}

} // namespace de
6 changes: 3 additions & 3 deletions doomsday/libshell/include/de/shell/keyevent.h
Expand Up @@ -55,9 +55,9 @@ class LIBSHELL_PUBLIC KeyEvent : public Event
}

private:
String _text;
int _code;
Modifiers _modifiers;
String _text; ///< Text to be inserted by the event.
int _code; ///< Qt key code.
Modifiers _modifiers; ///< Modifiers in effect.
};

Q_DECLARE_OPERATORS_FOR_FLAGS(KeyEvent::Modifiers)
Expand Down

0 comments on commit 4de6ad7

Please sign in to comment.