MaFenetre.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * MaFenetre.cpp
  3. * This file is part of ChronoCheckMate
  4. *
  5. * Copyright (C) 2010 - François Drouhard
  6. *
  7. * ChronoCheckMate is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * ChronoCheckMate is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with ChronoCheckMate; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor,
  20. * Boston, MA 02110-1301 USA
  21. */
  22. #include "MaFenetre.h"
  23. #include "Chronometre.h"
  24. #include "options.h"
  25. #include "Dialog.h"
  26. #include <QMenuBar>
  27. #include <QHBoxLayout>
  28. #include <QVBoxLayout>
  29. #include <QPushButton>
  30. #include <QKeyEvent>
  31. #include <QApplication>
  32. #include <QLabel>
  33. #include <QDialog>
  34. #include <QLineEdit>
  35. #include <QDialogButtonBox>
  36. #include <QMessageBox>
  37. #include <QSound>
  38. MaFenetre::MaFenetre() :
  39. QMainWindow (),
  40. marche(false),
  41. option(new Options(this))
  42. {
  43. QWidget *centralWidget = new QWidget;
  44. chrono1 = new Chronometre(option->nomJoueur1(), Blanc, option->tempsDepart(), this);
  45. chrono2 = new Chronometre(option->nomJoueur2(), Noir, option->tempsDepart(), this);
  46. boutonInverser = new QPushButton ("<->");
  47. boutonChangerCouleur = new QPushButton("Changer de couleur");
  48. boutonInverser->setMinimumWidth(50);
  49. boutonInverser->setMaximumWidth(50);
  50. ////////////////// Layout Boutons inversion /////////////////////////////////
  51. QVBoxLayout *layoutBoutonsInverser = new QVBoxLayout;
  52. layoutBoutonsInverser->addStretch(1);
  53. layoutBoutonsInverser->addWidget(boutonInverser, 0, Qt::AlignCenter);
  54. layoutBoutonsInverser->addWidget(boutonChangerCouleur, 0, Qt::AlignCenter);
  55. layoutBoutonsInverser->addSpacing(10);
  56. ////////////////// Groupe Définition de la classe ///////////////////////////
  57. layoutChrono = new QHBoxLayout;
  58. layoutChrono->addWidget(chrono1);
  59. layoutChrono->addLayout(layoutBoutonsInverser);
  60. layoutChrono->addWidget(chrono2);
  61. ///////////////////////////// Menus ////////////////////////////
  62. QMenu *menuFichier = menuBar()->addMenu("&Fichier");
  63. QAction *actionNouveau = new QAction ("&Nouveau" , this);
  64. QAction *actionOptions = new QAction ("&Options...", this);
  65. QAction *actionQuitter = new QAction ("&Quitter" , this);
  66. menuFichier->addAction(actionNouveau);
  67. menuFichier->addAction(actionOptions);
  68. menuFichier->addSeparator();
  69. menuFichier->addAction(actionQuitter);
  70. ///////////////////////////// Boutons /////////////////////////////
  71. boutonDemarrer = new QPushButton ("Commencer (Barre Espace)");
  72. boutonArreter = new QPushButton ("Arrêter(Touche Entrée)");
  73. boutonArreter->setFocusPolicy (Qt::NoFocus);
  74. boutonInverser->setFocusPolicy(Qt::NoFocus);
  75. boutonChangerCouleur->setFocusPolicy(Qt::NoFocus);
  76. layoutBoutonSwitch = new QHBoxLayout;
  77. layoutBoutonSwitch->addWidget (boutonDemarrer,0, Qt::AlignLeft);
  78. layoutBoutonSwitch->addWidget (boutonArreter, 0, Qt::AlignLeft);
  79. layoutBoutonSwitch->addStretch(1);
  80. ///////////////////////////// Aide /////////////////////////////////////
  81. QLabel *labelAide = new QLabel ("<b>Aide</b><br />Espace : Lancer le chrono/Donner le chrono<br />Entrée : Stopper les deux chronos");
  82. /////////////////////////////Mise en page finale ///////////////////////////
  83. layoutComplet = new QVBoxLayout;
  84. layoutComplet->addLayout (layoutChrono);
  85. layoutComplet->addLayout (layoutBoutonSwitch);
  86. layoutComplet->addWidget (labelAide);
  87. centralWidget->setLayout (layoutComplet);
  88. setCentralWidget(centralWidget);
  89. setWindowTitle ("ChronoCheckMate");
  90. ///////////////////////////// Initialisation du jeu /////////////////
  91. //lancerDialogueOptions();
  92. init();
  93. ///////////////////////////// connections ///////////////////////////
  94. QObject::connect (boutonDemarrer , SIGNAL(clicked () ) , this , SLOT(demarrer () ));
  95. QObject::connect (boutonInverser , SIGNAL(clicked()) , this , SLOT(inverser()));
  96. QObject::connect (boutonChangerCouleur, SIGNAL(clicked() ), this, SLOT(changerCouleur()));
  97. QObject::connect (boutonArreter , SIGNAL(clicked()), this, SLOT (arreter()));
  98. QObject::connect (actionNouveau, SIGNAL(triggered()) , this , SLOT(init()));
  99. QObject::connect (actionOptions , SIGNAL(triggered()), this, SLOT(lancerDialogueOptions()));
  100. QObject::connect (actionQuitter , SIGNAL(triggered()) , qApp , SLOT(quit()));
  101. QObject::connect (chrono1, SIGNAL(fin()), this, SLOT(afficheGagnant()));
  102. QObject::connect (chrono2, SIGNAL(fin()), this, SLOT(afficheGagnant()));
  103. }
  104. ///////////////// Méthodes ////////////////////
  105. void MaFenetre::etatBoutons (bool etatBoutonStart, bool etatBoutonStop, bool etatBoutonInverser) {
  106. boutonDemarrer->setEnabled (etatBoutonStart);
  107. boutonArreter->setEnabled (etatBoutonStop);
  108. boutonInverser->setEnabled(etatBoutonInverser);
  109. boutonChangerCouleur->setEnabled(etatBoutonInverser);
  110. }
  111. ////////////////// SLOTS ////////////////////////
  112. void MaFenetre::inverser() {
  113. init();
  114. QString temp;
  115. temp = option->nomJoueur1();
  116. option->setNomJoueur1(option->nomJoueur2());
  117. option->setNomJoueur2(temp);
  118. chrono1->modifierNomJoueur(option->nomJoueur1());
  119. chrono2->modifierNomJoueur(option->nomJoueur2());
  120. }
  121. void MaFenetre::changerCouleur() {
  122. init();
  123. chrono1->echangerCouleur(*chrono2);
  124. }
  125. void MaFenetre::demarrer () {
  126. if (marche == false ) {
  127. chrono1->reinit ();
  128. chrono2->reinit ();
  129. if (chrono1->getCouleur() == Blanc) {
  130. chrono1->start();
  131. } else if (chrono2->getCouleur() == Blanc) {
  132. chrono2->start();
  133. }
  134. marche = true;
  135. etatBoutons (false , true, false);
  136. }
  137. }
  138. void MaFenetre::switcher () {
  139. if (marche == true) {
  140. chrono1->basculer (*chrono2);
  141. }
  142. }
  143. void MaFenetre::arreter () {
  144. chrono1->stop();
  145. chrono2->stop();
  146. marche = false;
  147. etatBoutons (false , false, true);
  148. }
  149. void MaFenetre::afficheGagnant() {
  150. arreter();
  151. if (option->son() == true)
  152. QSound::play(":/ressources/sonnette.wav");
  153. QMessageBox message;
  154. QString texte;
  155. texte = "Le joueur " + (*chrono1+=*chrono2) + " a gagné au temps.";
  156. message.setText(texte);
  157. message.setStandardButtons(QMessageBox::Ok);
  158. message.exec();
  159. }
  160. void MaFenetre::init () {
  161. chrono1->reinit();
  162. chrono2->reinit();
  163. marche = false;
  164. etatBoutons (true , false, true);
  165. }
  166. void MaFenetre::lancerDialogueOptions() {
  167. Dialog dialog (*option);
  168. if (dialog.exec() == Dialog::Accepted) {
  169. option->setNomJoueur1(dialog.champs_joueur1());
  170. option->setNomJoueur2(dialog.champs_joueur2());
  171. option->setTempsDepart(dialog.champs_temps());
  172. option->setSon(dialog.champs_son());
  173. option->sauverConf();
  174. chrono1->modifierNomJoueur(option->nomJoueur1());
  175. chrono1->definirTemps(option->tempsDepart());
  176. chrono2->modifierNomJoueur(option->nomJoueur2());
  177. chrono2->definirTemps(option->tempsDepart());
  178. if (marche==false) {
  179. init();
  180. }
  181. }
  182. }
  183. void MaFenetre::keyReleaseEvent (QKeyEvent * evenement) {
  184. if (evenement->key() == Qt::Key_Space) {
  185. if (marche == true) {
  186. switcher();
  187. }
  188. if (marche == false) {
  189. demarrer();
  190. }
  191. }
  192. if (evenement->key() == Qt::Key_Return) {
  193. arreter();
  194. }
  195. }