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_:
116 if (currentTimers_.at(index.row())->isRunning()) //timer running
120 allseconds = currentTimers_.at(index.row())->getRemainingTimeInSeconds();
125 timeAsText = tr("-", "negative sign");
126 allseconds = -allseconds;
131 else //timer stopped or never started
135 allseconds = currentTimers_.at(index.row())->getOriginalTimeInSeconds();
138 hoursOnly.setNum( allseconds/(60*60));
141 minutesOnly.setNum((allseconds%(60*60))/60);
144 secondsOnly.setNum (allseconds%60);
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');
152 // qDebug () << timeAsText;
157 case statusColumnNumber_:
159 if (currentTimers_.at(index.row())->isAlerting() == true)
162 if (!currentTimers_.at(index.row())->isRunning())
163 return tr("stopped");
165 else return QString();
172 case Qt::ForegroundRole :
174 //No need to care for the column number, all have the same color
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!
182 // case Qt::BackgroundRole :
184 // //For some reason, these have no effect at all!!! They are asked by the view though.
186 // //No need to care for the column number, all have the same color
188 // qDebug() << "BackgroundRole asked";
190 // if (currentTimers_.at(index.row())->isAlerting())
192 // qDebug() << "black background";
193 // return QBrush (QColor(Qt::black));
197 // qDebug() << "red background";
198 // return QBrush (QColor(Qt::red));
212 void CurrentAlertsTableModel::addTimers(QList <Timer *> timers, bool startImmediately)
216 foreach (Timer* timer, timers)
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
227 currentTimers_.append(timers);
230 //start the timers if requested
232 if (startImmediately)
234 foreach (Timer* timer, timers)
243 void CurrentAlertsTableModel::refreshTimeAndStatusColumns()
245 if (updateViewOnChanges_ == true) //Only update GUI if active to save battery
247 emit dataChanged(createIndex(0,1),createIndex((rowCount(QModelIndex())-1),2)); //Entire time and status columns refreshed
256 void CurrentAlertsTableModel::startTimer(QModelIndex index)
258 Timer * ptimer = currentTimers_.value(index.row());
262 refreshTimeAndStatusColumns();
266 void CurrentAlertsTableModel::stopTimer(QModelIndex index)
268 Timer * ptimer = currentTimers_.value(index.row());
272 refreshTimeAndStatusColumns();
276 void CurrentAlertsTableModel::snoozeTimer(QModelIndex index)
278 Timer * ptimer = currentTimers_.value(index.row());
282 refreshTimeAndStatusColumns();
286 QModelIndex CurrentAlertsTableModel::giveIndexForTimer(Timer * ptimer)
288 int row = currentTimers_.indexOf(ptimer);
289 if (row <= -1) // if not found
290 return QModelIndex(); //return invalid index
293 return createIndex(row,0); //return index to the timer row's first column
298 QVariant CurrentAlertsTableModel::headerData(int section, Qt::Orientation orientation, int role) const
300 //Reimplemented from QAbsractTableModel
301 //No headers wanted so we just return an empty QVariant
306 void CurrentAlertsTableModel::setUpdateViewOnChanges(bool update)
308 updateViewOnChanges_ = update;
311 refreshTimeAndStatusColumns(); //Refresh to catch up with past changes
312 // qDebug() << "Just refreshed time and status colums after returning to the app";
316 bool CurrentAlertsTableModel::isThisTimerAlerting(QModelIndex index)
320 if (currentTimers_.at(index.row())->isAlerting())
329 void CurrentAlertsTableModel::removeTimer(QModelIndex index)
331 if (index.isValid() == false)
335 beginRemoveRows(QModelIndex(),i,i);
336 Timer * p_timer = currentTimers_.takeAt(i);
342 bool CurrentAlertsTableModel::saveTimer(QModelIndex index, QString filename)
344 return currentTimers_.at(index.row())->save(filename);