From a35b0c87e415be5da58f4f75769c1dc84b75ac2e Mon Sep 17 00:00:00 2001 From: Jon Jenssen Date: Wed, 13 Sep 2023 16:56:08 +0200 Subject: [PATCH] Simplify options for time step filter --- .../Faults/RimFaultReactivationModel.cpp | 52 +--------------- .../ProjectDataModel/RimTimeStepFilter.cpp | 60 +++++++++++++++++++ .../ProjectDataModel/RimTimeStepFilter.h | 6 ++ 3 files changed, 67 insertions(+), 51 deletions(-) diff --git a/ApplicationLibCode/ProjectDataModel/Faults/RimFaultReactivationModel.cpp b/ApplicationLibCode/ProjectDataModel/Faults/RimFaultReactivationModel.cpp index 1b0dee9db2..b8a1872866 100644 --- a/ApplicationLibCode/ProjectDataModel/Faults/RimFaultReactivationModel.cpp +++ b/ApplicationLibCode/ProjectDataModel/Faults/RimFaultReactivationModel.cpp @@ -316,57 +316,7 @@ QList RimFaultReactivationModel::calculateValueOptions( } else if ( fieldNeedingOptions == &m_selectedTimeSteps ) { - if ( m_availableTimeSteps.empty() ) - { - return options; - } - - std::set currentlySelectedTimeSteps( m_selectedTimeSteps().begin(), m_selectedTimeSteps().end() ); - std::set currentlySelectedTimeStepIndices; - std::vector allDateTimes; - for ( auto& dateTime : m_availableTimeSteps ) - { - if ( currentlySelectedTimeSteps.count( dateTime ) ) - { - currentlySelectedTimeStepIndices.insert( (int)allDateTimes.size() ); - } - allDateTimes.push_back( dateTime ); - } - - std::vector filteredTimeStepIndices = - RimTimeStepFilter::filteredTimeStepIndices( allDateTimes, 0, (int)allDateTimes.size() - 1, m_timeStepFilter(), 1 ); - - // Add existing time steps to list of options to avoid removing them when changing filter. - filteredTimeStepIndices.insert( filteredTimeStepIndices.end(), - currentlySelectedTimeStepIndices.begin(), - currentlySelectedTimeStepIndices.end() ); - std::sort( filteredTimeStepIndices.begin(), filteredTimeStepIndices.end() ); - filteredTimeStepIndices.erase( std::unique( filteredTimeStepIndices.begin(), filteredTimeStepIndices.end() ), - filteredTimeStepIndices.end() ); - - QString dateFormatString = RiaQDateTimeTools::dateFormatString( RiaPreferences::current()->dateFormat(), - RiaDefines::DateFormatComponents::DATE_FORMAT_YEAR_MONTH_DAY ); - QString timeFormatString = RiaQDateTimeTools::timeFormatString( RiaPreferences::current()->timeFormat(), - RiaDefines::TimeFormatComponents::TIME_FORMAT_HOUR_MINUTE ); - QString dateTimeFormatString = QString( "%1 %2" ).arg( dateFormatString ).arg( timeFormatString ); - - bool showTime = m_timeStepFilter() == RimTimeStepFilter::TS_ALL || m_timeStepFilter() == RimTimeStepFilter::TS_INTERVAL_DAYS; - - for ( auto timeStepIndex : filteredTimeStepIndices ) - { - QDateTime dateTime = allDateTimes[timeStepIndex]; - - if ( showTime && dateTime.time() != QTime( 0, 0, 0 ) ) - { - options.push_back( - caf::PdmOptionItemInfo( RiaQDateTimeTools::toStringUsingApplicationLocale( dateTime, dateTimeFormatString ), dateTime ) ); - } - else - { - options.push_back( - caf::PdmOptionItemInfo( RiaQDateTimeTools::toStringUsingApplicationLocale( dateTime, dateFormatString ), dateTime ) ); - } - } + RimTimeStepFilter::timeStepOptions( options, &m_selectedTimeSteps, m_availableTimeSteps, m_selectedTimeSteps, m_timeStepFilter() ); } return options; diff --git a/ApplicationLibCode/ProjectDataModel/RimTimeStepFilter.cpp b/ApplicationLibCode/ProjectDataModel/RimTimeStepFilter.cpp index 104c337997..9a5a53908d 100644 --- a/ApplicationLibCode/ProjectDataModel/RimTimeStepFilter.cpp +++ b/ApplicationLibCode/ProjectDataModel/RimTimeStepFilter.cpp @@ -18,6 +18,7 @@ #include "RimTimeStepFilter.h" +#include "RiaPreferences.h" #include "RiaQDateTimeTools.h" #include "RifReaderEclipseOutput.h" @@ -479,3 +480,62 @@ bool RimTimeStepFilter::readOnlyLastFrame() const { return m_readOnlyLastFrame; } + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +void RimTimeStepFilter::timeStepOptions( QList& options, + const caf::PdmFieldHandle* timestepField, + std::vector availableTimeSteps, + std::vector selectedTimeSteps, + RimTimeStepFilter::TimeStepFilterTypeEnum filterType ) +{ + if ( availableTimeSteps.empty() ) return; + + std::set currentlySelectedTimeSteps( selectedTimeSteps.begin(), selectedTimeSteps.end() ); + std::set currentlySelectedTimeStepIndices; + std::vector allDateTimes; + for ( auto& dateTime : availableTimeSteps ) + { + if ( currentlySelectedTimeSteps.count( dateTime ) ) + { + currentlySelectedTimeStepIndices.insert( (int)allDateTimes.size() ); + } + allDateTimes.push_back( dateTime ); + } + + std::vector filteredTimeStepIndices = + RimTimeStepFilter::filteredTimeStepIndices( allDateTimes, 0, (int)allDateTimes.size() - 1, filterType, 1 ); + + // Add existing time steps to list of options to avoid removing them when changing filter. + filteredTimeStepIndices.insert( filteredTimeStepIndices.end(), + currentlySelectedTimeStepIndices.begin(), + currentlySelectedTimeStepIndices.end() ); + std::sort( filteredTimeStepIndices.begin(), filteredTimeStepIndices.end() ); + filteredTimeStepIndices.erase( std::unique( filteredTimeStepIndices.begin(), filteredTimeStepIndices.end() ), + filteredTimeStepIndices.end() ); + + QString dateFormatString = RiaQDateTimeTools::dateFormatString( RiaPreferences::current()->dateFormat(), + RiaDefines::DateFormatComponents::DATE_FORMAT_YEAR_MONTH_DAY ); + QString timeFormatString = RiaQDateTimeTools::timeFormatString( RiaPreferences::current()->timeFormat(), + RiaDefines::TimeFormatComponents::TIME_FORMAT_HOUR_MINUTE ); + QString dateTimeFormatString = QString( "%1 %2" ).arg( dateFormatString ).arg( timeFormatString ); + + bool showTime = ( filterType == RimTimeStepFilter::TS_ALL ) || ( filterType == RimTimeStepFilter::TS_INTERVAL_DAYS ); + + for ( auto timeStepIndex : filteredTimeStepIndices ) + { + QDateTime dateTime = allDateTimes[timeStepIndex]; + + if ( showTime && dateTime.time() != QTime( 0, 0, 0 ) ) + { + options.push_back( + caf::PdmOptionItemInfo( RiaQDateTimeTools::toStringUsingApplicationLocale( dateTime, dateTimeFormatString ), dateTime ) ); + } + else + { + options.push_back( + caf::PdmOptionItemInfo( RiaQDateTimeTools::toStringUsingApplicationLocale( dateTime, dateFormatString ), dateTime ) ); + } + } +} diff --git a/ApplicationLibCode/ProjectDataModel/RimTimeStepFilter.h b/ApplicationLibCode/ProjectDataModel/RimTimeStepFilter.h index 4a99f4cb28..c965fd3fe4 100644 --- a/ApplicationLibCode/ProjectDataModel/RimTimeStepFilter.h +++ b/ApplicationLibCode/ProjectDataModel/RimTimeStepFilter.h @@ -65,6 +65,12 @@ class RimTimeStepFilter : public caf::PdmObject std::vector> allTimeSteps() const; + static void timeStepOptions( QList& options, + const caf::PdmFieldHandle* timestepField, + std::vector availableTimeSteps, + std::vector selectedTimeSteps, + TimeStepFilterTypeEnum filterType ); + protected: void initAfterRead() override;