diff --git a/scwx-qt/scwx-qt.cmake b/scwx-qt/scwx-qt.cmake index 225d2b19..67b41da6 100644 --- a/scwx-qt/scwx-qt.cmake +++ b/scwx-qt/scwx-qt.cmake @@ -60,14 +60,16 @@ set(HDR_GL_DRAW source/scwx/qt/gl/draw/draw_item.hpp set(SRC_GL_DRAW source/scwx/qt/gl/draw/draw_item.cpp source/scwx/qt/gl/draw/geo_line.cpp source/scwx/qt/gl/draw/rectangle.cpp) -set(HDR_MANAGER source/scwx/qt/manager/radar_product_manager.hpp +set(HDR_MANAGER source/scwx/qt/manager/placefile_manager.hpp + source/scwx/qt/manager/radar_product_manager.hpp source/scwx/qt/manager/radar_product_manager_notifier.hpp source/scwx/qt/manager/resource_manager.hpp source/scwx/qt/manager/settings_manager.hpp source/scwx/qt/manager/text_event_manager.hpp source/scwx/qt/manager/timeline_manager.hpp source/scwx/qt/manager/update_manager.hpp) -set(SRC_MANAGER source/scwx/qt/manager/radar_product_manager.cpp +set(SRC_MANAGER source/scwx/qt/manager/placefile_manager.cpp + source/scwx/qt/manager/radar_product_manager.cpp source/scwx/qt/manager/radar_product_manager_notifier.cpp source/scwx/qt/manager/resource_manager.cpp source/scwx/qt/manager/settings_manager.cpp diff --git a/scwx-qt/source/scwx/qt/manager/placefile_manager.cpp b/scwx-qt/source/scwx/qt/manager/placefile_manager.cpp new file mode 100644 index 00000000..42d59624 --- /dev/null +++ b/scwx-qt/source/scwx/qt/manager/placefile_manager.cpp @@ -0,0 +1,135 @@ +#include +#include +#include + +#include +#include + +#include +#include +#include + +namespace scwx +{ +namespace qt +{ +namespace manager +{ + +static const std::string logPrefix_ = "scwx::qt::manager::placefile_manager"; +static const auto logger_ = scwx::util::Logger::Create(logPrefix_); + +class PlacefileRecord +{ +public: + explicit PlacefileRecord(const std::string& name, + std::shared_ptr placefile, + bool enabled = true) : + name_ {name}, placefile_ {placefile}, enabled_ {enabled} + { + } + ~PlacefileRecord() + { + std::unique_lock lock(refreshMutex_); + refreshTimer_.cancel(); + } + + void Update(std::shared_ptr placefile); + + std::string name_; + std::shared_ptr placefile_; + bool enabled_; + boost::asio::thread_pool threadPool_ {1u}; + boost::asio::steady_timer refreshTimer_ {threadPool_}; + std::mutex refreshMutex_ {}; +}; + +class PlacefileManager::Impl +{ +public: + explicit Impl(PlacefileManager* self) : self_ {self} {} + ~Impl() {} + + boost::asio::thread_pool threadPool_ {1u}; + + PlacefileManager* self_; + + std::vector> placefileRecords_ {}; + std::shared_mutex placefileRecordLock_ {}; +}; + +PlacefileManager::PlacefileManager() : p(std::make_unique(this)) {} +PlacefileManager::~PlacefileManager() = default; + +void PlacefileManager::LoadFile(const std::string& filename) +{ + logger_->debug("LoadFile: {}", filename); + + boost::asio::post( + p->threadPool_, + [=, this]() + { + // Load file + std::shared_ptr placefile = + gr::Placefile::Load(filename); + + if (placefile == nullptr) + { + return; + } + + std::unique_lock lock(p->placefileRecordLock_); + + // Determine if the placefile has been loaded previously + auto it = std::find_if(p->placefileRecords_.begin(), + p->placefileRecords_.end(), + [&filename](auto& record) + { return record->name_ == filename; }); + if (it != p->placefileRecords_.end()) + { + // If the placefile has been loaded previously, update it + (*it)->Update(placefile); + + Q_EMIT PlacefileUpdated(filename); + } + else + { + // If this is a new placefile, add it + auto& record = p->placefileRecords_.emplace_back( + std::make_unique(filename, placefile)); + + Q_EMIT PlacefileEnabled(filename, record->enabled_); + } + }); +} + +void PlacefileRecord::Update(std::shared_ptr placefile) +{ + // Update placefile + placefile_ = placefile; + + // TODO: Update refresh timer +} + +std::shared_ptr PlacefileManager::Instance() +{ + static std::weak_ptr placefileManagerReference_ {}; + static std::mutex instanceMutex_ {}; + + std::unique_lock lock(instanceMutex_); + + std::shared_ptr placefileManager = + placefileManagerReference_.lock(); + + if (placefileManager == nullptr) + { + placefileManager = std::make_shared(); + placefileManagerReference_ = placefileManager; + } + + return placefileManager; +} + +} // namespace manager +} // namespace qt +} // namespace scwx diff --git a/scwx-qt/source/scwx/qt/manager/placefile_manager.hpp b/scwx-qt/source/scwx/qt/manager/placefile_manager.hpp new file mode 100644 index 00000000..1a61d65d --- /dev/null +++ b/scwx-qt/source/scwx/qt/manager/placefile_manager.hpp @@ -0,0 +1,38 @@ +#pragma once + +#include +#include + +#include + +namespace scwx +{ +namespace qt +{ +namespace manager +{ + +class PlacefileManager : public QObject +{ + Q_OBJECT + +public: + explicit PlacefileManager(); + ~PlacefileManager(); + + void LoadFile(const std::string& filename); + + static std::shared_ptr Instance(); + +signals: + void PlacefileEnabled(const std::string& name, bool enabled); + void PlacefileUpdated(const std::string& name); + +private: + class Impl; + std::unique_ptr p; +}; + +} // namespace manager +} // namespace qt +} // namespace scwx