1 /**************************************************************************
5 Copyright (C) 2010-2011 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>
46 #include <QFileDialog>
51 KitchenAlertMainWindow::KitchenAlertMainWindow(QWidget *parent) :
53 ui(new Ui::KitchenAlertMainWindow)
56 defaultSaveDirectory_ = "/home/user/MyDocs/KitchenAlert";
60 setWindowIcon(QIcon(":/kitchenalert.png"));
62 //alerts' tableview setup
64 ui->ComingAlertsTableView->setModel(&model_);
65 ui->ComingAlertsTableView->setSelectionMode(QAbstractItemView::SingleSelection);
66 ui->ComingAlertsTableView->setSelectionBehavior(QAbstractItemView::SelectRows);
68 ui->ComingAlertsTableView->horizontalHeader()->hide();
69 // ui->ComingAlertsTableView->verticalHeader()->setVisible(true);
71 ui->ComingAlertsTableView->horizontalHeader()->setResizeMode(QHeaderView::Fixed);
72 ui->ComingAlertsTableView->horizontalHeader()->resizeSection(0,535);
73 ui->ComingAlertsTableView->horizontalHeader()->resizeSection(1,140);
74 ui->ComingAlertsTableView->horizontalHeader()->resizeSection(2,100);
76 ui->ComingAlertsTableView->verticalHeader()->setResizeMode(QHeaderView::ResizeToContents);
79 //Buttons used to reacting to an alarm are hidden by default
81 disableSelectionDependentButtons();
84 connect(ui->ComingAlertsTableView->selectionModel(),SIGNAL(selectionChanged(QItemSelection,QItemSelection)),this,SLOT(timerSelected(QItemSelection,QItemSelection)));
85 connect(ui->CreateNewScheduleButton, SIGNAL (clicked()), this, SLOT (newTimerSequence()));
86 connect(ui->DoneButton,SIGNAL(clicked()),this,SLOT(stop()));
87 connect(ui->RestartButton,SIGNAL(clicked()),this,SLOT(restart()));
88 connect(ui->SnoozeButton,SIGNAL(clicked()),this, SLOT(snooze()));
89 connect(ui->RemoveButton,SIGNAL(clicked()),this,SLOT(remove()));
90 connect(ui->SaveButton,SIGNAL(clicked()),this,SLOT(saveTimer()));
91 connect(ui->OpenButton,SIGNAL(clicked()),this,SLOT(loadTimer()));
95 QAction * p_SelectSoundAction = new QAction(tr("Select alert sound"),this);
96 connect(p_SelectSoundAction, SIGNAL(triggered()), this, SLOT(openSelectSoundDialog()));
97 menuBar()->addAction(p_SelectSoundAction);
99 QAction * p_AboutAction = new QAction(tr("About"),this);
100 connect(p_AboutAction,SIGNAL(triggered()),this,SLOT(openAbout()));
101 menuBar()->addAction(p_AboutAction);
104 KitchenAlertMainWindow::~KitchenAlertMainWindow()
109 void KitchenAlertMainWindow::changeEvent(QEvent *e)
111 QMainWindow::changeEvent(e);
113 case QEvent::LanguageChange:
114 ui->retranslateUi(this);
124 void KitchenAlertMainWindow::newTimerSequence()
126 CreateTimerSequenceDialog createdialog;
129 if (createdialog.exec() == QDialog::Accepted) //if user pressed OK
133 QList<Timer *> alltimers = createdialog.getTimers(); //get user input from the dialog
135 Timer* timer1 = alltimers.at(0); // take first timer (currently the only one!)
139 connect(timer1,SIGNAL(alert(QModelIndex)),this,SLOT(alert(QModelIndex)));
143 connect(this,SIGNAL(defaultSoundEnabled()),timer1,SLOT(enableDefaultSound()));
144 connect(this,SIGNAL(soundChanged(QString)),timer1,SLOT(changeAlertSound(QString)));
148 model_.addTimers(alltimers); // give timers to the model, they are started automatically by default
150 // ui->ComingAlertsTableView->resizeColumnsToContents();
153 //Disable buttons, as selection is cleared when view is refreshed to show the new timer
154 //But only if the timer has not already alerted and thus been selected
156 if (!selectedRow().isValid())
157 disableSelectionDependentButtons();
162 // if cancelled, do nothing
172 void KitchenAlertMainWindow::alert(QModelIndex indexOfAlerter)
175 // The program is brought to front and activated when alerted
180 // removing everything below does not solve the bug #6752!
182 raise(); //this may be unnecessary
184 // The alerting timer is selected
185 ui->ComingAlertsTableView->selectionModel()->select(QItemSelection(indexOfAlerter,indexOfAlerter),QItemSelectionModel::SelectCurrent | QItemSelectionModel::Rows );
187 //Scrolls the view so that the alerting timer is visible
188 ui->ComingAlertsTableView->scrollTo(indexOfAlerter);
190 // qDebug() << "Should be selected now";
193 //Snooze button is enabled
196 ui->SnoozeButton->setEnabled(true);
197 //qDebug ("Snooze on when alerting");
202 void KitchenAlertMainWindow::timerSelected(QItemSelection selected,QItemSelection)
204 ui->DoneButton->setEnabled(true);
205 ui->RestartButton->setEnabled(true);
206 ui->RemoveButton->setEnabled(true);
207 ui->SaveButton->setEnabled(true);
210 //snooze button enabled only when alerting
211 QModelIndexList indexes = selected.indexes();
213 //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
214 QModelIndex index = indexes.value(0);
217 if (model_.isThisTimerAlerting(index) == true)
219 ui->SnoozeButton->setEnabled(true);
220 //qDebug() << "Snooze on";
224 ui->SnoozeButton->setDisabled(true);
225 //qDebug() << "Snooze off";
231 void KitchenAlertMainWindow::snooze()
233 QModelIndex row = selectedRow();
234 if (row.isValid()) //If there was no row selected invalid row was returned
236 model_.snoozeTimer(row);
238 ui->SnoozeButton->setDisabled(true);
243 void KitchenAlertMainWindow::restart()
245 QModelIndex row = selectedRow(); //If there was no row selected invalid row was returned
249 model_.startTimer(row);
253 if (model_.isThisTimerAlerting(row) == false) //This has to be checked, because 00:00:00 alerts alert *before* the program execution reaches here
255 ui->SnoozeButton->setDisabled(true);
257 // qDebug () << "disabled snooze because of restart";
262 void KitchenAlertMainWindow::stop()
264 QModelIndex row = selectedRow();
265 if (row.isValid()) //If there was no row selected invalid row was returned
267 model_.stopTimer(row);
269 ui->SnoozeButton->setDisabled(true);
273 QModelIndex KitchenAlertMainWindow::selectedRow()
275 //Returns the cells in row 0 that have the whole row selected (the selection mode used allows only selecting whole rows
277 QModelIndexList chosenRows = ui->ComingAlertsTableView->selectionModel()->selectedRows();
279 //The selection mode used allows only one row to be selected at time, so we just take the first
282 return chosenRows.value(0); //gives an invalid QModelIndex if the list is empty
285 void KitchenAlertMainWindow::openSelectSoundDialog()
288 SelectSoundDialog dialog;
289 if ( dialog.exec() == QDialog::Accepted) //if user pressed OK
291 QSettings settings ("KitchenAlert","KitchenAlert");
293 if (dialog.isDefaultSoundChecked() == true)
296 settings.setValue("UseDefaultSound",true);
297 emit defaultSoundEnabled();
301 QString filename = dialog.getSoundFileName();
302 settings.setValue("UseDefaultSound",false);
303 settings.setValue("soundfile",filename);
304 emit soundChanged(filename);
311 void KitchenAlertMainWindow::openAbout()
313 QMessageBox::about(this,tr("About KitchenAlert"),tr("<p>Version %1"
314 "<p>Copyright © Heli Hyvättinen 2010-2011"
315 "<p>License: General Public License v3"
316 "<p>Web page: http://kitchenalert.garage.maemo.org/"
317 "<p>Bugtracker: https://garage.maemo.org/projects/kitchenalert/").arg(QApplication::applicationVersion()));
320 bool KitchenAlertMainWindow::event(QEvent *event)
324 switch (event->type())
326 case QEvent::WindowActivate:
328 model_.setUpdateViewOnChanges(true);
329 // ui->debugLabel->setText("Returned to the application!");
332 case QEvent::WindowDeactivate:
333 model_.setUpdateViewOnChanges(false);
334 // ui->debugLabel->setText("");
342 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!
345 void KitchenAlertMainWindow::disableSelectionDependentButtons()
347 ui->DoneButton->setDisabled(true);
348 ui->SnoozeButton->setDisabled(true);
349 ui->RestartButton->setDisabled(true);
350 ui->RemoveButton->setDisabled(true);
351 ui->SaveButton->setDisabled(true);
356 void KitchenAlertMainWindow::initializeAlertSound()
360 bool useDefaultSound = settings.value("UseDefaultSound",true).toBool();
361 QString filename = settings.value("soundfile","").toString();
363 if (useDefaultSound == true)
365 openSelectSoundDialog();
367 else if (filename.isEmpty())
369 openSelectSoundDialog();
372 QString currentFilename = settings.value("soundfile","").toString();
374 if (currentFilename.isEmpty())
376 ui->debugLabel->setText("<FONT color=red>No alert sound file set. Alert sound will not be played!</FONT>");
382 void KitchenAlertMainWindow::remove()
384 QModelIndex row = selectedRow();
385 if (row.isValid()) //If there was no row selected invalid row was returned
387 QString text = tr("Are you sure you want to remove this timer from the list:\n");
388 text.append((row.data().toString()));
389 if (QMessageBox::question(this,tr("Confirm timer removal"),text,QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes)
391 model_.removeTimer(row);
392 ui->ComingAlertsTableView->clearSelection();
393 disableSelectionDependentButtons();
400 void KitchenAlertMainWindow::saveTimer()
403 QModelIndex row = selectedRow();
405 if (row.isValid() == false) //If there was no row selected invalid row was returned
408 //For consistency with loading, opens MyDocs if default directory has been removed
410 QString startDirectory;
412 if (QFile(defaultSaveDirectory_).exists())
414 startDirectory = defaultSaveDirectory_;
418 startDirectory = "/home/user/MyDocs";
419 qDebug () << "default save directory not found";
423 //file name is asked. As the filename will be appended, there's no point in confirming owerwrite here
424 QString filename = QFileDialog::getSaveFileName(this, "", startDirectory, "*.kitchenalert",NULL,QFileDialog::DontConfirmOverwrite);
428 // qDebug() << filename;
430 if (filename.isEmpty()) //user cancelled the dialog (or gave an empty name)
435 if (!filename.endsWith(".kitchenalert"))
437 filename.append(".kitchenalert");
441 // qDebug() << "filename appended to " << filename;
444 //MANUAL CONFIRMATION OF OWERWRITE
446 if ( QFile::exists(filename))
448 //ASK FOR CONFIRMATION
450 QString overwriteQuestion ("File ");
451 overwriteQuestion.append(filename);
452 overwriteQuestion.append(" already exists. Do you want to overwrite it?");
453 if (QMessageBox::question(this,"Confirm overwrite?", overwriteQuestion,QMessageBox::Yes | QMessageBox::No,QMessageBox::No) != QMessageBox::Yes)
462 QString errorMessage(tr("Cannot write to file "));
463 errorMessage.append(filename);
465 if (!model_.saveTimer(row,filename)) //Save the file, if not successful give an error message
467 QMessageBox::critical(this,tr("Save timer failed!"), errorMessage);
473 void KitchenAlertMainWindow::loadTimer()
478 //Get the filename to open with a dialog
480 QString filename = QFileDialog::getOpenFileName(this,"",defaultSaveDirectory_,tr("KitchenAlert timer files (*.kitchenalert)"));
481 if (!filename.isEmpty())
484 // if (!filename.endsWith(".kitchenalert")) //not needed, the dialog won't let the user to select files not ending with ".kitchenalert"
486 // filename.append(".kitchenalert");
489 QString errorTitle(tr("Failed to load file "));
490 errorTitle.append(filename);
492 Timer * p_timer = new Timer();
493 if (!p_timer->load(filename))
495 QMessageBox::critical(this,errorTitle,tr("Unable to open file or not a valid KitchenAlert timer file."));
500 initializeTimer(p_timer);
504 void KitchenAlertMainWindow::initializeTimer(Timer *p_timer)
510 connect(p_timer,SIGNAL(alert(QModelIndex)),this,SLOT(alert(QModelIndex)));
513 //connect change sound functions
515 connect(this,SIGNAL(defaultSoundEnabled()),p_timer,SLOT(enableDefaultSound()));
516 connect(this,SIGNAL(soundChanged(QString)),p_timer,SLOT(changeAlertSound(QString)));
519 //Disable buttons, as selection is cleared when view is refreshed to show the new timer
521 disableSelectionDependentButtons();
524 // give timers to the model (model wants list of timers now..)
526 QList<Timer *> timerList;
528 timerList.append(p_timer);
529 model_.addTimers(timerList,true); //timer gets started in the model