Ingen beskrivning

bird.cpp 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. //
  2. // RAN [2014-06-02]
  3. // - Changed the setEyebrow, setSize functions. Resize (+2,+2)
  4. // - EyeBrowType
  5. // RAN [2014-06-03]
  6. // - TranslucentBackground for non-default constructor
  7. // - Some more comments
  8. #include "bird.h"
  9. #include <QDebug>
  10. /// \fn Bird::Bird(QWidget *parent)
  11. /// \~English
  12. /// \brief Default constructor. The properties of the bird are set as follows:
  13. /// size: set to random value of either 50, 100, 150 or 200
  14. /// * eyeBrow: set randomly to one of the five possible values
  15. /// * color and eColor: set randomly to one of the five possible colors:
  16. /// "green", "blue", "red", "yellow", "white"
  17. /// \~Spanish
  18. /// \brief Constructor por defecto. Las propiedades de el pajaron son ajustados como sigue:
  19. /// * eyeBrow: ajustado aleatoriamente a uno de los 5 posibles valores
  20. /// * color y eColor: ajustao aleatoriamente a uno de los 5 posibles colores:
  21. /// "verde", "azul", "rojo", "amarillo", "blanco"
  22. ///
  23. Bird::Bird(QWidget *parent) :
  24. QWidget(parent)
  25. {
  26. QString prism[5] = {"green", "blue", "red", "yellow", "white"} ;
  27. size = randInt(1, 4) * 50 ;
  28. resize(size+2,size+2) ;
  29. move(0,0) ;
  30. eyeBrow = (EyeBrowType)randInt(0,3);
  31. color = prism[randInt(0,4)] ;
  32. eColor = prism[randInt(0,4)] ;
  33. setAttribute(Qt::WA_TranslucentBackground);
  34. setStyleSheet("background:transparent;");
  35. }
  36. /// \fn Bird::Bird(int s, EyeBrowType eb, QString col, QString eye, QWidget *parent)
  37. /// \~English
  38. /// \brief Constructor which accepts specification of the attributes of the bird
  39. /// \param s size
  40. /// \param eb eyeBrow
  41. /// \param col (faceColor)
  42. /// \param eye (eyeColor)
  43. /// \~Spanish
  44. /// \brief Constructor que acepta especificaciones de los atributos del pajaro
  45. /// \param s tamaño
  46. /// \param eb cejas
  47. /// \param col color de la cara
  48. /// \param eye color del ojo
  49. Bird::Bird(int s, EyeBrowType eb, QString col, QString eye, QWidget *parent):
  50. QWidget(parent)
  51. {
  52. resize(s+2,s+2) ;
  53. move(0,0) ;
  54. size = s; eyeBrow = eb; color=col ; eColor = eye ;
  55. setAttribute(Qt::WA_TranslucentBackground);
  56. setStyleSheet("background:transparent;");
  57. }
  58. /// \fn QString Bird::getEyeColor()
  59. /// \~English
  60. /// \brief Getter for the eyeColor.
  61. /// \~Spanish
  62. /// \brief Devuelve el color del ojo.
  63. QString Bird::getEyeColor() const {
  64. return eColor ;
  65. }
  66. /// \fn QString Bird::getFaceColor()
  67. /// \~English
  68. /// \brief Getter for the faceColor.
  69. /// \~Spanish
  70. /// \brief Devuelve el color de la cara.
  71. QString Bird::getFaceColor() const {
  72. return color ;
  73. }
  74. /// \fn void Bird::setEyeColor(QString eye)
  75. /// \~English
  76. /// \brief Setter for the eyeColor.
  77. /// \param eye eyeColor
  78. /// \~Spanish
  79. /// \brief Ajusta el color del ojo
  80. /// \param eje color del ojo
  81. void Bird::setEyeColor(QString eye){
  82. eColor = eye ;
  83. }
  84. /// \fn void Bird::setFaceColor(QString col)
  85. /// \~English
  86. /// \brief Setter for the face color.
  87. /// \param col face Color
  88. /// \~Spanish
  89. /// \brief Ajusta el color de la cara
  90. /// \param col color de la cara
  91. void Bird::setFaceColor(QString col){
  92. color = col;
  93. }
  94. /// \fn int Bird::getSize()
  95. /// \~English
  96. /// \brief Getter for the bird size.
  97. /// \~Spanish
  98. /// \brief Devuelve el tamano del pajaro.
  99. int Bird::getSize() const {
  100. return size ;
  101. }
  102. /// \fn int Bird::getEyeBrow()
  103. /// \~English
  104. /// \brief Getter for the bird eyeBrow type
  105. /// \~Spanish
  106. /// \brief Devuelve el tipo de ceja
  107. Bird::EyeBrowType Bird::getEyebrow() const{
  108. return eyeBrow ;
  109. }
  110. /// \fn void Bird::setSize(int s)
  111. /// \~English
  112. /// \brief Setter for the bird size.
  113. /// \param s bird size
  114. /// \~Spanish
  115. /// \brief Ajusta el tamano del pajaro
  116. /// \param s tamano del pajaro
  117. void Bird::setSize(int s){
  118. if(s <= 50) { size = 50; }
  119. else if (s >= 200) { size = 200; }
  120. else { size = s; }
  121. resize(size + 2, size + 2) ;
  122. }
  123. /// \fn void Bird::setEyeBrow(QString eye)
  124. /// \~English
  125. /// \brief Setter for the eyebrow type.
  126. /// \param eb eyebrow type
  127. /// \~Spanish
  128. /// \brief Ajusta el tipo de cejas
  129. /// \param eb tipo de ceja
  130. void Bird::setEyebrow(EyeBrowType eb){
  131. if(eb < 0 || eb > 3)
  132. eyeBrow = UNI ;
  133. else eyeBrow = eb;
  134. }
  135. /// \fn Qt::GlobalColor Bird::getColor(QString color)
  136. /// \~English
  137. /// \brief Converts a color specified as string to a Qt::GlobalColor
  138. /// \param color string with a color
  139. /// \~Spanish
  140. /// \brief Convierte un color especificado como un string a un Qt::GlobalColor
  141. /// \param color cadena de caracteres de un color
  142. Qt::GlobalColor Bird::getColor(QString color) const{
  143. if(color=="red")
  144. return Qt::red ;
  145. else if (color == "green")
  146. return Qt::green ;
  147. else if (color == "yellow")
  148. return Qt::yellow ;
  149. else if(color == "blue")
  150. return Qt::blue ;
  151. else
  152. return Qt::white ;
  153. }
  154. /// \fn int Bird::randInt(int min, int max)
  155. /// \~English
  156. /// \brief Generates a random integer in the range [min, max]
  157. /// \param min minimum random value
  158. /// \param max maximum random value
  159. /// \~Spanish
  160. /// \brief Genera un entero aleatorio en el rango [min, max]
  161. /// \param min valor aleatorio minimo
  162. /// \param max valor aleatorio maximo
  163. int Bird::randInt(int min, int max){
  164. return qrand() % ((max +1 )- min) + min ;
  165. }
  166. /// \fn void Bird::paintEvent(QPaintEvent *event)
  167. /// \~English
  168. /// \brief This function is automatically invoked each time the widget or
  169. /// its parent receives a repaint signal.
  170. /// \~Spanish
  171. /// \brief Esta funcion es invocada automaticamente cada ves que el widget
  172. /// o su padre recive una senal de repintar.
  173. ///
  174. void Bird::paintEvent(QPaintEvent *) {
  175. QPainter p(this);
  176. QPen outlinePen(Qt::black);
  177. QPen eyebrowsPen(Qt::black) ;
  178. int ball_size = getSize() ;
  179. float half_ball = ball_size/2.0 ;
  180. float white_ball = ball_size / 4.0 ;
  181. float black_ball = white_ball * .5 ;
  182. float up_gap = ball_size * .10 ;
  183. float eyebrow_gap = ball_size *.18 ;
  184. outlinePen.setWidth(2);
  185. eyebrowsPen.setWidth(ball_size *.05);
  186. // Face
  187. p.setPen(outlinePen) ;
  188. p.setBrush(getColor(getFaceColor())) ;
  189. p.drawEllipse(0, 0, ball_size, ball_size);
  190. // Eye Balls
  191. p.setBrush(getColor(getEyeColor())) ;
  192. p.drawEllipse(half_ball-white_ball,ball_size/2-up_gap,white_ball,white_ball) ; //left eye
  193. p.drawEllipse(half_ball,ball_size/2-up_gap,white_ball,white_ball) ; //right eye
  194. // Eye iris
  195. p.setBrush(Qt::black) ;
  196. p.drawEllipse(half_ball-black_ball,ball_size/2-up_gap+(white_ball-black_ball)/2.0,black_ball,black_ball) ;
  197. p.drawEllipse(half_ball,ball_size/2-up_gap+(white_ball-black_ball)/2.0 ,black_ball,black_ball) ;
  198. // Eyebrow
  199. p.setPen(eyebrowsPen) ;
  200. if(eyeBrow == UNI){
  201. //Normal Eyebrow
  202. p.drawLine(white_ball, ball_size/2-eyebrow_gap, 3*white_ball,ball_size/2-eyebrow_gap) ;
  203. }
  204. else if(eyeBrow == ANGRY){
  205. // Angry Eyebrow
  206. p.drawLine(white_ball, ball_size/2-eyebrow_gap, 2*white_ball, ball_size/2-eyebrow_gap+up_gap) ;
  207. p.drawLine(2*white_ball, ball_size/2-eyebrow_gap + up_gap, 3*white_ball,ball_size/2-eyebrow_gap) ;
  208. }
  209. else if(eyeBrow == UPSET){
  210. // Angry Eyebrow
  211. p.drawLine(white_ball, ball_size/2-eyebrow_gap, 2*white_ball, ball_size/2-eyebrow_gap+up_gap/2) ;
  212. p.drawLine(2*white_ball, ball_size/2-eyebrow_gap + up_gap/2, 3*white_ball,ball_size/2-eyebrow_gap) ;
  213. }
  214. else{
  215. // Hairy
  216. p.drawLine(white_ball, ball_size/2-eyebrow_gap, 2*white_ball, ball_size/2-eyebrow_gap+up_gap) ;
  217. p.drawLine(2*white_ball, ball_size/2-eyebrow_gap + up_gap, 3*white_ball,ball_size/2-eyebrow_gap) ;
  218. p.drawLine(white_ball, ball_size/2-eyebrow_gap, 2*white_ball, ball_size/2-eyebrow_gap+up_gap/2) ;
  219. p.drawLine(2*white_ball, ball_size/2-eyebrow_gap + up_gap/2, 3*white_ball,ball_size/2-eyebrow_gap) ;
  220. }
  221. }