bd1c1f2bbad28e4ffc8090350a256391c70c4f82
[kitchenalert] / src / kitchenalertmainwindow.cpp
1 /**************************************************************************
2
3         KitchenAlert
4
5         Copyright (C) 2010  Heli Hyvättinen
6
7         This file is part of KitchenAlert.
8
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.
13
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.
18
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/>.
21
22 **************************************************************************/
23
24
25
26
27
28 #include "kitchenalertmainwindow.h"
29 #include "ui_kitchenalertmainwindow.h"
30
31 #include <QString>
32 #include <QList>
33
34
35 #include "createtimersequencedialog.h"
36 #include "selectsounddialog.h"
37
38
39
40 #include <QDebug>
41
42 #include <QAction>
43 #include <QMenuBar>
44 #include <QMessageBox>
45
46
47
48 KitchenAlertMainWindow::KitchenAlertMainWindow(QWidget *parent) :
49     QMainWindow(parent),
50     ui(new Ui::KitchenAlertMainWindow)
51     {
52     ui->setupUi(this);
53
54
55
56
57   connect(ui->CreateNewScheduleButton, SIGNAL (pressed()), this, SLOT (newTimerSequence()));
58
59
60   //alerts' tableview setup
61
62
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();
68
69
70   //Buttons used to reacting an alarm are hidden by default
71
72   disableSelectionDependentButtons();
73
74
75   connect(ui->ComingAlertsTableView->selectionModel(),SIGNAL(selectionChanged(QItemSelection,QItemSelection)),this,SLOT(timerSelected(QItemSelection,QItemSelection)));
76
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()));
80
81   // menu setup
82
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);
86
87   QAction * p_AboutAction = new QAction(tr("About"),this);
88   connect(p_AboutAction,SIGNAL(triggered()),this,SLOT(openAbout()));
89   menuBar()->addAction(p_AboutAction);
90     }
91
92 KitchenAlertMainWindow::~KitchenAlertMainWindow()
93 {
94     delete ui;
95 }
96
97 void KitchenAlertMainWindow::changeEvent(QEvent *e)
98 {
99     QMainWindow::changeEvent(e);
100     switch (e->type()) {
101     case QEvent::LanguageChange:
102         ui->retranslateUi(this);
103         break;
104     default:
105         break;
106
107     }
108
109 }
110
111
112 void KitchenAlertMainWindow::newTimerSequence()
113 {
114     CreateTimerSequenceDialog createdialog;
115
116
117     if (createdialog.exec() == QDialog::Accepted) //if user pressed OK
118     {
119
120
121        QList<Timer *>  alltimers = createdialog.getTimers();  //get user input from the dialog
122
123        Timer* timer1 = alltimers.at(0); // take first timer (currently the only one!)
124
125
126        timer1->start();
127        connect(timer1,SIGNAL(alert(QModelIndex)),this,SLOT(alert(QModelIndex)));
128
129
130
131         model_.addTimers(alltimers); // give timers to the model
132
133  //       ui->ComingAlertsTableView->resizeColumnsToContents();
134
135
136         //Disable buttons, as selection is cleared when view is refreshed to show the new timer
137
138         disableSelectionDependentButtons();
139
140
141
142     }
143     // if cancelled, do nothing
144
145
146
147 }
148
149
150
151
152
153 void KitchenAlertMainWindow::alert(QModelIndex indexOfAlerter)
154 {
155
156     // The program is brought to front and activated when alerted
157
158     raise();
159     activateWindow();
160
161     // The alerting timer is selected
162     ui->ComingAlertsTableView->selectionModel()->select(QItemSelection(indexOfAlerter,indexOfAlerter),QItemSelectionModel::SelectCurrent | QItemSelectionModel::Rows );
163     qDebug() << "Should be selected now";
164
165
166     //Snooze button is enabled
167
168
169     ui->SnoozeButton->setEnabled(true);
170
171     //The alert sound is played
172
173     alertSound_.play();
174
175 }
176
177
178 void KitchenAlertMainWindow::timerSelected(QItemSelection selected,QItemSelection deselected)
179 {
180     ui->DoneButton->setEnabled(true);
181     ui->RestartButton->setEnabled(true);
182
183
184     //enabled only when alerting
185     QModelIndexList indexes = selected.indexes();
186
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);
189     if (index.isValid())
190     {
191         if (model_.isThisTimerAlerting(index) == true)
192         {
193              ui->SnoozeButton->setEnabled(true);
194         }
195         else ui->SnoozeButton->setDisabled(true);
196     }
197
198 }
199
200 void KitchenAlertMainWindow::snooze()
201 {
202     QModelIndex row = selectedRow();
203     if (row.isValid()) //If there was no row selected invalid row was returned
204     {
205         model_.snoozeTimer(row);
206     }
207     ui->SnoozeButton->setDisabled(true);
208     alertSound_.stop();
209
210 }
211
212 void KitchenAlertMainWindow::restart()
213 {
214     QModelIndex row = selectedRow(); //If there was no row selected invalid row was returned
215     if (row.isValid())
216     {
217
218         model_.startTimer(row);
219     }
220     ui->SnoozeButton->setDisabled(true);
221     alertSound_.stop();
222
223 }
224
225 void KitchenAlertMainWindow::stop()
226 {
227     QModelIndex row = selectedRow();
228     if (row.isValid()) //If there was no row selected invalid row was returned
229     {
230         model_.stopTimer(row);
231     }
232     ui->SnoozeButton->setDisabled(true);
233     alertSound_.stop();
234 }
235
236 QModelIndex KitchenAlertMainWindow::selectedRow()
237 {
238     QModelIndexList chosenRows = ui->ComingAlertsTableView->selectionModel()->selectedRows();
239
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
242
243     return chosenRows.value(0); //gives an invalid QModelIndex if the list is empty
244 }
245
246 void KitchenAlertMainWindow::openSelectSoundDialog()
247 {
248     SelectSoundDialog dialog;
249    if ( dialog.exec() == QDialog::Accepted) //if user pressed OK
250     {
251        if (dialog.isDefaultSoundChecked() == true)
252            alertSound_.setDefaultSound();
253        else
254         alertSound_.setSound(dialog.getSoundFileName());
255
256    //opening a dialog clears the selection so the selection dependen buttons must be disabled
257     }
258     disableSelectionDependentButtons();
259 }
260
261 void KitchenAlertMainWindow::openAbout()
262 {
263     QMessageBox::about(this,tr("About KitchenAlert"),tr("<p>Version 0.1"
264                                                         "<p>Copyright &copy; Heli Hyv&auml;ttinen 2010"
265                                                          "<p>License: General Public License v3"
266                                                          "<p>Bugtracker and project page: https://garage.maemo.org/projects/kitchenalert/"));
267 }
268
269 bool KitchenAlertMainWindow::event(QEvent *event)
270 {
271     QMainWindow::event(event);
272
273     switch (event->type())
274     {
275         case QEvent::WindowActivate:
276
277             model_.setUpdateViewOnChanges(true);
278               break;
279
280        case QEvent::WindowDeactivate:
281             model_.setUpdateViewOnChanges(false);
282             break;
283
284        default:
285             break;
286     }
287 }
288
289 void KitchenAlertMainWindow::disableSelectionDependentButtons()
290 {
291     ui->DoneButton->setDisabled(true);
292     ui->SnoozeButton->setDisabled(true);
293     ui->RestartButton->setDisabled(true);
294
295 }