MaFenetre.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 <QHBoxLayout>
  25. #include <QVBoxLayout>
  26. #include <QPushButton>
  27. #include <QKeyEvent>
  28. #include <QApplication>
  29. #include <QLabel>
  30. MaFenetre::MaFenetre() : QWidget () {
  31. marche = false;
  32. chrono1 = new Chronometre("Joueur 1" , this);
  33. chrono2 = new Chronometre("Joueur 2" , this);
  34. ////////////////// Groupe Définition de la classe ///////////////////////////
  35. layoutChrono = new QHBoxLayout;
  36. layoutChrono->addWidget(chrono1);
  37. layoutChrono->addWidget(chrono2);
  38. /*groupChrono = new QGroupBox ("Chronomètre d'échecs");
  39. groupChrono->setLayout (layoutChrono);*/
  40. ///////////////////////////// Boutons /////////////////////////////
  41. boutonDemarrer = new QPushButton ("Commencer");
  42. boutonSwitch = new QPushButton ("Changer");
  43. boutonArreter = new QPushButton ("Arrêter");
  44. boutonInit = new QPushButton ("Remettre à Zéro");
  45. boutonArreter->setFocusPolicy (Qt::NoFocus);
  46. boutonSwitch->setFocusPolicy (Qt::NoFocus);
  47. boutonInit->setFocusPolicy (Qt::NoFocus);
  48. etatBoutons (true , false , false , false);
  49. layoutBoutonSwitch = new QHBoxLayout;
  50. layoutBoutonSwitch->addWidget (boutonDemarrer);
  51. layoutBoutonSwitch->addWidget (boutonSwitch);
  52. layoutBoutonSwitch->addWidget (boutonArreter);
  53. layoutBoutonSwitch->addWidget (boutonInit);
  54. ///////////////////////////// Bouton Quitter ///////////////////////////
  55. boutonQuitter = new QPushButton ("Quitter");
  56. boutonQuitter->setFocusPolicy (Qt::NoFocus);
  57. layoutBoutons = new QHBoxLayout;
  58. layoutBoutons->addWidget (boutonQuitter);
  59. ///////////////////////////// Aide /////////////////////////////////////
  60. QLabel *labelAide = new QLabel ("<b>Aide</b><br />Espace : Lancer le chrono/Donner le chrono<br />Entrée : Stopper les deux chronos");
  61. /////////////////////////////Mise en page finale ///////////////////////////
  62. layoutComplet = new QVBoxLayout;
  63. layoutComplet->addLayout (layoutChrono);
  64. layoutComplet->addLayout (layoutBoutonSwitch);
  65. layoutComplet->addWidget (labelAide);
  66. layoutComplet->addLayout (layoutBoutons);
  67. setLayout (layoutComplet);
  68. setWindowTitle ("ChronoCheckMate");
  69. ///////////////////////////// connections ///////////////////////////
  70. QObject::connect (boutonQuitter , SIGNAL(clicked ()) , qApp , SLOT(quit ())) ;
  71. QObject::connect (boutonDemarrer , SIGNAL (clicked () ) , this , SLOT (demarrer () ));
  72. QObject::connect (boutonSwitch , SIGNAL (clicked() ) , this , SLOT (switcher() ));
  73. QObject::connect (boutonArreter , SIGNAL (clicked() ) , this , SLOT (arreter() ));
  74. QObject::connect (boutonInit , SIGNAL (clicked () ) , this , SLOT (init () ));
  75. }
  76. ///////////////// Méthodes ////////////////////
  77. void MaFenetre::etatBoutons (bool bouton1, bool bouton2 , bool bouton3 , bool bouton4) {
  78. boutonDemarrer->setEnabled (bouton1);
  79. boutonSwitch->setEnabled (bouton2);
  80. boutonArreter->setEnabled (bouton3);
  81. boutonInit->setEnabled (bouton4);
  82. }
  83. ////////////////// SLOTS ////////////////////////
  84. void MaFenetre::demarrer () {
  85. if (marche == false ) {
  86. chrono1->reinit ();
  87. chrono2->reinit ();
  88. chrono1->start ();
  89. marche = true;
  90. etatBoutons (false , true , true , false);
  91. }
  92. }
  93. void MaFenetre::switcher () {
  94. if (marche == true) {
  95. chrono1->basculer ();
  96. chrono2->basculer ();
  97. }
  98. }
  99. void MaFenetre::arreter () {
  100. chrono1->stop();
  101. chrono2->stop();
  102. marche = false;
  103. etatBoutons (true , false , false , true);
  104. }
  105. void MaFenetre::init () {
  106. if (marche == false) {
  107. chrono1->reinit();
  108. chrono2->reinit();
  109. etatBoutons (true , false , false , false);
  110. }
  111. }
  112. void MaFenetre::keyReleaseEvent (QKeyEvent * evenement) {
  113. if (evenement->key() == Qt::Key_Space) {
  114. if (marche == true) {
  115. switcher();
  116. }
  117. if (marche == false) {
  118. demarrer();
  119. }
  120. }
  121. if (evenement->key() == Qt::Key_Return) {
  122. arreter();
  123. }
  124. }