1
/***************************************************************************
2
* Copyright (c) 2023 Abdullah Tahiri <abdullah.tahiri.yo@gmail.com> *
4
* This file is part of the FreeCAD CAx development system. *
6
* This library is free software; you can redistribute it and/or *
7
* modify it under the terms of the GNU Library General Public *
8
* License as published by the Free Software Foundation; either *
9
* version 2 of the License, or (at your option) any later version. *
11
* This library is distributed in the hope that it will be useful, *
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14
* GNU Library General Public License for more details. *
16
* You should have received a copy of the GNU Library General Public *
17
* License along with this library; see the file COPYING.LIB. If not, *
18
* write to the Free Software Foundation, Inc., 59 Temple Place, *
19
* Suite 330, Boston, MA 02111-1307, USA *
21
***************************************************************************/
24
#include "PreCompiled.h"
27
#include <Inventor/events/SoKeyboardEvent.h>
28
#include <QApplication>
30
#include <QRegularExpression>
31
#include <QRegularExpressionMatch>
34
#include "ViewProviderSketch.h"
36
#include "DrawSketchKeyboardManager.h"
38
using namespace SketcherGui;
41
DrawSketchKeyboardManager::DrawSketchKeyboardManager()
43
, keyMode(KeyboardEventHandlingMode::DSHControl)
45
// get the active viewer, so that we can send it key events
46
auto doc = Gui::Application::Instance->activeDocument();
49
auto temp = dynamic_cast<Gui::View3DInventor*>(doc->getActiveView());
51
vpViewer = temp->getViewer();
52
keyMode = KeyboardEventHandlingMode::ViewProvider;
56
timer.setSingleShot(true);
58
QObject::connect(&timer, &QTimer::timeout, [this]() {
63
bool DrawSketchKeyboardManager::isMode(KeyboardEventHandlingMode mode)
65
return mode == keyMode;
68
DrawSketchKeyboardManager::KeyboardEventHandlingMode DrawSketchKeyboardManager::getMode()
73
bool DrawSketchKeyboardManager::eventFilter(QObject* object, QEvent* event)
77
if (event->type() == QEvent::KeyPress || event->type() == QEvent::KeyRelease) {
78
/*If a key shortcut is required to work on sketcher when a tool using ui controls is being
79
* used, then you have to add this key to the below section such that the spinbox doesn't
80
* keep the keypress event for itself. Note if you want the event to be handled by the
81
* spinbox too, you can return false.*/
83
auto keyEvent = static_cast<QKeyEvent*>(event);
85
detectKeyboardEventHandlingMode(keyEvent); // determine the handler
87
if (vpViewer && isMode(KeyboardEventHandlingMode::ViewProvider)) {
88
return QApplication::sendEvent(vpViewer, keyEvent);
91
return false; // do not intercept the event and feed it to the widget
97
void DrawSketchKeyboardManager::detectKeyboardEventHandlingMode(QKeyEvent* keyEvent)
99
QRegularExpression rx(QStringLiteral("^[0-9]$"));
100
auto match = rx.match(keyEvent->text());
101
if (keyEvent->key() == Qt::Key_Enter || keyEvent->key() == Qt::Key_Return
102
|| keyEvent->key() == Qt::Key_Tab || keyEvent->key() == Qt::Key_Backtab
103
|| keyEvent->key() == Qt::Key_Minus || keyEvent->key() == Qt::Key_Period
104
|| keyEvent->key() == Qt::Key_Comma || match.hasMatch()
105
|| keyEvent->matches(QKeySequence::Backspace) || keyEvent->matches(QKeySequence::Delete)) {
106
keyMode = KeyboardEventHandlingMode::DSHControl;
107
timer.start(timeOutValue);
111
void DrawSketchKeyboardManager::onTimeOut()
113
keyMode = KeyboardEventHandlingMode::ViewProvider;
116
/// sets the timeout to the amount of milliseconds.
117
void DrawSketchKeyboardManager::setTimeOut(int milliseconds)
119
timeOutValue = milliseconds;
122
// returns the current timeout amount
123
int DrawSketchKeyboardManager::timeOut()
129
#include "moc_DrawSketchKeyboardManager.cpp"