Qt: Make SVGWidget respond to palette changes

pull/3746/head
Stenzek 4 months ago
parent 21c6ab5822
commit 15bfa50cee
No known key found for this signature in database

@ -88,6 +88,11 @@ void SVGWidget::destroyDocument()
m_svg_data.deallocate();
}
QColor SVGWidget::effectiveColor() const
{
return m_color.isValid() ? m_color : palette().color(QPalette::WindowText);
}
void SVGWidget::rasterize()
{
m_pixmap = {};
@ -126,7 +131,13 @@ void SVGWidget::rasterize()
// Use white as currentColor so the SVG's own colours are preserved.
// Swap in a palette colour here if tinting is ever desired.
const plutovg_color_t current_color = {.r = 1.0f, .g = 1.0f, .b = 1.0f, .a = 1.0f};
const QColor color = effectiveColor();
const plutovg_color_t current_color = {
.r = static_cast<float>(color.redF()),
.g = static_cast<float>(color.greenF()),
.b = static_cast<float>(color.blueF()),
.a = static_cast<float>(color.alphaF()),
};
plutovg_surface_t* const surface =
plutosvg_document_render_to_surface(m_document, nullptr, render_w, render_h, &current_color, nullptr, nullptr);
@ -173,7 +184,9 @@ void SVGWidget::changeEvent(QEvent* event)
QWidget::changeEvent(event);
// Re-rasterize when the screen changes (e.g. window moved to a display with a different DPR).
if (event->type() == QEvent::DevicePixelRatioChange || event->type() == QEvent::ScreenChangeInternal)
// Or when the theme/current colour changes.
const QEvent::Type type = event->type();
if (type == QEvent::DevicePixelRatioChange || type == QEvent::ScreenChangeInternal || type == QEvent::PaletteChange)
{
m_last_raster_size = {}; // force re-render
rasterize();

@ -50,6 +50,9 @@ protected:
void changeEvent(QEvent* event) override;
private:
// Effective value of currentColor for rasterization, taking into account the widget's palette and m_color.
QColor effectiveColor() const;
/// (Re)render m_document at the current widget size + DPR and store the result in m_pixmap.
void rasterize();

Loading…
Cancel
Save