#include #include "elements.h" #include "itabs.h" QBrush *tb = 0; QPen *tp = 0; QBrush *db = 0; extern QSettings *conf; EdgeItem::EdgeItem( NodeItem *from, NodeItem *to ) { from_node = from; to_node = to; if(from)from->setOutEdge( this ); if(to) to->setInEdge( this ); setBlock(false); } QVariant NodeItem::itemChange(GraphicsItemChange change, const QVariant &value) { if (change == ItemPositionChange) { EdgeItem *edge = inItem; if (edge && !edge->block()){ edge->setToPoint( int(x()), int(y()) ); } edge = outItem; if (edge && !edge->block()){ edge->setFromPoint( int(x()), int(y()) ); } } return QGraphicsRectItem::itemChange(change, value); } void NodeItem::mousePressEvent ( QGraphicsSceneMouseEvent * event ) { if(inItem) { inItem->setFlag(ItemIsMovable, false); } else { outItem->setFlag(ItemIsMovable, false); } } void NodeItem::mouseReleaseEvent ( QGraphicsSceneMouseEvent * event ) { if(inItem) { inItem->setFlag(ItemIsMovable,true); } else { outItem->setFlag(ItemIsMovable,true); } } void NodeItem::setInEdge( EdgeItem *edge ){ inItem = edge;} NodeItem::NodeItem( ) : QGraphicsRectItem( QRectF(-4, -4, 8, 8) ) { if ( !tb ) tb = new QBrush( Qt::green ); if ( !tp ) tp = new QPen( QBrush(Qt::black),1); if ( !db ) { QColor color(Qt::blue); color.setAlpha(255); db = new QBrush(color); } inItem = 0; outItem = 0; setPen( *tp ); setBrush( *tb ); setZValue( 250 ); setFlag(ItemIsMovable); } LineItem::LineItem( NodeItem *from, NodeItem *to ) : EdgeItem(from, to), QGraphicsLineItem() { item_type = type(); setPen( *tp ); ipen = pen(); setCursor(QCursor(Qt::SizeAllCursor)); from->setOutEdge( this ); to->setInEdge( this ); setLine( QLineF(int(from->x()), int(from->y()), int(to->x()), int(to->y()) )); setFlag(ItemIsSelectable); setFlag(ItemIsMovable); } LineItem::LineItem( QDomElement el ) : EdgeItem(new NodeItem(), new NodeItem()), QGraphicsLineItem() { item_type = type(); setPen( *tp ); ipen = pen(); ipen.setColor(QColor(el.attribute("color"))); ipen.setWidth(el.attribute("width").toInt()); setPen(ipen); int x1 = el.attribute("x1").toInt(), y1 = el.attribute("y1").toInt(), x2 = el.attribute("x2").toInt(), y2 = el.attribute("y2").toInt(); from_node->setPos(x1,y1); to_node->setPos(x2, y2); setLine( QLineF(x1, y1, x2, y2)); setCursor(QCursor(Qt::SizeAllCursor)); setFlag(ItemIsSelectable); setFlag(ItemIsMovable); } void LineItem::paintItem( int x, int y, QPainter *pp, qreal scale ) { QLineF l = line(); QLineF l1 = QLineF(x+l.x1()*scale+scenePos().x(), y+l.y1()*scale+scenePos().y(),x+l.x2()*scale+scenePos().x(),y+l.y2()*scale+scenePos().y()); pp->setPen(pen()); pp->drawLine(l1); } void LineItem::setFromPoint( int x, int y ) { setBlock(true); setLine(QLineF( x-scenePos().x(),y-scenePos().y(), line().p2().x(), line().p2().y() )); emit position_changed(this); from_node->setPos(x,y); setBlock(false); } void LineItem::setToPoint( int x, int y ) { setBlock(true); setLine(QLineF( line().p1().x(), line().p1().y(), x-scenePos().x(),y-scenePos().y() )); emit position_changed(this); to_node->setPos(x,y); setBlock(false); } void LineItem::setTabs( QTabWidget *properties) { // conf = settings; PositionTab *ptab = new PositionTab(this); properties->addTab(ptab, tr("Position")); LineTab *ltab = new LineTab(this); properties->addTab(ltab, tr("Line")); } QVariant LineItem::itemChange(GraphicsItemChange change, const QVariant &value) { if(block()) {return QGraphicsLineItem::itemChange(change, value);} setBlock(true); if(change == ItemSelectedChange) { if(value.toBool()) { emit item_selected(this); from_node->show(); to_node->show(); } else { emit item_selected(0); from_node->hide(); to_node->hide(); } } else if (change == ItemPositionChange) { QLineF r = line(); from_node->setPos(r.x1()+scenePos().x(), r.y1()+scenePos().y()); to_node->setPos(r.x2()+scenePos().x(), r.y2()+scenePos().y()); emit position_changed(this); } setBlock(false); return QGraphicsLineItem::itemChange(change, value); } QDomElement LineItem::toXml(QDomDocument doc) { QDomElement l =doc.createElement("line"); l.setAttribute("x1",line().x1()+scenePos().x()); l.setAttribute("y1",line().y1()+scenePos().y()); l.setAttribute("x2",line().x2()+scenePos().x()); l.setAttribute("y2",line().y2()+scenePos().y()); l.setAttribute("width",ipen.width()); l.setAttribute("color",ipen.color().name()); return l; } RectItem::RectItem( NodeItem *from, NodeItem *to ) : EdgeItem(from, to),QGraphicsRectItem () { item_type = type(); setPen( *tp ); setBrush( *db); ipen = pen(); from->setOutEdge( (EdgeItem *)this ); to->setInEdge( (EdgeItem *)this ); setRect( QRectF(int(from->x()), int(from->y()), int(to->x()-from->x()), int(to->y()-from->y()) )); setFlag(ItemIsSelectable); setFlag(ItemIsMovable); } RectItem::RectItem( QDomElement el ) : EdgeItem(new NodeItem(), new NodeItem()), QGraphicsRectItem() { item_type = type(); setPen( *tp ); ipen = pen(); ipen.setColor(QColor(el.attribute("color"))); ipen.setWidth(el.attribute("width").toInt()); QColor color(el.attribute("fillcolor")); color.setAlpha(el.attribute("alpha").toInt()); setBrush(QBrush(color)); setPen(ipen); int x1 = el.attribute("x1").toInt(), y1 = el.attribute("y1").toInt(), x2 = el.attribute("x2").toInt(), y2 = el.attribute("y2").toInt(); from_node->setPos(x1,y1); to_node->setPos(x2, y2); setRect( QRectF(x1, y1, x2-x1, y2-y1)); setCursor(QCursor(Qt::SizeAllCursor)); setFlag(ItemIsSelectable); setFlag(ItemIsMovable); } void RectItem::paintItem( int x, int y, QPainter *pp, qreal scale ) { QRectF l = rect(); QPointF p = l.topLeft(); QPointF p1 = l.bottomRight(); p.setX(p.x()*scale+x+scenePos().x()); p.setY(p.y()*scale+y+scenePos().y()); p1.setX(p1.x()*scale+x+scenePos().x()); p1.setY(p1.y()*scale+y+scenePos().y()); l.setTopLeft(p); l.setBottomRight(p1); pp->setPen(pen()); pp->setBrush(brush()); pp->drawRect(l); } void RectItem::setFromPoint( int x, int y ) { setBlock(true); QRectF r = rect(); r.setTopLeft(QPointF(x-scenePos().x(),y-scenePos().y())); setRect(r); from_node->setPos(x,y); setBlock(false); emit position_changed(this); } void RectItem::setToPoint( int x, int y ) { setBlock(true); QRectF r = rect(); r.setBottomRight(QPointF(x-scenePos().x(),y-scenePos().y())); setRect(r); to_node->setPos(x,y); setBlock(false); emit position_changed(this); } void RectItem::setTabs( QTabWidget *properties) { // conf = settings; PositionTab *ptab = new PositionTab(this); properties->addTab(ptab, tr("Position")); LineTab *ltab = new LineTab(this); properties->addTab(ltab, tr("Line")); } QVariant RectItem::itemChange(GraphicsItemChange change, const QVariant &value) { if(block()) {return -1;} setBlock(true); if(change == ItemSelectedChange) { if(value.toBool()) { emit item_selected(this); from_node->show(); to_node->show(); } else { emit item_selected(0); from_node->hide(); to_node->hide(); } } else if (change == ItemPositionChange) { QRectF r = sceneBoundingRect(); from_node->setPos(r.x(), r.y()); to_node->setPos(r.x()+r.width(), r.y()+r.height()); emit position_changed(this); } setBlock(false); return QGraphicsRectItem::itemChange(change, value); } QDomElement RectItem::toXml(QDomDocument doc) { QDomElement l =doc.createElement("rectangle"); l.setAttribute("x1",rect().topLeft().x()+scenePos().x()); l.setAttribute("y1",rect().topLeft().y()+scenePos().y()); l.setAttribute("x2",rect().bottomRight().x()+scenePos().x()); l.setAttribute("y2",rect().bottomRight().y()+scenePos().y()); l.setAttribute("width",ipen.width()); l.setAttribute("color",ipen.color().name()); QColor color = brush().color(); l.setAttribute("fillcolor",color.name()); l.setAttribute("alpha",color.alpha()); return l; } EllipseItem::EllipseItem( NodeItem *from, NodeItem *to ) : EdgeItem(from, to),QGraphicsEllipseItem () { item_type = type(); setPen( *tp ); ipen = pen(); // ipen.setBrush(db); setBrush( *db); setPen( ipen ); from->setOutEdge( (EdgeItem *)this ); to->setInEdge( (EdgeItem *)this ); setRect( QRectF(int(from->x()), int(from->y()), int(to->x()-from->x()), int(to->y()-from->y()) )); setFlag(ItemIsSelectable); setFlag(ItemIsMovable); } EllipseItem::EllipseItem( QDomElement el ) : EdgeItem(new NodeItem(), new NodeItem()), QGraphicsEllipseItem() { item_type = type(); setPen( *tp ); ipen = pen(); ipen.setColor(QColor(el.attribute("color"))); ipen.setWidth(el.attribute("width").toInt()); setPen(ipen); QColor color(el.attribute("fillcolor")); color.setAlpha(el.attribute("alpha").toInt()); setBrush(QBrush(color)); int x1 = el.attribute("x1").toInt(), y1 = el.attribute("y1").toInt(), x2 = el.attribute("x2").toInt(), y2 = el.attribute("y2").toInt(); from_node->setPos(x1,y1); to_node->setPos(x2, y2); setRect( QRectF(x1, y1, x2-x1, y2-y1 )); setCursor(QCursor(Qt::SizeAllCursor)); setFlag(ItemIsSelectable); setFlag(ItemIsMovable); } void EllipseItem::paintItem( int x, int y, QPainter *pp, qreal scale ) { QRectF l = rect(); QPointF p = l.topLeft(); QPointF p1 = l.bottomRight(); p.setX(p.x()*scale+x+scenePos().x()); p.setY(p.y()*scale+y+scenePos().y()); p1.setX(p1.x()*scale+x+scenePos().x()); p1.setY(p1.y()*scale+y+scenePos().y()); l.setTopLeft(p); l.setBottomRight(p1); pp->setPen(pen()); pp->setBrush(brush()); pp->drawEllipse(l); } void EllipseItem::setFromPoint( int x, int y ) { setBlock(true); QRectF r = rect(); r.setTopLeft(QPointF(x-scenePos().x(),y-scenePos().y())); setRect(r); from_node->setPos(x,y); setBlock(false); emit position_changed(this); // setRect(QRectF( x,y,rect().bottomRight().x(), rect().bottomRight().y() )); } void EllipseItem::setToPoint( int x, int y ) { setBlock(true); QRectF r = rect(); r.setBottomRight(QPointF(x-scenePos().x(),y-scenePos().y())); setRect(r); to_node->setPos(x,y); setBlock(false); emit position_changed(this); // setRect(QRectF( rect().topLeft().x(), rect().topLeft().y(),x, y )); } void EllipseItem::setTabs( QTabWidget *properties) { // conf = settings; PositionTab *ptab = new PositionTab(this); properties->addTab(ptab, tr("Position")); LineTab *ltab = new LineTab(this); properties->addTab(ltab, tr("Line")); } QVariant EllipseItem::itemChange(GraphicsItemChange change, const QVariant &value) { if(block()) {return QGraphicsEllipseItem::itemChange(change, value);;} setBlock(true); if(change == ItemSelectedChange) { if(value.toBool()) { emit item_selected(this); from_node->show(); to_node->show(); } else { emit item_selected(0); from_node->hide(); to_node->hide(); } } else if (change == ItemPositionChange) { QRectF r = sceneBoundingRect(); from_node->setPos(r.x(), r.y()); to_node->setPos(r.x()+r.width(), r.y()+r.height()); emit position_changed(this); } setBlock(false); return QGraphicsEllipseItem::itemChange(change, value); } void EllipseItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { QPainterPath clipPath; clipPath.addRect(this->boundingRect()); painter->setClipPath(clipPath); QGraphicsEllipseItem::paint(painter, option, widget); } QDomElement EllipseItem::toXml(QDomDocument doc) { QDomElement l =doc.createElement("ellipse"); l.setAttribute("x1",rect().topLeft().x()+scenePos().x()); l.setAttribute("y1",rect().topLeft().y()+scenePos().y()); l.setAttribute("x2",rect().bottomRight().x()+scenePos().x()); l.setAttribute("y2",rect().bottomRight().y()+scenePos().y()); l.setAttribute("width",ipen.width()); l.setAttribute("color",ipen.color().name()); QColor color = brush().color(); l.setAttribute("fillcolor",color.name()); l.setAttribute("alpha",color.alpha()); return l; }