From 9e53b103a8ebf5306c6903df4eca515951dfcd1f Mon Sep 17 00:00:00 2001 From: Alfredo Monclus Date: Fri, 24 Jan 2025 16:36:42 -0300 Subject: [PATCH] PD(hole taskpanel): Add custom widget for font-scaled SVGs * feat: add custom widget for font scaled svgs * refactor(PD): hole taskpanel svg tweaks * fix(PD): build link on windows --- src/Gui/CMakeLists.txt | 2 + src/Gui/ElideCheckBox.h | 2 +- src/Gui/ElideLabel.h | 2 +- src/Gui/FontScaledSVG.cpp | 65 +++++++++++++++++++ src/Gui/FontScaledSVG.h | 53 +++++++++++++++ .../images/hole_counterbore_angled.svg | 10 +-- .../hole_counterbore_angled_included.svg | 10 +-- .../images/hole_counterbore_flat.svg | 12 ++-- .../images/hole_countersink_angled.svg | 56 ++++++++-------- .../Gui/Resources/images/hole_none_angled.svg | 8 +-- .../images/hole_none_angled_included.svg | 10 +-- src/Mod/PartDesign/Gui/TaskHoleParameters.cpp | 4 +- src/Mod/PartDesign/Gui/TaskHoleParameters.ui | 64 +++++------------- 13 files changed, 193 insertions(+), 105 deletions(-) create mode 100644 src/Gui/FontScaledSVG.cpp create mode 100644 src/Gui/FontScaledSVG.h diff --git a/src/Gui/CMakeLists.txt b/src/Gui/CMakeLists.txt index df9d37ee4c..4e30723bd6 100644 --- a/src/Gui/CMakeLists.txt +++ b/src/Gui/CMakeLists.txt @@ -1138,6 +1138,7 @@ SET(Widget_CPP_SRCS WorkbenchSelector.cpp ElideLabel.cpp ElideCheckBox.cpp + FontScaledSVG.cpp ) SET(Widget_HPP_SRCS FileDialog.h @@ -1161,6 +1162,7 @@ SET(Widget_HPP_SRCS WorkbenchSelector.h ElideLabel.h ElideCheckBox.h + FontScaledSVG.h ) SET(Widget_SRCS ${Widget_CPP_SRCS} diff --git a/src/Gui/ElideCheckBox.h b/src/Gui/ElideCheckBox.h index 8a32fb9484..7d83d4d1c0 100644 --- a/src/Gui/ElideCheckBox.h +++ b/src/Gui/ElideCheckBox.h @@ -34,7 +34,7 @@ #endif -class ElideCheckBox : public QCheckBox { +class GuiExport ElideCheckBox : public QCheckBox { Q_OBJECT public: diff --git a/src/Gui/ElideLabel.h b/src/Gui/ElideLabel.h index a356392c45..1afa460f90 100644 --- a/src/Gui/ElideLabel.h +++ b/src/Gui/ElideLabel.h @@ -33,7 +33,7 @@ #endif -class ElideLabel : public QLabel { +class GuiExport ElideLabel : public QLabel { Q_OBJECT public: diff --git a/src/Gui/FontScaledSVG.cpp b/src/Gui/FontScaledSVG.cpp new file mode 100644 index 0000000000..9fa10c948e --- /dev/null +++ b/src/Gui/FontScaledSVG.cpp @@ -0,0 +1,65 @@ +/*************************************************************************** + * Copyright (c) 2025 Alfredo Monclus * + * * + * This file is part of the FreeCAD CAx development system. * + * * + * This library is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Library General Public * + * License as published by the Free Software Foundation; either * + * version 2 of the License, or (at your option) any later version. * + * * + * This library is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU Library General Public License for more details. * + * * + * You should have received a copy of the GNU Library General Public * + * License along with this library; see the file COPYING.LIB. If not, * + * write to the Free Software Foundation, Inc., 51 Franklin Street, * + * Fifth Floor, Boston, MA 02110-1301, USA * + * * + ***************************************************************************/ + +// This custom widget scales an svg according to fonts + +#include "FontScaledSVG.h" + +FontScaledSVG::FontScaledSVG(QWidget *parent) + : QWidget(parent), m_svgRenderer(new QSvgRenderer(this)) { + setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); +} + +void FontScaledSVG::setSvg(const QString &svgPath) { + if (m_svgRenderer->load(svgPath)) { + updateScaledSize(); + update(); + } +} + +void FontScaledSVG::paintEvent(QPaintEvent *event) { + Q_UNUSED(event); + QPainter painter(this); + + if (m_svgRenderer->isValid()) { + QRect targetRect(0, 0, width(), height()); + m_svgRenderer->render(&painter, targetRect); + } +} + +void FontScaledSVG::resizeEvent(QResizeEvent *event) { + Q_UNUSED(event); + updateScaledSize(); +} + +void FontScaledSVG::updateScaledSize() { + QSize baseSize = m_svgRenderer->defaultSize(); + + QFontMetrics metrics(font()); + qreal spacing = metrics.lineSpacing(); + int baseFactor = 18; + qreal scalingFactor = spacing / baseFactor; + + QSize targetSize = baseSize * scalingFactor; + setFixedSize(targetSize); +} + diff --git a/src/Gui/FontScaledSVG.h b/src/Gui/FontScaledSVG.h new file mode 100644 index 0000000000..04f4f0ba05 --- /dev/null +++ b/src/Gui/FontScaledSVG.h @@ -0,0 +1,53 @@ +/*************************************************************************** + * Copyright (c) 2025 Alfredo Monclus * + * * + * This file is part of the FreeCAD CAx development system. * + * * + * This library is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Library General Public * + * License as published by the Free Software Foundation; either * + * version 2 of the License, or (at your option) any later version. * + * * + * This library is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU Library General Public License for more details. * + * * + * You should have received a copy of the GNU Library General Public * + * License along with this library; see the file COPYING.LIB. If not, * + * write to the Free Software Foundation, Inc., 51 Franklin Street, * + * Fifth Floor, Boston, MA 02110-1301, USA * + * * + ***************************************************************************/ + +// This custom widget scales an svg according to fonts + +#ifndef FONTSCALEDSVG_H +#define FONTSCALEDSVG_H + +#include "PreCompiled.h" +#ifndef _PreComp_ +#include +#include +#include +#include +#endif + +class GuiExport FontScaledSVG : public QWidget { + Q_OBJECT + +public: + explicit FontScaledSVG(QWidget *parent = nullptr); + void setSvg(const QString &svgPath); + +protected: + void paintEvent(QPaintEvent *event) override; + void resizeEvent(QResizeEvent *event) override; + +private: + QSvgRenderer *m_svgRenderer; + + void updateScaledSize(); +}; + +#endif // FONTSCALEDSVG_H diff --git a/src/Mod/PartDesign/Gui/Resources/images/hole_counterbore_angled.svg b/src/Mod/PartDesign/Gui/Resources/images/hole_counterbore_angled.svg index 1202071514..56891a95aa 100644 --- a/src/Mod/PartDesign/Gui/Resources/images/hole_counterbore_angled.svg +++ b/src/Mod/PartDesign/Gui/Resources/images/hole_counterbore_angled.svg @@ -30,9 +30,9 @@ inkscape:deskcolor="#d1d1d1" inkscape:document-units="px" showgrid="false" - inkscape:zoom="3.4166667" - inkscape:cx="45.073171" - inkscape:cy="90" + inkscape:zoom="4.8318964" + inkscape:cx="28.456736" + inkscape:cy="143.52543" inkscape:window-width="1600" inkscape:window-height="824" inkscape:window-x="0" @@ -109,11 +109,11 @@ id="path1853" sodipodi:nodetypes="cc" />cutDiagram->setPixmap( - QPixmap(QString::fromUtf8((":images/" + baseFileName + ".svg").c_str())) + ui->cutDiagram->setSvg( + QString::fromUtf8((":images/" + baseFileName + ".svg").c_str()) ); } diff --git a/src/Mod/PartDesign/Gui/TaskHoleParameters.ui b/src/Mod/PartDesign/Gui/TaskHoleParameters.ui index 52b761daba..b3eb1e2c68 100644 --- a/src/Mod/PartDesign/Gui/TaskHoleParameters.ui +++ b/src/Mod/PartDesign/Gui/TaskHoleParameters.ui @@ -6,8 +6,8 @@ 0 0 - 391 - 897 + 366 + 844 @@ -239,7 +239,7 @@ the screw's top below the surface - + true @@ -249,9 +249,6 @@ the screw's top below the surface 0 - - - 6 @@ -323,28 +320,25 @@ account for the depth of blind holes - + 0 0 + + + 20 + 40 + + 0 0 - - - - - :/images/hole_counterdrill_angled.svg - - - true - @@ -358,22 +352,6 @@ account for the depth of blind holes 0 - - - - Qt::Vertical - - - QSizePolicy::Fixed - - - - 20 - 8 - - - - @@ -497,22 +475,6 @@ account for the depth of blind holes - - - - Qt::Vertical - - - QSizePolicy::Fixed - - - - 20 - 17 - - - - @@ -1096,6 +1058,12 @@ Note that the calculation can take some time QCheckBox
Gui/ElideCheckBox.h
+ + FontScaledSVG + QWidget +
Gui/FontScaledSVG.h
+ 1 +