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>
48 KitchenAlertMainWindow::KitchenAlertMainWindow(QWidget *parent) :
50 ui(new Ui::KitchenAlertMainWindow)
57 connect(ui->CreateNewScheduleButton, SIGNAL (pressed()), this, SLOT (newTimerSequence()));
60 //alerts' tableview setup
63 ui->ComingAlertsTableView->setModel(&model_);
64 ui->ComingAlertsTableView->setSelectionMode(QAbstractItemView::SingleSelection);
65 ui->ComingAlertsTableView->setSelectionBehavior(QAbstractItemView::SelectRows);
66 ui->ComingAlertsTableView->horizontalHeader()->setResizeMode(QHeaderView::ResizeToContents);
67 ui->ComingAlertsTableView->horizontalHeader()->hide();
70 //Buttons used to reacting an alarm are hidden by default
72 disableSelectionDependentButtons();
75 connect(ui->ComingAlertsTableView->selectionModel(),SIGNAL(selectionChanged(QItemSelection,QItemSelection)),this,SLOT(timerSelected(QItemSelection,QItemSelection)));
77 connect(ui->DoneButton,SIGNAL(clicked()),this,SLOT(stop()));
78 connect(ui->RestartButton,SIGNAL(clicked()),this,SLOT(restart()));
79 connect(ui->SnoozeButton,SIGNAL(clicked()),this, SLOT(snooze()));
83 QAction * p_SelectSoundAction = new QAction(tr("Select alert sound"),this);
84 connect(p_SelectSoundAction, SIGNAL(triggered()), this, SLOT(openSelectSoundDialog()));
85 menuBar()->addAction(p_SelectSoundAction);
87 QAction * p_AboutAction = new QAction(tr("About"),this);
88 connect(p_AboutAction,SIGNAL(triggered()),this,SLOT(openAbout()));
89 menuBar()->addAction(p_AboutAction);
92 KitchenAlertMainWindow::~KitchenAlertMainWindow()
97 void KitchenAlertMainWindow::changeEvent(QEvent *e)
99 QMainWindow::changeEvent(e);
101 case QEvent::LanguageChange:
102 ui->retranslateUi(this);
112 void KitchenAlertMainWindow::newTimerSequence()
114 CreateTimerSequenceDialog createdialog;
117 if (createdialog.exec() == QDialog::Accepted) //if user pressed OK
121 QList<Timer *> alltimers = createdialog.getTimers(); //get user input from the dialog
123 Timer* timer1 = alltimers.at(0); // take first timer (currently the only one!)
127 connect(timer1,SIGNAL(alert(QModelIndex)),this,SLOT(alert(QModelIndex)));
131 model_.addTimers(alltimers); // give timers to the model
133 // ui->ComingAlertsTableView->resizeColumnsToContents();
136 //Disable buttons, as selection is cleared when view is refreshed to show the new timer
138 disableSelectionDependentButtons();
143 // if cancelled, do nothing
153 void KitchenAlertMainWindow::alert(QModelIndex indexOfAlerter)
156 // The program is brought to front and activated when alerted
161 // The alerting timer is selected
162 ui->ComingAlertsTableView->selectionModel()->select(QItemSelection(indexOfAlerter,indexOfAlerter),QItemSelectionModel::SelectCurrent | QItemSelectionModel::Rows );
163 qDebug() << "Should be selected now";
166 //Snooze button is enabled
169 ui->SnoozeButton->setEnabled(true);
171 //The alert sound is played
178 void KitchenAlertMainWindow::timerSelected(QItemSelection selected,QItemSelection deselected)
180 ui->DoneButton->setEnabled(true);
181 ui->RestartButton->setEnabled(true);
184 //enabled only when alerting
185 QModelIndexList indexes = selected.indexes();
187 //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
188 QModelIndex index = indexes.value(0);
191 if (model_.isThisTimerAlerting(index) == true)
193 ui->SnoozeButton->setEnabled(true);
195 else ui->SnoozeButton->setDisabled(true);
200 void KitchenAlertMainWindow::snooze()
202 QModelIndex row = selectedRow();
203 if (row.isValid()) //If there was no row selected invalid row was returned
205 model_.snoozeTimer(row);
207 ui->SnoozeButton->setDisabled(true);
212 void KitchenAlertMainWindow::restart()
214 QModelIndex row = selectedRow(); //If there was no row selected invalid row was returned
218 model_.startTimer(row);
220 ui->SnoozeButton->setDisabled(true);
225 void KitchenAlertMainWindow::stop()
227 QModelIndex row = selectedRow();
228 if (row.isValid()) //If there was no row selected invalid row was returned
230 model_.stopTimer(row);
232 ui->SnoozeButton->setDisabled(true);
236 QModelIndex KitchenAlertMainWindow::selectedRow()
238 QModelIndexList chosenRows = ui->ComingAlertsTableView->selectionModel()->selectedRows();
240 //The selection mode used allows only one row to be selected at time, so we just take the first
241 //There are indexes for all columns in the row in the list, but as we only use the row, it does not matter which one we take
243 return chosenRows.value(0); //gives an invalid QModelIndex if the list is empty
246 void KitchenAlertMainWindow::openSelectSoundDialog()
248 SelectSoundDialog dialog;
249 if ( dialog.exec() == QDialog::Accepted) //if user pressed OK
251 if (dialog.isDefaultSoundChecked() == true)
252 alertSound_.setDefaultSound();
254 alertSound_.setSound(dialog.getSoundFileName());
256 //opening a dialog clears the selection so the selection dependen buttons must be disabled
258 disableSelectionDependentButtons();
261 void KitchenAlertMainWindow::openAbout()
263 QMessageBox::about(this,tr("About KitchenAlert"),tr("<p>Version 0.1"
264 "<p>Copyright © Heli Hyvättinen 2010"
265 "<p>License: General Public License v3"
266 "<p>Bugtracker and project page: https://garage.maemo.org/projects/kitchenalert/"));
269 bool KitchenAlertMainWindow::event(QEvent *event)
271 QMainWindow::event(event);
273 switch (event->type())
275 case QEvent::WindowActivate:
277 model_.setUpdateViewOnChanges(true);
280 case QEvent::WindowDeactivate:
281 model_.setUpdateViewOnChanges(false);
289 void KitchenAlertMainWindow::disableSelectionDependentButtons()
291 ui->DoneButton->setDisabled(true);
292 ui->SnoozeButton->setDisabled(true);
293 ui->RestartButton->setDisabled(true);