2
0

aircraftwidget.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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/testing/adebug.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. QObject::connect(ui->tableView->selectionModel(), &QItemSelectionModel::selectionChanged,
  60. this, &AircraftWidget::tableView_selectionChanged);
  61. QObject::connect(ui->tableView->horizontalHeader(), &QHeaderView::sectionClicked,
  62. this, &AircraftWidget::tableView_headerClicked);
  63. }
  64. /*
  65. * Slots
  66. */
  67. void AircraftWidget::onAircraftWidget_settingChanged(SettingsWidget::SettingSignal signal)
  68. {
  69. if (signal != SettingsWidget::AircraftWidget)
  70. return;
  71. setupModelAndView();
  72. }
  73. void AircraftWidget::onAircraftWidget_dataBaseUpdated()
  74. {
  75. refreshView();
  76. }
  77. void AircraftWidget::onNewTailDialog_editingFinished()
  78. {
  79. refreshView();
  80. }
  81. void AircraftWidget::on_newAircraftButton_clicked()
  82. {
  83. NewTailDialog nt(QString(), this);
  84. QObject::connect(&nt, &QDialog::accepted,
  85. this, &AircraftWidget::onNewTailDialog_editingFinished);
  86. QObject::connect(&nt, &QDialog::rejected,
  87. this, &AircraftWidget::onNewTailDialog_editingFinished);
  88. nt.exec();
  89. }
  90. /*!
  91. * \brief Displays a dialog in which the Tail can be edited
  92. */
  93. void AircraftWidget::tableView_selectionChanged()
  94. {
  95. if (this->findChild<NewTailDialog*>() != nullptr)
  96. delete this->findChild<NewTailDialog*>();
  97. selectedTails.clear();
  98. for (const auto& row : selection->selectedRows()) {
  99. selectedTails << row.data().toInt();
  100. DEB << "Selected Tails(s) with ID: " << selectedTails;
  101. }
  102. if(selectedTails.length() == 1) {
  103. auto* nt = new NewTailDialog(selectedTails.first(), this);
  104. QObject::connect(nt, &QDialog::accepted,
  105. this, &AircraftWidget::onNewTailDialog_editingFinished);
  106. QObject::connect(nt, &QDialog::rejected,
  107. this, &AircraftWidget::onNewTailDialog_editingFinished);
  108. ui->stackedWidget->addWidget(nt);
  109. ui->stackedWidget->setCurrentWidget(nt);
  110. nt->setWindowFlag(Qt::Widget);
  111. nt->setAttribute(Qt::WA_DeleteOnClose);
  112. nt->exec();
  113. }
  114. }
  115. /*!
  116. * \brief Acts as a filter on the display model
  117. */
  118. void AircraftWidget::on_aircraftSearchLineEdit_textChanged(const QString &arg1)
  119. {
  120. if(ui->aircraftSearchComboBox->currentIndex() == 0){
  121. ui->aircraftSearchLineEdit->setText(arg1.toUpper());
  122. }
  123. model->setFilter(ui->aircraftSearchComboBox->currentText()
  124. + QLatin1String(" LIKE \"%")
  125. + arg1 + QLatin1String("%\""));
  126. }
  127. void AircraftWidget::tableView_headerClicked(int column)
  128. {
  129. sortColumn = column;
  130. ASettings::write(ASettings::UserData::TailSortColumn, column);
  131. }
  132. void AircraftWidget::on_deleteAircraftButton_clicked()
  133. {
  134. if (selectedTails.length() == 0) {
  135. QMessageBox message_box(this);
  136. message_box.setText(tr("No Aircraft selected."));
  137. message_box.exec();
  138. } else if (selectedTails.length() > 1) {
  139. QMessageBox message_box(this);
  140. message_box.setText(tr("Deleting multiple entries is currently not supported"));
  141. message_box.exec();
  142. /// [F] to do: for (const auto& row_id : selectedPilots) { do batchDelete }
  143. /// I am not sure if enabling this functionality for this widget is a good idea.
  144. /// On the one hand, deleting many entries could be useful in a scenario where
  145. /// for example, the user has changed airlines and does not want to have his 'old'
  146. /// colleagues polluting his logbook anymore.
  147. /// On the other hand we could run into issues with foreign key constraints on the
  148. /// flights table (see on_delete_unsuccessful) below.
  149. /// I think batch-editing should be implemented at some point, but batch-deleting should not.
  150. } else if (selectedTails.length() == 1) {
  151. auto entry = aDB->getTailEntry(selectedTails.first());
  152. QMessageBox message_box(this);
  153. message_box.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
  154. message_box.setDefaultButton(QMessageBox::No);
  155. message_box.setIcon(QMessageBox::Question);
  156. message_box.setWindowTitle(tr("Delete Aircraft"));
  157. message_box.setText(tr("You are deleting the following aircraft:<br><br><b><tt>"
  158. "%1 - (%2)</b></tt><br><br>Are you sure?"
  159. ).arg(entry.registration(),
  160. entry.type()));
  161. if (message_box.exec() == QMessageBox::Yes) {
  162. if(!aDB->remove(entry))
  163. onDeleteUnsuccessful();
  164. }
  165. }
  166. refreshView();
  167. }
  168. /*!
  169. * \brief Informs the user that deleting a database entry has been unsuccessful
  170. *
  171. * \abstract Normally, when one of these entries can not be deleted, it is because of
  172. * a [foreign key constraint](https://sqlite.org/foreignkeys.html), meaning that a flight
  173. * is associated with the aircraft that was supposed to be deleted.
  174. *
  175. * This function is used to inform the user and give hints on how to solve the problem.
  176. */
  177. void AircraftWidget::onDeleteUnsuccessful()
  178. {
  179. QList<int> foreign_key_constraints = aDB->getForeignKeyConstraints(selectedTails.first(),
  180. ADatabaseTarget::tails);
  181. QList<AFlightEntry> constrained_flights;
  182. for (const auto &row_id : qAsConst(foreign_key_constraints)) {
  183. constrained_flights.append(aDB->getFlightEntry(row_id));
  184. }
  185. QMessageBox message_box(this);
  186. if (constrained_flights.isEmpty()) {
  187. message_box.setText(tr("<br>Unable to delete.<br><br>The following error has ocurred: %1"
  188. ).arg(aDB->lastError.text()));
  189. message_box.exec();
  190. return;
  191. } else {
  192. QString constrained_flights_string;
  193. for (int i=0; i<constrained_flights.length(); i++) {
  194. constrained_flights_string.append(constrained_flights[i].summary() + QLatin1String("&nbsp;&nbsp;&nbsp;&nbsp;<br>"));
  195. if (i>10) {
  196. constrained_flights_string.append(QLatin1String("<br>[...]<br>"));
  197. break;
  198. }
  199. }
  200. message_box.setText(tr("Unable to delete.<br><br>"
  201. "This is most likely the case because a flight exists with the aircraft "
  202. "you are trying to delete.<br><br>"
  203. "%1 flight(s) with this aircraft have been found:<br><br><br><b><tt>"
  204. "%2"
  205. "</b></tt><br><br>You have to change or remove the conflicting flight(s) "
  206. "before removing this aircraft from the database.<br><br>"
  207. ).arg(QString::number(constrained_flights.length()), constrained_flights_string));
  208. message_box.setIcon(QMessageBox::Critical);
  209. message_box.exec();
  210. }
  211. }