Remove button, fixed column width, limited text & fixed 00:00:00
[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
44 int Timer::getOriginalTimeInSeconds()
45 {
46     return _originalTime;
47 }
48
49 void Timer::setOriginalTimeInSeconds(int seconds)
50 {
51 _originalTime = seconds;
52 }
53
54 int Timer::getRemainingTimeInSeconds()
55 {
56     return _remainingTime;
57 }
58
59 QString Timer::getAlertText()
60 {
61     return _alertText;
62 }
63
64 void Timer::setAlertText(QString text)
65 {
66     _alertText = text;
67 }
68
69 void Timer::secondPassed()
70 {
71     _remainingTime--;
72
73
74     if (_remainingTime == 0)
75     {
76         alerting_ = true;
77         emit alert(whereAmI());
78         qDebug() << "alerted";
79     }
80
81     emit remainingTimeChanged(); //after alerting in case of alert so that status gets updated immediately
82 }
83
84 void Timer::start()
85 {
86     _remainingTime = _originalTime;
87     _actualTimer.start();
88
89     alerting_ = false;
90
91     if (_originalTime == 0) //has to be checked here, since 00:00:00 alert will already be negative when checked next time
92     //THIS ALERTS EVERY SECOND TIME THE TIMER IS STARTED! //This bug disappeared without explanation...
93     {
94         alerting_ = true;
95         emit alert(whereAmI());
96         qDebug () << "Alerted for 00:00:00 alert";
97     }
98 }
99
100
101 void Timer::stop()
102 {
103     _actualTimer.stop();
104     _remainingTime = 0; //Stopped timer shows 00:00:00
105
106     alerting_ = false;
107 }
108
109 void Timer::snooze()
110 {
111     _remainingTime = 120;
112
113     alerting_ = false;
114 }
115
116
117 bool Timer::isAlerting()
118 {
119     return alerting_;
120 }
121
122 QModelIndex Timer::whereAmI()
123 {
124
125
126    QObject * p_parent = parent();
127
128
129    CurrentAlertsTableModel* p_parentModel = qobject_cast<CurrentAlertsTableModel *> (p_parent);
130
131    if (p_parentModel == NULL) //If no parent or parent is not of right class, return an invalid index
132         return QModelIndex();
133
134
135   return p_parentModel->giveIndexForTimer(this);
136
137
138 }
139