Debian Codegraph @

src/Gui/FontScaledSVG.cpp

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 /****************************************************************************
3 * *
4 * Copyright (c) 2025 Alfredo Monclus <[email protected]> *
5 * *
6 * This file is part of FreeCAD. *
7 * *
8 * FreeCAD is free software: you can redistribute it and/or modify it *
9 * under the terms of the GNU Lesser General Public License as *
10 * published by the Free Software Foundation, either version 2.1 of the *
11 * License, or (at your option) any later version. *
12 * *
13 * FreeCAD is distributed in the hope that it will be useful, but *
14 * WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
16 * Lesser General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Lesser General Public *
19 * License along with FreeCAD. If not, see *
20 * <https://www.gnu.org/licenses/>. *
21 * *
22 ***************************************************************************/
23
24 // This custom widget scales an svg according to fonts
25
26 #include "FontScaledSVG.h"
27
28 namespace Gui
29 {
30
31 FontScaledSVG::FontScaledSVG(QWidget* parent)
32 : QWidget(parent)
33 , m_svgRenderer(new QSvgRenderer(this))
34 {
35 setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
36 }
37
38 void FontScaledSVG::setSvg(const QString& svgPath)
39 {
40 if (m_svgRenderer->load(svgPath)) {
41 updateScaledSize();
42 update();
43 }
44 }
45
46 void FontScaledSVG::paintEvent(QPaintEvent* event)
47 {
48 Q_UNUSED(event);
49 QPainter painter(this);
50
51 if (m_svgRenderer->isValid()) {
52 QRect targetRect(0, 0, width(), height());
53 m_svgRenderer->render(&painter, targetRect);
54 }
55 }
56
57 void FontScaledSVG::resizeEvent(QResizeEvent* event)
58 {
59 Q_UNUSED(event);
60 updateScaledSize();
61 }
62
63 void FontScaledSVG::updateScaledSize()
64 {
65 QSize baseSize = m_svgRenderer->defaultSize();
66
67 QFontMetrics metrics(font());
68 qreal spacing = metrics.lineSpacing();
69 constexpr int baseFactor = 18;
70 qreal scalingFactor = spacing / baseFactor;
71
72 QSize targetSize = baseSize * scalingFactor;
73 setFixedSize(targetSize);
74 }
75
76 } // namespace Gui
77
78 #include "moc_FontScaledSVG.cpp" // NOLINT