123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505 |
- // RAN 2015-06-26 - Added Zulu Zone label
- // - Removed the onclick functions for the buttons that are no longer used
-
- #include "mainwindow.h"
- #include "ui_mainwindow.h"
- #include "secondwindow.cpp"
-
- #include <QDebug>
- #include <QTimer>
- #include <QtCore/qmath.h>
- #include <QMessageBox>
- #include "functions.h"
-
- MainWindow::MainWindow(QWidget *parent) :
- QMainWindow(parent),
- ui(new Ui::MainWindow)
- {
- ui->setupUi(this);
-
- //These are the size of the arrays of widgets 'line-edit',
- //'label' and 'button'
- buttonSize = 2;
- lineSize = 7;
- labelSize = 8;
-
- // We set every pointer member to point NULL because we
- // when we delete every pointer we dont want to delete a
- // pointer that was already deleted in other moment or
- // was never used
- layout = NULL;
- for (int i = 0; i<buttonSize ; i++){
- button[i] = 0;
- }
- for (int i = 0; i < lineSize ; i++){
- line[i] = 0;
- }
- for (int i = 0; i<labelSize ; i++){
- label[i] = 0;
- }
-
- window = new secondwindow;
-
- // We need to know whenever the second window is closed to show the
- // main window, or to hide when the second one is showed
- connect(window, SIGNAL(closeIt(bool)), this, SLOT(showMW(bool)));
-
- if(!dice1.load(":/images/resources/d1.png") || !dice2.load(":/images/resources/d1.png")){
- qDebug() << "Error1 Loading image";
- }
-
- initCheckWMaps();
- }
-
- MainWindow::~MainWindow()
- {
- delete ui;
- }
-
- void MainWindow::showMW(bool doShow){
- if (doShow){
- show();
-
- // Deleting pointers and point them to NULL
- for (int i = 0; i<buttonSize ; i++){
- delete button[i];
- button[i] = NULL;
- }
- for (int i = 0; i<lineSize ; i++){
- delete line[i];
- line[i] = NULL;
- }
- for (int i = 0; i<labelSize ; i++){
- delete label[i];
- label[i] = NULL;
- }
- delete layout;
- layout = NULL;
- delete window;
-
- // Create the new window and connecting it again with the signal
- window = new secondwindow;
- connect(window, SIGNAL(closeIt(bool)), this, SLOT(showMW(bool)));
- }
- else hide();
- }
-
-
- void MainWindow::setOption(QString str){
- option = str;
- }
-
- //Clear all the lines that are used
- void MainWindow::clearLines(){
- for (int i = 0; i < lineSize; i++){
- if (line[i] != NULL){
- line[i]->clear();
- }
- }
- score1 = score2 = 0;
- }
-
-
- void MainWindow::callOneOfTheSorts(){
- QString a = line[0]->text();
- QString b = line[1]->text();
- QString c = line[2]->text();
-
- if (!validateSorts(a,b,c)) {
- QMessageBox::warning(this, "Alert",
- "Please provide the three non-empty strings");
- return;
- }
-
- if (option == "Version Alpha"){
- mySortAlpha(a,b,c);
- }
- else if (option == "Version Beta"){
- mySortBeta(a,b,c);
- }
- else if (option == "Version Gamma"){
- mySortGamma(a,b,c);
- }
- else{
- mySortDelta(a,b,c);
- }
- line[3]->setText(a);
- line[4]->setText(b);
- line[5]->setText(c);
- }
-
-
- void MainWindow::callOneOfTheRPS(){
-
- QString p1 = this->cmbPlayer01->currentText();
- QString p2 = this->cmbPlayer02->currentText();
-
- if (p1.length() * p2.length() == 0) {
- QMessageBox::warning(this, "Alert",
- "Please provide a play for both players and the number of games to win");
- return;
- }
-
- int winnerNum;
- if (option == "Version Alpha")
- winnerNum = RPSDelta(p1.toStdString()[0], p2.toStdString()[0]);
- else if (option == "Version Beta")
- winnerNum = RPSBeta(p1.toStdString()[0], p2.toStdString()[0]);
- else if (option == "Version Gamma")
- winnerNum = RPSGamma(p1.toStdString()[0], p2.toStdString()[0]);
- else
- winnerNum = RPSDelta(p1.toStdString()[0], p2.toStdString()[0]);
-
- QString st;
-
-
- switch(winnerNum) {
- case 1: st = "Player 1"; break;
- case 2: st = "Player 2"; break;
- case 0: st = "Tie"; break;
- default: st = "Error";
- }
-
- line[0]->setText(st);
-
- }
-
-
- void MainWindow::paintDice() {
- this->timerCounter++;
- int a, b;
- a = rand()%6;
- b = rand()%6;
- label[3]->setPixmap(QPixmap::fromImage(diceImages[a]));
- label[4]->setPixmap(QPixmap::fromImage(diceImages[b]));
- if (this->timerCounter == 10) {
- aTimer->stop();
- diceFinal01 = a;
- diceFinal02 = b;
- if (option == "Version Alpha")
- diceBeta();
- else if (option == "Version Beta")
- diceGamma();
- else if (option == "Version Gamma")
- diceAlpha();
- else
- diceAlpha();
- }
- }
-
-
- void MainWindow::callOneOfTheDices(){
-
- for (int i = 1; i <= 6; i++)
- this->diceImages.push_back(QImage(":/images/resources/d" + QString::number(i) + ".png"));
-
- aTimer = new QTimer;
- aTimer->setInterval(100);
- aTimer->setSingleShot(false);
- aTimer->start();
- timerCounter = 0;
- QObject::connect(aTimer,&QTimer::timeout, this, &MainWindow::paintDice); //[&](){ birth(w, juana, avelardo, piolin);});
-
-
- }
-
-
-
-
- //========================================================
- // Functions for the dice roller
- //========================================================
-
-
- void MainWindow::diceAlpha(){
- int total = diceFinal01 + diceFinal02 + 2;
- line[0]->setText(QString::number(total));
- }
-
-
- void MainWindow::diceBeta(){
- int total = diceFinal01 + diceFinal01 + 2;
- line[0]->setText(QString::number(total));
- }
-
-
- void MainWindow::diceGamma(){
- int total = diceFinal01 - diceFinal02;
- line[0]->setText(QString::number(total));
- }
-
-
- void MainWindow::diceDelta(){
- int total = diceFinal01 + diceFinal02;
- line[0]->setText(QString::number(total));
- }
-
-
- void MainWindow::callOneOfTheChecks(){
- unsigned int qty;
- if (!validateCheckQty(line[0]->text(),qty)) {
- QMessageBox::warning(this, "Alert",
- "Enter a valid amount!");
- }
-
- if (option == "Version Alpha")
- line[1]->setText( checkWAlpha(qty) );
- else if (option == "Version Beta")
- line[1]->setText( checkWBeta(qty) );
- else if (option == "Version Gamma")
- line[1]->setText( checkWGamma(qty) );
- else
- line[1]->setText( checkWDelta(qty) );
- }
-
- void MainWindow::callOneOfTheZulus(){
- QString zuluTime = line[0]->text();
- QString zuluZone = line[1]->text();
- int hours, minutes;
-
- if (!validZuluTime(zuluTime, zuluZone, hours, minutes) ) {
-
- QMessageBox::warning(this, "Alert",
- "Either Zulu Time or Zone is not valid.");
- line[2]->setText("Error");
- return;
- }
- if (option == "Version Alpha")
- line[2]->setText( zuluAlpha(hours, minutes, zuluZone.toStdString()[0]) );
- else if (option == "Version Beta")
- line[2]->setText( zuluBeta(hours, minutes, zuluZone.toStdString()[0]) );
- else if (option == "Version Gamma")
- line[2]->setText( zuluGamma(hours, minutes, zuluZone.toStdString()[0]) );
- else
- line[2]->setText( zuluDelta(hours, minutes, zuluZone.toStdString()[0]) );
-
- }
-
-
- void MainWindow::on_SortsButton_clicked()
- {
- //Create a new QComboBox and add the items in it
- method = new QComboBox;
- method->addItem("Version Alpha");
- method->addItem("Version Beta");
- method->addItem("Version Gamma");
- method->addItem("Version Delta");
- option = "Version Alpha"; //Default option is alpha
-
- //We create a new layout and insert the comboBox to it
- layout = new QGridLayout;
- layout->addWidget(method, 0, 0, 1, -1);
-
- //The buttons needed are sort and clear so we create them
- button[0] = new QPushButton("Sort");
- button[1] = new QPushButton("Clear");
-
- //3 lines for input and 3 for output
- for (int i = 0; i<6 ; i++){
- line[i] = new QLineEdit;
- }
-
- //The user is not able to write on the output lines
- line[3]->setEnabled(false);
- line[4]->setEnabled(false);
- line[5]->setEnabled(false);
-
- //Labels to let the user understand the app
- label[0] = new QLabel("Input");
- label[1] = new QLabel("Output");
-
- //Here we insert the widgets on the layout
- layout->addWidget(label[0], 1, 0);
- layout->addWidget(label[1], 1, 2);
- layout->addWidget(line[0], 2, 0);
- layout->addWidget(line[1], 3, 0);
- layout->addWidget(line[2], 4, 0);
- layout->addWidget(button[0], 1, 1, 2, 1);
- layout->addWidget(button[1], 3, 1, 2, 1);
- layout->addWidget(line[3], 2, 2);
- layout->addWidget(line[4], 3, 2);
- layout->addWidget(line[5], 4, 2);
-
- //Here we connect the signals of the widgets generated
- //by code to their respective functions
- connect(method, SIGNAL(currentIndexChanged(QString)), this, SLOT(setOption(QString)));
- connect(button[0], SIGNAL(clicked()), this, SLOT(callOneOfTheSorts()));
- connect(button[1], SIGNAL(clicked()), this, SLOT(clearLines()));
-
- //Now that we have set our new window, we hide the main-window
- //and show the new one. The new-window has a signal that
- //notify when it was closed so we can shor the main-window
- window->setLayout(layout);
- showMW(false);
- window->show();
- }
-
- void MainWindow::on_DiceButton_clicked()
- {
- //Create a new QComboBox and add the items in it
- method = new QComboBox;
- method->addItem("Version Alpha");
- method->addItem("Version Beta");
- method->addItem("Version Gamma");
- method->addItem("Version Delta");
- option = "Version Alpha"; //Default option is alpha
-
- //We create a new layout and insert the comboBox to it
- layout = new QGridLayout;
- layout->addWidget(method, 0, 0, 1, -1);
-
- //Labels to let the user understand the app
- label[0] = new QLabel("Dice 1");
- label[1] = new QLabel("Dice 2");
- label[2] = new QLabel("Total");
-
- //The user is not able to write on the output line
- line[0] = new QLineEdit;
- line[0]->setEnabled(false);
-
- //Here we just need one button to roll the dices
- button[0] = new QPushButton("Roll them!");
-
- //Labels to put the dices' images on them
- label[3] = new QLabel;
- label[4] = new QLabel;
- label[3]->setPixmap(QPixmap::fromImage(dice1));
- label[4]->setPixmap(QPixmap::fromImage(dice2));
-
- //Here we insert the widgets on the layout
- layout->addWidget(label[0], 1, 0);
- layout->addWidget(label[3], 1, 1);
- layout->addWidget(label[1], 2, 0);
- layout->addWidget(label[4], 2, 1);
- layout->addWidget(label[2], 3, 0);
- layout->addWidget(line[0], 3, 1);
- layout->addWidget(button[0], 1, 2, 2, 1);
-
- //Here we connect the signals of the widgets generated
- //by code to their respective functions
- connect(method, SIGNAL(currentIndexChanged(QString)), this, SLOT(setOption(QString)));
- connect(button[0], SIGNAL(clicked()), this, SLOT(callOneOfTheDices()));
-
- //Now that we have set our new window, we hide the main-window
- //and show the new one. The new-window has a signal that
- //notify when it was closed so we can shor the main-window
- window->setLayout(layout);
- showMW(false);
- window->show();
- }
-
- void MainWindow::on_RPSButton_clicked()
- {
- //Create a new QComboBox and add the items in it
- method = new QComboBox;
- method->addItem("Version Alpha");
- method->addItem("Version Beta");
- method->addItem("Version Gamma");
- method->addItem("Version Delta");
- option = "Version Alpha"; //Default option is alpha
-
- cmbPlayer01 = new QComboBox;
- cmbPlayer02 = new QComboBox;
- cmbPlayer01->addItem("rock"); cmbPlayer02->addItem("rock");
- cmbPlayer01->addItem("paper"); cmbPlayer02->addItem("paper");
- cmbPlayer01->addItem("scisors"); cmbPlayer02->addItem("scisors");
-
-
- //The buttons needed here are the 'play' and 'clear' buttons
- button[0] = new QPushButton("Play");
-
- line[0] = new QLineEdit;
- //The user is not able to write on the output line
- line[0]->setEnabled(false);
-
- //Labels to let the user understand the app
- label[0] = new QLabel("Player 1's moves");
- label[1] = new QLabel("Player 2's moves");
- label[2] = new QLabel("Winner:");
-
- //We create a new layout and insert the comboBox to it
- int row = 0;
- layout = new QGridLayout;
- layout->addWidget(method, row++, 0);
- layout->addWidget(label[0], row, 0);
- layout->addWidget(cmbPlayer01, row++, 1);
- layout->addWidget(label[1], row, 0);
- layout->addWidget(cmbPlayer02, row++, 1);
- layout->addWidget(button[0], row++, 1);
- layout->addWidget(line[0], row, 1);
- layout->addWidget(label[2], row++, 0);
-
-
- //Here we connect the signals of the widgets generated
- //by code to their respective functions
- connect(method, SIGNAL(currentIndexChanged(QString)), this, SLOT(setOption(QString)));
- connect(button[0], SIGNAL(clicked()), this, SLOT(callOneOfTheRPS()));
-
- //Now that we have set our new window, we hide the main-window
- //and show the new one. The new-window has a signal that
- //notify when it was closed so we can shor the main-window
- window->setLayout(layout);
- showMW(false);
- window->show();
- }
-
-
- void MainWindow::on_ZuluButton_clicked()
- {
- //Create a new QComboBox and add the items in it
- method = new QComboBox;
- method->addItem("Version Alpha");
- method->addItem("Version Beta");
- method->addItem("Version Gamma");
- method->addItem("Version Delta");
- option = "Version Alpha"; //Default option is alpha
-
- //We create a new layout and insert the comboBox to it
- layout = new QGridLayout;
- layout->addWidget(method, 0, 0, 1, -1);
-
- //Labels to let the user understand the app
- label[0] = new QLabel("Zulu time:");
- label[1] = new QLabel("Standard time");
- label[2] = new QLabel("Zulu zone:");
-
- //Just the buttons to clear and convert the time
- button[0] = new QPushButton("Clear");
- button[1] = new QPushButton("Convert");
-
- //2 inputs and 1 output
- for (int i = 0; i<3; i++){
- line[i] = new QLineEdit;
- }
-
- //The user is not able to write on the output line
- line[2]->setEnabled(false);
-
- //Here we insert the widgets on the layout
- layout->addWidget(label[0], 1, 0);
- layout->addWidget(label[2], 1, 1);
- layout->addWidget(line[0], 2, 0);
- layout->addWidget(line[1], 2, 1);
- layout->addWidget(button[0], 3, 0);
- layout->addWidget(button[1], 3, 1);
- layout->addWidget(label[1], 4, 0);
- layout->addWidget(line[2], 4, 1);
-
- //Here we connect the signals of the widgets generated
- //by code to their respective functions
- connect(method, SIGNAL(currentIndexChanged(QString)), this, SLOT(setOption(QString)));
- connect(button[0], SIGNAL(clicked()), this, SLOT(clearLines()));
- connect(button[1], SIGNAL(clicked()), this, SLOT(callOneOfTheZulus()));
-
- // Rafa 2014-09-16 - Validation will be done when button is clicked
- // connect(line[0], SIGNAL(textEdited(QString)), this, SLOT(Zulunormalizer1(QString)));
- // connect(line[1], SIGNAL(textEdited(QString)), this, SLOT(Zulunormalizer2(QString)));
-
- //Now that we have set our new window, we hide the main-window
- //and show the new one. The new-window has a signal that
- //notify when it was closed so we can shor the main-window
- window->setLayout(layout);
- showMW(false);
- window->show();
- }
|