Remove button, fixed column width, limited text & fixed 00:00:00
[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   //alerts' tableview setup
56
57
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);
65
66   ui->ComingAlertsTableView->verticalHeader()->setResizeMode(QHeaderView::Fixed);
67   ui->ComingAlertsTableView->verticalHeader()->setDefaultSectionSize(40);
68
69
70
71
72   ui->ComingAlertsTableView->horizontalHeader()->hide();
73   ui->ComingAlertsTableView->setWordWrap(true);
74
75
76   //Buttons used when a timer is selected are disabled by default and enabled upon selection
77
78   disableSelectionDependentButtons();
79
80   connect(ui->ComingAlertsTableView->selectionModel(),SIGNAL(selectionChanged(QItemSelection,QItemSelection)),this,SLOT(timerSelected(QItemSelection,QItemSelection)));
81
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()));
88
89   // menu setup
90
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);
94
95   QAction * p_AboutAction = new QAction(tr("About"),this);
96   connect(p_AboutAction,SIGNAL(triggered()),this,SLOT(openAbout()));
97   menuBar()->addAction(p_AboutAction);
98     }
99
100 KitchenAlertMainWindow::~KitchenAlertMainWindow()
101 {
102     delete ui;
103 }
104
105 void KitchenAlertMainWindow::changeEvent(QEvent *e)
106 {
107     QMainWindow::changeEvent(e);
108     switch (e->type()) {
109     case QEvent::LanguageChange:
110         ui->retranslateUi(this);
111         break;
112     default:
113         break;
114
115     }
116
117 }
118
119
120 void KitchenAlertMainWindow::newTimerSequence()
121 {
122     CreateTimerSequenceDialog createdialog;
123
124
125     if (createdialog.exec() == QDialog::Accepted) //if user pressed OK
126     {
127
128         //get user input from the dialog
129
130
131        QList<Timer *>  alltimers = createdialog.getTimers();
132
133        // take first timer (currently the only one!)
134
135
136        Timer* timer1 = alltimers.at(0);
137
138
139        //connect alert
140
141
142        connect(timer1,SIGNAL(alert(QModelIndex)),this,SLOT(alert(QModelIndex)));
143
144
145        //Disable buttons, as selection is cleared when view is refreshed to show the new timer
146
147        disableSelectionDependentButtons();
148
149
150        // give timers to the model
151
152        model_.addTimers(alltimers);
153
154
155        //start the timer when it's safely in the model (consider moving this to the model's addTimers function)
156
157
158        timer1->start();
159
160
161     }
162     // if cancelled, do nothing
163
164
165
166 }
167
168
169
170
171
172 void KitchenAlertMainWindow::alert(QModelIndex indexOfAlerter)
173 {
174
175     // The program is brought to front and activated when alerted
176
177     raise();
178     activateWindow();
179
180     // The alerting timer is selected
181     ui->ComingAlertsTableView->selectionModel()->select(QItemSelection(indexOfAlerter,indexOfAlerter),QItemSelectionModel::SelectCurrent | QItemSelectionModel::Rows );
182 //    qDebug() << "Should be selected now";
183
184
185     //Snooze button is enabled
186
187
188     ui->SnoozeButton->setEnabled(true);
189
190     //The alert sound is played (consider moving this operation inside timer, as now stopping one alert will silence all alerting alerts)
191
192     alertSound_.play();
193
194 }
195
196
197 void KitchenAlertMainWindow::timerSelected(QItemSelection selected,QItemSelection deselected)
198 {
199     ui->DoneButton->setEnabled(true);
200     ui->RestartButton->setEnabled(true);
201     ui->RemoveButton->setEnabled(true);
202
203
204     //enabled only when alerting
205     QModelIndexList indexes = selected.indexes();
206
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);
209     if (index.isValid())
210     {
211         if (model_.isThisTimerAlerting(index) == true)
212         {
213              ui->SnoozeButton->setEnabled(true);
214         }
215         else ui->SnoozeButton->setDisabled(true);
216     }
217
218 }
219
220 void KitchenAlertMainWindow::snooze()
221 {
222     QModelIndex row = selectedRow();
223     if (row.isValid()) //If there was no row selected invalid row was returned
224     {
225         model_.snoozeTimer(row);
226     }
227     ui->SnoozeButton->setDisabled(true);
228     alertSound_.stop();
229
230 }
231
232 void KitchenAlertMainWindow::restart()
233 {
234     QModelIndex row = selectedRow(); //If there was no row selected invalid row was returned
235     if (row.isValid())
236     {
237
238         model_.startTimer(row);
239     }
240     ui->SnoozeButton->setDisabled(true);
241     alertSound_.stop();
242
243 }
244
245 void KitchenAlertMainWindow::stop()
246 {
247     QModelIndex row = selectedRow();
248     if (row.isValid()) //If there was no row selected invalid row was returned
249     {
250         model_.stopTimer(row);
251     }
252     ui->SnoozeButton->setDisabled(true);
253     alertSound_.stop();
254     qDebug() << "Stopped.";
255 }
256
257 QModelIndex KitchenAlertMainWindow::selectedRow()
258 {
259     QModelIndexList chosenRows = ui->ComingAlertsTableView->selectionModel()->selectedRows();
260
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
263
264     return chosenRows.value(0); //gives an invalid QModelIndex if the list is empty
265 }
266
267 void KitchenAlertMainWindow::openSelectSoundDialog()
268 {
269     SelectSoundDialog dialog;
270    if ( dialog.exec() == QDialog::Accepted) //if user pressed OK
271     {
272        if (dialog.isDefaultSoundChecked() == true)
273            alertSound_.setDefaultSound();
274        else
275         alertSound_.setSound(dialog.getSoundFileName());
276
277    //opening a dialog clears the selection so the selection dependen buttons must be disabled
278     }
279     disableSelectionDependentButtons();
280 }
281
282 void KitchenAlertMainWindow::openAbout()
283 {
284     QMessageBox::about(this,tr("About KitchenAlert"),tr("<p>Version 0.1.1"
285                                                         "<p>Copyright &copy; Heli Hyv&auml;ttinen 2010"
286                                                          "<p>License: General Public License v3"
287                                                          "<p>Bugtracker and project page: https://garage.maemo.org/projects/kitchenalert/"));
288 }
289
290 bool KitchenAlertMainWindow::event(QEvent *event)
291 {
292     QMainWindow::event(event);
293
294     switch (event->type())
295     {
296         case QEvent::WindowActivate:
297
298             model_.setUpdateViewOnChanges(true);
299               break;
300
301        case QEvent::WindowDeactivate:
302             model_.setUpdateViewOnChanges(false);
303             break;
304
305        default:
306             break;
307     }
308 }
309
310 void KitchenAlertMainWindow::disableSelectionDependentButtons()
311 {
312     ui->DoneButton->setDisabled(true);
313     ui->SnoozeButton->setDisabled(true);
314     ui->RestartButton->setDisabled(true);
315     ui->RemoveButton->setDisabled(true);
316 }
317
318 void KitchenAlertMainWindow::remove()
319 {
320    QModelIndex row = selectedRow();
321    if (row.isValid())
322    {
323     model_.removeTimer(row);
324     alertSound_.stop();
325     ui->ComingAlertsTableView->clearSelection();
326     disableSelectionDependentButtons();
327    }
328 }