Skip to content
Permalink
Browse files

A button for duplication of keyframes

  • Loading branch information
wonder-sk committed Jul 8, 2018
1 parent dd8424c commit da5f354434c04fd7357bcebbb9946d7fcfe11522
Showing with 92 additions and 29 deletions.
  1. +61 −29 src/app/3d/qgs3danimationwidget.cpp
  2. +3 −0 src/app/3d/qgs3danimationwidget.h
  3. +28 −0 src/ui/3d/animation3dwidget.ui
@@ -32,6 +32,7 @@ Qgs3DAnimationWidget::Qgs3DAnimationWidget( QWidget *parent )
btnRemoveKeyframe->setIcon( QIcon( QgsApplication::iconPath( "symbologyRemove.svg" ) ) );
btnEditKeyframe->setIcon( QIcon( QgsApplication::iconPath( "symbologyEdit.svg" ) ) );
btnPlayPause->setIcon( QIcon( QgsApplication::iconPath( "mTaskRunning.svg" ) ) );
btnDuplicateKeyframe->setIcon( QIcon( QgsApplication::iconPath( "mActionEditCopy.svg" ) ) );

cboKeyframe->addItem( tr( "<none>" ) );

@@ -42,6 +43,7 @@ Qgs3DAnimationWidget::Qgs3DAnimationWidget( QWidget *parent )
connect( btnAddKeyframe, &QToolButton::clicked, this, &Qgs3DAnimationWidget::onAddKeyframe );
connect( btnRemoveKeyframe, &QToolButton::clicked, this, &Qgs3DAnimationWidget::onRemoveKeyframe );
connect( btnEditKeyframe, &QToolButton::clicked, this, &Qgs3DAnimationWidget::onEditKeyframe );
connect( btnDuplicateKeyframe, &QToolButton::clicked, this, &Qgs3DAnimationWidget::onDuplicateKeyframe );

btnPlayPause->setCheckable( true );
connect( btnPlayPause, &QToolButton::clicked, this, &Qgs3DAnimationWidget::onPlayPause );
@@ -181,28 +183,48 @@ void Qgs3DAnimationWidget::onKeyframeChanged()
mCameraController->setLookingAtPoint( kf.point, kf.dist, kf.pitch, kf.yaw );
}

int Qgs3DAnimationWidget::findIndexForKeyframe( float time )
{
int newIndex = 0;
for ( const Qgs3DAnimationSettings::Keyframe &keyframe : mAnimationSettings->keyFrames() )
{
if ( keyframe.time > time )
break;
newIndex++;
}
return newIndex;
}

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

// 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;
QMessageBox::warning( this, tr( "Keyframe time" ), tr( "There is already a keyframe at the given time" ) );
*ok = false;
return 0;
}
if ( keyframe.time > t )
break;
index++;
}

*ok = true;
return t;
}

void Qgs3DAnimationWidget::onAddKeyframe()
{
bool ok;
float t = askForKeyframeTime( sliderTime->value() / 100., &ok );
if ( !ok )
return;

int index = findIndexForKeyframe( t );

Qgs3DAnimationSettings::Keyframe kf;
kf.time = t;
kf.point = mCameraController->lookingAtPoint();
@@ -239,33 +261,43 @@ void Qgs3DAnimationWidget::onEditKeyframe()
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 );
float t = askForKeyframeTime( kf.time, &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++;
}
int newIndex = findIndexForKeyframe( t );

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 );
}

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

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

bool ok;
float t = askForKeyframeTime( kf.time, &ok );
if ( !ok )
return;

// figure out position of this keyframe
int newIndex = findIndexForKeyframe( t );

kf.time = t;

@@ -50,9 +50,12 @@ class Qgs3DAnimationWidget : public QWidget, private Ui::Animation3DWidget
void onAddKeyframe();
void onRemoveKeyframe();
void onEditKeyframe();
void onDuplicateKeyframe();

private:
void initializeController( const Qgs3DAnimationSettings &animSettings );
float askForKeyframeTime( float defaultTime, bool *ok );
int findIndexForKeyframe( float time );

private:
std::unique_ptr<Qgs3DAnimationSettings> mAnimationSettings;
@@ -28,20 +28,39 @@
</item>
<item>
<widget class="QToolButton" name="btnAddKeyframe">
<property name="toolTip">
<string>Add keyframe</string>
</property>
<property name="text">
<string> + </string>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="btnRemoveKeyframe">
<property name="toolTip">
<string>Remove keyframe</string>
</property>
<property name="text">
<string> - </string>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="btnEditKeyframe">
<property name="toolTip">
<string>Edit keyframe</string>
</property>
<property name="text">
<string>...</string>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="btnDuplicateKeyframe">
<property name="toolTip">
<string>Duplicate keyframe</string>
</property>
<property name="text">
<string>...</string>
</property>
@@ -91,6 +110,15 @@
</item>
</layout>
</widget>
<tabstops>
<tabstop>cboKeyframe</tabstop>
<tabstop>btnAddKeyframe</tabstop>
<tabstop>btnRemoveKeyframe</tabstop>
<tabstop>btnEditKeyframe</tabstop>
<tabstop>btnDuplicateKeyframe</tabstop>
<tabstop>btnPlayPause</tabstop>
<tabstop>sliderTime</tabstop>
</tabstops>
<resources/>
<connections/>
</ui>

0 comments on commit da5f354

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