Initial commit: added current sources
[kitchenalert] / src / kitchenalertmainwindow.cpp
1 /**************************************************************************
2
3         KitchenAlert v.0.01
4
5         Copyright (C) 2010  Heli Hyvättinen
6
7         This program is free software: you can redistribute it and/or modify
8         it under the terms of the GNU General Public License as published by
9         the Free Software Foundation, either version 3 of the License, or
10         (at your option) any later version.
11
12         This program is distributed in the hope that it will be useful,
13         but WITHOUT ANY WARRANTY; without even the implied warranty of
14         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15         GNU General Public License for more details.
16
17         You should have received a copy of the GNU General Public License
18         along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20 **************************************************************************/
21
22
23
24
25
26 #include "kitchenalertmainwindow.h"
27 #include "ui_kitchenalertmainwindow.h"
28
29 #include <QString>
30 #include <QList>
31
32 #include "choosetimersequencedialog.h"
33 #include "createtimersequencedialog.h"
34
35
36 #include <QDebug>
37 #include <QMessageBox>
38 #include <QMediaPlayer>
39
40 KitchenAlertMainWindow::KitchenAlertMainWindow(QWidget *parent) :
41     QMainWindow(parent),
42     ui(new Ui::KitchenAlertMainWindow)
43     {
44     ui->setupUi(this);
45
46   connect(ui->LoadTimerScheduleButton, SIGNAL (pressed()), this, SLOT (openTimerSequence()));
47   connect(ui->CreateNewScheduleButton, SIGNAL (pressed()), this, SLOT (newTimerSequence()));
48
49   ui->ComingAlertsTableView->setModel(&model_);
50   ui->ComingAlertsTableView->setSelectionMode(QAbstractItemView::SingleSelection);
51   ui->ComingAlertsTableView->setSelectionBehavior(QAbstractItemView::SelectRows);
52
53
54   //Buttons used to reacting an alarm are hidden by default
55
56   ui->DoneButton->setDisabled(true);
57   ui->SnoozeButton->setDisabled(true);
58   ui->RestartButton->setDisabled(true);
59
60
61   connect(ui->ComingAlertsTableView->selectionModel(),SIGNAL(selectionChanged(QItemSelection,QItemSelection)),this,SLOT(timerSelected(QItemSelection,QItemSelection)));
62
63   connect(ui->DoneButton,SIGNAL(clicked()),this,SLOT(stop()));
64   connect(ui->RestartButton,SIGNAL(clicked()),this,SLOT(restart()));
65   connect(ui->SnoozeButton,SIGNAL(clicked()),this, SLOT(snooze()));
66
67
68     }
69
70 KitchenAlertMainWindow::~KitchenAlertMainWindow()
71 {
72     delete ui;
73 }
74
75 void KitchenAlertMainWindow::changeEvent(QEvent *e)
76 {
77     QMainWindow::changeEvent(e);
78     switch (e->type()) {
79     case QEvent::LanguageChange:
80         ui->retranslateUi(this);
81         break;
82     default:
83         break;
84
85     }
86
87 }
88
89 void KitchenAlertMainWindow::openTimerSequence()
90 {
91     ChooseTimerSequenceDialog opendialog;
92     opendialog.exec();
93
94
95
96 }
97
98 void KitchenAlertMainWindow::newTimerSequence()
99 {
100     CreateTimerSequenceDialog createdialog;
101
102
103     if (createdialog.exec() == QDialog::Accepted) //if user pressed OK
104     {
105
106
107        QList<Timer *>  alltimers = createdialog.getTimers();  //get user input from the dialog
108
109        Timer* timer1 = alltimers.at(0); // take first timer (currently the only one!)
110
111
112
113
114 //       ui->nextAlertText->setText( timer1->getAlertText());
115
116
117        timer1->start();
118        connect(timer1,SIGNAL(alert(QModelIndex)),this,SLOT(alert(QModelIndex)));
119
120
121
122         model_.addTimers(alltimers); // give timers to the model
123 //        ui->ComingAlertsTableView->update();
124
125
126
127     }
128     // if cancelled, do nothing
129
130
131
132 }
133
134
135 void KitchenAlertMainWindow::updateTime(int seconds)
136 {
137
138
139 //    ui->hoursLcdNumber->display(seconds/360);
140 //    ui->minutesLcdNumber->display((seconds%360)/60);
141 //    ui->secondsLcdNumber->display((seconds%60));
142
143
144
145 }
146
147 void KitchenAlertMainWindow::alert(QModelIndex indexOfAlerter)
148 {
149  //   QMessageBox::information ( NULL, "KitchenAlert","Alert!!!");
150
151     // The program is brought to front and activated when alerted
152
153     raise();
154     activateWindow();
155
156     // The alerting timer is selected
157     ui->ComingAlertsTableView->selectionModel()->select(QItemSelection(indexOfAlerter,indexOfAlerter),QItemSelectionModel::SelectCurrent | QItemSelectionModel::Rows );
158     qDebug() << "Should be selected now";
159
160
161
162     //Snooze button is enabled
163
164
165     ui->SnoozeButton->setEnabled(true);
166
167     //The alert sound is played
168
169
170         //TODO
171
172
173
174 }
175
176
177 void KitchenAlertMainWindow::timerSelected(QItemSelection,QItemSelection)
178 {
179     ui->DoneButton->setEnabled(true);
180     ui->RestartButton->setEnabled(true);
181
182 }
183
184 void KitchenAlertMainWindow::snooze()
185 {
186
187     model_.snoozeTimer(selectedRow());
188     ui->SnoozeButton->setDisabled(true);
189
190 }
191
192 void KitchenAlertMainWindow::restart()
193 {
194
195     model_.startTimer(selectedRow());
196       ui->SnoozeButton->setDisabled(true);
197
198 }
199
200 void KitchenAlertMainWindow::stop()
201 {
202     model_.stopTimer(selectedRow());
203       ui->SnoozeButton->setDisabled(true);
204 }
205
206 QModelIndex KitchenAlertMainWindow::selectedRow()
207 {
208     QModelIndexList chosenRows = ui->ComingAlertsTableView->selectionModel()->selectedRows();
209
210     //The selection mode used allows only one row to be selected at time, so we just take the first
211     //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
212
213     return chosenRows.takeFirst();
214 }