X-Git-Url: https://vcs.maemo.org/git/?a=blobdiff_plain;f=src%2Fkitchenalertmainwindow.cpp;h=f74da41e6dadd73789ca179bb60d27b175b084f0;hb=d6ebfc1ae1c6c753f39a44478dbf2ad2087a3883;hp=5d601177e87ac0203fc1feac7743b2bcdaf3ffad;hpb=16677c97be29bac7c5194744c370810b7cc3574e;p=kitchenalert diff --git a/src/kitchenalertmainwindow.cpp b/src/kitchenalertmainwindow.cpp index 5d60117..f74da41 100644 --- a/src/kitchenalertmainwindow.cpp +++ b/src/kitchenalertmainwindow.cpp @@ -2,7 +2,7 @@ KitchenAlert - Copyright (C) 2010 Heli Hyvättinen + Copyright (C) 2010-2011 Heli Hyvättinen This file is part of KitchenAlert. @@ -35,34 +35,32 @@ #include "createtimersequencedialog.h" #include "selectsounddialog.h" - - #include #include #include #include #include +#include +#include +#include +#include "stickydialog.h" KitchenAlertMainWindow::KitchenAlertMainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::KitchenAlertMainWindow) { - ui->setupUi(this); - - setWindowIcon(QIcon(":/kitchenalert.png")); - + + defaultSaveDirectory_ = "/home/user/KitchenAlert"; + ui->setupUi(this); - - connect(ui->CreateNewScheduleButton, SIGNAL (pressed()), this, SLOT (newTimerSequence())); - + setWindowIcon(QIcon(":/kitchenalert.png")); //alerts' tableview setup - ui->ComingAlertsTableView->setModel(&model_); ui->ComingAlertsTableView->setSelectionMode(QAbstractItemView::SingleSelection); ui->ComingAlertsTableView->setSelectionBehavior(QAbstractItemView::SelectRows); @@ -78,17 +76,20 @@ KitchenAlertMainWindow::KitchenAlertMainWindow(QWidget *parent) : ui->ComingAlertsTableView->verticalHeader()->setResizeMode(QHeaderView::ResizeToContents); - //Buttons used to reacting an alarm are hidden by default + //Buttons used to reacting to an alarm are hidden by default disableSelectionDependentButtons(); connect(ui->ComingAlertsTableView->selectionModel(),SIGNAL(selectionChanged(QItemSelection,QItemSelection)),this,SLOT(timerSelected(QItemSelection,QItemSelection))); - + connect(ui->CreateNewScheduleButton, SIGNAL (clicked()), this, SLOT (newTimerSequence())); connect(ui->DoneButton,SIGNAL(clicked()),this,SLOT(stop())); connect(ui->RestartButton,SIGNAL(clicked()),this,SLOT(restart())); connect(ui->SnoozeButton,SIGNAL(clicked()),this, SLOT(snooze())); connect(ui->RemoveButton,SIGNAL(clicked()),this,SLOT(remove())); + connect(ui->SaveButton,SIGNAL(clicked()),this,SLOT(saveTimer())); + connect(ui->OpenButton,SIGNAL(clicked()),this,SLOT(loadTimer())); + // menu setup QAction * p_SelectSoundAction = new QAction(tr("Select alert sound"),this); @@ -98,6 +99,13 @@ KitchenAlertMainWindow::KitchenAlertMainWindow(QWidget *parent) : QAction * p_AboutAction = new QAction(tr("About"),this); connect(p_AboutAction,SIGNAL(triggered()),this,SLOT(openAbout())); menuBar()->addAction(p_AboutAction); + + QAction * p_StickyAction = new QAction(tr("Edit sticky timers"),this); + connect(p_StickyAction, SIGNAL(triggered()),this,SLOT(openStickyDialog())); + menuBar()->addAction(p_StickyAction); + + QTimer::singleShot(0,this,SLOT(loadStickies())); //Load sticky timers after construction to speed the application showing up + } KitchenAlertMainWindow::~KitchenAlertMainWindow() @@ -193,7 +201,7 @@ void KitchenAlertMainWindow::alert(QModelIndex indexOfAlerter) ui->SnoozeButton->setEnabled(true); -qDebug ("Snooze päälle hälytyksessä"); +//qDebug ("Snooze on when alerting"); } @@ -203,6 +211,7 @@ void KitchenAlertMainWindow::timerSelected(QItemSelection selected,QItemSelectio ui->DoneButton->setEnabled(true); ui->RestartButton->setEnabled(true); ui->RemoveButton->setEnabled(true); + ui->SaveButton->setEnabled(true); //snooze button enabled only when alerting @@ -215,12 +224,12 @@ void KitchenAlertMainWindow::timerSelected(QItemSelection selected,QItemSelectio if (model_.isThisTimerAlerting(index) == true) { ui->SnoozeButton->setEnabled(true); -qDebug() << "Snooze päälle"; +//qDebug() << "Snooze on"; } else { ui->SnoozeButton->setDisabled(true); -qDebug() << "Snooze pois päältä"; +//qDebug() << "Snooze off"; } } @@ -234,7 +243,7 @@ void KitchenAlertMainWindow::snooze() model_.snoozeTimer(row); } ui->SnoozeButton->setDisabled(true); - // alertSound_.stop(); + } @@ -248,9 +257,12 @@ void KitchenAlertMainWindow::restart() } - ui->SnoozeButton->setDisabled(true); - qDebug () << "disabled snooze because of restart"; - // alertSound_.stop(); + if (model_.isThisTimerAlerting(row) == false) //This has to be checked, because 00:00:00 alerts alert *before* the program execution reaches here + { + ui->SnoozeButton->setDisabled(true); + } + // qDebug () << "disabled snooze because of restart"; + } @@ -262,7 +274,7 @@ void KitchenAlertMainWindow::stop() model_.stopTimer(row); } ui->SnoozeButton->setDisabled(true); -// alertSound_.stop(); + } QModelIndex KitchenAlertMainWindow::selectedRow() @@ -283,10 +295,22 @@ void KitchenAlertMainWindow::openSelectSoundDialog() SelectSoundDialog dialog; if ( dialog.exec() == QDialog::Accepted) //if user pressed OK { + QSettings settings ("KitchenAlert","KitchenAlert"); + if (dialog.isDefaultSoundChecked() == true) + { + + settings.setValue("UseDefaultSound",true); emit defaultSoundEnabled(); - else - emit soundChanged(dialog.getSoundFileName()); + } + else + { + QString filename = dialog.getSoundFileName(); + settings.setValue("UseDefaultSound",false); + settings.setValue("soundfile",filename); + emit soundChanged(filename); + } + } } @@ -294,7 +318,7 @@ void KitchenAlertMainWindow::openSelectSoundDialog() void KitchenAlertMainWindow::openAbout() { QMessageBox::about(this,tr("About KitchenAlert"),tr("

Version %1" - "

Copyright © Heli Hyvättinen 2010" + "

Copyright © Heli Hyvättinen 2010-2011" "

License: General Public License v3" "

Web page: http://kitchenalert.garage.maemo.org/" "

Bugtracker: https://garage.maemo.org/projects/kitchenalert/").arg(QApplication::applicationVersion())); @@ -331,6 +355,8 @@ void KitchenAlertMainWindow::disableSelectionDependentButtons() ui->SnoozeButton->setDisabled(true); ui->RestartButton->setDisabled(true); ui->RemoveButton->setDisabled(true); + ui->SaveButton->setDisabled(true); + } @@ -370,7 +396,180 @@ void KitchenAlertMainWindow::remove() if (QMessageBox::question(this,tr("Confirm timer removal"),text,QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) { model_.removeTimer(row); + ui->ComingAlertsTableView->clearSelection(); + disableSelectionDependentButtons(); + } } - ui->SnoozeButton->setDisabled(true); + +} + +void KitchenAlertMainWindow::saveTimer() +{ + + QModelIndex row = selectedRow(); + + if (row.isValid() == false) //If there was no row selected invalid row was returned + return; + + + //file name is asked. As the filename will be appended, there's no point in confirming owerwrite here + QString filename = QFileDialog::getSaveFileName(this, "", defaultSaveDirectory_, "*.kitchenalert",NULL,QFileDialog::DontConfirmOverwrite); + + + +// qDebug() << filename; + + if (filename.isEmpty()) //user cancelled the dialog (or gave an empty name) + { + return; + } + + if (!filename.endsWith(".kitchenalert")) + { + filename.append(".kitchenalert"); + + } + + // qDebug() << "filename appended to " << filename; + + + //MANUAL CONFIRMATION OF OWERWRITE + + if ( QFile::exists(filename)) + { + //ASK FOR CONFIRMATION + + QString overwriteQuestion ("File "); + overwriteQuestion.append(filename); + overwriteQuestion.append(" already exists. Do you want to overwrite it?"); + if (QMessageBox::question(this,"Confirm overwrite?", overwriteQuestion,QMessageBox::Yes | QMessageBox::No,QMessageBox::No) != QMessageBox::Yes) + { + return; + } + } + + + + + QString errorMessage(tr("Cannot write to file ")); + errorMessage.append(filename); + + if (!model_.saveTimer(row,filename)) //Save the file, if not successful give an error message + { + QMessageBox::critical(this,tr("Save timer failed!"), errorMessage); + } + + +} + +void KitchenAlertMainWindow::loadTimer() +{ + +// If the default save directory does not exist use /home/user instead (as that's what the save dialog will use) +// This avoids a situation where save directs to a folder that cannot be accessed with open... + + QString startDirectory; + + if (QFile(defaultSaveDirectory_).exists()) + { + startDirectory = defaultSaveDirectory_; + } + else + { + startDirectory = "/home/user/"; + qDebug () << "default save directory not found"; + } + + + //Get the filename to open with a dialog + + QString filename = QFileDialog::getOpenFileName(this,"",startDirectory,tr("KitchenAlert timer files (*.kitchenalert)")); + if (!filename.isEmpty()) + { + +// if (!filename.endsWith(".kitchenalert")) //not needed, the dialog won't let the user to select files not ending with ".kitchenalert" +// { +// filename.append(".kitchenalert"); +// } + + loadTimer(filename,true); //timer gets started + + } +} + +void KitchenAlertMainWindow::initializeTimer(Timer *p_timer, bool startImmediately) +{ + +//connect alert + + +connect(p_timer,SIGNAL(alert(QModelIndex)),this,SLOT(alert(QModelIndex))); + + +//connect change sound functions + +connect(this,SIGNAL(defaultSoundEnabled()),p_timer,SLOT(enableDefaultSound())); +connect(this,SIGNAL(soundChanged(QString)),p_timer,SLOT(changeAlertSound(QString))); + + +//Disable buttons, as selection is cleared when view is refreshed to show the new timer + +disableSelectionDependentButtons(); + + +// give timers to the model (model wants list of timers now..) + +QList timerList; + +timerList.append(p_timer); +model_.addTimers(timerList,startImmediately); //timer gets started in the model if startImmediately is true (default) + +} + +void KitchenAlertMainWindow::openStickyDialog() +{ + StickyDialog stickyDialog(defaultSaveDirectory_); + + + if (stickyDialog.exec()== QDialog::Accepted) + { + QSettings settings; + settings.setValue("sticky",stickyDialog.getStickyList()); + } +} + +bool KitchenAlertMainWindow::loadStickies() +{ + bool allSuccess = true; + + QSettings settings; + + QStringList stickies = settings.value("sticky").toStringList(); + + + foreach (QString stickyFileName, stickies) + { + if (!loadTimer(stickyFileName,false)) //initializes the timer without starting it + allSuccess = false; + } + + return allSuccess; +} + +bool KitchenAlertMainWindow::loadTimer(QString filename, bool startImmediately) +{ + QString errorTitle(tr("Failed to load file ")); + errorTitle.append(filename); + + Timer * p_timer = new Timer(); + if (!p_timer->load(filename)) + { + QMessageBox::critical(this,errorTitle,tr("Unable to open file or not a valid KitchenAlert timer file.")); + delete p_timer; + return false; + } + + initializeTimer(p_timer,startImmediately); + return true; }