暫無描述

bird.cpp 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. ///
  10. /// Default constructor. The properties of the bird are set as follows:
  11. /// * size: set to random value of either 50, 100, 150 or 200
  12. /// * eyeBrow: set randomly to one of the five possible values
  13. /// * color and eColor: set randomly to one of the five possible colors:
  14. /// "green", "blue", "red", "yellow", "white"
  15. ///
  16. Bird::Bird(QWidget *parent) :
  17. QWidget(parent)
  18. {
  19. QString prism[5] = {"green", "blue", "red", "yellow", "white"} ;
  20. size = randInt(1, 4) * 50 ;
  21. resize(size+2,size+2) ;
  22. move(0,0) ;
  23. eyeBrow = (EyeBrowType)randInt(0,3);
  24. color = prism[randInt(0,4)] ;
  25. eColor = prism[randInt(0,4)] ;
  26. setAttribute(Qt::WA_TranslucentBackground);
  27. setStyleSheet("background:transparent;");
  28. }
  29. ///
  30. /// Constructor which accepts specification for s (size), eb (eyeBrow),
  31. /// col (faceColor), eye (eyeColor)
  32. ///
  33. Bird::Bird(int s, EyeBrowType eb, QString col, QString eye, QWidget *parent):
  34. QWidget(parent)
  35. {
  36. resize(s+2,s+2) ;
  37. move(0,0) ;
  38. size = s; eyeBrow = eb; color=col ; eColor = eye ;
  39. setAttribute(Qt::WA_TranslucentBackground);
  40. setStyleSheet("background:transparent;");
  41. }
  42. ///
  43. /// Getter for the eyeColor.
  44. ///
  45. QString Bird::getEyeColor(){
  46. return eColor ;
  47. }
  48. ///
  49. /// Getter for the faceColor.
  50. ///
  51. QString Bird::getFaceColor(){
  52. return color ;
  53. }
  54. ///
  55. /// Setter for the eyeColor.
  56. ///
  57. void Bird::setEyeColor(QString eye){
  58. eColor = eye ;
  59. }
  60. ///
  61. /// Setter for the faceColor.
  62. ///
  63. void Bird::setFaceColor(QString col){
  64. color = col;
  65. }
  66. ///
  67. /// Getter for the size.
  68. ///
  69. int Bird::getSize(){
  70. return size ;
  71. }
  72. ///
  73. /// Getter for the Eyebrow.
  74. ///
  75. Bird::EyeBrowType Bird::getEyebrow(){
  76. return eyeBrow ;
  77. }
  78. ///
  79. /// Setter for the size.
  80. ///
  81. void Bird::setSize(int s){
  82. // if(s <= 100 || s > 300)
  83. // size = 100 ;
  84. // else
  85. size = s;
  86. resize(size + 2, size + 2) ;
  87. }
  88. ///
  89. /// Setter for the Eyebrow.
  90. ///
  91. void Bird::setEyebrow(EyeBrowType eb){
  92. if(eb < 0 || eb > 3)
  93. eyeBrow = UNI ;
  94. else eyeBrow = eb;
  95. }
  96. ///
  97. /// Converts a color specified as string to a Qt::GlobalColor
  98. ///
  99. Qt::GlobalColor Bird::getColor(QString color){
  100. if(color=="red")
  101. return Qt::red ;
  102. else if (color == "green")
  103. return Qt::green ;
  104. else if (color == "yellow")
  105. return Qt::yellow ;
  106. else if(color == "blue")
  107. return Qt::blue ;
  108. else
  109. return Qt::white ;
  110. }
  111. ///
  112. /// Generates a random integer in the range [min, max]
  113. ///
  114. int Bird::randInt(int min, int max){
  115. return qrand() % ((max +1 )- min) + min ;
  116. }
  117. ///
  118. /// Assignment operator
  119. ///
  120. Bird & Bird::operator=(Bird &b) {
  121. return *this;
  122. }
  123. ///
  124. /// This function is automatically invoked each time the widget or
  125. /// its parent receives a repaint signal.
  126. ///
  127. void Bird::paintEvent(QPaintEvent *event) {
  128. QPainter p(this);
  129. QPen outlinePen(Qt::black);
  130. QPen eyebrowsPen(Qt::black) ;
  131. int ball_size = getSize() ;
  132. float half_ball = ball_size/2.0 ;
  133. float white_ball = ball_size / 4.0 ;
  134. float black_ball = white_ball * .5 ;
  135. float up_gap = ball_size * .10 ;
  136. float eyebrow_gap = ball_size *.18 ;
  137. outlinePen.setWidth(2);
  138. eyebrowsPen.setWidth(ball_size *.05);
  139. // Face
  140. p.setPen(outlinePen) ;
  141. p.setBrush(getColor(getFaceColor())) ;
  142. p.drawEllipse(0, 0, ball_size, ball_size);
  143. // Eye Balls
  144. p.setBrush(getColor(getEyeColor())) ;
  145. p.drawEllipse(half_ball-white_ball,ball_size/2-up_gap,white_ball,white_ball) ; //left eye
  146. p.drawEllipse(half_ball,ball_size/2-up_gap,white_ball,white_ball) ; //right eye
  147. // Eye iris
  148. p.setBrush(Qt::black) ;
  149. p.drawEllipse(half_ball-black_ball,ball_size/2-up_gap+(white_ball-black_ball)/2.0,black_ball,black_ball) ;
  150. p.drawEllipse(half_ball,ball_size/2-up_gap+(white_ball-black_ball)/2.0 ,black_ball,black_ball) ;
  151. // Eyebrow
  152. p.setPen(eyebrowsPen) ;
  153. if(eyeBrow == UNI){
  154. //Normal Eyebrow
  155. p.drawLine(white_ball, ball_size/2-eyebrow_gap, 3*white_ball,ball_size/2-eyebrow_gap) ;
  156. }
  157. else if(eyeBrow == ANGRY){
  158. // Angry Eyebrow
  159. p.drawLine(white_ball, ball_size/2-eyebrow_gap, 2*white_ball, ball_size/2-eyebrow_gap+up_gap) ;
  160. p.drawLine(2*white_ball, ball_size/2-eyebrow_gap + up_gap, 3*white_ball,ball_size/2-eyebrow_gap) ;
  161. }
  162. else if(eyeBrow == UPSET){
  163. // Angry Eyebrow
  164. p.drawLine(white_ball, ball_size/2-eyebrow_gap, 2*white_ball, ball_size/2-eyebrow_gap+up_gap/2) ;
  165. p.drawLine(2*white_ball, ball_size/2-eyebrow_gap + up_gap/2, 3*white_ball,ball_size/2-eyebrow_gap) ;
  166. }
  167. else{
  168. // Hairy
  169. p.drawLine(white_ball, ball_size/2-eyebrow_gap, 2*white_ball, ball_size/2-eyebrow_gap+up_gap) ;
  170. p.drawLine(2*white_ball, ball_size/2-eyebrow_gap + up_gap, 3*white_ball,ball_size/2-eyebrow_gap) ;
  171. p.drawLine(white_ball, ball_size/2-eyebrow_gap, 2*white_ball, ball_size/2-eyebrow_gap+up_gap/2) ;
  172. p.drawLine(2*white_ball, ball_size/2-eyebrow_gap + up_gap/2, 3*white_ball,ball_size/2-eyebrow_gap) ;
  173. }
  174. }