Skip to content

Commit

Permalink
Save the time step and frame rate settings into the project and restore
Browse files Browse the repository at this point in the history
alongside projects
  • Loading branch information
nyalldawson committed Mar 12, 2020
1 parent ac3409e commit d66175a
Show file tree
Hide file tree
Showing 5 changed files with 187 additions and 3 deletions.
58 changes: 58 additions & 0 deletions python/core/auto_generated/qgsprojecttimesettings.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,64 @@ Reads the settings's state from a DOM ``element``.
Returns a DOM element representing the settings.

.. seealso:: :py:func:`readXml`
%End

QgsUnitTypes::TemporalUnit timeStepUnit() const;
%Docstring
Returns the project's time step (length of one animation frame) unit, which is used as the default value when
animating the project.

.. seealso:: :py:func:`setTimeStepUnit`

.. seealso:: :py:func:`timeStep`
%End

void setTimeStepUnit( QgsUnitTypes::TemporalUnit unit );
%Docstring
Sets the project's time step (length of one animation frame) ``unit``, which is used as the default value when
animating the project.

.. seealso:: :py:func:`timeStepUnit`

.. seealso:: :py:func:`setTimeStep`
%End

double timeStep() const;
%Docstring
Returns the project's time step (length of one animation frame), which is used as the default value when
animating the project.

Units are specified via timeStepUnit()

.. seealso:: :py:func:`setTimeStep`

.. seealso:: :py:func:`timeStepUnit`
%End

void setTimeStep( double step );
%Docstring
Sets the project's time ``step`` (length of one animation frame), which is used as the default value when
animating the project.

Units are specified via setTimeStepUnit()

.. seealso:: :py:func:`timeStep`

.. seealso:: :py:func:`setTimeStepUnit`
%End

void setFramesPerSecond( double rate );
%Docstring
Sets the project's default animation frame ``rate``, in frames per second.

.. seealso:: :py:func:`framesPerSecond`
%End

double framesPerSecond() const;
%Docstring
Returns the project's default animation frame rate, in frames per second.

.. seealso:: :py:func:`setFramesPerSecond`
%End

signals:
Expand Down
39 changes: 39 additions & 0 deletions src/core/qgsprojecttimesettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ bool QgsProjectTimeSettings::readXml( const QDomElement &element, const QgsReadW

}

mTimeStepUnit = QgsUnitTypes::decodeTemporalUnit( element.attribute( QStringLiteral( "timeStepUnit" ), QgsUnitTypes::encodeUnit( QgsUnitTypes::TemporalHours ) ) );
mTimeStep = element.attribute( QStringLiteral( "timeStep" ), "1" ).toDouble();
mFrameRate = element.attribute( QStringLiteral( "frameRate" ), "1" ).toDouble();

return true;
}

Expand All @@ -85,5 +89,40 @@ QDomElement QgsProjectTimeSettings::writeXml( QDomDocument &document, const QgsR
element.appendChild( temporalElement );
}

element.setAttribute( QStringLiteral( "timeStepUnit" ), QgsUnitTypes::encodeUnit( mTimeStepUnit ) );
element.setAttribute( QStringLiteral( "timeStep" ), qgsDoubleToString( mTimeStep ) );
element.setAttribute( QStringLiteral( "frameRate" ), qgsDoubleToString( mFrameRate ) );

return element;
}

QgsUnitTypes::TemporalUnit QgsProjectTimeSettings::timeStepUnit() const
{
return mTimeStepUnit;
}

void QgsProjectTimeSettings::setTimeStepUnit( QgsUnitTypes::TemporalUnit unit )
{
mTimeStepUnit = unit;
}

double QgsProjectTimeSettings::timeStep() const
{
return mTimeStep;
}

void QgsProjectTimeSettings::setTimeStep( double timeStep )
{
mTimeStep = timeStep;
}

void QgsProjectTimeSettings::setFramesPerSecond( double rate )
{
mFrameRate = rate;
}

double QgsProjectTimeSettings::framesPerSecond() const
{
return mFrameRate;
}

58 changes: 58 additions & 0 deletions src/core/qgsprojecttimesettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include "qgis_core.h"
#include "qgsrange.h"
#include "qgsunittypes.h"
#include <QObject>

class QDomElement;
Expand Down Expand Up @@ -86,6 +87,60 @@ class CORE_EXPORT QgsProjectTimeSettings : public QObject
*/
QDomElement writeXml( QDomDocument &document, const QgsReadWriteContext &context ) const;

/**
* Returns the project's time step (length of one animation frame) unit, which is used as the default value when
* animating the project.
*
* \see setTimeStepUnit()
* \see timeStep()
*/
QgsUnitTypes::TemporalUnit timeStepUnit() const;

/**
* Sets the project's time step (length of one animation frame) \a unit, which is used as the default value when
* animating the project.
*
* \see timeStepUnit()
* \see setTimeStep()
*/
void setTimeStepUnit( QgsUnitTypes::TemporalUnit unit );

/**
* Returns the project's time step (length of one animation frame), which is used as the default value when
* animating the project.
*
* Units are specified via timeStepUnit()
*
* \see setTimeStep()
* \see timeStepUnit()
*/
double timeStep() const;

/**
* Sets the project's time \a step (length of one animation frame), which is used as the default value when
* animating the project.
*
* Units are specified via setTimeStepUnit()
*
* \see timeStep()
* \see setTimeStepUnit()
*/
void setTimeStep( double step );

/**
* Sets the project's default animation frame \a rate, in frames per second.
*
* \see framesPerSecond()
*/
void setFramesPerSecond( double rate );

/**
* Returns the project's default animation frame rate, in frames per second.
*
* \see setFramesPerSecond()
*/
double framesPerSecond() const;

signals:

/**
Expand All @@ -99,6 +154,9 @@ class CORE_EXPORT QgsProjectTimeSettings : public QObject
private:

QgsDateTimeRange mRange;
QgsUnitTypes::TemporalUnit mTimeStepUnit = QgsUnitTypes::TemporalHours;
double mTimeStep = 1;
double mFrameRate = 1;
};


Expand Down
30 changes: 27 additions & 3 deletions src/gui/qgstemporalcontrollerdockwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ QgsTemporalControllerDockWidget::QgsTemporalControllerDockWidget( const QString
mFastForwardButton->setToolTip( tr( "Fast forward to end" ) );

updateFrameDuration();

connect( QgsProject::instance(), &QgsProject::readProject, this, &QgsTemporalControllerDockWidget::setWidgetStateFromProject );
}

void QgsTemporalControllerDockWidget::updateTemporalExtent()
Expand All @@ -122,10 +124,29 @@ void QgsTemporalControllerDockWidget::updateTemporalExtent()

void QgsTemporalControllerDockWidget::updateFrameDuration()
{
mNavigationObject->setFrameDuration( QgsInterval( mSpinBox->value(), static_cast< QgsUnitTypes::TemporalUnit>( mTimeStepsComboBox->currentData().toInt() ) ) );
if ( mBlockSettingUpdates )
return;

// save new settings into project
QgsProject::instance()->timeSettings()->setTimeStepUnit( static_cast< QgsUnitTypes::TemporalUnit>( mTimeStepsComboBox->currentData().toInt() ) );
QgsProject::instance()->timeSettings()->setTimeStep( mSpinBox->value() );

mNavigationObject->setFrameDuration( QgsInterval( QgsProject::instance()->timeSettings()->timeStep(),
QgsProject::instance()->timeSettings()->timeStepUnit() ) );
mSlider->setRange( 0, mNavigationObject->totalFrameCount() - 1 );
}

void QgsTemporalControllerDockWidget::setWidgetStateFromProject()
{
mBlockSettingUpdates++;
mTimeStepsComboBox->setCurrentIndex( mTimeStepsComboBox->findData( QgsProject::instance()->timeSettings()->timeStepUnit() ) );
mSpinBox->setValue( QgsProject::instance()->timeSettings()->timeStep() );
mBlockSettingUpdates--;
updateFrameDuration();

mNavigationObject->setFramesPerSecond( QgsProject::instance()->timeSettings()->framesPerSecond() );
}

void QgsTemporalControllerDockWidget::updateSlider( const QgsDateTimeRange &range )
{
whileBlocking( mSlider )->setValue( mNavigationObject->currentFrameNumber() );
Expand All @@ -148,11 +169,14 @@ QgsTemporalController *QgsTemporalControllerDockWidget::temporalController()
void QgsTemporalControllerDockWidget::settings_clicked()
{
QgsTemporalMapSettingsDialog dialog( this );
dialog.mapSettingsWidget()->setFrameRateValue( mNavigationObject->framesPerSeconds() );
dialog.mapSettingsWidget()->setFrameRateValue( mNavigationObject->framesPerSecond() );

if ( dialog.exec() )
{
mNavigationObject->setFramesPerSeconds( dialog.mapSettingsWidget()->frameRateValue() );
// save new settings into project
QgsProject::instance()->timeSettings()->setFramesPerSecond( dialog.mapSettingsWidget()->frameRateValue() );

mNavigationObject->setFramesPerSecond( QgsProject::instance()->timeSettings()->framesPerSecond() );
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/gui/qgstemporalcontrollerdockwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ class GUI_EXPORT QgsTemporalControllerDockWidget : public QgsDockWidget, private
//! Handles all non gui navigation logic
QgsTemporalNavigationObject *mNavigationObject = nullptr;

int mBlockSettingUpdates = 0;

private slots:

/**
Expand Down Expand Up @@ -107,6 +109,9 @@ class GUI_EXPORT QgsTemporalControllerDockWidget : public QgsDockWidget, private
**/
void updateFrameDuration();

void setWidgetStateFromProject();


};

#endif // QGSTEMPORALCONTROLLERDOCKWIDGET_H

0 comments on commit d66175a

Please sign in to comment.