36b11425cf99a1a841b1d6e4b10284106e49586c
[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 #include "choosetimersequencedialog.h"
35 #include "createtimersequencedialog.h"
36
37
38
39 #include <QDebug>
40 #include <QMessageBox>
41
42
43
44 KitchenAlertMainWindow::KitchenAlertMainWindow(QWidget *parent) :
45     QMainWindow(parent),
46     ui(new Ui::KitchenAlertMainWindow)
47     {
48     ui->setupUi(this);
49
50   connect(ui->LoadTimerScheduleButton, SIGNAL (pressed()), this, SLOT (openTimerSequence()));
51   connect(ui->CreateNewScheduleButton, SIGNAL (pressed()), this, SLOT (newTimerSequence()));
52
53   ui->ComingAlertsTableView->setModel(&model_);
54   ui->ComingAlertsTableView->setSelectionMode(QAbstractItemView::SingleSelection);
55   ui->ComingAlertsTableView->setSelectionBehavior(QAbstractItemView::SelectRows);
56
57
58   //Buttons used to reacting an alarm are hidden by default
59
60   ui->DoneButton->setDisabled(true);
61   ui->SnoozeButton->setDisabled(true);
62   ui->RestartButton->setDisabled(true);
63
64
65   connect(ui->ComingAlertsTableView->selectionModel(),SIGNAL(selectionChanged(QItemSelection,QItemSelection)),this,SLOT(timerSelected(QItemSelection,QItemSelection)));
66
67   connect(ui->DoneButton,SIGNAL(clicked()),this,SLOT(stop()));
68   connect(ui->RestartButton,SIGNAL(clicked()),this,SLOT(restart()));
69   connect(ui->SnoozeButton,SIGNAL(clicked()),this, SLOT(snooze()));
70
71
72     }
73
74 KitchenAlertMainWindow::~KitchenAlertMainWindow()
75 {
76     delete ui;
77 }
78
79 void KitchenAlertMainWindow::changeEvent(QEvent *e)
80 {
81     QMainWindow::changeEvent(e);
82     switch (e->type()) {
83     case QEvent::LanguageChange:
84         ui->retranslateUi(this);
85         break;
86     default:
87         break;
88
89     }
90
91 }
92
93 void KitchenAlertMainWindow::openTimerSequence()
94 {
95     ChooseTimerSequenceDialog opendialog;
96     opendialog.exec();
97
98
99
100 }
101
102 void KitchenAlertMainWindow::newTimerSequence()
103 {
104     CreateTimerSequenceDialog createdialog;
105
106
107     if (createdialog.exec() == QDialog::Accepted) //if user pressed OK
108     {
109
110
111        QList<Timer *>  alltimers = createdialog.getTimers();  //get user input from the dialog
112
113        Timer* timer1 = alltimers.at(0); // take first timer (currently the only one!)
114
115
116
117
118 //       ui->nextAlertText->setText( timer1->getAlertText());
119
120
121        timer1->start();
122        connect(timer1,SIGNAL(alert(QModelIndex)),this,SLOT(alert(QModelIndex)));
123
124
125
126         model_.addTimers(alltimers); // give timers to the model
127 //        ui->ComingAlertsTableView->update();
128
129
130
131     }
132     // if cancelled, do nothing
133
134
135
136 }
137
138
139 void KitchenAlertMainWindow::updateTime(int seconds)
140 {
141
142
143 //    ui->hoursLcdNumber->display(seconds/360);
144 //    ui->minutesLcdNumber->display((seconds%360)/60);
145 //    ui->secondsLcdNumber->display((seconds%60));
146
147
148
149 }
150
151 void KitchenAlertMainWindow::alert(QModelIndex indexOfAlerter)
152 {
153  //   QMessageBox::information ( NULL, "KitchenAlert","Alert!!!");
154
155     // The program is brought to front and activated when alerted
156
157     raise();
158     activateWindow();
159
160     // The alerting timer is selected
161     ui->ComingAlertsTableView->selectionModel()->select(QItemSelection(indexOfAlerter,indexOfAlerter),QItemSelectionModel::SelectCurrent | QItemSelectionModel::Rows );
162     qDebug() << "Should be selected now";
163
164
165
166     //Snooze button is enabled
167
168
169     ui->SnoozeButton->setEnabled(true);
170
171     //The alert sound is played
172
173
174
175
176         //TODO
177
178     alertSound_.play();
179
180 }
181
182
183 void KitchenAlertMainWindow::timerSelected(QItemSelection,QItemSelection)
184 {
185     ui->DoneButton->setEnabled(true);
186     ui->RestartButton->setEnabled(true);
187
188 }
189
190 void KitchenAlertMainWindow::snooze()
191 {
192
193     model_.snoozeTimer(selectedRow());
194     ui->SnoozeButton->setDisabled(true);
195     alertSound_.stop();
196
197 }
198
199 void KitchenAlertMainWindow::restart()
200 {
201
202     model_.startTimer(selectedRow());
203     ui->SnoozeButton->setDisabled(true);
204     alertSound_.stop();
205
206 }
207
208 void KitchenAlertMainWindow::stop()
209 {
210     model_.stopTimer(selectedRow());
211     ui->SnoozeButton->setDisabled(true);
212     alertSound_.stop();
213 }
214
215 QModelIndex KitchenAlertMainWindow::selectedRow()
216 {
217     QModelIndexList chosenRows = ui->ComingAlertsTableView->selectionModel()->selectedRows();
218
219     //The selection mode used allows only one row to be selected at time, so we just take the first
220     //There are indexes for all coloumns in the row in the list, but as we only use the row, it does not matter which one we take
221
222     return chosenRows.takeFirst();
223 }