MaFenetre.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. labelOptions = new QLabel;
  84. refreshLabel();
  85. QHBoxLayout *layoutAide = new QHBoxLayout;
  86. layoutAide->addWidget(labelAide);
  87. layoutAide->addWidget(labelOptions);
  88. /////////////////////////////Mise en page finale ///////////////////////////
  89. layoutComplet = new QVBoxLayout;
  90. layoutComplet->addLayout (layoutChrono);
  91. layoutComplet->addLayout (layoutBoutonSwitch);
  92. layoutComplet->addLayout (layoutAide);
  93. centralWidget->setLayout (layoutComplet);
  94. setCentralWidget(centralWidget);
  95. setWindowTitle ("ChronoCheckMate");
  96. ///////////////////////////// Initialisation du jeu /////////////////
  97. //lancerDialogueOptions();
  98. init();
  99. ///////////////////////////// connections ///////////////////////////
  100. QObject::connect (boutonDemarrer , &QPushButton::clicked , this , &MaFenetre::demarrer);
  101. QObject::connect (boutonInverser , &QPushButton::clicked , this , &MaFenetre::inverser);
  102. QObject::connect (boutonChangerCouleur, &QPushButton::clicked , this, &MaFenetre::changerCouleur);
  103. QObject::connect (boutonArreter , &QPushButton::clicked , this, &MaFenetre::arreter);
  104. QObject::connect (actionNouveau, &QAction::triggered , this , &MaFenetre::init);
  105. QObject::connect (actionOptions , &QAction::triggered , this, &MaFenetre::lancerDialogueOptions);
  106. QObject::connect (actionQuitter , &QAction::triggered , qApp , &QApplication::quit);
  107. QObject::connect (chrono1, &Chronometre::fin, this, &MaFenetre::afficheGagnant);
  108. QObject::connect (chrono2, &Chronometre::fin, this, &MaFenetre::afficheGagnant);
  109. QObject::connect (chrono1, &Chronometre::is_paused, this, &MaFenetre::ajouterTempsJoueur);
  110. QObject::connect (chrono2, &Chronometre::is_paused, this, &MaFenetre::ajouterTempsJoueur);
  111. }
  112. ///////////////// Méthodes ////////////////////
  113. void MaFenetre::etatBoutons (bool etatBoutonStart, bool etatBoutonStop, bool etatBoutonInverser) {
  114. boutonDemarrer->setEnabled (etatBoutonStart);
  115. boutonArreter->setEnabled (etatBoutonStop);
  116. boutonInverser->setEnabled(etatBoutonInverser);
  117. boutonChangerCouleur->setEnabled(etatBoutonInverser);
  118. }
  119. void MaFenetre::refreshLabel()
  120. {
  121. labelOptions->setText("<b>Options</b><br />Temps : "
  122. + option->tempsDepart().toString("HH:mm:ss") + "<br />"
  123. + (option->isTempsSupplementaire() ? " + " + option->tempsSupplementaire().toString("ss") + " secondes" : ""));
  124. }
  125. ////////////////// EVENT ////////////////////////
  126. void MaFenetre::closeEvent(QCloseEvent *event)
  127. {
  128. option->setValue("MAIN/geometry", saveGeometry());
  129. QMainWindow::closeEvent(event);
  130. }
  131. ////////////////// SLOTS ////////////////////////
  132. void MaFenetre::inverser() {
  133. init();
  134. QString temp;
  135. temp = option->nomJoueur1();
  136. option->setNomJoueur1(option->nomJoueur2());
  137. option->setNomJoueur2(temp);
  138. chrono1->modifierNomJoueur(option->nomJoueur1());
  139. chrono2->modifierNomJoueur(option->nomJoueur2());
  140. }
  141. void MaFenetre::changerCouleur() {
  142. init();
  143. chrono1->echangerCouleur(*chrono2);
  144. }
  145. void MaFenetre::demarrer () {
  146. if (marche == false ) {
  147. chrono1->reinit ();
  148. chrono2->reinit ();
  149. if (chrono1->getCouleur() == Blanc) {
  150. chrono1->start();
  151. } else if (chrono2->getCouleur() == Blanc) {
  152. chrono2->start();
  153. }
  154. marche = true;
  155. etatBoutons (false , true, false);
  156. }
  157. }
  158. void MaFenetre::switcher () {
  159. if (marche == true) {
  160. chrono1->basculer (*chrono2);
  161. }
  162. }
  163. void MaFenetre::arreter () {
  164. chrono1->stop();
  165. chrono2->stop();
  166. marche = false;
  167. etatBoutons (false , false, true);
  168. }
  169. void MaFenetre::afficheGagnant() {
  170. arreter();
  171. if (option->son() == true)
  172. QSound::play(":/ressources/sonnette.wav");
  173. QMessageBox message;
  174. QString texte;
  175. texte = "Le joueur " + (*chrono1+=*chrono2) + " a gagné au temps.";
  176. message.setText(texte);
  177. message.setStandardButtons(QMessageBox::Ok);
  178. message.exec();
  179. }
  180. void MaFenetre::init () {
  181. chrono1->reinit();
  182. chrono2->reinit();
  183. marche = false;
  184. etatBoutons (true , false, true);
  185. }
  186. void MaFenetre::lancerDialogueOptions() {
  187. Dialog dialog (*option);
  188. if (dialog.exec() == Dialog::Accepted) {
  189. option->setNomJoueur1(dialog.champs_joueur1());
  190. option->setNomJoueur2(dialog.champs_joueur2());
  191. option->setTempsDepart(dialog.champs_temps());
  192. option->setSon(dialog.champs_son());
  193. option->setTempsSupplementaire(dialog.champs_temps_Supplementaire());
  194. option->setTempsSupplementaire(dialog.champs_is_temps_supplementaire());
  195. option->sauverConf();
  196. refreshLabel();
  197. chrono1->modifierNomJoueur(option->nomJoueur1());
  198. chrono1->definirTemps(option->tempsDepart());
  199. chrono2->modifierNomJoueur(option->nomJoueur2());
  200. chrono2->definirTemps(option->tempsDepart());
  201. if (marche==false) {
  202. init();
  203. }
  204. }
  205. }
  206. void MaFenetre::keyReleaseEvent (QKeyEvent * evenement) {
  207. if (evenement->key() == Qt::Key_Space) {
  208. if (marche) {
  209. switcher();
  210. }
  211. if ( !marche && boutonDemarrer->isEnabled()) {
  212. demarrer();
  213. } else if ( !marche && !boutonDemarrer->isEnabled()) {
  214. init();
  215. }
  216. }
  217. if (evenement->key() == Qt::Key_Return) {
  218. arreter();
  219. }
  220. }
  221. void MaFenetre::ajouterTempsJoueur(Chronometre * chrono)
  222. {
  223. if (option->isTempsSupplementaire())
  224. {
  225. chrono->addTime(option->tempsSupplementaire());
  226. chrono->affichage();
  227. }
  228. }