Skip to content

Commit

Permalink
[TechDraw] Fix uncentered balloon label
Browse files Browse the repository at this point in the history
  • Loading branch information
benj5378 authored and WandererFan committed Sep 12, 2024
1 parent 614a14f commit 5785cb5
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 25 deletions.
22 changes: 22 additions & 0 deletions src/Mod/TechDraw/Gui/QGCustomText.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
3 changes: 3 additions & 0 deletions src/Mod/TechDraw/Gui/QGCustomText.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ class TechDrawGuiExport QGCustomText : public QGraphicsTextItem
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();
Expand Down
25 changes: 8 additions & 17 deletions src/Mod/TechDraw/Gui/QGIViewBalloon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,6 @@ using DGU = DrawGuiUtil;

QGIBalloonLabel::QGIBalloonLabel()
{
posX = 0;
posY = 0;
m_ctrl = false;
m_drag = false;

Expand All @@ -78,6 +76,7 @@ QGIBalloonLabel::QGIBalloonLabel()
setAcceptHoverEvents(true);

m_labelText = new QGCustomText();
m_labelText->setTightBounding(true);
m_labelText->setParentItem(this);

verticalSep = false;
Expand All @@ -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);
}
Expand Down Expand Up @@ -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); }
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand Down
16 changes: 8 additions & 8 deletions src/Mod/TechDraw/Gui/QGIViewBalloon.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,15 @@ class QGIBalloonLabel: public QGraphicsObject
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()
Expand All @@ -114,6 +115,7 @@ class QGIBalloonLabel: public QGraphicsObject

void setDimText(QGCustomText* newText)
{
newText->setTightBounding(true);
m_labelText = newText;
}
bool getVerticalSep() const
Expand All @@ -132,6 +134,7 @@ class QGIBalloonLabel: public QGraphicsObject
{
seps = newSeps;
}
QGCustomText* m_labelText;

Q_SIGNALS:
void dragging(bool state);
Expand All @@ -153,11 +156,8 @@ class QGIBalloonLabel: public QGraphicsObject
bool verticalSep;
std::vector<int> seps;

QGCustomText* m_labelText;
QColor m_colNormal;

double posX;
double posY;
bool m_ctrl;
bool m_drag;
};
Expand Down

0 comments on commit 5785cb5

Please sign in to comment.