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>
50 KitchenAlertMainWindow::KitchenAlertMainWindow(QWidget *parent) :
52 ui(new Ui::KitchenAlertMainWindow)
57 //alerts' tableview setup
60 ui->ComingAlertsTableView->setModel(&model_);
61 ui->ComingAlertsTableView->setSelectionMode(QAbstractItemView::SingleSelection);
62 ui->ComingAlertsTableView->setSelectionBehavior(QAbstractItemView::SelectRows);
63 ui->ComingAlertsTableView->horizontalHeader()->setResizeMode(QHeaderView::Fixed);
64 ui->ComingAlertsTableView->horizontalHeader()->resizeSection(0,460);
65 ui->ComingAlertsTableView->horizontalHeader()->resizeSection(1,140);
66 ui->ComingAlertsTableView->horizontalHeader()->resizeSection(2,100);
68 ui->ComingAlertsTableView->verticalHeader()->setResizeMode(QHeaderView::ResizeToContents);
69 // ui->ComingAlertsTableView->verticalHeader()->setDefaultSectionSize(40); //Needed with fixed cell heght only
74 ui->ComingAlertsTableView->horizontalHeader()->hide();
75 ui->ComingAlertsTableView->setWordWrap(true);
78 //Buttons used when a timer is selected are disabled by default and enabled upon selection
80 disableSelectionDependentButtons();
82 connect(ui->ComingAlertsTableView->selectionModel(),SIGNAL(selectionChanged(QItemSelection,QItemSelection)),this,SLOT(timerSelected(QItemSelection,QItemSelection)));
84 //connect buttons to respective functions
85 connect(ui->CreateNewScheduleButton, SIGNAL (pressed()), 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->loadButton,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
132 //get user input from the dialog
135 QList<Timer *> alltimers = createdialog.getTimers();
137 // take first timer (currently the only one!)
140 Timer* timer1 = alltimers.at(0);
146 connect(timer1,SIGNAL(alert(QModelIndex)),this,SLOT(alert(QModelIndex)));
149 //Disable buttons, as selection is cleared when view is refreshed to show the new timer
151 disableSelectionDependentButtons();
154 // give timers to the model
156 model_.addTimers(alltimers);
159 //start the timer when it's safely in the model (consider moving this to the model's addTimers function)
166 // if cancelled, do nothing
176 void KitchenAlertMainWindow::alert(QModelIndex indexOfAlerter)
179 // The program is brought to front and activated when alerted
184 // The alerting timer is selected
185 ui->ComingAlertsTableView->selectionModel()->select(QItemSelection(indexOfAlerter,indexOfAlerter),QItemSelectionModel::SelectCurrent | QItemSelectionModel::Rows );
186 // qDebug() << "Should be selected now";
189 //Snooze button is enabled
192 ui->SnoozeButton->setEnabled(true);
194 //The alert sound is played (consider moving this operation inside timer, as now stopping one alert will silence all alerting alerts)
201 void KitchenAlertMainWindow::timerSelected(QItemSelection selected,QItemSelection deselected)
203 ui->DoneButton->setEnabled(true);
204 ui->RestartButton->setEnabled(true);
205 ui->RemoveButton->setEnabled(true);
206 ui->SaveButton->setEnabled(true);
209 //enabled only when alerting
210 QModelIndexList indexes = selected.indexes();
212 //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
213 QModelIndex index = indexes.value(0);
216 if (model_.isThisTimerAlerting(index) == true)
218 ui->SnoozeButton->setEnabled(true);
220 else ui->SnoozeButton->setDisabled(true);
225 void KitchenAlertMainWindow::snooze()
227 QModelIndex row = selectedRow();
228 if (row.isValid()) //If there was no row selected invalid row was returned
230 model_.snoozeTimer(row);
232 ui->SnoozeButton->setDisabled(true);
237 void KitchenAlertMainWindow::restart()
239 QModelIndex row = selectedRow(); //If there was no row selected invalid row was returned
243 model_.startTimer(row);
245 ui->SnoozeButton->setDisabled(true);
250 void KitchenAlertMainWindow::stop()
252 QModelIndex row = selectedRow();
253 if (row.isValid()) //If there was no row selected invalid row was returned
255 model_.stopTimer(row);
257 ui->SnoozeButton->setDisabled(true);
259 qDebug() << "Stopped.";
262 QModelIndex KitchenAlertMainWindow::selectedRow()
264 QModelIndexList chosenRows = ui->ComingAlertsTableView->selectionModel()->selectedRows();
266 //The selection mode used allows only one row to be selected at time, so we just take the first
267 //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
269 return chosenRows.value(0); //gives an invalid QModelIndex if the list is empty
272 void KitchenAlertMainWindow::openSelectSoundDialog()
274 SelectSoundDialog dialog;
275 if ( dialog.exec() == QDialog::Accepted) //if user pressed OK
277 if (dialog.isDefaultSoundChecked() == true)
278 alertSound_.setDefaultSound();
280 alertSound_.setSound(dialog.getSoundFileName());
282 //opening a dialog clears the selection so the selection dependen buttons must be disabled
284 disableSelectionDependentButtons();
287 void KitchenAlertMainWindow::openAbout()
289 QMessageBox::about(this,tr("About KitchenAlert"),tr("<p>Version 0.1.1"
290 "<p>Copyright © Heli Hyvättinen 2010"
291 "<p>License: General Public License v3"
292 "<p>Bugtracker and project page: https://garage.maemo.org/projects/kitchenalert/"));
295 bool KitchenAlertMainWindow::event(QEvent *event)
297 QMainWindow::event(event);
299 switch (event->type())
301 case QEvent::WindowActivate:
303 model_.setUpdateViewOnChanges(true);
306 case QEvent::WindowDeactivate:
307 model_.setUpdateViewOnChanges(false);
315 void KitchenAlertMainWindow::disableSelectionDependentButtons()
317 ui->DoneButton->setDisabled(true);
318 ui->SnoozeButton->setDisabled(true);
319 ui->RestartButton->setDisabled(true);
320 ui->RemoveButton->setDisabled(true);
321 ui->SaveButton->setDisabled(true);
324 void KitchenAlertMainWindow::remove()
326 QModelIndex row = selectedRow();
329 model_.removeTimer(row);
331 ui->ComingAlertsTableView->clearSelection();
332 disableSelectionDependentButtons();
336 void KitchenAlertMainWindow::saveTimer()
339 QModelIndex row = selectedRow();
341 if (row.isValid() == false) //If there was no row selected invalid row was returned
345 //file name is asked. As the filename will be appended, there's no point in confirming owerwrite here
346 QString filename = QFileDialog::getSaveFileName(this, "", "", "*.kitchenalert",NULL,QFileDialog::DontConfirmOverwrite);
348 disableSelectionDependentButtons();
350 qDebug() << filename;
352 if (filename.isEmpty()) //user cancelled the dialog (or gave an empty name)
357 if (!filename.endsWith(".kitchenalert"))
359 filename.append(".kitchenalert");
363 qDebug() << "filename appended to " << filename;
366 //MANUAL CONFIRMATION OF OWERWRITE
368 if ( QFile::exists(filename))
370 //ASK FOR CONFIRMATION
372 QString overwriteQuestion ("File ");
373 overwriteQuestion.append(filename);
374 overwriteQuestion.append(" already exists. Do you want to overwrite it?");
375 if (QMessageBox::question(this,"Confirm overwrite?", overwriteQuestion,QMessageBox::Yes | QMessageBox::No,QMessageBox::No) != QMessageBox::Yes)
385 QString errorMessage(tr("Cannot write to file "));
386 errorMessage.append(filename);
388 if (!model_.saveTimer(row,filename)) //Save the file, if not successful give an error message
390 QMessageBox::critical(this,tr("Save timer failed!"), errorMessage);
396 void KitchenAlertMainWindow::loadTimer()
398 QString filename = QFileDialog::getOpenFileName(this,"","",tr("KitchenAlert timer files (*.kitchenalert)"));
399 if (!filename.isEmpty())
402 // if (!filename.endsWith(".kitchenalert")) //not needed, the dialog won't let the user to select files not ending with ".kitchenalert"
404 // filename.append(".kitchenalert");
407 QString errorTitle(tr("Failed to load file "));
408 errorTitle.append(filename);
410 Timer * p_timer = new Timer();
411 if (!p_timer->load(filename))
413 QMessageBox::critical(this,errorTitle,tr("Unable to open file or not a valid KitchenAlert timer file."));
418 initializeTimer(p_timer);
423 void KitchenAlertMainWindow::initializeTimer(Timer *p_timer)
429 connect(p_timer,SIGNAL(alert(QModelIndex)),this,SLOT(alert(QModelIndex)));
432 //Disable buttons, as selection is cleared when view is refreshed to show the new timer
434 disableSelectionDependentButtons();
437 // give timers to the model (model wants list of timers now..)
439 QList<Timer *> timerList;
441 timerList.append(p_timer);
442 model_.addTimers(timerList);
445 //start the timer when it's safely in the model (consider moving this to the model's addTimers function)