Advertisement
Valderman

NodeEdge.cpp

May 16th, 2022
329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.53 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(nullptr)
  12.     // , QGraphicsPathItem()
  13.     /*,*/ nodeType(type_)
  14.     , start(start_)
  15.     , finish(finish_)
  16. {
  17.     QGraphicsObject::setFlag(QGraphicsObject::ItemIsSelectable);
  18.     QGraphicsPathItem::setFlag(QGraphicsPathItem::ItemIsSelectable);
  19.  
  20.     QGraphicsObject::setZValue(-1);
  21.     QGraphicsPathItem::setZValue(-1);
  22. }
  23. //---------------------------------------------------------------------------------//
  24.  
  25.  
  26. //---------------------------------------------------------------------------------//
  27. void NodeEdge::setSource (QPointF source_)
  28. {
  29.     source = std::move(source_);
  30. }
  31. //---------------------------------------------------------------------------------//
  32.  
  33.  
  34. //---------------------------------------------------------------------------------//
  35. void NodeEdge::setDestination (QPointF destination_)
  36. {
  37.     destination = std::move(destination_);
  38. }
  39. //---------------------------------------------------------------------------------//
  40.  
  41.  
  42. //---------------------------------------------------------------------------------//
  43. QRectF NodeEdge::boundingRect () const
  44. {
  45.     return NodeEdge::QGraphicsPathItem::boundingRect();
  46. }
  47. //---------------------------------------------------------------------------------//
  48.  
  49.  
  50. //---------------------------------------------------------------------------------//
  51. void NodeEdge::paint(QPainter* painter, const QStyleOptionGraphicsItem*, QWidget*)
  52. {
  53.     switch (nodeType)
  54.     {
  55.         case (NodeEdgeType::DIRECT):
  56.         {
  57.             updatePathDirect();
  58.             break;
  59.         }
  60.         case (NodeEdgeType::BEZIER):
  61.         {
  62.             updatePathBezier();
  63.             break;
  64.         }
  65.     }
  66.  
  67.     if (QGraphicsObject::isSelected())
  68.     {
  69.         QPen pen(QColor("#00ff00"));
  70.         pen.setWidth(2.0);
  71.         painter->setPen(pen);
  72.     }
  73.     else
  74.     {
  75.         QPen pen(QColor("#001000"));
  76.         pen.setWidth(2.0);
  77.         painter->setPen(pen);
  78.     }
  79.  
  80.     painter->setBrush(Qt::NoBrush);
  81.     painter->drawPath(QGraphicsPathItem::path());
  82. }
  83. //---------------------------------------------------------------------------------//
  84.  
  85. //---------------------------------------------------------------------------------//
  86. bool NodeEdge::contains(const QPointF &point) const
  87. {
  88.     return QGraphicsPathItem::contains(point);
  89. }
  90.  
  91. bool NodeEdge::isObscuredBy(const QGraphicsItem *item) const
  92. {
  93.     return QGraphicsPathItem::isObscuredBy(item);
  94. }
  95.  
  96. QPainterPath NodeEdge::opaqueArea() const
  97. {
  98.     return QGraphicsPathItem::opaqueArea();
  99. }
  100.  
  101. QPainterPath NodeEdge::shape() const
  102. {
  103.     return QGraphicsPathItem::shape();
  104. }
  105.  
  106. int NodeEdge::type() const
  107. {
  108.     return QGraphicsPathItem::type();
  109. }
  110.  
  111. //---------------------------------------------------------------------------------//
  112.  
  113. //---------------------------------------------------------------------------------//
  114. void NodeEdge::updatePathDirect()
  115. {
  116.     setSource (start->getSocketPosition());
  117.     setDestination (finish->getSocketPosition());
  118.  
  119.     QPainterPath pathTemp(source);
  120.     pathTemp.lineTo(destination);
  121.  
  122.     QGraphicsPathItem::setPath(pathTemp);
  123. }
  124. //---------------------------------------------------------------------------------//
  125.  
  126.  
  127. //---------------------------------------------------------------------------------//
  128. void NodeEdge::updatePathBezier()
  129. {
  130.     setSource (start->getSocketPosition());
  131.     setDestination (finish->getSocketPosition());
  132.  
  133.     double distance = (destination.x() - source.x()) * 0.5;
  134.  
  135.     if (source.x() > destination.x())
  136.     {
  137.         distance *= -1;
  138.     }
  139.  
  140.     QPainterPath pathTemp(source);
  141.     pathTemp.cubicTo(source.x()      + distance, source.y(),
  142.                      destination.x() - distance, destination.y(),
  143.                      destination.x(),            destination.y());
  144.  
  145.     QGraphicsPathItem::setPath(pathTemp);
  146. }
  147. //---------------------------------------------------------------------------------//
  148.  
  149.  
  150. //---------------------------------------------------------------------------------//
  151. NodeEdge::~NodeEdge()
  152. {
  153.     #ifdef QT_DEBUG
  154.         qDebug() << "Edge deleted.";
  155.     #endif
  156. }
  157. //---------------------------------------------------------------------------------//
  158.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement