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
This commit is contained in:
Alfredo Monclus
2025-01-24 16:36:42 -03:00
committed by Chris Hennes
parent de7a237a8a
commit 3202dde7ca
13 changed files with 193 additions and 105 deletions

View File

@@ -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}

View File

@@ -34,7 +34,7 @@
#endif
class ElideCheckBox : public QCheckBox {
class GuiExport ElideCheckBox : public QCheckBox {
Q_OBJECT
public:

View File

@@ -33,7 +33,7 @@
#endif
class ElideLabel : public QLabel {
class GuiExport ElideLabel : public QLabel {
Q_OBJECT
public:

65
src/Gui/FontScaledSVG.cpp Normal file
View File

@@ -0,0 +1,65 @@
/***************************************************************************
* Copyright (c) 2025 Alfredo Monclus <alfredomonclus@gmail.com> *
* *
* 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);
}

53
src/Gui/FontScaledSVG.h Normal file
View File

@@ -0,0 +1,53 @@
/***************************************************************************
* Copyright (c) 2025 Alfredo Monclus <alfredomonclus@gmail.com> *
* *
* 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 <QWidget>
#include <QSvgRenderer>
#include <QPainter>
#include <QFontMetrics>
#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