aircraftwidget.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 "aircraftwidget.h"
  19. #include "ui_aircraftwidget.h"
  20. #include "src/gui/dialogues/newtaildialog.h"
  21. #include "src/classes/asettings.h"
  22. #include "src/database/adatabase.h"
  23. #include "src/classes/atailentry.h"
  24. #include "src/classes/aflightentry.h"
  25. #include "src/functions/alog.h"
  26. AircraftWidget::AircraftWidget(QWidget *parent) :
  27. QWidget(parent),
  28. ui(new Ui::AircraftWidget)
  29. {
  30. ui->setupUi(this);
  31. ui->tableView->setMinimumWidth(this->width()/2);
  32. ui->stackedWidget->setMinimumWidth(this->width()/2);
  33. setupModelAndView();
  34. }
  35. AircraftWidget::~AircraftWidget()
  36. {
  37. delete ui;
  38. }
  39. void AircraftWidget::setupModelAndView()
  40. {
  41. model = new QSqlTableModel(this);
  42. model->setTable(QStringLiteral("viewTails"));
  43. model->select();
  44. view = ui->tableView;
  45. view->setModel(model);
  46. view->setSelectionBehavior(QAbstractItemView::SelectRows);
  47. view->setSelectionMode(QAbstractItemView::SingleSelection); // For now, editing multiple entries is not supported.
  48. view->setEditTriggers(QAbstractItemView::NoEditTriggers);
  49. view->horizontalHeader()->setStretchLastSection(QHeaderView::Stretch);
  50. view->hideColumn(0);
  51. view->resizeColumnsToContents();
  52. view->verticalHeader()->hide();
  53. view->setAlternatingRowColors(true);
  54. sortColumn = ASettings::read(ASettings::UserData::TailSortColumn).toInt();
  55. view->setSortingEnabled(true);
  56. view->sortByColumn(sortColumn, Qt::DescendingOrder);
  57. view->show();
  58. selection = view->selectionModel();
  59. connectSignalsAndSlots();
  60. }
  61. void AircraftWidget::connectSignalsAndSlots()
  62. {
  63. QObject::connect(ui->tableView->selectionModel(), &QItemSelectionModel::selectionChanged,
  64. this, &AircraftWidget::tableView_selectionChanged);
  65. QObject::connect(ui->tableView->horizontalHeader(), &QHeaderView::sectionClicked,
  66. this, &AircraftWidget::tableView_headerClicked);
  67. }
  68. /*
  69. * Slots
  70. */
  71. void AircraftWidget::onAircraftWidget_settingChanged(SettingsWidget::SettingSignal signal)
  72. {
  73. if (signal != SettingsWidget::AircraftWidget)
  74. return;
  75. setupModelAndView();
  76. }
  77. void AircraftWidget::onAircraftWidget_dataBaseUpdated()
  78. {
  79. refreshView();
  80. }
  81. void AircraftWidget::onNewTailDialog_editingFinished()
  82. {
  83. refreshView();
  84. }
  85. void AircraftWidget::on_newAircraftButton_clicked()
  86. {
  87. NewTailDialog nt(QString(), this);
  88. QObject::connect(&nt, &QDialog::accepted,
  89. this, &AircraftWidget::onNewTailDialog_editingFinished);
  90. QObject::connect(&nt, &QDialog::rejected,
  91. this, &AircraftWidget::onNewTailDialog_editingFinished);
  92. nt.exec();
  93. }
  94. /*!
  95. * \brief Displays a dialog in which the Tail can be edited
  96. */
  97. void AircraftWidget::tableView_selectionChanged()
  98. {
  99. if (this->findChild<NewTailDialog*>() != nullptr)
  100. delete this->findChild<NewTailDialog*>();
  101. selectedTails.clear();
  102. for (const auto& row : selection->selectedRows()) {
  103. selectedTails << row.data().toInt();
  104. DEB << "Selected Tails(s) with ID: " << selectedTails;
  105. }
  106. if(selectedTails.length() == 1) {
  107. auto* nt = new NewTailDialog(selectedTails.first(), this);
  108. QObject::connect(nt, &QDialog::accepted,
  109. this, &AircraftWidget::onNewTailDialog_editingFinished);
  110. QObject::connect(nt, &QDialog::rejected,
  111. this, &AircraftWidget::onNewTailDialog_editingFinished);
  112. ui->stackedWidget->addWidget(nt);
  113. ui->stackedWidget->setCurrentWidget(nt);
  114. nt->setWindowFlag(Qt::Widget);
  115. nt->setAttribute(Qt::WA_DeleteOnClose);
  116. nt->exec();
  117. }
  118. }
  119. /*!
  120. * \brief Acts as a filter on the display model
  121. */
  122. void AircraftWidget::on_aircraftSearchLineEdit_textChanged(const QString &arg1)
  123. {
  124. if(ui->aircraftSearchComboBox->currentIndex() == 0){
  125. ui->aircraftSearchLineEdit->setText(arg1.toUpper());
  126. }
  127. model->setFilter(ui->aircraftSearchComboBox->currentText()
  128. + QLatin1String(" LIKE \"%")
  129. + arg1 + QLatin1String("%\""));
  130. }
  131. void AircraftWidget::tableView_headerClicked(int column)
  132. {
  133. sortColumn = column;
  134. ASettings::write(ASettings::UserData::TailSortColumn, column);
  135. }
  136. void AircraftWidget::on_deleteAircraftButton_clicked()
  137. {
  138. if (selectedTails.length() == 0) {
  139. QMessageBox message_box(this);
  140. message_box.setText(tr("No Aircraft selected."));
  141. message_box.exec();
  142. } else if (selectedTails.length() > 1) {
  143. QMessageBox message_box(this);
  144. message_box.setText(tr("Deleting multiple entries is currently not supported"));
  145. message_box.exec();
  146. /// [F] to do: for (const auto& row_id : selectedPilots) { do batchDelete }
  147. /// I am not sure if enabling this functionality for this widget is a good idea.
  148. /// On the one hand, deleting many entries could be useful in a scenario where
  149. /// for example, the user has changed airlines and does not want to have his 'old'
  150. /// colleagues polluting his logbook anymore.
  151. /// On the other hand we could run into issues with foreign key constraints on the
  152. /// flights table (see on_delete_unsuccessful) below.
  153. /// I think batch-editing should be implemented at some point, but batch-deleting should not.
  154. } else if (selectedTails.length() == 1) {
  155. auto entry = aDB->getTailEntry(selectedTails.first());
  156. QMessageBox message_box(this);
  157. message_box.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
  158. message_box.setDefaultButton(QMessageBox::No);
  159. message_box.setIcon(QMessageBox::Question);
  160. message_box.setWindowTitle(tr("Delete Aircraft"));
  161. message_box.setText(tr("You are deleting the following aircraft:<br><br><b><tt>"
  162. "%1 - (%2)</b></tt><br><br>Are you sure?"
  163. ).arg(entry.registration(),
  164. entry.type()));
  165. if (message_box.exec() == QMessageBox::Yes) {
  166. if(!aDB->remove(entry))
  167. onDeleteUnsuccessful();
  168. }
  169. }
  170. refreshView();
  171. }
  172. /*!
  173. * \brief Informs the user that deleting a database entry has been unsuccessful
  174. *
  175. * \abstract Normally, when one of these entries can not be deleted, it is because of
  176. * a [foreign key constraint](https://sqlite.org/foreignkeys.html), meaning that a flight
  177. * is associated with the aircraft that was supposed to be deleted.
  178. *
  179. * This function is used to inform the user and give hints on how to solve the problem.
  180. */
  181. void AircraftWidget::onDeleteUnsuccessful()
  182. {
  183. QList<int> foreign_key_constraints = aDB->getForeignKeyConstraints(selectedTails.first(),
  184. ADatabaseTarget::tails);
  185. QList<AFlightEntry> constrained_flights;
  186. for (const auto &row_id : qAsConst(foreign_key_constraints)) {
  187. constrained_flights.append(aDB->getFlightEntry(row_id));
  188. }
  189. QMessageBox message_box(this);
  190. if (constrained_flights.isEmpty()) {
  191. message_box.setText(tr("<br>Unable to delete.<br><br>The following error has ocurred: %1"
  192. ).arg(aDB->lastError.text()));
  193. message_box.exec();
  194. return;
  195. } else {
  196. QString constrained_flights_string;
  197. for (int i=0; i<constrained_flights.length(); i++) {
  198. constrained_flights_string.append(constrained_flights[i].summary() + QLatin1String("&nbsp;&nbsp;&nbsp;&nbsp;<br>"));
  199. if (i>10) {
  200. constrained_flights_string.append(QLatin1String("<br>[...]<br>"));
  201. break;
  202. }
  203. }
  204. message_box.setText(tr("Unable to delete.<br><br>"
  205. "This is most likely the case because a flight exists with the aircraft "
  206. "you are trying to delete.<br><br>"
  207. "%1 flight(s) with this aircraft have been found:<br><br><br><b><tt>"
  208. "%2"
  209. "</b></tt><br><br>You have to change or remove the conflicting flight(s) "
  210. "before removing this aircraft from the database.<br><br>"
  211. ).arg(QString::number(constrained_flights.length()), constrained_flights_string));
  212. message_box.setIcon(QMessageBox::Critical);
  213. message_box.exec();
  214. }
  215. }
  216. void AircraftWidget::repopulateModel()
  217. {
  218. // unset the current model and delete it to avoid leak
  219. view->setModel(nullptr);
  220. delete model;
  221. // create a new model and populate it
  222. model = new QSqlTableModel(this);
  223. setupModelAndView();
  224. connectSignalsAndSlots();
  225. }