CLineItem.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #include "CLineItem.h"
  2. CLineItem::CLineItem(QGraphicsItem *parent) : CLineItem(QPointF(), QPointF(), parent) { }
  3. CLineItem::CLineItem(const QPointF &startPos, const QPointF &endPos, QGraphicsItem *parent)
  4. : CPathItem(parent), m_startPos(startPos), m_endPos(endPos)
  5. {
  6. setNormalLineColor(Qt::gray);
  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. void CLineItem::updatePath()
  37. {
  38. QPainterPath path;
  39. switch (m_lineType) {
  40. case Curve: {
  41. path.moveTo(m_startPos);
  42. double d = (m_endPos.x() - m_startPos.x()) / 4;
  43. QPointF ctr1 = QPointF(m_startPos.x() + d, m_startPos.y());
  44. QPointF ctr2 = QPointF(m_endPos.x() - d, m_endPos.y());
  45. path.cubicTo(ctr1, ctr2, m_endPos);
  46. break;
  47. }
  48. case Line: {
  49. path.moveTo(m_startPos);
  50. path.lineTo(m_endPos);
  51. break;
  52. }
  53. case Polyline: {
  54. path.moveTo(m_startPos);
  55. path.lineTo(QPointF(m_startPos.x() + 20, m_startPos.y()));
  56. path.lineTo(QPointF(m_startPos.x() + 20, m_endPos.y()));
  57. path.lineTo(m_endPos);
  58. break;
  59. }
  60. case CurveToLine: {
  61. path.moveTo(m_startPos);
  62. double d = 5;
  63. QPointF ctr1 = QPointF(m_startPos.x() + d, m_startPos.y());
  64. QPointF ctr2 = QPointF(m_startPos.x() + d * 2, m_endPos.y());
  65. QPointF p = QPointF(m_startPos.x() + d * 3, m_endPos.y());
  66. path.cubicTo(ctr1, ctr2, p);
  67. path.lineTo(m_endPos);
  68. break;
  69. }
  70. }
  71. setPath(path);
  72. }