pilotswidget.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 "pilotswidget.h"
  19. #include "ui_pilotswidget.h"
  20. #include "debug.h"
  21. PilotsWidget::PilotsWidget(QWidget *parent) :
  22. QWidget(parent),
  23. ui(new Ui::PilotsWidget)
  24. {
  25. ui->setupUi(this);
  26. sortColumn = Settings::read("userdata/pilSortColumn").toInt();
  27. refreshModelAndView();
  28. }
  29. PilotsWidget::~PilotsWidget()
  30. {
  31. delete ui;
  32. }
  33. void PilotsWidget::tableView_selectionChanged()//const QItemSelection &index, const QItemSelection &
  34. {
  35. auto *selection = ui->tableView->selectionModel();
  36. selectedPilots.clear();
  37. for (const auto& row : selection->selectedRows()) {
  38. selectedPilots << row.data().toInt();
  39. DEB("Selected Tails(s) with ID: " << selectedPilots);
  40. }
  41. if(selectedPilots.length() == 1) {
  42. NewPilotDialog* np = new NewPilotDialog(Pilot(selectedPilots.first()), Db::editExisting, this);
  43. connect(np, SIGNAL(accepted()), this, SLOT(pilot_editing_finished()));
  44. connect(np, SIGNAL(rejected()), this, SLOT(pilot_editing_finished()));
  45. np->setWindowFlag(Qt::Widget);
  46. np->setAttribute(Qt::WA_DeleteOnClose);
  47. ui->stackedWidget->addWidget(np);
  48. ui->stackedWidget->setCurrentWidget(np);
  49. np->exec();
  50. }
  51. }
  52. void PilotsWidget::tableView_headerClicked(int column)
  53. {
  54. sortColumn = column;
  55. Settings::write("userdata/pilSortColumn", column);
  56. }
  57. void PilotsWidget::on_newButton_clicked()
  58. {
  59. NewPilotDialog* np = new NewPilotDialog(Db::createNew, this);
  60. np->setAttribute(Qt::WA_DeleteOnClose);
  61. connect(np, SIGNAL(accepted()),
  62. this, SLOT(pilot_editing_finished()));
  63. np->exec();
  64. }
  65. void PilotsWidget::on_deletePushButton_clicked()
  66. {
  67. if(selectedPilots.length() == 1){
  68. for(const auto& selectedPilot : selectedPilots){
  69. if (selectedPilot > 0) {
  70. auto pil = Pilot(selectedPilot);
  71. if(!pil.remove()) {
  72. QVector<QString> columns = {"doft","dept","dest"};
  73. QVector<QString> details = Db::multiSelect(columns, "flights", "pic",
  74. QString::number(selectedPilot), Db::exactMatch);
  75. auto mb = QMessageBox(this);
  76. QString message = "\nUnable to delete. The following error has ocurred:\n\n";
  77. if(!details.isEmpty()){
  78. message += pil.error + QLatin1String("\n\n");
  79. message += "This is most likely the case because a flight exists with the Pilot "
  80. "you are trying to delete. You have to change or remove this flight "
  81. "before being able to remove this pilot from the database.\n\n"
  82. "The following flight(s) with this pilot have been found:\n\n";
  83. auto space = QLatin1Char(' ');
  84. for(int i = 0; i <= 30 && i <=details.length()-3; i+=3){
  85. message += details[i] + space
  86. + details[i+1] + space
  87. + details[i+2] + QLatin1String("\n");
  88. }
  89. }
  90. mb.setText(message);
  91. mb.setIcon(QMessageBox::Critical);
  92. mb.exec();
  93. }
  94. }
  95. }
  96. refreshModelAndView();
  97. } else {
  98. auto mb = QMessageBox(this);
  99. mb.setText("No Pilot selected.");
  100. mb.show();
  101. }
  102. }
  103. void PilotsWidget::pilot_editing_finished()
  104. {
  105. refreshModelAndView();
  106. }
  107. void PilotsWidget::refreshModelAndView()
  108. {
  109. ui->stackedWidget->addWidget(parent()->findChild<QWidget*>("welcomePage"));
  110. ui->stackedWidget->setCurrentWidget(parent()->findChild<QWidget*>("welcomePage"));
  111. model->setTable("viewPilots");
  112. model->setFilter("ID > 1");//to not allow editing of self, shall be done via settings
  113. model->select();
  114. QTableView *view = ui->tableView;
  115. view->setModel(model);
  116. view->setSelectionBehavior(QAbstractItemView::SelectRows);
  117. view->setSelectionMode(QAbstractItemView::SingleSelection);
  118. view->setEditTriggers(QAbstractItemView::NoEditTriggers);
  119. view->horizontalHeader()->setStretchLastSection(QHeaderView::Stretch);
  120. view->hideColumn(0);
  121. view->setColumnWidth(1, 180);
  122. view->setColumnWidth(2, 180);
  123. view->verticalHeader()->hide();
  124. view->setAlternatingRowColors(true);
  125. view->setSortingEnabled(true);
  126. view->sortByColumn(sortColumn, Qt::AscendingOrder);
  127. view->show();
  128. connect(ui->tableView->selectionModel(), SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
  129. this, SLOT(tableView_selectionChanged()));
  130. connect(ui->tableView->horizontalHeader(), SIGNAL(sectionClicked(int)),
  131. this, SLOT(tableView_headerClicked(int)));
  132. }
  133. void PilotsWidget::on_searchLineEdit_textChanged(const QString &arg1)
  134. {
  135. model->setFilter("\"" + ui->searchComboBox->currentText() + "\" LIKE \"%" + arg1 + "%\" AND ID > 1");
  136. }