Updated the web page
[kitchenalert] / src / kitchenalertmainwindow.cpp
index 49add3b..ff4c78a 100644 (file)
@@ -42,6 +42,8 @@
 #include <QAction>
 #include <QMenuBar>
 #include <QMessageBox>
+#include <QFileDialog>
+#include <QFile>
 
 
 
@@ -63,8 +65,8 @@ KitchenAlertMainWindow::KitchenAlertMainWindow(QWidget *parent) :
   ui->ComingAlertsTableView->horizontalHeader()->resizeSection(1,140);
   ui->ComingAlertsTableView->horizontalHeader()->resizeSection(2,100);
 
-  ui->ComingAlertsTableView->verticalHeader()->setResizeMode(QHeaderView::Fixed);
-  ui->ComingAlertsTableView->verticalHeader()->setDefaultSectionSize(40);
+  ui->ComingAlertsTableView->verticalHeader()->setResizeMode(QHeaderView::ResizeToContents);
+//  ui->ComingAlertsTableView->verticalHeader()->setDefaultSectionSize(40); //Needed with fixed cell heght only
 
 
 
@@ -85,6 +87,8 @@ KitchenAlertMainWindow::KitchenAlertMainWindow(QWidget *parent) :
   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->loadButton,SIGNAL(clicked()),this,SLOT(loadTimer()));
 
   // menu setup
 
@@ -199,6 +203,7 @@ void KitchenAlertMainWindow::timerSelected(QItemSelection selected,QItemSelectio
     ui->DoneButton->setEnabled(true);
     ui->RestartButton->setEnabled(true);
     ui->RemoveButton->setEnabled(true);
+    ui->SaveButton->setEnabled(true);
 
 
     //enabled only when alerting
@@ -313,6 +318,7 @@ void KitchenAlertMainWindow::disableSelectionDependentButtons()
     ui->SnoozeButton->setDisabled(true);
     ui->RestartButton->setDisabled(true);
     ui->RemoveButton->setDisabled(true);
+    ui->SaveButton->setDisabled(true);
 }
 
 void KitchenAlertMainWindow::remove()
@@ -326,3 +332,118 @@ void KitchenAlertMainWindow::remove()
     disableSelectionDependentButtons();
    }
 }
+
+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, "", "", "*.kitchenalert",NULL,QFileDialog::DontConfirmOverwrite);
+
+    disableSelectionDependentButtons();
+
+    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()
+{
+    QString filename = QFileDialog::getOpenFileName(this,"","",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");
+//        }
+
+        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;
+        }
+
+        initializeTimer(p_timer);
+    }
+}
+
+
+void KitchenAlertMainWindow::initializeTimer(Timer *p_timer)
+{
+
+//connect alert
+
+
+connect(p_timer,SIGNAL(alert(QModelIndex)),this,SLOT(alert(QModelIndex)));
+
+
+//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<Timer *> timerList;
+
+timerList.append(p_timer);
+model_.addTimers(timerList);
+
+
+//start the timer when it's safely in the model (consider moving this to the model's addTimers function)
+
+
+p_timer->start();
+}