simple element : comment is snapped and centered to the bottom of text tagged "label"

git-svn-id: svn+ssh://svn.tuxfamily.org/svnroot/qet/qet/trunk@3432 bfdf4180-ca20-0410-9c96-a3a8aa849046
This commit is contained in:
blacksun
2014-11-02 14:12:38 +00:00
parent 305b8845cc
commit 822da4fe66
5 changed files with 161 additions and 58 deletions

View File

@@ -19,6 +19,8 @@
#include "element.h" #include "element.h"
#include "qetapp.h" #include "qetapp.h"
#include "diagram.h" #include "diagram.h"
#include "elementtextitem.h"
#include "qgraphicsitemutility.h"
#include <QPainter> #include <QPainter>
/** /**
@@ -27,13 +29,17 @@
* element is also the parent item of this item * element is also the parent item of this item
*/ */
CommentItem::CommentItem(Element *elmt) : CommentItem::CommentItem(Element *elmt) :
QGraphicsObject(elmt), QGraphicsObject (elmt),
m_element (elmt) m_element (elmt),
m_text_parent (false)
{ {
updateLabel(); if (! setTextParent() ) {
connect(elmt, SIGNAL(yChanged()), this, SLOT (autoPos())); connect(elmt, SIGNAL(yChanged()), this, SLOT (autoPos()));
connect(elmt, SIGNAL(rotationChanged()), this, SLOT (autoPos())); connect(elmt, SIGNAL(rotationChanged()), this, SLOT (autoPos()));
}
connect(elmt, SIGNAL(elementInfoChange(DiagramContext)), this, SLOT (updateLabel())); connect(elmt, SIGNAL(elementInfoChange(DiagramContext)), this, SLOT (updateLabel()));
updateLabel();
} }
/** /**
@@ -49,21 +55,10 @@ QRectF CommentItem::boundingRect() const {
* Adjust the position of this item. * Adjust the position of this item.
*/ */
void CommentItem::autoPos() { void CommentItem::autoPos() {
if(!m_element -> diagram()) return; if (m_text_parent)
centerToParentBottom(this);
QRectF border = m_element -> diagram() -> border(); else
QPointF point = m_element -> sceneBoundingRect().center(); centerToBottomDiagram(this, m_element);
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());
}
} }
/** /**
@@ -87,6 +82,24 @@ void CommentItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) {
m_element -> editProperty(); 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 * @brief CommentItem::updateLabel
* update the content of this item * update the content of this item
@@ -94,29 +107,30 @@ void CommentItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) {
*/ */
void CommentItem::updateLabel() { void CommentItem::updateLabel() {
QString comment = m_element -> elementInformations()["comment"].toString(); 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; if (comment != m_comment) {
m_show = show;
QPen pen(Qt::black); m_comment = comment;
pen.setWidthF (0.5);
QPainter painter(&m_picture); QPen pen(Qt::black);
painter.setPen (pen); pen.setWidthF (0.5);
painter.setFont (QETApp::diagramTextsFont(6));
QRectF drawing_rect(QPointF(0,0), QSizeF(100, 100)); QPainter painter(&m_picture);
QRectF text_bounding; 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.drawText(drawing_rect, Qt::TextWordWrap | Qt::AlignHCenter, m_comment, &text_bounding);
painter.drawRoundedRect(text_bounding, 2, 2);
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(); autoPos();
} }

View File

@@ -45,14 +45,14 @@ class CommentItem : public QGraphicsObject
protected: protected:
virtual void paint (QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); virtual void paint (QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
virtual void mouseDoubleClickEvent (QGraphicsSceneMouseEvent * event ); virtual void mouseDoubleClickEvent (QGraphicsSceneMouseEvent * event );
virtual bool setTextParent ();
private: private:
Element *m_element; Element *m_element;
QPicture m_picture; QPicture m_picture;
QRectF m_bounding_rect; QRectF m_bounding_rect;
QString m_comment; QString m_comment;
bool m_show; bool m_text_parent;
}; };
#endif // COMMENTITEM_H #endif // COMMENTITEM_H

View File

@@ -21,6 +21,7 @@
#include "diagramposition.h" #include "diagramposition.h"
#include "elementtextitem.h" #include "elementtextitem.h"
#include "diagram.h" #include "diagram.h"
#include "qgraphicsitemutility.h"
//define the height of the header. //define the height of the header.
#define header 5 #define header 5
@@ -192,27 +193,10 @@ void CrossRefItem::updateLabel() {
*/ */
void CrossRefItem::autoPos() { void CrossRefItem::autoPos() {
//We calcul the position according to the @snapTo of the xrefproperties //We calcul the position according to the @snapTo of the xrefproperties
if (m_properties.snapTo() == XRefProperties::Bottom) { if (m_properties.snapTo() == XRefProperties::Bottom)
QRectF border = m_element->diagram()->border(); centerToBottomDiagram(this, m_element);
QPointF point = m_element->sceneBoundingRect().center(); else
centerToParentBottom(this);
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);
}
} }
/** /**

View File

@@ -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 <http://www.gnu.org/licenses/>.
*/
#include "qgraphicsitemutility.h"
#include "element.h"
#include "diagram.h"
#include <QGraphicsItem>
#include <QDebug>
/**
* @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;
}

View File

@@ -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 <http://www.gnu.org/licenses/>.
*/
#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