aircraftwidget.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. *openPilot Log - A FOSS Pilot Logbook Application
  3. *Copyright (C) 2020 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. // Debug Makro
  21. #define DEB(expr) \
  22. qDebug() << __PRETTY_FUNCTION__ << "\t" << expr
  23. AircraftWidget::AircraftWidget(QWidget *parent) :
  24. QWidget(parent),
  25. ui(new Ui::AircraftWidget)
  26. {
  27. ui->setupUi(this);
  28. sortColumn = Settings::read("userdata/acSortColumn").toInt();
  29. refreshModelAndView();
  30. }
  31. AircraftWidget::~AircraftWidget()
  32. {
  33. delete ui;
  34. }
  35. /*
  36. * Functions
  37. */
  38. void AircraftWidget::refreshModelAndView()
  39. {
  40. ui->stackedWidget->addWidget(parent()->findChild<QWidget*>("welcomePageTails"));
  41. ui->stackedWidget->setCurrentWidget(parent()->findChild<QWidget*>("welcomePageTails"));
  42. DEB("wp" << parent()->findChild<QWidget*>("welcomePageTails"));
  43. model->setTable("viewTails");
  44. model->select();
  45. QTableView *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->setColumnWidth(1, 180);
  53. view->setColumnWidth(2, 180);
  54. view->verticalHeader()->hide();
  55. view->setAlternatingRowColors(true);
  56. view->setSortingEnabled(true);
  57. view->sortByColumn(sortColumn);
  58. view->show();
  59. connect(ui->tableView->selectionModel(), SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
  60. this, SLOT(tableView_selectionChanged()));
  61. connect(ui->tableView->horizontalHeader(), SIGNAL(sectionClicked(int)),
  62. this, SLOT(tableView_headerClicked(int)));
  63. }
  64. /*
  65. * Slots
  66. */
  67. void AircraftWidget::on_deleteButton_clicked()
  68. {
  69. if (selectedTails.length() > 0) {
  70. for(const auto& selectedTail : selectedTails){
  71. auto ac = Aircraft(selectedTail);
  72. auto& warningMsg = "Deleting Aircraft with registration:<br><center><b>"
  73. + ac.data.value("registration")
  74. + "<br></center></b>Do you want to proceed?";
  75. QMessageBox confirm;
  76. confirm.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
  77. confirm.setDefaultButton(QMessageBox::No);
  78. confirm.setIcon(QMessageBox::Question);
  79. confirm.setWindowTitle("Delete Aircraft");
  80. confirm.setText(warningMsg);
  81. int reply = confirm.exec();
  82. if(reply == QMessageBox::Yes) {
  83. if(!ac.remove()) {
  84. QVector<QString> columns = {"doft","dept","dest"};
  85. QVector<QString> details = Db::multiSelect(columns, "flights", "acft",
  86. QString::number(selectedTail), Db::exactMatch);
  87. auto mb = QMessageBox(this);
  88. QString message = "\nUnable to delete. The following error has ocurred:\n\n";
  89. if(!details.isEmpty()){
  90. message += ac.error + QLatin1String("\n\n");
  91. message += "This is most likely the case because a flight exists with the aircaft "
  92. "you are trying to delete. In order to maintain database integrity, you have to\n"
  93. "change or remove this flight before being able to remove this aircraft.\n\n"
  94. "The following flight(s) with this tail have been found:\n\n";
  95. auto space = QLatin1Char(' ');
  96. for(int i = 0; i <= 30 && i <=details.length()-3; i+=3){
  97. message += details[i] + space
  98. + details[i+1] + space
  99. + details[i+2] + QLatin1String("\n");
  100. }
  101. }
  102. mb.setText(message);
  103. mb.setIcon(QMessageBox::Critical);
  104. mb.show();
  105. }
  106. }
  107. }
  108. refreshModelAndView();
  109. } else {
  110. auto mb = QMessageBox(this);
  111. mb.setText("No aircraft selected.");
  112. mb.show();
  113. }
  114. }
  115. void AircraftWidget::on_newButton_clicked()
  116. {
  117. auto nt = new NewTailDialog(QString(), Db::createNew, this);
  118. connect(nt, SIGNAL(accepted()), this, SLOT(acft_editing_finished()));
  119. connect(nt, SIGNAL(rejected()), this, SLOT(acft_editing_finished()));
  120. if(nt->exec() == QDialog::Accepted || QDialog::Rejected) {
  121. delete nt;
  122. }
  123. }
  124. void AircraftWidget::on_searchLineEdit_textChanged(const QString &arg1)
  125. {
  126. if(ui->searchComboBox->currentIndex() == 0){
  127. ui->searchLineEdit->setText(arg1.toUpper());
  128. }
  129. model->setFilter(ui->searchComboBox->currentText() + " LIKE \"%" + arg1 + "%\"");
  130. }
  131. void AircraftWidget::tableView_selectionChanged()
  132. {
  133. auto *selection = ui->tableView->selectionModel();
  134. selectedTails.clear();
  135. for (const auto& row : selection->selectedRows()) {
  136. selectedTails << row.data().toInt();
  137. DEB("Selected Tails(s) with ID: " << selectedTails);
  138. }
  139. if(selectedTails.length() == 1) {
  140. auto nt = new NewTailDialog(Aircraft(selectedTails.first()), Db::editExisting, this);
  141. connect(nt, SIGNAL(accepted()), this, SLOT(acft_editing_finished()));
  142. connect(nt, SIGNAL(rejected()), this, SLOT(acft_editing_finished()));
  143. nt->setWindowFlag(Qt::Widget);
  144. ui->stackedWidget->addWidget(nt);
  145. ui->stackedWidget->setCurrentWidget(nt);
  146. if(nt->exec() == QDialog::Accepted || QDialog::Rejected) {
  147. delete nt;
  148. }
  149. }
  150. }
  151. void AircraftWidget::tableView_headerClicked(int column)
  152. {
  153. sortColumn = column;
  154. Settings::write("userdata/acSortColumn", column);
  155. }
  156. void AircraftWidget::acft_editing_finished()
  157. {
  158. refreshModelAndView();
  159. }