Merge pull request #389 from AdenKoperczak/rework_layer_text

Rework layer text
This commit is contained in:
Dan Paulat 2025-03-19 23:09:27 -05:00 committed by GitHub
commit 8dfb2fe69c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 195 additions and 56 deletions

View file

@ -29,6 +29,7 @@ static const std::string logPrefix_ = "scwx::qt::manager::font_manager";
static const auto logger_ = scwx::util::Logger::Create(logPrefix_);
static const std::string kFcTrueType_ {"TrueType"};
static const std::string kFcOpenType_ {"CFF"};
struct FontRecord
{
@ -70,6 +71,7 @@ public:
const std::vector<char>& GetRawFontData(const std::string& filename);
static bool CheckFontFormat(const FcChar8* format);
static FontRecord MatchFontFile(const std::string& family,
const std::vector<std::string>& styles);
@ -457,6 +459,13 @@ void FontManager::Impl::FinalizeFontconfig()
FcFini();
}
bool FontManager::Impl::CheckFontFormat(const FcChar8* format)
{
const std::string stdFormat = reinterpret_cast<const char*>(format);
return stdFormat == kFcTrueType_ || stdFormat == kFcOpenType_;
}
FontRecord
FontManager::Impl::MatchFontFile(const std::string& family,
const std::vector<std::string>& styles)
@ -469,9 +478,6 @@ FontManager::Impl::MatchFontFile(const std::string& family,
FcPatternAddString(
pattern, FC_FAMILY, reinterpret_cast<const FcChar8*>(family.c_str()));
FcPatternAddString(pattern,
FC_FONTFORMAT,
reinterpret_cast<const FcChar8*>(kFcTrueType_.c_str()));
FcPatternAddBool(pattern, FC_SYMBOL, FcFalse);
if (!styles.empty())
@ -505,6 +511,7 @@ FontManager::Impl::MatchFontFile(const std::string& family,
FcChar8* fcFamily = nullptr;
FcChar8* fcStyle = nullptr;
FcChar8* fcFile = nullptr;
FcChar8* fcFormat = nullptr;
FcBool fcSymbol = FcFalse;
// Match was found, get properties
@ -515,7 +522,10 @@ FontManager::Impl::MatchFontFile(const std::string& family,
FcPatternGetString(match, FC_FILE, 0, &fcFile) == FcResultMatch &&
FcPatternGetBool(match, FC_SYMBOL, 0, &fcSymbol) ==
FcResultMatch &&
fcSymbol == FcFalse /*Must check fcSymbol manually*/)
FcPatternGetString(match, FC_FONTFORMAT, 0, &fcFormat) ==
FcResultMatch &&
fcSymbol == FcFalse /*Must check fcSymbol manually*/ &&
CheckFontFormat(fcFormat))
{
record.family_ = reinterpret_cast<char*>(fcFamily);
record.style_ = reinterpret_cast<char*>(fcStyle);

View file

@ -229,7 +229,10 @@ public:
AlertLayer::AlertLayer(std::shared_ptr<MapContext> context,
awips::Phenomenon phenomenon) :
DrawLayer(context), p(std::make_unique<Impl>(this, context, phenomenon))
DrawLayer(
context,
fmt::format("AlertLayer {}", awips::GetPhenomenonText(phenomenon))),
p(std::make_unique<Impl>(this, context, phenomenon))
{
for (auto alertActive : {false, true})
{

View file

@ -1,12 +1,17 @@
#include <ranges>
#include <scwx/qt/manager/font_manager.hpp>
#include <scwx/qt/map/draw_layer.hpp>
#include <scwx/qt/model/imgui_context_model.hpp>
#include <scwx/qt/gl/shader_program.hpp>
#include <scwx/util/logger.hpp>
namespace scwx
{
namespace qt
{
namespace map
#include <backends/imgui_impl_opengl3.h>
#include <backends/imgui_impl_qt.hpp>
#include <utility>
#include <fmt/format.h>
#include <imgui.h>
namespace scwx::qt::map
{
static const std::string logPrefix_ = "scwx::qt::map::draw_layer";
@ -15,21 +20,57 @@ static const auto logger_ = scwx::util::Logger::Create(logPrefix_);
class DrawLayerImpl
{
public:
explicit DrawLayerImpl(std::shared_ptr<MapContext> context) :
context_ {context}, drawList_ {}, textureAtlas_ {GL_INVALID_INDEX}
explicit DrawLayerImpl(std::shared_ptr<MapContext> context,
const std::string& imGuiContextName) :
context_ {std::move(context)}, drawList_ {}
{
static size_t currentLayerId_ {0u};
imGuiContextName_ =
fmt::format("{} {}", imGuiContextName, ++currentLayerId_);
// This must be initialized after the last line
// NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer)
imGuiContext_ =
model::ImGuiContextModel::Instance().CreateContext(imGuiContextName_);
// Initialize ImGui Qt backend
ImGui_ImplQt_Init();
}
~DrawLayerImpl() {}
~DrawLayerImpl()
{
// Set ImGui Context
ImGui::SetCurrentContext(imGuiContext_);
// Shutdown ImGui Context
if (imGuiRendererInitialized_)
{
ImGui_ImplOpenGL3_Shutdown();
}
ImGui_ImplQt_Shutdown();
// Destroy ImGui Context
model::ImGuiContextModel::Instance().DestroyContext(imGuiContextName_);
}
DrawLayerImpl(const DrawLayerImpl&) = delete;
DrawLayerImpl& operator=(const DrawLayerImpl&) = delete;
DrawLayerImpl(const DrawLayerImpl&&) = delete;
DrawLayerImpl& operator=(const DrawLayerImpl&&) = delete;
std::shared_ptr<MapContext> context_;
std::vector<std::shared_ptr<gl::draw::DrawItem>> drawList_;
GLuint textureAtlas_;
GLuint textureAtlas_ {GL_INVALID_INDEX};
std::uint64_t textureAtlasBuildCount_ {};
std::string imGuiContextName_;
ImGuiContext* imGuiContext_;
bool imGuiRendererInitialized_ {};
};
DrawLayer::DrawLayer(const std::shared_ptr<MapContext>& context) :
GenericLayer(context), p(std::make_unique<DrawLayerImpl>(context))
DrawLayer::DrawLayer(const std::shared_ptr<MapContext>& context,
const std::string& imGuiContextName) :
GenericLayer(context),
p(std::make_unique<DrawLayerImpl>(context, imGuiContextName))
{
}
DrawLayer::~DrawLayer() = default;
@ -42,9 +83,45 @@ void DrawLayer::Initialize()
{
item->Initialize();
}
ImGuiInitialize();
}
void DrawLayer::Render(const QMapLibre::CustomLayerRenderParameters& params)
void DrawLayer::ImGuiFrameStart()
{
auto defaultFont = manager::FontManager::Instance().GetImGuiFont(
types::FontCategory::Default);
// Setup ImGui Frame
ImGui::SetCurrentContext(p->imGuiContext_);
// Start ImGui Frame
ImGui_ImplQt_NewFrame(p->context_->widget());
ImGui_ImplOpenGL3_NewFrame();
ImGui::NewFrame();
ImGui::PushFont(defaultFont->font());
}
void DrawLayer::ImGuiFrameEnd()
{
// Pop default font
ImGui::PopFont();
// Render ImGui Frame
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
}
void DrawLayer::ImGuiInitialize()
{
ImGui::SetCurrentContext(p->imGuiContext_);
ImGui_ImplQt_RegisterWidget(p->context_->widget());
ImGui_ImplOpenGL3_Init();
p->imGuiRendererInitialized_ = true;
}
void DrawLayer::RenderWithoutImGui(
const QMapLibre::CustomLayerRenderParameters& params)
{
gl::OpenGLFunctions& gl = p->context_->gl();
p->textureAtlas_ = p->context_->GetTextureAtlas();
@ -68,6 +145,17 @@ void DrawLayer::Render(const QMapLibre::CustomLayerRenderParameters& params)
p->textureAtlasBuildCount_ = newTextureAtlasBuildCount;
}
void DrawLayer::ImGuiSelectContext()
{
ImGui::SetCurrentContext(p->imGuiContext_);
}
void DrawLayer::Render(const QMapLibre::CustomLayerRenderParameters& params)
{
ImGuiFrameStart();
RenderWithoutImGui(params);
ImGuiFrameEnd();
}
void DrawLayer::Deinitialize()
{
@ -90,10 +178,10 @@ bool DrawLayer::RunMousePicking(
bool itemPicked = false;
// For each draw item in the draw list in reverse
for (auto it = p->drawList_.rbegin(); it != p->drawList_.rend(); ++it)
for (auto& it : std::ranges::reverse_view(p->drawList_))
{
// Run mouse picking on each draw item
if ((*it)->RunMousePicking(params,
if (it->RunMousePicking(params,
mouseLocalPos,
mouseGlobalPos,
mouseCoords,
@ -114,6 +202,4 @@ void DrawLayer::AddDrawItem(const std::shared_ptr<gl::draw::DrawItem>& drawItem)
p->drawList_.push_back(drawItem);
}
} // namespace map
} // namespace qt
} // namespace scwx
} // namespace scwx::qt::map

View file

@ -15,7 +15,8 @@ class DrawLayerImpl;
class DrawLayer : public GenericLayer
{
public:
explicit DrawLayer(const std::shared_ptr<MapContext>& context);
explicit DrawLayer(const std::shared_ptr<MapContext>& context,
const std::string& imGuiContextName);
virtual ~DrawLayer();
virtual void Initialize() override;
@ -32,6 +33,12 @@ public:
protected:
void AddDrawItem(const std::shared_ptr<gl::draw::DrawItem>& drawItem);
void ImGuiFrameStart();
void ImGuiFrameEnd();
void ImGuiInitialize();
void
RenderWithoutImGui(const QMapLibre::CustomLayerRenderParameters& params);
void ImGuiSelectContext();
private:
std::unique_ptr<DrawLayerImpl> p;

View file

@ -36,6 +36,8 @@ public:
std::shared_ptr<view::OverlayProductView> overlayProductView_ {nullptr};
std::shared_ptr<view::RadarProductView> radarProductView_;
QWidget* widget_;
};
MapContext::MapContext(
@ -109,6 +111,11 @@ int16_t MapContext::radar_product_code() const
return p->radarProductCode_;
}
QWidget* MapContext::widget() const
{
return p->widget_;
}
void MapContext::set_map(const std::shared_ptr<QMapLibre::Map>& map)
{
p->map_ = map;
@ -167,6 +174,11 @@ void MapContext::set_radar_product_code(int16_t radarProductCode)
p->radarProductCode_ = radarProductCode;
}
void MapContext::set_widget(QWidget* widget)
{
p->widget_ = widget;
}
} // namespace map
} // namespace qt
} // namespace scwx

View file

@ -38,18 +38,21 @@ public:
MapContext(MapContext&&) noexcept;
MapContext& operator=(MapContext&&) noexcept;
std::weak_ptr<QMapLibre::Map> map() const;
std::string map_copyrights() const;
MapProvider map_provider() const;
MapSettings& settings();
QMargins color_table_margins() const;
float pixel_ratio() const;
common::Coordinate mouse_coordinate() const;
std::shared_ptr<view::OverlayProductView> overlay_product_view() const;
std::shared_ptr<view::RadarProductView> radar_product_view() const;
common::RadarProductGroup radar_product_group() const;
std::string radar_product() const;
int16_t radar_product_code() const;
[[nodiscard]] std::weak_ptr<QMapLibre::Map> map() const;
[[nodiscard]] std::string map_copyrights() const;
[[nodiscard]] MapProvider map_provider() const;
[[nodiscard]] MapSettings& settings();
[[nodiscard]] QMargins color_table_margins() const;
[[nodiscard]] float pixel_ratio() const;
[[nodiscard]] common::Coordinate mouse_coordinate() const;
[[nodiscard]] std::shared_ptr<view::OverlayProductView>
overlay_product_view() const;
[[nodiscard]] std::shared_ptr<view::RadarProductView>
radar_product_view() const;
[[nodiscard]] common::RadarProductGroup radar_product_group() const;
[[nodiscard]] std::string radar_product() const;
[[nodiscard]] int16_t radar_product_code() const;
[[nodiscard]] QWidget* widget() const;
void set_map(const std::shared_ptr<QMapLibre::Map>& map);
void set_map_copyrights(const std::string& copyrights);
@ -64,6 +67,7 @@ public:
void set_radar_product_group(common::RadarProductGroup radarProductGroup);
void set_radar_product(const std::string& radarProduct);
void set_radar_product_code(int16_t radarProductCode);
void set_widget(QWidget* widget);
private:
class Impl;

View file

@ -114,6 +114,7 @@ public:
context_->set_map_provider(
GetMapProvider(generalSettings.map_provider().GetValue()));
context_->set_overlay_product_view(overlayProductView);
context_->set_widget(widget);
// Initialize map data
SetRadarSite(generalSettings.default_radar_site().GetValue());
@ -1571,21 +1572,13 @@ void MapWidget::paintGL()
// Handle hotkey updates
p->HandleHotkeyUpdates();
// Setup ImGui Frame
ImGui::SetCurrentContext(p->imGuiContext_);
// Lock ImGui font atlas prior to new ImGui frame
std::shared_lock imguiFontAtlasLock {
manager::FontManager::Instance().imgui_font_atlas_mutex()};
// Start ImGui Frame
ImGui_ImplQt_NewFrame(this);
ImGui_ImplOpenGL3_NewFrame();
// Check ImGui fonts
ImGui::SetCurrentContext(p->imGuiContext_);
p->ImGuiCheckFonts();
ImGui::NewFrame();
// Set default font
ImGui::PushFont(defaultFont->font());
// Update pixel ratio
p->context_->set_pixel_ratio(pixelRatio());
@ -1596,6 +1589,18 @@ void MapWidget::paintGL()
size() * pixelRatio());
p->map_->render();
// ImGui tool tip code
// Setup ImGui Frame
ImGui::SetCurrentContext(p->imGuiContext_);
// Start ImGui Frame
ImGui_ImplQt_NewFrame(this);
ImGui_ImplOpenGL3_NewFrame();
ImGui::NewFrame();
// Set default font
ImGui::PushFont(defaultFont->font());
// Perform mouse picking
if (p->hasMouse_)
{

View file

@ -129,7 +129,8 @@ void MarkerLayer::Impl::ReloadMarkers()
}
MarkerLayer::MarkerLayer(const std::shared_ptr<MapContext>& context) :
DrawLayer(context), p(std::make_unique<MarkerLayer::Impl>(this, context))
DrawLayer(context, "MarkerLayer"),
p(std::make_unique<MarkerLayer::Impl>(this, context))
{
AddDrawItem(p->geoIcons_);
}

View file

@ -143,7 +143,8 @@ public:
};
OverlayLayer::OverlayLayer(std::shared_ptr<MapContext> context) :
DrawLayer(context), p(std::make_unique<OverlayLayerImpl>(this, context))
DrawLayer(context, "OverlayLayer"),
p(std::make_unique<OverlayLayerImpl>(this, context))
{
AddDrawItem(p->activeBoxOuter_);
AddDrawItem(p->activeBoxInner_);
@ -292,6 +293,8 @@ void OverlayLayer::Render(const QMapLibre::CustomLayerRenderParameters& params)
auto& settings = context()->settings();
const float pixelRatio = context()->pixel_ratio();
ImGuiFrameStart();
p->sweepTimePicked_ = false;
if (radarProductView != nullptr)
@ -457,7 +460,7 @@ void OverlayLayer::Render(const QMapLibre::CustomLayerRenderParameters& params)
p->icons_->SetIconVisible(p->mapLogoIcon_,
generalSettings.show_map_logo().GetValue());
DrawLayer::Render(params);
DrawLayer::RenderWithoutImGui(params);
auto mapCopyrights = context()->map_copyrights();
if (mapCopyrights.length() > 0 &&
@ -491,6 +494,8 @@ void OverlayLayer::Render(const QMapLibre::CustomLayerRenderParameters& params)
p->lastFontSize_ = ImGui::GetFontSize();
p->lastColorTableMargins_ = colorTableMargins;
ImGuiFrameEnd();
SCWX_GL_CHECK_ERROR();
}

View file

@ -109,7 +109,8 @@ public:
};
OverlayProductLayer::OverlayProductLayer(std::shared_ptr<MapContext> context) :
DrawLayer(context), p(std::make_unique<Impl>(this, context))
DrawLayer(context, "OverlayProductLayer"),
p(std::make_unique<Impl>(this, context))
{
auto overlayProductView = context->overlay_product_view();
connect(overlayProductView.get(),

View file

@ -66,7 +66,7 @@ public:
PlacefileLayer::PlacefileLayer(const std::shared_ptr<MapContext>& context,
const std::string& placefileName) :
DrawLayer(context),
DrawLayer(context, fmt::format("PlacefileLayer {}", placefileName)),
p(std::make_unique<PlacefileLayer::Impl>(this, context, placefileName))
{
AddDrawItem(p->placefileImages_);

View file

@ -44,7 +44,7 @@ public:
};
RadarSiteLayer::RadarSiteLayer(std::shared_ptr<MapContext> context) :
DrawLayer(context), p(std::make_unique<Impl>(this))
DrawLayer(context, "RadarSiteLayer"), p(std::make_unique<Impl>(this))
{
}
@ -55,6 +55,8 @@ void RadarSiteLayer::Initialize()
logger_->debug("Initialize()");
p->radarSites_ = config::RadarSite::GetAll();
ImGuiInitialize();
}
void RadarSiteLayer::Render(
@ -84,6 +86,7 @@ void RadarSiteLayer::Render(
p->halfWidth_ = params.width * 0.5f;
p->halfHeight_ = params.height * 0.5f;
ImGuiFrameStart();
// Radar site ImGui windows shouldn't have padding
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2 {0.0f, 0.0f});
@ -93,6 +96,7 @@ void RadarSiteLayer::Render(
}
ImGui::PopStyleVar();
ImGuiFrameEnd();
SCWX_GL_CHECK_ERROR();
}
@ -136,6 +140,7 @@ void RadarSiteLayer::Impl::RenderRadarSite(
if (ImGui::Button(radarSite->id().c_str()))
{
Q_EMIT self_->RadarSiteSelected(radarSite->id());
self_->ImGuiSelectContext();
}
// Store hover text for mouse picking pass