Skip to content
Permalink
Browse files

Add/remove/edit keyframes in GUI

  • Loading branch information
wonder-sk committed Jul 8, 2018
1 parent 2f6b569 commit 3c855b29eff99d9a62df7a326860eb87763c4339
Showing with 105 additions and 1 deletion.
  1. +102 −1 src/app/3d/qgs3danimationwidget.cpp
  2. +3 −0 src/app/3d/qgs3danimationwidget.h
@@ -19,7 +19,8 @@
#include "qgsapplication.h"
#include "qgscameracontroller.h"

#include <Qt3DRender/QCamera>
#include <QInputDialog>
#include <QMessageBox>
#include <QTimer>

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

connect( btnAddKeyframe, &QToolButton::clicked, this, &Qgs3DAnimationWidget::onAddKeyframe );
connect( btnRemoveKeyframe, &QToolButton::clicked, this, &Qgs3DAnimationWidget::onRemoveKeyframe );
connect( btnEditKeyframe, &QToolButton::clicked, this, &Qgs3DAnimationWidget::onEditKeyframe );

btnPlayPause->setCheckable( true );
connect( btnPlayPause, &QToolButton::clicked, this, &Qgs3DAnimationWidget::onPlayPause );

@@ -175,3 +180,99 @@ void Qgs3DAnimationWidget::onKeyframeChanged()
whileBlocking( sliderTime )->setValue( kf.time * 100 );
mCameraController->setLookingAtPoint( kf.point, kf.dist, kf.pitch, kf.yaw );
}


void Qgs3DAnimationWidget::onAddKeyframe()
{
bool ok;
double t = QInputDialog::getDouble( this, tr( "Add keyframe" ), tr( "Keyframe time [seconds]:" ), sliderTime->value() / 100., 0, 9999, 2, &ok );
if ( !ok )
return;

// figure out position of this keyframe
int index = 0;
for ( const Qgs3DAnimationSettings::Keyframe &keyframe : mAnimationSettings->keyFrames() )
{
if ( keyframe.time == t )
{
QMessageBox::warning( this, tr( "Add keyframe" ), tr( "There is already a keyframe at the given time" ) );
return;
}
if ( keyframe.time > t )
break;
index++;
}

Qgs3DAnimationSettings::Keyframe kf;
kf.time = t;
kf.point = mCameraController->lookingAtPoint();
kf.dist = mCameraController->distance();
kf.pitch = mCameraController->pitch();
kf.yaw = mCameraController->yaw();

cboKeyframe->insertItem( index + 1, QString( "%1 s" ).arg( kf.time ) );
cboKeyframe->setItemData( index + 1, QVariant::fromValue<Qgs3DAnimationSettings::Keyframe>( kf ), Qt::UserRole + 1 );

initializeController( animation() );

cboKeyframe->setCurrentIndex( index + 1 );
}

void Qgs3DAnimationWidget::onRemoveKeyframe()
{
int index = cboKeyframe->currentIndex();
if ( index <= 0 )
return;

cboKeyframe->setCurrentIndex( 0 );
cboKeyframe->removeItem( index );

initializeController( animation() );
}

void Qgs3DAnimationWidget::onEditKeyframe()
{
int index = cboKeyframe->currentIndex();
if ( index <= 0 )
return;

Qgs3DAnimationSettings::Keyframe kf = cboKeyframe->itemData( index, Qt::UserRole + 1 ).value<Qgs3DAnimationSettings::Keyframe>();

bool ok;
double t = QInputDialog::getDouble( this, tr( "Edit keyframe" ), tr( "Keyframe time [seconds]:" ), kf.time, 0, 9999, 2, &ok );
if ( !ok )
return;

// figure out position of this keyframe
for ( const Qgs3DAnimationSettings::Keyframe &keyframe : mAnimationSettings->keyFrames() )
{
if ( keyframe.time == t )
{
QMessageBox::warning( this, tr( "Edit keyframe" ), tr( "There is already a keyframe at the given time" ) );
return;
}
}

cboKeyframe->setCurrentIndex( 0 );
cboKeyframe->removeItem( index );

initializeController( animation() );

// figure out position of this keyframe
int newIndex = 0;
for ( const Qgs3DAnimationSettings::Keyframe &keyframe : mAnimationSettings->keyFrames() )
{
if ( keyframe.time > t )
break;
newIndex++;
}

kf.time = t;

cboKeyframe->insertItem( newIndex + 1, QString( "%1 s" ).arg( kf.time ) );
cboKeyframe->setItemData( newIndex + 1, QVariant::fromValue<Qgs3DAnimationSettings::Keyframe>( kf ), Qt::UserRole + 1 );

initializeController( animation() );

cboKeyframe->setCurrentIndex( newIndex + 1 );
}
@@ -47,6 +47,9 @@ class Qgs3DAnimationWidget : public QWidget, private Ui::Animation3DWidget
void onSliderValueChanged();
void onCameraChanged();
void onKeyframeChanged();
void onAddKeyframe();
void onRemoveKeyframe();
void onEditKeyframe();

private:
void initializeController( const Qgs3DAnimationSettings &animSettings );

0 comments on commit 3c855b2

Please sign in to comment.
You can’t perform that action at this time.