Skip to content

Commit

Permalink
Simplify options for time step filter
Browse files Browse the repository at this point in the history
  • Loading branch information
jonjenssen committed Sep 13, 2023
1 parent 187df96 commit a35b0c8
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -316,57 +316,7 @@ QList<caf::PdmOptionItemInfo> RimFaultReactivationModel::calculateValueOptions(
}
else if ( fieldNeedingOptions == &m_selectedTimeSteps )
{
if ( m_availableTimeSteps.empty() )
{
return options;
}

std::set<QDateTime> currentlySelectedTimeSteps( m_selectedTimeSteps().begin(), m_selectedTimeSteps().end() );
std::set<int> currentlySelectedTimeStepIndices;
std::vector<QDateTime> allDateTimes;
for ( auto& dateTime : m_availableTimeSteps )
{
if ( currentlySelectedTimeSteps.count( dateTime ) )
{
currentlySelectedTimeStepIndices.insert( (int)allDateTimes.size() );
}
allDateTimes.push_back( dateTime );
}

std::vector<int> 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;
Expand Down
60 changes: 60 additions & 0 deletions ApplicationLibCode/ProjectDataModel/RimTimeStepFilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "RimTimeStepFilter.h"

#include "RiaPreferences.h"
#include "RiaQDateTimeTools.h"

#include "RifReaderEclipseOutput.h"
Expand Down Expand Up @@ -479,3 +480,62 @@ bool RimTimeStepFilter::readOnlyLastFrame() const
{
return m_readOnlyLastFrame;
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimTimeStepFilter::timeStepOptions( QList<caf::PdmOptionItemInfo>& options,
const caf::PdmFieldHandle* timestepField,
std::vector<QDateTime> availableTimeSteps,
std::vector<QDateTime> selectedTimeSteps,
RimTimeStepFilter::TimeStepFilterTypeEnum filterType )
{
if ( availableTimeSteps.empty() ) return;

std::set<QDateTime> currentlySelectedTimeSteps( selectedTimeSteps.begin(), selectedTimeSteps.end() );
std::set<int> currentlySelectedTimeStepIndices;
std::vector<QDateTime> allDateTimes;
for ( auto& dateTime : availableTimeSteps )
{
if ( currentlySelectedTimeSteps.count( dateTime ) )
{
currentlySelectedTimeStepIndices.insert( (int)allDateTimes.size() );
}
allDateTimes.push_back( dateTime );
}

std::vector<int> 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 ) );
}
}
}
6 changes: 6 additions & 0 deletions ApplicationLibCode/ProjectDataModel/RimTimeStepFilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ class RimTimeStepFilter : public caf::PdmObject

std::vector<std::pair<QString, QDateTime>> allTimeSteps() const;

static void timeStepOptions( QList<caf::PdmOptionItemInfo>& options,
const caf::PdmFieldHandle* timestepField,
std::vector<QDateTime> availableTimeSteps,
std::vector<QDateTime> selectedTimeSteps,
TimeStepFilterTypeEnum filterType );

protected:
void initAfterRead() override;

Expand Down

0 comments on commit a35b0c8

Please sign in to comment.