From cfde3232eb1628488bf82a9efb0309f0de71038a Mon Sep 17 00:00:00 2001 From: blacksun Date: Fri, 26 Sep 2014 08:48:55 +0000 Subject: [PATCH] Conductor : improve text position git-svn-id: svn+ssh://svn.tuxfamily.org/svnroot/qet/qet/trunk@3333 bfdf4180-ca20-0410-9c96-a3a8aa849046 --- sources/qetgraphicsitem/conductor.cpp | 73 +++++++++++++++++++++++---- sources/qetgraphicsitem/conductor.h | 1 + 2 files changed, 63 insertions(+), 11 deletions(-) diff --git a/sources/qetgraphicsitem/conductor.cpp b/sources/qetgraphicsitem/conductor.cpp index 75f053327..ee9eb3791 100644 --- a/sources/qetgraphicsitem/conductor.cpp +++ b/sources/qetgraphicsitem/conductor.cpp @@ -1125,29 +1125,80 @@ ConductorSegment *Conductor::middleSegment() { } /** - Positionne le texte du conducteur au milieu du segment qui contient le - point au milieu du conducteur - @see middleSegment() -*/ + * @brief Conductor::posForText + * Calculate and return the better pos for text. + * @param flag : flag is used to know if text pos is near of + * a vertical or horizontal conductor segment. + */ +QPointF Conductor::posForText(Qt::Orientations &flag) { + + ConductorSegment *segment = segments; + + //Go to first segement + while (!segment->isFirstSegment()) { + segment = segment->previousSegment(); + } + + + QPointF p1 = segment -> firstPoint(); // hasNextSegment()) { + segment = segment -> nextSegment(); + if (segment -> length() > biggest_segment -> length()) + biggest_segment = segment; + } + + QPointF p2 = segment -> secondPoint();// p2.y()) { + p1.setY(p1.y() - (length()/2)); + } else { + p1.setY(p1.y() + (length()/2)); + } + } else if (p1.y() == p2.y()) { // p2.x()) { + p1.setX(p1.x() - (length()/2)); + } else { + p1.setX(p1.x() + (length()/2)); + } + } else { //Return the point at the middle of biggest segment. + p1 = biggest_segment->middle(); + flag = (biggest_segment->isHorizontal())? Qt::Horizontal : Qt::Vertical; + } + return p1; +} + +/** + * @brief Conductor::calculateTextItemPosition + * Move the text at middle of conductor (if is vertical or horizontal) + * otherwise, move conductor at the middle of the longest segment of conductor. + * If text was moved by user, this function do nothing, except check if text is near conductor. + */ void Conductor::calculateTextItemPosition() { if (!text_item) return; //position if (text_item -> wasMovedByUser()) { - // le champ de texte a ete deplace par l'utilisateur : - // on verifie qu'il est encore a proximite du conducteur + //Text field was moved by user : + //we check if text field is yet near the conductor QPointF text_item_pos = text_item -> pos(); QPainterPath near_shape = nearShape(); if (!near_shape.contains(text_item_pos)) { text_item -> setPos(movePointIntoPolygon(text_item_pos, near_shape)); } } else { - // positionnement automatique basique - text_item -> setPos(middleSegment() -> middle()); - //rotation + //Position of text is calculated and also is rotation + Qt::Orientations rotation; + text_item -> setPos(posForText(rotation)); if (!text_item -> wasRotateByUser()) { - middleSegment() -> isVertical()? text_item -> setRotationAngle(properties_.verti_rotate_text): - text_item -> setRotationAngle(properties_.horiz_rotate_text); + rotation == Qt::Vertical ? text_item -> setRotationAngle(properties_.verti_rotate_text): + text_item -> setRotationAngle(properties_.horiz_rotate_text); } } } diff --git a/sources/qetgraphicsitem/conductor.h b/sources/qetgraphicsitem/conductor.h index 8ea43fa64..b1ca05298 100644 --- a/sources/qetgraphicsitem/conductor.h +++ b/sources/qetgraphicsitem/conductor.h @@ -88,6 +88,7 @@ class Conductor : public QObject, public QGraphicsPathItem { virtual bool isNearConductor(const QPointF &); qreal length(); ConductorSegment *middleSegment(); + QPointF posForText(Qt::Orientations &flag); bool containsPoint(const QPointF &) const; QString text() const; void setText(const QString &);