Nessuna descrizione

mainwindow.cpp 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. // RAN 2015-06-26 - Added Zulu Zone label
  2. // - Removed the onclick functions for the buttons that are no longer used
  3. #include "mainwindow.h"
  4. #include "ui_mainwindow.h"
  5. #include "secondwindow.cpp"
  6. #include <QDebug>
  7. #include <QTimer>
  8. #include <QtCore/qmath.h>
  9. #include <QMessageBox>
  10. #include "functions.h"
  11. MainWindow::MainWindow(QWidget *parent) :
  12. QMainWindow(parent),
  13. ui(new Ui::MainWindow)
  14. {
  15. ui->setupUi(this);
  16. //These are the size of the arrays of widgets 'line-edit',
  17. //'label' and 'button'
  18. buttonSize = 2;
  19. lineSize = 7;
  20. labelSize = 8;
  21. // We set every pointer member to point NULL because we
  22. // when we delete every pointer we dont want to delete a
  23. // pointer that was already deleted in other moment or
  24. // was never used
  25. layout = NULL;
  26. for (int i = 0; i<buttonSize ; i++){
  27. button[i] = 0;
  28. }
  29. for (int i = 0; i < lineSize ; i++){
  30. line[i] = 0;
  31. }
  32. for (int i = 0; i<labelSize ; i++){
  33. label[i] = 0;
  34. }
  35. window = new secondwindow;
  36. // We need to know whenever the second window is closed to show the
  37. // main window, or to hide when the second one is showed
  38. connect(window, SIGNAL(closeIt(bool)), this, SLOT(showMW(bool)));
  39. if(!dice1.load(":/images/resources/d1.png") || !dice2.load(":/images/resources/d1.png")){
  40. qDebug() << "Error1 Loading image";
  41. }
  42. initCheckWMaps();
  43. }
  44. MainWindow::~MainWindow()
  45. {
  46. delete ui;
  47. }
  48. void MainWindow::showMW(bool doShow){
  49. if (doShow){
  50. show();
  51. // Deleting pointers and point them to NULL
  52. for (int i = 0; i<buttonSize ; i++){
  53. delete button[i];
  54. button[i] = NULL;
  55. }
  56. for (int i = 0; i<lineSize ; i++){
  57. delete line[i];
  58. line[i] = NULL;
  59. }
  60. for (int i = 0; i<labelSize ; i++){
  61. delete label[i];
  62. label[i] = NULL;
  63. }
  64. delete layout;
  65. layout = NULL;
  66. delete window;
  67. // Create the new window and connecting it again with the signal
  68. window = new secondwindow;
  69. connect(window, SIGNAL(closeIt(bool)), this, SLOT(showMW(bool)));
  70. }
  71. else hide();
  72. }
  73. void MainWindow::setOption(QString str){
  74. option = str;
  75. }
  76. //Clear all the lines that are used
  77. void MainWindow::clearLines(){
  78. for (int i = 0; i < lineSize; i++){
  79. if (line[i] != NULL){
  80. line[i]->clear();
  81. }
  82. }
  83. score1 = score2 = 0;
  84. }
  85. void MainWindow::callOneOfTheSorts(){
  86. QString a = line[0]->text();
  87. QString b = line[1]->text();
  88. QString c = line[2]->text();
  89. if (!validateSorts(a,b,c)) {
  90. QMessageBox::warning(this, "Alert",
  91. "Please provide the three non-empty strings");
  92. return;
  93. }
  94. if (option == "Version Alpha"){
  95. mySortAlpha(a,b,c);
  96. }
  97. else if (option == "Version Beta"){
  98. mySortBeta(a,b,c);
  99. }
  100. else if (option == "Version Gamma"){
  101. mySortGamma(a,b,c);
  102. }
  103. else{
  104. mySortDelta(a,b,c);
  105. }
  106. line[3]->setText(a);
  107. line[4]->setText(b);
  108. line[5]->setText(c);
  109. }
  110. void MainWindow::callOneOfTheRPS(){
  111. QString p1 = this->cmbPlayer01->currentText();
  112. QString p2 = this->cmbPlayer02->currentText();
  113. if (p1.length() * p2.length() == 0) {
  114. QMessageBox::warning(this, "Alert",
  115. "Please provide a play for both players and the number of games to win");
  116. return;
  117. }
  118. int winnerNum;
  119. if (option == "Version Alpha")
  120. winnerNum = RPSDelta(p1.toStdString()[0], p2.toStdString()[0]);
  121. else if (option == "Version Beta")
  122. winnerNum = RPSBeta(p1.toStdString()[0], p2.toStdString()[0]);
  123. else if (option == "Version Gamma")
  124. winnerNum = RPSGamma(p1.toStdString()[0], p2.toStdString()[0]);
  125. else
  126. winnerNum = RPSDelta(p1.toStdString()[0], p2.toStdString()[0]);
  127. QString st;
  128. switch(winnerNum) {
  129. case 1: st = "Player 1"; break;
  130. case 2: st = "Player 2"; break;
  131. case 0: st = "Tie"; break;
  132. default: st = "Error";
  133. }
  134. line[0]->setText(st);
  135. }
  136. void MainWindow::paintDice() {
  137. this->timerCounter++;
  138. int a, b;
  139. a = rand()%6;
  140. b = rand()%6;
  141. label[3]->setPixmap(QPixmap::fromImage(diceImages[a]));
  142. label[4]->setPixmap(QPixmap::fromImage(diceImages[b]));
  143. if (this->timerCounter == 10) {
  144. aTimer->stop();
  145. diceFinal01 = a;
  146. diceFinal02 = b;
  147. if (option == "Version Alpha")
  148. diceBeta();
  149. else if (option == "Version Beta")
  150. diceGamma();
  151. else if (option == "Version Gamma")
  152. diceAlpha();
  153. else
  154. diceAlpha();
  155. }
  156. }
  157. void MainWindow::callOneOfTheDices(){
  158. for (int i = 1; i <= 6; i++)
  159. this->diceImages.push_back(QImage(":/images/resources/d" + QString::number(i) + ".png"));
  160. aTimer = new QTimer;
  161. aTimer->setInterval(100);
  162. aTimer->setSingleShot(false);
  163. aTimer->start();
  164. timerCounter = 0;
  165. QObject::connect(aTimer,&QTimer::timeout, this, &MainWindow::paintDice); //[&](){ birth(w, juana, avelardo, piolin);});
  166. }
  167. //========================================================
  168. // Functions for the dice roller
  169. //========================================================
  170. void MainWindow::diceAlpha(){
  171. int total = diceFinal01 + diceFinal02 + 2;
  172. line[0]->setText(QString::number(total));
  173. }
  174. void MainWindow::diceBeta(){
  175. int total = diceFinal01 + diceFinal01 + 2;
  176. line[0]->setText(QString::number(total));
  177. }
  178. void MainWindow::diceGamma(){
  179. int total = diceFinal01 - diceFinal02;
  180. line[0]->setText(QString::number(total));
  181. }
  182. void MainWindow::diceDelta(){
  183. int total = diceFinal01 + diceFinal02;
  184. line[0]->setText(QString::number(total));
  185. }
  186. void MainWindow::callOneOfTheChecks(){
  187. unsigned int qty;
  188. if (!validateCheckQty(line[0]->text(),qty)) {
  189. QMessageBox::warning(this, "Alert",
  190. "Enter a valid amount!");
  191. }
  192. if (option == "Version Alpha")
  193. line[1]->setText( checkWAlpha(qty) );
  194. else if (option == "Version Beta")
  195. line[1]->setText( checkWBeta(qty) );
  196. else if (option == "Version Gamma")
  197. line[1]->setText( checkWGamma(qty) );
  198. else
  199. line[1]->setText( checkWDelta(qty) );
  200. }
  201. void MainWindow::callOneOfTheZulus(){
  202. QString zuluTime = line[0]->text();
  203. QString zuluZone = line[1]->text();
  204. int hours, minutes;
  205. if (!validZuluTime(zuluTime, zuluZone, hours, minutes) ) {
  206. QMessageBox::warning(this, "Alert",
  207. "Either Zulu Time or Zone is not valid.");
  208. line[2]->setText("Error");
  209. return;
  210. }
  211. if (option == "Version Alpha")
  212. line[2]->setText( zuluAlpha(hours, minutes, zuluZone.toStdString()[0]) );
  213. else if (option == "Version Beta")
  214. line[2]->setText( zuluBeta(hours, minutes, zuluZone.toStdString()[0]) );
  215. else if (option == "Version Gamma")
  216. line[2]->setText( zuluGamma(hours, minutes, zuluZone.toStdString()[0]) );
  217. else
  218. line[2]->setText( zuluDelta(hours, minutes, zuluZone.toStdString()[0]) );
  219. }
  220. void MainWindow::on_SortsButton_clicked()
  221. {
  222. //Create a new QComboBox and add the items in it
  223. method = new QComboBox;
  224. method->addItem("Version Alpha");
  225. method->addItem("Version Beta");
  226. method->addItem("Version Gamma");
  227. method->addItem("Version Delta");
  228. option = "Version Alpha"; //Default option is alpha
  229. //We create a new layout and insert the comboBox to it
  230. layout = new QGridLayout;
  231. layout->addWidget(method, 0, 0, 1, -1);
  232. //The buttons needed are sort and clear so we create them
  233. button[0] = new QPushButton("Sort");
  234. button[1] = new QPushButton("Clear");
  235. //3 lines for input and 3 for output
  236. for (int i = 0; i<6 ; i++){
  237. line[i] = new QLineEdit;
  238. }
  239. //The user is not able to write on the output lines
  240. line[3]->setEnabled(false);
  241. line[4]->setEnabled(false);
  242. line[5]->setEnabled(false);
  243. //Labels to let the user understand the app
  244. label[0] = new QLabel("Input");
  245. label[1] = new QLabel("Output");
  246. //Here we insert the widgets on the layout
  247. layout->addWidget(label[0], 1, 0);
  248. layout->addWidget(label[1], 1, 2);
  249. layout->addWidget(line[0], 2, 0);
  250. layout->addWidget(line[1], 3, 0);
  251. layout->addWidget(line[2], 4, 0);
  252. layout->addWidget(button[0], 1, 1, 2, 1);
  253. layout->addWidget(button[1], 3, 1, 2, 1);
  254. layout->addWidget(line[3], 2, 2);
  255. layout->addWidget(line[4], 3, 2);
  256. layout->addWidget(line[5], 4, 2);
  257. //Here we connect the signals of the widgets generated
  258. //by code to their respective functions
  259. connect(method, SIGNAL(currentIndexChanged(QString)), this, SLOT(setOption(QString)));
  260. connect(button[0], SIGNAL(clicked()), this, SLOT(callOneOfTheSorts()));
  261. connect(button[1], SIGNAL(clicked()), this, SLOT(clearLines()));
  262. //Now that we have set our new window, we hide the main-window
  263. //and show the new one. The new-window has a signal that
  264. //notify when it was closed so we can shor the main-window
  265. window->setLayout(layout);
  266. showMW(false);
  267. window->show();
  268. }
  269. void MainWindow::on_DiceButton_clicked()
  270. {
  271. //Create a new QComboBox and add the items in it
  272. method = new QComboBox;
  273. method->addItem("Version Alpha");
  274. method->addItem("Version Beta");
  275. method->addItem("Version Gamma");
  276. method->addItem("Version Delta");
  277. option = "Version Alpha"; //Default option is alpha
  278. //We create a new layout and insert the comboBox to it
  279. layout = new QGridLayout;
  280. layout->addWidget(method, 0, 0, 1, -1);
  281. //Labels to let the user understand the app
  282. label[0] = new QLabel("Dice 1");
  283. label[1] = new QLabel("Dice 2");
  284. label[2] = new QLabel("Total");
  285. //The user is not able to write on the output line
  286. line[0] = new QLineEdit;
  287. line[0]->setEnabled(false);
  288. //Here we just need one button to roll the dices
  289. button[0] = new QPushButton("Roll them!");
  290. //Labels to put the dices' images on them
  291. label[3] = new QLabel;
  292. label[4] = new QLabel;
  293. label[3]->setPixmap(QPixmap::fromImage(dice1));
  294. label[4]->setPixmap(QPixmap::fromImage(dice2));
  295. //Here we insert the widgets on the layout
  296. layout->addWidget(label[0], 1, 0);
  297. layout->addWidget(label[3], 1, 1);
  298. layout->addWidget(label[1], 2, 0);
  299. layout->addWidget(label[4], 2, 1);
  300. layout->addWidget(label[2], 3, 0);
  301. layout->addWidget(line[0], 3, 1);
  302. layout->addWidget(button[0], 1, 2, 2, 1);
  303. //Here we connect the signals of the widgets generated
  304. //by code to their respective functions
  305. connect(method, SIGNAL(currentIndexChanged(QString)), this, SLOT(setOption(QString)));
  306. connect(button[0], SIGNAL(clicked()), this, SLOT(callOneOfTheDices()));
  307. //Now that we have set our new window, we hide the main-window
  308. //and show the new one. The new-window has a signal that
  309. //notify when it was closed so we can shor the main-window
  310. window->setLayout(layout);
  311. showMW(false);
  312. window->show();
  313. }
  314. void MainWindow::on_RPSButton_clicked()
  315. {
  316. //Create a new QComboBox and add the items in it
  317. method = new QComboBox;
  318. method->addItem("Version Alpha");
  319. method->addItem("Version Beta");
  320. method->addItem("Version Gamma");
  321. method->addItem("Version Delta");
  322. option = "Version Alpha"; //Default option is alpha
  323. cmbPlayer01 = new QComboBox;
  324. cmbPlayer02 = new QComboBox;
  325. cmbPlayer01->addItem("rock"); cmbPlayer02->addItem("rock");
  326. cmbPlayer01->addItem("paper"); cmbPlayer02->addItem("paper");
  327. cmbPlayer01->addItem("scisors"); cmbPlayer02->addItem("scisors");
  328. //The buttons needed here are the 'play' and 'clear' buttons
  329. button[0] = new QPushButton("Play");
  330. line[0] = new QLineEdit;
  331. //The user is not able to write on the output line
  332. line[0]->setEnabled(false);
  333. //Labels to let the user understand the app
  334. label[0] = new QLabel("Player 1's moves");
  335. label[1] = new QLabel("Player 2's moves");
  336. label[2] = new QLabel("Winner:");
  337. //We create a new layout and insert the comboBox to it
  338. int row = 0;
  339. layout = new QGridLayout;
  340. layout->addWidget(method, row++, 0);
  341. layout->addWidget(label[0], row, 0);
  342. layout->addWidget(cmbPlayer01, row++, 1);
  343. layout->addWidget(label[1], row, 0);
  344. layout->addWidget(cmbPlayer02, row++, 1);
  345. layout->addWidget(button[0], row++, 1);
  346. layout->addWidget(line[0], row, 1);
  347. layout->addWidget(label[2], row++, 0);
  348. //Here we connect the signals of the widgets generated
  349. //by code to their respective functions
  350. connect(method, SIGNAL(currentIndexChanged(QString)), this, SLOT(setOption(QString)));
  351. connect(button[0], SIGNAL(clicked()), this, SLOT(callOneOfTheRPS()));
  352. //Now that we have set our new window, we hide the main-window
  353. //and show the new one. The new-window has a signal that
  354. //notify when it was closed so we can shor the main-window
  355. window->setLayout(layout);
  356. showMW(false);
  357. window->show();
  358. }
  359. void MainWindow::on_ZuluButton_clicked()
  360. {
  361. //Create a new QComboBox and add the items in it
  362. method = new QComboBox;
  363. method->addItem("Version Alpha");
  364. method->addItem("Version Beta");
  365. method->addItem("Version Gamma");
  366. method->addItem("Version Delta");
  367. option = "Version Alpha"; //Default option is alpha
  368. //We create a new layout and insert the comboBox to it
  369. layout = new QGridLayout;
  370. layout->addWidget(method, 0, 0, 1, -1);
  371. //Labels to let the user understand the app
  372. label[0] = new QLabel("Zulu time:");
  373. label[1] = new QLabel("Standard time");
  374. label[2] = new QLabel("Zulu zone:");
  375. //Just the buttons to clear and convert the time
  376. button[0] = new QPushButton("Clear");
  377. button[1] = new QPushButton("Convert");
  378. //2 inputs and 1 output
  379. for (int i = 0; i<3; i++){
  380. line[i] = new QLineEdit;
  381. }
  382. //The user is not able to write on the output line
  383. line[2]->setEnabled(false);
  384. //Here we insert the widgets on the layout
  385. layout->addWidget(label[0], 1, 0);
  386. layout->addWidget(label[2], 1, 1);
  387. layout->addWidget(line[0], 2, 0);
  388. layout->addWidget(line[1], 2, 1);
  389. layout->addWidget(button[0], 3, 0);
  390. layout->addWidget(button[1], 3, 1);
  391. layout->addWidget(label[1], 4, 0);
  392. layout->addWidget(line[2], 4, 1);
  393. //Here we connect the signals of the widgets generated
  394. //by code to their respective functions
  395. connect(method, SIGNAL(currentIndexChanged(QString)), this, SLOT(setOption(QString)));
  396. connect(button[0], SIGNAL(clicked()), this, SLOT(clearLines()));
  397. connect(button[1], SIGNAL(clicked()), this, SLOT(callOneOfTheZulus()));
  398. // Rafa 2014-09-16 - Validation will be done when button is clicked
  399. // connect(line[0], SIGNAL(textEdited(QString)), this, SLOT(Zulunormalizer1(QString)));
  400. // connect(line[1], SIGNAL(textEdited(QString)), this, SLOT(Zulunormalizer2(QString)));
  401. //Now that we have set our new window, we hide the main-window
  402. //and show the new one. The new-window has a signal that
  403. //notify when it was closed so we can shor the main-window
  404. window->setLayout(layout);
  405. showMW(false);
  406. window->show();
  407. }