Timeline step back

This commit is contained in:
Dan Paulat 2023-05-26 16:14:33 -05:00
parent 6f1fb84397
commit 0ddd9d91ea
2 changed files with 90 additions and 12 deletions

View file

@ -33,6 +33,7 @@ public:
TimelineManager* self_;
void SelectTime(std::chrono::system_clock::time_point selectedTime = {});
void StepBack();
std::string radarSite_ {"?"};
std::string previousRadarSite_ {"?"};
@ -51,6 +52,12 @@ TimelineManager::~TimelineManager() = default;
void TimelineManager::SetRadarSite(const std::string& radarSite)
{
if (p->radarSite_ == radarSite)
{
// No action needed
return;
}
logger_->debug("SetRadarSite: {}", radarSite);
p->radarSite_ = radarSite;
@ -123,6 +130,8 @@ void TimelineManager::AnimationStepBegin()
void TimelineManager::AnimationStepBack()
{
logger_->debug("AnimationStepBack");
p->StepBack();
}
void TimelineManager::AnimationPlay()
@ -217,6 +226,60 @@ void TimelineManager::Impl::SelectTime(
});
}
void TimelineManager::Impl::StepBack()
{
scwx::util::async(
[this]()
{
// Take a lock for time selection
std::unique_lock lock {selectTimeMutex_};
// Determine time to get active volume times
std::chrono::system_clock::time_point queryTime = adjustedTime_;
if (queryTime == std::chrono::system_clock::time_point {})
{
queryTime = std::chrono::system_clock::now();
}
// Request active volume times
auto radarProductManager =
manager::RadarProductManager::Instance(radarSite_);
auto volumeTimes =
radarProductManager->GetActiveVolumeTimes(queryTime);
std::set<std::chrono::system_clock::time_point>::const_iterator it;
if (adjustedTime_ == std::chrono::system_clock::time_point {})
{
// If the adjusted time is live, get the last element in the set
it = volumeTimes.cend();
if (!volumeTimes.empty())
{
--it;
}
}
else
{
// Get the current element in the set
it = scwx::util::GetBoundedElementIterator(volumeTimes,
adjustedTime_);
}
// Only if we aren't at the beginning of the volume times set
if (it != volumeTimes.cbegin())
{
// Select the previous time
adjustedTime_ = *(--it);
selectedTime_ = adjustedTime_;
logger_->debug("Volume time updated: {}",
scwx::util::TimeString(adjustedTime_));
emit self_->VolumeTimeUpdated(adjustedTime_);
}
});
}
std::shared_ptr<TimelineManager> TimelineManager::Instance()
{
static std::weak_ptr<TimelineManager> timelineManagerReference_ {};

View file

@ -8,14 +8,13 @@ namespace scwx
namespace util
{
template<class Container, class ReturnType = Container::const_pointer>
ReturnType GetBoundedElementPointer(Container& container,
template<class Container>
Container::const_iterator
GetBoundedElementIterator(Container& container,
const typename Container::key_type& key)
{
ReturnType elementPtr {nullptr};
// Find the first element greater than the key requested
auto it = container.upper_bound(key);
typename Container::const_iterator it = container.upper_bound(key);
// An element with a key greater was found
if (it != container.cend())
@ -25,19 +24,34 @@ ReturnType GetBoundedElementPointer(Container& container,
{
// Get the element immediately preceding, this the element we are
// looking for
elementPtr = &(*(--it));
--it;
}
else
{
// The current element is a good substitute
elementPtr = &(*it);
}
}
else if (container.size() > 0)
{
// An element with a key greater was not found. If it exists, it must be
// the last element.
elementPtr = &(*container.rbegin());
// the last element. Decrement the end iterator.
--it;
}
return it;
}
template<class Container, class ReturnType = Container::const_pointer>
ReturnType GetBoundedElementPointer(Container& container,
const typename Container::key_type& key)
{
ReturnType elementPtr {nullptr};
auto it = GetBoundedElementIterator(container, key);
if (it != container.cend())
{
elementPtr = &(*(it));
}
return elementPtr;
@ -48,9 +62,10 @@ ReturnType GetBoundedElement(std::map<Key, T>& map, const Key& key)
{
ReturnType element;
typename std::map<Key, T>::pointer elementPtr =
typename std::map<Key, T>::const_pointer elementPtr =
GetBoundedElementPointer<std::map<Key, T>,
typename std::map<Key, T>::pointer>(map, key);
typename std::map<Key, T>::const_pointer>(map,
key);
if (elementPtr != nullptr)
{
element = elementPtr->second;