homewidget.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. *openPilotLog - A FOSS Pilot Logbook Application
  3. *Copyright (C) 2020-2021 Felix Turowsky
  4. *
  5. *This program is free software: you can redistribute it and/or modify
  6. *it under the terms of the GNU General Public License as published by
  7. *the Free Software Foundation, either version 3 of the License, or
  8. *(at your option) any later version.
  9. *
  10. *This program is distributed in the hope that it will be useful,
  11. *but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. *GNU General Public License for more details.
  14. *
  15. *You should have received a copy of the GNU General Public License
  16. *along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. #include "homewidget.h"
  19. #include "ui_homewidget.h"
  20. #include "src/functions/alog.h"
  21. #include "src/database/adatabase.h"
  22. #include "src/functions/atime.h"
  23. #include "src/classes/asettings.h"
  24. // EASA FTL Limitations in minutes
  25. // 100 hours per 28 days
  26. static const int ROLLING_28_DAYS = 6000;
  27. // 900 hours per calendar year
  28. static const int CALENDAR_YEAR = 54000;
  29. // 1000 hours per rolling 12 months
  30. static const int ROLLING_12_MONTHS = 60000;
  31. // Todo: Encapsulate and plan to also use non-EASA (FAA,...) options
  32. HomeWidget::HomeWidget(QWidget *parent) :
  33. QWidget(parent),
  34. ui(new Ui::HomeWidget)
  35. {
  36. ui->setupUi(this);
  37. today = QDate::currentDate();
  38. ftlWarningThreshold = ASettings::read(ASettings::UserData::FtlWarningThreshold).toDouble();
  39. currWarningThreshold = ASettings::read(ASettings::UserData::CurrWarningThreshold).toInt();
  40. auto logo = QPixmap(OPL::Assets::LOGO);
  41. ui->logoLabel->setPixmap(logo);
  42. ui->welcomeLabel->setText(tr("Welcome to openPilotLog, %1!").arg(userName()));
  43. limitationDisplayLabels = {
  44. ui->TakeOffDisplayLabel, ui->LandingsDisplayLabel,
  45. ui->FlightTime28dDisplayLabel, ui->FlightTimeCalYearDisplayLabel,
  46. ui->FlightTime12mDisplayLabel
  47. };
  48. LOG << "Filling Home Widget...";
  49. fillTotals();
  50. fillSelectedCurrencies();
  51. fillLimitations();
  52. }
  53. HomeWidget::~HomeWidget()
  54. {
  55. delete ui;
  56. }
  57. void HomeWidget::refresh()
  58. {
  59. LOG << "Updating HomeWidget...";
  60. const auto label_list = this->findChildren<QLabel *>();
  61. for (const auto label : label_list)
  62. label->setVisible(true);
  63. for (const auto &label : qAsConst(limitationDisplayLabels))
  64. label->setStyleSheet(QString());
  65. fillTotals();
  66. fillSelectedCurrencies();
  67. fillLimitations();
  68. }
  69. void HomeWidget::changeEvent(QEvent *event)
  70. {
  71. if (event != nullptr)
  72. if(event->type() == QEvent::LanguageChange)
  73. ui->retranslateUi(this);
  74. }
  75. /*!
  76. * \brief HomeWidget::fillTotals Retreives a Database Summary of Total Flight Time via the AStat::totals
  77. * function and parses the return to fill out the QLineEdits.
  78. */
  79. void HomeWidget::fillTotals()
  80. {
  81. const auto data = AStat::totals();
  82. for (const auto &field : data) {
  83. auto line_edit = this->findChild<QLineEdit *>(field.first + QLatin1String("LineEdit"));
  84. line_edit->setText(field.second);
  85. }
  86. }
  87. void HomeWidget::fillCurrency(ACurrencyEntry::CurrencyName currency_name, QLabel* display_label)
  88. {
  89. auto currency_entry = aDB->getCurrencyEntry(currency_name);
  90. if (currency_entry.isValid()) {
  91. auto currency_date = QDate::fromString(currency_entry.tableData.value(
  92. OPL::Db::CURRENCIES_EXPIRYDATE).toString(),
  93. Qt::ISODate);
  94. display_label->setText(currency_date.toString(Qt::TextDate));
  95. setLabelColour(display_label, Colour::None);
  96. if (today >= currency_date) { // is expired
  97. setLabelColour(display_label, Colour::Red);
  98. return;
  99. } else if (today.addDays(currWarningThreshold) >=currency_date) { // expires less than <currWarningThreshold> days from current Date
  100. setLabelColour(display_label, Colour::Orange);
  101. }
  102. } else {
  103. display_label->setText(tr("Invalid Date"));
  104. }
  105. }
  106. /*!
  107. * \brief HomeWidget::fillSelectedCurrencies Checks whether a currency is selected and
  108. * retreives and displays relevant data.
  109. */
  110. void HomeWidget::fillSelectedCurrencies()
  111. {
  112. fillCurrencyTakeOffLanding();
  113. ASettings::read(ASettings::UserData::ShowLicCurrency).toBool() ?
  114. fillCurrency(ACurrencyEntry::CurrencyName::Licence, ui->currLicDisplayLabel)
  115. : hideLabels(ui->currLicLabel, ui->currLicDisplayLabel);
  116. ASettings::read(ASettings::UserData::ShowTrCurrency).toBool() ?
  117. fillCurrency(ACurrencyEntry::CurrencyName::TypeRating, ui->currTrDisplayLabel)
  118. : hideLabels(ui->currTrLabel, ui->currTrDisplayLabel);
  119. ASettings::read(ASettings::UserData::ShowLckCurrency).toBool() ?
  120. fillCurrency(ACurrencyEntry::CurrencyName::LineCheck, ui->currLckDisplayLabel)
  121. : hideLabels(ui->currLckLabel, ui->currLckDisplayLabel);
  122. ASettings::read(ASettings::UserData::ShowMedCurrency).toBool() ?
  123. fillCurrency(ACurrencyEntry::CurrencyName::Medical, ui->currMedDisplayLabel)
  124. : hideLabels(ui->currMedLabel, ui->currMedDisplayLabel);
  125. ASettings::read(ASettings::UserData::ShowCustom1Currency).toBool() ?
  126. fillCurrency(ACurrencyEntry::CurrencyName::Custom1, ui->currCustom1DisplayLabel)
  127. : hideLabels(ui->currCustom1Label, ui->currCustom1DisplayLabel);
  128. const QString custom1_text = ASettings::read(ASettings::UserData::Custom1CurrencyName).toString();
  129. if (!custom1_text.isEmpty())
  130. ui->currCustom1Label->setText(custom1_text);
  131. ASettings::read(ASettings::UserData::ShowCustom2Currency).toBool() ?
  132. fillCurrency(ACurrencyEntry::CurrencyName::Custom2, ui->currCustom2DisplayLabel)
  133. : hideLabels(ui->currCustom2Label, ui->currCustom2DisplayLabel);
  134. const QString custom2_text = ASettings::read(ASettings::UserData::Custom2CurrencyName).toString();
  135. if (!custom2_text.isEmpty())
  136. ui->currCustom2Label->setText(custom2_text);
  137. }
  138. /*!
  139. * \brief HomeWidget::fillCurrencyTakeOffLanding Uses AStat::countTakeOffLandings to determine
  140. * the amount of Take-Offs and Landings in the last 90 days and displays data and notifications
  141. * as required.
  142. */
  143. void HomeWidget::fillCurrencyTakeOffLanding()
  144. {
  145. const auto takeoff_landings = AStat::countTakeOffLanding();
  146. if(takeoff_landings.isEmpty())
  147. return;
  148. ui->TakeOffDisplayLabel->setText(takeoff_landings[0].toString());
  149. if (takeoff_landings[0].toUInt() < 3)
  150. setLabelColour(ui->TakeOffDisplayLabel, Colour::Red);
  151. ui->LandingsDisplayLabel->setText(takeoff_landings[1].toString());
  152. if (takeoff_landings[1].toUInt() < 3)
  153. setLabelColour(ui->LandingsDisplayLabel, Colour::Red);
  154. if (ASettings::read(ASettings::UserData::ShowToLgdCurrency).toBool()) {
  155. QDate expiration_date = AStat::currencyTakeOffLandingExpiry();
  156. if (expiration_date <= QDate::currentDate())
  157. setLabelColour(ui->currToLdgDisplayLabel, Colour::Red);
  158. ui->currToLdgDisplayLabel->setText(expiration_date.toString(Qt::TextDate));
  159. } else {
  160. ui->currToLdgLabel->hide();
  161. ui->currToLdgDisplayLabel->hide();
  162. }
  163. }
  164. /*!
  165. * \brief HomeWidget::fillLimitations Queries AStat to obtain information regarding cumulative
  166. * Flight Times and Calculates and Notifies about approaching Flight Time Limitations
  167. */
  168. void HomeWidget::fillLimitations()
  169. {
  170. int minutes = AStat::totalTime(AStat::TimeFrame::Rolling28Days);
  171. ui->FlightTime28dDisplayLabel->setText(ATime::toString(minutes));
  172. if (minutes >= ROLLING_28_DAYS) {
  173. setLabelColour(ui->FlightTime28dDisplayLabel, Colour::Red);
  174. } else if (minutes >= ROLLING_28_DAYS * ftlWarningThreshold) {
  175. setLabelColour(ui->FlightTime28dDisplayLabel, Colour::Orange);
  176. }
  177. minutes = AStat::totalTime(AStat::TimeFrame::Rolling12Months);
  178. ui->FlightTime12mDisplayLabel->setText(ATime::toString(minutes));
  179. if (minutes >= ROLLING_12_MONTHS) {
  180. setLabelColour(ui->FlightTime12mDisplayLabel, Colour::Red);
  181. } else if (minutes >= ROLLING_12_MONTHS * ftlWarningThreshold) {
  182. setLabelColour(ui->FlightTime12mDisplayLabel, Colour::Orange);
  183. }
  184. minutes = AStat::totalTime(AStat::TimeFrame::CalendarYear);
  185. ui->FlightTimeCalYearDisplayLabel->setText(ATime::toString(minutes));
  186. if (minutes >= CALENDAR_YEAR) {
  187. setLabelColour(ui->FlightTimeCalYearDisplayLabel, Colour::Red);
  188. } else if (minutes >= CALENDAR_YEAR * ftlWarningThreshold) {
  189. setLabelColour(ui->FlightTimeCalYearDisplayLabel, Colour::Orange);
  190. }
  191. }
  192. const QString HomeWidget::userName()
  193. {
  194. const auto statement = QStringLiteral("SELECT firstname FROM pilots WHERE ROWID=1");
  195. const auto name = aDB->customQuery(statement, 1);
  196. if (!name.isEmpty())
  197. return name.first().toString();
  198. return QString();
  199. }