diff --git a/src/sakia/gui/navigation/txhistory/controller.py b/src/sakia/gui/navigation/txhistory/controller.py index 17e37ba71021117dab961e6698425c9953d7c438..e064f5023b18e76c98bc2cd833c408c81d92a589 100644 --- a/src/sakia/gui/navigation/txhistory/controller.py +++ b/src/sakia/gui/navigation/txhistory/controller.py @@ -1,6 +1,6 @@ import logging -from PyQt5.QtCore import QTime, pyqtSignal, QObject +from PyQt5.QtCore import QTime, pyqtSignal, QObject, QDateTime from PyQt5.QtGui import QCursor from sakia.decorators import asyncify @@ -125,14 +125,50 @@ class TxHistoryController(QObject): def dates_changed(self): self._logger.debug("Changed dates") if self.view.table_history.model(): - qdate_from = self.view.date_from + # capture datetimes from calendar widget + qdate_from = self.view.date_from.dateTime() # type: QDateTime + qdate_to = self.view.date_to.dateTime() # type: QDateTime + # time is midnight qdate_from.setTime(QTime(0, 0, 0)) - qdate_to = self.view.date_to qdate_to.setTime(QTime(0, 0, 0)) - ts_from = qdate_from.dateTime().toTime_t() - ts_to = qdate_to.dateTime().toTime_t() + # calculate dates + qdate_from_plus_one_year = qdate_from.addYears(1) + qdate_to_minus_one_month = qdate_to.addMonths(-1) + qone_month_ago = QDateTime.currentDateTime().addMonths(-1) + qtomorrow = QDateTime.currentDateTime().addDays(1) + qtomorrow.setTime(QTime(0, 0, 0)) + + # if start later than one month ago... + if qdate_from > qone_month_ago: + # start = now - 1 month + qdate_from = qone_month_ago + + # if start > end minus one month... + if qdate_from > qdate_to_minus_one_month: + # end = start + 1 month + qdate_to = qdate_from.addMonths(1) + + # if period is more than one year long... + if qdate_to > qdate_from_plus_one_year: + # end = start + 1 year + qdate_to.setDate(qdate_from_plus_one_year.date()) + + # if end > tomorrow... + if qdate_to > qtomorrow: + # end = tomorrow + qdate_to = qtomorrow + + # todo: update minimum and maximum of the calendar to forbid bad dates + # update calendar + self.view.date_from.setDateTime(qdate_from) + self.view.date_to.setDateTime(qdate_to) + + # update model in table + ts_from = qdate_from.toTime_t() + ts_to = qdate_to.toTime_t() self.view.table_history.model().set_period(ts_from, ts_to) + # refresh self.refresh_balance() self.refresh_pages()