From fa6c81d23e3239c726d66fdad15009ebe02720d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Br=C3=A6strup=20Sayoc?= Date: Wed, 4 Sep 2024 12:52:51 +0200 Subject: [PATCH] [TechDraw] Fix uncentered balloon label --- src/Mod/TechDraw/Gui/QGCustomText.cpp | 22 ++++++++++++++++++++++ src/Mod/TechDraw/Gui/QGCustomText.h | 3 +++ src/Mod/TechDraw/Gui/QGIViewBalloon.cpp | 25 ++++++++----------------- src/Mod/TechDraw/Gui/QGIViewBalloon.h | 16 ++++++++-------- 4 files changed, 41 insertions(+), 25 deletions(-) diff --git a/src/Mod/TechDraw/Gui/QGCustomText.cpp b/src/Mod/TechDraw/Gui/QGCustomText.cpp index ff64f74751..e07f776d03 100644 --- a/src/Mod/TechDraw/Gui/QGCustomText.cpp +++ b/src/Mod/TechDraw/Gui/QGCustomText.cpp @@ -218,6 +218,28 @@ QPointF QGCustomText::tightBoundingAdjust() const return QPointF(tight.x()-original.x(), tight.y()-original.y()); } +// TODO: when setting position, it doesn't take into account the tight bounding rect +// Meaning top left corner has distance to pos(0, 0) +// Here is a sketch for a fix +// Note that the position adjustment will have to carried out every time the font changes +// void QGCustomText::setPos(const QPointF &pos) { +// if(tightBounding) { +// QGraphicsTextItem::setPos(pos.x() - tightBoundingAdjust().x(), pos.y() - tightBoundingAdjust().y()); +// return; +// } +// QGraphicsTextItem::setPos(pos); +// } + +// void QGCustomText::setPos(qreal x, qreal y) { +// setPos(QPointF(x, y)); +// } + +// QPointF QGCustomText::pos() const +// { +// // Native Qt pos function doesn't take into account the tight bounding rect +// return boundingRect().topLeft(); +// } + QColor QGCustomText::getNormalColor() //preference! { return PreferencesGui::normalQColor(); diff --git a/src/Mod/TechDraw/Gui/QGCustomText.h b/src/Mod/TechDraw/Gui/QGCustomText.h index 47951086a6..5840bf6450 100644 --- a/src/Mod/TechDraw/Gui/QGCustomText.h +++ b/src/Mod/TechDraw/Gui/QGCustomText.h @@ -52,6 +52,9 @@ public: QRectF boundingRect() const override; QRectF tightBoundingRect() const; QPointF tightBoundingAdjust() const; + // void setPos(const QPointF &pos); + // void setPos(qreal x, qreal y); + // QPointF pos() const; void setHighlighted(bool state); virtual void setPrettyNormal(); diff --git a/src/Mod/TechDraw/Gui/QGIViewBalloon.cpp b/src/Mod/TechDraw/Gui/QGIViewBalloon.cpp index 9d467132bf..f4e2ebec89 100644 --- a/src/Mod/TechDraw/Gui/QGIViewBalloon.cpp +++ b/src/Mod/TechDraw/Gui/QGIViewBalloon.cpp @@ -66,8 +66,6 @@ using DGU = DrawGuiUtil; QGIBalloonLabel::QGIBalloonLabel() { - posX = 0; - posY = 0; m_ctrl = false; m_drag = false; @@ -78,6 +76,7 @@ QGIBalloonLabel::QGIBalloonLabel() setAcceptHoverEvents(true); m_labelText = new QGCustomText(); + m_labelText->setTightBounding(true); m_labelText->setParentItem(this); verticalSep = false; @@ -99,7 +98,6 @@ QVariant QGIBalloonLabel::itemChange(GraphicsItemChange change, const QVariant& update(); } else if (change == ItemPositionHasChanged && scene()) { - setLabelCenter(); if (m_drag) { Q_EMIT dragging(m_ctrl); } @@ -195,20 +193,13 @@ void QGIBalloonLabel::paint(QPainter* painter, const QStyleOptionGraphicsItem* o void QGIBalloonLabel::setPosFromCenter(const double& xCenter, const double& yCenter) { //set label's Qt position(top, left) given boundingRect center point - setPos(xCenter - m_labelText->boundingRect().width() / 2., - yCenter - m_labelText->boundingRect().height() / 2.); -} - -void QGIBalloonLabel::setLabelCenter() -{ - //save label's bRect center (posX, posY) given Qt position (top, left) - posX = x() + m_labelText->boundingRect().width() / 2.; - posY = y() + m_labelText->boundingRect().height() / 2.; + setPos(xCenter - m_labelText->boundingRect().center().x(), + yCenter - m_labelText->boundingRect().center().y()); } Base::Vector3d QGIBalloonLabel::getLabelCenter() const { - return Base::Vector3d(posX, posY, 0.0); + return Base::Vector3d(getCenterX(), getCenterY(), 0.0); } void QGIBalloonLabel::setFont(QFont font) { m_labelText->setFont(font); } @@ -504,8 +495,8 @@ void QGIViewBalloon::balloonLabelDragFinished() scale = balloonParent->getScale(); //set feature position (x, y) from graphic position - double x = Rez::appX(balloonLabel->X() / scale); - double y = Rez::appX(balloonLabel->Y() / scale); + double x = Rez::appX(balloonLabel->getCenterX() / scale); + double y = Rez::appX(balloonLabel->getCenterY() / scale); Gui::Command::openCommand(QT_TRANSLATE_NOOP("Command", "Drag Balloon")); Gui::Command::doCommand(Gui::Command::Doc, "App.ActiveDocument.%s.X = %f", dvb->getNameInDocument(), x); @@ -1002,8 +993,8 @@ void QGIViewBalloon::getBalloonPoints(TechDraw::DrawViewBalloon* balloon, DrawVi arrowTipPosInParent = DGU::toGuiPoint(refObj, originApp); } else { - x = balloonLabel->X(); - y = -balloonLabel->Y(); // invert from Qt scene units to R2 mm + x = balloonLabel->getCenterX(); + y = -balloonLabel->getCenterY(); // invert from Qt scene units to R2 mm if (m_originDragged) { // moving the whole bubble object. do not adjust origin point. arrowTipPosInParent = arrowPosInDrag(); diff --git a/src/Mod/TechDraw/Gui/QGIViewBalloon.h b/src/Mod/TechDraw/Gui/QGIViewBalloon.h index be657bb804..671b6e7df4 100644 --- a/src/Mod/TechDraw/Gui/QGIViewBalloon.h +++ b/src/Mod/TechDraw/Gui/QGIViewBalloon.h @@ -80,14 +80,15 @@ public: void setLabelCenter(); Base::Vector3d getLabelCenter() const; void setPosFromCenter(const double& xCenter, const double& yCenter); - double X() const + + double getCenterX() const { - return posX; + return mapToParent(m_labelText->boundingRect().center()).x(); } - double Y() const + double getCenterY() const { - return posY; - }//minus posY? + return mapToParent(m_labelText->boundingRect().center()).y(); + } void setFont(QFont font); QFont getFont() @@ -114,6 +115,7 @@ public: void setDimText(QGCustomText* newText) { + newText->setTightBounding(true); m_labelText = newText; } bool getVerticalSep() const @@ -132,6 +134,7 @@ public: { seps = newSeps; } + QGCustomText* m_labelText; Q_SIGNALS: void dragging(bool state); @@ -153,11 +156,8 @@ private: bool verticalSep; std::vector seps; - QGCustomText* m_labelText; QColor m_colNormal; - double posX; - double posY; bool m_ctrl; bool m_drag; };