Problemi con emit score
[qmemory] / mainwindow.cpp
index abad199..2bea9f2 100644 (file)
@@ -2,13 +2,17 @@
 #include "ui_mainwindow.h"
 #include <QMessageBox>
 #include <QDebug>
+#include <QSound>
+#include <QSettings>
 
 MainWindow::MainWindow(QWidget *parent) :
     QMainWindow(parent),
     ui(new Ui::MainWindow)
 {
-
     ui->setupUi(this);
+
+    bestScore=0;
+    readSettings();
     statusLed = new QLed(ui->verticalLayoutWidget);
     ui->horizontalLayout_4->insertWidget(0,statusLed);
 
@@ -22,7 +26,7 @@ MainWindow::MainWindow(QWidget *parent) :
     connect(&tread,SIGNAL(exit(int)),this,SLOT(sbagliato(int)));
     connect(&tread,SIGNAL(disableInterface()),this,SLOT(disableAll()));
     connect(&tread,SIGNAL(enableInterface()),this,SLOT(enableAll()));
-    connect(this,SIGNAL(cliccatoIlBottone(int)),&tread,SLOT(updateCaption(int)));
+    connect(this,SIGNAL(cliccatoIlBottone(int)),&tread,SLOT(addUserInput(int)));
     connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(clickA()));
     connect(ui->pushButton_2,SIGNAL(clicked()),this,SLOT(clickB()));
     connect(ui->pushButton_3,SIGNAL(clicked()),this,SLOT(clickC()));
@@ -32,6 +36,15 @@ MainWindow::MainWindow(QWidget *parent) :
     connect(ui->pushButton_7,SIGNAL(clicked()),this,SLOT(clickG()));
     connect(ui->pushButton_8,SIGNAL(clicked()),this,SLOT(clickH()));
     connect(ui->pushButton_9,SIGNAL(clicked()),this,SLOT(clickI()));
+    ui->pushButton->setShortcut(QKeySequence("1"));
+    ui->pushButton_2->setShortcut(QKeySequence("2"));
+    ui->pushButton_3->setShortcut(QKeySequence("3"));
+    ui->pushButton_4->setShortcut(QKeySequence("4"));
+    ui->pushButton_5->setShortcut(QKeySequence("5"));
+    ui->pushButton_6->setShortcut(QKeySequence("6"));
+    ui->pushButton_7->setShortcut(QKeySequence("7"));
+    ui->pushButton_8->setShortcut(QKeySequence("8"));
+    ui->pushButton_9->setShortcut(QKeySequence("9"));
 
     tread.start();
 
@@ -55,13 +68,31 @@ MainWindow::MainWindow(QWidget *parent) :
                  "padding-top: 15px;"
                  "}");
     //showMaximized();
+     setWindowTitle(tr("QMemory"));
 }
 void MainWindow::sbagliato(int score){
-    QString sscore("Score ");
+
+    if(bestScore<= score){
+        bestScore=score;
+        writeSettings();
+        QString sscore("This is Best Score ");
+        sscore.append(QString::number(score));
+        QMessageBox::warning(this, tr("End Game"),sscore,QMessageBox::Escape);
+        bestScore=0;
+        readSettings();
+        tread.genera();
+        //exit(0);
+    }
+    QString sscore("Your Score: ");
     sscore.append(QString::number(score));
+    sscore.append("\n Best score: ");
+    sscore.append(QString::number(bestScore));
     QMessageBox::warning(this, tr("End Game"),sscore,QMessageBox::Escape);
-    //tread.genera();
-    exit(0);
+    bestScore=0;
+    readSettings();
+    tread.genera();
+
+    //exit(0);
 
 }
 void MainWindow::disableAll(){
@@ -95,30 +126,48 @@ void MainWindow::cliccaBottone(int id)
 {
     switch(id){
         case 0:
+                if(ui->pushButton->getStateColor())
+                    QSound::play("sounds/dog.wav");
                 ui->pushButton->changeColor();
                 break;
         case 1:
+                if(ui->pushButton_2->getStateColor())
+                    QSound::play("sounds/cat.wav");
                 ui->pushButton_2->changeColor();
                 break;
         case 2:
+                if(ui->pushButton_3->getStateColor())
+                    QSound::play("sounds/cow.wav");
                 ui->pushButton_3->changeColor();
                 break;
         case 3:
+                if(ui->pushButton_4->getStateColor())
+                    QSound::play("sounds/duck.wav");
                 ui->pushButton_4->changeColor();
                 break;
         case 4:
+                if(ui->pushButton_5->getStateColor())
+                    QSound::play("sounds/goat.wav");
                 ui->pushButton_5->changeColor();
                 break;
         case 5:
+                if(ui->pushButton_6->getStateColor())
+                    QSound::play("sounds/donkey.wav");
                 ui->pushButton_6->changeColor();
                 break;
         case 6:
+                if(ui->pushButton_7->getStateColor())
+                    QSound::play("sounds/crow.wav");
                 ui->pushButton_7->changeColor();
                 break;
         case 7:
+                if(ui->pushButton_8->getStateColor())
+                    QSound::play("sounds/rosteer.wav");
                 ui->pushButton_8->changeColor();
                 break;
         case 8:
+                if(ui->pushButton_9->getStateColor())
+                    QSound::play("sounds/bleeeh.wav");
                 ui->pushButton_9->changeColor();
                 break;
     }
@@ -127,41 +176,59 @@ void MainWindow::cliccaBottone(int id)
 MainWindow::~MainWindow()
 {
     delete ui;
+    delete statusLed;
 }
+
 void MainWindow::clickA(){
+    QSound::play("sounds/dog.wav");
     emit cliccatoIlBottone(0);
 
 }
 
 void MainWindow::clickB(){
+    QSound::play("sounds/cat.wav");
     emit cliccatoIlBottone(1);
 
 }
+
 void MainWindow::clickC(){
+    QSound::play("sounds/cow.wav");
     emit cliccatoIlBottone(2);
 
 }
+
 void MainWindow::clickD(){
+    QSound::play("sounds/duck.wav");
     emit cliccatoIlBottone(3);
 
 }
+
 void MainWindow::clickE(){
+    QSound::play("sounds/goat.wav");
     emit cliccatoIlBottone(4);
 
 }
+
 void MainWindow::clickF(){
+    QSound::play("sounds/donkey.wav");
     emit cliccatoIlBottone(5);
 
 }
+
 void MainWindow::clickG(){
+    QSound::play("sounds/crow.wav");
     emit cliccatoIlBottone(6);
 
 }
+
 void MainWindow::clickH(){
+    QSound::play("sounds/rosteer.wav");
     emit cliccatoIlBottone(7);
 
 }
+
 void MainWindow::clickI(){
+    QSound::play("sounds/bleeeh.wav");
     emit cliccatoIlBottone(8);
 
 }
@@ -179,3 +246,17 @@ void MainWindow::changeEvent(QEvent *e)
     }
 }
 
+void MainWindow::writeSettings()
+{
+    QSettings settings("Emmynet Inc.", "QMemory");
+    settings.setValue("bestscore",bestScore);
+
+}
+
+void MainWindow::readSettings()
+{
+    QSettings settings("Emmynet Inc.", "QMemory");
+    bestScore=(settings.value("bestscore").toInt());
+
+}
+