CLineItem.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #include "CLineItem.h"
  2. #include <QPen>
  3. CLineItem::CLineItem(QGraphicsItem *parent) : CLineItem(QPointF(), QPointF(), parent) { }
  4. CLineItem::CLineItem(const QPointF &startPos, const QPointF &endPos, QGraphicsItem *parent)
  5. : CPathItem(parent), m_startPos(startPos), m_endPos(endPos)
  6. {
  7. updatePath();
  8. }
  9. QPointF CLineItem::startPos() const
  10. {
  11. return m_startPos;
  12. }
  13. void CLineItem::setStartPos(const QPointF start)
  14. {
  15. m_startPos = start;
  16. updatePath();
  17. }
  18. QPointF CLineItem::endPos() const
  19. {
  20. return m_endPos;
  21. }
  22. void CLineItem::setEndPos(const QPointF end)
  23. {
  24. m_endPos = end;
  25. updatePath();
  26. }
  27. CLineItem::CLineType CLineItem::lineType() const
  28. {
  29. return m_lineType;
  30. }
  31. void CLineItem::setLineType(CLineItem::CLineType type)
  32. {
  33. m_lineType = type;
  34. updatePath();
  35. }
  36. bool CLineItem::showArrow() const
  37. {
  38. return m_showArrow;
  39. }
  40. void CLineItem::setShowArrow(bool s)
  41. {
  42. m_showArrow = s;
  43. }
  44. void CLineItem::updatePath()
  45. {
  46. QPainterPath path;
  47. switch (m_lineType) {
  48. case Curve: {
  49. path.moveTo(m_startPos);
  50. double d = (m_endPos.x() - m_startPos.x()) / 4;
  51. QPointF ctr1 = QPointF(m_startPos.x() + d, m_startPos.y());
  52. QPointF ctr2 = QPointF(m_endPos.x() - d, m_endPos.y());
  53. path.cubicTo(ctr1, ctr2, m_endPos);
  54. break;
  55. }
  56. case Line: {
  57. path.moveTo(m_startPos);
  58. path.lineTo(m_endPos);
  59. break;
  60. }
  61. case Polyline: {
  62. path.moveTo(m_startPos);
  63. path.lineTo(QPointF(m_startPos.x() + 20, m_startPos.y()));
  64. path.lineTo(QPointF(m_startPos.x() + 20, m_endPos.y()));
  65. path.lineTo(m_endPos);
  66. break;
  67. }
  68. case CurveToLine: {
  69. path.moveTo(m_startPos);
  70. double d = 5;
  71. QPointF ctr1 = QPointF(m_startPos.x() + d, m_startPos.y());
  72. QPointF ctr2 = QPointF(m_startPos.x() + d * 2, m_endPos.y());
  73. QPointF p = QPointF(m_startPos.x() + d * 3, m_endPos.y());
  74. path.cubicTo(ctr1, ctr2, p);
  75. path.lineTo(m_endPos);
  76. break;
  77. }
  78. }
  79. if (m_showArrow && m_startPos != m_endPos) {
  80. path.moveTo(m_endPos + QPointF(-10, -5));
  81. path.lineTo(m_endPos);
  82. path.lineTo(m_endPos + QPointF(-10, 5));
  83. }
  84. setPath(path);
  85. }