Skip to content

Commit

Permalink
set time step when first loading meshlayer
Browse files Browse the repository at this point in the history
  • Loading branch information
vcloarec authored and nyalldawson committed Jun 7, 2020
1 parent 2f74596 commit 1513273
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,13 @@ Returns the relative time in milliseconds of the dataset
void clear();
%Docstring
Clears alls stored reference times and dataset times
%End

qint64 firstTimeStepDuration( int group ) const;
%Docstring
Returns the duration of the first time step of the dataset group with index ``group``

The value is -1 if the dataset group is not present or if it contains only one dataset (non temporal dataset)
%End

};
Expand Down
7 changes: 7 additions & 0 deletions python/core/auto_generated/mesh/qgsmeshlayer.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,13 @@ Doesn't take ownership of the pointed item, the root item is cloned.
%Docstring
Reset the dataset group tree item to default from provider

.. versionadded:: 3.14
%End

QgsInterval firstValidTimeStep() const;
%Docstring
Return the first valid time step of the dataset groups, invalid QgInterval if no time step is present

.. versionadded:: 3.14
%End

Expand Down
12 changes: 12 additions & 0 deletions src/core/mesh/qgsmeshdataprovidertemporalcapabilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,15 @@ void QgsMeshDataProviderTemporalCapabilities::clear()
mGroupsReferenceDateTime.clear();
mDatasetTimeSinceGroupReference.clear();
}

qint64 QgsMeshDataProviderTemporalCapabilities::firstTimeStepDuration( int group ) const
{
qint64 ret = -1;
if ( mDatasetTimeSinceGroupReference.contains( group ) )
{
const QList<qint64> times = mDatasetTimeSinceGroupReference[group];
if ( times.count() > 1 )
ret = times.at( 1 ) - times.at( 0 );
}
return ret;
}
7 changes: 7 additions & 0 deletions src/core/mesh/qgsmeshdataprovidertemporalcapabilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,13 @@ class CORE_EXPORT QgsMeshDataProviderTemporalCapabilities: public QgsDataProvide
*/
void clear();

/**
* Returns the duration of the first time step of the dataset group with index \a group
*
* The value is -1 if the dataset group is not present or if it contains only one dataset (non temporal dataset)
*/
qint64 firstTimeStepDuration( int group ) const;

private:

//! Holds the global reference date/time value if exists (min of all groups), otherwise it is invalid
Expand Down
15 changes: 15 additions & 0 deletions src/core/mesh/qgsmeshlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,21 @@ void QgsMeshLayer::resetDatasetGroupTreeItem()
updateActiveDatasetGroups();
}

QgsInterval QgsMeshLayer::firstValidTimeStep() const
{
if ( !mDataProvider )
return QgsInterval();
int groupCount = mDataProvider->datasetGroupCount();
for ( int i = 0; i < groupCount; ++i )
{
qint64 timeStep = mDataProvider->temporalCapabilities()->firstTimeStepDuration( i );
if ( timeStep > 0 )
return QgsInterval( timeStep, QgsUnitTypes::TemporalMilliseconds );
}

return QgsInterval();
}

void QgsMeshLayer::updateActiveDatasetGroups()
{
if ( !mDatasetGroupTreeRootItem )
Expand Down
8 changes: 8 additions & 0 deletions src/core/mesh/qgsmeshlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <memory>

#include "qgis_core.h"
#include "qgsinterval.h"
#include "qgsmaplayer.h"
#include "qgsmeshdataprovider.h"
#include "qgsmeshrenderersettings.h"
Expand Down Expand Up @@ -454,6 +455,13 @@ class CORE_EXPORT QgsMeshLayer : public QgsMapLayer
*/
void resetDatasetGroupTreeItem();

/**
* Return the first valid time step of the dataset groups, invalid QgInterval if no time step is present
*
* \since QGIS 3.14
*/
QgsInterval firstValidTimeStep() const;

public slots:

/**
Expand Down
48 changes: 43 additions & 5 deletions src/gui/qgstemporalcontrollerwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "qgstemporalmapsettingswidget.h"
#include "qgstemporalutils.h"
#include "qgsmaplayertemporalproperties.h"
#include "qgsmeshlayer.h"

#include <QAction>
#include <QMenu>
Expand Down Expand Up @@ -389,19 +390,26 @@ void QgsTemporalControllerWidget::onLayersAdded( const QList<QgsMapLayer *> &lay
if ( layer->isValid() && layer->temporalProperties()->isActive() && !mHasTemporalLayersLoaded )
{
mHasTemporalLayersLoaded = true;
// if we are moving from zero temporal layers to non-zero temporal layers, let's set temporal extent
this->setDatesToProjectTime();
firstTemporalLayerLoaded( layer );
}
} );
}

firstTemporalLayerLoaded( layer );
}
}

if ( mHasTemporalLayersLoaded )
setDatesToProjectTime();
}
}

void QgsTemporalControllerWidget::firstTemporalLayerLoaded( QgsMapLayer *layer )
{
setDatesToProjectTime();

QgsMeshLayer *meshLayer = qobject_cast<QgsMeshLayer *>( layer );
if ( meshLayer )
setTimeStep( meshLayer->firstValidTimeStep() );
}

void QgsTemporalControllerWidget::onProjectCleared()
{
mHasTemporalLayersLoaded = false;
Expand All @@ -414,6 +422,8 @@ void QgsTemporalControllerWidget::onProjectCleared()
whileBlocking( mFixedRangeStartDateTime )->setDateTime( QDateTime( QDate::currentDate(), QTime( 0, 0, 0, Qt::UTC ) ) );
whileBlocking( mFixedRangeEndDateTime )->setDateTime( mStartDateTime->dateTime() );
updateTemporalExtent();
mTimeStepsComboBox->setCurrentIndex( mTimeStepsComboBox->findData( QgsUnitTypes::TemporalHours ) );
mStepSpinBox->setValue( 1 );
}

void QgsTemporalControllerWidget::updateSlider( const QgsDateTimeRange &range )
Expand Down Expand Up @@ -498,6 +508,34 @@ void QgsTemporalControllerWidget::mRangeSetToAllLayersAction_triggered()
saveRangeToProject();
}

void QgsTemporalControllerWidget::setTimeStep( const QgsInterval &timeStep )
{
if ( ! timeStep.isValid() || timeStep.seconds() <= 0 )
return;
int indexUnit = -1;
double value = std::numeric_limits<double>::min();
double previousValue;
do
{
indexUnit++;
previousValue = value;
QgsUnitTypes::TemporalUnit unit = static_cast<QgsUnitTypes::TemporalUnit>( mTimeStepsComboBox->itemData( indexUnit ).toInt() );
value = timeStep.seconds() * QgsUnitTypes::fromUnitToUnitFactor( QgsUnitTypes::TemporalSeconds, unit );
}
while ( value > 10 || indexUnit == mTimeStepsComboBox->count() );

if ( fabs( log( value ) ) > fabs( log( previousValue ) ) )
{
indexUnit--;
value = previousValue;
}

mTimeStepsComboBox->setCurrentIndex( indexUnit );
mStepSpinBox->setValue( value );

updateFrameDuration();
}

void QgsTemporalControllerWidget::mRangeSetToProjectAction_triggered()
{
setDatesToProjectTime();
Expand Down
3 changes: 3 additions & 0 deletions src/gui/qgstemporalcontrollerwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ class GUI_EXPORT QgsTemporalControllerWidget : public QgsPanelWidget, private Ui
std::unique_ptr< QMenu > mRangeLayersSubMenu;
QgsMapLayerModel *mMapLayerModel = nullptr;

void firstTemporalLayerLoaded( QgsMapLayer *layer );
void setTimeStep( const QgsInterval &timeStep );

private slots:

/**
Expand Down

0 comments on commit 1513273

Please sign in to comment.