mirror of
https://github.com/qelectrotech/qelectrotech-source-mirror.git
synced 2025-12-18 13:30:34 +01:00
Simple element : Comment is show at the bottom of diagram inside a rounded rectangle.
git-svn-id: svn+ssh://svn.tuxfamily.org/svnroot/qet/qet/trunk@3407 bfdf4180-ca20-0410-9c96-a3a8aa849046
This commit is contained in:
113
sources/qetgraphicsitem/commentitem.cpp
Normal file
113
sources/qetgraphicsitem/commentitem.cpp
Normal file
@@ -0,0 +1,113 @@
|
|||||||
|
/*
|
||||||
|
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 "commentitem.h"
|
||||||
|
#include "element.h"
|
||||||
|
#include "qetapp.h"
|
||||||
|
#include "diagram.h"
|
||||||
|
#include <QPainter>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief CommentItem::CommentItem
|
||||||
|
* @param elmt : the element to display comment,
|
||||||
|
* element is also the parent item of this item
|
||||||
|
*/
|
||||||
|
CommentItem::CommentItem(Element *elmt) :
|
||||||
|
QGraphicsObject(elmt),
|
||||||
|
m_element (elmt)
|
||||||
|
{
|
||||||
|
updateLabel();
|
||||||
|
connect(elmt, SIGNAL(yChanged()), this, SLOT (autoPos()));
|
||||||
|
connect(elmt, SIGNAL(rotationChanged()), this, SLOT (autoPos()));
|
||||||
|
connect(elmt, SIGNAL(elementInfoChange(DiagramContext)), this, SLOT (updateLabel()));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief CommentItem::boundingRect
|
||||||
|
* @return the bounding rect of this item
|
||||||
|
*/
|
||||||
|
QRectF CommentItem::boundingRect() const {
|
||||||
|
return m_bounding_rect;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief CommentItem::autoPos
|
||||||
|
* 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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief CommentItem::paint
|
||||||
|
* Paint this item
|
||||||
|
* @param painter
|
||||||
|
* @param option
|
||||||
|
* @param widget
|
||||||
|
*/
|
||||||
|
void CommentItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
|
||||||
|
Q_UNUSED(option); Q_UNUSED(widget);
|
||||||
|
m_picture.play(painter);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief CommentItem::updateLabel
|
||||||
|
* update the content of this item
|
||||||
|
* (draw this item in a QPicture)
|
||||||
|
*/
|
||||||
|
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;
|
||||||
|
|
||||||
|
m_comment = comment;
|
||||||
|
m_show = show;
|
||||||
|
|
||||||
|
QPen pen(Qt::black);
|
||||||
|
pen.setWidthF (0.5);
|
||||||
|
|
||||||
|
QPainter painter(&m_picture);
|
||||||
|
painter.setPen (pen);
|
||||||
|
painter.setFont (QETApp::diagramTextsFont(6));
|
||||||
|
|
||||||
|
QRectF drawing_rect(QPointF(0,0), QSizeF(100, 100));
|
||||||
|
QRectF text_bounding;
|
||||||
|
|
||||||
|
painter.drawText(drawing_rect, Qt::TextWordWrap | Qt::AlignHCenter, m_comment, &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();
|
||||||
|
}
|
||||||
57
sources/qetgraphicsitem/commentitem.h
Normal file
57
sources/qetgraphicsitem/commentitem.h
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
/*
|
||||||
|
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 COMMENTITEM_H
|
||||||
|
#define COMMENTITEM_H
|
||||||
|
|
||||||
|
#include <QGraphicsObject>
|
||||||
|
#include <QPicture>
|
||||||
|
|
||||||
|
class Element;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief The CommentItem class
|
||||||
|
* This item display the comment of an element
|
||||||
|
* at the bottom of element diagram in a rounded rect
|
||||||
|
*/
|
||||||
|
class CommentItem : public QGraphicsObject
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
public:
|
||||||
|
explicit CommentItem(Element *elmt);
|
||||||
|
|
||||||
|
virtual QRectF boundingRect() const;
|
||||||
|
|
||||||
|
signals:
|
||||||
|
|
||||||
|
public slots:
|
||||||
|
void autoPos();
|
||||||
|
void updateLabel();
|
||||||
|
|
||||||
|
protected:
|
||||||
|
virtual void paint (QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
|
||||||
|
|
||||||
|
private:
|
||||||
|
Element *m_element;
|
||||||
|
QPicture m_picture;
|
||||||
|
QRectF m_bounding_rect;
|
||||||
|
QString m_comment;
|
||||||
|
bool m_show;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // COMMENTITEM_H
|
||||||
@@ -16,6 +16,7 @@
|
|||||||
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
|
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
#include "simpleelement.h"
|
#include "simpleelement.h"
|
||||||
|
#include "commentitem.h"
|
||||||
/**
|
/**
|
||||||
* @brief SimpleElement::SimpleElement
|
* @brief SimpleElement::SimpleElement
|
||||||
* @param location
|
* @param location
|
||||||
@@ -24,7 +25,8 @@
|
|||||||
* @param state
|
* @param state
|
||||||
*/
|
*/
|
||||||
SimpleElement::SimpleElement(const ElementsLocation &location, QGraphicsItem *qgi, Diagram *s, int *state) :
|
SimpleElement::SimpleElement(const ElementsLocation &location, QGraphicsItem *qgi, Diagram *s, int *state) :
|
||||||
CustomElement(location, qgi, s, state)
|
CustomElement(location, qgi, s, state),
|
||||||
|
m_comment_item (nullptr)
|
||||||
{
|
{
|
||||||
link_type_ = Simple;
|
link_type_ = Simple;
|
||||||
connect(this, SIGNAL(elementInfoChange(DiagramContext)), this, SLOT(updateLabel()));
|
connect(this, SIGNAL(elementInfoChange(DiagramContext)), this, SLOT(updateLabel()));
|
||||||
@@ -35,6 +37,12 @@ SimpleElement::SimpleElement(const ElementsLocation &location, QGraphicsItem *qg
|
|||||||
*/
|
*/
|
||||||
SimpleElement::~SimpleElement() {
|
SimpleElement::~SimpleElement() {
|
||||||
disconnect(this, SIGNAL(elementInfoChange(DiagramContext)), this, SLOT(updateLabel()));
|
disconnect(this, SIGNAL(elementInfoChange(DiagramContext)), this, SLOT(updateLabel()));
|
||||||
|
if (m_comment_item) delete m_comment_item;
|
||||||
|
}
|
||||||
|
|
||||||
|
void SimpleElement::initLink(QETProject *project) {
|
||||||
|
CustomElement::initLink(project);
|
||||||
|
updateLabel();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -42,6 +50,7 @@ SimpleElement::~SimpleElement() {
|
|||||||
* update label of this element
|
* update label of this element
|
||||||
*/
|
*/
|
||||||
void SimpleElement::updateLabel() {
|
void SimpleElement::updateLabel() {
|
||||||
|
//Label of element
|
||||||
QString label = elementInformations()["label"].toString();
|
QString label = elementInformations()["label"].toString();
|
||||||
bool show = elementInformations().keyMustShow("label");
|
bool show = elementInformations().keyMustShow("label");
|
||||||
|
|
||||||
@@ -49,4 +58,16 @@ void SimpleElement::updateLabel() {
|
|||||||
(label.isEmpty() || !show)?
|
(label.isEmpty() || !show)?
|
||||||
setTaggedText("label", "_", false):
|
setTaggedText("label", "_", false):
|
||||||
setTaggedText("label", label, true);
|
setTaggedText("label", label, true);
|
||||||
|
|
||||||
|
//Comment of element
|
||||||
|
QString comment = elementInformations()["comment"].toString();
|
||||||
|
bool must_show = elementInformations().keyMustShow("comment");
|
||||||
|
|
||||||
|
if (!(comment.isEmpty() || !must_show) && !m_comment_item) {
|
||||||
|
m_comment_item = new CommentItem(this);
|
||||||
|
}
|
||||||
|
else if ((comment.isEmpty() || !must_show) && m_comment_item) {
|
||||||
|
delete m_comment_item;
|
||||||
|
m_comment_item = nullptr;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,6 +20,9 @@
|
|||||||
|
|
||||||
#include "customelement.h"
|
#include "customelement.h"
|
||||||
|
|
||||||
|
class CommentItem;
|
||||||
|
class QETProject;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief The SimpleElement class
|
* @brief The SimpleElement class
|
||||||
*this class represente a simple element with no specific attribute
|
*this class represente a simple element with no specific attribute
|
||||||
@@ -32,11 +35,16 @@ class SimpleElement : public CustomElement {
|
|||||||
explicit SimpleElement(const ElementsLocation &, QGraphicsItem * = 0, Diagram * = 0, int * = 0);
|
explicit SimpleElement(const ElementsLocation &, QGraphicsItem * = 0, Diagram * = 0, int * = 0);
|
||||||
~SimpleElement();
|
~SimpleElement();
|
||||||
|
|
||||||
|
virtual void initLink(QETProject *project);
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
void updateLabel();
|
void updateLabel();
|
||||||
|
|
||||||
|
private:
|
||||||
|
CommentItem *m_comment_item;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // SIMPLEELEMENT_H
|
#endif // SIMPLEELEMENT_H
|
||||||
|
|||||||
Reference in New Issue
Block a user