diff --git a/src/Mod/TechDraw/Gui/DlgPrefsTechDraw.ui b/src/Mod/TechDraw/Gui/DlgPrefsTechDraw.ui index 22ece13fc5c6..4626cf662fe4 100644 --- a/src/Mod/TechDraw/Gui/DlgPrefsTechDraw.ui +++ b/src/Mod/TechDraw/Gui/DlgPrefsTechDraw.ui @@ -707,7 +707,7 @@ View Label size in units - 6.000000000000000 + 8.000000000000000 LabelSize diff --git a/src/Mod/TechDraw/Gui/DlgPrefsTechDraw2.ui b/src/Mod/TechDraw/Gui/DlgPrefsTechDraw2.ui index 6b5587e6786b..6eca55cea92f 100644 --- a/src/Mod/TechDraw/Gui/DlgPrefsTechDraw2.ui +++ b/src/Mod/TechDraw/Gui/DlgPrefsTechDraw2.ui @@ -259,7 +259,7 @@ Dimension font size in units - 3.500000000000000 + 5.000000000000000 FontSize diff --git a/src/Mod/TechDraw/Gui/MDIViewPage.cpp b/src/Mod/TechDraw/Gui/MDIViewPage.cpp index 635459c9d69a..ebff473784d2 100644 --- a/src/Mod/TechDraw/Gui/MDIViewPage.cpp +++ b/src/Mod/TechDraw/Gui/MDIViewPage.cpp @@ -1374,4 +1374,13 @@ void MDIViewPage::showStatusMsg(const char* s1, const char* s2, const char* s3) } } +MDIViewPage *MDIViewPage::getFromScene(const QGraphicsScene *scene) +{ + if (scene != nullptr && scene->parent() != nullptr) { + return dynamic_cast(scene->parent()); + } + + return nullptr; +} + #include diff --git a/src/Mod/TechDraw/Gui/MDIViewPage.h b/src/Mod/TechDraw/Gui/MDIViewPage.h index 3ef99c037a92..f25346c1b21b 100644 --- a/src/Mod/TechDraw/Gui/MDIViewPage.h +++ b/src/Mod/TechDraw/Gui/MDIViewPage.h @@ -71,7 +71,7 @@ class TechDrawGuiExport MDIViewPage : public Gui::MDIView, public Gui::Selection void matchSceneRectToTemplate(void); bool onMsg(const char* pMsg,const char** ppReturn); - bool onHasMsg(const char* pMsg) const; + bool onHasMsg(const char* pMsg) const; void print(); void print(QPrinter* printer); @@ -101,6 +101,8 @@ class TechDrawGuiExport MDIViewPage : public Gui::MDIView, public Gui::Selection bool addView(const App::DocumentObject *obj); + static MDIViewPage *getFromScene(const QGraphicsScene *scene); + public Q_SLOTS: void viewAll(); void saveSVG(void); diff --git a/src/Mod/TechDraw/Gui/QGCustomLabel.cpp b/src/Mod/TechDraw/Gui/QGCustomLabel.cpp index c2b5db434b04..4ab194f3a933 100644 --- a/src/Mod/TechDraw/Gui/QGCustomLabel.cpp +++ b/src/Mod/TechDraw/Gui/QGCustomLabel.cpp @@ -77,23 +77,5 @@ void QGCustomLabel::paint ( QPainter * painter, const QStyleOptionGraphicsItem * QStyleOptionGraphicsItem myOption(*option); myOption.state &= ~QStyle::State_Selected; - //see QGCustomText for explanation of this code - double dppt = 3.53; - double svgMagicX = Rez::guiX(8.0); - double svgMagicY = Rez::guiX(12.0); - double svgMagicYoffset = Rez::guiX(3.0); - double fontSize = Rez::appX(font().pointSizeF()); - double ty = svgMagicY + (svgMagicYoffset*fontSize)/dppt; - QPointF svgMove(-svgMagicX/dppt,ty); - - QPaintDevice* hw = painter->device(); - QSvgGenerator* svg = dynamic_cast(hw); - if (svg) { - painter->scale(Rez::appX(dppt),Rez::appX(dppt)); - painter->translate(svgMove); - } else { - painter->scale(1.0,1.0); - } - QGraphicsTextItem::paint (painter, &myOption, widget); } diff --git a/src/Mod/TechDraw/Gui/QGCustomText.cpp b/src/Mod/TechDraw/Gui/QGCustomText.cpp index eb9da437dbf3..c6796977d1f3 100644 --- a/src/Mod/TechDraw/Gui/QGCustomText.cpp +++ b/src/Mod/TechDraw/Gui/QGCustomText.cpp @@ -131,32 +131,6 @@ void QGCustomText::paint ( QPainter * painter, const QStyleOptionGraphicsItem * QStyleOptionGraphicsItem myOption(*option); myOption.state &= ~QStyle::State_Selected; - //svg text is much larger than screen text. scene units(mm or 0.1mm in hirez) vs points. - //need to scale text if going to svg. - //TODO: magic translation happens. why? approx: right ~8mm down: 12mm + (3mm per mm of text height) - //SVG transform matrix translation values are different for same font size + different fonts (Sans vs Ubuntu vs Arial)??? - // scale values are same for same font size + different fonts. - //calculate dots/mm - //in hirez - say factor = 10, we have 10 dpmm in scene space. - // so 254dpi / 72pts/in => 3.53 dppt - double dpmm = 3.53; //dots/pt - double svgMagicX = Rez::guiX(8.0); //8mm -> 80 gui dots - double svgMagicY = Rez::guiX(12.0); - double svgMagicYoffset = Rez::guiX(3.0); // 3mm per mm of font size => 30gunits / mm of font size - double fontSize = Rez::appX(font().pointSizeF()); //gui pts 4mm text * 10 scunits/mm = size 40 text but still only 4mm - double ty = svgMagicY + (svgMagicYoffset*fontSize)/dpmm; - // 12mm (in gunits) + [3mm (in gunits) * (# of mm)]/ [dots per mm] works out to dots? - QPointF svgMove(-svgMagicX/dpmm,ty); - - QPaintDevice* hw = painter->device(); - QSvgGenerator* svg = dynamic_cast(hw); - if (svg) { - painter->scale(Rez::appX(dpmm),Rez::appX(dpmm)); - painter->translate(svgMove); - } else { - painter->scale(1.0,1.0); - } - // painter->drawRect(boundingRect()); //good for debugging setDefaultTextColor(m_colCurrent); diff --git a/src/Mod/TechDraw/Gui/QGIHighlight.cpp b/src/Mod/TechDraw/Gui/QGIHighlight.cpp index f55154f7b11f..f9bf1903bb59 100644 --- a/src/Mod/TechDraw/Gui/QGIHighlight.cpp +++ b/src/Mod/TechDraw/Gui/QGIHighlight.cpp @@ -84,7 +84,7 @@ void QGIHighlight::makeHighlight() void QGIHighlight::makeReference() { prepareGeometryChange(); - m_refFont.setPointSize(m_refSize); + m_refFont.setPointSizeF(QGIView::calculateFontPointSizeF(this, m_refSize)); m_reference->setFont(m_refFont); m_reference->setPlainText(QString::fromUtf8(m_refText)); double fudge = Rez::guiX(1.0); diff --git a/src/Mod/TechDraw/Gui/QGISectionLine.cpp b/src/Mod/TechDraw/Gui/QGISectionLine.cpp index 0b2c7011632f..916353df849a 100644 --- a/src/Mod/TechDraw/Gui/QGISectionLine.cpp +++ b/src/Mod/TechDraw/Gui/QGISectionLine.cpp @@ -203,7 +203,7 @@ void QGISectionLine::makeSymbolsTrad() extLineStart = m_start + offset; extLineEnd = m_end + offset; prepareGeometryChange(); - m_symFont.setPointSize(m_symSize); + m_symFont.setPointSizeF(QGIView::calculateFontPointSizeF(this, m_symSize)); m_symbol1->setFont(m_symFont); m_symbol1->setPlainText(QString::fromUtf8(m_symbol)); if (m_arrowDir.y < 0.0) { //pointing down @@ -246,7 +246,7 @@ void QGISectionLine::makeSymbolsISO() symPosEnd = m_end - offset; prepareGeometryChange(); - m_symFont.setPointSize(m_symSize); + m_symFont.setPointSizeF(QGIView::calculateFontPointSizeF(this, m_symSize)); m_symbol1->setFont(m_symFont); m_symbol1->setPlainText(QString::fromUtf8(m_symbol)); m_symbol1->centerAt(symPosStart); diff --git a/src/Mod/TechDraw/Gui/QGIView.cpp b/src/Mod/TechDraw/Gui/QGIView.cpp index eae86d85b8d8..f773e48ce01a 100644 --- a/src/Mod/TechDraw/Gui/QGIView.cpp +++ b/src/Mod/TechDraw/Gui/QGIView.cpp @@ -98,7 +98,7 @@ QGIView::QGIView() m_pen.setColor(m_colCurrent); //Border/Label styling - m_font.setPointSize(getPrefFontSize()); //scene units (mm), not points + m_font.setPointSizeF(calculateFontPointSizeF(getPrefFontSize())); m_decorPen.setStyle(Qt::DashLine); m_decorPen.setWidth(0); // 0 => 1px "cosmetic pen" @@ -430,7 +430,7 @@ void QGIView::drawCaption() QRectF displayArea = customChildrenBoundingRect(); m_caption->setDefaultTextColor(m_colCurrent); m_font.setFamily(getPrefFont()); - m_font.setPointSize(getPrefFontSize()); //scene units (0.1 mm), not points + m_font.setPointSizeF(calculateFontPointSizeF(getPrefFontSize())); m_caption->setFont(m_font); QString captionStr = QString::fromUtf8(getViewObject()->Caption.getValue()); m_caption->setPlainText(captionStr); @@ -473,7 +473,8 @@ void QGIView::drawBorder() m_label->setDefaultTextColor(m_colCurrent); m_font.setFamily(getPrefFont()); - m_font.setPointSize(getPrefFontSize()); //scene units (0.1 mm), not points + m_font.setPointSizeF(calculateFontPointSizeF(getPrefFontSize())); + m_label->setFont(m_font); QString labelStr = QString::fromUtf8(getViewObject()->Label.getValue()); m_label->setPlainText(labelStr); @@ -608,21 +609,10 @@ QGVPage* QGIView::getGraphicsView(TechDraw::DrawView* dv) } return graphicsView; } + MDIViewPage* QGIView::getMDIViewPage(void) const { - MDIViewPage* result = nullptr; - QGraphicsScene* s = scene(); - QObject* parent = nullptr; - if (s != nullptr) { - parent = s->parent(); - } - if (parent != nullptr) { - MDIViewPage* mdi = dynamic_cast(parent); - if (mdi != nullptr) { - result = mdi; - } - } - return result; + return MDIViewPage::getFromScene(scene()); } bool QGIView::getFrameState(void) @@ -690,10 +680,37 @@ double QGIView::getPrefFontSize() { Base::Reference hGrp = App::GetApplication().GetUserParameter(). GetGroup("BaseApp")->GetGroup("Preferences")->GetGroup("Mod/TechDraw/Labels"); - double fontSize = hGrp->GetFloat("LabelSize", 3.5); - return Rez::guiX(fontSize); + return hGrp->GetFloat("LabelSize", DefaultFontSizeInMM); +} + +double QGIView::calculateFontPointSizeF(const QGraphicsItem *item, double sizeInMillimetres) +{ + const QWidget *widget = MDIViewPage::getFromScene(item->scene()); + if (widget == nullptr && !QApplication::topLevelWidgets().isEmpty()) { + // Fallback to some top level window if we are not assigned to a scene/widget yet + widget = QApplication::topLevelWidgets().first(); + } + + double logicalDPI = 96.0; // This is the most common value in PC world + if (widget != nullptr) { + logicalDPI = widget->logicalDpiY(); + } + + // Now calculate the correct font size to be used by a QGraphicsTextItem: + // 1) Start with font size in mm. + // 2) Convert it to "pixels" via Rex::GuiX(). + // 3) Convert "pixels" to inches dividing them by DPI. + // 4) Convert inches to points - there are exactly 72 points to one inch. + return 72.0*Rez::guiX(sizeInMillimetres)/logicalDPI; } +double QGIView::calculateFontPointSizeF(double sizeInMillimetres) const +{ + return calculateFontPointSizeF(this, sizeInMillimetres); +} + +const double QGIView::DefaultFontSizeInMM = 5.0; + void QGIView::dumpRect(char* text, QRectF r) { Base::Console().Message("DUMP - %s - rect: (%.3f,%.3f) x (%.3f,%.3f)\n",text, r.left(),r.top(),r.right(),r.bottom()); diff --git a/src/Mod/TechDraw/Gui/QGIView.h b/src/Mod/TechDraw/Gui/QGIView.h index d8bd58e7375b..b4975eb59f59 100644 --- a/src/Mod/TechDraw/Gui/QGIView.h +++ b/src/Mod/TechDraw/Gui/QGIView.h @@ -111,7 +111,11 @@ class TechDrawGuiExport QGIView : public QObject, public QGraphicsItemGroup static Gui::ViewProvider* getViewProvider(App::DocumentObject* obj); static QGVPage* getGraphicsView(TechDraw::DrawView* dv); + static double calculateFontPointSizeF(const QGraphicsItem *graphicsItem, double sizeInMillimetres); + static const double DefaultFontSizeInMM; + MDIViewPage* getMDIViewPage(void) const; + // Mouse handling virtual void mousePressEvent(QGraphicsSceneMouseEvent *event) override; boost::signals2::signal signalSelectPoint; @@ -133,6 +137,8 @@ public Q_SLOTS: QString getPrefFont(void); double getPrefFontSize(void); + double calculateFontPointSizeF(double sizeInMillimetres) const; + Base::Reference getParmGroupCol(void); TechDraw::DrawView *viewObj; diff --git a/src/Mod/TechDraw/Gui/QGIViewBalloon.cpp b/src/Mod/TechDraw/Gui/QGIViewBalloon.cpp index f97106bbdcad..384fc704bc56 100644 --- a/src/Mod/TechDraw/Gui/QGIViewBalloon.cpp +++ b/src/Mod/TechDraw/Gui/QGIViewBalloon.cpp @@ -181,7 +181,7 @@ void QGIViewBalloon::placeBalloon(QPointF pos) balloon->Text.setValue(std::to_string(idx).c_str()); QFont font = balloonLabel->getFont(); - font.setPointSizeF(Rez::guiX(vp->Fontsize.getValue())); + font.setPointSizeF(calculateFontPointSizeF(vp->Fontsize.getValue())); font.setFamily(QString::fromUtf8(vp->Font.getValue())); balloonLabel->setFont(font); prepareGeometryChange(); @@ -263,7 +263,7 @@ void QGIViewBalloon::updateBalloon(bool obtuse) } QFont font = balloonLabel->getFont(); - font.setPointSizeF(Rez::guiX(vp->Fontsize.getValue())); + font.setPointSizeF(calculateFontPointSizeF(vp->Fontsize.getValue())); font.setFamily(QString::fromUtf8(vp->Font.getValue())); balloonLabel->setFont(font); diff --git a/src/Mod/TechDraw/Gui/QGIViewDimension.cpp b/src/Mod/TechDraw/Gui/QGIViewDimension.cpp index 76be5a2495a4..498752750b92 100644 --- a/src/Mod/TechDraw/Gui/QGIViewDimension.cpp +++ b/src/Mod/TechDraw/Gui/QGIViewDimension.cpp @@ -402,10 +402,6 @@ void QGIViewDimension::updateView(bool update) } else if(vp->Fontsize.isTouched() || vp->Font.isTouched()) { - QFont font = datumLabel->getFont(); - font.setPointSizeF(Rez::guiX(vp->Fontsize.getValue())); - font.setFamily(QString::fromLatin1(vp->Font.getValue())); - datumLabel->setFont(font); updateDim(); } else if (vp->LineWidth.isTouched()) { //never happens!! m_lineWidth = vp->LineWidth.getValue(); @@ -432,10 +428,10 @@ void QGIViewDimension::updateDim(bool obtuse) QString labelText = QString::fromUtf8(dim->getFormatedValue(m_obtuse).c_str()); QFont font = datumLabel->getFont(); - font.setPointSizeF(Rez::guiX(vp->Fontsize.getValue())); font.setFamily(QString::fromUtf8(vp->Font.getValue())); - + font.setPointSizeF(calculateFontPointSizeF(this, vp->Fontsize.getValue())); datumLabel->setFont(font); + prepareGeometryChange(); datumLabel->setDimString(labelText); datumLabel->setTolString(); diff --git a/src/Mod/TechDraw/Gui/QGVPage.cpp b/src/Mod/TechDraw/Gui/QGVPage.cpp index b2f60beda5ae..5e20ce9c237e 100644 --- a/src/Mod/TechDraw/Gui/QGVPage.cpp +++ b/src/Mod/TechDraw/Gui/QGVPage.cpp @@ -96,6 +96,7 @@ #include "ZVALUE.h" #include "ViewProviderPage.h" #include "QGVPage.h" +#include "MDIViewPage.h" using namespace Gui; using namespace TechDraw; @@ -746,7 +747,9 @@ void QGVPage::saveSvg(QString filename) // the width and height attributes of the element." >> but Inkscape won't read it without size info?? svgGen.setViewBox(QRect(0, 0, Rez::guiX(page->getPageWidth()), Rez::guiX(page->getPageHeight()))); - svgGen.setResolution(Rez::guiX(25.4)); // docs say this is DPI. Rez::guiX(1dot/mm) so 254 dpi? + // Set resolution in DPI. To keep text dimensions as they are on screen, + // use the very same resolution the screen paint device reports. + svgGen.setResolution(MDIViewPage::getFromScene(scene())->logicalDpiY()); svgGen.setTitle(QObject::tr("FreeCAD SVG Export")); svgGen.setDescription(svgDescription); diff --git a/src/Mod/TechDraw/Gui/ViewProviderBalloon.cpp b/src/Mod/TechDraw/Gui/ViewProviderBalloon.cpp index 7e9dc544a3a3..fd42d55b67a7 100644 --- a/src/Mod/TechDraw/Gui/ViewProviderBalloon.cpp +++ b/src/Mod/TechDraw/Gui/ViewProviderBalloon.cpp @@ -70,7 +70,7 @@ ViewProviderBalloon::ViewProviderBalloon() hGrp = App::GetApplication().GetUserParameter() .GetGroup("BaseApp")->GetGroup("Preferences")->GetGroup("Mod/TechDraw/Dimensions"); - double fontSize = hGrp->GetFloat("FontSize", 3.5); + double fontSize = hGrp->GetFloat("FontSize", QGIView::DefaultFontSizeInMM); ADD_PROPERTY_TYPE(Font ,(fontName.c_str()),group,App::Prop_None, "The name of the font to use"); ADD_PROPERTY_TYPE(Fontsize,(fontSize) ,group,(App::PropertyType)(App::Prop_None),"Dimension text size in units"); diff --git a/src/Mod/TechDraw/Gui/ViewProviderDimension.cpp b/src/Mod/TechDraw/Gui/ViewProviderDimension.cpp index b2ef6ddaf4e8..3f5ad9ed3eca 100644 --- a/src/Mod/TechDraw/Gui/ViewProviderDimension.cpp +++ b/src/Mod/TechDraw/Gui/ViewProviderDimension.cpp @@ -60,7 +60,7 @@ ViewProviderDimension::ViewProviderDimension() hGrp = App::GetApplication().GetUserParameter() .GetGroup("BaseApp")->GetGroup("Preferences")->GetGroup("Mod/TechDraw/Dimensions"); - double fontSize = hGrp->GetFloat("FontSize", 3.5); + double fontSize = hGrp->GetFloat("FontSize", QGIView::DefaultFontSizeInMM); ADD_PROPERTY_TYPE(Font ,(fontName.c_str()),group,App::Prop_None, "The name of the font to use"); ADD_PROPERTY_TYPE(Fontsize,(fontSize) ,group,(App::PropertyType)(App::Prop_None),"Dimension text size in units");