MaFenetre.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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(Options* mesOptions) :
  39. QMainWindow (),
  40. marche(false),
  41. option(mesOptions)
  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 ();
  141. chrono2->basculer ();
  142. }
  143. }
  144. void MaFenetre::arreter () {
  145. chrono1->stop();
  146. chrono2->stop();
  147. marche = false;
  148. etatBoutons (false , false, true);
  149. }
  150. void MaFenetre::afficheGagnant() {
  151. arreter();
  152. if (option->son() == true)
  153. QSound::play(":/ressources/sonnette.wav");
  154. QMessageBox message;
  155. QString texte;
  156. texte = "Le joueur " + (*chrono1+=*chrono2) + " a gagné au temps.";
  157. message.setText(texte);
  158. message.setStandardButtons(QMessageBox::Ok);
  159. message.exec();
  160. }
  161. void MaFenetre::init () {
  162. chrono1->reinit();
  163. chrono2->reinit();
  164. marche = false;
  165. etatBoutons (true , false, true);
  166. }
  167. void MaFenetre::lancerDialogueOptions() {
  168. Dialog dialog (option);
  169. if (dialog.exec()) {
  170. chrono1->modifierNomJoueur(option->nomJoueur1());
  171. chrono1->definirTemps(option->tempsDepart());
  172. chrono2->modifierNomJoueur(option->nomJoueur2());
  173. chrono2->definirTemps(option->tempsDepart());
  174. if (marche==false) {
  175. init();
  176. }
  177. }
  178. }
  179. void MaFenetre::keyReleaseEvent (QKeyEvent * evenement) {
  180. if (evenement->key() == Qt::Key_Space) {
  181. if (marche == true) {
  182. switcher();
  183. }
  184. if (marche == false) {
  185. demarrer();
  186. }
  187. }
  188. if (evenement->key() == Qt::Key_Return) {
  189. arreter();
  190. }
  191. }