diff --git a/src/libs/vmisc/def.h b/src/libs/vmisc/def.h index 6ef4ff1d3..4f67cd982 100644 --- a/src/libs/vmisc/def.h +++ b/src/libs/vmisc/def.h @@ -228,6 +228,7 @@ enum class Vis : ToolVisHolderType GrainlineItem, PieceItem, TextGraphicsItem, + ScenePoint, LAST_ONE_DO_NOT_USE //add new stuffs above this, this constant must be last and never used }; diff --git a/src/libs/vwidgets/vgraphicssimpletextitem.cpp b/src/libs/vwidgets/vgraphicssimpletextitem.cpp index 59c7e32d7..84f7f8696 100644 --- a/src/libs/vwidgets/vgraphicssimpletextitem.cpp +++ b/src/libs/vwidgets/vgraphicssimpletextitem.cpp @@ -46,6 +46,9 @@ #include "vmaingraphicsscene.h" #include "vmaingraphicsview.h" #include "global.h" +#include "vscenepoint.h" + +#define DEFAULT_SCALE 0.0001 //--------------------------------------------------------------------------------------------------------------------- /** @@ -53,7 +56,8 @@ * @param parent parent object. */ VGraphicsSimpleTextItem::VGraphicsSimpleTextItem(QGraphicsItem * parent) - :QGraphicsSimpleTextItem(parent), m_fontSize(0), selectionType(SelectionType::ByMouseRelease) + :QGraphicsSimpleTextItem(parent), m_fontSize(0), selectionType(SelectionType::ByMouseRelease), + m_oldScale(DEFAULT_SCALE) { this->setFlag(QGraphicsItem::ItemIsMovable, true); this->setFlag(QGraphicsItem::ItemIsSelectable, true); @@ -64,6 +68,7 @@ VGraphicsSimpleTextItem::VGraphicsSimpleTextItem(QGraphicsItem * parent) font.setPointSize(font.pointSize()+20); m_fontSize = font.pointSize(); this->setFont(font); + setScale(m_oldScale); } //--------------------------------------------------------------------------------------------------------------------- @@ -73,23 +78,53 @@ VGraphicsSimpleTextItem::VGraphicsSimpleTextItem(QGraphicsItem * parent) * @param parent parent object. */ VGraphicsSimpleTextItem::VGraphicsSimpleTextItem( const QString & text, QGraphicsItem * parent ) - :QGraphicsSimpleTextItem(text, parent), m_fontSize(0), selectionType(SelectionType::ByMouseRelease) + :QGraphicsSimpleTextItem(text, parent), m_fontSize(0), selectionType(SelectionType::ByMouseRelease), + m_oldScale(DEFAULT_SCALE) { this->setFlag(QGraphicsItem::ItemIsMovable, true); this->setFlag(QGraphicsItem::ItemIsSelectable, true); this->setFlag(QGraphicsItem::ItemSendsGeometryChanges, true); this->setAcceptHoverEvents(true); + setScale(m_oldScale); } //--------------------------------------------------------------------------------------------------------------------- void VGraphicsSimpleTextItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { - const qreal scale = SceneScale(scene()); - if (scale > 1) + auto UpdateLine = [this]() { + if (VScenePoint *parent = dynamic_cast(parentItem())) + { + parent->RefreshLine(); + } + }; + + QGraphicsScene *scene = this->scene(); + const qreal scale = SceneScale(scene); + if (scale > 1 && not VFuzzyComparePossibleNulls(m_oldScale, scale)) + { + const QRectF nameRec = boundingRect(); + setTransformOriginPoint(nameRec.center()); setScale(1/scale); + UpdateLine(); + m_oldScale = scale; + } + else if (scale <= 1 && not VFuzzyComparePossibleNulls(m_oldScale, 1.0)) + { + const QRectF nameRec = boundingRect(); + setTransformOriginPoint(nameRec.center()); + setScale(1); + UpdateLine(); + m_oldScale = 1; } + if (scene) + { + if (QGraphicsView *view = scene->views().at(0)) + { + VMainGraphicsView::NewSceneRect(scene, view); + } + } QGraphicsSimpleTextItem::paint(painter, option, widget); } diff --git a/src/libs/vwidgets/vgraphicssimpletextitem.h b/src/libs/vwidgets/vgraphicssimpletextitem.h index 26f232398..34a739448 100644 --- a/src/libs/vwidgets/vgraphicssimpletextitem.h +++ b/src/libs/vwidgets/vgraphicssimpletextitem.h @@ -87,6 +87,7 @@ private: /** @brief fontSize label font size. */ qint32 m_fontSize; SelectionType selectionType; + qreal m_oldScale; }; //--------------------------------------------------------------------------------------------------------------------- diff --git a/src/libs/vwidgets/vscenepoint.h b/src/libs/vwidgets/vscenepoint.h index 25f59f777..55be9e34e 100644 --- a/src/libs/vwidgets/vscenepoint.h +++ b/src/libs/vwidgets/vscenepoint.h @@ -32,6 +32,8 @@ #include #include +#include "../vmisc/def.h" + class VGraphicsSimpleTextItem; class VPointF; class VScaledLine; @@ -42,10 +44,15 @@ public: explicit VScenePoint(QGraphicsItem *parent = nullptr); virtual ~VScenePoint() = default; + virtual int type() const Q_DECL_OVERRIDE {return Type;} + enum { Type = UserType + static_cast(Vis::ScenePoint)}; + virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr) Q_DECL_OVERRIDE; virtual void RefreshPointGeometry(const VPointF &point); + void RefreshLine(); + protected: /** @brief namePoint point label. */ VGraphicsSimpleTextItem *m_namePoint; @@ -63,8 +70,6 @@ protected: virtual void hoverEnterEvent(QGraphicsSceneHoverEvent *event) Q_DECL_OVERRIDE; virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *event) Q_DECL_OVERRIDE; - void RefreshLine(); - void SetOnlyPoint(bool value); bool IsOnlyPoint() const; private: