Chronometre.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 <QWidget>
  25. #include <QLabel>
  26. #include <QObject>
  27. #include <QHBoxLayout>
  28. #include <QDebug>
  29. #include <QFont>
  30. Chronometre::Chronometre (const QString & nom , QWidget * parent = 0) : QGroupBox (nom , parent) {
  31. setFlat(false);
  32. heure = 0;
  33. minute = 0;
  34. seconde = 0;
  35. centieme = 0;
  36. QFont font("Times New Roman" , 22 , 5);
  37. ecranHeure = new QLabel ("00", this);
  38. ecranMinute = new QLabel ("00" , this);
  39. ecranSeconde = new QLabel ("00" , this);
  40. ecranCentieme = new QLabel ("00" , this);
  41. ecranHeure->setFont(font);
  42. ecranMinute->setFont(font);
  43. ecranSeconde->setFont(font);
  44. ecranCentieme->setFont(font);
  45. ecranLayout = new QHBoxLayout;
  46. ecranLayout->addWidget(ecranHeure);
  47. ecranLayout->addWidget(new QLabel(":"));
  48. ecranLayout->addWidget(ecranMinute);
  49. ecranLayout->addWidget(new QLabel(":"));
  50. ecranLayout->addWidget(ecranSeconde);
  51. ecranLayout->addWidget(new QLabel(":"));
  52. ecranLayout->addWidget(ecranCentieme);
  53. ecranLayout->addSpacing(40);
  54. //ecranLayout->addStretch(-1);
  55. setLayout (ecranLayout);
  56. affichage();
  57. timer = new QTimer (this);
  58. timer->setInterval (10);
  59. QObject::connect (timer , SIGNAL (timeout() ) , this , SLOT (refresh()));
  60. }
  61. void Chronometre::refresh () {
  62. if (centieme == 99) {
  63. if (seconde == 59) {
  64. if (minute == 59) {
  65. heure += 1;
  66. minute = 0;
  67. seconde = 0;
  68. centieme = 0;
  69. }
  70. else {
  71. minute += 1;
  72. seconde = 0;
  73. centieme = 0;
  74. }
  75. }
  76. else {
  77. seconde += 1;
  78. centieme = 0;
  79. }
  80. }
  81. else {
  82. centieme += 1;
  83. }
  84. affichage ();
  85. }
  86. void Chronometre::start () {
  87. timer->start ();
  88. }
  89. void Chronometre::stop () {
  90. timer->stop();
  91. }
  92. int Chronometre::getHeure () {
  93. return heure;
  94. }
  95. int Chronometre::getMinute () {
  96. return minute;
  97. }
  98. int Chronometre::getSeconde () {
  99. return seconde;
  100. }
  101. int Chronometre::getCentieme () {
  102. return centieme;
  103. }
  104. QString Chronometre::qString (int nombre) {
  105. QString qNombre;
  106. qNombre = "<b>";
  107. if (nombre < 10) {
  108. qNombre.append("0");
  109. }
  110. qNombre.append(QString::number(nombre));
  111. qNombre.append("</b>");
  112. return qNombre;
  113. }
  114. void Chronometre::affichage () {
  115. ecranHeure->setText(qString (heure));
  116. ecranMinute->setText(qString(minute));
  117. ecranSeconde->setText(qString(seconde));
  118. ecranCentieme->setText(qString(centieme));
  119. }
  120. void Chronometre::reinit () {
  121. if (timer->isActive()) {
  122. timer->stop();
  123. }
  124. heure = 0;
  125. minute = 0;
  126. seconde = 0;
  127. centieme = 0;
  128. affichage();
  129. }
  130. void Chronometre::basculer () {
  131. if (timer->isActive()) {
  132. timer->stop();
  133. }
  134. else {
  135. timer->start ();
  136. }
  137. }