Best Score
[qmemory] / mainwindow.cpp
1 #include "mainwindow.h"
2 #include "ui_mainwindow.h"
3 #include <QMessageBox>
4 #include <QDebug>
5 #include <QSound>
6 #include <QSettings>
7
8 MainWindow::MainWindow(QWidget *parent) :
9     QMainWindow(parent),
10     ui(new Ui::MainWindow)
11 {
12     bestScore=0;
13     readSettings();
14     ui->setupUi(this);
15     statusLed = new QLed(ui->verticalLayoutWidget);
16     ui->horizontalLayout_4->insertWidget(0,statusLed);
17
18     statusLed->setOnColor(QLed::Red);
19     statusLed->setOffColor(QLed::Green);
20     //statusLed->setGeometry(QRect(0,0,100,100));
21     statusLed->setShape(QLed::Rounded);
22     setCentralWidget(ui->verticalLayoutWidget);
23     connect(&tread,SIGNAL(generate(int)),this,SLOT(cliccaBottone(int)));
24     connect(&tread,SIGNAL(score(int)),ui->lcdNumber,SLOT(display(int)));
25     connect(&tread,SIGNAL(exit(int)),this,SLOT(sbagliato(int)));
26     connect(&tread,SIGNAL(disableInterface()),this,SLOT(disableAll()));
27     connect(&tread,SIGNAL(enableInterface()),this,SLOT(enableAll()));
28     connect(this,SIGNAL(cliccatoIlBottone(int)),&tread,SLOT(updateCaption(int)));
29     connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(clickA()));
30     connect(ui->pushButton_2,SIGNAL(clicked()),this,SLOT(clickB()));
31     connect(ui->pushButton_3,SIGNAL(clicked()),this,SLOT(clickC()));
32     connect(ui->pushButton_4,SIGNAL(clicked()),this,SLOT(clickD()));
33     connect(ui->pushButton_5,SIGNAL(clicked()),this,SLOT(clickE()));
34     connect(ui->pushButton_6,SIGNAL(clicked()),this,SLOT(clickF()));
35     connect(ui->pushButton_7,SIGNAL(clicked()),this,SLOT(clickG()));
36     connect(ui->pushButton_8,SIGNAL(clicked()),this,SLOT(clickH()));
37     connect(ui->pushButton_9,SIGNAL(clicked()),this,SLOT(clickI()));
38
39     tread.start();
40
41     setStyleSheet("CrazyButton {"
42                  "color: white;"
43                  "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);"
44                  "border-width: 1px;"
45                  "border-color: #339;"
46                  "border-style: solid;"
47                  "border-radius: 7;"
48                  "padding: 3px;"
49                  "font-size: 50px;"
50                  "padding-left: 5px;"
51                  "padding-right: 5px;"
52                  "min-width: 15px;"
53                  "max-width: 300px;"
54                  "min-height: 11px;"
55                  "max-height: 300px;"
56                  "}"
57                  "QPushButton:pressed {"
58                  "padding-top: 15px;"
59                  "}");
60     //showMaximized();
61 }
62 void MainWindow::sbagliato(int score){
63
64     if(bestScore<= score){
65         bestScore=score;
66         writeSettings();
67         QString sscore("This is Best Score ");
68         sscore.append(QString::number(score));
69         QMessageBox::warning(this, tr("End Game"),sscore,QMessageBox::Escape);
70         //tread.genera();
71         exit(0);
72     }
73     QString sscore("Your Score: ");
74     sscore.append(QString::number(score));
75     sscore.append("\n Best score: ");
76     sscore.append(QString::number(bestScore));
77     QMessageBox::warning(this, tr("End Game"),sscore,QMessageBox::Escape);
78     //tread.genera();
79     exit(0);
80
81 }
82 void MainWindow::disableAll(){
83     ui->pushButton->setEnabled(false);
84     ui->pushButton_2->setEnabled(false);
85     ui->pushButton_3->setEnabled(false);
86     ui->pushButton_4->setEnabled(false);
87     ui->pushButton_5->setEnabled(false);
88     ui->pushButton_6->setEnabled(false);
89     ui->pushButton_7->setEnabled(false);
90     ui->pushButton_8->setEnabled(false);
91     ui->pushButton_9->setEnabled(false);
92     statusLed->setValue(true);
93 }
94
95 void MainWindow::enableAll(){
96     ui->pushButton->setEnabled(true);
97     ui->pushButton_2->setEnabled(true);
98     ui->pushButton_3->setEnabled(true);
99     ui->pushButton_4->setEnabled(true);
100     ui->pushButton_5->setEnabled(true);
101     ui->pushButton_6->setEnabled(true);
102     ui->pushButton_7->setEnabled(true);
103     ui->pushButton_8->setEnabled(true);
104     ui->pushButton_9->setEnabled(true);
105     statusLed->setValue(false);
106 }
107
108
109 void MainWindow::cliccaBottone(int id)
110 {
111     switch(id){
112         case 0:
113                 if(ui->pushButton->getStateColor())
114                     QSound::play("sounds/dog.wav");
115                 ui->pushButton->changeColor();
116                 break;
117         case 1:
118                 if(ui->pushButton_2->getStateColor())
119                     QSound::play("sounds/cat.wav");
120                 ui->pushButton_2->changeColor();
121                 break;
122         case 2:
123                 if(ui->pushButton_3->getStateColor())
124                     QSound::play("sounds/cow.wav");
125                 ui->pushButton_3->changeColor();
126                 break;
127         case 3:
128                 if(ui->pushButton_4->getStateColor())
129                     QSound::play("sounds/duck.wav");
130                 ui->pushButton_4->changeColor();
131                 break;
132         case 4:
133                 if(ui->pushButton_5->getStateColor())
134                     QSound::play("sounds/goat.wav");
135                 ui->pushButton_5->changeColor();
136                 break;
137         case 5:
138                 if(ui->pushButton_6->getStateColor())
139                     QSound::play("sounds/donkey.wav");
140                 ui->pushButton_6->changeColor();
141                 break;
142         case 6:
143                 if(ui->pushButton_7->getStateColor())
144                     QSound::play("sounds/crow.wav");
145                 ui->pushButton_7->changeColor();
146                 break;
147         case 7:
148                 if(ui->pushButton_8->getStateColor())
149                     QSound::play("sounds/rosteer.wav");
150                 ui->pushButton_8->changeColor();
151                 break;
152         case 8:
153                 if(ui->pushButton_9->getStateColor())
154                     QSound::play("sounds/bleeeh.wav");
155                 ui->pushButton_9->changeColor();
156                 break;
157     }
158 }
159
160 MainWindow::~MainWindow()
161 {
162     delete ui;
163 }
164 void MainWindow::clickA(){
165     QSound::play("sounds/dog.wav");
166     emit cliccatoIlBottone(0);
167
168 }
169
170 void MainWindow::clickB(){
171     QSound::play("sounds/cat.wav");
172     emit cliccatoIlBottone(1);
173
174 }
175 void MainWindow::clickC(){
176     QSound::play("sounds/cow.wav");
177     emit cliccatoIlBottone(2);
178
179 }
180 void MainWindow::clickD(){
181     QSound::play("sounds/duck.wav");
182     emit cliccatoIlBottone(3);
183
184 }
185 void MainWindow::clickE(){
186     QSound::play("sounds/goat.wav");
187     emit cliccatoIlBottone(4);
188
189 }
190 void MainWindow::clickF(){
191     QSound::play("sounds/donkey.wav");
192     emit cliccatoIlBottone(5);
193
194 }
195 void MainWindow::clickG(){
196     QSound::play("sounds/crow.wav");
197     emit cliccatoIlBottone(6);
198
199 }
200 void MainWindow::clickH(){
201     QSound::play("sounds/rosteer.wav");
202     emit cliccatoIlBottone(7);
203
204 }
205 void MainWindow::clickI(){
206     QSound::play("sounds/bleeeh.wav");
207     emit cliccatoIlBottone(8);
208
209 }
210
211
212 void MainWindow::changeEvent(QEvent *e)
213 {
214     QMainWindow::changeEvent(e);
215     switch (e->type()) {
216     case QEvent::LanguageChange:
217         ui->retranslateUi(this);
218         break;
219     default:
220         break;
221     }
222 }
223 void MainWindow::writeSettings()
224 {
225     QSettings settings("Emmynet Inc.", "QMemory");
226     settings.setValue("bestscore",bestScore);
227
228 }
229
230 void MainWindow::readSettings()
231 {
232     QSettings settings("Emmynet Inc.", "QMemory");
233     bestScore=(settings.value("bestscore").toInt());
234
235 }
236