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>
48 const QString defaultSaveDirectory_ = "/home/user/MyDocs/KitchenAlert/";
51 KitchenAlertMainWindow::KitchenAlertMainWindow(QWidget *parent) :
53 ui(new Ui::KitchenAlertMainWindow)
58 setWindowIcon(QIcon(":/kitchenalert.png"));
60 //alerts' tableview setup
62 ui->ComingAlertsTableView->setModel(&model_);
63 ui->ComingAlertsTableView->setSelectionMode(QAbstractItemView::SingleSelection);
64 ui->ComingAlertsTableView->setSelectionBehavior(QAbstractItemView::SelectRows);
66 ui->ComingAlertsTableView->horizontalHeader()->hide();
67 // ui->ComingAlertsTableView->verticalHeader()->setVisible(true);
69 ui->ComingAlertsTableView->horizontalHeader()->setResizeMode(QHeaderView::Fixed);
70 ui->ComingAlertsTableView->horizontalHeader()->resizeSection(0,535);
71 ui->ComingAlertsTableView->horizontalHeader()->resizeSection(1,140);
72 ui->ComingAlertsTableView->horizontalHeader()->resizeSection(2,100);
74 ui->ComingAlertsTableView->verticalHeader()->setResizeMode(QHeaderView::ResizeToContents);
77 //Buttons used to reacting to an alarm are hidden by default
79 disableSelectionDependentButtons();
82 connect(ui->ComingAlertsTableView->selectionModel(),SIGNAL(selectionChanged(QItemSelection,QItemSelection)),this,SLOT(timerSelected(QItemSelection,QItemSelection)));
83 connect(ui->CreateNewScheduleButton, SIGNAL (clicked()), 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 connect(ui->SaveButton,SIGNAL(clicked()),this,SLOT(saveTimer()));
89 connect(ui->OpenButton,SIGNAL(clicked()),this,SLOT(loadTimer()));
93 QAction * p_SelectSoundAction = new QAction(tr("Select alert sound"),this);
94 connect(p_SelectSoundAction, SIGNAL(triggered()), this, SLOT(openSelectSoundDialog()));
95 menuBar()->addAction(p_SelectSoundAction);
97 QAction * p_AboutAction = new QAction(tr("About"),this);
98 connect(p_AboutAction,SIGNAL(triggered()),this,SLOT(openAbout()));
99 menuBar()->addAction(p_AboutAction);
102 KitchenAlertMainWindow::~KitchenAlertMainWindow()
107 void KitchenAlertMainWindow::changeEvent(QEvent *e)
109 QMainWindow::changeEvent(e);
111 case QEvent::LanguageChange:
112 ui->retranslateUi(this);
122 void KitchenAlertMainWindow::newTimerSequence()
124 CreateTimerSequenceDialog createdialog;
127 if (createdialog.exec() == QDialog::Accepted) //if user pressed OK
131 QList<Timer *> alltimers = createdialog.getTimers(); //get user input from the dialog
133 Timer* timer1 = alltimers.at(0); // take first timer (currently the only one!)
137 connect(timer1,SIGNAL(alert(QModelIndex)),this,SLOT(alert(QModelIndex)));
141 connect(this,SIGNAL(defaultSoundEnabled()),timer1,SLOT(enableDefaultSound()));
142 connect(this,SIGNAL(soundChanged(QString)),timer1,SLOT(changeAlertSound(QString)));
146 model_.addTimers(alltimers); // give timers to the model, they are started automatically by default
148 // ui->ComingAlertsTableView->resizeColumnsToContents();
151 //Disable buttons, as selection is cleared when view is refreshed to show the new timer
152 //But only if the timer has not already alerted and thus been selected
154 if (!selectedRow().isValid())
155 disableSelectionDependentButtons();
160 // if cancelled, do nothing
170 void KitchenAlertMainWindow::alert(QModelIndex indexOfAlerter)
173 // The program is brought to front and activated when alerted
178 // removing everything below does not solve the bug #6752!
180 raise(); //this may be unnecessary
182 // The alerting timer is selected
183 ui->ComingAlertsTableView->selectionModel()->select(QItemSelection(indexOfAlerter,indexOfAlerter),QItemSelectionModel::SelectCurrent | QItemSelectionModel::Rows );
185 //Scrolls the view so that the alerting timer is visible
186 ui->ComingAlertsTableView->scrollTo(indexOfAlerter);
188 // qDebug() << "Should be selected now";
191 //Snooze button is enabled
194 ui->SnoozeButton->setEnabled(true);
195 //qDebug ("Snooze on when alerting");
200 void KitchenAlertMainWindow::timerSelected(QItemSelection selected,QItemSelection)
202 ui->DoneButton->setEnabled(true);
203 ui->RestartButton->setEnabled(true);
204 ui->RemoveButton->setEnabled(true);
205 ui->SaveButton->setEnabled(true);
208 //snooze button enabled only when alerting
209 QModelIndexList indexes = selected.indexes();
211 //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
212 QModelIndex index = indexes.value(0);
215 if (model_.isThisTimerAlerting(index) == true)
217 ui->SnoozeButton->setEnabled(true);
218 //qDebug() << "Snooze on";
222 ui->SnoozeButton->setDisabled(true);
223 //qDebug() << "Snooze off";
229 void KitchenAlertMainWindow::snooze()
231 QModelIndex row = selectedRow();
232 if (row.isValid()) //If there was no row selected invalid row was returned
234 model_.snoozeTimer(row);
236 ui->SnoozeButton->setDisabled(true);
241 void KitchenAlertMainWindow::restart()
243 QModelIndex row = selectedRow(); //If there was no row selected invalid row was returned
247 model_.startTimer(row);
251 if (model_.isThisTimerAlerting(row) == false) //This has to be checked, because 00:00:00 alerts alert *before* the program execution reaches here
253 ui->SnoozeButton->setDisabled(true);
255 // qDebug () << "disabled snooze because of restart";
260 void KitchenAlertMainWindow::stop()
262 QModelIndex row = selectedRow();
263 if (row.isValid()) //If there was no row selected invalid row was returned
265 model_.stopTimer(row);
267 ui->SnoozeButton->setDisabled(true);
271 QModelIndex KitchenAlertMainWindow::selectedRow()
273 //Returns the cells in row 0 that have the whole row selected (the selection mode used allows only selecting whole rows
275 QModelIndexList chosenRows = ui->ComingAlertsTableView->selectionModel()->selectedRows();
277 //The selection mode used allows only one row to be selected at time, so we just take the first
280 return chosenRows.value(0); //gives an invalid QModelIndex if the list is empty
283 void KitchenAlertMainWindow::openSelectSoundDialog()
286 SelectSoundDialog dialog;
287 if ( dialog.exec() == QDialog::Accepted) //if user pressed OK
289 QSettings settings ("KitchenAlert","KitchenAlert");
291 if (dialog.isDefaultSoundChecked() == true)
294 settings.setValue("UseDefaultSound",true);
295 emit defaultSoundEnabled();
299 QString filename = dialog.getSoundFileName();
300 settings.setValue("UseDefaultSound",false);
301 settings.setValue("soundfile",filename);
302 emit soundChanged(filename);
309 void KitchenAlertMainWindow::openAbout()
311 QMessageBox::about(this,tr("About KitchenAlert"),tr("<p>Version %1"
312 "<p>Copyright © Heli Hyvättinen 2010-2011"
313 "<p>License: General Public License v3"
314 "<p>Web page: http://kitchenalert.garage.maemo.org/"
315 "<p>Bugtracker: https://garage.maemo.org/projects/kitchenalert/").arg(QApplication::applicationVersion()));
318 bool KitchenAlertMainWindow::event(QEvent *event)
322 switch (event->type())
324 case QEvent::WindowActivate:
326 model_.setUpdateViewOnChanges(true);
327 // ui->debugLabel->setText("Returned to the application!");
330 case QEvent::WindowDeactivate:
331 model_.setUpdateViewOnChanges(false);
332 // ui->debugLabel->setText("");
340 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!
343 void KitchenAlertMainWindow::disableSelectionDependentButtons()
345 ui->DoneButton->setDisabled(true);
346 ui->SnoozeButton->setDisabled(true);
347 ui->RestartButton->setDisabled(true);
348 ui->RemoveButton->setDisabled(true);
349 ui->SaveButton->setDisabled(true);
354 void KitchenAlertMainWindow::initializeAlertSound()
358 bool useDefaultSound = settings.value("UseDefaultSound",true).toBool();
359 QString filename = settings.value("soundfile","").toString();
361 if (useDefaultSound == true)
363 openSelectSoundDialog();
365 else if (filename.isEmpty())
367 openSelectSoundDialog();
370 QString currentFilename = settings.value("soundfile","").toString();
372 if (currentFilename.isEmpty())
374 ui->debugLabel->setText("<FONT color=red>No alert sound file set. Alert sound will not be played!</FONT>");
380 void KitchenAlertMainWindow::remove()
382 QModelIndex row = selectedRow();
383 if (row.isValid()) //If there was no row selected invalid row was returned
385 QString text = tr("Are you sure you want to remove this timer from the list:\n");
386 text.append((row.data().toString()));
387 if (QMessageBox::question(this,tr("Confirm timer removal"),text,QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes)
389 model_.removeTimer(row);
390 ui->ComingAlertsTableView->clearSelection();
391 disableSelectionDependentButtons();
398 void KitchenAlertMainWindow::saveTimer()
401 QModelIndex row = selectedRow();
403 if (row.isValid() == false) //If there was no row selected invalid row was returned
407 //file name is asked. As the filename will be appended, there's no point in confirming owerwrite here
408 QString filename = QFileDialog::getSaveFileName(this, "", defaultSaveDirectory_, "*.kitchenalert",NULL,QFileDialog::DontConfirmOverwrite);
412 // qDebug() << filename;
414 if (filename.isEmpty()) //user cancelled the dialog (or gave an empty name)
419 if (!filename.endsWith(".kitchenalert"))
421 filename.append(".kitchenalert");
425 // qDebug() << "filename appended to " << filename;
428 //MANUAL CONFIRMATION OF OWERWRITE
430 if ( QFile::exists(filename))
432 //ASK FOR CONFIRMATION
434 QString overwriteQuestion ("File ");
435 overwriteQuestion.append(filename);
436 overwriteQuestion.append(" already exists. Do you want to overwrite it?");
437 if (QMessageBox::question(this,"Confirm overwrite?", overwriteQuestion,QMessageBox::Yes | QMessageBox::No,QMessageBox::No) != QMessageBox::Yes)
446 QString errorMessage(tr("Cannot write to file "));
447 errorMessage.append(filename);
449 if (!model_.saveTimer(row,filename)) //Save the file, if not successful give an error message
451 QMessageBox::critical(this,tr("Save timer failed!"), errorMessage);
457 void KitchenAlertMainWindow::loadTimer()
459 QString filename = QFileDialog::getOpenFileName(this,"",defaultSaveDirectory_,tr("KitchenAlert timer files (*.kitchenalert)"));
460 if (!filename.isEmpty())
463 // if (!filename.endsWith(".kitchenalert")) //not needed, the dialog won't let the user to select files not ending with ".kitchenalert"
465 // filename.append(".kitchenalert");
468 QString errorTitle(tr("Failed to load file "));
469 errorTitle.append(filename);
471 Timer * p_timer = new Timer();
472 if (!p_timer->load(filename))
474 QMessageBox::critical(this,errorTitle,tr("Unable to open file or not a valid KitchenAlert timer file."));
479 initializeTimer(p_timer);
483 void KitchenAlertMainWindow::initializeTimer(Timer *p_timer)
489 connect(p_timer,SIGNAL(alert(QModelIndex)),this,SLOT(alert(QModelIndex)));
492 //connect change sound functions
494 connect(this,SIGNAL(defaultSoundEnabled()),p_timer,SLOT(enableDefaultSound()));
495 connect(this,SIGNAL(soundChanged(QString)),p_timer,SLOT(changeAlertSound(QString)));
498 //Disable buttons, as selection is cleared when view is refreshed to show the new timer
500 disableSelectionDependentButtons();
503 // give timers to the model (model wants list of timers now..)
505 QList<Timer *> timerList;
507 timerList.append(p_timer);
508 model_.addTimers(timerList,true); //timer gets started in the model