Skip to content
Permalink
Browse files

set time step when first loading meshlayer

  • Loading branch information
vcloarec authored and nyalldawson committed Jun 7, 2020
1 parent 2f74596 commit 15132730afe254d5ce536ac487ed1aea83889638
@@ -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

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

@@ -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;
}
@@ -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
@@ -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 )
@@ -21,6 +21,7 @@
#include <memory>

#include "qgis_core.h"
#include "qgsinterval.h"
#include "qgsmaplayer.h"
#include "qgsmeshdataprovider.h"
#include "qgsmeshrenderersettings.h"
@@ -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:

/**
@@ -24,6 +24,7 @@
#include "qgstemporalmapsettingswidget.h"
#include "qgstemporalutils.h"
#include "qgsmaplayertemporalproperties.h"
#include "qgsmeshlayer.h"

#include <QAction>
#include <QMenu>
@@ -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;
@@ -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 )
@@ -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();
@@ -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:

/**

0 comments on commit 1513273

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