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 **************************************************************************/
28 #include "currentalertstablemodel.h"
31 #include <QXmlStreamWriter>
33 Timer::Timer(QObject *parent) :
38 _actualTimer.setInterval(1000); // 1 s
39 connect(&_actualTimer, SIGNAL(timeout()), this, SLOT(secondPassed()));
43 _remainingTime = 0; //Same as when stopped
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)
81 emit alert(whereAmI());
82 // qDebug() << "alerting";
85 emit remainingTimeChanged(); //after alerting in case of alert so that status gets updated immediately
90 _remainingTime = _originalTime;
95 if (_originalTime == 0) //a 00:00:00 alert has to be checked here, since it's already negative when checked for the next time
99 emit alert(whereAmI());
100 // qDebug () << "Alerting 00:00:00 from row: " << whereAmI().row();
115 _remainingTime = 0; //Stopped timer shows 00:00:00
123 _remainingTime = 120;
130 bool Timer::isAlerting()
135 QModelIndex Timer::whereAmI()
139 QObject * p_parent = parent();
142 CurrentAlertsTableModel* p_parentModel = qobject_cast<CurrentAlertsTableModel *> (p_parent);
144 if (p_parentModel == NULL) //If no parent or parent is not of right class, return an invalid index
145 return QModelIndex();
148 return p_parentModel->giveIndexForTimer(this);
153 void Timer::enableDefaultSound()
155 alertSound_.setDefaultSound();
158 void Timer::changeAlertSound(QString filename)
160 alertSound_.setSound(filename);
166 bool Timer::save(QString filename)
168 QFile file(filename);
170 if (!file.open(QFile::WriteOnly | QFile::Text))
175 QXmlStreamWriter xmlWriter(&file);
176 xmlWriter.setAutoFormatting(true);
177 xmlWriter.writeStartDocument();
178 xmlWriter.writeStartElement("kitchenalert");
179 xmlWriter.writeStartElement("timer");
180 xmlWriter.writeAttribute("alert_text",_alertText);
181 xmlWriter.writeAttribute("time_in_seconds", QString().setNum(_originalTime));
182 xmlWriter.writeEndDocument(); //this should close all open elements
187 bool Timer::load(QString filename)
189 QFile file (filename);
190 if (!file.open(QFile::ReadOnly | QFile::Text))
195 QXmlStreamReader reader;
196 reader.setDevice(&file);
198 reader.readNextStartElement();
200 if (reader.name() != "kitchenalert")
203 reader.readNextStartElement();
204 if (reader.name() != "timer")
208 _alertText = reader.attributes().value("alert_text").toString();
209 _originalTime = reader.attributes().value("time_in_seconds").toString().toInt();
211 filenameWithPath_ = filename;
216 bool Timer::isRunning()
218 return _actualTimer.isActive();