FreeCAD

Форк
0
/
DrawSketchKeyboardManager.cpp 
129 строк · 4.8 Кб
1
/***************************************************************************
2
 *   Copyright (c) 2023 Abdullah Tahiri <abdullah.tahiri.yo@gmail.com>     *
3
 *                                                                         *
4
 *   This file is part of the FreeCAD CAx development system.              *
5
 *                                                                         *
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.      *
10
 *                                                                         *
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.                  *
15
 *                                                                         *
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                                *
20
 *                                                                         *
21
 ***************************************************************************/
22

23

24
#include "PreCompiled.h"
25

26
#ifndef _PreComp_
27
#include <Inventor/events/SoKeyboardEvent.h>
28
#include <QApplication>
29
#include <QEvent>
30
#include <QRegularExpression>
31
#include <QRegularExpressionMatch>
32
#endif
33

34
#include "ViewProviderSketch.h"
35

36
#include "DrawSketchKeyboardManager.h"
37

38
using namespace SketcherGui;
39

40

41
DrawSketchKeyboardManager::DrawSketchKeyboardManager()
42
    : QObject(nullptr)
43
    , keyMode(KeyboardEventHandlingMode::DSHControl)
44
{
45
    // get the active viewer, so that we can send it key events
46
    auto doc = Gui::Application::Instance->activeDocument();
47

48
    if (doc) {
49
        auto temp = dynamic_cast<Gui::View3DInventor*>(doc->getActiveView());
50
        if (temp) {
51
            vpViewer = temp->getViewer();
52
            keyMode = KeyboardEventHandlingMode::ViewProvider;
53
        }
54
    }
55

56
    timer.setSingleShot(true);
57

58
    QObject::connect(&timer, &QTimer::timeout, [this]() {
59
        onTimeOut();
60
    });
61
}
62

63
bool DrawSketchKeyboardManager::isMode(KeyboardEventHandlingMode mode)
64
{
65
    return mode == keyMode;
66
}
67

68
DrawSketchKeyboardManager::KeyboardEventHandlingMode DrawSketchKeyboardManager::getMode()
69
{
70
    return keyMode;
71
}
72

73
bool DrawSketchKeyboardManager::eventFilter(QObject* object, QEvent* event)
74
{
75
    Q_UNUSED(object);
76

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.*/
82

83
        auto keyEvent = static_cast<QKeyEvent*>(event);
84

85
        detectKeyboardEventHandlingMode(keyEvent);  // determine the handler
86

87
        if (vpViewer && isMode(KeyboardEventHandlingMode::ViewProvider)) {
88
            return QApplication::sendEvent(vpViewer, keyEvent);
89
        }
90

91
        return false;  // do not intercept the event and feed it to the widget
92
    }
93

94
    return false;
95
}
96

97
void DrawSketchKeyboardManager::detectKeyboardEventHandlingMode(QKeyEvent* keyEvent)
98
{
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);
108
    }
109
}
110

111
void DrawSketchKeyboardManager::onTimeOut()
112
{
113
    keyMode = KeyboardEventHandlingMode::ViewProvider;
114
}
115

116
/// sets the timeout to the amount of milliseconds.
117
void DrawSketchKeyboardManager::setTimeOut(int milliseconds)
118
{
119
    timeOutValue = milliseconds;
120
}
121

122
// returns the current timeout amount
123
int DrawSketchKeyboardManager::timeOut()
124
{
125
    return timeOutValue;
126
}
127

128

129
#include "moc_DrawSketchKeyboardManager.cpp"
130

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.