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