1 | // Copyright (C) 2020 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #include "qsvgwidget.h" |
5 | |
6 | #include <qsvgrenderer.h> |
7 | |
8 | #include "qstyleoption.h" |
9 | #include "qpainter.h" |
10 | #include <QtWidgets/private/qwidget_p.h> |
11 | |
12 | QT_BEGIN_NAMESPACE |
13 | |
14 | /*! |
15 | \class QSvgWidget |
16 | \inmodule QtSvgWidgets |
17 | \ingroup painting |
18 | |
19 | \brief The QSvgWidget class provides a widget that is used to display the contents of |
20 | Scalable Vector Graphics (SVG) files. |
21 | \since 4.1 |
22 | |
23 | This class enables developers to display SVG drawings alongside standard widgets, and |
24 | is used in much the same way as QLabel is used for displaying text and bitmap images. |
25 | |
26 | Since QSvgWidget is a subclass of QWidget, SVG drawings are rendered using the properties |
27 | of the display. More control can be exercised over the rendering process with the |
28 | QSvgRenderer class, as this can be used to paint onto other paint devices, such as QImage |
29 | and QGLWidget. The renderer used by the widget can be obtained with the renderer() |
30 | function. |
31 | |
32 | Each QSvgWidget can be constructed with the file name of a SVG file, or they can be |
33 | constructed without a specific file to render and one can be supplied later. The load() |
34 | functions provide two different ways to load an SVG file: they accept either the file name |
35 | of an SVG file or a QByteArray containing the serialized XML representation of an SVG file. |
36 | |
37 | By default, the widget provides a size hint to reflect the size of the drawing that it |
38 | displays. If no data has been loaded, the widget provides the default QWidget size hint. |
39 | Subclass this class and reimplement sizeHint() if you need to customize this behavior. |
40 | |
41 | \sa QSvgRenderer, {Qt SVG C++ Classes}, QPicture |
42 | */ |
43 | |
44 | class QSvgWidgetPrivate : public QWidgetPrivate |
45 | { |
46 | Q_DECLARE_PUBLIC(QSvgWidget) |
47 | public: |
48 | QSvgRenderer *renderer; |
49 | }; |
50 | |
51 | /*! |
52 | Constructs a new SVG display widget with the given \a parent. |
53 | */ |
54 | QSvgWidget::QSvgWidget(QWidget *parent) |
55 | : QWidget(*new QSvgWidgetPrivate, parent, {}) |
56 | { |
57 | d_func()->renderer = new QSvgRenderer(this); |
58 | QObject::connect(sender: d_func()->renderer, SIGNAL(repaintNeeded()), |
59 | receiver: this, SLOT(update())); |
60 | } |
61 | |
62 | /*! |
63 | Constructs a new SVG display widget with the given \a parent and loads the contents |
64 | of the specified \a file. |
65 | */ |
66 | QSvgWidget::QSvgWidget(const QString &file, QWidget *parent) |
67 | : QWidget(*new QSvgWidgetPrivate, parent, {}) |
68 | { |
69 | d_func()->renderer = new QSvgRenderer(file, this); |
70 | QObject::connect(sender: d_func()->renderer, SIGNAL(repaintNeeded()), |
71 | receiver: this, SLOT(update())); |
72 | } |
73 | |
74 | /*! |
75 | Destroys the widget. |
76 | */ |
77 | QSvgWidget::~QSvgWidget() |
78 | { |
79 | |
80 | } |
81 | |
82 | /*! |
83 | Returns the renderer used to display the contents of the widget. |
84 | */ |
85 | QSvgRenderer * QSvgWidget::renderer() const |
86 | { |
87 | Q_D(const QSvgWidget); |
88 | return d->renderer; |
89 | } |
90 | |
91 | |
92 | /*! |
93 | \reimp |
94 | */ |
95 | QSize QSvgWidget::sizeHint() const |
96 | { |
97 | Q_D(const QSvgWidget); |
98 | if (d->renderer->isValid()) |
99 | return d->renderer->defaultSize(); |
100 | else |
101 | return QSize(128, 64); |
102 | } |
103 | |
104 | |
105 | /*! |
106 | \reimp |
107 | */ |
108 | void QSvgWidget::paintEvent(QPaintEvent *) |
109 | { |
110 | Q_D(QSvgWidget); |
111 | QStyleOption opt; |
112 | opt.initFrom(w: this); |
113 | QPainter p(this); |
114 | style()->drawPrimitive(pe: QStyle::PE_Widget, opt: &opt, p: &p, w: this); |
115 | d->renderer->render(p: &p); |
116 | } |
117 | |
118 | /*! |
119 | Loads the contents of the specified SVG \a file and updates the widget. |
120 | */ |
121 | void QSvgWidget::load(const QString &file) |
122 | { |
123 | Q_D(const QSvgWidget); |
124 | d->renderer->load(filename: file); |
125 | } |
126 | |
127 | /*! |
128 | Loads the specified SVG format \a contents and updates the widget. |
129 | */ |
130 | void QSvgWidget::load(const QByteArray &contents) |
131 | { |
132 | Q_D(const QSvgWidget); |
133 | d->renderer->load(contents); |
134 | } |
135 | |
136 | QT_END_NAMESPACE |
137 | |