Restored some changes that allow changing the alert sound when there are no alerts...
[kitchenalert] / src / kitchenalertmainwindow.cpp
1 /**************************************************************************
2
3         KitchenAlert
4
5         Copyright (C) 2010-2011  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 #include <QSettings>
46
47
48
49 KitchenAlertMainWindow::KitchenAlertMainWindow(QWidget *parent) :
50     QMainWindow(parent),
51     ui(new Ui::KitchenAlertMainWindow)
52     {
53     ui->setupUi(this);
54
55     setWindowIcon(QIcon(":/kitchenalert.png"));
56
57
58
59
60   connect(ui->CreateNewScheduleButton, SIGNAL (pressed()), this, SLOT (newTimerSequence()));
61
62
63   //alerts' tableview setup
64
65
66   ui->ComingAlertsTableView->setModel(&model_);
67   ui->ComingAlertsTableView->setSelectionMode(QAbstractItemView::SingleSelection);
68   ui->ComingAlertsTableView->setSelectionBehavior(QAbstractItemView::SelectRows);
69
70   ui->ComingAlertsTableView->horizontalHeader()->hide();
71 //  ui->ComingAlertsTableView->verticalHeader()->setVisible(true);
72
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);
77
78   ui->ComingAlertsTableView->verticalHeader()->setResizeMode(QHeaderView::ResizeToContents);
79
80
81   //Buttons used to reacting an alarm are hidden by default
82
83   disableSelectionDependentButtons();
84
85
86   connect(ui->ComingAlertsTableView->selectionModel(),SIGNAL(selectionChanged(QItemSelection,QItemSelection)),this,SLOT(timerSelected(QItemSelection,QItemSelection)));
87
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()));
92   // menu setup
93
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);
97
98   QAction * p_AboutAction = new QAction(tr("About"),this);
99   connect(p_AboutAction,SIGNAL(triggered()),this,SLOT(openAbout()));
100   menuBar()->addAction(p_AboutAction);
101     }
102
103 KitchenAlertMainWindow::~KitchenAlertMainWindow()
104 {
105     delete ui;
106 }
107
108 void KitchenAlertMainWindow::changeEvent(QEvent *e)
109 {
110     QMainWindow::changeEvent(e);
111     switch (e->type()) {
112     case QEvent::LanguageChange:
113         ui->retranslateUi(this);
114         break;
115     default:
116         break;
117
118     }
119
120 }
121
122
123 void KitchenAlertMainWindow::newTimerSequence()
124 {
125     CreateTimerSequenceDialog createdialog;
126
127
128     if (createdialog.exec() == QDialog::Accepted) //if user pressed OK
129     {
130
131
132        QList<Timer *>  alltimers = createdialog.getTimers();  //get user input from the dialog
133
134        Timer* timer1 = alltimers.at(0); // take first timer (currently the only one!)
135
136
137
138        connect(timer1,SIGNAL(alert(QModelIndex)),this,SLOT(alert(QModelIndex)));
139
140
141
142        connect(this,SIGNAL(defaultSoundEnabled()),timer1,SLOT(enableDefaultSound()));
143        connect(this,SIGNAL(soundChanged(QString)),timer1,SLOT(changeAlertSound(QString)));
144
145
146
147         model_.addTimers(alltimers); // give timers to the model, they are started automatically by default
148
149  //       ui->ComingAlertsTableView->resizeColumnsToContents();
150
151
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
154
155         if (!selectedRow().isValid())
156             disableSelectionDependentButtons();
157
158
159
160     }
161     // if cancelled, do nothing
162
163
164
165 }
166
167
168
169
170
171 void KitchenAlertMainWindow::alert(QModelIndex indexOfAlerter)
172 {
173
174     // The program is brought to front and activated when alerted
175
176
177     activateWindow();
178
179 // removing everything below does not solve the bug #6752!
180
181     raise();  //this may be unnecessary
182
183     // The alerting timer is selected
184     ui->ComingAlertsTableView->selectionModel()->select(QItemSelection(indexOfAlerter,indexOfAlerter),QItemSelectionModel::SelectCurrent | QItemSelectionModel::Rows );
185
186     //Scrolls the view so that the alerting timer is visible
187     ui->ComingAlertsTableView->scrollTo(indexOfAlerter);
188
189    // qDebug() << "Should be selected now";
190
191
192     //Snooze button is enabled
193
194
195     ui->SnoozeButton->setEnabled(true);
196 //qDebug ("Snooze on when alerting");
197
198 }
199
200
201 void KitchenAlertMainWindow::timerSelected(QItemSelection selected,QItemSelection)
202 {
203     ui->DoneButton->setEnabled(true);
204     ui->RestartButton->setEnabled(true);
205     ui->RemoveButton->setEnabled(true);
206
207
208     //snooze button enabled only when alerting
209     QModelIndexList indexes = selected.indexes();
210
211     //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
212     QModelIndex index = indexes.value(0);
213     if (index.isValid())
214     {
215         if (model_.isThisTimerAlerting(index) == true)
216         {
217              ui->SnoozeButton->setEnabled(true);
218 //qDebug() << "Snooze on";
219         }
220         else
221         {
222             ui->SnoozeButton->setDisabled(true);
223 //qDebug() << "Snooze off";
224         }
225     }
226
227 }
228
229 void KitchenAlertMainWindow::snooze()
230 {
231     QModelIndex row = selectedRow();
232     if (row.isValid()) //If there was no row selected invalid row was returned
233     {
234         model_.snoozeTimer(row);
235     }
236     ui->SnoozeButton->setDisabled(true);
237
238
239 }
240
241 void KitchenAlertMainWindow::restart()
242 {
243     QModelIndex row = selectedRow(); //If there was no row selected invalid row was returned
244     if (row.isValid())
245     {
246
247         model_.startTimer(row);
248     }
249
250
251    if (model_.isThisTimerAlerting(row) == false) //This has to be checked, because 00:00:00 alerts alert *before* the program execution reaches here
252     {
253         ui->SnoozeButton->setDisabled(true);
254     }
255  //   qDebug () << "disabled snooze because of restart";
256
257
258 }
259
260 void KitchenAlertMainWindow::stop()
261 {
262     QModelIndex row = selectedRow();
263     if (row.isValid()) //If there was no row selected invalid row was returned
264     {
265         model_.stopTimer(row);
266     }
267     ui->SnoozeButton->setDisabled(true);
268
269 }
270
271 QModelIndex KitchenAlertMainWindow::selectedRow()
272 {
273     //Returns the cells in row 0 that have the whole row selected (the selection mode used allows only selecting whole rows
274
275     QModelIndexList chosenRows = ui->ComingAlertsTableView->selectionModel()->selectedRows();
276
277     //The selection mode used allows only one row to be selected at time, so we just take the first
278
279
280     return chosenRows.value(0); //gives an invalid QModelIndex if the list is empty
281 }
282
283 void KitchenAlertMainWindow::openSelectSoundDialog()
284 {
285
286     SelectSoundDialog dialog;
287    if ( dialog.exec() == QDialog::Accepted) //if user pressed OK
288     {
289        QSettings settings ("KitchenAlert","KitchenAlert");
290       
291        if (dialog.isDefaultSoundChecked() == true)
292        { 
293          
294            settings.setValue("UseDefaultSound",true); 
295            emit defaultSoundEnabled();
296        }   
297       else
298        {
299            QString filename = dialog.getSoundFileName();
300            settings.setValue("UseDefaultSound",false);
301            settings.setValue("soundfile",filename);
302            emit soundChanged(filename);
303        }
304
305     }
306
307 }
308
309 void KitchenAlertMainWindow::openAbout()
310 {
311     QMessageBox::about(this,tr("About KitchenAlert"),tr("<p>Version %1"
312                                                         "<p>Copyright &copy; Heli Hyv&auml;ttinen 2010-2011"
313                                                          "<p>License: General Public License v3"
314                                                          "<p>Web page: http://kitchenalert.garage.maemo.org/"
315                                                          "<p>Bugtracker: https://garage.maemo.org/projects/kitchenalert/").arg(QApplication::applicationVersion()));
316 }
317
318 bool KitchenAlertMainWindow::event(QEvent *event)
319 {
320
321
322     switch (event->type())
323     {
324         case QEvent::WindowActivate:
325
326             model_.setUpdateViewOnChanges(true);
327 //            ui->debugLabel->setText("Returned to the application!");
328             break;
329
330        case QEvent::WindowDeactivate:
331             model_.setUpdateViewOnChanges(false);
332 //            ui->debugLabel->setText("");
333             break;
334
335        default:
336             break;
337
338     }
339
340     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!
341 }
342
343 void KitchenAlertMainWindow::disableSelectionDependentButtons()
344 {
345     ui->DoneButton->setDisabled(true);
346     ui->SnoozeButton->setDisabled(true);
347     ui->RestartButton->setDisabled(true);
348     ui->RemoveButton->setDisabled(true);
349
350 }
351
352 void KitchenAlertMainWindow::initializeAlertSound()
353 {
354     QSettings settings;
355
356     bool useDefaultSound = settings.value("UseDefaultSound",true).toBool();
357     QString filename = settings.value("soundfile","").toString();
358
359     if (useDefaultSound == true)
360     {
361         openSelectSoundDialog();
362     }
363     else if (filename.isEmpty())
364     {
365         openSelectSoundDialog();
366     }
367
368    QString currentFilename = settings.value("soundfile","").toString();
369
370    if (currentFilename.isEmpty())
371    {
372         ui->debugLabel->setText("<FONT color=red>No alert sound file set. Alert sound will not be played!</FONT>");
373
374    }
375
376 }
377
378 void KitchenAlertMainWindow::remove()
379 {
380     QModelIndex row = selectedRow();
381     if (row.isValid()) //If there was no row selected invalid row was returned
382     {
383         QString text = tr("Are you sure you want to remove this timer from the list:\n");
384         text.append((row.data().toString()));
385         if (QMessageBox::question(this,tr("Confirm timer removal"),text,QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes)
386         {
387             model_.removeTimer(row);
388         }
389     }
390     ui->SnoozeButton->setDisabled(true);
391 }