2014-03-13 14:59:40 +00:00
|
|
|
/*
|
|
|
|
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/>.
|
|
|
|
*/
|
2014-03-03 17:31:45 +00:00
|
|
|
#include "crossrefitem.h"
|
|
|
|
#include "element.h"
|
|
|
|
#include "qetapp.h"
|
|
|
|
#include "diagramposition.h"
|
|
|
|
|
2014-03-21 02:28:47 +00:00
|
|
|
//define the height of the header.
|
|
|
|
#define header 5
|
2014-04-02 16:57:13 +00:00
|
|
|
//define the widht of the cross
|
|
|
|
#define crossWidth 50
|
2014-03-21 02:28:47 +00:00
|
|
|
|
2014-03-03 17:31:45 +00:00
|
|
|
/**
|
|
|
|
* @brief CrossRefItem::CrossRefItem
|
|
|
|
* Default constructor
|
|
|
|
* @param elmt element to dispaly the cross ref
|
|
|
|
* @param parent parent QetGraphicsItem
|
|
|
|
*/
|
|
|
|
CrossRefItem::CrossRefItem(Element *elmt, QetGraphicsItem *parent) :
|
|
|
|
QetGraphicsItem(parent),
|
|
|
|
element_ (elmt)
|
|
|
|
{
|
2014-03-26 10:41:06 +00:00
|
|
|
snap_to_grid_=false;
|
2014-03-03 17:31:45 +00:00
|
|
|
setFlags(QGraphicsItem::ItemIsSelectable|QGraphicsItem::ItemIsMovable);
|
|
|
|
connect(elmt, SIGNAL(positionChange(QPointF)), this, SLOT(autoPos()));
|
2014-03-26 10:41:06 +00:00
|
|
|
connect(elmt, SIGNAL(elementInfoChange(DiagramContext)), this, SLOT(updateLabel()));
|
2014-03-03 17:31:45 +00:00
|
|
|
connect(diagram()->project(), SIGNAL(projectDiagramsOrderChanged(QETProject*,int,int)), this, SLOT(updateLabel()));
|
|
|
|
updateLabel();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief CrossRefItem::~CrossRefItem
|
|
|
|
* Default destructor
|
|
|
|
*/
|
|
|
|
CrossRefItem::~CrossRefItem() {
|
|
|
|
disconnect(element_, SIGNAL(positionChange(QPointF)), this, SLOT(autoPos()));
|
2014-03-26 10:41:06 +00:00
|
|
|
disconnect(element_, SIGNAL(elementInfoChange(DiagramContext)), this, SLOT(updateLabel()));
|
2014-03-03 17:31:45 +00:00
|
|
|
disconnect(diagram()->project(), SIGNAL(projectDiagramsOrderChanged(QETProject*,int,int)), this, SLOT(updateLabel()));
|
|
|
|
}
|
|
|
|
|
2014-03-26 10:41:06 +00:00
|
|
|
/**
|
|
|
|
* @brief CrossRefItem::boundingRect
|
|
|
|
* @return the bounding rect of this item
|
|
|
|
*/
|
|
|
|
QRectF CrossRefItem::boundingRect() const {
|
|
|
|
return bounding_rect_;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief CrossRefItem::shape
|
|
|
|
* @return the shape of this item
|
|
|
|
*/
|
|
|
|
QPainterPath CrossRefItem::shape() const{
|
|
|
|
return shape_path_;
|
|
|
|
}
|
|
|
|
|
2014-03-03 17:31:45 +00:00
|
|
|
/**
|
|
|
|
* @brief CrossRefItem::updateLabel
|
|
|
|
* Update the content of the item
|
|
|
|
*/
|
|
|
|
void CrossRefItem::updateLabel() {
|
2014-03-26 10:41:06 +00:00
|
|
|
//init the shape
|
|
|
|
shape_path_= QPainterPath();
|
2014-03-03 17:31:45 +00:00
|
|
|
//init the painter
|
2014-03-26 10:41:06 +00:00
|
|
|
QPainter qp;
|
|
|
|
qp.begin(&drawing_);
|
2014-03-03 17:31:45 +00:00
|
|
|
QPen pen_;
|
2014-03-20 13:56:53 +00:00
|
|
|
pen_.setWidthF(0.2);
|
2014-03-03 17:31:45 +00:00
|
|
|
qp.setPen(pen_);
|
|
|
|
|
2014-03-13 14:59:40 +00:00
|
|
|
//calcul the size
|
2014-03-26 10:41:06 +00:00
|
|
|
setUpBoundingRect(qp);
|
2014-03-13 14:59:40 +00:00
|
|
|
|
2014-03-03 17:31:45 +00:00
|
|
|
//draw the cross
|
2014-03-26 10:41:06 +00:00
|
|
|
QRectF br = boundingRect();
|
2014-04-02 17:36:40 +00:00
|
|
|
qp.drawLine(br.width()/2, 0, br.width()/2, br.height() - text_rect_.height()); //vertical line
|
|
|
|
qp.drawLine(br.width()/2-(crossWidth/2), header, br.width()/2+(crossWidth/2), header); //horizontal line
|
2014-03-03 17:31:45 +00:00
|
|
|
|
2014-03-17 17:58:04 +00:00
|
|
|
//draw the symbolic NO
|
2014-03-26 10:41:06 +00:00
|
|
|
qreal xoffset = br.width()/2 - 25;
|
|
|
|
qp.drawLine(xoffset+5, 3, xoffset+10, 3);
|
|
|
|
QPointF p1[3] = {
|
|
|
|
QPointF(xoffset+10, 0),
|
|
|
|
QPointF(xoffset+15, 3),
|
|
|
|
QPointF(xoffset+20, 3),
|
2014-03-17 17:58:04 +00:00
|
|
|
};
|
|
|
|
qp.drawPolyline(p1,3);
|
|
|
|
|
|
|
|
//draw the symbolic NC
|
2014-03-26 10:41:06 +00:00
|
|
|
xoffset = br.width()/2;
|
|
|
|
QPointF p2[3] = {
|
|
|
|
QPointF(xoffset+5, 3),
|
|
|
|
QPointF(xoffset+10, 3),
|
|
|
|
QPointF(xoffset+10, 0)
|
2014-03-17 17:58:04 +00:00
|
|
|
};
|
|
|
|
qp.drawPolyline(p2,3);
|
2014-03-26 10:41:06 +00:00
|
|
|
QPointF p3[3] = {
|
|
|
|
QPointF(xoffset+9, 0),
|
|
|
|
QPointF(xoffset+15, 3),
|
|
|
|
QPointF(xoffset+20, 3),
|
2014-03-17 17:58:04 +00:00
|
|
|
};
|
|
|
|
qp.drawPolyline(p3,3);
|
|
|
|
|
|
|
|
///keep this code for possible next feature
|
|
|
|
///choice to use symbolic or text.
|
2014-03-03 17:31:45 +00:00
|
|
|
//draw the header
|
2014-03-17 17:58:04 +00:00
|
|
|
/*qp.setFont(QETApp::diagramTextsFont(7));
|
2014-03-03 17:31:45 +00:00
|
|
|
QRectF header_rect (0,0,30,10);
|
|
|
|
qp.drawText(header_rect, Qt::AlignCenter, "NO");
|
|
|
|
header_rect.setRect(30, 0, 30, 10);
|
2014-03-17 17:58:04 +00:00
|
|
|
qp.drawText(header_rect, Qt::AlignCenter, "NC");*/
|
2014-03-03 17:31:45 +00:00
|
|
|
|
|
|
|
//and fill it
|
2014-03-26 10:41:06 +00:00
|
|
|
fillCrossRef(qp);
|
|
|
|
qp.end();
|
2014-03-14 14:45:54 +00:00
|
|
|
|
2014-03-13 14:59:40 +00:00
|
|
|
autoPos();
|
2014-03-14 14:45:54 +00:00
|
|
|
update();
|
2014-03-03 17:31:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief CrossRefItem::autoPos
|
|
|
|
* Calculate and set position automaticaly.
|
|
|
|
*/
|
|
|
|
void CrossRefItem::autoPos() {
|
|
|
|
if (isSelected() && element_->isSelected()) return;
|
|
|
|
QRectF border= element_->diagram()->border();
|
|
|
|
QPointF point;
|
|
|
|
|
2014-03-26 10:41:06 +00:00
|
|
|
//if this item have parent calcule the position by using mapped point.
|
2014-03-03 17:31:45 +00:00
|
|
|
if(parentItem()) {
|
|
|
|
point = element_->boundingRect().center();
|
|
|
|
QPointF ypoint_ = mapToParent(mapFromScene(0, border.height() - element_->diagram()->border_and_titleblock.titleBlockHeight() - boundingRect().height()));
|
|
|
|
point.setY(ypoint_.y());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
point = element_->sceneBoundingRect().center();
|
|
|
|
point.setY(border.height() - element_->diagram()->border_and_titleblock.titleBlockHeight() - boundingRect().height());
|
|
|
|
}
|
|
|
|
|
2014-03-26 10:41:06 +00:00
|
|
|
point.setX(point.x() - bounding_rect_.width()/2);
|
2014-03-03 17:31:45 +00:00
|
|
|
setPos(point);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief CrossRefItem::paint
|
|
|
|
* Paint this item
|
|
|
|
* @param painter
|
|
|
|
* @param option
|
|
|
|
* @param widget
|
|
|
|
*/
|
|
|
|
void CrossRefItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
|
|
|
|
Q_UNUSED(option);
|
|
|
|
Q_UNUSED(widget);
|
|
|
|
|
|
|
|
//draw the selection rect
|
|
|
|
if (isSelected()) {
|
|
|
|
painter->save();
|
|
|
|
QPen t(Qt::black);
|
|
|
|
t.setStyle(Qt::DashLine);
|
2014-03-20 13:47:58 +00:00
|
|
|
t.setCosmetic(true);
|
2014-03-03 17:31:45 +00:00
|
|
|
painter -> setPen(t);
|
2014-03-20 11:56:06 +00:00
|
|
|
painter -> setRenderHint(QPainter::Antialiasing, false);
|
2014-03-26 10:41:06 +00:00
|
|
|
painter -> drawPath(shape_path_);
|
2014-03-21 02:28:47 +00:00
|
|
|
painter -> restore();
|
2014-03-03 17:31:45 +00:00
|
|
|
}
|
|
|
|
drawing_.play(painter);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief CrossRefItem::mouseMoveEvent
|
|
|
|
* handle mouse move event
|
|
|
|
* @param e event
|
|
|
|
*/
|
|
|
|
void CrossRefItem::mouseMoveEvent(QGraphicsSceneMouseEvent *e) {
|
|
|
|
element_->setHighlighted(true);
|
|
|
|
QetGraphicsItem::mouseMoveEvent(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief CrossRefItem::mouseReleaseEvent
|
|
|
|
* handle mouse release event
|
|
|
|
* @param e event
|
|
|
|
*/
|
|
|
|
void CrossRefItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *e) {
|
|
|
|
element_->setHighlighted(false);
|
|
|
|
QetGraphicsItem::mouseReleaseEvent(e);
|
|
|
|
}
|
|
|
|
|
2014-03-13 14:59:40 +00:00
|
|
|
/**
|
|
|
|
* @brief CrossRefItem::setUpBoundingRect
|
|
|
|
* Get the numbers of slaves elements linked to this parent element,
|
|
|
|
* for calculate the size of the bounding rect.
|
2014-03-26 10:41:06 +00:00
|
|
|
* Add size of comment text if needed
|
2014-03-13 14:59:40 +00:00
|
|
|
* The cross ref item is drawing according to the size of the bounding rect.
|
|
|
|
*/
|
2014-03-26 10:41:06 +00:00
|
|
|
void CrossRefItem::setUpBoundingRect(QPainter &painter) {
|
2014-03-13 14:59:40 +00:00
|
|
|
//this is the default size of cross ref item
|
2014-04-02 16:57:13 +00:00
|
|
|
QRectF default_bounding(0, 0, crossWidth, 40);
|
2014-03-13 14:59:40 +00:00
|
|
|
|
|
|
|
//No need to calcul if nothing is linked
|
|
|
|
if (!element_->isFree()) {
|
|
|
|
QList <Element *> NO_list;
|
|
|
|
QList <Element *> NC_list;
|
|
|
|
|
|
|
|
//find each no and nc of connected element to element_
|
|
|
|
foreach (Element *elmt, element_->linkedElements()) {
|
|
|
|
QString state = elmt->kindInformations()["state"].toString();
|
|
|
|
if (state == "NO") NO_list << elmt;
|
|
|
|
else if (state == "NC") NC_list << elmt;
|
|
|
|
}
|
|
|
|
|
|
|
|
int i =0;
|
|
|
|
if (NO_list.count()>4 || NC_list.count()>4) {
|
|
|
|
i = NO_list.count() > NC_list.count()?
|
|
|
|
NO_list.count() : NC_list.count();
|
|
|
|
|
|
|
|
//increase the height of bounding rect,
|
|
|
|
//according to the number of slave item less 4.
|
|
|
|
i-=4;
|
2014-03-21 02:28:47 +00:00
|
|
|
default_bounding.setHeight(default_bounding.height() + (i*8));
|
2014-03-13 14:59:40 +00:00
|
|
|
}
|
|
|
|
}
|
2014-03-26 10:41:06 +00:00
|
|
|
shape_path_.addRect(default_bounding);
|
2014-03-13 14:59:40 +00:00
|
|
|
bounding_rect_ = default_bounding;
|
2014-03-26 10:41:06 +00:00
|
|
|
|
|
|
|
//check if need to show the comment of @element_
|
|
|
|
//and add rect of text to the boundingrect
|
|
|
|
QString comment = element_-> elementInformations()["comment"].toString();
|
|
|
|
bool must_show = element_-> elementInformations().keyMustShow("comment");
|
|
|
|
if (!comment.isEmpty() && must_show) {
|
|
|
|
painter.save();
|
|
|
|
painter.setFont(QETApp::diagramTextsFont(6));
|
|
|
|
//calcule the size au graphic text
|
2014-03-28 10:33:14 +00:00
|
|
|
text_rect_ = QRectF(default_bounding.bottomLeft(), QPointF(default_bounding.bottomRight().x(), default_bounding.bottomRight().y()-1));
|
2014-03-26 10:41:06 +00:00
|
|
|
text_rect_ = painter.boundingRect(text_rect_, Qt::AlignHCenter ,comment);
|
|
|
|
bounding_rect_.setSize(default_bounding.united(text_rect_).size());
|
|
|
|
|
|
|
|
shape_path_.addRect(text_rect_);
|
|
|
|
//translate content of shape_path_ if text_rect width
|
|
|
|
//is bigger than default_bounding width
|
|
|
|
if(text_rect_.width() > default_bounding.width()) {
|
|
|
|
int offset = (text_rect_.width()-default_bounding.width())/2;
|
|
|
|
shape_path_.translate(offset,0);
|
|
|
|
text_rect_.translate(offset,0);
|
|
|
|
}
|
|
|
|
painter.restore();
|
|
|
|
}
|
2014-03-28 10:33:14 +00:00
|
|
|
else text_rect_ = QRectF();
|
2014-03-13 14:59:40 +00:00
|
|
|
}
|
|
|
|
|
2014-03-03 17:31:45 +00:00
|
|
|
/**
|
|
|
|
* @brief CrossRefItem::fillCrossRef
|
|
|
|
* Fill the content of the cross ref
|
|
|
|
* @param painter painter to use.
|
|
|
|
*/
|
2014-03-26 10:41:06 +00:00
|
|
|
void CrossRefItem::fillCrossRef(QPainter &painter) {
|
2014-03-03 17:31:45 +00:00
|
|
|
if (element_->isFree()) return;
|
|
|
|
|
|
|
|
QList <Element *> NO_list;
|
|
|
|
QList <Element *> NC_list;
|
|
|
|
|
|
|
|
//find each no and nc of connected element to element_
|
|
|
|
foreach (Element *elmt, element_->linkedElements()) {
|
2014-04-02 17:36:40 +00:00
|
|
|
if (elmt->kindInformations()["type"].toString() == "power") continue;
|
2014-03-03 17:31:45 +00:00
|
|
|
QString state = elmt->kindInformations()["state"].toString();
|
|
|
|
if (state == "NO") NO_list << elmt;
|
|
|
|
else if (state == "NC") NC_list << elmt;
|
|
|
|
}
|
|
|
|
|
2014-03-26 10:41:06 +00:00
|
|
|
painter.setFont(QETApp::diagramTextsFont(5));
|
2014-04-02 16:57:13 +00:00
|
|
|
qreal middle_cross = bounding_rect_.width()/2;
|
2014-03-03 17:31:45 +00:00
|
|
|
//fill the NO
|
|
|
|
QString contact_str;
|
|
|
|
foreach (Element *elmt, NO_list) {
|
|
|
|
contact_str += QString::number(elmt->diagram()->folioIndex() + 1);
|
|
|
|
contact_str += "-";
|
|
|
|
contact_str += elmt->diagram()->convertPosition(elmt -> scenePos()).toString();
|
|
|
|
contact_str += "\n";
|
|
|
|
}
|
2014-04-02 16:57:13 +00:00
|
|
|
QRectF rect_(middle_cross - (crossWidth/2),
|
|
|
|
header,
|
|
|
|
middle_cross,
|
|
|
|
(bounding_rect_.height()-header));
|
2014-04-01 01:58:37 +00:00
|
|
|
painter.drawText(rect_, Qt::AlignTop | Qt::AlignLeft, contact_str);
|
2014-03-03 17:31:45 +00:00
|
|
|
|
|
|
|
//fill the NC
|
|
|
|
contact_str.clear();
|
|
|
|
foreach (Element *elmt, NC_list) {
|
|
|
|
contact_str += QString::number(elmt->diagram()->folioIndex() + 1);
|
|
|
|
contact_str += "-";
|
|
|
|
contact_str += elmt->diagram()->convertPosition(elmt -> scenePos()).toString();
|
|
|
|
contact_str += "\n";
|
|
|
|
}
|
2014-04-02 16:57:13 +00:00
|
|
|
rect_.setRect(middle_cross,
|
|
|
|
header,
|
|
|
|
crossWidth/2,
|
|
|
|
(bounding_rect_.height()-header));
|
|
|
|
painter.drawText(rect_, Qt::AlignTop | Qt::AlignRight, contact_str);
|
2014-03-26 10:41:06 +00:00
|
|
|
|
|
|
|
fillExtraInfo(painter);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief CrossRefItem::fillExtraInfo
|
|
|
|
* Fill the comment info of the parent item if needed.
|
|
|
|
* @param painter painter to use for draw the text
|
|
|
|
*/
|
|
|
|
void CrossRefItem::fillExtraInfo(QPainter &painter) {
|
|
|
|
//check if need to show the comment of @element_
|
|
|
|
QString comment = element_-> elementInformations()["comment"].toString();
|
|
|
|
bool must_show = element_-> elementInformations().keyMustShow("comment");
|
|
|
|
if (!comment.isEmpty() && must_show) {
|
|
|
|
painter.setFont(QETApp::diagramTextsFont(6));
|
|
|
|
//draw text inside a roundedrect
|
|
|
|
painter.drawText(text_rect_, Qt::AlignHCenter, comment);
|
|
|
|
painter.drawRoundedRect(text_rect_, 2, 2);
|
|
|
|
}
|
2014-03-03 17:31:45 +00:00
|
|
|
}
|