1 /**************************************************************************
4 Copyright (C) 2010 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 **************************************************************************/
28 #include "currentalertstablemodel.h"
31 #include <QXmlStreamWriter>
32 #include <QXmlStreamReader>
34 Timer::Timer(QObject *parent) :
39 _actualTimer.setInterval(1000); // 1 s
40 connect(&_actualTimer, SIGNAL(timeout()), this, SLOT(secondPassed()));
47 int Timer::getOriginalTimeInSeconds()
52 void Timer::setOriginalTimeInSeconds(int seconds)
54 _originalTime = seconds;
57 int Timer::getRemainingTimeInSeconds()
59 return _remainingTime;
62 QString Timer::getAlertText()
67 void Timer::setAlertText(QString text)
72 void Timer::secondPassed()
77 if (_remainingTime == 0)
80 emit alert(whereAmI());
81 qDebug() << "alerted";
84 emit remainingTimeChanged(); //after alerting in case of alert so that status gets updated immediately
89 _remainingTime = _originalTime;
94 if (_originalTime == 0) //has to be checked here, since 00:00:00 alert will already be negative when checked next time
95 //THIS ALERTS EVERY SECOND TIME THE TIMER IS STARTED! //This bug disappeared without explanation...
98 emit alert(whereAmI());
99 qDebug () << "Alerted for 00:00:00 alert";
107 _remainingTime = 0; //Stopped timer shows 00:00:00
114 _remainingTime = 120;
120 bool Timer::isAlerting()
125 QModelIndex Timer::whereAmI()
129 QObject * p_parent = parent();
132 CurrentAlertsTableModel* p_parentModel = qobject_cast<CurrentAlertsTableModel *> (p_parent);
134 if (p_parentModel == NULL) //If no parent or parent is not of right class, return an invalid index
135 return QModelIndex();
138 return p_parentModel->giveIndexForTimer(this);
143 bool Timer::save(QString filename)
145 QFile file(filename);
147 if (!file.open(QFile::WriteOnly | QFile::Text))
152 QXmlStreamWriter xmlWriter(&file);
153 xmlWriter.setAutoFormatting(true);
154 xmlWriter.writeStartDocument();
155 xmlWriter.writeStartElement("kitchenalert");
156 xmlWriter.writeStartElement("timer");
157 xmlWriter.writeAttribute("alert_text",_alertText);
158 xmlWriter.writeAttribute("time_in_seconds", QString().setNum(_originalTime));
159 xmlWriter.writeEndDocument(); //this should close all open elements
164 bool Timer::load(QString filename)
166 QFile file (filename);
167 if (!file.open(QFile::ReadOnly | QFile::Text))
172 QXmlStreamReader reader;
173 reader.setDevice(&file);
175 reader.readNextStartElement();
177 if (reader.name() != "kitchenalert")
180 reader.readNextStartElement();
181 if (reader.name() != "timer")
185 _alertText = reader.attributes().value("alert_text").toString();
186 _originalTime = reader.attributes().value("time_in_seconds").toString().toInt();