/**************************************************************************
- KitchenAlert v.0.019
+ KitchenAlert
- Copyright (C) 2010 Heli Hyvättinen
+ Copyright (C) 2010-2011 Heli Hyvättinen
- This program is free software: you can redistribute it and/or modify
+ This file is part of KitchenAlert.
+
+ Kitchen Alert is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
CurrentAlertsTableModel::CurrentAlertsTableModel(QObject *parent) :
QAbstractTableModel(parent)
{
+
+ updateViewOnChanges_ = true;
}
//No need to mind about the parameter, it has no meaning for table models.
- qDebug () << "rowCount asked";
+// qDebug () << "rowCount asked";
- qDebug () << currentTimers_.length();
+// qDebug () << currentTimers_.length();
return currentTimers_.length();
}
case alertTextColumnNumber_:
- return int (Qt::AlignLeft || Qt::AlignVCenter);
+ return int (Qt::AlignLeft | Qt::AlignVCenter);
case timeRemainingColumnNumber_:
- return int (Qt::AlignRight || Qt::AlignVCenter);
+ return int (Qt::AlignRight | Qt::AlignVCenter);
case statusColumnNumber_:
- return int (Qt::AlignLeft || Qt::AlignVCenter);
+ return int (Qt::AlignLeft | Qt::AlignVCenter);
}
- qDebug () << timeAsText;
+// qDebug () << timeAsText;
return timeAsText;
case statusColumnNumber_:
- return QVariant(); //TO BE REPLACED WITH SENDING STATUS !
+ if (currentTimers_.at(index.row())->isAlerting() == true)
+ return QString("ALERT!");
+
+ else return QString();
}
//No need to care for the column number, all have the same color
- if (currentTimers_.at(index.row())->getRemainingTimeInSeconds() > 0)
+ if (currentTimers_.at(index.row())->isAlerting() == false)
return QBrush (QColor(Qt::white));
else return QBrush (QColor(Qt::red)); //change this to black if backgroundrole starts to work!
- case Qt::BackgroundRole :
+// case Qt::BackgroundRole :
- //For some reason, these have no effect at all!!! They are asked by the view though.
+// //For some reason, these have no effect at all!!! They are asked by the view though.
- //No need to care for the column number, all have the same color
+// //No need to care for the column number, all have the same color
- qDebug() << "BackgroundRole asked";
+// qDebug() << "BackgroundRole asked";
- if (currentTimers_.at(index.row())->getRemainingTimeInSeconds() > 0)
- {
- qDebug() << "black background";
- return QBrush (QColor(Qt::black));
- }
- else
- {
- qDebug() << "red background";
- return QBrush (QColor(Qt::red));
- }
+// if (currentTimers_.at(index.row())->isAlerting())
+// {
+// qDebug() << "black background";
+// return QBrush (QColor(Qt::black));
+// }
+// else
+// {
+// qDebug() << "red background";
+// return QBrush (QColor(Qt::red));
+// }
default:
return QVariant();
-void CurrentAlertsTableModel::addTimers(QList <Timer *> timers)
+void CurrentAlertsTableModel::addTimers(QList <Timer *> timers, bool startImmediately)
{
+
+//preparatory work
foreach (Timer* timer, timers)
{
- connect (timer,SIGNAL(remainingTimeChanged()),this,SLOT(refreshTimeColumn()));
- qDebug() << "timer connected";
+ connect (timer,SIGNAL(remainingTimeChanged()),this,SLOT(refreshTimeAndStatusColumns()));
+// qDebug() << "timer connected";
timer->setParent(this); //The model becomes the timers parent giving the timer access to model
}
+
+
+//Add the timers
+
+ beginResetModel();
currentTimers_.append(timers);
- qDebug() << "Timers should be appended";
- reset();
+ endResetModel();
+
+ //start the timers if requested
+
+ if (startImmediately)
+ {
+ foreach (Timer* timer, timers)
+ {
+ timer->start();
+ }
+ }
+
}
-void CurrentAlertsTableModel::refreshTimeColumn()
+void CurrentAlertsTableModel::refreshTimeAndStatusColumns()
{
- qDebug() << "Refresh time column";
+ if (updateViewOnChanges_ == true) //Only update GUI if active to save battery
+ {
+ emit dataChanged(createIndex(0,1),createIndex((rowCount(QModelIndex())-1),2)); //Entire time and status columns refreshed
+ }
- emit dataChanged(createIndex(0,1),createIndex(rowCount(QModelIndex())-1,1)); //Entire time column refreshed
}
void CurrentAlertsTableModel::startTimer(QModelIndex index)
{
- currentTimers_.at(index.row())->start();
- refreshTimeColumn();
+ Timer * ptimer = currentTimers_.value(index.row());
+ if (ptimer != NULL)
+ {
+ ptimer->start();
+ refreshTimeAndStatusColumns();
+ }
}
void CurrentAlertsTableModel::stopTimer(QModelIndex index)
{
- currentTimers_.at(index.row())->stop();
- refreshTimeColumn();
+ Timer * ptimer = currentTimers_.value(index.row());
+ if (ptimer != NULL)
+ {
+ ptimer->stop();
+ refreshTimeAndStatusColumns();
+ }
}
void CurrentAlertsTableModel::snoozeTimer(QModelIndex index)
{
-
- currentTimers_.at(index.row())->snooze();
- refreshTimeColumn();
+ Timer * ptimer = currentTimers_.value(index.row());
+ if (ptimer != NULL)
+ {
+ ptimer->snooze();
+ refreshTimeAndStatusColumns();
+ }
}
QModelIndex CurrentAlertsTableModel::giveIndexForTimer(Timer * ptimer)
{
int row = currentTimers_.indexOf(ptimer);
- if (row < -1) // if not found
+ if (row <= -1) // if not found
return QModelIndex(); //return invalid index
return createIndex(row,0); //return index to the timer row's first column
}
+
+
+QVariant CurrentAlertsTableModel::headerData(int section, Qt::Orientation orientation, int role) const
+{
+ //Reimplemented from QAbsractTableModel
+ //No headers wanted so we just return an empty QVariant
+ return QVariant();
+}
+
+
+void CurrentAlertsTableModel::setUpdateViewOnChanges(bool update)
+{
+ updateViewOnChanges_ = update;
+ if (update == true)
+ {
+ refreshTimeAndStatusColumns(); //Refresh to catch up with past changes
+// qDebug() << "Just refreshed time and status colums after returning to the app";
+ }
+ }
+
+bool CurrentAlertsTableModel::isThisTimerAlerting(QModelIndex index)
+{
+ if (index.isValid())
+ {
+ if (currentTimers_.at(index.row())->isAlerting())
+ {
+ return true;
+ }
+
+ }
+ return false;
+}
+
+void CurrentAlertsTableModel::removeTimer(QModelIndex index)
+{
+ if (index.isValid() == false)
+ return;
+
+ int i = index.row();
+ beginRemoveRows(QModelIndex(),i,i);
+ Timer * p_timer = currentTimers_.takeAt(i);
+ delete p_timer;
+ endRemoveRows();
+
+}
+