| 
									
										
										
										
											2018-01-05 05:05:12 +01:00
										 |  |  | ///////////////////////////////////////////////////////////////////////////////////
 | 
					
						
							|  |  |  | // Copyright (C) 2018 Edouard Griffiths, F4EXB                                   //
 | 
					
						
							|  |  |  | //                                                                               //
 | 
					
						
							|  |  |  | // 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 as version 3 of the License, or                  //
 | 
					
						
							| 
									
										
										
										
											2019-04-11 14:43:33 +02:00
										 |  |  | // (at your option) any later version.                                           //
 | 
					
						
							| 
									
										
										
										
											2018-01-05 05:05:12 +01:00
										 |  |  | //                                                                               //
 | 
					
						
							|  |  |  | // 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 V3 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/>.          //
 | 
					
						
							|  |  |  | ///////////////////////////////////////////////////////////////////////////////////
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <gui/commandkeyreceiver.h>
 | 
					
						
							|  |  |  | #include <QEvent>
 | 
					
						
							|  |  |  | #include <QKeyEvent>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const std::vector<Qt::Key> CommandKeyReceiver::m_composeKeys = {Qt::Key_Shift, Qt::Key_Control, Qt::Key_Meta, Qt::Key_Alt, Qt::Key_AltGr}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | CommandKeyReceiver::CommandKeyReceiver() : | 
					
						
							|  |  |  |     m_release(false), | 
					
						
							|  |  |  |     m_pass(true) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool CommandKeyReceiver::eventFilter(QObject* obj, QEvent* event) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (event->type() == QEvent::KeyPress) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ((!keyEvent->isAutoRepeat()) && (!isComposeKey(static_cast<Qt::Key>(keyEvent->key())))) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  | //            qDebug("KeyReceiver::eventFilter: KeyPress");
 | 
					
						
							|  |  |  |             Qt::Key key; | 
					
						
							|  |  |  |             Qt::KeyboardModifiers keyModifiers; | 
					
						
							|  |  |  |             keyEventHandler(keyEvent, key, keyModifiers); | 
					
						
							|  |  |  |             emit capturedKey(key, keyModifiers, false); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-05 09:59:37 +01:00
										 |  |  |             if (!m_pass) { // do not pass the event
 | 
					
						
							|  |  |  |                 return true; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2018-01-05 05:05:12 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else if (m_release && (event->type()==QEvent::KeyRelease)) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ((!keyEvent->isAutoRepeat()) && (!isComposeKey(static_cast<Qt::Key>(keyEvent->key())))) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  | //            qDebug("KeyReceiver::eventFilter: KeyRelease");
 | 
					
						
							|  |  |  |             Qt::Key key; | 
					
						
							|  |  |  |             Qt::KeyboardModifiers keyModifiers; | 
					
						
							|  |  |  |             keyEventHandler(keyEvent, key, keyModifiers); | 
					
						
							|  |  |  |             emit capturedKey(key, keyModifiers, true); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-05 09:59:37 +01:00
										 |  |  |             if (!m_pass) { // do not pass the event
 | 
					
						
							|  |  |  |                 return true; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2018-01-05 05:05:12 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-05 09:59:37 +01:00
										 |  |  |     return QObject::eventFilter(obj, event); // pass the event on
 | 
					
						
							| 
									
										
										
										
											2018-01-05 05:05:12 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void CommandKeyReceiver::keyEventHandler(QKeyEvent *e, Qt::Key& key, Qt::KeyboardModifiers& keyModifiers) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     key = static_cast<Qt::Key>(e->key()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (e->modifiers()) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         keyModifiers = e->modifiers(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         keyModifiers = Qt::NoModifier; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool CommandKeyReceiver::isComposeKey(Qt::Key key) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     auto it = std::find(m_composeKeys.begin(), m_composeKeys.end(), key); | 
					
						
							|  |  |  |     return it != m_composeKeys.end(); | 
					
						
							|  |  |  | } |