1 /**************************************************************************
5 Copyright (C) 2010 Heli Hyvättinen
7 This file is part of KitchenAlert.
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.
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.
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/>.
22 **************************************************************************/
28 #include "kitchenalertmainwindow.h"
29 #include "ui_kitchenalertmainwindow.h"
35 #include "createtimersequencedialog.h"
36 #include "selectsounddialog.h"
44 #include <QMessageBox>
45 #include <QFileDialog>
51 KitchenAlertMainWindow::KitchenAlertMainWindow(QWidget *parent) :
53 ui(new Ui::KitchenAlertMainWindow)
57 setWindowIcon(QIcon(":/icon64.png"));
59 //load sticky alerts to the model here...
63 //alerts' tableview setup
66 ui->ComingAlertsTableView->setModel(&model_);
67 ui->ComingAlertsTableView->setSelectionMode(QAbstractItemView::SingleSelection);
68 ui->ComingAlertsTableView->setSelectionBehavior(QAbstractItemView::SelectRows);
71 //Commented out for testing their potebtial effect for slowdown:
73 // ui->ComingAlertsTableView->horizontalHeader()->setResizeMode(QHeaderView::Fixed);
74 // ui->ComingAlertsTableView->horizontalHeader()->resizeSection(0,460);
75 // ui->ComingAlertsTableView->horizontalHeader()->resizeSection(1,140);
76 // ui->ComingAlertsTableView->horizontalHeader()->resizeSection(2,100);
78 // ui->ComingAlertsTableView->verticalHeader()->setResizeMode(QHeaderView::ResizeToContents);
83 // ui->ComingAlertsTableView->verticalHeader()->setDefaultSectionSize(40); //Needed with fixed cell height only
88 ui->ComingAlertsTableView->horizontalHeader()->hide();
89 ui->ComingAlertsTableView->setWordWrap(true);
92 //Buttons used when a timer is selected are disabled by default and enabled upon selection
94 disableSelectionDependentButtons();
96 connect(ui->ComingAlertsTableView->selectionModel(),SIGNAL(selectionChanged(QItemSelection,QItemSelection)),this,SLOT(timerSelected(QItemSelection,QItemSelection)));
98 //connect buttons to respective functions
99 connect(ui->CreateNewScheduleButton, SIGNAL (pressed()), this, SLOT (newTimerSequence()));
100 connect(ui->DoneButton,SIGNAL(clicked()),this,SLOT(stop()));
101 connect(ui->RestartButton,SIGNAL(clicked()),this,SLOT(restart()));
102 connect(ui->SnoozeButton,SIGNAL(clicked()),this, SLOT(snooze()));
103 connect(ui->RemoveButton,SIGNAL(clicked()),this,SLOT(remove()));
104 connect(ui->SaveButton,SIGNAL(clicked()),this,SLOT(saveTimer()));
105 connect(ui->loadButton,SIGNAL(clicked()),this,SLOT(loadTimer()));
109 QAction * p_SelectSoundAction = new QAction(tr("Select alert sound"),this);
110 connect(p_SelectSoundAction, SIGNAL(triggered()), this, SLOT(openSelectSoundDialog()));
111 menuBar()->addAction(p_SelectSoundAction);
113 QAction * p_AboutAction = new QAction(tr("About"),this);
114 connect(p_AboutAction,SIGNAL(triggered()),this,SLOT(openAbout()));
115 menuBar()->addAction(p_AboutAction);
118 KitchenAlertMainWindow::~KitchenAlertMainWindow()
123 void KitchenAlertMainWindow::changeEvent(QEvent *e)
125 QMainWindow::changeEvent(e);
127 case QEvent::LanguageChange:
128 ui->retranslateUi(this);
138 void KitchenAlertMainWindow::newTimerSequence()
140 CreateTimerSequenceDialog createdialog;
143 if (createdialog.exec() == QDialog::Accepted) //if user pressed OK
146 //get user input from the dialog
149 QList<Timer *> alltimers = createdialog.getTimers();
151 // take first timer (currently the only one!)
154 Timer* timer1 = alltimers.at(0);
160 connect(timer1,SIGNAL(alert(QModelIndex)),this,SLOT(alert(QModelIndex)));
163 //Disable buttons, as selection is cleared when view is refreshed to show the new timer
165 disableSelectionDependentButtons();
168 // give timers to the model
170 model_.addTimers(alltimers);
173 //start the timer when it's safely in the model (consider moving this to the model's addTimers function)
180 // if cancelled, do nothing
190 void KitchenAlertMainWindow::alert(QModelIndex indexOfAlerter)
193 // The program is brought to front and activated when alerted
198 // The alerting timer is selected
199 ui->ComingAlertsTableView->selectionModel()->select(QItemSelection(indexOfAlerter,indexOfAlerter),QItemSelectionModel::SelectCurrent | QItemSelectionModel::Rows );
200 // qDebug() << "Should be selected now";
203 //Snooze button is enabled
206 ui->SnoozeButton->setEnabled(true);
208 //The alert sound is played (consider moving this operation inside timer, as now stopping one alert will silence all alerting alerts)
215 void KitchenAlertMainWindow::timerSelected(QItemSelection selected,QItemSelection deselected)
217 ui->DoneButton->setEnabled(true);
218 ui->RestartButton->setEnabled(true);
219 ui->RemoveButton->setEnabled(true);
220 ui->SaveButton->setEnabled(true);
223 //enabled only when alerting
224 QModelIndexList indexes = selected.indexes();
226 //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
227 QModelIndex index = indexes.value(0);
230 if (model_.isThisTimerAlerting(index) == true)
232 ui->SnoozeButton->setEnabled(true);
234 else ui->SnoozeButton->setDisabled(true);
239 void KitchenAlertMainWindow::snooze()
241 QModelIndex row = selectedRow();
242 if (row.isValid()) //If there was no row selected invalid row was returned
244 model_.snoozeTimer(row);
246 ui->SnoozeButton->setDisabled(true);
251 void KitchenAlertMainWindow::restart()
253 QModelIndex row = selectedRow(); //If there was no row selected invalid row was returned
257 model_.startTimer(row);
259 ui->SnoozeButton->setDisabled(true);
264 void KitchenAlertMainWindow::stop()
266 QModelIndex row = selectedRow();
267 if (row.isValid()) //If there was no row selected invalid row was returned
269 model_.stopTimer(row);
271 ui->SnoozeButton->setDisabled(true);
273 qDebug() << "Stopped.";
276 QModelIndex KitchenAlertMainWindow::selectedRow()
278 QModelIndexList chosenRows = ui->ComingAlertsTableView->selectionModel()->selectedRows();
280 //The selection mode used allows only one row to be selected at time, so we just take the first
281 //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
283 return chosenRows.value(0); //gives an invalid QModelIndex if the list is empty
286 void KitchenAlertMainWindow::openSelectSoundDialog()
288 SelectSoundDialog dialog;
289 if ( dialog.exec() == QDialog::Accepted) //if user pressed OK
291 if (dialog.isDefaultSoundChecked() == true)
292 alertSound_.setDefaultSound();
294 alertSound_.setSound(dialog.getSoundFileName());
296 //opening a dialog clears the selection so the selection dependen buttons must be disabled
298 disableSelectionDependentButtons();
301 void KitchenAlertMainWindow::openAbout()
303 QMessageBox::about(this,tr("About KitchenAlert"),tr("<p>Version %1"
304 "<p>Copyright © Heli Hyvättinen 2010"
305 "<p>License: General Public License v3"
306 "<p>Bugtracker and project page: https://garage.maemo.org/projects/kitchenalert/").arg(QApplication::applicationVersion()));
309 bool KitchenAlertMainWindow::event(QEvent *event)
311 QMainWindow::event(event);
313 switch (event->type())
315 case QEvent::WindowActivate:
317 model_.setUpdateViewOnChanges(true);
320 case QEvent::WindowDeactivate:
321 model_.setUpdateViewOnChanges(false);
329 void KitchenAlertMainWindow::disableSelectionDependentButtons()
331 ui->DoneButton->setDisabled(true);
332 ui->SnoozeButton->setDisabled(true);
333 ui->RestartButton->setDisabled(true);
334 ui->RemoveButton->setDisabled(true);
335 ui->SaveButton->setDisabled(true);
338 void KitchenAlertMainWindow::remove()
340 QModelIndex row = selectedRow();
343 model_.removeTimer(row);
345 ui->ComingAlertsTableView->clearSelection();
346 disableSelectionDependentButtons();
350 void KitchenAlertMainWindow::saveTimer()
353 QModelIndex row = selectedRow();
355 if (row.isValid() == false) //If there was no row selected invalid row was returned
359 //file name is asked. As the filename will be appended, there's no point in confirming owerwrite here
360 QString filename = QFileDialog::getSaveFileName(this, "", "", "*.kitchenalert",NULL,QFileDialog::DontConfirmOverwrite);
362 disableSelectionDependentButtons();
364 qDebug() << filename;
366 if (filename.isEmpty()) //user cancelled the dialog (or gave an empty name)
371 if (!filename.endsWith(".kitchenalert"))
373 filename.append(".kitchenalert");
377 qDebug() << "filename appended to " << filename;
380 //MANUAL CONFIRMATION OF OWERWRITE
382 if ( QFile::exists(filename))
384 //ASK FOR CONFIRMATION
386 QString overwriteQuestion ("File ");
387 overwriteQuestion.append(filename);
388 overwriteQuestion.append(" already exists. Do you want to overwrite it?");
389 if (QMessageBox::question(this,"Confirm overwrite?", overwriteQuestion,QMessageBox::Yes | QMessageBox::No,QMessageBox::No) != QMessageBox::Yes)
399 QString errorMessage(tr("Cannot write to file "));
400 errorMessage.append(filename);
402 if (!model_.saveTimer(row,filename)) //Save the file, if not successful give an error message
404 QMessageBox::critical(this,tr("Save timer failed!"), errorMessage);
410 void KitchenAlertMainWindow::loadTimer()
412 QString filename = QFileDialog::getOpenFileName(this,"","",tr("KitchenAlert timer files (*.kitchenalert)"));
413 if (!filename.isEmpty())
416 // if (!filename.endsWith(".kitchenalert")) //not needed, the dialog won't let the user to select files not ending with ".kitchenalert"
418 // filename.append(".kitchenalert");
421 QString errorTitle(tr("Failed to load file "));
422 errorTitle.append(filename);
424 Timer * p_timer = new Timer();
425 if (!p_timer->load(filename))
427 QMessageBox::critical(this,errorTitle,tr("Unable to open file or not a valid KitchenAlert timer file."));
432 initializeTimer(p_timer);
437 void KitchenAlertMainWindow::initializeTimer(Timer *p_timer)
443 connect(p_timer,SIGNAL(alert(QModelIndex)),this,SLOT(alert(QModelIndex)));
446 //Disable buttons, as selection is cleared when view is refreshed to show the new timer
448 disableSelectionDependentButtons();
451 // give timers to the model (model wants list of timers now..)
453 QList<Timer *> timerList;
455 timerList.append(p_timer);
456 model_.addTimers(timerList);
459 //start the timer when it's safely in the model (consider moving this to the model's addTimers function)