19
19
#include " qgsapplication.h"
20
20
#include " qgscameracontroller.h"
21
21
22
- #include < Qt3DRender/QCamera>
22
+ #include < QInputDialog>
23
+ #include < QMessageBox>
23
24
#include < QTimer>
24
25
25
26
Qgs3DAnimationWidget::Qgs3DAnimationWidget ( QWidget *parent )
@@ -38,6 +39,10 @@ Qgs3DAnimationWidget::Qgs3DAnimationWidget( QWidget *parent )
38
39
mAnimationTimer ->setInterval ( 10 );
39
40
connect ( mAnimationTimer , &QTimer::timeout, this , &Qgs3DAnimationWidget::onAnimationTimer );
40
41
42
+ connect ( btnAddKeyframe, &QToolButton::clicked, this , &Qgs3DAnimationWidget::onAddKeyframe );
43
+ connect ( btnRemoveKeyframe, &QToolButton::clicked, this , &Qgs3DAnimationWidget::onRemoveKeyframe );
44
+ connect ( btnEditKeyframe, &QToolButton::clicked, this , &Qgs3DAnimationWidget::onEditKeyframe );
45
+
41
46
btnPlayPause->setCheckable ( true );
42
47
connect ( btnPlayPause, &QToolButton::clicked, this , &Qgs3DAnimationWidget::onPlayPause );
43
48
@@ -175,3 +180,99 @@ void Qgs3DAnimationWidget::onKeyframeChanged()
175
180
whileBlocking ( sliderTime )->setValue ( kf.time * 100 );
176
181
mCameraController ->setLookingAtPoint ( kf.point , kf.dist , kf.pitch , kf.yaw );
177
182
}
183
+
184
+
185
+ void Qgs3DAnimationWidget::onAddKeyframe ()
186
+ {
187
+ bool ok;
188
+ double t = QInputDialog::getDouble ( this , tr ( " Add keyframe" ), tr ( " Keyframe time [seconds]:" ), sliderTime->value () / 100 ., 0 , 9999 , 2 , &ok );
189
+ if ( !ok )
190
+ return ;
191
+
192
+ // figure out position of this keyframe
193
+ int index = 0 ;
194
+ for ( const Qgs3DAnimationSettings::Keyframe &keyframe : mAnimationSettings ->keyFrames () )
195
+ {
196
+ if ( keyframe.time == t )
197
+ {
198
+ QMessageBox::warning ( this , tr ( " Add keyframe" ), tr ( " There is already a keyframe at the given time" ) );
199
+ return ;
200
+ }
201
+ if ( keyframe.time > t )
202
+ break ;
203
+ index++;
204
+ }
205
+
206
+ Qgs3DAnimationSettings::Keyframe kf;
207
+ kf.time = t;
208
+ kf.point = mCameraController ->lookingAtPoint ();
209
+ kf.dist = mCameraController ->distance ();
210
+ kf.pitch = mCameraController ->pitch ();
211
+ kf.yaw = mCameraController ->yaw ();
212
+
213
+ cboKeyframe->insertItem ( index + 1 , QString ( " %1 s" ).arg ( kf.time ) );
214
+ cboKeyframe->setItemData ( index + 1 , QVariant::fromValue<Qgs3DAnimationSettings::Keyframe>( kf ), Qt::UserRole + 1 );
215
+
216
+ initializeController ( animation () );
217
+
218
+ cboKeyframe->setCurrentIndex ( index + 1 );
219
+ }
220
+
221
+ void Qgs3DAnimationWidget::onRemoveKeyframe ()
222
+ {
223
+ int index = cboKeyframe->currentIndex ();
224
+ if ( index <= 0 )
225
+ return ;
226
+
227
+ cboKeyframe->setCurrentIndex ( 0 );
228
+ cboKeyframe->removeItem ( index );
229
+
230
+ initializeController ( animation () );
231
+ }
232
+
233
+ void Qgs3DAnimationWidget::onEditKeyframe ()
234
+ {
235
+ int index = cboKeyframe->currentIndex ();
236
+ if ( index <= 0 )
237
+ return ;
238
+
239
+ Qgs3DAnimationSettings::Keyframe kf = cboKeyframe->itemData ( index, Qt::UserRole + 1 ).value <Qgs3DAnimationSettings::Keyframe>();
240
+
241
+ bool ok;
242
+ double t = QInputDialog::getDouble ( this , tr ( " Edit keyframe" ), tr ( " Keyframe time [seconds]:" ), kf.time , 0 , 9999 , 2 , &ok );
243
+ if ( !ok )
244
+ return ;
245
+
246
+ // figure out position of this keyframe
247
+ for ( const Qgs3DAnimationSettings::Keyframe &keyframe : mAnimationSettings ->keyFrames () )
248
+ {
249
+ if ( keyframe.time == t )
250
+ {
251
+ QMessageBox::warning ( this , tr ( " Edit keyframe" ), tr ( " There is already a keyframe at the given time" ) );
252
+ return ;
253
+ }
254
+ }
255
+
256
+ cboKeyframe->setCurrentIndex ( 0 );
257
+ cboKeyframe->removeItem ( index );
258
+
259
+ initializeController ( animation () );
260
+
261
+ // figure out position of this keyframe
262
+ int newIndex = 0 ;
263
+ for ( const Qgs3DAnimationSettings::Keyframe &keyframe : mAnimationSettings ->keyFrames () )
264
+ {
265
+ if ( keyframe.time > t )
266
+ break ;
267
+ newIndex++;
268
+ }
269
+
270
+ kf.time = t;
271
+
272
+ cboKeyframe->insertItem ( newIndex + 1 , QString ( " %1 s" ).arg ( kf.time ) );
273
+ cboKeyframe->setItemData ( newIndex + 1 , QVariant::fromValue<Qgs3DAnimationSettings::Keyframe>( kf ), Qt::UserRole + 1 );
274
+
275
+ initializeController ( animation () );
276
+
277
+ cboKeyframe->setCurrentIndex ( newIndex + 1 );
278
+ }
0 commit comments