Qt Quick based coffee brewing control chart.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

qmllineitem.cpp 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #include "qmllineitem.h"
  2. QmlLineItem::QmlLineItem(QDeclarativeItem *parent) :
  3. QDeclarativeItem(parent), m_x1(0), m_y1(0), m_x2(0), m_y2(0),
  4. m_color(Qt::black), m_penWidth(1)
  5. {
  6. setFlag(QGraphicsItem::ItemHasNoContents, false);
  7. }
  8. void QmlLineItem::paint(QPainter *painter,
  9. const QStyleOptionGraphicsItem *option,
  10. QWidget *widget)
  11. {
  12. QPen pen(m_color, m_penWidth);
  13. painter->setPen(pen);
  14. if(smooth() == true)
  15. {
  16. painter->setRenderHint(QPainter::Antialiasing, true);
  17. }
  18. int x = qMin(m_x1, m_x2) - m_penWidth/2;
  19. int y = qMin(m_y1, m_y2) - m_penWidth/2;
  20. painter->drawLine(m_x1 - x, m_y1 - y, m_x2 - x, m_y2 - y);
  21. }
  22. int QmlLineItem::x1() const
  23. {
  24. return m_x1;
  25. }
  26. int QmlLineItem::y1() const
  27. {
  28. return m_y1;
  29. }
  30. int QmlLineItem::x2() const
  31. {
  32. return m_x2;
  33. }
  34. int QmlLineItem::y2() const
  35. {
  36. return m_y2;
  37. }
  38. QColor QmlLineItem::color() const
  39. {
  40. return m_color;
  41. }
  42. int QmlLineItem::penWidth() const
  43. {
  44. return m_penWidth;
  45. }
  46. void QmlLineItem::setX1(int x1)
  47. {
  48. if(m_x1 == x1)
  49. {
  50. return;
  51. }
  52. m_x1 = x1;
  53. updateSize();
  54. emit x1Changed();
  55. update();
  56. }
  57. void QmlLineItem::setY1(int y1)
  58. {
  59. if(m_y1 == y1)
  60. {
  61. return;
  62. }
  63. m_y1 = y1;
  64. updateSize();
  65. emit y1Changed();
  66. update();
  67. }
  68. void QmlLineItem::setX2(int x2)
  69. {
  70. if(m_x2 == x2)
  71. {
  72. return;
  73. }
  74. m_x2 = x2;
  75. updateSize();
  76. emit x2Changed();
  77. update();
  78. }
  79. void QmlLineItem::setY2(int y2)
  80. {
  81. if(m_y2 == y2)
  82. {
  83. return;
  84. }
  85. m_y2 = y2;
  86. updateSize();
  87. emit y2Changed();
  88. update();
  89. }
  90. void QmlLineItem::setColor(const QColor &color)
  91. {
  92. if(m_color == color)
  93. {
  94. return;
  95. }
  96. m_color = color;
  97. emit colorChanged();
  98. update();
  99. }
  100. void QmlLineItem::setPenWidth(int newWidth)
  101. {
  102. if(m_penWidth == newWidth)
  103. {
  104. return;
  105. }
  106. m_penWidth = newWidth;
  107. updateSize();
  108. emit penWidthChanged();
  109. update();
  110. }
  111. void QmlLineItem::updateSize()
  112. {
  113. setX(qMin(m_x1, m_x2) - m_penWidth/2);
  114. setY(qMin(m_y1, m_y2) - m_penWidth/2);
  115. setWidth(qAbs(m_x2 - m_x1) + m_penWidth);
  116. setHeight(qAbs(m_y2 - m_y1) + m_penWidth);
  117. }