1 /**************************************************************************
5 Copyright (C) 2010 Heli Hyvättinen
7 This file is part of KitchenAlert.
9 Kitchen Alert is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 **************************************************************************/
28 #include "kitchenalertmainwindow.h"
29 #include "ui_kitchenalertmainwindow.h"
35 #include "createtimersequencedialog.h"
36 #include "selectsounddialog.h"
44 #include <QMessageBox>
49 KitchenAlertMainWindow::KitchenAlertMainWindow(QWidget *parent) :
51 ui(new Ui::KitchenAlertMainWindow)
55 setWindowIcon(QIcon(":/kitchenalert.png"));
60 connect(ui->CreateNewScheduleButton, SIGNAL (pressed()), this, SLOT (newTimerSequence()));
63 //alerts' tableview setup
66 ui->ComingAlertsTableView->setModel(&model_);
67 ui->ComingAlertsTableView->setSelectionMode(QAbstractItemView::SingleSelection);
68 ui->ComingAlertsTableView->setSelectionBehavior(QAbstractItemView::SelectRows);
70 ui->ComingAlertsTableView->horizontalHeader()->hide();
71 // ui->ComingAlertsTableView->verticalHeader()->setVisible(true);
73 ui->ComingAlertsTableView->horizontalHeader()->setResizeMode(QHeaderView::Fixed);
74 ui->ComingAlertsTableView->horizontalHeader()->resizeSection(0,535);
75 ui->ComingAlertsTableView->horizontalHeader()->resizeSection(1,140);
76 ui->ComingAlertsTableView->horizontalHeader()->resizeSection(2,100);
78 ui->ComingAlertsTableView->verticalHeader()->setResizeMode(QHeaderView::ResizeToContents);
81 //Buttons used to reacting an alarm are hidden by default
83 disableSelectionDependentButtons();
86 connect(ui->ComingAlertsTableView->selectionModel(),SIGNAL(selectionChanged(QItemSelection,QItemSelection)),this,SLOT(timerSelected(QItemSelection,QItemSelection)));
88 connect(ui->DoneButton,SIGNAL(clicked()),this,SLOT(stop()));
89 connect(ui->RestartButton,SIGNAL(clicked()),this,SLOT(restart()));
90 connect(ui->SnoozeButton,SIGNAL(clicked()),this, SLOT(snooze()));
91 connect(ui->RemoveButton,SIGNAL(clicked()),this,SLOT(remove()));
94 QAction * p_SelectSoundAction = new QAction(tr("Select alert sound"),this);
95 connect(p_SelectSoundAction, SIGNAL(triggered()), this, SLOT(openSelectSoundDialog()));
96 menuBar()->addAction(p_SelectSoundAction);
98 QAction * p_AboutAction = new QAction(tr("About"),this);
99 connect(p_AboutAction,SIGNAL(triggered()),this,SLOT(openAbout()));
100 menuBar()->addAction(p_AboutAction);
103 KitchenAlertMainWindow::~KitchenAlertMainWindow()
108 void KitchenAlertMainWindow::changeEvent(QEvent *e)
110 QMainWindow::changeEvent(e);
112 case QEvent::LanguageChange:
113 ui->retranslateUi(this);
123 void KitchenAlertMainWindow::newTimerSequence()
125 CreateTimerSequenceDialog createdialog;
128 if (createdialog.exec() == QDialog::Accepted) //if user pressed OK
132 QList<Timer *> alltimers = createdialog.getTimers(); //get user input from the dialog
134 Timer* timer1 = alltimers.at(0); // take first timer (currently the only one!)
138 connect(timer1,SIGNAL(alert(QModelIndex)),this,SLOT(alert(QModelIndex)));
140 //TODO: FIND A WAY TO INFORM THE TIMERS' ALERTSOUND'S OF A CHANGE OF THE SOUND FILE THEY SHOULD USE!!!!
142 connect(this,SIGNAL(defaultSoundEnabled()),timer1,SLOT(enableDefaultSound()));
143 connect(this,SIGNAL(soundChanged(QString)),timer1,SLOT(changeAlertSound(QString)));
147 model_.addTimers(alltimers); // give timers to the model, they are started automatically by default
149 // ui->ComingAlertsTableView->resizeColumnsToContents();
152 //Disable buttons, as selection is cleared when view is refreshed to show the new timer
153 //But only if the timer has not already alerted and thus been selected
155 if (!selectedRow().isValid())
156 disableSelectionDependentButtons();
161 // if cancelled, do nothing
171 void KitchenAlertMainWindow::alert(QModelIndex indexOfAlerter)
174 // The program is brought to front and activated when alerted
180 // The alerting timer is selected
181 ui->ComingAlertsTableView->selectionModel()->select(QItemSelection(indexOfAlerter,indexOfAlerter),QItemSelectionModel::SelectCurrent | QItemSelectionModel::Rows );
183 //Scrolls the view so that the alerting timer is visible
184 ui->ComingAlertsTableView->scrollTo(indexOfAlerter);
186 // qDebug() << "Should be selected now";
189 //Snooze button is enabled
192 ui->SnoozeButton->setEnabled(true);
198 ui->debugLabel->setText(tr("Alert received from row %1").arg(indexOfAlerter.row()));
199 qDebug() << "Wrote the debug message";
201 //The alert sound is played
202 //TESTING TO MOVE THIS OPERATION TO THE TIMER ITSELF
203 // alertSound_.play();
208 void KitchenAlertMainWindow::timerSelected(QItemSelection selected,QItemSelection)
210 ui->DoneButton->setEnabled(true);
211 ui->RestartButton->setEnabled(true);
212 ui->RemoveButton->setEnabled(true);
215 //snooze button enabled only when alerting
216 QModelIndexList indexes = selected.indexes();
218 //the selection model only allows selecting one row at the time & we only need to know the row, so we can just take the first one
219 QModelIndex index = indexes.value(0);
222 if (model_.isThisTimerAlerting(index) == true)
224 ui->SnoozeButton->setEnabled(true);
226 else ui->SnoozeButton->setDisabled(true);
231 void KitchenAlertMainWindow::snooze()
233 QModelIndex row = selectedRow();
234 if (row.isValid()) //If there was no row selected invalid row was returned
236 model_.snoozeTimer(row);
238 ui->SnoozeButton->setDisabled(true);
239 // alertSound_.stop();
243 void KitchenAlertMainWindow::restart()
245 QModelIndex row = selectedRow(); //If there was no row selected invalid row was returned
249 model_.startTimer(row);
251 ui->SnoozeButton->setDisabled(true);
252 // alertSound_.stop();
256 void KitchenAlertMainWindow::stop()
258 QModelIndex row = selectedRow();
259 if (row.isValid()) //If there was no row selected invalid row was returned
261 model_.stopTimer(row);
263 ui->SnoozeButton->setDisabled(true);
264 // alertSound_.stop();
267 QModelIndex KitchenAlertMainWindow::selectedRow()
269 //Returns the cells in row 0 that have the whole row selected (the selection mode used allows only selecting whole rows
271 QModelIndexList chosenRows = ui->ComingAlertsTableView->selectionModel()->selectedRows();
273 //The selection mode used allows only one row to be selected at time, so we just take the first
276 return chosenRows.value(0); //gives an invalid QModelIndex if the list is empty
279 void KitchenAlertMainWindow::openSelectSoundDialog()
282 SelectSoundDialog dialog;
283 if ( dialog.exec() == QDialog::Accepted) //if user pressed OK
285 if (dialog.isDefaultSoundChecked() == true)
286 emit defaultSoundEnabled();
288 emit soundChanged(dialog.getSoundFileName());
293 void KitchenAlertMainWindow::openAbout()
295 QMessageBox::about(this,tr("About KitchenAlert"),tr("<p>Version %1"
296 "<p>Copyright © Heli Hyvättinen 2010"
297 "<p>License: General Public License v3"
298 "<p>Web page: http://kitchenalert.garage.maemo.org/"
299 "<p>Bugtracker: https://garage.maemo.org/projects/kitchenalert/").arg(QApplication::applicationVersion()));
302 bool KitchenAlertMainWindow::event(QEvent *event)
306 switch (event->type())
308 case QEvent::WindowActivate:
310 model_.setUpdateViewOnChanges(true);
311 ui->debugLabel->setText("Returned to the application!");
314 case QEvent::WindowDeactivate:
315 model_.setUpdateViewOnChanges(false);
316 ui->debugLabel->setText("");
324 return QMainWindow::event(event); // Send the event to the base class implementation (also when handling the event in this function): necessary for the program to work!
327 void KitchenAlertMainWindow::disableSelectionDependentButtons()
329 ui->DoneButton->setDisabled(true);
330 ui->SnoozeButton->setDisabled(true);
331 ui->RestartButton->setDisabled(true);
332 ui->RemoveButton->setDisabled(true);
336 void KitchenAlertMainWindow::initializeAlertSound()
340 bool useDefaultSound = settings.value("UseDefaultSound",true).toBool();
341 QString filename = settings.value("soundfile","").toString();
343 if (useDefaultSound == true)
345 openSelectSoundDialog();
347 else if (filename.isEmpty())
349 openSelectSoundDialog();
352 QString currentFilename = settings.value("soundfile","").toString();
354 if (currentFilename.isEmpty())
356 ui->debugLabel->setText("<FONT color=red>No alert sound file set. Alert sound will not be played!</FONT>");
362 void KitchenAlertMainWindow::remove()
364 QModelIndex row = selectedRow();
365 if (row.isValid()) //If there was no row selected invalid row was returned
367 QString text = tr("Are you sure you want to remove this timer from the list:\n");
368 text.append((row.data().toString()));
369 if (QMessageBox::question(this,tr("Confirm timer removal"),text,QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes)
371 model_.removeTimer(row);
374 ui->SnoozeButton->setDisabled(true);