Salome HOME
2957c247b93cd373a9652a0131438560653760e0
[modules/gui.git] / src / SUIT / SUIT_AutoRotate.cxx
1 // Copyright (C) 2007-2023  CEA, EDF, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 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 GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "SUIT_AutoRotate.h"
24 #include "SUIT_ViewWindow.h"
25
26 #include <QtGlobal>
27 #include <QDateTime>
28 #include <QMouseEvent>
29 #include <QPoint>
30
31
32
33 SUIT_AutoRotate::SUIT_AutoRotate(SUIT_ViewWindow* theWindow)
34   : QObject()
35   , myWindow(theWindow)
36 {
37   initialize();
38
39   if (myWindow) {
40     // Let's intercept all view rotation relevant signals inside this class
41     connect( myWindow, SIGNAL( vpStartRotate(int, int, qint64) ), this, SLOT( onStartRotate(int, int, qint64) ) );
42     connect( myWindow, SIGNAL( vpRotate(int, int, qint64) ),      this, SLOT( onRotate(int, int, qint64) ) );
43     connect( myWindow, SIGNAL( vpEndRotate(int, int, qint64) ),   this, SLOT( onEndRotate(int, int, qint64) ) );
44     connect( myWindow, SIGNAL( mousePressed(SUIT_ViewWindow*, QMouseEvent*) ), this, SLOT( onStopRotate() ) );
45   }
46 }
47
48
49 SUIT_AutoRotate::~SUIT_AutoRotate()
50 {
51   delete [] myLog.myPosition;
52   delete [] myLog.myTime;
53 }
54
55
56 void SUIT_AutoRotate::initialize()
57 {
58   myLog.mySize = 16;
59   myLog.myPosition = new QPoint [16];
60   myLog.myTime = new qint64 [16];
61   myLog.myHistorySize = 0;
62 }
63
64
65 void SUIT_AutoRotate::onStartRotate(int theMouseX, int theMouseY, qint64 theTime)
66 {
67   myLog.myHistorySize = 0;
68   addToLog(QPoint(theMouseX, theMouseY), theTime);
69 }
70
71
72 void SUIT_AutoRotate::onRotate(int theMouseX, int theMouseY, qint64 theTime)
73 {
74   addToLog(QPoint(theMouseX, theMouseY), theTime);
75 }
76
77
78 void SUIT_AutoRotate::onEndRotate(int theMouseX, int theMouseY, qint64 theTime)
79 {
80   startAnimation();
81 }
82
83
84 void SUIT_AutoRotate::onStopRotate()
85 {
86   stopAnimation();
87 }
88
89
90 void SUIT_AutoRotate::addToLog(const QPoint& thePos, qint64 theTime)
91 {
92   if (myLog.myHistorySize > 0 && thePos == myLog.myPosition[0]) {
93     return;
94   }
95
96   int aLastIdx = myLog.myHistorySize;
97   // If we have filled up the log, throw away the last item
98   if (aLastIdx == myLog.mySize) { aLastIdx--; }
99
100   for (int i = aLastIdx; i > 0; i--) {
101     myLog.myPosition[i] = myLog.myPosition[i-1];
102     myLog.myTime[i] = myLog.myTime[i-1];
103   }
104
105   myLog.myPosition[0] = thePos;
106   myLog.myTime[0] = theTime;
107   if (myLog.myHistorySize < myLog.mySize)
108     myLog.myHistorySize++;
109 }
110
111
112 bool SUIT_AutoRotate::startAnimation()
113 {
114   // override this method in derived classes
115 }
116
117
118 bool SUIT_AutoRotate::stopAnimation()
119 {
120   // override this method in derived classes
121 }