| 1 | /*********************************************************************** |
| 2 | * |
| 3 | * Copyright (C) 2010-2017 Innocent De Marchi <[email protected]> |
| 4 | * |
| 5 | * This program is free software: you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation, either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | * |
| 18 | ***********************************************************************/ |
| 19 | #include <QtGui> |
| 20 | #include <QTimer> |
| 21 | #include <math.h> |
| 22 | |
| 23 | #include "rellotge.h" |
| 24 | |
| 25 | Rellotge::Rellotge(QWidget * parent , Qt::WindowFlags) |
| 26 | : QLabel(parent) |
| 27 | { |
| 28 | rellotgeQTimer = new QTimer(); |
| 29 | connect(rellotgeQTimer, SIGNAL(timeout()), this, SLOT(actualitzaRellotge())); |
| 30 | p_rellotgeEnMarxa=false; |
| 31 | hores=0; |
| 32 | minuts=0; |
| 33 | segons=0; |
| 34 | } |
| 35 | |
| 36 | void Rellotge::actualitzaRellotge(){ |
| 37 | // milisegons10++; |
| 38 | segons=segons+1; |
| 39 | // if (milisegons10>=100){ |
| 40 | // milisegons10=milisegons10-100; |
| 41 | // segons=segons+1; |
| 42 | // } |
| 43 | if (segons>=60){ |
| 44 | segons=segons-60; |
| 45 | minuts=minuts+1; |
| 46 | } |
| 47 | if (minuts>=60){ |
| 48 | minuts=minuts-60; |
| 49 | hores=hores+1; |
| 50 | } |
| 51 | setText(retornaTemps()); |
| 52 | } |
| 53 | |
| 54 | void Rellotge::iniciaRellotge(){ |
| 55 | p_rellotgeEnMarxa= true; |
| 56 | setText(retornaTemps()); |
| 57 | rellotgeQTimer->start(1000); |
| 58 | } |
| 59 | |
| 60 | //Fa que el rellotge aturat segueixi en marxa |
| 61 | void Rellotge::reIniciaRellotge(){ |
| 62 | rellotgeQTimer->start(1000); |
| 63 | p_rellotgeEnMarxa= true; |
| 64 | estableixTemps(retornaTemps()); |
| 65 | setText(retornaTemps()); |
| 66 | } |
| 67 | |
| 68 | void Rellotge::aturaRellotge(){ |
| 69 | rellotgeQTimer->stop(); |
| 70 | p_rellotgeEnMarxa=false; |
| 71 | setText(retornaTemps()); |
| 72 | update(); |
| 73 | } |
| 74 | |
| 75 | void Rellotge::estableixTemps(QString temps){ |
| 76 | QStringList list1 = temps.split(":"); |
| 77 | hores=list1.at(0).toInt(); |
| 78 | minuts=list1.at(1).toInt(); |
| 79 | segons=list1.at(2).toInt(); |
| 80 | setText(retornaTemps()); |
| 81 | update(); |
| 82 | } |
| 83 | |
| 84 | QString Rellotge::retornaTemps(int tipus){ |
| 85 | |
| 86 | QString sHores,sMinuts,sSegons; |
| 87 | |
| 88 | if (hores<10){ |
| 89 | sHores= QString("0%1:").arg(hores); |
| 90 | } |
| 91 | else sHores= QString("%1:").arg(hores); |
| 92 | |
| 93 | if (minuts<10){ |
| 94 | sMinuts= QString("0%1:").arg(minuts); |
| 95 | } |
| 96 | else sMinuts= QString("%1:").arg(minuts); |
| 97 | |
| 98 | if (segons<10){ |
| 99 | sSegons= QString("0%1").arg(segons); |
| 100 | } |
| 101 | else sSegons= QString("%1").arg(segons); |
| 102 | |
| 103 | if (tipus==0){ |
| 104 | if(rellotgeQTimer->isActive()){ |
| 105 | return QString("<h1><font color=red>%1%2%3</font></h1>").arg(sHores,sMinuts,sSegons); |
| 106 | } |
| 107 | else { |
| 108 | return QString("<h1><font color=blue>%1%2%3</font></h1>").arg(sHores,sMinuts,sSegons); |
| 109 | } |
| 110 | } |
| 111 | else { |
| 112 | return QString("%1%2%3").arg(sHores,sMinuts,sSegons); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | bool Rellotge::rellotgeEnMarxa(){ |
| 117 | return p_rellotgeEnMarxa; |
| 118 | } |
| 119 | |
| 120 | /*Retorna el temps marcat en segons totals |
| 121 | No es fa servir*/ |
| 122 | int Rellotge::retornarSegonsTotals(){ |
| 123 | return hores*3600+minuts*60+segons; |
| 124 | } |