6587e01036c96cd389ba985e8c9409a10a530f91
[kitchenalert] / src / currentalertstablemodel.cpp
1 /**************************************************************************
2         KitchenAlert v.0.019
3
4         Copyright (C) 2010  Heli Hyvättinen
5
6         This program is free software: you can redistribute it and/or modify
7         it under the terms of the GNU General Public License as published by
8         the Free Software Foundation, either version 3 of the License, or
9         (at your option) any later version.
10
11         This program is distributed in the hope that it will be useful,
12         but WITHOUT ANY WARRANTY; without even the implied warranty of
13         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14         GNU General Public License for more details.
15
16         You should have received a copy of the GNU General Public License
17         along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19 **************************************************************************/
20
21
22
23
24
25 #include "currentalertstablemodel.h"
26
27
28 #include <QBrush>
29
30
31 #include <qdebug.h>
32
33 CurrentAlertsTableModel::CurrentAlertsTableModel(QObject *parent) :
34     QAbstractTableModel(parent)
35 {
36 }
37
38
39 int CurrentAlertsTableModel::rowCount(const QModelIndex &parent) const
40 {
41
42 //No need to mind about the parameter, it has no meaning for table models.
43
44     qDebug () << "rowCount asked";
45
46     qDebug () << currentTimers_.length();
47
48     return currentTimers_.length();
49 }
50
51
52 int CurrentAlertsTableModel::columnCount(const QModelIndex &parent) const
53 {
54
55     //No need to mind about the parameter, it has no meaning for table models.
56     return numberOfColumns_;
57
58 }
59
60
61 QVariant CurrentAlertsTableModel::data(const QModelIndex &index, int role) const
62 {
63     if (!index.isValid())
64         return QVariant();
65
66     QString timeAsText; //here, because seems not to be allowed inside switch
67     int allseconds;     //likewise
68     QString hoursOnly;
69     QString minutesOnly;
70     QString secondsOnly;
71
72
73
74     switch(role)
75     {
76         case Qt::TextAlignmentRole :
77
78             switch (index.column())
79             {
80
81             case alertTextColumnNumber_:
82
83                 return int (Qt::AlignLeft || Qt::AlignVCenter);
84
85
86             case timeRemainingColumnNumber_:
87
88                 return int (Qt::AlignRight || Qt::AlignVCenter);
89
90             case statusColumnNumber_:
91
92                 return int (Qt::AlignLeft || Qt::AlignVCenter);
93
94             }
95
96             break;
97
98         case Qt::DisplayRole :
99
100             switch (index.column())
101             {
102                 case alertTextColumnNumber_:
103
104
105
106                     return currentTimers_.at(index.row())->getAlertText();
107
108
109                 case timeRemainingColumnNumber_:
110
111
112
113                     allseconds = currentTimers_.at(index.row())->getRemainingTimeInSeconds();
114
115
116                     if (allseconds < 0)
117                     {
118                         timeAsText = tr("-", "negative sign");
119                         allseconds = -allseconds;
120
121                     }
122
123                     hoursOnly.setNum( allseconds/(60*60));
124
125
126                     minutesOnly.setNum((allseconds%(60*60))/60);
127
128
129                     secondsOnly.setNum (allseconds%60);
130
131
132                     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');
133
134
135
136
137                     qDebug () << timeAsText;
138
139                     return timeAsText;
140
141
142                 case statusColumnNumber_:
143
144                     return QVariant();  //TO BE REPLACED WITH SENDING STATUS !
145
146
147             }
148
149             break;
150
151       case Qt::ForegroundRole :
152
153             //No need to care for the column number, all have the same color
154
155             if (currentTimers_.at(index.row())->getRemainingTimeInSeconds() > 0)
156                 return QBrush (QColor(Qt::white));
157             else return QBrush (QColor(Qt::red)); //change this to black if backgroundrole starts to work!
158
159
160
161       case Qt::BackgroundRole :
162
163             //For some reason, these have no effect at all!!! They are asked by the view though.
164
165             //No need to care for the column number, all have the same color
166
167             qDebug() << "BackgroundRole asked";
168
169             if (currentTimers_.at(index.row())->getRemainingTimeInSeconds() > 0)
170             {
171                 qDebug() << "black background";
172                 return QBrush (QColor(Qt::black));
173             }
174             else
175             {
176                 qDebug() << "red background";
177                 return QBrush (QColor(Qt::red));
178             }
179         default:
180             return QVariant();
181
182     }
183
184
185
186 }
187
188
189
190
191 void CurrentAlertsTableModel::addTimers(QList <Timer *> timers)
192 {
193     foreach (Timer* timer, timers)
194     {
195         connect (timer,SIGNAL(remainingTimeChanged()),this,SLOT(refreshTimeColumn()));
196         qDebug() << "timer connected";
197         timer->setParent(this); //The model becomes the timers parent giving the timer access to model
198     }
199     currentTimers_.append(timers);
200     qDebug() << "Timers should be appended";
201     reset();
202 }
203
204
205 void CurrentAlertsTableModel::refreshTimeColumn()
206 {
207     qDebug() << "Refresh time column";
208
209
210
211     emit dataChanged(createIndex(0,1),createIndex(rowCount(QModelIndex())-1,1));  //Entire time column refreshed
212
213 }
214
215
216 void CurrentAlertsTableModel::startTimer(QModelIndex index)
217 {
218     currentTimers_.at(index.row())->start();
219     refreshTimeColumn();
220 }
221
222 void CurrentAlertsTableModel::stopTimer(QModelIndex index)
223 {
224   currentTimers_.at(index.row())->stop();
225    refreshTimeColumn();
226 }
227
228 void CurrentAlertsTableModel::snoozeTimer(QModelIndex index)
229 {
230
231     currentTimers_.at(index.row())->snooze();
232     refreshTimeColumn();
233 }
234
235 QModelIndex CurrentAlertsTableModel::giveIndexForTimer(Timer * ptimer)
236 {
237     int row = currentTimers_.indexOf(ptimer);
238     if (row < -1) // if not found
239         return QModelIndex(); //return invalid index
240
241
242     return createIndex(row,0); //return index to the timer row's first column
243
244 }