#include #include #include #include #include #include #include #include #include namespace scwx { namespace qt { namespace map { static const std::string logPrefix_ = "scwx::qt::map::placefile_layer"; static const auto logger_ = scwx::util::Logger::Create(logPrefix_); class PlacefileLayer::Impl { public: explicit Impl(PlacefileLayer* self, std::shared_ptr context, const std::string& placefileName) : self_ {self}, placefileName_ {placefileName}, placefileIcons_ {std::make_shared(context)}, placefileLines_ {std::make_shared(context)}, placefilePolygons_ { std::make_shared(context)}, placefileText_ { std::make_shared(context, placefileName)} { ConnectSignals(); } ~Impl() = default; void ConnectSignals(); boost::asio::thread_pool threadPool_ {1}; PlacefileLayer* self_; std::string placefileName_; std::mutex dataMutex_ {}; std::shared_ptr placefileIcons_; std::shared_ptr placefileLines_; std::shared_ptr placefilePolygons_; std::shared_ptr placefileText_; }; PlacefileLayer::PlacefileLayer(std::shared_ptr context, const std::string& placefileName) : DrawLayer(context), p(std::make_unique(this, context, placefileName)) { AddDrawItem(p->placefileIcons_); AddDrawItem(p->placefileLines_); AddDrawItem(p->placefilePolygons_); AddDrawItem(p->placefileText_); ReloadData(); } PlacefileLayer::~PlacefileLayer() = default; void PlacefileLayer::Impl::ConnectSignals() { auto placefileManager = manager::PlacefileManager::Instance(); QObject::connect(placefileManager.get(), &manager::PlacefileManager::PlacefileUpdated, self_, [this](const std::string& name) { if (name == placefileName_) { self_->ReloadData(); } }); } std::string PlacefileLayer::placefile_name() const { return p->placefileName_; } void PlacefileLayer::set_placefile_name(const std::string& placefileName) { p->placefileName_ = placefileName; p->placefileText_->set_placefile_name(placefileName); ReloadData(); } void PlacefileLayer::Initialize() { logger_->debug("Initialize()"); DrawLayer::Initialize(); } void PlacefileLayer::Render( const QMapLibreGL::CustomLayerRenderParameters& params) { gl::OpenGLFunctions& gl = context()->gl(); // Set OpenGL blend mode for transparency gl.glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); std::shared_ptr placefileManager = manager::PlacefileManager::Instance(); auto placefile = placefileManager->placefile(p->placefileName_); // Render placefile if (placefile != nullptr) { bool thresholded = placefileManager->placefile_thresholded(placefile->name()); p->placefileIcons_->set_thresholded(thresholded); p->placefileLines_->set_thresholded(thresholded); p->placefilePolygons_->set_thresholded(thresholded); p->placefileText_->set_thresholded(thresholded); } DrawLayer::Render(params); SCWX_GL_CHECK_ERROR(); } void PlacefileLayer::Deinitialize() { logger_->debug("Deinitialize()"); DrawLayer::Deinitialize(); } void PlacefileLayer::ReloadData() { boost::asio::post( p->threadPool_, [this]() { logger_->debug("ReloadData: {}", p->placefileName_); std::unique_lock lock {p->dataMutex_}; std::shared_ptr placefileManager = manager::PlacefileManager::Instance(); auto placefile = placefileManager->placefile(p->placefileName_); if (placefile == nullptr) { return; } // Start draw items p->placefileIcons_->StartIcons(); p->placefileLines_->StartLines(); p->placefilePolygons_->StartPolygons(); p->placefileText_->StartText(); p->placefileIcons_->SetIconFiles(placefile->icon_files(), placefile->name()); for (auto& drawItem : placefile->GetDrawItems()) { switch (drawItem->itemType_) { case gr::Placefile::ItemType::Text: p->placefileText_->AddText( std::static_pointer_cast( drawItem)); break; case gr::Placefile::ItemType::Icon: p->placefileIcons_->AddIcon( std::static_pointer_cast( drawItem)); break; case gr::Placefile::ItemType::Line: p->placefileLines_->AddLine( std::static_pointer_cast( drawItem)); break; case gr::Placefile::ItemType::Polygon: p->placefilePolygons_->AddPolygon( std::static_pointer_cast( drawItem)); break; default: break; } } // Finish draw items p->placefileIcons_->FinishIcons(); p->placefileLines_->FinishLines(); p->placefilePolygons_->FinishPolygons(); p->placefileText_->FinishText(); Q_EMIT DataReloaded(); }); } } // namespace map } // namespace qt } // namespace scwx