Bez popisu

mainwindow.cpp 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. ///
  2. /// Modifications:
  3. /// - 2015-01-13 [RAN] - Changed the quadraticFormulaCheck function to
  4. /// prevent giving away the expression
  5. /// - 2015-06-09 [LAA] - Modified the graphics windwow so that you aren't
  6. /// able to scroll
  7. #include "mainwindow.h"
  8. #include "ui_mainwindow.h"
  9. #include <QMessageBox>
  10. /// \fn MainWindow::MainWindow(QWidget *parent)
  11. /// \~English
  12. /// \brief Default constructor.
  13. /// \~Spanish
  14. /// \brief Constructor por defecto.
  15. ///
  16. MainWindow::MainWindow(QWidget *parent) :
  17. QMainWindow(parent),
  18. ui(new Ui::MainWindow)
  19. {
  20. quadraticFormulaCheck();
  21. // set up mainWindow
  22. ui->setupUi(this);
  23. scene= new QGraphicsScene(this);
  24. scene->setSceneRect(QRectF(QPointF(0,0), QPointF(0,0)));
  25. ui->MainView->setScene(scene);
  26. ui->MainView->setAlignment((Qt::AlignLeft | Qt::AlignTop));
  27. ui->MainView->resize(580,345);
  28. ui->MainView->move(0,0);
  29. ui->MainView->setEnabled(false);
  30. // set lables and buttons
  31. ui->alabel->move(10,360);
  32. ui->alineEdit->move(25,358);
  33. ui->blabel->move(85,360);
  34. ui->blineEdit->move(100,358);
  35. ui->clabel->move(160,360);
  36. ui->clineEdit->move(175,358);
  37. ui->RunPushButton->move(10,385);
  38. ui->retryButton->move(180,385);
  39. ui->RunPushButton->setEnabled(false);
  40. frogger = new frog;
  41. globalTimer = new QTimer;
  42. scene->addWidget(frogger);
  43. }
  44. /// \fn MainWindow::~MainWindow()
  45. /// \~English
  46. /// \brief Default destructor
  47. /// \~Spanish
  48. /// \brief Destructor por defecto
  49. ///
  50. MainWindow::~MainWindow()
  51. {
  52. delete ui;
  53. delete scene;
  54. }
  55. /// \fn void MainWindow::quadraticFormulaCheck()
  56. /// \~English
  57. /// \brief Function that uses an array of values to determine
  58. /// if the equation written by the student is correct.
  59. /// \~Spanish
  60. /// \brief Funcion que utiliza un arreglo de valores para
  61. /// determinar si la ecuacion que escribio el estudiante
  62. /// esta correcta.
  63. ///
  64. void MainWindow::quadraticFormulaCheck()
  65. {
  66. float resultFromFunction;
  67. bool pass = true;
  68. float values[] = {357 , -1000 , 698 , 1.4804781099 , 748 , -392 , 51 , 0.28391805554 ,
  69. 689 , -689 , 172 , 0.519048482944 , 90 , 521 , 751 , -2.71178575308 , 5 , 107 ,
  70. 465 , -6.0642692054 , 1609 , -983 , 150 , 0.314734649792 , 273 , 496 , 224 ,
  71. -0.839700867988 , 2 , 91 , 920 , -15.1630045473 , 48 , 300 , 463 , -2.77889067238 ,
  72. 852 , 907 , 241 , -0.510947439149};
  73. float valuesForMinus[] = {129 , -486 , 456 , 1.76744186047 , 384 , 980 , 625 , -1.30208333333 ,
  74. 270 , -904 , 755 , 1.59515823795 , 67 , -450 , 752 , 3.12650485528 , 98 , 506 , 651 ,
  75. -2.72985550047 , 38 , -373 , 901 , 4.29396930804 , 273 , -282 , 72 , 0.461538461538 ,
  76. 225 , -889 , 874 , 1.84 , 8 , 136 , 522 , -11.1457513111 , 5 , 77 , 214 , -11.760788100};
  77. float a, b, c, expected;
  78. //Verifying if the equation written by the student is correct
  79. for (int i = 0; i < 40 && pass; i+=4){
  80. a = values[i];
  81. b = values[i+1];
  82. c = values[i+2];
  83. expected = values[i+3];
  84. // firstX = (((-b) + sqrt((pow(b,2)) - 4*a*c))/(2*a));
  85. resultFromFunction = frogger->QuadraticPlus(a,b,c);
  86. if( fabs(resultFromFunction - expected) > 0.00001) {
  87. pass = false;
  88. }
  89. }
  90. if(!pass){
  91. QMessageBox::information(this, "Error", "La funcion cuadratica esta mal escrita!");
  92. exit(1);
  93. }
  94. //Verifying if the equation written by the student is correct
  95. for (int i = 0; i < 40 && pass; i+=4){
  96. a = valuesForMinus[i];
  97. b = valuesForMinus[i+1];
  98. c = valuesForMinus[i+2];
  99. expected = valuesForMinus[i+3];
  100. // firstX = (((-b) + sqrt((pow(b,2)) - 4*a*c))/(2*a));
  101. resultFromFunction = frogger->QuadraticMinus(a,b,c);
  102. if( fabs(resultFromFunction - expected) > 0.00001) {
  103. pass = false;
  104. }
  105. }
  106. if(!pass){
  107. QMessageBox::information(this, "Error", "La funcion cuadratica esta mal escrita!");
  108. exit(1);
  109. }
  110. }
  111. /// \fn void on_RunPushButton_clicked()
  112. /// \~English
  113. /// \brief Function that is invoked when the jump button is pressed,
  114. /// frog jumps to calculated location.
  115. /// \~Spanish
  116. /// \brief Funcion que se invoca cuando el boton de "jump" es presionado,
  117. /// el sapo brinca a la localizacion calculada.
  118. ///
  119. void MainWindow::on_RunPushButton_clicked()
  120. {
  121. int a = 0;
  122. int b = 0;
  123. int c = 0;
  124. int det = 0;
  125. a = (ui->alineEdit->text().toInt());
  126. b = (ui->blineEdit->text().toInt());
  127. c = (ui->clineEdit->text().toInt());
  128. det = (pow(b,2) - (4*a*c));
  129. if(a > 0){
  130. QMessageBox::information(this, "Error", "La parabola abre hacia arriba y por lo tanto el sapo no brincara");
  131. ui->alineEdit->clear();
  132. ui->blineEdit->clear();
  133. ui->clineEdit->clear();
  134. }
  135. else if(a == 0){
  136. QMessageBox::information(this, "Error", "No hay parabola y por lo tanto el sapo no brincara");
  137. ui->alineEdit->clear();
  138. ui->blineEdit->clear();
  139. ui->clineEdit->clear();
  140. }
  141. else{
  142. if(det < 0){
  143. QMessageBox::information(this, "Error", "No tiene intercepto y por lo tanto el sapo no brincara");
  144. ui->alineEdit->clear();
  145. ui->blineEdit->clear();
  146. ui->clineEdit->clear();
  147. }
  148. else if(det == 0){
  149. QMessageBox::information(this, "Error", "El vertice esta en el intercepto y por lo tanto el sapo no brincara");
  150. ui->alineEdit->clear();
  151. ui->blineEdit->clear();
  152. ui->clineEdit->clear();
  153. }
  154. else{
  155. frogger->run(a, b, c);
  156. ui->RunPushButton->setEnabled(false);
  157. ui->alineEdit->clear();
  158. ui->blineEdit->clear();
  159. ui->clineEdit->clear();
  160. }
  161. }
  162. }
  163. /// \fn void on_retryButton_clicked()
  164. /// \~English
  165. /// \brief Function that is invoked when the reset button is pressed,
  166. /// returns the window to its original state.
  167. /// \~Spanish
  168. /// \brief Funcion que se invoca cuando el boton de "reset" es presionado,
  169. /// devuelve la ventana a su estado original.
  170. ///
  171. void MainWindow::on_retryButton_clicked()
  172. {
  173. frogger->reset();
  174. }
  175. /// \fn MainWindow::on_alineEdit_textChanged(QString &arg1)
  176. /// \~English
  177. /// \brief Function that is invoked when the text is changed in the
  178. /// alineEdit, enables jump buttton if the other line edits aren't empty.
  179. /// \~Spanish
  180. /// \brief Funcion que se invoca cuando el texto dentro de alineEdit
  181. /// cambia, permite que el button de "jump" sea presionado si los otros
  182. /// "line edits" no estan vacios.
  183. ///
  184. void MainWindow::on_alineEdit_textChanged()
  185. {
  186. if(ui->alineEdit->text() != "" && ui->blineEdit->text() != "" && ui->clineEdit->text() != "" ){
  187. ui->RunPushButton->setEnabled(true);
  188. }
  189. else{
  190. ui->RunPushButton->setEnabled(false);
  191. }
  192. }
  193. /// \fn MainWindow::on_blineEdit_textChanged(QString &arg1)
  194. /// \~English
  195. /// \brief Function that is invoked when the text is changed in the
  196. /// alineEdit, enables jump buttton if the other line edits aren't empty.
  197. /// \~Spanish
  198. /// \brief Funcion que se invoca cuando el texto dentro de alineEdit
  199. /// cambia, permite que el button de "jump" sea presionado si los otros
  200. /// "line edits" no estan vacios.
  201. ///
  202. void MainWindow::on_blineEdit_textChanged()
  203. {
  204. if(ui->alineEdit->text() != "" && ui->blineEdit->text() != "" && ui->clineEdit->text() != "" ){
  205. ui->RunPushButton->setEnabled(true);
  206. }
  207. else{
  208. ui->RunPushButton->setEnabled(false);
  209. }
  210. }
  211. /// \fn MainWindow::on_clineEdit_textChanged(QString &arg1)
  212. /// \~English
  213. /// \brief Function that is invoked when the text is changed in the
  214. /// alineEdit, enables jump buttton if the other line edits aren't empty.
  215. /// \~Spanish
  216. /// \brief Funcion que se invoca cuando el texto dentro de alineEdit
  217. /// cambia, permite que el button de "jump" sea presionado si los otros
  218. /// "line edits" no estan vacios.
  219. ///
  220. void MainWindow::on_clineEdit_textChanged()
  221. {
  222. if(ui->alineEdit->text() != "" && ui->blineEdit->text() != "" && ui->clineEdit->text() != "" ){
  223. ui->RunPushButton->setEnabled(true);
  224. }
  225. else{
  226. ui->RunPushButton->setEnabled(false);
  227. }
  228. }