Alignment works + scrolls to the alerting timer
[kitchenalert] / src / currentalertstablemodel.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 "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
117                     allseconds = currentTimers_.at(index.row())->getRemainingTimeInSeconds();
118
119
120                     if (allseconds < 0)
121                     {
122                         timeAsText = tr("-", "negative sign");
123                         allseconds = -allseconds;
124
125                     }
126
127                     hoursOnly.setNum( allseconds/(60*60));
128
129
130                     minutesOnly.setNum((allseconds%(60*60))/60);
131
132
133                     secondsOnly.setNum (allseconds%60);
134
135
136                     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');
137
138
139
140
141 //                    qDebug () << timeAsText;
142
143                     return timeAsText;
144
145
146                 case statusColumnNumber_:
147
148                     if (currentTimers_.at(index.row())->isAlerting() == true)
149                         return QString("ALERT!");
150
151                     else return QString();
152
153
154             }
155
156             break;
157
158       case Qt::ForegroundRole :
159
160             //No need to care for the column number, all have the same color
161
162             if (currentTimers_.at(index.row())->isAlerting() == false)
163                 return QBrush (QColor(Qt::white));
164             else return QBrush (QColor(Qt::red)); //change this to black if backgroundrole starts to work!
165
166
167
168 //      case Qt::BackgroundRole :
169
170 //            //For some reason, these have no effect at all!!! They are asked by the view though.
171
172 //            //No need to care for the column number, all have the same color
173
174 //            qDebug() << "BackgroundRole asked";
175
176 //            if (currentTimers_.at(index.row())->isAlerting())
177 //            {
178 //                qDebug() << "black background";
179 //                return QBrush (QColor(Qt::black));
180 //            }
181 //            else
182 //            {
183 //                qDebug() << "red background";
184 //                return QBrush (QColor(Qt::red));
185 //            }
186         default:
187             return QVariant();
188
189     }
190
191
192
193 }
194
195
196
197
198 void CurrentAlertsTableModel::addTimers(QList <Timer *> timers, bool startImmediately)
199 {
200
201 //preparatory work
202     foreach (Timer* timer, timers)
203     {
204         connect (timer,SIGNAL(remainingTimeChanged()),this,SLOT(refreshTimeAndStatusColumns()));
205 //        qDebug() << "timer connected";
206         timer->setParent(this); //The model becomes the timers parent giving the timer access to model
207     }
208
209
210 //Add the timers
211
212     beginResetModel();
213     currentTimers_.append(timers);
214     endResetModel();
215
216     //start the timers if requested
217
218     if (startImmediately)
219     {
220         foreach (Timer* timer, timers)
221         {
222             timer->start();
223         }
224     }
225
226 }
227
228
229 void CurrentAlertsTableModel::refreshTimeAndStatusColumns()
230 {
231     if (updateViewOnChanges_ == true) //Only update GUI if active to save battery
232     {
233         emit dataChanged(createIndex(0,1),createIndex((rowCount(QModelIndex())-1),2));  //Entire time and status columns refreshed
234
235
236     }
237
238
239 }
240
241
242 void CurrentAlertsTableModel::startTimer(QModelIndex index)
243 {
244     Timer * ptimer = currentTimers_.value(index.row());
245     if (ptimer != NULL)
246     {
247         ptimer->start();
248         refreshTimeAndStatusColumns();
249     }
250 }
251
252 void CurrentAlertsTableModel::stopTimer(QModelIndex index)
253 {
254     Timer * ptimer = currentTimers_.value(index.row());
255     if (ptimer != NULL)
256     {
257         ptimer->stop();
258         refreshTimeAndStatusColumns();
259     }
260 }
261
262 void CurrentAlertsTableModel::snoozeTimer(QModelIndex index)
263 {
264     Timer * ptimer = currentTimers_.value(index.row());
265     if (ptimer != NULL)
266     {
267         ptimer->snooze();
268         refreshTimeAndStatusColumns();
269     }
270 }
271
272 QModelIndex CurrentAlertsTableModel::giveIndexForTimer(Timer * ptimer)
273 {
274     int row = currentTimers_.indexOf(ptimer);
275     if (row <= -1) // if not found
276         return QModelIndex(); //return invalid index
277
278
279     return createIndex(row,0); //return index to the timer row's first column
280
281 }
282
283
284 QVariant CurrentAlertsTableModel::headerData(int section, Qt::Orientation orientation, int role) const
285 {
286     //Reimplemented from QAbsractTableModel
287     //No headers wanted so we just return an empty QVariant
288     return QVariant();
289 }
290
291
292 void CurrentAlertsTableModel::setUpdateViewOnChanges(bool update)
293 {
294     updateViewOnChanges_ = update;
295     if (update == true)
296         refreshTimeAndStatusColumns(); //Refresh to catch up with past changes
297 }
298
299 bool CurrentAlertsTableModel::isThisTimerAlerting(QModelIndex index)
300 {
301     if (index.isValid())
302     {
303         if (currentTimers_.at(index.row())->isAlerting())
304         {
305             return true;
306         }
307
308     }
309     return false;
310 }