X-Git-Url: https://vcs.maemo.org/git/?a=blobdiff_plain;f=src%2Fcurrentalertstablemodel.cpp;h=d0e70c30f35e5a51f51ae9adbed66ddeee535cfa;hb=e3d78d642357c3cbd65564b4784088d548b1ba6a;hp=b856739832c5af4b808452c569510b19a4a60974;hpb=d703fb607c0291912211a5d43cdd04b60b6e87c6;p=kitchenalert diff --git a/src/currentalertstablemodel.cpp b/src/currentalertstablemodel.cpp index b856739..d0e70c3 100644 --- a/src/currentalertstablemodel.cpp +++ b/src/currentalertstablemodel.cpp @@ -1,7 +1,7 @@ /************************************************************************** KitchenAlert - Copyright (C) 2010 Heli Hyvättinen + Copyright (C) 2010-2011 Heli Hyvättinen This file is part of KitchenAlert. @@ -35,6 +35,8 @@ CurrentAlertsTableModel::CurrentAlertsTableModel(QObject *parent) : QAbstractTableModel(parent) { + + updateViewOnChanges_ = true; } @@ -43,9 +45,9 @@ int CurrentAlertsTableModel::rowCount(const QModelIndex &parent) const //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(); } @@ -82,16 +84,16 @@ QVariant CurrentAlertsTableModel::data(const QModelIndex &index, int role) const 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); } @@ -111,15 +113,26 @@ QVariant CurrentAlertsTableModel::data(const QModelIndex &index, int role) const case timeRemainingColumnNumber_: + if (currentTimers_.at(index.row())->isRunning()) //timer running + { + allseconds = currentTimers_.at(index.row())->getRemainingTimeInSeconds(); - if (allseconds < 0) - { - timeAsText = tr("-", "negative sign"); - allseconds = -allseconds; + if (allseconds < 0) + { + timeAsText = tr("-", "negative sign"); + allseconds = -allseconds; + } + } + + else //timer stopped or never started + + { + //use original time + allseconds = currentTimers_.at(index.row())->getOriginalTimeInSeconds(); } hoursOnly.setNum( allseconds/(60*60)); @@ -136,14 +149,20 @@ QVariant CurrentAlertsTableModel::data(const QModelIndex &index, int role) const - qDebug () << timeAsText; +// qDebug () << timeAsText; return timeAsText; case statusColumnNumber_: - return QVariant(); //TO BE REPLACED WITH SENDING STATUS ! + if (currentTimers_.at(index.row())->isAlerting() == true) + return tr("ALERT!"); + + if (!currentTimers_.at(index.row())->isRunning()) + return tr("stopped"); + + else return QString(); } @@ -154,30 +173,30 @@ QVariant CurrentAlertsTableModel::data(const QModelIndex &index, int role) const //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(); @@ -190,57 +209,138 @@ QVariant CurrentAlertsTableModel::data(const QModelIndex &index, int role) const -void CurrentAlertsTableModel::addTimers(QList timers) +void CurrentAlertsTableModel::addTimers(QList 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(); + +} + +bool CurrentAlertsTableModel::saveTimer(QModelIndex index, QString filename) +{ + return currentTimers_.at(index.row())->save(filename); +} +