File indexing completed on 2025-02-02 04:11:32

0001 /*
0002  * SPDX-FileCopyrightText: 2019-2023 Mattia Basaglia <dev@dragon.best>
0003  *
0004  * SPDX-License-Identifier: GPL-3.0-or-later
0005  */
0006 
0007 #pragma once
0008 
0009 #include <QWidget>
0010 #include <QPainter>
0011 #include <QPointer>
0012 #include <QResizeEvent>
0013 
0014 #include "model/assets/brush_style.hpp"
0015 #include "model/assets/gradient.hpp"
0016 
0017 namespace glaxnimate::gui {
0018 
0019 class ShapeStylePreviewWidget : public QWidget
0020 {
0021 public:
0022     ShapeStylePreviewWidget(QWidget* parent = nullptr)
0023         : QWidget(parent)
0024     {
0025         setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
0026     }
0027 
0028     void set_fill_color(const QColor& color)
0029     {
0030         fill_color = color;
0031         update();
0032     }
0033     void set_stroke_color(const QColor& color)
0034     {
0035         stroke_color = color;
0036         update();
0037     }
0038     void set_fill_ref(model::BrushStyle* ref)
0039     {
0040         fill_ref = ref;
0041         update();
0042     }
0043     void set_stroke_ref(model::BrushStyle* ref)
0044     {
0045         stroke_ref = ref;
0046         update();
0047     }
0048 
0049     void clear_gradients()
0050     {
0051         clear_gradient(fill_ref);
0052         clear_gradient(stroke_ref);
0053         update();
0054     }
0055 
0056 protected:
0057     void paintEvent(QPaintEvent*) override
0058     {
0059         QPainter painter(this);
0060         int border = width() / 5.;
0061         QRectF area(rect());
0062         draw_rect(painter, area, stroke_ref, stroke_color);
0063         area.adjust(border, border, -border, -border);
0064         draw_rect(painter, area, fill_ref, fill_color);
0065     }
0066 
0067 private:
0068     void clear_gradient(QPointer<model::BrushStyle>& item)
0069     {
0070         if ( item && item->is_instance<model::Gradient>() )
0071             item = {};
0072     }
0073 
0074     void draw_rect(QPainter& painter, const QRectF& rect, const QPointer<model::BrushStyle>& linked, const QColor& plain)
0075     {
0076         static QBrush background(QPixmap(QStringLiteral(":/color_widgets/alphaback.png")));
0077         painter.fillRect(rect, background);
0078 
0079         QBrush brush = linked ? linked->constrained_brush_style(0, rect) : plain;
0080         painter.fillRect(rect, brush);
0081     }
0082 
0083     QColor fill_color;
0084     QPointer<model::BrushStyle> fill_ref;
0085     QColor stroke_color;
0086     QPointer<model::BrushStyle> stroke_ref;
0087     QSize hecking_size;
0088 };
0089 
0090 } // namespace glaxnimate::gui