Ei kuvausta

line.cpp 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #include "line.h"
  2. #include <QDebug>
  3. #include <cmath>
  4. using namespace std;
  5. /// \fn Line::Line(QWidget *parent)
  6. /// \~English
  7. /// \brief Constructor
  8. /// \~Spanish
  9. /// \brief Constructor
  10. Line::Line(QWidget *parent) :
  11. QWidget(parent)
  12. {
  13. x0 = y0 = x1 = y1 = 0;
  14. penColor = Qt::black;
  15. penWidth = 1;
  16. }
  17. /// \fn Line::Line(int fromX, int fromY, int toX, int toY, int w, QColor c, QWidget *parent)
  18. /// \~English
  19. /// \brief Constructor for a line, specifying the (fromX,fromY) and (toX,toY)
  20. /// \param fromX starting x coordinate
  21. /// \param fromY starting y coordinate
  22. /// \param toX end x coordinate
  23. /// \param toY end y coordinate
  24. /// \param w pen width
  25. /// \param c line color
  26. /// \param parent parent of this line
  27. /// \~Spanish
  28. /// \brief Constructor para una linea especificando el (fromX,fromY) and (toX,toY)
  29. /// \param fromX coordenada x inicial
  30. /// \param fromY coordenada y inicial
  31. /// \param toX coordenada x final
  32. /// \param toY coordenada y final
  33. /// \param w ancho de la pluma
  34. /// \param c color de la linea
  35. /// \param parent padre de esta linea
  36. Line::Line(int fromX, int fromY, int toX, int toY, int w, QColor c, QWidget *parent):
  37. QWidget(parent), x0(fromX), y0(fromY), x1(toX), y1(toY), penWidth(w), penColor(c)
  38. {
  39. resize(max(x0,x1)+1,max(y0,y1)+1);
  40. }
  41. /// \fn Line::Line(int fromX, int fromY, int length, double angle, int w, QColor c, QWidget *parent)
  42. /// \~English
  43. /// \brief Constructor for a line, specifying the (fromX,fromY) and (toX,toY)
  44. /// \param fromX starting x coordinate
  45. /// \param fromY starting y coordinate
  46. /// \param length length of the line
  47. /// \param angle angle
  48. /// \param w pen width
  49. /// \param c line color
  50. /// \param parent parent of this line
  51. /// \~Spanish
  52. /// \brief Constructor para una linea especificando el (fromX,fromY) and (toX,toY)
  53. /// \param fromX coordenada x inicial
  54. /// \param fromY coordenada y inicial
  55. /// \param length largo de la linea
  56. /// \param angle angulo de la linea
  57. /// \param w ancho de la pluma
  58. /// \param c color de la linea
  59. /// \param parent padre de esta linea
  60. Line::Line(int fromX, int fromY, int length, double angle, int w, QColor c, QWidget *parent):
  61. QWidget(parent), x0(fromX), y0(fromY), penWidth(w), penColor(c)
  62. {
  63. double angleRad = M_PI * angle / 180.;
  64. x1 = x0 + round(length * cos(angleRad));
  65. y1 = y0 + round(length * sin(angleRad));
  66. resize(max(x0,x1)+1,max(y0,y1)+1);
  67. }
  68. /// \fn void Line::setCoords(int fromX, int fromY, int toX, int toY)
  69. /// \~English
  70. /// \brief Setter for the line coordinates
  71. /// \param fromX starting x coordinate
  72. /// \param fromY starting y coordinate
  73. /// \param toX end x coordinate
  74. /// \param toY end y coordinate
  75. /// \~Spanish
  76. /// \brief Ajustador de las coordenadas de la linea
  77. /// \param fromX coordenada x inicial
  78. /// \param fromY coordenada y inicial
  79. /// \param toX coordenada x final
  80. /// \param toY end coordenada y final
  81. void Line::setCoords(int fromX, int fromY, int toX, int toY) {
  82. x0 = fromX;
  83. y0 = fromY;
  84. x1 = toX;
  85. y1 = toY;
  86. resize(max(x0,x1)+1,max(y0,y1)+1);
  87. }
  88. /// \fn void Line::setpenColor(QColor c)
  89. /// \~English
  90. /// \brief Setter for the pen color
  91. /// \param c line color
  92. /// \~Spanish
  93. /// \brief Ajustador del color del boligrafo
  94. /// \param c color de la linea
  95. void Line::setpenColor(QColor c) { penColor = c; }
  96. /// \fn void Line::setPenWidth(int w)
  97. /// \~English
  98. /// \brief Setter for the pen width
  99. /// \param w pen width
  100. /// \~Spanish
  101. /// \brief Ajustador del ancho del boligrafo
  102. /// \param w ancho de la linea
  103. void Line::setPenWidth(int w) { penWidth = w; }
  104. /// \fn int Line::getX0()
  105. /// \~English
  106. /// \brief Getter for the starting coordinate x.
  107. /// \return starting coordinate x.
  108. /// \~Spanish
  109. /// \brief Devuelve la coordenada inicial x.
  110. /// \return coordenada inicial x
  111. int Line::getX0() { return x0; }
  112. /// \fn int Line::getY0()
  113. /// \~English
  114. /// \brief Getter for the starting coordinate y.
  115. /// \return starting coordinate y.
  116. /// \~Spanish
  117. /// \brief Devuelve la coordenada inicial y.
  118. /// \return coordenada inicial y
  119. int Line::getY0() { return y0; }
  120. /// \fn int Line::getX1()
  121. /// \~English
  122. /// \brief Getter for the ending coordinate x.
  123. /// \return ending coordinate x.
  124. /// \~Spanish
  125. /// \brief Devuelve la coordenada final x.
  126. /// \return coordenada final x.
  127. int Line::getX1() { return x1; }
  128. /// \fn int Line::getY1()
  129. /// \~English
  130. /// \brief Getter for the ending coordinate y.
  131. /// \return ending coordinate y.
  132. /// \~Spanish
  133. /// \brief Devuelve la coordenada final y.
  134. /// \return coordenada final y
  135. int Line::getY1() { return y1; }
  136. /// \fn void Line::paintEvent(QPaintEvent *)
  137. /// \~English
  138. /// \brief The paint event function is automatically invoked
  139. /// whenever a resize or repaint happens.
  140. /// \~Spanish
  141. /// \brief La funcion para el evento de pintar es invocada automaticamente
  142. /// cada ves que evento de repintar ocurre.
  143. void Line::paintEvent(QPaintEvent *) {
  144. QPainter p(this);
  145. QPen myPen;
  146. myPen.setWidth(penWidth);
  147. myPen.setColor(penColor);
  148. p.setPen(myPen);
  149. p.drawLine(x0,y0,x1,y1);
  150. }