Açıklama Yok

line.cpp 5.1KB

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