Skip to content

Commit

Permalink
new function for handling updates to timestep ui update in temporal c…
Browse files Browse the repository at this point in the history
…ontroller widget
  • Loading branch information
Samweli authored and nyalldawson committed Jan 10, 2021
1 parent 64a8623 commit 77c59ef
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
49 changes: 48 additions & 1 deletion src/gui/qgstemporalcontrollerwidget.cpp
Expand Up @@ -59,7 +59,7 @@ QgsTemporalControllerWidget::QgsTemporalControllerWidget( QWidget *parent )
return;

mBlockFrameDurationUpdates++;
setTimeStep( timeStep );
updateTimeStepInputs( timeStep );
mBlockFrameDurationUpdates--;
} );
connect( mNavigationOff, &QPushButton::clicked, this, &QgsTemporalControllerWidget::mNavigationOff_clicked );
Expand Down Expand Up @@ -534,6 +534,53 @@ void QgsTemporalControllerWidget::mRangeSetToAllLayersAction_triggered()
}

void QgsTemporalControllerWidget::setTimeStep( const QgsInterval &timeStep )
{
if ( ! timeStep.isValid() || timeStep.seconds() <= 0 )
return;

// Search the time unit the most appropriate :
// the one that gives the smallest time step value for double spin box with round value (if possible) and/or the less signifiant digits

int selectedUnit = -1;
int stringSize = std::numeric_limits<int>::max();
int precision = mStepSpinBox->decimals();
double selectedValue = std::numeric_limits<double>::max();
for ( int i = 0; i < mTimeStepsComboBox->count(); ++i )
{
QgsUnitTypes::TemporalUnit unit = static_cast<QgsUnitTypes::TemporalUnit>( mTimeStepsComboBox->itemData( i ).toInt() );
double value = timeStep.seconds() * QgsUnitTypes::fromUnitToUnitFactor( QgsUnitTypes::TemporalSeconds, unit );
QString string = QString::number( value, 'f', precision );
string.remove( QRegExp( "0+$" ) ); //remove trailing zero
string.remove( QRegExp( "[.]+$" ) ); //remove last point if present

if ( value >= 1
&& string.size() <= stringSize // less significant digit than currently selected
&& value < selectedValue ) // less than currently selected
{
selectedUnit = i;
selectedValue = value;
stringSize = string.size();
}
else if ( string != '0'
&& string.size() < precision + 2 //round value (ex: 0.xx with precision=3)
&& string.size() < stringSize ) //less significant digit than currently selected
{
selectedUnit = i ;
selectedValue = value ;
stringSize = string.size();
}
}

if ( selectedUnit >= 0 )
{
mStepSpinBox->setValue( selectedValue );
mTimeStepsComboBox->setCurrentIndex( selectedUnit );
}

updateFrameDuration();
}

void QgsTemporalControllerWidget::updateTimeStepInputs( const QgsInterval &timeStep )
{
if ( ! timeStep.isValid() || timeStep.seconds() <= 0 )
return;
Expand Down
17 changes: 17 additions & 0 deletions src/gui/qgstemporalcontrollerwidget.h
Expand Up @@ -93,6 +93,23 @@ class GUI_EXPORT QgsTemporalControllerWidget : public QgsPanelWidget, private Ui
void firstTemporalLayerLoaded( QgsMapLayer *layer );
void setTimeStep( const QgsInterval &timeStep );

/**
* Updates the widget timestep and timestep unit inputs using the passed
* interval \a timeStep original duration and original unit.
*
* If the passed interval \a timeStep has different values of original duration
* and original unit compared to the widget input values for timestep and
* timestep unit then the corresponding widget input values will be updated
* to match the \a timeStep original duration and unit.
*
* After updating the widget inputs, an update to the frame duration is executed.
*
* \note Updates will only be made if the \a timeStep is valid.
*
* \since 3.18
*/
void updateTimeStepInputs( const QgsInterval &timeStep );

private slots:

/**
Expand Down

0 comments on commit 77c59ef

Please sign in to comment.