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)
55 //alerts' tableview setup
58 ui->ComingAlertsTableView->setModel(&model_);
59 ui->ComingAlertsTableView->setSelectionMode(QAbstractItemView::SingleSelection);
60 ui->ComingAlertsTableView->setSelectionBehavior(QAbstractItemView::SelectRows);
61 ui->ComingAlertsTableView->horizontalHeader()->setResizeMode(QHeaderView::Fixed);
62 ui->ComingAlertsTableView->horizontalHeader()->resizeSection(0,460);
63 ui->ComingAlertsTableView->horizontalHeader()->resizeSection(1,140);
64 ui->ComingAlertsTableView->horizontalHeader()->resizeSection(2,100);
66 ui->ComingAlertsTableView->verticalHeader()->setResizeMode(QHeaderView::Fixed);
67 ui->ComingAlertsTableView->verticalHeader()->setDefaultSectionSize(40);
72 ui->ComingAlertsTableView->horizontalHeader()->hide();
73 ui->ComingAlertsTableView->setWordWrap(true);
76 //Buttons used when a timer is selected are disabled by default and enabled upon selection
78 disableSelectionDependentButtons();
80 connect(ui->ComingAlertsTableView->selectionModel(),SIGNAL(selectionChanged(QItemSelection,QItemSelection)),this,SLOT(timerSelected(QItemSelection,QItemSelection)));
82 //connect buttons to respective functions
83 connect(ui->CreateNewScheduleButton, SIGNAL (pressed()), this, SLOT (newTimerSequence()));
84 connect(ui->DoneButton,SIGNAL(clicked()),this,SLOT(stop()));
85 connect(ui->RestartButton,SIGNAL(clicked()),this,SLOT(restart()));
86 connect(ui->SnoozeButton,SIGNAL(clicked()),this, SLOT(snooze()));
87 connect(ui->RemoveButton,SIGNAL(clicked()),this,SLOT(remove()));
91 QAction * p_SelectSoundAction = new QAction(tr("Select alert sound"),this);
92 connect(p_SelectSoundAction, SIGNAL(triggered()), this, SLOT(openSelectSoundDialog()));
93 menuBar()->addAction(p_SelectSoundAction);
95 QAction * p_AboutAction = new QAction(tr("About"),this);
96 connect(p_AboutAction,SIGNAL(triggered()),this,SLOT(openAbout()));
97 menuBar()->addAction(p_AboutAction);
100 KitchenAlertMainWindow::~KitchenAlertMainWindow()
105 void KitchenAlertMainWindow::changeEvent(QEvent *e)
107 QMainWindow::changeEvent(e);
109 case QEvent::LanguageChange:
110 ui->retranslateUi(this);
120 void KitchenAlertMainWindow::newTimerSequence()
122 CreateTimerSequenceDialog createdialog;
125 if (createdialog.exec() == QDialog::Accepted) //if user pressed OK
128 //get user input from the dialog
131 QList<Timer *> alltimers = createdialog.getTimers();
133 // take first timer (currently the only one!)
136 Timer* timer1 = alltimers.at(0);
142 connect(timer1,SIGNAL(alert(QModelIndex)),this,SLOT(alert(QModelIndex)));
145 //Disable buttons, as selection is cleared when view is refreshed to show the new timer
147 disableSelectionDependentButtons();
150 // give timers to the model
152 model_.addTimers(alltimers);
155 //start the timer when it's safely in the model (consider moving this to the model's addTimers function)
162 // if cancelled, do nothing
172 void KitchenAlertMainWindow::alert(QModelIndex indexOfAlerter)
175 // 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 );
182 // qDebug() << "Should be selected now";
185 //Snooze button is enabled
188 ui->SnoozeButton->setEnabled(true);
190 //The alert sound is played (consider moving this operation inside timer, as now stopping one alert will silence all alerting alerts)
197 void KitchenAlertMainWindow::timerSelected(QItemSelection selected,QItemSelection deselected)
199 ui->DoneButton->setEnabled(true);
200 ui->RestartButton->setEnabled(true);
201 ui->RemoveButton->setEnabled(true);
204 //enabled only when alerting
205 QModelIndexList indexes = selected.indexes();
207 //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
208 QModelIndex index = indexes.value(0);
211 if (model_.isThisTimerAlerting(index) == true)
213 ui->SnoozeButton->setEnabled(true);
215 else ui->SnoozeButton->setDisabled(true);
220 void KitchenAlertMainWindow::snooze()
222 QModelIndex row = selectedRow();
223 if (row.isValid()) //If there was no row selected invalid row was returned
225 model_.snoozeTimer(row);
227 ui->SnoozeButton->setDisabled(true);
232 void KitchenAlertMainWindow::restart()
234 QModelIndex row = selectedRow(); //If there was no row selected invalid row was returned
238 model_.startTimer(row);
240 ui->SnoozeButton->setDisabled(true);
245 void KitchenAlertMainWindow::stop()
247 QModelIndex row = selectedRow();
248 if (row.isValid()) //If there was no row selected invalid row was returned
250 model_.stopTimer(row);
252 ui->SnoozeButton->setDisabled(true);
254 qDebug() << "Stopped.";
257 QModelIndex KitchenAlertMainWindow::selectedRow()
259 QModelIndexList chosenRows = ui->ComingAlertsTableView->selectionModel()->selectedRows();
261 //The selection mode used allows only one row to be selected at time, so we just take the first
262 //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
264 return chosenRows.value(0); //gives an invalid QModelIndex if the list is empty
267 void KitchenAlertMainWindow::openSelectSoundDialog()
269 SelectSoundDialog dialog;
270 if ( dialog.exec() == QDialog::Accepted) //if user pressed OK
272 if (dialog.isDefaultSoundChecked() == true)
273 alertSound_.setDefaultSound();
275 alertSound_.setSound(dialog.getSoundFileName());
277 //opening a dialog clears the selection so the selection dependen buttons must be disabled
279 disableSelectionDependentButtons();
282 void KitchenAlertMainWindow::openAbout()
284 QMessageBox::about(this,tr("About KitchenAlert"),tr("<p>Version 0.1.1"
285 "<p>Copyright © Heli Hyvättinen 2010"
286 "<p>License: General Public License v3"
287 "<p>Bugtracker and project page: https://garage.maemo.org/projects/kitchenalert/"));
290 bool KitchenAlertMainWindow::event(QEvent *event)
292 QMainWindow::event(event);
294 switch (event->type())
296 case QEvent::WindowActivate:
298 model_.setUpdateViewOnChanges(true);
301 case QEvent::WindowDeactivate:
302 model_.setUpdateViewOnChanges(false);
310 void KitchenAlertMainWindow::disableSelectionDependentButtons()
312 ui->DoneButton->setDisabled(true);
313 ui->SnoozeButton->setDisabled(true);
314 ui->RestartButton->setDisabled(true);
315 ui->RemoveButton->setDisabled(true);
318 void KitchenAlertMainWindow::remove()
320 QModelIndex row = selectedRow();
323 model_.removeTimer(row);
325 ui->ComingAlertsTableView->clearSelection();
326 disableSelectionDependentButtons();