1 /**************************************************************************
4 Copyright (C) 2010-2011 Heli Hyvättinen
6 This file is part of KitchenAlert.
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.
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.
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/>.
21 **************************************************************************/
27 #include "currentalertstablemodel.h"
35 CurrentAlertsTableModel::CurrentAlertsTableModel(QObject *parent) :
36 QAbstractTableModel(parent)
39 updateViewOnChanges_ = true;
43 int CurrentAlertsTableModel::rowCount(const QModelIndex &parent) const
46 //No need to mind about the parameter, it has no meaning for table models.
48 // qDebug () << "rowCount asked";
50 // qDebug () << currentTimers_.length();
52 return currentTimers_.length();
56 int CurrentAlertsTableModel::columnCount(const QModelIndex &parent) const
59 //No need to mind about the parameter, it has no meaning for table models.
60 return numberOfColumns_;
65 QVariant CurrentAlertsTableModel::data(const QModelIndex &index, int role) const
70 QString timeAsText; //here, because seems not to be allowed inside switch
71 int allseconds; //likewise
80 case Qt::TextAlignmentRole :
82 switch (index.column())
85 case alertTextColumnNumber_:
87 return int (Qt::AlignLeft | Qt::AlignVCenter);
90 case timeRemainingColumnNumber_:
92 return int (Qt::AlignRight | Qt::AlignVCenter);
94 case statusColumnNumber_:
96 return int (Qt::AlignLeft | Qt::AlignVCenter);
102 case Qt::DisplayRole :
104 switch (index.column())
106 case alertTextColumnNumber_:
110 return currentTimers_.at(index.row())->getAlertText();
113 case timeRemainingColumnNumber_:
117 allseconds = currentTimers_.at(index.row())->getRemainingTimeInSeconds();
122 timeAsText = tr("-", "negative sign");
123 allseconds = -allseconds;
127 hoursOnly.setNum( allseconds/(60*60));
130 minutesOnly.setNum((allseconds%(60*60))/60);
133 secondsOnly.setNum (allseconds%60);
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');
141 // qDebug () << timeAsText;
146 case statusColumnNumber_:
148 if (currentTimers_.at(index.row())->isAlerting() == true)
149 return QString("ALERT!");
151 else return QString();
158 case Qt::ForegroundRole :
160 //No need to care for the column number, all have the same color
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!
168 // case Qt::BackgroundRole :
170 // //For some reason, these have no effect at all!!! They are asked by the view though.
172 // //No need to care for the column number, all have the same color
174 // qDebug() << "BackgroundRole asked";
176 // if (currentTimers_.at(index.row())->isAlerting())
178 // qDebug() << "black background";
179 // return QBrush (QColor(Qt::black));
183 // qDebug() << "red background";
184 // return QBrush (QColor(Qt::red));
198 void CurrentAlertsTableModel::addTimers(QList <Timer *> timers, bool startImmediately)
202 foreach (Timer* timer, timers)
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
213 currentTimers_.append(timers);
216 //start the timers if requested
218 if (startImmediately)
220 foreach (Timer* timer, timers)
229 void CurrentAlertsTableModel::refreshTimeAndStatusColumns()
231 if (updateViewOnChanges_ == true) //Only update GUI if active to save battery
233 emit dataChanged(createIndex(0,1),createIndex((rowCount(QModelIndex())-1),2)); //Entire time and status columns refreshed
242 void CurrentAlertsTableModel::startTimer(QModelIndex index)
244 Timer * ptimer = currentTimers_.value(index.row());
248 refreshTimeAndStatusColumns();
252 void CurrentAlertsTableModel::stopTimer(QModelIndex index)
254 Timer * ptimer = currentTimers_.value(index.row());
258 refreshTimeAndStatusColumns();
262 void CurrentAlertsTableModel::snoozeTimer(QModelIndex index)
264 Timer * ptimer = currentTimers_.value(index.row());
268 refreshTimeAndStatusColumns();
272 QModelIndex CurrentAlertsTableModel::giveIndexForTimer(Timer * ptimer)
274 int row = currentTimers_.indexOf(ptimer);
275 if (row <= -1) // if not found
276 return QModelIndex(); //return invalid index
279 return createIndex(row,0); //return index to the timer row's first column
284 QVariant CurrentAlertsTableModel::headerData(int section, Qt::Orientation orientation, int role) const
286 //Reimplemented from QAbsractTableModel
287 //No headers wanted so we just return an empty QVariant
292 void CurrentAlertsTableModel::setUpdateViewOnChanges(bool update)
294 updateViewOnChanges_ = update;
297 refreshTimeAndStatusColumns(); //Refresh to catch up with past changes
298 // qDebug() << "Just refreshed time and status colums after returning to the app";
302 bool CurrentAlertsTableModel::isThisTimerAlerting(QModelIndex index)
306 if (currentTimers_.at(index.row())->isAlerting())
315 void CurrentAlertsTableModel::removeTimer(QModelIndex index)
317 if (index.isValid() == false)
321 beginRemoveRows(QModelIndex(),i,i);
322 Timer * p_timer = currentTimers_.takeAt(i);