Skip to content

Commit 3c855b2

Browse files
committed
Add/remove/edit keyframes in GUI
1 parent 2f6b569 commit 3c855b2

File tree

2 files changed

+105
-1
lines changed

2 files changed

+105
-1
lines changed

src/app/3d/qgs3danimationwidget.cpp

Lines changed: 102 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
#include "qgsapplication.h"
2020
#include "qgscameracontroller.h"
2121

22-
#include <Qt3DRender/QCamera>
22+
#include <QInputDialog>
23+
#include <QMessageBox>
2324
#include <QTimer>
2425

2526
Qgs3DAnimationWidget::Qgs3DAnimationWidget( QWidget *parent )
@@ -38,6 +39,10 @@ Qgs3DAnimationWidget::Qgs3DAnimationWidget( QWidget *parent )
3839
mAnimationTimer->setInterval( 10 );
3940
connect( mAnimationTimer, &QTimer::timeout, this, &Qgs3DAnimationWidget::onAnimationTimer );
4041

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+
4146
btnPlayPause->setCheckable( true );
4247
connect( btnPlayPause, &QToolButton::clicked, this, &Qgs3DAnimationWidget::onPlayPause );
4348

@@ -175,3 +180,99 @@ void Qgs3DAnimationWidget::onKeyframeChanged()
175180
whileBlocking( sliderTime )->setValue( kf.time * 100 );
176181
mCameraController->setLookingAtPoint( kf.point, kf.dist, kf.pitch, kf.yaw );
177182
}
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+
}

src/app/3d/qgs3danimationwidget.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ class Qgs3DAnimationWidget : public QWidget, private Ui::Animation3DWidget
4747
void onSliderValueChanged();
4848
void onCameraChanged();
4949
void onKeyframeChanged();
50+
void onAddKeyframe();
51+
void onRemoveKeyframe();
52+
void onEditKeyframe();
5053

5154
private:
5255
void initializeController( const Qgs3DAnimationSettings &animSettings );

0 commit comments

Comments
 (0)