Chronometre.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * Chronometre.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 "Chronometre.h"
  23. #include <QTimer>
  24. #include <QtWidgets>
  25. #include <QLabel>
  26. #include <QObject>
  27. #include <QHBoxLayout>
  28. #include <QDebug>
  29. #include <QFont>
  30. #include <QTime>
  31. #include <QGroupBox>
  32. #include <QDebug>
  33. Chronometre::Chronometre (const QString & nom, Couleur unecouleur, QTime const& tempsDepart, Placement placement, QWidget * parent) :
  34. QWidget (parent),
  35. couleur(unecouleur),
  36. temps(tempsDepart),
  37. msTempsDepart(tempsDepart)
  38. {
  39. QHBoxLayout *layoutPrincipal = new QHBoxLayout;
  40. labelPion = new QLabel;
  41. pion = new QPixmap();
  42. setCouleur(couleur);
  43. labelPion->setPixmap(*pion);
  44. groupBox = new QGroupBox(nom);
  45. groupBox->setFlat(false);
  46. groupBox->setFont(QFont(groupBox->font().family(), 15));
  47. QFont font("Arial", 45, QFont::Black);
  48. ecranTemps = new QLabel();
  49. ecranTemps->setMinimumWidth(200);
  50. ecranTemps->setFont(font);
  51. ecranTemps->setDisabled(true);
  52. ecranLayout = new QHBoxLayout;
  53. //ecranLayout->addSpacing(10);
  54. //ecranLayout->addStretch(1);
  55. ecranLayout->addWidget(ecranTemps, 0, Qt::AlignHCenter);
  56. //ecranLayout->addStretch(1);
  57. //ecranLayout->addSpacing(10);
  58. if (placement == Placement::Gauche)
  59. {
  60. layoutPrincipal->addWidget(labelPion,0);
  61. layoutPrincipal->addWidget(groupBox,1);
  62. } else
  63. {
  64. layoutPrincipal->addWidget(groupBox,1);
  65. layoutPrincipal->addWidget(labelPion,0);
  66. }
  67. groupBox->setLayout (ecranLayout);
  68. setLayout(layoutPrincipal);
  69. timer = new QTimer (this);
  70. timer->setInterval (INTERVAL);
  71. QObject::connect (timer , &QTimer::timeout , this , &Chronometre::refresh);
  72. }
  73. void Chronometre::modifierNomJoueur(QString const& joueur) {
  74. groupBox->setTitle(joueur);
  75. }
  76. void Chronometre::definirTemps(QTime const& tempsDepart) {
  77. msTempsDepart = tempsDepart;
  78. }
  79. void Chronometre::echangerCouleur(Chronometre & chrono) {
  80. Couleur temp = couleur;
  81. setCouleur(chrono.couleur);
  82. chrono.setCouleur(temp);
  83. }
  84. void Chronometre::setCouleur (Couleur const& color) {
  85. couleur = color;
  86. QString stringCouleur;
  87. if (couleur == Blanc)
  88. stringCouleur = "white";
  89. else
  90. stringCouleur = "black";
  91. pion->load(QString (":/ressources/pawn_%1.svg").arg(stringCouleur));
  92. labelPion->setPixmap(*pion);
  93. }
  94. Couleur Chronometre::getCouleur() const {
  95. return couleur;
  96. }
  97. void Chronometre::refresh () {
  98. temps = temps.addMSecs(-INTERVAL);
  99. affichage ();
  100. if (temps <= QTime (0,0,0,0)) {
  101. emit fin(this);
  102. }
  103. }
  104. void Chronometre::start () {
  105. ecranTemps->setEnabled(true);
  106. timer->start ();
  107. }
  108. void Chronometre::stop () {
  109. ecranTemps->setDisabled(true);
  110. timer->stop();
  111. }
  112. void Chronometre::affichage () {
  113. QString chaineTemps = temps.toString("hh:mm:ss");
  114. ecranTemps->setText(chaineTemps);
  115. //qDebug() << "Taille : " << ecranTemps->width();
  116. }
  117. void Chronometre::reinit () {
  118. if (timer->isActive()) {
  119. timer->stop();
  120. }
  121. temps = msTempsDepart;
  122. affichage();
  123. }
  124. void Chronometre::basculer () {
  125. if (timer->isActive()) {
  126. stop();
  127. emit is_paused(this);
  128. }
  129. else {
  130. start ();
  131. }
  132. }
  133. void Chronometre::declareGagnant()
  134. {
  135. emit won(this);
  136. }
  137. void Chronometre::basculer(Chronometre & Chrono) {
  138. basculer();
  139. Chrono.basculer();
  140. }
  141. QString Chronometre::nomJoueur() const
  142. {
  143. return groupBox->title();
  144. }
  145. /**
  146. * @brief Ajouter du temps, prévu pour plus que des secondes
  147. */
  148. void Chronometre::addSecs(unsigned int secondes)
  149. {
  150. temps = temps.addSecs(secondes);
  151. }