Small fixes
[kitchenalert] / src / kitchenalertmainwindow.cpp
index bd1c1f2..f65a002 100644 (file)
@@ -42,6 +42,7 @@
 #include <QAction>
 #include <QMenuBar>
 #include <QMessageBox>
+#include <QSettings>
 
 
 
@@ -51,6 +52,8 @@ KitchenAlertMainWindow::KitchenAlertMainWindow(QWidget *parent) :
     {
     ui->setupUi(this);
 
+    setWindowIcon(QIcon(":/kitchenalert.png"));
+
 
 
 
@@ -63,8 +66,16 @@ KitchenAlertMainWindow::KitchenAlertMainWindow(QWidget *parent) :
   ui->ComingAlertsTableView->setModel(&model_);
   ui->ComingAlertsTableView->setSelectionMode(QAbstractItemView::SingleSelection);
   ui->ComingAlertsTableView->setSelectionBehavior(QAbstractItemView::SelectRows);
-  ui->ComingAlertsTableView->horizontalHeader()->setResizeMode(QHeaderView::ResizeToContents);
+
   ui->ComingAlertsTableView->horizontalHeader()->hide();
+//  ui->ComingAlertsTableView->verticalHeader()->setVisible(true);
+
+  ui->ComingAlertsTableView->horizontalHeader()->setResizeMode(QHeaderView::Fixed);
+  ui->ComingAlertsTableView->horizontalHeader()->resizeSection(0,535);
+  ui->ComingAlertsTableView->horizontalHeader()->resizeSection(1,140);
+  ui->ComingAlertsTableView->horizontalHeader()->resizeSection(2,100);
+
+  ui->ComingAlertsTableView->verticalHeader()->setResizeMode(QHeaderView::ResizeToContents);
 
 
   //Buttons used to reacting an alarm are hidden by default
@@ -77,7 +88,7 @@ KitchenAlertMainWindow::KitchenAlertMainWindow(QWidget *parent) :
   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()));
   // menu setup
 
   QAction * p_SelectSoundAction = new QAction(tr("Select alert sound"),this);
@@ -123,19 +134,26 @@ void KitchenAlertMainWindow::newTimerSequence()
        Timer* timer1 = alltimers.at(0); // take first timer (currently the only one!)
 
 
-       timer1->start();
+
        connect(timer1,SIGNAL(alert(QModelIndex)),this,SLOT(alert(QModelIndex)));
 
+       //TODO: FIND A WAY TO INFORM THE TIMERS' ALERTSOUND'S OF A CHANGE OF THE SOUND FILE THEY SHOULD USE!!!!
+
+       connect(this,SIGNAL(defaultSoundEnabled()),timer1,SLOT(enableDefaultSound()));
+       connect(this,SIGNAL(soundChanged(QString)),timer1,SLOT(changeAlertSound(QString)));
 
 
-        model_.addTimers(alltimers); // give timers to the model
+
+        model_.addTimers(alltimers); // give timers to the model, they are started automatically by default
 
  //       ui->ComingAlertsTableView->resizeColumnsToContents();
 
 
         //Disable buttons, as selection is cleared when view is refreshed to show the new timer
+        //But only if the timer has not already alerted and thus been selected
 
-        disableSelectionDependentButtons();
+        if (!selectedRow().isValid())
+            disableSelectionDependentButtons();
 
 
 
@@ -155,12 +173,17 @@ void KitchenAlertMainWindow::alert(QModelIndex indexOfAlerter)
 
     // The program is brought to front and activated when alerted
 
-    raise();
+
     activateWindow();
+    raise();
 
     // The alerting timer is selected
     ui->ComingAlertsTableView->selectionModel()->select(QItemSelection(indexOfAlerter,indexOfAlerter),QItemSelectionModel::SelectCurrent | QItemSelectionModel::Rows );
-    qDebug() << "Should be selected now";
+
+    //Scrolls the view so that the alerting timer is visible
+    ui->ComingAlertsTableView->scrollTo(indexOfAlerter);
+
+   // qDebug() << "Should be selected now";
 
 
     //Snooze button is enabled
@@ -168,20 +191,28 @@ void KitchenAlertMainWindow::alert(QModelIndex indexOfAlerter)
 
     ui->SnoozeButton->setEnabled(true);
 
-    //The alert sound is played
+    //Debug message
+
 
-    alertSound_.play();
+
+    ui->debugLabel->setText(tr("Alert received from row %1").arg(indexOfAlerter.row()));
+    qDebug() << "Wrote the debug message";
+
+    //The alert sound is played
+   //TESTING TO MOVE THIS OPERATION TO THE TIMER ITSELF
+//    alertSound_.play();
 
 }
 
 
-void KitchenAlertMainWindow::timerSelected(QItemSelection selected,QItemSelection deselected)
+void KitchenAlertMainWindow::timerSelected(QItemSelection selected,QItemSelection)
 {
     ui->DoneButton->setEnabled(true);
     ui->RestartButton->setEnabled(true);
+    ui->RemoveButton->setEnabled(true);
 
 
-    //enabled only when alerting
+    //snooze button enabled only when alerting
     QModelIndexList indexes = selected.indexes();
 
     //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
@@ -205,7 +236,7 @@ void KitchenAlertMainWindow::snooze()
         model_.snoozeTimer(row);
     }
     ui->SnoozeButton->setDisabled(true);
-    alertSound_.stop();
+  //  alertSound_.stop();
 
 }
 
@@ -218,7 +249,7 @@ void KitchenAlertMainWindow::restart()
         model_.startTimer(row);
     }
     ui->SnoozeButton->setDisabled(true);
-    alertSound_.stop();
+ //   alertSound_.stop();
 
 }
 
@@ -230,59 +261,69 @@ void KitchenAlertMainWindow::stop()
         model_.stopTimer(row);
     }
     ui->SnoozeButton->setDisabled(true);
-    alertSound_.stop();
+//    alertSound_.stop();
 }
 
 QModelIndex KitchenAlertMainWindow::selectedRow()
 {
+    //Returns the cells in row 0 that have the whole row selected (the selection mode used allows only selecting whole rows
+
     QModelIndexList chosenRows = ui->ComingAlertsTableView->selectionModel()->selectedRows();
 
     //The selection mode used allows only one row to be selected at time, so we just take the first
-    //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
+
 
     return chosenRows.value(0); //gives an invalid QModelIndex if the list is empty
 }
 
 void KitchenAlertMainWindow::openSelectSoundDialog()
 {
+
     SelectSoundDialog dialog;
    if ( dialog.exec() == QDialog::Accepted) //if user pressed OK
     {
        if (dialog.isDefaultSoundChecked() == true)
-           alertSound_.setDefaultSound();
+           emit defaultSoundEnabled();
        else
-        alertSound_.setSound(dialog.getSoundFileName());
-
-   //opening a dialog clears the selection so the selection dependen buttons must be disabled
+           emit soundChanged(dialog.getSoundFileName());
     }
-    disableSelectionDependentButtons();
+
 }
 
 void KitchenAlertMainWindow::openAbout()
 {
-    QMessageBox::about(this,tr("About KitchenAlert"),tr("<p>Version 0.1"
+    QMessageBox::about(this,tr("About KitchenAlert"),tr("<p>Version %1"
                                                         "<p>Copyright &copy; Heli Hyv&auml;ttinen 2010"
                                                          "<p>License: General Public License v3"
-                                                         "<p>Bugtracker and project page: https://garage.maemo.org/projects/kitchenalert/"));
+                                                         "<p>Web page: http://kitchenalert.garage.maemo.org/"
+                                                         "<p>Bugtracker: https://garage.maemo.org/projects/kitchenalert/").arg(QApplication::applicationVersion()));
 }
 
 bool KitchenAlertMainWindow::event(QEvent *event)
 {
     QMainWindow::event(event);
 
+
+
     switch (event->type())
     {
         case QEvent::WindowActivate:
 
             model_.setUpdateViewOnChanges(true);
+            ui->debugLabel->setText("Returned to the application!");
+
+
               break;
 
        case QEvent::WindowDeactivate:
             model_.setUpdateViewOnChanges(false);
+            ui->debugLabel->setText("");
             break;
 
        default:
             break;
+
+
     }
 }
 
@@ -291,5 +332,47 @@ void KitchenAlertMainWindow::disableSelectionDependentButtons()
     ui->DoneButton->setDisabled(true);
     ui->SnoozeButton->setDisabled(true);
     ui->RestartButton->setDisabled(true);
+    ui->RemoveButton->setDisabled(true);
+
+}
+
+void KitchenAlertMainWindow::initializeAlertSound()
+{
+    QSettings settings;
+
+    bool useDefaultSound = settings.value("UseDefaultSound",true).toBool();
+    QString filename = settings.value("soundfile","").toString();
+
+    if (useDefaultSound == true)
+    {
+        openSelectSoundDialog();
+    }
+    else if (filename.isEmpty())
+    {
+        openSelectSoundDialog();
+    }
 
+   QString currentFilename = settings.value("soundfile","").toString();
+
+   if (currentFilename.isEmpty())
+   {
+        ui->debugLabel->setText("<FONT color=red>No alert sound file set. Alert sound will not be played!</FONT>");
+
+   }
+
+}
+
+void KitchenAlertMainWindow::remove()
+{
+    QModelIndex row = selectedRow();
+    if (row.isValid()) //If there was no row selected invalid row was returned
+    {
+        QString text = tr("Are you sure you want to remove this timer from the list:\n");
+        text.append((row.data().toString()));
+        if (QMessageBox::question(this,tr("Confirm timer removal"),text,QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes)
+        {
+            model_.removeTimer(row);
+        }
+    }
+    ui->SnoozeButton->setDisabled(true);
 }