Added sound to the alert
[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
40
41 int CurrentAlertsTableModel::rowCount(const QModelIndex &parent) const
42 {
43
44 //No need to mind about the parameter, it has no meaning for table models.
45
46     qDebug () << "rowCount asked";
47
48     qDebug () << currentTimers_.length();
49
50     return currentTimers_.length();
51 }
52
53
54 int CurrentAlertsTableModel::columnCount(const QModelIndex &parent) const
55 {
56
57     //No need to mind about the parameter, it has no meaning for table models.
58     return numberOfColumns_;
59
60 }
61
62
63 QVariant CurrentAlertsTableModel::data(const QModelIndex &index, int role) const
64 {
65     if (!index.isValid())
66         return QVariant();
67
68     QString timeAsText; //here, because seems not to be allowed inside switch
69     int allseconds;     //likewise
70     QString hoursOnly;
71     QString minutesOnly;
72     QString secondsOnly;
73
74
75
76     switch(role)
77     {
78         case Qt::TextAlignmentRole :
79
80             switch (index.column())
81             {
82
83             case alertTextColumnNumber_:
84
85                 return int (Qt::AlignLeft || Qt::AlignVCenter);
86
87
88             case timeRemainingColumnNumber_:
89
90                 return int (Qt::AlignRight || Qt::AlignVCenter);
91
92             case statusColumnNumber_:
93
94                 return int (Qt::AlignLeft || Qt::AlignVCenter);
95
96             }
97
98             break;
99
100         case Qt::DisplayRole :
101
102             switch (index.column())
103             {
104                 case alertTextColumnNumber_:
105
106
107
108                     return currentTimers_.at(index.row())->getAlertText();
109
110
111                 case timeRemainingColumnNumber_:
112
113
114
115                     allseconds = currentTimers_.at(index.row())->getRemainingTimeInSeconds();
116
117
118                     if (allseconds < 0)
119                     {
120                         timeAsText = tr("-", "negative sign");
121                         allseconds = -allseconds;
122
123                     }
124
125                     hoursOnly.setNum( allseconds/(60*60));
126
127
128                     minutesOnly.setNum((allseconds%(60*60))/60);
129
130
131                     secondsOnly.setNum (allseconds%60);
132
133
134                     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');
135
136
137
138
139                     qDebug () << timeAsText;
140
141                     return timeAsText;
142
143
144                 case statusColumnNumber_:
145
146                     return QVariant();  //TO BE REPLACED WITH SENDING STATUS !
147
148
149             }
150
151             break;
152
153       case Qt::ForegroundRole :
154
155             //No need to care for the column number, all have the same color
156
157             if (currentTimers_.at(index.row())->getRemainingTimeInSeconds() > 0)
158                 return QBrush (QColor(Qt::white));
159             else return QBrush (QColor(Qt::red)); //change this to black if backgroundrole starts to work!
160
161
162
163       case Qt::BackgroundRole :
164
165             //For some reason, these have no effect at all!!! They are asked by the view though.
166
167             //No need to care for the column number, all have the same color
168
169             qDebug() << "BackgroundRole asked";
170
171             if (currentTimers_.at(index.row())->getRemainingTimeInSeconds() > 0)
172             {
173                 qDebug() << "black background";
174                 return QBrush (QColor(Qt::black));
175             }
176             else
177             {
178                 qDebug() << "red background";
179                 return QBrush (QColor(Qt::red));
180             }
181         default:
182             return QVariant();
183
184     }
185
186
187
188 }
189
190
191
192
193 void CurrentAlertsTableModel::addTimers(QList <Timer *> timers)
194 {
195     foreach (Timer* timer, timers)
196     {
197         connect (timer,SIGNAL(remainingTimeChanged()),this,SLOT(refreshTimeColumn()));
198         qDebug() << "timer connected";
199         timer->setParent(this); //The model becomes the timers parent giving the timer access to model
200     }
201     currentTimers_.append(timers);
202     qDebug() << "Timers should be appended";
203     reset();
204 }
205
206
207 void CurrentAlertsTableModel::refreshTimeColumn()
208 {
209     qDebug() << "Refresh time column";
210
211
212
213     emit dataChanged(createIndex(0,1),createIndex(rowCount(QModelIndex())-1,1));  //Entire time column refreshed
214
215 }
216
217
218 void CurrentAlertsTableModel::startTimer(QModelIndex index)
219 {
220     currentTimers_.at(index.row())->start();
221     refreshTimeColumn();
222 }
223
224 void CurrentAlertsTableModel::stopTimer(QModelIndex index)
225 {
226   currentTimers_.at(index.row())->stop();
227    refreshTimeColumn();
228 }
229
230 void CurrentAlertsTableModel::snoozeTimer(QModelIndex index)
231 {
232
233     currentTimers_.at(index.row())->snooze();
234     refreshTimeColumn();
235 }
236
237 QModelIndex CurrentAlertsTableModel::giveIndexForTimer(Timer * ptimer)
238 {
239     int row = currentTimers_.indexOf(ptimer);
240     if (row < -1) // if not found
241         return QModelIndex(); //return invalid index
242
243
244     return createIndex(row,0); //return index to the timer row's first column
245
246 }