Initial commit: added current sources
[kitchenalert] / src / timer.cpp
1 /**************************************************************************
2         KitchenAlert v.0.01
3
4         Copyright (C) 2010  Heli Hyvättinen
5
6         This program is free software: you can redistribute it and/or modify
7         it under the terms of the GNU General Public License as published by
8         the Free Software Foundation, either version 3 of the License, or
9         (at your option) any later version.
10
11         This program is distributed in the hope that it will be useful,
12         but WITHOUT ANY WARRANTY; without even the implied warranty of
13         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14         GNU General Public License for more details.
15
16         You should have received a copy of the GNU General Public License
17         along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19 **************************************************************************/
20
21
22
23
24
25 #include "timer.h"
26 #include "currentalertstablemodel.h"
27 #include <qdebug.h>
28
29 Timer::Timer(QObject *parent) :
30     QObject(parent)
31 {
32     _originalTime = 0;
33
34     _actualTimer.setInterval(1000); // 1 s
35     connect(&_actualTimer, SIGNAL(timeout()), this, SLOT(secondPassed()));
36
37     alerting_ = false;
38 }
39
40
41 int Timer::getOriginalTimeInSeconds()
42 {
43     return _originalTime;
44 }
45
46 void Timer::setOriginalTimeInSeconds(int seconds)
47 {
48 _originalTime = seconds;
49 }
50
51 int Timer::getRemainingTimeInSeconds()
52 {
53     return _remainingTime;
54 }
55
56 QString Timer::getAlertText()
57 {
58     return _alertText;
59 }
60
61 void Timer::setAlertText(QString text)
62 {
63     _alertText = text;
64 }
65
66 void Timer::secondPassed()
67 {
68     _remainingTime--;
69     emit remainingTimeChanged();
70
71     if (_remainingTime == 0)
72     {
73         alerting_ = true;
74         emit alert(whereAmI());
75         qDebug() << "alerting";
76     }
77
78 }
79
80 void Timer::start()
81 {
82     _remainingTime = _originalTime;
83     _actualTimer.start();
84
85 }
86
87
88 void Timer::stop()
89 {
90     _actualTimer.stop();
91     _remainingTime = 0; //Stopped timer shows 00:00:00 (which unfortunately makes it red...)
92
93
94 }
95
96 void Timer::snooze()
97 {
98     _remainingTime = 120;
99
100 }
101
102
103 bool Timer::isAlerting()
104 {
105     return alerting_;
106 }
107
108 QModelIndex Timer::whereAmI()
109 {
110
111
112    QObject * p_parent = parent();
113
114
115    CurrentAlertsTableModel* p_parentModel = qobject_cast<CurrentAlertsTableModel *> (p_parent);
116
117    if (p_parentModel == NULL) //If no parent or parent is not of right class, return an invalid index
118         return QModelIndex();
119
120
121   return p_parentModel->giveIndexForTimer(this);
122
123
124 }
125