diff --git a/sources/qetgraphicsitem/commentitem.cpp b/sources/qetgraphicsitem/commentitem.cpp index e6a5f4626..60c7f9bdb 100644 --- a/sources/qetgraphicsitem/commentitem.cpp +++ b/sources/qetgraphicsitem/commentitem.cpp @@ -19,6 +19,8 @@ #include "element.h" #include "qetapp.h" #include "diagram.h" +#include "elementtextitem.h" +#include "qgraphicsitemutility.h" #include /** @@ -27,13 +29,17 @@ * element is also the parent item of this item */ CommentItem::CommentItem(Element *elmt) : - QGraphicsObject(elmt), - m_element (elmt) + QGraphicsObject (elmt), + m_element (elmt), + m_text_parent (false) { - updateLabel(); - connect(elmt, SIGNAL(yChanged()), this, SLOT (autoPos())); - connect(elmt, SIGNAL(rotationChanged()), this, SLOT (autoPos())); + if (! setTextParent() ) { + connect(elmt, SIGNAL(yChanged()), this, SLOT (autoPos())); + connect(elmt, SIGNAL(rotationChanged()), this, SLOT (autoPos())); + } connect(elmt, SIGNAL(elementInfoChange(DiagramContext)), this, SLOT (updateLabel())); + + updateLabel(); } /** @@ -49,21 +55,10 @@ QRectF CommentItem::boundingRect() const { * Adjust the position of this item. */ void CommentItem::autoPos() { - if(!m_element -> diagram()) return; - - QRectF border = m_element -> diagram() -> border(); - QPointF point = m_element -> sceneBoundingRect().center(); - - point.setY(border.height() - m_element -> diagram() -> border_and_titleblock.titleBlockHeight() - boundingRect().height()); - point.rx() -= (m_bounding_rect.width()/2 + m_bounding_rect.left()); //< we add boundingrect.left because this value can be négative - - setPos(0,0); //Due to a weird behavior or bug, before set the new position and rotation, - setRotation(0); //we must to set the position and rotation at 0. - setPos(mapFromScene(point)); - if (rotation() != - m_element -> rotation()) { - setRotation(0); - setRotation(- m_element -> rotation()); - } + if (m_text_parent) + centerToParentBottom(this); + else + centerToBottomDiagram(this, m_element); } /** @@ -87,6 +82,24 @@ void CommentItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) { m_element -> editProperty(); } +/** + * @brief CommentItem::setTextParent + * Set text tagged "label" of element has parent. + * @return true if text has been set has parent. + * else return false, the element is the parent of this comment item + */ +bool CommentItem::setTextParent() { + if (ElementTextItem *eti = m_element->taggedText("label")) { + setParentItem(eti); + m_text_parent = true; + return true; + } + + qDebug() << "Comment item: can't found text tagged 'label' from actual parent element to set has parent, " + "comment will be displayed at bottom of diagram"; + return false; +} + /** * @brief CommentItem::updateLabel * update the content of this item @@ -94,29 +107,30 @@ void CommentItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) { */ void CommentItem::updateLabel() { QString comment = m_element -> elementInformations()["comment"].toString(); - bool show = m_element -> elementInformations().keyMustShow("comment"); - if (comment == m_comment && show == m_show) return; + if (comment == m_comment && !m_text_parent) return; - m_comment = comment; - m_show = show; + if (comment != m_comment) { - QPen pen(Qt::black); - pen.setWidthF (0.5); + m_comment = comment; - QPainter painter(&m_picture); - painter.setPen (pen); - painter.setFont (QETApp::diagramTextsFont(6)); + QPen pen(Qt::black); + pen.setWidthF (0.5); - QRectF drawing_rect(QPointF(0,0), QSizeF(100, 100)); - QRectF text_bounding; + QPainter painter(&m_picture); + painter.setPen (pen); + painter.setFont (QETApp::diagramTextsFont(6)); - painter.drawText(drawing_rect, Qt::TextWordWrap | Qt::AlignHCenter, m_comment, &text_bounding); + QRectF drawing_rect(QPointF(0,0), QSizeF(100, 100)); + QRectF text_bounding; - text_bounding.adjust(-1,0,1,0); //adjust only for better visual - painter.drawRoundedRect(text_bounding, 2, 2); + painter.drawText(drawing_rect, Qt::TextWordWrap | Qt::AlignHCenter, m_comment, &text_bounding); - m_bounding_rect = text_bounding; + text_bounding.adjust(-1,0,1,0); //adjust only for better visual + painter.drawRoundedRect(text_bounding, 2, 2); + + m_bounding_rect = text_bounding; + } autoPos(); } diff --git a/sources/qetgraphicsitem/commentitem.h b/sources/qetgraphicsitem/commentitem.h index 49cc4b8ac..0d437c212 100644 --- a/sources/qetgraphicsitem/commentitem.h +++ b/sources/qetgraphicsitem/commentitem.h @@ -45,14 +45,14 @@ class CommentItem : public QGraphicsObject protected: virtual void paint (QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); virtual void mouseDoubleClickEvent (QGraphicsSceneMouseEvent * event ); + virtual bool setTextParent (); private: Element *m_element; QPicture m_picture; QRectF m_bounding_rect; QString m_comment; - bool m_show; - + bool m_text_parent; }; #endif // COMMENTITEM_H diff --git a/sources/qetgraphicsitem/crossrefitem.cpp b/sources/qetgraphicsitem/crossrefitem.cpp index 090796061..b9c5c9c51 100644 --- a/sources/qetgraphicsitem/crossrefitem.cpp +++ b/sources/qetgraphicsitem/crossrefitem.cpp @@ -21,6 +21,7 @@ #include "diagramposition.h" #include "elementtextitem.h" #include "diagram.h" +#include "qgraphicsitemutility.h" //define the height of the header. #define header 5 @@ -192,27 +193,10 @@ void CrossRefItem::updateLabel() { */ void CrossRefItem::autoPos() { //We calcul the position according to the @snapTo of the xrefproperties - if (m_properties.snapTo() == XRefProperties::Bottom) { - QRectF border = m_element->diagram()->border(); - QPointF point = m_element->sceneBoundingRect().center(); - - point.setY(border.height() - m_element->diagram()->border_and_titleblock.titleBlockHeight() - boundingRect().height()); - point.rx() -= (m_bounding_rect.width()/2 + m_bounding_rect.left()); //< we add boundingrect.left because this value can be négative - - setPos(0,0); //Due to a weird behavior or bug, before set the new position and rotation, - setRotation(0); //we must to set the position and rotation at 0. - setPos(mapFromScene(point)); - if (rotation() != - m_element->rotation()) { - setRotation(0); - setRotation(- m_element->rotation()); - } - } - else { - QPointF p = parentItem()->boundingRect().center(); - p.ry() += parentItem()->boundingRect().height()/2; - p.rx() -= (m_bounding_rect.width()/2 + m_bounding_rect.left()); //< we add boundingrect.left because this value can be négative - setPos(p); - } + if (m_properties.snapTo() == XRefProperties::Bottom) + centerToBottomDiagram(this, m_element); + else + centerToParentBottom(this); } /** diff --git a/sources/qetgraphicsitem/qgraphicsitemutility.cpp b/sources/qetgraphicsitem/qgraphicsitemutility.cpp new file mode 100644 index 000000000..9af90c675 --- /dev/null +++ b/sources/qetgraphicsitem/qgraphicsitemutility.cpp @@ -0,0 +1,78 @@ +/* + Copyright 2006-2014 The QElectroTech Team + This file is part of QElectroTech. + + QElectroTech is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + QElectroTech is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with QElectroTech. If not, see . +*/ +#include "qgraphicsitemutility.h" +#include "element.h" +#include "diagram.h" +#include +#include + +/** + * @brief centerToParentBottom + * Center the item at the bottom of is parent. + * @param item item to center + * @return true if centered else false (item have not parent) + */ +bool centerToParentBottom(QGraphicsItem *item) { + if (! item->parentItem()) { + qDebug() << "Qet::centerToParentBottom : item have not parent"; + return false; + } + + QPointF p = item -> parentItem() -> boundingRect().center(); + p.ry() += item -> parentItem() -> boundingRect().height()/2; + p.rx() -= (item -> boundingRect().width()/2 + item->boundingRect().left()); //< we add boundingrect.left because this value can be négative + + item -> setPos(p); + return true; +} + +/** + * @brief centerToBottomDiagram + * Set item pos to the bottom of diagram and centered vertically to element_to_follow + * @param item_to_center + * @param element_to_follow + * @return true if element is centered else false (element_to_follow have not diagram) + */ +bool centerToBottomDiagram (QGraphicsItem *item_to_center, Element *element_to_follow) { + if (! element_to_follow -> diagram()) { + qDebug() << "qgraphicsitemutility centerAtBottomDiagram : Element_to_follow have not diagram"; + return false; + } + + QRectF border = element_to_follow -> diagram() -> border(); + QPointF point = element_to_follow -> sceneBoundingRect().center(); + + point.setY(border.height() - + element_to_follow -> diagram() -> border_and_titleblock.titleBlockHeight() - + item_to_center -> boundingRect().height()); + + point.rx() -= (item_to_center -> boundingRect().width()/2 + + item_to_center -> boundingRect().left()); //< we add boundingrect.left because this value can be négative + + item_to_center -> setPos(0,0); //Due to a weird behavior or bug, before set the new position and rotation, + item_to_center -> setRotation(0); //we must to set the position and rotation at 0. + + item_to_center -> setPos(item_to_center -> mapFromScene(point)); + + if (item_to_center -> rotation() != - element_to_follow -> rotation()) { + item_to_center -> setRotation(0); + item_to_center -> setRotation(- element_to_follow -> rotation()); + } + + return true; +} diff --git a/sources/qetgraphicsitem/qgraphicsitemutility.h b/sources/qetgraphicsitem/qgraphicsitemutility.h new file mode 100644 index 000000000..231d3df92 --- /dev/null +++ b/sources/qetgraphicsitem/qgraphicsitemutility.h @@ -0,0 +1,27 @@ +/* + Copyright 2006-2014 The QElectroTech Team + This file is part of QElectroTech. + + QElectroTech is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + QElectroTech is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with QElectroTech. If not, see . +*/ +#ifndef QGRAPHICSITEMUTILITY_H +#define QGRAPHICSITEMUTILITY_H + +class QGraphicsItem; +class Element; + +bool centerToParentBottom (QGraphicsItem *item); +bool centerToBottomDiagram (QGraphicsItem *item_to_center, Element *element_to_follow); + +#endif // QGRAPHICSITEMUTILITY_H