diff --git a/oscar/daily.cpp b/oscar/daily.cpp index 84b8a86f..98e2e8d4 100644 --- a/oscar/daily.cpp +++ b/oscar/daily.cpp @@ -509,6 +509,9 @@ Daily::Daily(QWidget *parent,gGraphView * shared) connect(GraphView, SIGNAL(updateCurrentTime(double)), this, SLOT(on_LineCursorUpdate(double))); connect(GraphView, SIGNAL(updateRange(double,double)), this, SLOT(on_RangeUpdate(double,double))); connect(GraphView, SIGNAL(GraphsChanged()), this, SLOT(updateGraphCombo())); + + // Watch for focusOut events on the JournalNotes widget + ui->JournalNotes->installEventFilter(this); // qDebug() << "Finished making new Daily object"; // sleep(3); } @@ -521,9 +524,11 @@ Daily::~Daily() disconnect(sessionbar, SIGNAL(sessionClicked(Session*)), this, SLOT(doToggleSession(Session*))); disconnect(webView,SIGNAL(anchorClicked(QUrl)),this,SLOT(Link_clicked(QUrl))); + ui->JournalNotes->removeEventFilter(this); - if (previous_date.isValid()) + if (previous_date.isValid()) { Unload(previous_date); + } // Save graph orders and pin status, etc... GraphView->SaveSettings("Daily"); @@ -2206,6 +2211,9 @@ void Daily::on_JournalNotesUnderline_clicked() void Daily::on_prevDayButton_clicked() { + if (previous_date.isValid()) { + Unload(previous_date); + } if (!p_profile->ExistsAndTrue("SkipEmptyDays")) { LoadDate(previous_date.addDays(-1)); } else { @@ -2220,8 +2228,23 @@ void Daily::on_prevDayButton_clicked() } } +bool Daily::eventFilter(QObject *object, QEvent *event) +{ + if (object == ui->JournalNotes && event->type() == QEvent::FocusOut) { + // Trigger immediate save of journal when we focus out from it so we never + // lose any journal entry text... + if (previous_date.isValid()) { + Unload(previous_date); + } + } + return false; +} + void Daily::on_nextDayButton_clicked() { + if (previous_date.isValid()) { + Unload(previous_date); + } if (!p_profile->ExistsAndTrue("SkipEmptyDays")) { LoadDate(previous_date.addDays(1)); } else { @@ -2252,6 +2275,9 @@ void Daily::on_calButton_toggled(bool checked) void Daily::on_todayButton_clicked() { + if (previous_date.isValid()) { + Unload(previous_date); + } // QDate d=QDate::currentDate(); // if (d > p_profile->LastDay()) { QDate lastcpap = p_profile->LastDay(MT_CPAP); @@ -2424,21 +2450,10 @@ void Daily::on_bookmarkTable_itemChanged(QTableWidgetItem *item) void Daily::on_weightSpinBox_valueChanged(double arg1) { - // Update the BMI display - double kg; - if (p_profile->general->unitSystem()==US_English) { - kg=((arg1*pound_convert) + (ui->ouncesSpinBox->value()*ounce_convert)) / 1000.0; - } else kg=arg1; - double height=p_profile->user->height()/100.0; - if ((height>0) && (kg>0)) { - double bmi=kg/(height * height); - ui->BMI->display(bmi); - ui->BMI->setVisible(true); - ui->BMIlabel->setVisible(true); - } else { - ui->BMI->setVisible(false); - ui->BMIlabel->setVisible(false); - } + // This is called if up/down arrows are used, in which case editingFinished is + // never called. So always call editingFinished instead + Q_UNUSED(arg1); + this->on_weightSpinBox_editingFinished(); } void Daily::on_weightSpinBox_editingFinished() @@ -2457,7 +2472,25 @@ void Daily::on_weightSpinBox_editingFinished() } else { kg=arg1; } - journal->settings[Journal_Weight]=kg; + if (journal->settings.contains(Journal_Weight)) { + QVariant old = journal->settings[Journal_Weight]; + if (old == kg && kg > 0) { + // No change to weight - skip + return; + } + } else if (kg == 0) { + // Still zero - skip + return; + } + if (kg > 0) { + journal->settings[Journal_Weight]=kg; + } else { + // Weight now zero - remove from journal + auto jit = journal->settings.find(Journal_Weight); + if (jit != journal->settings.end()) { + journal->settings.erase(jit); + } + } gGraphView *gv=mainwin->getOverview()->graphView(); gGraph *g; if (gv) { @@ -2470,66 +2503,35 @@ void Daily::on_weightSpinBox_editingFinished() ui->BMI->setVisible(true); ui->BMIlabel->setVisible(true); journal->settings[Journal_BMI]=bmi; - if (gv) { - g=gv->findGraph(STR_GRAPH_BMI); - if (g) g->setDay(nullptr); - } } else { + // BMI now zero - remove it + auto jit = journal->settings.find(Journal_BMI); + if (jit != journal->settings.end()) { + journal->settings.erase(jit); + } + // And make it invisible ui->BMI->setVisible(false); ui->BMIlabel->setVisible(false); } + if (gv) { + g=gv->findGraph(STR_GRAPH_BMI); + if (g) g->setDay(nullptr); + } journal->SetChanged(true); } void Daily::on_ouncesSpinBox_valueChanged(int arg1) { - // just update for BMI display - double height=p_profile->user->height()/100.0; - double kg=((ui->weightSpinBox->value()*pound_convert) + (arg1*ounce_convert)) / 1000.0; - if ((height>0) && (kg>0)) { - double bmi=kg/(height * height); - ui->BMI->display(bmi); - ui->BMI->setVisible(true); - ui->BMIlabel->setVisible(true); - } else { - ui->BMI->setVisible(false); - ui->BMIlabel->setVisible(false); - } + // This is called if up/down arrows are used, in which case editingFinished is + // never called. So always call editingFinished instead + Q_UNUSED(arg1); + this->on_weightSpinBox_editingFinished(); } void Daily::on_ouncesSpinBox_editingFinished() { - double arg1=ui->ouncesSpinBox->value(); - Session *journal=GetJournalSession(previous_date); - if (!journal) { - journal=CreateJournalSession(previous_date); - } - double height=p_profile->user->height()/100.0; - double kg=((ui->weightSpinBox->value()*pound_convert) + (arg1*ounce_convert)) / 1000.0; - journal->settings[Journal_Weight]=kg; - - gGraph *g; - if (mainwin->getOverview()) { - g=mainwin->getOverview()->graphView()->findGraph(STR_GRAPH_Weight); - if (g) g->setDay(nullptr); - } - - if ((height>0) && (kg>0)) { - double bmi=kg/(height * height); - ui->BMI->display(bmi); - ui->BMI->setVisible(true); - ui->BMIlabel->setVisible(true); - - journal->settings[Journal_BMI]=bmi; - if (mainwin->getOverview()) { - g=mainwin->getOverview()->graphView()->findGraph(STR_GRAPH_BMI); - if (g) g->setDay(nullptr); - } - } else { - ui->BMI->setVisible(false); - ui->BMIlabel->setVisible(false); - } - journal->SetChanged(true); + // This is functionally identical to the weightSpinBox_editingFinished, so just call that + this->on_weightSpinBox_editingFinished(); } QString Daily::GetDetailsText()