From 33d6e2ca661b1150cca06e6c481d69e6d7972fc8 Mon Sep 17 00:00:00 2001 From: ftomei Date: Fri, 1 Mar 2024 19:27:08 +0100 Subject: [PATCH] fix meteoWidget --- agrolib/meteoWidget/meteoWidget.cpp | 123 ++++++++++++++++++++-------- agrolib/meteoWidget/meteoWidget.h | 3 +- agrolib/project/project.cpp | 8 +- 3 files changed, 94 insertions(+), 40 deletions(-) diff --git a/agrolib/meteoWidget/meteoWidget.cpp b/agrolib/meteoWidget/meteoWidget.cpp index 5e1e63387..1633cf7d8 100644 --- a/agrolib/meteoWidget/meteoWidget.cpp +++ b/agrolib/meteoWidget/meteoWidget.cpp @@ -406,11 +406,56 @@ void Crit3DMeteoWidget::setDateIntervalHourly(QDate firstDate, QDate lastDate) } -void Crit3DMeteoWidget::draw(Crit3DMeteoPoint mp, bool isAppend) +// search bigger data interval to show between meteoPoints +void Crit3DMeteoWidget::updateTimeRange() +{ + for (int i = 0; i < meteoPoints.size(); i++) + { + QDate myDailyDateFirst; + myDailyDateFirst.setDate(meteoPoints[i].obsDataD[0].date.year, + meteoPoints[i].obsDataD[0].date.month, + meteoPoints[i].obsDataD[0].date.day); + QDate myDailyDateLast = myDailyDateFirst.addDays(meteoPoints[i].nrObsDataDaysD-1); + + // updates daily range + if (myDailyDateFirst.isValid() && + (! firstDailyDate.isValid() || firstDailyDate.year() == 1800 || myDailyDateFirst < firstDailyDate)) + { + firstDailyDate = myDailyDateFirst; + } + if (myDailyDateLast.isValid() && + (! lastDailyDate.isValid() || lastDailyDate.year() == 1800 || myDailyDateLast > lastDailyDate)) + { + lastDailyDate = myDailyDateLast; + } + + QDate myHourlyDateFirst; + myHourlyDateFirst.setDate(meteoPoints[i].getMeteoPointHourlyValuesDate(0).year, + meteoPoints[i].getMeteoPointHourlyValuesDate(0).month, + meteoPoints[i].getMeteoPointHourlyValuesDate(0).day); + QDate myHourlyDateLast = myHourlyDateFirst.addDays(meteoPoints[i].nrObsDataDaysH-1); + + // updates hourly range + if (myHourlyDateFirst.isValid() && + (! firstHourlyDate.isValid() || firstHourlyDate.year() == 1800 || myHourlyDateFirst < firstHourlyDate)) + { + firstHourlyDate = myHourlyDateFirst; + } + if (myHourlyDateLast.isValid() && + (! lastHourlyDate.isValid() || lastHourlyDate.year() == 1800 || myHourlyDateLast > lastHourlyDate)) + { + lastHourlyDate = myHourlyDateLast; + } + } +} + + +void Crit3DMeteoWidget::drawMeteoPoint(Crit3DMeteoPoint mp, bool isAppend) { if (! isInitialized) return; meteoPoints.append(mp); + updateTimeRange(); if (! isAppend) { @@ -418,43 +463,17 @@ void Crit3DMeteoWidget::draw(Crit3DMeteoPoint mp, bool isAppend) firstDate->blockSignals(true); lastDate->blockSignals(true); - QDate myDailyDateFirst; - QDate myDailyDateLast; - QDate myHourlyDateFirst; - QDate myHourlyDateLast; - // search bigger data interval to show between meteoPoints - for (int i = 0; i < meteoPoints.size(); i++) - { - myDailyDateFirst.setDate(meteoPoints[i].obsDataD[0].date.year, meteoPoints[i].obsDataD[0].date.month, meteoPoints[i].obsDataD[0].date.day); - myDailyDateLast = myDailyDateFirst.addDays(meteoPoints[i].nrObsDataDaysD-1); - if (myDailyDateFirst.isValid() && myDailyDateFirst < firstDailyDate) - { - firstDailyDate = myDailyDateFirst; - } - if (myDailyDateLast.isValid() && myDailyDateLast > lastDailyDate) - { - lastDailyDate = myDailyDateLast; - } - myHourlyDateFirst.setDate(meteoPoints[i].getMeteoPointHourlyValuesDate(0).year, meteoPoints[i].getMeteoPointHourlyValuesDate(0).month, - meteoPoints[i].getMeteoPointHourlyValuesDate(0).day); - myHourlyDateLast = myHourlyDateFirst.addDays(meteoPoints[i].nrObsDataDaysH-1); - if (myHourlyDateFirst.isValid() && myHourlyDateFirst < firstHourlyDate) - { - firstHourlyDate = myHourlyDateFirst; - } - if (myHourlyDateLast.isValid() && myHourlyDateLast > lastHourlyDate) - { - lastHourlyDate = myHourlyDateLast; - } - } - lastDate->setDate(currentDate); // draw period (31 days for daily, 3 days for hourly) if (currentFreq == daily) + { firstDate->setDate(currentDate.addDays(-30)); + } else if (currentFreq == hourly) + { firstDate->setDate(currentDate.addDays(-2)); + } firstDate->blockSignals(false); lastDate->blockSignals(false); @@ -1646,13 +1665,30 @@ void Crit3DMeteoWidget::redraw() void Crit3DMeteoWidget::shiftPrevious() { int nDays = firstDate->date().daysTo(lastDate->date()); - if (firstDailyDate < firstDate->date().addDays(-nDays-1)) + + QDate firstValidDate; + if (currentFreq == daily) + { + if (! firstDailyDate.isValid() || firstDailyDate.year() == 1800) + return; + + firstValidDate = firstDailyDate; + } + else + { + if (! firstHourlyDate.isValid() || firstHourlyDate.year() == 1800) + return; + + firstValidDate = firstHourlyDate; + } + + if (firstValidDate < firstDate->date().addDays(-nDays-1)) { firstDate->setDate(firstDate->date().addDays(-nDays-1)); } else { - firstDate->setDate(firstDailyDate); + firstDate->setDate(firstValidDate); } lastDate->setDate(firstDate->date().addDays(nDays)); @@ -1664,13 +1700,30 @@ void Crit3DMeteoWidget::shiftPrevious() void Crit3DMeteoWidget::shiftFollowing() { int nDays = firstDate->date().daysTo(lastDate->date()); - if (lastDate->date().addDays(nDays+1) < lastDailyDate) + + QDate lastValidDate; + if (currentFreq == daily) + { + if (! lastDailyDate.isValid() || lastDailyDate.year() == 1800) + return; + + lastValidDate = lastDailyDate; + } + else + { + if (! lastHourlyDate.isValid() || lastHourlyDate.year() == 1800) + return; + + lastValidDate = lastHourlyDate; + } + + if (lastDate->date().addDays(nDays+1) < lastValidDate) { lastDate->setDate(lastDate->date().addDays(nDays+1)); } else { - lastDate->setDate(lastDailyDate); + lastDate->setDate(lastValidDate); } firstDate->setDate(lastDate->date().addDays(-nDays)); diff --git a/agrolib/meteoWidget/meteoWidget.h b/agrolib/meteoWidget/meteoWidget.h index 4e8f02b43..77facb062 100644 --- a/agrolib/meteoWidget/meteoWidget.h +++ b/agrolib/meteoWidget/meteoWidget.h @@ -26,7 +26,8 @@ void addMeteoPointsEnsemble(Crit3DMeteoPoint mp); - void draw(Crit3DMeteoPoint mp, bool isAppend); + void updateTimeRange(); + void drawMeteoPoint(Crit3DMeteoPoint mp, bool isAppend); void drawEnsemble(); void resetValues(); diff --git a/agrolib/project/project.cpp b/agrolib/project/project.cpp index 545465a04..efeaf05ac 100644 --- a/agrolib/project/project.cpp +++ b/agrolib/project/project.cpp @@ -3228,7 +3228,7 @@ void Project::showMeteoWidgetPoint(std::string idMeteoPoint, std::string namePoi { meteoPointsDbHandler->loadDailyData(getCrit3DDate(firstDaily), getCrit3DDate(lastDaily), &mp); meteoPointsDbHandler->loadHourlyData(getCrit3DDate(firstHourly.date()), getCrit3DDate(lastHourly.date()), &mp); - meteoWidgetPointList[meteoWidgetPointList.size()-1]->draw(mp, isAppend); + meteoWidgetPointList[meteoWidgetPointList.size()-1]->drawMeteoPoint(mp, isAppend); } else { @@ -3258,7 +3258,7 @@ void Project::showMeteoWidgetPoint(std::string idMeteoPoint, std::string namePoi } meteoWidgetPoint->setCurrentDate(this->currentDate); - meteoWidgetPoint->draw(mp, isAppend); + meteoWidgetPoint->drawMeteoPoint(mp, isAppend); } closeLogInfo(); @@ -3330,7 +3330,7 @@ void Project::showMeteoWidgetGrid(std::string idCell, bool isAppend) unsigned row, col; if (meteoGridDbHandler->meteoGrid()->findMeteoPointFromId(&row, &col, idCell)) { - meteoWidgetGridList[meteoWidgetGridList.size()-1]->draw(meteoGridDbHandler->meteoGrid()->meteoPoint(row,col), isAppend); + meteoWidgetGridList[meteoWidgetGridList.size()-1]->drawMeteoPoint(meteoGridDbHandler->meteoGrid()->meteoPoint(row,col), isAppend); } return; } @@ -3425,7 +3425,7 @@ void Project::showMeteoWidgetGrid(std::string idCell, bool isAppend) meteoWidgetGrid->setDateIntervalHourly(firstDateTime.date(), lastDateTime.date()); } - meteoWidgetGrid->draw(meteoGridDbHandler->meteoGrid()->meteoPoint(row,col), isAppend); + meteoWidgetGrid->drawMeteoPoint(meteoGridDbHandler->meteoGrid()->meteoPoint(row,col), isAppend); } } return;