diff --git a/sources/diagramcommands.cpp b/sources/diagramcommands.cpp index 660701baf..ba2f15733 100644 --- a/sources/diagramcommands.cpp +++ b/sources/diagramcommands.cpp @@ -286,7 +286,7 @@ void DeleteElementsCommand::redo() { QList conductor_list; conductor_list << c -> relatedPotentialConductors(false).toList(); if (conductor_list.count()) { - conductor_list.first() -> adjustTextItemPosition(); + conductor_list.first() -> calculateTextItemPosition(); } } } @@ -780,7 +780,7 @@ void RotateElementsCommand::undo() { if (ConductorTextItem *cti = qgraphicsitem_cast(dti)) { cti -> forceRotateByUser(previous_rotate_by_user_[cti]); (cti -> wasRotateByUser()) ? cti -> rotateBy(-applied_rotation_angle_) : - cti -> parentConductor() -> adjustTextItemPosition(); + cti -> parentConductor() -> calculateTextItemPosition(); } else {dti -> rotateBy(-applied_rotation_angle_);} } diff --git a/sources/qetgraphicsitem/conductor.cpp b/sources/qetgraphicsitem/conductor.cpp index 880917ab8..c8624498d 100644 --- a/sources/qetgraphicsitem/conductor.cpp +++ b/sources/qetgraphicsitem/conductor.cpp @@ -1090,18 +1090,11 @@ const QList Conductor::segmentsList() const { } /** - @return La longueur totale du conducteur -*/ -qreal Conductor::length() { - qreal length = 0.0; - - ConductorSegment *s = segments; - while (s -> hasNextSegment()) { - length += qAbs(s -> length()); - s = s -> nextSegment(); - } - - return(length); + * @brief Conductor::length + * @return the length of this conductor + */ +qreal Conductor::length() const{ + return path().length(); } /** @@ -1191,15 +1184,7 @@ void Conductor::calculateTextItemPosition() { if (!text_item || !diagram()) return; if (diagram() -> defaultConductorProperties.m_one_text_per_folio == true) { - QSet conductor_list = relatedPotentialConductors(false); - Conductor *longuest_conductor = this; - - //Search the longuest conductor - foreach (Conductor *c, conductor_list) { - if (c -> length() > longuest_conductor -> length()) { - longuest_conductor = c; - } - } + Conductor *longuest_conductor = longuestConductorInPotential(this); //The longuest conductor isn't this conductor //we call calculateTextItemPosition of the longuest conductor @@ -1210,7 +1195,7 @@ void Conductor::calculateTextItemPosition() { //At this point this conductor is the longuest conductor //we hide all text of conductor_list - foreach (Conductor *c, conductor_list) { + foreach (Conductor *c, relatedPotentialConductors(false)) { c -> textItem() -> setVisible(false); } } @@ -1350,15 +1335,6 @@ void Conductor::readProperties() { calculateTextItemPosition(); } -/** - S'assure que le texte du conducteur est a une position raisonnable - Cette methode ne fait rien si ce conducteur n'affiche pas son champ de - texte. -*/ -void Conductor::adjustTextItemPosition() { - calculateTextItemPosition(); -} - /** @return true si le conducteur est mis en evidence */ @@ -1417,17 +1393,6 @@ void Conductor::displayedTextChanged() { } } -/** - @return les conducteurs avec lesquels ce conducteur partage des bornes - communes -*/ -QSet Conductor::relatedConductors() const { - QList other_conductors_list = terminal1 -> conductors(); - other_conductors_list += terminal2 -> conductors(); - QSet other_conductors = other_conductors_list.toSet(); - other_conductors.remove(const_cast(this)); - return(other_conductors); -} /** * @brief Conductor::relatedPotentialConductors @@ -1526,20 +1491,6 @@ void Conductor::editProperty() { ConductorPropertiesDialog::PropertiesDialog(this, diagramEditor()); } -/** - @param a reel - @param b reel - @param c reel - @return true si a est entre b et c ou est egal a l'un des deux -*/ -bool isBetween(qreal a, qreal b, qreal c) { - if (b <= c) { - return(a >= b && a <= c); - } else { - return(a <= b && a >= c); - } -} - /** @param a point @param b point @@ -1560,7 +1511,7 @@ QList Conductor::junctions() const { QList junctions_list; // pour qu'il y ait des jonctions, il doit y avoir d'autres conducteurs et des bifurcations - QSet other_conductors = relatedConductors(); + QList other_conductors = relatedConductors(this); QList bends_list = bends(); if (other_conductors.isEmpty() || bends_list.isEmpty()) { return(junctions_list); @@ -1787,3 +1738,32 @@ QPointF Conductor::movePointIntoPolygon(const QPointF &point, const QPainterPath return(points.at(point_index)); } } + +/** + * @brief longuestConductorInPotential + * @param conductor : a conductor in the potential to search + * @param all_diagram : true -> search in the whole project, false -> search only in the diagram of conductor + * @return the longuest conductor in the same potential of conductor + */ +Conductor * longuestConductorInPotential(Conductor *conductor, bool all_diagram) { + Conductor *longuest_conductor = conductor; + //Search the longuest conductor + foreach (Conductor *c, conductor -> relatedPotentialConductors(all_diagram)) + if (c -> length() > longuest_conductor -> length()) + longuest_conductor = c; + + return longuest_conductor; +} + +/** + * @brief relatedConductors + * @param conductor + * @return return all conductors who share the same terminals of @conductor given as parametre, + * except @conductor himself. + */ +QList relatedConductors(const Conductor *conductor) { + QList other_conductors_list = conductor -> terminal1 -> conductors(); + other_conductors_list << conductor -> terminal2->conductors(); + other_conductors_list.removeAll(const_cast (conductor)); + return(other_conductors_list); +} diff --git a/sources/qetgraphicsitem/conductor.h b/sources/qetgraphicsitem/conductor.h index ddb3b7e9a..4095f10a7 100644 --- a/sources/qetgraphicsitem/conductor.h +++ b/sources/qetgraphicsitem/conductor.h @@ -86,7 +86,7 @@ class Conductor : public QObject, public QGraphicsPathItem { virtual QPainterPath nearShape() const; virtual QPainterPath variableShape(const qreal &) const; virtual bool isNearConductor(const QPointF &); - qreal length(); + qreal length() const; ConductorSegment *middleSegment(); QPointF posForText(Qt::Orientations &flag); bool containsPoint(const QPointF &) const; @@ -103,7 +103,7 @@ class Conductor : public QObject, public QGraphicsPathItem { void setProfiles(const ConductorProfilesGroup &); ConductorProfilesGroup profiles() const; void readProperties(); - void adjustTextItemPosition(); + void calculateTextItemPosition(); virtual Highlight highlight() const; virtual void setHighlighted(Highlight); void autoText(); @@ -165,12 +165,10 @@ class Conductor : public QObject, public QGraphicsPathItem { void updateConductorPath(const QPointF &, Qet::Orientation, const QPointF &, Qet::Orientation); uint segmentsCount(QET::ConductorSegmentType = QET::Both) const; QList segmentsToPoints() const; - QSet relatedConductors() const; QList bends() const; QList junctions() const; void pointsToSegments(QList); bool hasClickedOn(QPointF, QPointF) const; - void calculateTextItemPosition(); Qt::Corner currentPathType() const; void deleteSegments(); static int getCoeff(const qreal &, const qreal &); @@ -183,4 +181,15 @@ class Conductor : public QObject, public QGraphicsPathItem { static QPointF movePointIntoPolygon(const QPointF &, const QPainterPath &); Terminal * relatedPotentialTerminal (Terminal *, const bool all_diagram = true); }; + +Conductor * longuestConductorInPotential (Conductor *conductor, bool all_diagram = false); +QList relatedConductors (const Conductor *conductor); + + +//return true if @a is between or at @b and @c. +template +bool isBetween (const T a, const T b, const T c) { + return (b <= c)? (a >= b && a <= c) : (a <= b && a >= c); +} + #endif diff --git a/sources/qetgraphicsitem/conductortextitem.cpp b/sources/qetgraphicsitem/conductortextitem.cpp index 61691df2e..6aad9c763 100644 --- a/sources/qetgraphicsitem/conductortextitem.cpp +++ b/sources/qetgraphicsitem/conductortextitem.cpp @@ -128,7 +128,7 @@ void ConductorTextItem::forceMovedByUser(bool moved_by_user) { moved_by_user_ = moved_by_user; if (!moved_by_user && parent_conductor_) { - parent_conductor_ -> adjustTextItemPosition(); + parent_conductor_ -> calculateTextItemPosition(); } } @@ -144,7 +144,7 @@ void ConductorTextItem::forceRotateByUser(bool rotate_by_user) { rotate_by_user_ = rotate_by_user; if (!rotate_by_user && parent_conductor_) { - parent_conductor_ -> adjustTextItemPosition(); + parent_conductor_ -> calculateTextItemPosition(); } }