totalswidget.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. *openPilotLog - A FOSS Pilot Logbook Application
  3. *Copyright (C) 2020-2023 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 "totalswidget.h"
  19. #include "QtWidgets/qlineedit.h"
  20. #include "src/database/database.h"
  21. #include "src/database/previousexperienceentry.h"
  22. #include "src/opl.h"
  23. #include "src/classes/time.h"
  24. #include "ui_totalswidget.h"
  25. #include "src/classes/settings.h"
  26. TotalsWidget::TotalsWidget(WidgetType widgetType, QWidget *parent) :
  27. QWidget(parent),
  28. ui(new Ui::TotalsWidget)
  29. {
  30. ui->setupUi(this);
  31. setup(widgetType);
  32. }
  33. TotalsWidget::~TotalsWidget()
  34. {
  35. delete ui;
  36. }
  37. /*!
  38. * \brief TotalsWidget::setup Sets the line edits as editable or read-only and connects signals if required
  39. * \details This widget can be used to either display the totals (in the home widget) or
  40. * to edit the total previous experience, from previous logbooks (in the settings widget).
  41. */
  42. void TotalsWidget::setup(const WidgetType widgetType)
  43. {
  44. m_format = Settings::getDisplayFormat();
  45. const QList<QLineEdit *> lineEdits = this->findChildren<QLineEdit *>();
  46. switch (widgetType) {
  47. case TotalTimeWidget:
  48. LOG << "Setting up totals widget";
  49. // disable editing
  50. for (const auto &lineEdit : lineEdits) {
  51. lineEdit->setFocusPolicy(Qt::FocusPolicy::NoFocus);
  52. }
  53. // populate the UI
  54. fillTotals(widgetType);
  55. break;
  56. case PreviousExperienceWidget:
  57. LOG << "Setting up previous XP widget";
  58. for (const auto &lineEdit : lineEdits) {
  59. lineEdit->setFocusPolicy(Qt::FocusPolicy::StrongFocus);
  60. // set a validator for the TO/LDG line edits, the other ones get validated seperately
  61. if(lineEdit->objectName().contains(QLatin1String("to")) || lineEdit->objectName().contains(QLatin1String("ldg"))) {
  62. lineEdit->setValidator(new QIntValidator(0, std::numeric_limits<int>::max(), this));
  63. }
  64. }
  65. // initialise m_rowData
  66. m_rowData = DB->getRowData(OPL::DbTable::PreviousExperience, ROW_ID);
  67. // populate the UI
  68. fillTotals(widgetType);
  69. connectSignalsAndSlots();
  70. break;
  71. default:
  72. break;
  73. }
  74. }
  75. /*!
  76. * \brief HomeWidget::fillTotals Retreives a Database Summary of Total Flight Time and fills the UI.
  77. */
  78. void TotalsWidget::fillTotals(const WidgetType widgetType)
  79. {
  80. OPL::RowData_T time_data;
  81. // retreive times from database
  82. switch (widgetType) {
  83. case TotalTimeWidget:
  84. time_data = DB->getTotals(true);
  85. break;
  86. case PreviousExperienceWidget:
  87. time_data = DB->getRowData(OPL::DbTable::PreviousExperience, ROW_ID);
  88. break;
  89. }
  90. // fill the line edits with the data obtained
  91. const OPL::RowData_T &const_time_data = std::as_const(time_data);
  92. for (const auto &field : const_time_data) {
  93. // match the db entries to the line edits using their object name
  94. const QString search_term = time_data.key(field) + QLatin1String("LineEdit");
  95. QLineEdit* line_edit = this->findChild<QLineEdit *>(search_term);
  96. // fill the line edit with the corresponding data
  97. if(line_edit != nullptr) {
  98. const QString &le_name = line_edit->objectName();
  99. if(le_name.contains("to") || le_name.contains("ldg")) {
  100. // line edits for take offs and landings
  101. line_edit->setText(field.toString());
  102. } else {
  103. // line edits for total time
  104. OPL::Time time = OPL::Time(field.toInt(), m_format);
  105. line_edit->setText(time.toString());
  106. }
  107. }
  108. }
  109. }
  110. /*!
  111. * \brief TotalsWidget::connectSignalsAndSlots If the widget is editable, connects the signals and slots
  112. */
  113. void TotalsWidget::connectSignalsAndSlots()
  114. {
  115. connect(ui->tblkLineEdit, &QLineEdit::editingFinished,
  116. this, &TotalsWidget::timeLineEditEditingFinished);
  117. connect(ui->tSPSELineEdit, &QLineEdit::editingFinished,
  118. this, &TotalsWidget::timeLineEditEditingFinished);
  119. connect(ui->tSPMELineEdit, &QLineEdit::editingFinished,
  120. this, &TotalsWidget::timeLineEditEditingFinished);
  121. connect(ui->tMPLineEdit, &QLineEdit::editingFinished,
  122. this, &TotalsWidget::timeLineEditEditingFinished);
  123. connect(ui->tPICLineEdit, &QLineEdit::editingFinished,
  124. this, &TotalsWidget::timeLineEditEditingFinished);
  125. connect(ui->tSICLineEdit, &QLineEdit::editingFinished,
  126. this, &TotalsWidget::timeLineEditEditingFinished);
  127. connect(ui->tDUALLineEdit, &QLineEdit::editingFinished,
  128. this, &TotalsWidget::timeLineEditEditingFinished);
  129. connect(ui->tFILineEdit, &QLineEdit::editingFinished,
  130. this, &TotalsWidget::timeLineEditEditingFinished);
  131. connect(ui->tPICUSLineEdit, &QLineEdit::editingFinished,
  132. this, &TotalsWidget::timeLineEditEditingFinished);
  133. connect(ui->tIFRLineEdit, &QLineEdit::editingFinished,
  134. this, &TotalsWidget::timeLineEditEditingFinished);
  135. connect(ui->tNIGHTLineEdit, &QLineEdit::editingFinished,
  136. this, &TotalsWidget::timeLineEditEditingFinished);
  137. connect(ui->tSIMLineEdit, &QLineEdit::editingFinished,
  138. this, &TotalsWidget::timeLineEditEditingFinished);
  139. connect(ui->toDayLineEdit, &QLineEdit::editingFinished,
  140. this, &TotalsWidget::movementLineEditEditingFinished);
  141. connect(ui->toNightLineEdit, &QLineEdit::editingFinished,
  142. this, &TotalsWidget::movementLineEditEditingFinished);
  143. connect(ui->ldgDayLineEdit, &QLineEdit::editingFinished,
  144. this, &TotalsWidget::movementLineEditEditingFinished);
  145. connect(ui->ldgNightLineEdit, &QLineEdit::editingFinished,
  146. this, &TotalsWidget::movementLineEditEditingFinished);
  147. }
  148. void TotalsWidget::timeLineEditEditingFinished()
  149. {
  150. LOG << sender()->objectName() + "Editing finished.";
  151. QLineEdit* line_edit = this->findChild<QLineEdit*>(sender()->objectName());
  152. const QString& text = line_edit->text();
  153. // make sure the input is usable
  154. if(!text.contains(QChar(':'))) {
  155. WARN(tr("Please enter the time as: <br><br> hh:mm"));
  156. line_edit->setText(QString());
  157. return;
  158. }
  159. // write the updated value to the database
  160. const QString db_field = line_edit->objectName().remove(QLatin1String("LineEdit"));
  161. const QVariant value = OPL::Time::fromString(line_edit->text(), m_format).toMinutes();
  162. m_rowData.insert(db_field, value);
  163. LOG << "Added row data: " + db_field + ": " + value.toString();
  164. const auto previous_experience = OPL::PreviousExperienceEntry(ROW_ID, m_rowData);
  165. DB->commit(previous_experience);
  166. // Read back the value and set the line edit to confirm input is correct and provide user feedback
  167. m_rowData = DB->getRowData(OPL::DbTable::PreviousExperience, ROW_ID);
  168. OPL::Time new_time = OPL::Time(m_rowData.value(db_field).toInt(), m_format);
  169. line_edit->setText(new_time.toString());
  170. }
  171. void TotalsWidget::movementLineEditEditingFinished()
  172. {
  173. // input validation is done by the QValidator
  174. QLineEdit* line_edit = this->findChild<QLineEdit*>(sender()->objectName());
  175. LOG << line_edit->objectName() + "Editing finished.";
  176. // extract the value from the input and update the DB
  177. const QString db_field = line_edit->objectName().remove(QLatin1String("LineEdit"));
  178. const QVariant value = line_edit->text().toInt();
  179. m_rowData.insert(db_field, value);
  180. const auto previous_experience = OPL::PreviousExperienceEntry(ROW_ID, m_rowData);
  181. DB->commit(previous_experience);
  182. // read back the value and set the line edit to the retreived value to give user feedback
  183. m_rowData = DB->getRowData(OPL::DbTable::PreviousExperience, ROW_ID);
  184. const QString new_value = QString::number(m_rowData.value(db_field).toInt());
  185. line_edit->setText(new_value);
  186. }