FreeCAD

Форк
0
/
SoTouchEvents.cpp 
205 строк · 7.2 Кб
1
/***************************************************************************
2
 *   Copyright (c) 2015 Victor Titov (DeepSOIC) <vv.titov@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
#include "PreCompiled.h"
24

25
#include <QApplication>
26
#include <QGestureEvent>
27
#include <QWidget>
28

29
#include <Base/Exception.h>
30

31
#include "SoTouchEvents.h"
32

33

34
SO_EVENT_SOURCE(SoGestureEvent);
35

36
SbBool SoGestureEvent::isSoGestureEvent(const SoEvent *ev) const
37
{
38
    return ev->isOfType(SoGestureEvent::getClassTypeId());
39
}
40

41
//----------------------------SoGesturePanEvent--------------------------------
42

43
SO_EVENT_SOURCE(SoGesturePanEvent);
44

45
SoGesturePanEvent::SoGesturePanEvent(QPanGesture* qpan, QWidget *widget)
46
{
47
    Q_UNUSED(widget);
48
    totalOffset = SbVec2f(qpan->offset().x(), -qpan->offset().y());
49
    deltaOffset = SbVec2f(qpan->delta().x(), -qpan->delta().y());
50
    state = SbGestureState(qpan->state());
51

52
    Qt::KeyboardModifiers mods = QApplication::keyboardModifiers();
53
    this->setAltDown(mods.testFlag(Qt::AltModifier));
54
    this->setCtrlDown(mods.testFlag(Qt::ControlModifier));
55
    this->setShiftDown(mods.testFlag(Qt::ShiftModifier));
56
    this->setTime(SbTime::getTimeOfDay());
57
}
58

59
SbBool SoGesturePanEvent::isSoGesturePanEvent(const SoEvent *ev) const
60
{
61
    return ev->isOfType(SoGesturePanEvent::getClassTypeId());
62
}
63

64
//----------------------------SoGesturePinchEvent--------------------------------
65

66
SO_EVENT_SOURCE(SoGesturePinchEvent);
67

68
SoGesturePinchEvent::SoGesturePinchEvent(QPinchGesture* qpinch, QWidget *widget)
69
{
70
    int h = widget->height();
71
    QPointF widgetCorner = QPointF(widget->mapToGlobal(QPoint(0,0)));
72
    qreal scaleToWidget = (widget->mapFromGlobal(QPoint(800,800))-widget->mapFromGlobal(QPoint(0,0))).x()/800.0;
73
    QPointF pnt;//temporary
74
    pnt = qpinch->startCenterPoint();
75
    pnt = (pnt-widgetCorner)*scaleToWidget;//translate screen coord. into widget coord.
76
    startCenter = SbVec2f(pnt.x(), h - pnt.y());
77

78
    pnt = qpinch->centerPoint();
79
    pnt = (pnt-widgetCorner)*scaleToWidget;
80
    curCenter = SbVec2f(pnt.x(), h - pnt.y());
81

82
    pnt = qpinch->lastCenterPoint();
83
    pnt = (pnt-widgetCorner)*scaleToWidget;
84
    deltaCenter = curCenter - SbVec2f(pnt.x(), h - pnt.y());
85

86
    deltaZoom = qpinch->scaleFactor();
87
    totalZoom = qpinch->totalScaleFactor();
88

89
    deltaAngle = -unbranchAngle((qpinch->rotationAngle()-qpinch->lastRotationAngle()) / 180.0 * M_PI);
90
    totalAngle = -qpinch->totalRotationAngle() / 180 * M_PI;
91

92
    state = SbGestureState(qpinch->state());
93

94
    this->setPosition(SbVec2s(curCenter));
95
    Qt::KeyboardModifiers mods = QApplication::keyboardModifiers();
96
    this->setAltDown(mods.testFlag(Qt::AltModifier));
97
    this->setCtrlDown(mods.testFlag(Qt::ControlModifier));
98
    this->setShiftDown(mods.testFlag(Qt::ShiftModifier));
99
    this->setTime(SbTime::getTimeOfDay());
100
}
101

102
SbBool SoGesturePinchEvent::isSoGesturePinchEvent(const SoEvent *ev) const
103
{
104
    return ev->isOfType(SoGesturePinchEvent::getClassTypeId());
105
}
106

107
/*!
108
 * \brief SoGesturePinchEvent::unbranchAngle : utility function to bring an angle into -pi..pi region.
109
 * \param ang - in radians
110
 * \return
111
 */
112
double SoGesturePinchEvent::unbranchAngle(double ang)
113
{
114
    return ang - 2.0 * M_PI * floor((ang + M_PI) / (2.0 * M_PI));
115
}
116

117

118
//----------------------------SoGestureSwipeEvent--------------------------------
119

120
SO_EVENT_SOURCE(SoGestureSwipeEvent);
121

122
SoGestureSwipeEvent::SoGestureSwipeEvent(QSwipeGesture *qwsipe, QWidget *widget)
123
{
124
    Q_UNUSED(widget);
125
    angle = qwsipe->swipeAngle();
126
    switch (qwsipe->verticalDirection()){
127
    case QSwipeGesture::Up :
128
        vertDir = +1;
129
        break;
130
    case QSwipeGesture::Down :
131
        vertDir = -1;
132
        break;
133
    default:
134
        vertDir = 0;
135
        break;
136
    }
137
    switch (qwsipe->horizontalDirection()){
138
    case QSwipeGesture::Right :
139
        horzDir = +1;
140
        break;
141
    case QSwipeGesture::Left :
142
        horzDir = -1;
143
        break;
144
    default:
145
        horzDir = 0;
146
        break;
147
    }
148

149
    state = SbGestureState(qwsipe->state());
150

151
    Qt::KeyboardModifiers mods = QApplication::keyboardModifiers();
152
    this->setAltDown(mods.testFlag(Qt::AltModifier));
153
    this->setCtrlDown(mods.testFlag(Qt::ControlModifier));
154
    this->setShiftDown(mods.testFlag(Qt::ShiftModifier));
155
    this->setTime(SbTime::getTimeOfDay());
156
}
157

158
SbBool SoGestureSwipeEvent::isSoGestureSwipeEvent(const SoEvent *ev) const
159
{
160
    return ev->isOfType(SoGestureSwipeEvent::getClassTypeId());
161
}
162

163

164
//----------------------------GesturesDevice-------------------------------
165

166
GesturesDevice::GesturesDevice(QWidget* widget)
167
  : InputDevice(nullptr)
168
{
169
    if (SoGestureEvent::getClassTypeId().isBad()){
170
        SoGestureEvent::initClass();
171
        SoGesturePanEvent::initClass();
172
        SoGesturePinchEvent::initClass();
173
        SoGestureSwipeEvent::initClass();
174
    }
175
    if (! widget)
176
        throw Base::ValueError("Can't create a gestures quarter input device without widget (null pointer was passed).");
177
    this->widget = widget;
178
}
179

180
const SoEvent* GesturesDevice::translateEvent(QEvent* event)
181
{
182
    if (event->type() == QEvent::Gesture
183
            || event->type() == QEvent::GestureOverride) {
184
        auto gevent = static_cast<QGestureEvent*>(event);
185

186
        auto zg = static_cast<QPinchGesture*>(gevent->gesture(Qt::PinchGesture));
187
        if(zg){
188
            gevent->setAccepted(Qt::PinchGesture,true);//prefer it over pan
189
            return new SoGesturePinchEvent(zg,this->widget);
190
        }
191

192
        auto pg = static_cast<QPanGesture*>(gevent->gesture(Qt::PanGesture));
193
        if(pg){
194
            gevent->setAccepted(Qt::PanGesture,true);
195
            return new SoGesturePanEvent(pg,this->widget);
196
        }
197

198
        auto sg = static_cast<QSwipeGesture*>(gevent->gesture(Qt::SwipeGesture));
199
        if(sg){
200
            gevent->setAccepted(Qt::SwipeGesture,true);
201
            return new SoGesturePanEvent(pg,this->widget);
202
        }
203
    }
204
    return nullptr;
205
}
206

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

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

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

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