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>
50 KitchenAlertMainWindow::KitchenAlertMainWindow(QWidget *parent) :
52 ui(new Ui::KitchenAlertMainWindow)
56 setWindowIcon(QIcon(":/kitchenalert.png"));
61 connect(ui->CreateNewScheduleButton, SIGNAL (pressed()), this, SLOT (newTimerSequence()));
64 //alerts' tableview setup
67 ui->ComingAlertsTableView->setModel(&model_);
68 ui->ComingAlertsTableView->setSelectionMode(QAbstractItemView::SingleSelection);
69 ui->ComingAlertsTableView->setSelectionBehavior(QAbstractItemView::SelectRows);
71 ui->ComingAlertsTableView->horizontalHeader()->hide();
72 // ui->ComingAlertsTableView->verticalHeader()->setVisible(true);
74 ui->ComingAlertsTableView->horizontalHeader()->setResizeMode(QHeaderView::Fixed);
75 ui->ComingAlertsTableView->horizontalHeader()->resizeSection(0,535);
76 ui->ComingAlertsTableView->horizontalHeader()->resizeSection(1,140);
77 ui->ComingAlertsTableView->horizontalHeader()->resizeSection(2,100);
79 ui->ComingAlertsTableView->verticalHeader()->setResizeMode(QHeaderView::ResizeToContents);
82 //Buttons used to reacting an alarm are hidden by default
84 disableSelectionDependentButtons();
87 connect(ui->ComingAlertsTableView->selectionModel(),SIGNAL(selectionChanged(QItemSelection,QItemSelection)),this,SLOT(timerSelected(QItemSelection,QItemSelection)));
89 connect(ui->DoneButton,SIGNAL(clicked()),this,SLOT(stop()));
90 connect(ui->RestartButton,SIGNAL(clicked()),this,SLOT(restart()));
91 connect(ui->SnoozeButton,SIGNAL(clicked()),this, SLOT(snooze()));
92 connect(ui->RemoveButton,SIGNAL(clicked()),this,SLOT(remove()));
93 connect(ui->SaveButton,SIGNAL(clicked()),this,SLOT(saveTimer()));
94 connect(ui->OpenButton,SIGNAL(clicked()),this,SLOT(loadTimer()));
97 QAction * p_SelectSoundAction = new QAction(tr("Select alert sound"),this);
98 connect(p_SelectSoundAction, SIGNAL(triggered()), this, SLOT(openSelectSoundDialog()));
99 menuBar()->addAction(p_SelectSoundAction);
101 QAction * p_AboutAction = new QAction(tr("About"),this);
102 connect(p_AboutAction,SIGNAL(triggered()),this,SLOT(openAbout()));
103 menuBar()->addAction(p_AboutAction);
106 KitchenAlertMainWindow::~KitchenAlertMainWindow()
111 void KitchenAlertMainWindow::changeEvent(QEvent *e)
113 QMainWindow::changeEvent(e);
115 case QEvent::LanguageChange:
116 ui->retranslateUi(this);
126 void KitchenAlertMainWindow::newTimerSequence()
128 CreateTimerSequenceDialog createdialog;
131 if (createdialog.exec() == QDialog::Accepted) //if user pressed OK
135 QList<Timer *> alltimers = createdialog.getTimers(); //get user input from the dialog
137 Timer* timer1 = alltimers.at(0); // take first timer (currently the only one!)
141 connect(timer1,SIGNAL(alert(QModelIndex)),this,SLOT(alert(QModelIndex)));
145 connect(this,SIGNAL(defaultSoundEnabled()),timer1,SLOT(enableDefaultSound()));
146 connect(this,SIGNAL(soundChanged(QString)),timer1,SLOT(changeAlertSound(QString)));
150 model_.addTimers(alltimers); // give timers to the model, they are started automatically by default
152 // ui->ComingAlertsTableView->resizeColumnsToContents();
155 //Disable buttons, as selection is cleared when view is refreshed to show the new timer
156 //But only if the timer has not already alerted and thus been selected
158 if (!selectedRow().isValid())
159 disableSelectionDependentButtons();
164 // if cancelled, do nothing
174 void KitchenAlertMainWindow::alert(QModelIndex indexOfAlerter)
177 // The program is brought to front and activated when alerted
182 // removing everything below does not solve the bug #6752!
184 raise(); //this may be unnecessary
186 // The alerting timer is selected
187 ui->ComingAlertsTableView->selectionModel()->select(QItemSelection(indexOfAlerter,indexOfAlerter),QItemSelectionModel::SelectCurrent | QItemSelectionModel::Rows );
189 //Scrolls the view so that the alerting timer is visible
190 ui->ComingAlertsTableView->scrollTo(indexOfAlerter);
192 // qDebug() << "Should be selected now";
195 //Snooze button is enabled
198 ui->SnoozeButton->setEnabled(true);
199 //qDebug ("Snooze on when alerting");
204 void KitchenAlertMainWindow::timerSelected(QItemSelection selected,QItemSelection)
206 ui->DoneButton->setEnabled(true);
207 ui->RestartButton->setEnabled(true);
208 ui->RemoveButton->setEnabled(true);
211 //snooze button enabled only when alerting
212 QModelIndexList indexes = selected.indexes();
214 //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
215 QModelIndex index = indexes.value(0);
218 if (model_.isThisTimerAlerting(index) == true)
220 ui->SnoozeButton->setEnabled(true);
221 //qDebug() << "Snooze on";
225 ui->SnoozeButton->setDisabled(true);
226 //qDebug() << "Snooze off";
232 void KitchenAlertMainWindow::snooze()
234 QModelIndex row = selectedRow();
235 if (row.isValid()) //If there was no row selected invalid row was returned
237 model_.snoozeTimer(row);
239 ui->SnoozeButton->setDisabled(true);
244 void KitchenAlertMainWindow::restart()
246 QModelIndex row = selectedRow(); //If there was no row selected invalid row was returned
250 model_.startTimer(row);
254 if (model_.isThisTimerAlerting(row) == false) //This has to be checked, because 00:00:00 alerts alert *before* the program execution reaches here
256 ui->SnoozeButton->setDisabled(true);
258 // qDebug () << "disabled snooze because of restart";
263 void KitchenAlertMainWindow::stop()
265 QModelIndex row = selectedRow();
266 if (row.isValid()) //If there was no row selected invalid row was returned
268 model_.stopTimer(row);
270 ui->SnoozeButton->setDisabled(true);
274 QModelIndex KitchenAlertMainWindow::selectedRow()
276 //Returns the cells in row 0 that have the whole row selected (the selection mode used allows only selecting whole rows
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
283 return chosenRows.value(0); //gives an invalid QModelIndex if the list is empty
286 void KitchenAlertMainWindow::openSelectSoundDialog()
289 SelectSoundDialog dialog;
290 if ( dialog.exec() == QDialog::Accepted) //if user pressed OK
292 QSettings settings ("KitchenAlert","KitchenAlert");
294 if (dialog.isDefaultSoundChecked() == true)
297 settings.setValue("UseDefaultSound",true);
298 emit defaultSoundEnabled();
302 QString filename = dialog.getSoundFileName();
303 settings.setValue("UseDefaultSound",false);
304 settings.setValue("soundfile",filename);
305 emit soundChanged(filename);
312 void KitchenAlertMainWindow::openAbout()
314 QMessageBox::about(this,tr("About KitchenAlert"),tr("<p>Version %1"
315 "<p>Copyright © Heli Hyvättinen 2010-2011"
316 "<p>License: General Public License v3"
317 "<p>Web page: http://kitchenalert.garage.maemo.org/"
318 "<p>Bugtracker: https://garage.maemo.org/projects/kitchenalert/").arg(QApplication::applicationVersion()));
321 bool KitchenAlertMainWindow::event(QEvent *event)
325 switch (event->type())
327 case QEvent::WindowActivate:
329 model_.setUpdateViewOnChanges(true);
330 // ui->debugLabel->setText("Returned to the application!");
333 case QEvent::WindowDeactivate:
334 model_.setUpdateViewOnChanges(false);
335 // ui->debugLabel->setText("");
343 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!
346 void KitchenAlertMainWindow::disableSelectionDependentButtons()
348 ui->DoneButton->setDisabled(true);
349 ui->SnoozeButton->setDisabled(true);
350 ui->RestartButton->setDisabled(true);
351 ui->RemoveButton->setDisabled(true);
355 void KitchenAlertMainWindow::initializeAlertSound()
359 bool useDefaultSound = settings.value("UseDefaultSound",true).toBool();
360 QString filename = settings.value("soundfile","").toString();
362 if (useDefaultSound == true)
364 openSelectSoundDialog();
366 else if (filename.isEmpty())
368 openSelectSoundDialog();
371 QString currentFilename = settings.value("soundfile","").toString();
373 if (currentFilename.isEmpty())
375 ui->debugLabel->setText("<FONT color=red>No alert sound file set. Alert sound will not be played!</FONT>");
381 void KitchenAlertMainWindow::remove()
383 QModelIndex row = selectedRow();
384 if (row.isValid()) //If there was no row selected invalid row was returned
386 QString text = tr("Are you sure you want to remove this timer from the list:\n");
387 text.append((row.data().toString()));
388 if (QMessageBox::question(this,tr("Confirm timer removal"),text,QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes)
390 model_.removeTimer(row);
393 ui->SnoozeButton->setDisabled(true);
396 void KitchenAlertMainWindow::saveTimer()
399 QModelIndex row = selectedRow();
401 if (row.isValid() == false) //If there was no row selected invalid row was returned
405 //file name is asked. As the filename will be appended, there's no point in confirming owerwrite here
406 QString filename = QFileDialog::getSaveFileName(this, "", "", "*.kitchenalert",NULL,QFileDialog::DontConfirmOverwrite);
408 disableSelectionDependentButtons();
410 qDebug() << filename;
412 if (filename.isEmpty()) //user cancelled the dialog (or gave an empty name)
417 if (!filename.endsWith(".kitchenalert"))
419 filename.append(".kitchenalert");
423 qDebug() << "filename appended to " << filename;
426 //MANUAL CONFIRMATION OF OWERWRITE
428 if ( QFile::exists(filename))
430 //ASK FOR CONFIRMATION
432 QString overwriteQuestion ("File ");
433 overwriteQuestion.append(filename);
434 overwriteQuestion.append(" already exists. Do you want to overwrite it?");
435 if (QMessageBox::question(this,"Confirm overwrite?", overwriteQuestion,QMessageBox::Yes | QMessageBox::No,QMessageBox::No) != QMessageBox::Yes)
444 QString errorMessage(tr("Cannot write to file "));
445 errorMessage.append(filename);
447 if (!model_.saveTimer(row,filename)) //Save the file, if not successful give an error message
449 QMessageBox::critical(this,tr("Save timer failed!"), errorMessage);
455 void KitchenAlertMainWindow::loadTimer()
457 QString filename = QFileDialog::getOpenFileName(this,"","",tr("KitchenAlert timer files (*.kitchenalert)"));
458 if (!filename.isEmpty())
461 // if (!filename.endsWith(".kitchenalert")) //not needed, the dialog won't let the user to select files not ending with ".kitchenalert"
463 // filename.append(".kitchenalert");
466 QString errorTitle(tr("Failed to load file "));
467 errorTitle.append(filename);
469 Timer * p_timer = new Timer();
470 if (!p_timer->load(filename))
472 QMessageBox::critical(this,errorTitle,tr("Unable to open file or not a valid KitchenAlert timer file."));
477 initializeTimer(p_timer);
481 void KitchenAlertMainWindow::initializeTimer(Timer *p_timer)
487 connect(p_timer,SIGNAL(alert(QModelIndex)),this,SLOT(alert(QModelIndex)));
490 //connect change sound functions
492 connect(this,SIGNAL(defaultSoundEnabled()),p_timer,SLOT(enableDefaultSound()));
493 connect(this,SIGNAL(soundChanged(QString)),p_timer,SLOT(changeAlertSound(QString)));
496 //Disable buttons, as selection is cleared when view is refreshed to show the new timer
498 disableSelectionDependentButtons();
501 // give timers to the model (model wants list of timers now..)
503 QList<Timer *> timerList;
505 timerList.append(p_timer);
506 model_.addTimers(timerList,true); //timer gets started in the model