MaFenetre.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. restoreGeometry(option->value("MAIN/geometry").toByteArray());
  44. QWidget *centralWidget = new QWidget;
  45. chrono1 = new Chronometre(option->nomJoueur1(), Blanc, option->tempsDepart(), Placement::Gauche, this);
  46. chrono2 = new Chronometre(option->nomJoueur2(), Noir, option->tempsDepart(), Placement::Droite, this);
  47. boutonInverser = new QPushButton ("<->");
  48. boutonChangerCouleur = new QPushButton("Changer de couleur");
  49. boutonInverser->setMinimumWidth(50);
  50. boutonInverser->setMaximumWidth(50);
  51. ////////////////// Layout Boutons inversion /////////////////////////////////
  52. QVBoxLayout *layoutBoutonsInverser = new QVBoxLayout;
  53. layoutBoutonsInverser->addStretch(1);
  54. layoutBoutonsInverser->addWidget(boutonInverser, 0, Qt::AlignCenter);
  55. layoutBoutonsInverser->addWidget(boutonChangerCouleur, 0, Qt::AlignCenter);
  56. layoutBoutonsInverser->addSpacing(10);
  57. ////////////////// Groupe Définition de la classe ///////////////////////////
  58. layoutChrono = new QHBoxLayout;
  59. layoutChrono->addWidget(chrono1);
  60. layoutChrono->addLayout(layoutBoutonsInverser);
  61. layoutChrono->addWidget(chrono2);
  62. ///////////////////////////// Menus ////////////////////////////
  63. QMenu *menuFichier = menuBar()->addMenu("&Fichier");
  64. QAction *actionNouveau = new QAction ("&Nouveau" , this);
  65. QAction *actionOptions = new QAction ("&Options...", this);
  66. QAction *actionQuitter = new QAction ("&Quitter" , this);
  67. menuFichier->addAction(actionNouveau);
  68. menuFichier->addAction(actionOptions);
  69. menuFichier->addSeparator();
  70. menuFichier->addAction(actionQuitter);
  71. ///////////////////////////// Boutons /////////////////////////////
  72. boutonDemarrer = new QPushButton ("Commencer (Barre Espace)");
  73. boutonArreter = new QPushButton ("Arrêter(Touche Entrée)");
  74. boutonArreter->setFocusPolicy (Qt::NoFocus);
  75. boutonInverser->setFocusPolicy(Qt::NoFocus);
  76. boutonChangerCouleur->setFocusPolicy(Qt::NoFocus);
  77. layoutBoutonSwitch = new QHBoxLayout;
  78. layoutBoutonSwitch->addWidget (boutonDemarrer,0, Qt::AlignLeft);
  79. layoutBoutonSwitch->addWidget (boutonArreter, 0, Qt::AlignLeft);
  80. layoutBoutonSwitch->addStretch(1);
  81. ///////////////////////////// Aide /////////////////////////////////////
  82. QLabel *labelAide = new QLabel ("<b>Aide</b><br />Espace : Lancer le chrono/Donner le chrono<br />Entrée : Stopper les deux chronos");
  83. /////////////////////////////Mise en page finale ///////////////////////////
  84. layoutComplet = new QVBoxLayout;
  85. layoutComplet->addLayout (layoutChrono);
  86. layoutComplet->addLayout (layoutBoutonSwitch);
  87. layoutComplet->addWidget (labelAide);
  88. centralWidget->setLayout (layoutComplet);
  89. setCentralWidget(centralWidget);
  90. setWindowTitle ("ChronoCheckMate");
  91. ///////////////////////////// Initialisation du jeu /////////////////
  92. //lancerDialogueOptions();
  93. init();
  94. ///////////////////////////// connections ///////////////////////////
  95. QObject::connect (boutonDemarrer , &QPushButton::clicked , this , &MaFenetre::demarrer);
  96. QObject::connect (boutonInverser , &QPushButton::clicked , this , &MaFenetre::inverser);
  97. QObject::connect (boutonChangerCouleur, &QPushButton::clicked , this, &MaFenetre::changerCouleur);
  98. QObject::connect (boutonArreter , &QPushButton::clicked , this, &MaFenetre::arreter);
  99. QObject::connect (actionNouveau, &QAction::triggered , this , &MaFenetre::init);
  100. QObject::connect (actionOptions , &QAction::triggered , this, &MaFenetre::lancerDialogueOptions);
  101. QObject::connect (actionQuitter , &QAction::triggered , qApp , &QApplication::quit);
  102. QObject::connect (chrono1, &Chronometre::fin, this, &MaFenetre::afficheGagnant);
  103. QObject::connect (chrono2, &Chronometre::fin, this, &MaFenetre::afficheGagnant);
  104. }
  105. ///////////////// Méthodes ////////////////////
  106. void MaFenetre::etatBoutons (bool etatBoutonStart, bool etatBoutonStop, bool etatBoutonInverser) {
  107. boutonDemarrer->setEnabled (etatBoutonStart);
  108. boutonArreter->setEnabled (etatBoutonStop);
  109. boutonInverser->setEnabled(etatBoutonInverser);
  110. boutonChangerCouleur->setEnabled(etatBoutonInverser);
  111. }
  112. ////////////////// EVENT ////////////////////////
  113. void MaFenetre::closeEvent(QCloseEvent *event)
  114. {
  115. option->setValue("MAIN/geometry", saveGeometry());
  116. QMainWindow::closeEvent(event);
  117. }
  118. ////////////////// SLOTS ////////////////////////
  119. void MaFenetre::inverser() {
  120. init();
  121. QString temp;
  122. temp = option->nomJoueur1();
  123. option->setNomJoueur1(option->nomJoueur2());
  124. option->setNomJoueur2(temp);
  125. chrono1->modifierNomJoueur(option->nomJoueur1());
  126. chrono2->modifierNomJoueur(option->nomJoueur2());
  127. }
  128. void MaFenetre::changerCouleur() {
  129. init();
  130. chrono1->echangerCouleur(*chrono2);
  131. }
  132. void MaFenetre::demarrer () {
  133. if (marche == false ) {
  134. chrono1->reinit ();
  135. chrono2->reinit ();
  136. if (chrono1->getCouleur() == Blanc) {
  137. chrono1->start();
  138. } else if (chrono2->getCouleur() == Blanc) {
  139. chrono2->start();
  140. }
  141. marche = true;
  142. etatBoutons (false , true, false);
  143. }
  144. }
  145. void MaFenetre::switcher () {
  146. if (marche == true) {
  147. chrono1->basculer (*chrono2);
  148. }
  149. }
  150. void MaFenetre::arreter () {
  151. chrono1->stop();
  152. chrono2->stop();
  153. marche = false;
  154. etatBoutons (false , false, true);
  155. }
  156. void MaFenetre::afficheGagnant() {
  157. arreter();
  158. if (option->son() == true)
  159. QSound::play(":/ressources/sonnette.wav");
  160. QMessageBox message;
  161. QString texte;
  162. texte = "Le joueur " + (*chrono1+=*chrono2) + " a gagné au temps.";
  163. message.setText(texte);
  164. message.setStandardButtons(QMessageBox::Ok);
  165. message.exec();
  166. }
  167. void MaFenetre::init () {
  168. chrono1->reinit();
  169. chrono2->reinit();
  170. marche = false;
  171. etatBoutons (true , false, true);
  172. }
  173. void MaFenetre::lancerDialogueOptions() {
  174. Dialog dialog (*option);
  175. if (dialog.exec() == Dialog::Accepted) {
  176. option->setNomJoueur1(dialog.champs_joueur1());
  177. option->setNomJoueur2(dialog.champs_joueur2());
  178. option->setTempsDepart(dialog.champs_temps());
  179. option->setSon(dialog.champs_son());
  180. option->sauverConf();
  181. chrono1->modifierNomJoueur(option->nomJoueur1());
  182. chrono1->definirTemps(option->tempsDepart());
  183. chrono2->modifierNomJoueur(option->nomJoueur2());
  184. chrono2->definirTemps(option->tempsDepart());
  185. if (marche==false) {
  186. init();
  187. }
  188. }
  189. }
  190. void MaFenetre::keyReleaseEvent (QKeyEvent * evenement) {
  191. if (evenement->key() == Qt::Key_Space) {
  192. if (marche) {
  193. switcher();
  194. }
  195. if ( !marche && boutonDemarrer->isEnabled()) {
  196. demarrer();
  197. } else if ( !marche && !boutonDemarrer->isEnabled()) {
  198. init();
  199. }
  200. }
  201. if (evenement->key() == Qt::Key_Return) {
  202. arreter();
  203. }
  204. }