No Description

bird.cpp 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. /// \fn Bird::Bird(QWidget *parent)
  10. /// \~English
  11. /// \brief Default constructor. The properties of the bird are set as follows:
  12. /// size: set to random value of either 50, 100, 150 or 200
  13. /// * eyeBrow: set randomly to one of the five possible values
  14. /// * color and eColor: set randomly to one of the five possible colors:
  15. /// "green", "blue", "red", "yellow", "white"
  16. /// \~Spanish
  17. /// \brief Constructor por defecto. Las propiedades de el pajaron son ajustados como sigue:
  18. /// * eyeBrow: ajustado aleatoriamente a uno de los 5 posibles valores
  19. /// * color y eColor: ajustao aleatoriamente a uno de los 5 posibles colores:
  20. /// "verde", "azul", "rojo", "amarillo", "blanco"
  21. ///
  22. Bird::Bird(QWidget *parent) :
  23. QWidget(parent)
  24. {
  25. QString prism[5] = {"green", "blue", "red", "yellow", "white"} ;
  26. size = randInt(1, 4) * 50 ;
  27. resize(size+2,size+2) ;
  28. move(0,0) ;
  29. eyeBrow = (EyeBrowType)randInt(0,3);
  30. color = prism[randInt(0,4)] ;
  31. eColor = prism[randInt(0,4)] ;
  32. setAttribute(Qt::WA_TranslucentBackground);
  33. setStyleSheet("background:transparent;");
  34. }
  35. /// \fn Bird::Bird(int s, EyeBrowType eb, QString col, QString eye, QWidget *parent)
  36. /// \~English
  37. /// \brief Constructor which accepts specification of the attributes of the bird
  38. /// \param s size
  39. /// \param eb eyeBrow
  40. /// \param col (faceColor)
  41. /// \param eye (eyeColor)
  42. /// \~Spanish
  43. /// \brief Constructor que acepta especificaciones de los atributos del pajaro
  44. /// \param s tamaño
  45. /// \param eb cejas
  46. /// \param col color de la cara
  47. /// \param eye color del ojo
  48. Bird::Bird(int s, EyeBrowType eb, QString col, QString eye, QWidget *parent):
  49. QWidget(parent)
  50. {
  51. resize(s+2,s+2) ;
  52. move(0,0) ;
  53. size = s; eyeBrow = eb; color=col ; eColor = eye ;
  54. setAttribute(Qt::WA_TranslucentBackground);
  55. setStyleSheet("background:transparent;");
  56. }
  57. /// \fn QString Bird::getEyeColor()
  58. /// \~English
  59. /// \brief Getter for the eyeColor.
  60. /// \~Spanish
  61. /// \brief Devuelve el color del ojo.
  62. QString Bird::getEyeColor() const{
  63. return eColor ;
  64. }
  65. /// \fn QString Bird::getFaceColor()
  66. /// \~English
  67. /// \brief Getter for the faceColor.
  68. /// \~Spanish
  69. /// \brief Devuelve el color de la cara.
  70. QString Bird::getFaceColor() const{
  71. return color ;
  72. }
  73. /// \fn void Bird::setEyeColor(QString eye)
  74. /// \~English
  75. /// \brief Setter for the eyeColor.
  76. /// \param eye eyeColor
  77. /// \~Spanish
  78. /// \brief Ajusta el color del ojo
  79. /// \param eje color del ojo
  80. void Bird::setEyeColor(QString eye){
  81. eColor = eye ;
  82. }
  83. /// \fn void Bird::setFaceColor(QString col)
  84. /// \~English
  85. /// \brief Setter for the face color.
  86. /// \param col face Color
  87. /// \~Spanish
  88. /// \brief Ajusta el color de la cara
  89. /// \param col color de la cara
  90. void Bird::setFaceColor(QString col){
  91. color = col;
  92. }
  93. /// \fn int Bird::getSize()
  94. /// \~English
  95. /// \brief Getter for the bird size.
  96. /// \~Spanish
  97. /// \brief Devuelve el tamano del pajaro.
  98. int Bird::getSize() const{
  99. return size ;
  100. }
  101. /// \fn int Bird::getEyeBrow()
  102. /// \~English
  103. /// \brief Getter for the bird eyeBrow type
  104. /// \~Spanish
  105. /// \brief Devuelve el tipo de ceja
  106. Bird::EyeBrowType Bird::getEyebrow() const{
  107. return eyeBrow ;
  108. }
  109. /// \fn void Bird::setSize(int s)
  110. /// \~English
  111. /// \brief Setter for the bird size.
  112. /// \param s bird size
  113. /// \~Spanish
  114. /// \brief Ajusta el tamano del pajaro
  115. /// \param s tamano del pajaro
  116. void Bird::setSize(int s){
  117. if(s <= 100 || s > 300)
  118. size = 100 ;
  119. else size = s;
  120. resize(size + 2, size + 2) ;
  121. }
  122. /// \fn void Bird::setEyebrow(QString eye)
  123. /// \~English
  124. /// \brief Setter for the eyebrow type.
  125. /// \param eb eyebrow type
  126. /// \~Spanish
  127. /// \brief Ajusta el tipo de cejas
  128. /// \param eb tipo de ceja
  129. void Bird::setEyebrow(EyeBrowType eb){
  130. if(eb < 0 || eb > 3)
  131. eyeBrow = UNI ;
  132. else eyeBrow = eb;
  133. }
  134. /// \fn Qt::GlobalColor Bird::getColor(QString color)
  135. /// \~English
  136. /// \brief Converts a color specified as string to a Qt::GlobalColor
  137. /// \param color string with a color
  138. /// \~Spanish
  139. /// \brief Convierte un color especificado como un string a un Qt::GlobalColor
  140. /// \param color cadena de caracteres de un color
  141. Qt::GlobalColor Bird::getColor(QString color) const{
  142. if(color=="red")
  143. return Qt::red ;
  144. else if (color == "green")
  145. return Qt::green ;
  146. else if (color == "yellow")
  147. return Qt::yellow ;
  148. else if(color == "blue")
  149. return Qt::blue ;
  150. else
  151. return Qt::white ;
  152. }
  153. /// \fn int Bird::randInt(int min, int max)
  154. /// \~English
  155. /// \brief Generates a random integer in the range [min, max]
  156. /// \param min minimum random value
  157. /// \param max maximum random value
  158. /// \~Spanish
  159. /// \brief Genera un entero aleatorio en el rango [min, max]
  160. /// \param min valor aleatorio minimo
  161. /// \param max valor aleatorio maximo
  162. ///
  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. }