airportwidget.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #include "airportwidget.h"
  2. #include "ui_airportwidget.h"
  3. #include "src/gui/dialogues/newairportdialog.h"
  4. #include "src/database/database.h"
  5. AirportWidget::AirportWidget(QWidget *parent) :
  6. QWidget(parent),
  7. ui(new Ui::AirportWidget)
  8. {
  9. ui->setupUi(this);
  10. setupModelAndeView();
  11. setupSearch();
  12. QObject::connect(DB, &OPL::Database::dataBaseUpdated,
  13. this, &AirportWidget::refresh);
  14. QObject::connect(view->selectionModel(), &QItemSelectionModel::selectionChanged,
  15. this, &AirportWidget::onSelectionChanged);
  16. QObject::connect(view, &QTableView::doubleClicked,
  17. this, &AirportWidget::on_editAirportPushButton_clicked);
  18. }
  19. AirportWidget::~AirportWidget()
  20. {
  21. delete ui;
  22. }
  23. void AirportWidget::setupModelAndeView()
  24. {
  25. model = new QSqlTableModel(this);
  26. model->setEditStrategy(QSqlTableModel::OnFieldChange);
  27. model->setTable(TABLE_NAME);
  28. model->select();
  29. model->sort(1, Qt::AscendingOrder);
  30. view = ui->tableView;
  31. view->setModel(model);
  32. view->horizontalHeader()->setStretchLastSection(QHeaderView::Stretch);
  33. view->setSelectionBehavior(QAbstractItemView::SelectRows);
  34. view->setSelectionMode(QAbstractItemView::ExtendedSelection);
  35. view->setEditTriggers(QAbstractItemView::NoEditTriggers);
  36. view->hideColumn(0);
  37. view->resizeColumnsToContents();
  38. view->verticalHeader()->hide();
  39. view->setAlternatingRowColors(true);
  40. view->show();
  41. }
  42. void AirportWidget::setupSearch()
  43. {
  44. for (const auto &item : HEADER_MAP)
  45. ui->searchComboBox->addItem(item);
  46. }
  47. void AirportWidget::on_searchLineEdit_textChanged(const QString &arg1)
  48. {
  49. if(arg1.length() == 0) {
  50. model->setFilter("");
  51. model->select();
  52. return;
  53. }
  54. model->setFilter(FILTER_MAP.value(ui->searchComboBox->currentIndex())
  55. + arg1 + QLatin1String("%\""));
  56. DEB << "Filter set: " << FILTER_MAP.value(ui->searchComboBox->currentIndex())
  57. + arg1 + QLatin1String("%\"");
  58. }
  59. void AirportWidget::on_searchComboBox_currentIndexChanged(int index)
  60. {
  61. ui->searchLineEdit->setText(QString());
  62. }
  63. void AirportWidget::on_newAirportPushButton_clicked()
  64. {
  65. auto ap_dialog = new NewAirportDialog(this);
  66. if (ap_dialog->exec() == QDialog::Accepted) {
  67. model->select();
  68. emit DB->dataBaseUpdated(OPL::DbTable::Airports);
  69. }
  70. }
  71. void AirportWidget::on_deletePushButton_clicked()
  72. {
  73. DEB << "Airports selected: " << selectedEntries.length();
  74. if (selectedEntries.length() == 0) {
  75. WARN(tr("<br>No Airport selected.<br>"));
  76. return;
  77. } else if (selectedEntries.length() > 0 && selectedEntries.length() <= 10) {
  78. QStringList selected_airport_names;
  79. for (const auto row_id : qAsConst(selectedEntries)) {
  80. const auto data = DB->getRowData(OPL::DbTable::Airports, row_id);
  81. selected_airport_names.append(data.value(OPL::AirportEntry::NAME).toString());
  82. }
  83. QString selected_airports_string;
  84. for (auto &name : selected_airport_names) {
  85. selected_airports_string.append(name);
  86. selected_airports_string.append(QStringLiteral("&nbsp;&nbsp;&nbsp;&nbsp;<br>"));
  87. }
  88. QMessageBox confirm(this);
  89. confirm.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
  90. confirm.setDefaultButton(QMessageBox::No);
  91. confirm.setIcon(QMessageBox::Question);
  92. confirm.setWindowTitle("Delete Airport");
  93. confirm.setText(tr("The following airport(s) will be deleted:<br><br><b><tt>"
  94. "%1<br></b></tt>"
  95. "Deleting airports is irreversible.<br>Do you want to proceed?"
  96. ).arg(selected_airports_string));
  97. if (confirm.exec() == QMessageBox::Yes) {
  98. for (auto& row_id : selectedEntries) {
  99. if(!DB->remove(OPL::Row(OPL::DbTable::Airports, row_id))) {
  100. WARN(tr("<br>Unable to delete.<br><br>The following error has ocurred: %1"
  101. ).arg(DB->lastError.text()));
  102. return;
  103. }
  104. }
  105. INFO(tr("%1 Airports have been deleted successfully."
  106. ).arg(QString::number(selectedEntries.length())));
  107. model->select();
  108. }
  109. } else if (selectedEntries.length() > 10) {
  110. QMessageBox confirm;
  111. confirm.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
  112. confirm.setDefaultButton(QMessageBox::No);
  113. confirm.setIcon(QMessageBox::Warning);
  114. confirm.setWindowTitle("Delete Airports");
  115. confirm.setText(tr("You have selected %1 airports.<br><br>"
  116. "Deleting airports is irreversible.<br><br>"
  117. "Are you sure you want to proceed?"
  118. ).arg(QString::number(selectedEntries.length())));
  119. if(confirm.exec() == QMessageBox::Yes) {
  120. if (!DB->removeMany(OPL::DbTable::Airports, selectedEntries)) {
  121. WARN(tr("Unable to delete. No changes have been made to the database. The following error has ocurred:<br><br>%1").arg(DB->lastError.text()));
  122. return;
  123. }
  124. INFO(tr("%1 Airports have been deleted successfully."
  125. ).arg(QString::number(selectedEntries.length())));
  126. model->select();
  127. }
  128. model->select();
  129. }
  130. }
  131. void AirportWidget::on_editAirportPushButton_clicked()
  132. {
  133. if (selectedEntries.isEmpty()) {
  134. WARN(tr("No airport selected."));
  135. return;
  136. }
  137. auto apd = new NewAirportDialog(selectedEntries.first(), this);
  138. apd->exec();
  139. }
  140. void AirportWidget::refresh()
  141. {
  142. model->select();
  143. }
  144. void AirportWidget::onSelectionChanged()
  145. {
  146. selectedEntries.clear();
  147. for (const auto& row : view->selectionModel()->selectedRows()) {
  148. selectedEntries.append(row.data().toInt());
  149. DEB << "Selected Airport(s) with ID: " << selectedEntries;
  150. }
  151. }