Removed a lot of old cruft and use MUtils functions where possible.
[simple-x264-launcher.git] / src / input_filter.cpp
blob5a2dd87b4bc6ac5848240a7c288a062e6d65900f
1 ///////////////////////////////////////////////////////////////////////////////
2 // Simple x264 Launcher
3 // Copyright (C) 2004-2015 LoRd_MuldeR <MuldeR2@GMX.de>
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License along
16 // with this program; if not, write to the Free Software Foundation, Inc.,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 // http://www.gnu.org/licenses/gpl-2.0.txt
20 ///////////////////////////////////////////////////////////////////////////////
22 #include "input_filter.h"
24 //Internal
25 #include "global.h"
27 //MUTils
28 #include <MUtils/Global.h>
30 //Qt
31 #include <QWidget>
32 #include <QKeyEvent>
33 #include <QMouseEvent>
34 #include <QHash>
36 InputEventFilter::InputEventFilter(QWidget *target)
38 m_target(target),
39 m_keyMapping(new QHash<int, int>()),
40 m_mouseMapping(new QHash<int, int>())
42 m_target->installEventFilter(this);
45 InputEventFilter::~InputEventFilter(void)
47 m_target->removeEventFilter(this);
48 MUTILS_DELETE(m_keyMapping);
49 MUTILS_DELETE(m_mouseMapping);
52 void InputEventFilter::addKeyFilter(const int &keyCode, const int &tag)
54 m_keyMapping->insert(keyCode, tag);
57 void InputEventFilter::addMouseFilter(const int &mouseCode, const int &tag)
59 m_mouseMapping->insert(mouseCode, tag);
62 bool InputEventFilter::eventFilter(QObject *obj, QEvent *event)
64 if(obj == m_target)
66 if(event->type() == QEvent::KeyPress)
68 QKeyEvent *keyEvent = dynamic_cast<QKeyEvent*>(event);
69 if(keyEvent)
71 return eventFilter(keyEvent);
74 else if(event->type() == QEvent::MouseButtonPress)
76 QMouseEvent *mouseEvent = dynamic_cast<QMouseEvent*>(event);
77 if(mouseEvent)
79 return eventFilter(mouseEvent);
83 return false;
86 bool InputEventFilter::eventFilter(QKeyEvent *keyEvent)
88 const int keyCode = keyEvent->key() | keyEvent->modifiers();
89 if(m_keyMapping->contains(keyCode))
91 emit keyPressed(m_keyMapping->value(keyCode));
92 return true;
94 return false;
97 bool InputEventFilter::eventFilter(QMouseEvent *mouseEvent)
99 if(m_mouseMapping->contains(mouseEvent->button()))
101 emit mouseClicked(m_mouseMapping->value(mouseEvent->button()));
102 return true;
104 return false;