Stopped timers now show full time and "stopped"
[kitchenalert] / src / currentalertstablemodel.cpp
1 /**************************************************************************
2         KitchenAlert
3
4         Copyright (C) 2010-2011  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 "currentalertstablemodel.h"
28
29
30 #include <QBrush>
31
32
33 #include <qdebug.h>
34
35 CurrentAlertsTableModel::CurrentAlertsTableModel(QObject *parent) :
36     QAbstractTableModel(parent)
37 {
38
39     updateViewOnChanges_ = true;
40 }
41
42
43 int CurrentAlertsTableModel::rowCount(const QModelIndex &parent) const
44 {
45
46 //No need to mind about the parameter, it has no meaning for table models.
47
48 //    qDebug () << "rowCount asked";
49
50 //    qDebug () << currentTimers_.length();
51
52     return currentTimers_.length();
53 }
54
55
56 int CurrentAlertsTableModel::columnCount(const QModelIndex &parent) const
57 {
58
59     //No need to mind about the parameter, it has no meaning for table models.
60     return numberOfColumns_;
61
62 }
63
64
65 QVariant CurrentAlertsTableModel::data(const QModelIndex &index, int role) const
66 {
67     if (!index.isValid())
68         return QVariant();
69
70     QString timeAsText; //here, because seems not to be allowed inside switch
71     int allseconds;     //likewise
72     QString hoursOnly;
73     QString minutesOnly;
74     QString secondsOnly;
75
76
77
78     switch(role)
79     {
80         case Qt::TextAlignmentRole :
81
82             switch (index.column())
83             {
84
85             case alertTextColumnNumber_:
86
87                 return int (Qt::AlignLeft | Qt::AlignVCenter);
88
89
90             case timeRemainingColumnNumber_:
91
92                 return int (Qt::AlignRight | Qt::AlignVCenter);
93
94             case statusColumnNumber_:
95
96                 return int (Qt::AlignLeft | Qt::AlignVCenter);
97
98             }
99
100             break;
101
102         case Qt::DisplayRole :
103
104             switch (index.column())
105             {
106                 case alertTextColumnNumber_:
107
108
109
110                     return currentTimers_.at(index.row())->getAlertText();
111
112
113                 case timeRemainingColumnNumber_:
114
115
116                     if (currentTimers_.at(index.row())->isRunning()) //timer running
117                     {
118
119
120                     allseconds = currentTimers_.at(index.row())->getRemainingTimeInSeconds();
121
122
123                         if (allseconds < 0)
124                         {
125                             timeAsText = tr("-", "negative sign");
126                             allseconds = -allseconds;
127
128                         }
129                     }
130
131                     else //timer stopped or never started
132
133                     {
134                         //use original time
135                         allseconds = currentTimers_.at(index.row())->getOriginalTimeInSeconds();
136                     }
137
138                     hoursOnly.setNum( allseconds/(60*60));
139
140
141                     minutesOnly.setNum((allseconds%(60*60))/60);
142
143
144                     secondsOnly.setNum (allseconds%60);
145
146
147                     timeAsText += tr("%1:%2:%3", "%1 is hours, %2 is minutes and % 3 is seconds. Time remaining to alert, not time of day.").arg (hoursOnly,2,'0').arg(minutesOnly,2,'0').arg(secondsOnly,2,'0');
148
149
150
151
152 //                    qDebug () << timeAsText;
153
154                     return timeAsText;
155
156
157                 case statusColumnNumber_:
158
159                     if (currentTimers_.at(index.row())->isAlerting() == true)
160                         return tr("ALERT!");
161
162                     if (!currentTimers_.at(index.row())->isRunning())
163                         return tr("stopped");
164
165                     else return QString();
166
167
168             }
169
170             break;
171
172       case Qt::ForegroundRole :
173
174             //No need to care for the column number, all have the same color
175
176             if (currentTimers_.at(index.row())->isAlerting() == false)
177                 return QBrush (QColor(Qt::white));
178             else return QBrush (QColor(Qt::red)); //change this to black if backgroundrole starts to work!
179
180
181
182 //      case Qt::BackgroundRole :
183
184 //            //For some reason, these have no effect at all!!! They are asked by the view though.
185
186 //            //No need to care for the column number, all have the same color
187
188 //            qDebug() << "BackgroundRole asked";
189
190 //            if (currentTimers_.at(index.row())->isAlerting())
191 //            {
192 //                qDebug() << "black background";
193 //                return QBrush (QColor(Qt::black));
194 //            }
195 //            else
196 //            {
197 //                qDebug() << "red background";
198 //                return QBrush (QColor(Qt::red));
199 //            }
200         default:
201             return QVariant();
202
203     }
204
205
206
207 }
208
209
210
211
212 void CurrentAlertsTableModel::addTimers(QList <Timer *> timers, bool startImmediately)
213 {
214
215 //preparatory work
216     foreach (Timer* timer, timers)
217     {
218         connect (timer,SIGNAL(remainingTimeChanged()),this,SLOT(refreshTimeAndStatusColumns()));
219 //        qDebug() << "timer connected";
220         timer->setParent(this); //The model becomes the timers parent giving the timer access to model
221     }
222
223
224 //Add the timers
225
226     beginResetModel();
227     currentTimers_.append(timers);
228     endResetModel();
229
230     //start the timers if requested
231
232     if (startImmediately)
233     {
234         foreach (Timer* timer, timers)
235         {
236             timer->start();
237         }
238     }
239
240 }
241
242
243 void CurrentAlertsTableModel::refreshTimeAndStatusColumns()
244 {
245     if (updateViewOnChanges_ == true) //Only update GUI if active to save battery
246     {
247         emit dataChanged(createIndex(0,1),createIndex((rowCount(QModelIndex())-1),2));  //Entire time and status columns refreshed
248
249
250     }
251
252
253 }
254
255
256 void CurrentAlertsTableModel::startTimer(QModelIndex index)
257 {
258     Timer * ptimer = currentTimers_.value(index.row());
259     if (ptimer != NULL)
260     {
261         ptimer->start();
262         refreshTimeAndStatusColumns();
263     }
264 }
265
266 void CurrentAlertsTableModel::stopTimer(QModelIndex index)
267 {
268     Timer * ptimer = currentTimers_.value(index.row());
269     if (ptimer != NULL)
270     {
271         ptimer->stop();
272         refreshTimeAndStatusColumns();
273     }
274 }
275
276 void CurrentAlertsTableModel::snoozeTimer(QModelIndex index)
277 {
278     Timer * ptimer = currentTimers_.value(index.row());
279     if (ptimer != NULL)
280     {
281         ptimer->snooze();
282         refreshTimeAndStatusColumns();
283     }
284 }
285
286 QModelIndex CurrentAlertsTableModel::giveIndexForTimer(Timer * ptimer)
287 {
288     int row = currentTimers_.indexOf(ptimer);
289     if (row <= -1) // if not found
290         return QModelIndex(); //return invalid index
291
292
293     return createIndex(row,0); //return index to the timer row's first column
294
295 }
296
297
298 QVariant CurrentAlertsTableModel::headerData(int section, Qt::Orientation orientation, int role) const
299 {
300     //Reimplemented from QAbsractTableModel
301     //No headers wanted so we just return an empty QVariant
302     return QVariant();
303 }
304
305
306 void CurrentAlertsTableModel::setUpdateViewOnChanges(bool update)
307 {
308     updateViewOnChanges_ = update;
309     if (update == true)
310     {
311         refreshTimeAndStatusColumns(); //Refresh to catch up with past changes
312 //        qDebug() << "Just refreshed time and status colums after returning to the app";
313     }
314     }
315
316 bool CurrentAlertsTableModel::isThisTimerAlerting(QModelIndex index)
317 {
318     if (index.isValid())
319     {
320         if (currentTimers_.at(index.row())->isAlerting())
321         {
322             return true;
323         }
324
325     }
326     return false;
327 }
328
329 void CurrentAlertsTableModel::removeTimer(QModelIndex index)
330 {
331     if (index.isValid() == false)
332         return;
333
334     int i = index.row();
335     beginRemoveRows(QModelIndex(),i,i);
336     Timer * p_timer = currentTimers_.takeAt(i);
337     delete p_timer;
338     endRemoveRows();
339
340 }
341
342 bool CurrentAlertsTableModel::saveTimer(QModelIndex index, QString filename)
343 {
344     return currentTimers_.at(index.row())->save(filename);
345 }
346