OSDN Git Service

6384476fb0e3dcec952ae8d245f88834c5c87246
[x264-launcher/x264-launcher.git] / src / input_filter.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Simple x264 Launcher
3 // Copyright (C) 2004-2018 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.
14 //
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.
18 //
19 // http://www.gnu.org/licenses/gpl-2.0.txt
20 ///////////////////////////////////////////////////////////////////////////////
21
22 #include "input_filter.h"
23
24 //Internal
25 #include "global.h"
26
27 //MUTils
28 #include <MUtils/Global.h>
29
30 //Qt
31 #include <QWidget>
32 #include <QKeyEvent>
33 #include <QMouseEvent>
34 #include <QHash>
35
36 InputEventFilter::InputEventFilter(QWidget *target)
37 :
38         m_target(target),
39         m_keyMapping(new QHash<int, int>()),
40         m_mouseMapping(new QHash<int, int>())
41 {
42         m_target->installEventFilter(this);
43 }
44
45 InputEventFilter::~InputEventFilter(void)
46 {
47         m_target->removeEventFilter(this);
48         MUTILS_DELETE(m_keyMapping);
49         MUTILS_DELETE(m_mouseMapping);
50 }
51
52 void InputEventFilter::addKeyFilter(const int &keyCode, const int &tag)
53 {
54         m_keyMapping->insert(keyCode, tag);
55 }
56
57 void InputEventFilter::addMouseFilter(const int &mouseCode, const int &tag)
58 {
59         m_mouseMapping->insert(mouseCode, tag);
60 }
61
62 bool InputEventFilter::eventFilter(QObject *obj, QEvent *event)
63 {
64         if(obj == m_target)
65         {
66                 if(event->type() == QEvent::KeyPress)
67                 {
68                         QKeyEvent *keyEvent = dynamic_cast<QKeyEvent*>(event);
69                         if(keyEvent)
70                         {
71                                 return eventFilter(keyEvent);
72                         }
73                 }
74                 else if(event->type() == QEvent::MouseButtonPress)
75                 {
76                         QMouseEvent *mouseEvent = dynamic_cast<QMouseEvent*>(event);
77                         if(mouseEvent)
78                         {
79                                 return eventFilter(mouseEvent);
80                         }
81                 }
82         }
83         return false;
84 }
85
86 bool InputEventFilter::eventFilter(QKeyEvent *keyEvent)
87 {
88         const int keyCode = keyEvent->key() | keyEvent->modifiers();
89         if(m_keyMapping->contains(keyCode))
90         {
91                 emit keyPressed(m_keyMapping->value(keyCode));
92                 return true;
93         }
94         return false;
95 }
96
97 bool InputEventFilter::eventFilter(QMouseEvent *mouseEvent)
98 {
99         if(m_mouseMapping->contains(mouseEvent->button()))
100         {
101                 emit mouseClicked(m_mouseMapping->value(mouseEvent->button()));
102                 return true;
103         }
104         return false;
105 }