OSDN Git Service

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