Problemi con emit score
[qmemory] / mainwindow.cpp
index 178e2a1..2bea9f2 100644 (file)
@@ -2,18 +2,31 @@
 #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);
+
+    statusLed->setOnColor(QLed::Red);
+    statusLed->setOffColor(QLed::Green);
+    //statusLed->setGeometry(QRect(0,0,100,100));
+    statusLed->setShape(QLed::Rounded);
+    setCentralWidget(ui->verticalLayoutWidget);
     connect(&tread,SIGNAL(generate(int)),this,SLOT(cliccaBottone(int)));
-    connect(&tread,SIGNAL(score(int)),this,SLOT(sbagliato(int)));
+    connect(&tread,SIGNAL(score(int)),ui->lcdNumber,SLOT(display(int)));
+    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()));
@@ -23,10 +36,19 @@ 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();
 
-    setStyleSheet("QPushButton {"
+    setStyleSheet("CrazyButton {"
                  "color: white;"
                  "background-color: QLinearGradient( x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #88d, stop: 0.1 #99e, stop: 0.49 #77c, stop: 0.5 #66b, stop: 1 #77c);"
                  "border-width: 1px;"
@@ -37,22 +59,40 @@ MainWindow::MainWindow(QWidget *parent) :
                  "font-size: 50px;"
                  "padding-left: 5px;"
                  "padding-right: 5px;"
-                 "min-width: 150px;"
-                 "max-width: 150px;"
-                 "min-height: 113px;"
-                 "max-height: 113px;"
+                 "min-width: 15px;"
+                 "max-width: 300px;"
+                 "min-height: 11px;"
+                 "max-height: 300px;"
                  "}"
                  "QPushButton:pressed {"
                  "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));
-    QMessageBox::warning(this, tr("Sbagliato"),sscore,QMessageBox::Escape);
-    //tread.genera();
-    quick_exit(0);
+    sscore.append("\n Best score: ");
+    sscore.append(QString::number(bestScore));
+    QMessageBox::warning(this, tr("End Game"),sscore,QMessageBox::Escape);
+    bestScore=0;
+    readSettings();
+    tread.genera();
+
+    //exit(0);
 
 }
 void MainWindow::disableAll(){
@@ -65,6 +105,7 @@ void MainWindow::disableAll(){
     ui->pushButton_7->setEnabled(false);
     ui->pushButton_8->setEnabled(false);
     ui->pushButton_9->setEnabled(false);
+    statusLed->setValue(true);
 }
 
 void MainWindow::enableAll(){
@@ -77,6 +118,7 @@ void MainWindow::enableAll(){
     ui->pushButton_7->setEnabled(true);
     ui->pushButton_8->setEnabled(true);
     ui->pushButton_9->setEnabled(true);
+    statusLed->setValue(false);
 }
 
 
@@ -84,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;
     }
@@ -116,43 +176,61 @@ void MainWindow::cliccaBottone(int id)
 MainWindow::~MainWindow()
 {
     delete ui;
+    delete statusLed;
 }
+
 void MainWindow::clickA(){
+    QSound::play("sounds/dog.wav");
     emit cliccatoIlBottone(0);
-    qDebug()<< "Click 1";
+
 }
 
 void MainWindow::clickB(){
+    QSound::play("sounds/cat.wav");
     emit cliccatoIlBottone(1);
-    qDebug()<< "Click 2";
+
 }
+
 void MainWindow::clickC(){
+    QSound::play("sounds/cow.wav");
     emit cliccatoIlBottone(2);
-    qDebug()<< "Click 3";
+
 }
+
 void MainWindow::clickD(){
+    QSound::play("sounds/duck.wav");
     emit cliccatoIlBottone(3);
-    qDebug()<< "Click 4";
+
 }
+
 void MainWindow::clickE(){
+    QSound::play("sounds/goat.wav");
     emit cliccatoIlBottone(4);
-    qDebug()<< "Click 5";
+
 }
+
 void MainWindow::clickF(){
+    QSound::play("sounds/donkey.wav");
     emit cliccatoIlBottone(5);
-    qDebug()<< "Click 6";
+
 }
+
 void MainWindow::clickG(){
+    QSound::play("sounds/crow.wav");
     emit cliccatoIlBottone(6);
-    qDebug()<< "Click 7";
+
 }
+
 void MainWindow::clickH(){
+    QSound::play("sounds/rosteer.wav");
     emit cliccatoIlBottone(7);
-    qDebug()<< "Click 8";
+
 }
+
 void MainWindow::clickI(){
+    QSound::play("sounds/bleeeh.wav");
     emit cliccatoIlBottone(8);
-    qDebug()<< "Click 9";
+
 }
 
 
@@ -168,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());
+
+}
+