Radium Engine  1.5.20
Loading...
Searching...
No Matches
TimelineScrollArea.cpp
1#include <Gui/Timeline/TimelineScrollArea.hpp>
2
3#include <cmath>
4
5#include <QScrollBar>
6#include <QWheelEvent>
7
8#include <Gui/Timeline/Timeline.hpp>
9#include <Gui/Timeline/TimelineFrameSelector.hpp>
10
11namespace Ra::Gui {
12
13TimelineScrollArea::TimelineScrollArea( QWidget* parent ) : QScrollArea( parent ) {
14 horizontalScrollBar()->setStyleSheet( "\
15 QScrollBar:horizontal {\
16 background: transparent;\
17 }\
18 \
19 QScrollBar::handle:horizontal {\
20 background: white;\
21 }\
22 \
23 QScrollBar::add-line:horizontal {\
24 border: none;\
25 background: transparent;\
26 }\
27 \
28 QScrollBar::sub-line:horizontal {\
29 border: none;\
30 background: transparent;\
31 }" );
32}
33
34int TimelineScrollArea::getNbInterval() {
35 return m_nbInterval;
36}
37
38Scalar TimelineScrollArea::getMaxDuration() {
39 return m_maxDuration;
40}
41
42void TimelineScrollArea::setMaxDuration( Scalar duration ) {
43 m_maxDuration = duration;
44}
45
46int TimelineScrollArea::getZero() {
47 return m_zero;
48}
49
50Scalar TimelineScrollArea::getPixPerSec() {
51 return m_pixPerSec;
52}
53
54Scalar TimelineScrollArea::getStep() {
55 return m_step;
56}
57
58void TimelineScrollArea::onDrawRuler( int width ) {
59 int iStep = 0;
60 while ( iStep < s_nbSteps && width * s_steps[iStep] < 50 * m_maxDuration )
61 iStep++;
62
63 if ( iStep == s_nbSteps ) { return; }
64
65 m_step = s_steps[iStep];
66 emit stepChanged( m_step );
67
68 m_nbInterval = int( std::ceil( m_maxDuration / m_step ) ) + 2;
69 m_pixPerSec = ( Scalar( width ) / m_nbInterval ) / m_step;
70 m_zero = int( m_pixPerSec * m_step );
71 widget()->setMinimumWidth( width );
72
73 update();
74}
75
76void TimelineScrollArea::keyPressEvent( QKeyEvent* event ) {
77 switch ( event->key() ) {
78 case Qt::Key_Space:
79 emit togglePlayPause();
80 break;
81
82 case Qt::Key_Delete:
83 emit removeKeyFrame();
84 break;
85
86 case Qt::Key_I:
87 if ( event->modifiers() & Qt::Modifier::SHIFT ) { emit removeKeyFrame(); }
88 else { emit addKeyFrame(); }
89 break;
90
91 case Qt::Key_Left:
92 emit previousKeyFrame();
93 break;
94
95 case Qt::Key_Right:
96 emit nextKeyFrame();
97 break;
98
99 case Qt::Key_Up:
100 emit durationIncrement();
101 break;
102
103 case Qt::Key_Down:
104 emit durationDecrement();
105 break;
106
107 case Qt::Key_Z:
108 if ( event->modifiers() & Qt::Modifier::CTRL ) {
109 if ( event->modifiers() & Qt::Modifier::SHIFT ) { emit redo(); }
110 else { emit undo(); }
111 }
112 break;
113
114 case Qt::Key_U:
115 emit undo();
116 break;
117
118 case Qt::Key_R:
119 emit redo();
120 break;
121 default:
122 break;
123 }
124}
125
126void TimelineScrollArea::wheelEvent( QWheelEvent* event ) {
127 int ry = event->angleDelta().ry();
128 bool ctrlDown = event->modifiers() & Qt::Modifier::CTRL;
129 bool shiftDown = event->modifiers() & Qt::Modifier::SHIFT;
130 // next/previous KeyFrame
131 if ( shiftDown ) {
132 if ( ry > 0 ) { emit nextKeyFrame(); }
133 else { emit previousKeyFrame(); }
134 }
135 // scroll left/right bar
136 else if ( ctrlDown ) {
137 horizontalScrollBar()->setValue(
138 int( horizontalScrollBar()->value() + ry * TIMELINE_SLIDE_SPEED ) );
139 }
140 // zoom in/out
141 else {
142 int newRulerWidth = int( widget()->minimumWidth() +
143 ry * TIMELINE_ZOOM_SPEED * widget()->minimumWidth() / width() );
144 if ( newRulerWidth <= width() - 2 ) {
145 if ( widget()->minimumWidth() == width() - 2 ) { return; }
146 else { newRulerWidth = width() - 2; }
147 }
148
149 double hScroll = horizontalScrollBar()->value();
150
151 double x = event->position().x();
152
153 double time = ( hScroll + x - double( m_zero ) ) / double( m_pixPerSec );
154
155 onDrawRuler( newRulerWidth );
156
157 // ruler values may have changed
158 double a = time * double( m_pixPerSec ) + double( m_zero );
159 double hScrollAfterProjection = a - x;
160 horizontalScrollBar()->setValue( static_cast<int>( hScrollAfterProjection ) );
161 }
162 event->accept(); // parent is animTimeline (root) with non event catching
163}
164
165void TimelineScrollArea::mousePressEvent( QMouseEvent* event ) {
166 if ( event->button() == Qt::MiddleButton ) {
167 setCursor( Qt::SplitHCursor );
168 m_mousePosX = event->x();
169 m_sliderPos = horizontalScrollBar()->value();
170 }
171}
172
173void TimelineScrollArea::mouseReleaseEvent( QMouseEvent* event ) {
174 if ( event->button() == Qt::MiddleButton ) { setCursor( Qt::ArrowCursor ); }
175}
176
177void TimelineScrollArea::mouseMoveEvent( QMouseEvent* event ) {
178 if ( event->buttons() & Qt::MiddleButton ) {
179 horizontalScrollBar()->setValue( ( m_sliderPos + m_mousePosX - event->x() ) );
180 }
181}
182
183} // namespace Ra::Gui
T ceil(T... args)
T time(T... args)