Chronometre.cpp 3.5 KB

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