Advertisement
Valderman

NodeEdge.cpp

May 5th, 2022
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.72 KB | None | 0 0
  1. #include "NodeEdge.hpp"
  2. //---------------------------------------------------------------------------------//
  3.  
  4.  
  5. //---------------------------------------------------------------------------------//
  6. NodeEdge::NodeEdge(
  7.             NodeEdgeType            type_,
  8.             std::shared_ptr<Socket> start_,
  9.             std::shared_ptr<Socket> finish_
  10.           )
  11.     : QGraphicsObject()
  12.     , QGraphicsPathItem()
  13.     , type(type_)
  14.     , start(start_)
  15.     , finish(finish_)
  16. {
  17.     QGraphicsObject::setFlag(QGraphicsObject::ItemIsSelectable);
  18.     QGraphicsObject::setZValue(-1);
  19. }
  20. //---------------------------------------------------------------------------------//
  21.  
  22.  
  23. //---------------------------------------------------------------------------------//
  24. void NodeEdge::setSource (QPointF source_)
  25. {
  26.     source = std::move(source_);
  27. }
  28. //---------------------------------------------------------------------------------//
  29.  
  30.  
  31. //---------------------------------------------------------------------------------//
  32. void NodeEdge::setDestination (QPointF destination_)
  33. {
  34.     destination = std::move(destination_);
  35. }
  36. //---------------------------------------------------------------------------------//
  37.  
  38.  
  39. //---------------------------------------------------------------------------------//
  40. QRectF NodeEdge::boundingRect () const
  41. {
  42.     return QGraphicsPathItem::boundingRect();
  43. }
  44. //---------------------------------------------------------------------------------//
  45.  
  46.  
  47. //---------------------------------------------------------------------------------//
  48. void NodeEdge::paint(QPainter* painter, const QStyleOptionGraphicsItem* /*a*/, QWidget* /*b*/)
  49. {
  50.  
  51.     switch (type)
  52.     {
  53.         case (NodeEdgeType::DIRECT):
  54.         {
  55.             updatePathDirect();
  56.             break;
  57.         }
  58.         case (NodeEdgeType::BEZIER):
  59.         {
  60.             updatePathBezier();
  61.             break;
  62.         }
  63.     }
  64.  
  65.     if (QGraphicsObject::isSelected())
  66.     {
  67.         QPen pen(QColor("#00ff00"));
  68.         pen.setWidth(2.0);
  69.         painter->setPen(pen);
  70.     }
  71.     else
  72.     {
  73.         QPen pen(QColor("#001000"));
  74.         pen.setWidth(2.0);
  75.         painter->setPen(pen);
  76.     }
  77.  
  78.     painter->setBrush(Qt::NoBrush);
  79.     painter->drawPath(path());
  80. }
  81. //---------------------------------------------------------------------------------//
  82.  
  83.  
  84. //---------------------------------------------------------------------------------//
  85. void NodeEdge::updatePathDirect()
  86. {
  87.     setSource (start->getSocketPosition());
  88.     setDestination (finish->getSocketPosition());
  89.  
  90.     QPainterPath pathTemp(source);
  91.     pathTemp.lineTo(destination);
  92.     setPath(pathTemp);
  93. }
  94. //---------------------------------------------------------------------------------//
  95.  
  96.  
  97. //---------------------------------------------------------------------------------//
  98. void NodeEdge::updatePathBezier()
  99. {
  100.     setSource (start->getSocketPosition());
  101.     setDestination (finish->getSocketPosition());
  102.  
  103.     double distance = (destination.x() - source.x()) * 0.5;
  104.  
  105.     if (source.x() > destination.x())
  106.     {
  107.         distance *= -1;
  108.     }
  109.  
  110.     QPainterPath pathTemp(source);
  111.     pathTemp.cubicTo(source.x()      + distance, source.y(),
  112.                      destination.x() - distance, destination.y(),
  113.                      destination.x(),            destination.y());
  114.     QGraphicsPathItem::setPath(pathTemp);
  115. }
  116. //---------------------------------------------------------------------------------//
  117.  
  118.  
  119. //---------------------------------------------------------------------------------//
  120. NodeEdge::~NodeEdge()
  121. {
  122.     #ifdef QT_DEBUG
  123.         qDebug() << "Edge deleted.";
  124.     #endif
  125. }
  126. //---------------------------------------------------------------------------------//
  127.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement