pilotswidget.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*
  2. *openPilotLog - A FOSS Pilot Logbook Application
  3. *Copyright (C) 2020-2022 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 "pilotswidget.h"
  19. #include "ui_pilotswidget.h"
  20. #include "src/gui/dialogues/newpilotdialog.h"
  21. #include "src/opl.h"
  22. #include "src/database/database.h"
  23. #include "src/database/row.h"
  24. #include "src/functions/time.h"
  25. #include "src/classes/settings.h"
  26. PilotsWidget::PilotsWidget(QWidget *parent) :
  27. QWidget(parent),
  28. ui(new Ui::PilotsWidget)
  29. {
  30. ui->setupUi(this);
  31. ui->tableView->setMinimumWidth(this->width()/2);
  32. ui->stackedWidget->setMinimumWidth(this->width()/2);
  33. setupModelAndView();
  34. }
  35. PilotsWidget::~PilotsWidget()
  36. {
  37. delete ui;
  38. }
  39. void PilotsWidget::setupModelAndView()
  40. {
  41. model = new QSqlTableModel(this);
  42. model->setTable(QStringLiteral("viewPilots"));
  43. model->setFilter(QStringLiteral("ID > 1")); // Don't show self
  44. model->select();
  45. view = ui->tableView;
  46. view->setModel(model);
  47. view->setSelectionBehavior(QAbstractItemView::SelectRows);
  48. view->setSelectionMode(QAbstractItemView::SingleSelection);
  49. view->setEditTriggers(QAbstractItemView::NoEditTriggers);
  50. view->horizontalHeader()->setStretchLastSection(QHeaderView::Stretch);
  51. view->hideColumn(0);
  52. view->resizeColumnsToContents();
  53. view->verticalHeader()->hide();
  54. view->setAlternatingRowColors(true);
  55. view->setSortingEnabled(true);
  56. sortColumn = Settings::read(Settings::UserData::PilotSortColumn).toInt();
  57. view->sortByColumn(sortColumn, Qt::AscendingOrder);
  58. view->show();
  59. selectionModel = view->selectionModel();
  60. connectSignalsAndSlots();
  61. }
  62. void PilotsWidget::connectSignalsAndSlots()
  63. {
  64. QObject::connect(ui->tableView->selectionModel(), &QItemSelectionModel::selectionChanged,
  65. this, &PilotsWidget::tableView_selectionChanged);
  66. QObject::connect(ui->tableView->horizontalHeader(), &QHeaderView::sectionClicked,
  67. this, &PilotsWidget::tableView_headerClicked);
  68. }
  69. void PilotsWidget::setUiEnabled(bool enabled)
  70. {
  71. ui->tableView->setEnabled(enabled);
  72. ui->newPilotButton->setEnabled(enabled);
  73. ui->deletePilotButton->setEnabled(enabled);
  74. ui->pilotSearchLineEdit->setEnabled(enabled);
  75. ui->pilotsSearchComboBox->setEnabled(enabled);
  76. }
  77. void PilotsWidget::changeEvent(QEvent *event)
  78. {
  79. if (event != nullptr)
  80. if(event->type() == QEvent::LanguageChange)
  81. ui->retranslateUi(this);
  82. }
  83. void PilotsWidget::onPilotsWidget_settingChanged(SettingsWidget::SettingSignal signal)
  84. {
  85. if (signal == SettingsWidget::PilotsWidget)
  86. setupModelAndView();
  87. }
  88. void PilotsWidget::onPilotsWidget_databaseUpdated()
  89. {
  90. refreshView();
  91. }
  92. void PilotsWidget::on_pilotSearchLineEdit_textChanged(const QString &arg1)
  93. {
  94. model->setFilter(QLatin1Char('\"') + ui->pilotsSearchComboBox->currentText()
  95. + QLatin1String("\" LIKE '%") + arg1
  96. + QLatin1String("%' AND ID > 1"));
  97. }
  98. void PilotsWidget::tableView_selectionChanged()
  99. {
  100. if (this->findChild<NewPilotDialog*>() != nullptr) {
  101. delete this->findChild<NewPilotDialog*>();
  102. }
  103. auto selection = ui->tableView->selectionModel();
  104. selectedPilots.clear();
  105. for (const auto& row : selection->selectedRows()) {
  106. selectedPilots.append(row.data().toInt());
  107. DEB << "Selected Tails(s) with ID: " << selectedPilots;
  108. }
  109. if(selectedPilots.length() == 1) {
  110. NewPilotDialog np = NewPilotDialog(selectedPilots.first(), this);
  111. np.setWindowFlag(Qt::Widget);
  112. ui->stackedWidget->addWidget(&np);
  113. ui->stackedWidget->setCurrentWidget(&np);
  114. setUiEnabled(false);
  115. np.exec();
  116. refreshView();
  117. setUiEnabled(true);
  118. }
  119. }
  120. void PilotsWidget::tableView_headerClicked(int column)
  121. {
  122. sortColumn = column;
  123. Settings::write(Settings::UserData::PilotSortColumn, column);
  124. }
  125. void PilotsWidget::on_newPilotButton_clicked()
  126. {
  127. NewPilotDialog np = NewPilotDialog(this);
  128. np.exec();
  129. refreshView();
  130. }
  131. void PilotsWidget::on_deletePilotButton_clicked()
  132. {
  133. if (selectedPilots.length() == 0) {
  134. INFO(tr("No Pilot selected."));
  135. } else if (selectedPilots.length() > 1) {
  136. WARN(tr("Deleting multiple entries is currently not supported"));
  137. /// [F] to do: for (const auto& row_id : selectedPilots) { do batchDelete }
  138. /// I am not sure if enabling this functionality for this widget is a good idea.
  139. /// On the one hand, deleting many entries could be useful in a scenario where
  140. /// for example, the user has changed airlines and does not want to have his 'old'
  141. /// colleagues polluting his logbook anymore.
  142. /// On the other hand we could run into issues with foreign key constraints on the
  143. /// flights table (see on_delete_unsuccessful) below.
  144. /// I think batch-editing should be implemented at some point, but batch-deleting should not.
  145. } else if (selectedPilots.length() == 1) {
  146. auto entry = DB->getPilotEntry(selectedPilots.first());
  147. QMessageBox confirm(this);
  148. confirm.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
  149. confirm.setDefaultButton(QMessageBox::No);
  150. confirm.setIcon(QMessageBox::Question);
  151. confirm.setWindowTitle(tr("Delete Pilot"));
  152. confirm.setText(tr("You are deleting the following pilot:<br><br><b><tt>"
  153. "%1</b></tt><br><br>Are you sure?").arg(getPilotName(entry)));
  154. if (confirm.exec() == QMessageBox::Yes) {
  155. if(!DB->remove(entry))
  156. onDeleteUnsuccessful();
  157. }
  158. }
  159. refreshView();
  160. ui->stackedWidget->setCurrentIndex(0);
  161. ui->pilotSearchLineEdit->setText(QString());
  162. }
  163. /*!
  164. * \brief Informs the user that deleting a database entry has been unsuccessful
  165. *
  166. * \details Normally, when one of these entries can not be deleted, it is because of
  167. * a [foreign key constraint](https://sqlite.org/foreignkeys.html), meaning that a flight
  168. * is associated with the Pilot that was supposed to be deleted as Pilot-in-command.
  169. *
  170. * This function is used to inform the user and give hints on how to solve the problem.
  171. */
  172. void PilotsWidget::onDeleteUnsuccessful()
  173. {
  174. const QList<int> foreign_key_constraints = DB->getForeignKeyConstraints(selectedPilots.first(),
  175. OPL::DbTable::Pilots);
  176. QList<OPL::FlightEntry> constrained_flights;
  177. for (const auto &row_id : foreign_key_constraints) {
  178. constrained_flights.append(DB->getFlightEntry(row_id));
  179. }
  180. if (constrained_flights.isEmpty()) {
  181. WARN(tr("<br>Unable to delete.<br><br>The following error has ocurred:<br>%1"
  182. ).arg(DB->lastError.text()));
  183. return;
  184. } else {
  185. QString constrained_flights_string;
  186. for (int i=0; i<constrained_flights.length(); i++) {
  187. constrained_flights_string.append(getFlightSummary(constrained_flights[i]) + QStringLiteral("&nbsp;&nbsp;&nbsp;&nbsp;<br>"));
  188. if (i>10) {
  189. constrained_flights_string.append("<br>[...]<br>");
  190. break;
  191. }
  192. }
  193. WARN(tr("Unable to delete.<br><br>"
  194. "This is most likely the case because a flight exists with the Pilot "
  195. "you are trying to delete as PIC.<br><br>"
  196. "%1 flight(s) with this pilot have been found:<br><br><br><b><tt>"
  197. "%2"
  198. "</b></tt><br><br>You have to change or remove the conflicting flight(s) "
  199. "before removing this pilot from the database.<br><br>"
  200. ).arg(QString::number(constrained_flights.length()),
  201. constrained_flights_string));
  202. }
  203. }
  204. void PilotsWidget::repopulateModel()
  205. {
  206. // unset the current model and delete it to avoid leak
  207. view->setModel(nullptr);
  208. delete model;
  209. // create a new model and populate it
  210. model = new QSqlTableModel(this);
  211. setupModelAndView();
  212. connectSignalsAndSlots();
  213. }
  214. const QString PilotsWidget::getPilotName(const OPL::PilotEntry &pilot)
  215. {
  216. if (!pilot.isValid())
  217. return QString();
  218. return pilot.getData().value(OPL::Db::PILOTS_LASTNAME).toString() + QLatin1String(", ")
  219. + pilot.getData().value(OPL::Db::PILOTS_FIRSTNAME).toString();
  220. }
  221. const QString PilotsWidget::getFlightSummary(const OPL::FlightEntry &flight) const
  222. {
  223. if(!flight.isValid())
  224. return QString();
  225. auto tableData = flight.getData();
  226. QString flight_summary;
  227. auto space = QLatin1Char(' ');
  228. flight_summary.append(tableData.value(OPL::Db::FLIGHTS_DOFT).toString() + space);
  229. flight_summary.append(tableData.value(OPL::Db::FLIGHTS_DEPT).toString() + space);
  230. flight_summary.append(OPL::Time::toString(tableData.value(OPL::Db::FLIGHTS_TOFB).toInt())
  231. + space);
  232. flight_summary.append(OPL::Time::toString(tableData.value(OPL::Db::FLIGHTS_TONB).toInt())
  233. + space);
  234. flight_summary.append(tableData.value(OPL::Db::FLIGHTS_DEST).toString());
  235. return flight_summary;
  236. }