pilottableeditwidget.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #include "pilottableeditwidget.h"
  2. #include "src/database/database.h"
  3. #include "src/gui/dialogues/entryeditdialog.h"
  4. #include "src/gui/dialogues/newpilotdialog.h"
  5. #include "src/opl.h"
  6. #include <QGridLayout>
  7. PilotTableEditWidget::PilotTableEditWidget(QWidget *parent)
  8. : TableEditWidget(Horizontal, parent)
  9. {}
  10. void PilotTableEditWidget::setupModelAndView()
  11. {
  12. model = new QSqlTableModel(this, DB->database());
  13. model->setTable(OPL::GLOBALS->getDbTableName(OPL::DbTable::Pilots));
  14. model->select();
  15. model->setHeaderData(COL_LASTNAME, Qt::Horizontal, tr("Last Name"));
  16. model->setHeaderData(COL_FIRSTNAME, Qt::Horizontal, tr("First Name"));
  17. model->setHeaderData(COL_COMPANY, Qt::Horizontal, tr("Company"));
  18. model->setFilter(QStringLiteral("%1 > 1").arg(OPL::PilotEntry::ROWID)); // hide self
  19. view->setModel(model);
  20. view->setSelectionMode(QAbstractItemView::SingleSelection);
  21. view->setSelectionBehavior(QAbstractItemView::SelectRows);
  22. view->setEditTriggers(QAbstractItemView::NoEditTriggers);
  23. view->horizontalHeader()->setStretchLastSection(QHeaderView::Stretch);
  24. view->resizeColumnsToContents();
  25. view->verticalHeader()->hide();
  26. view->setAlternatingRowColors(true);
  27. for(const int i : COLS_TO_HIDE)
  28. view->hideColumn(i);
  29. }
  30. void PilotTableEditWidget::setupUI()
  31. {
  32. // the base class does most of the setup
  33. TableEditWidget::setupUI();
  34. // only need to set the table specific labels and combo box items
  35. addNewEntryPushButton->setText(tr("Add New Pilot"));
  36. deleteEntryPushButton->setText(tr("Delete Selected Pilot"));
  37. filterSelectionComboBox->addItems(FILTER_COLUMNS);
  38. }
  39. EntryEditDialog *PilotTableEditWidget::getEntryEditDialog(QWidget *parent)
  40. {
  41. return new NewPilotDialog(parent);
  42. }
  43. QString PilotTableEditWidget::deleteErrorString(int pilotId)
  44. {
  45. const QList<int> foreign_key_constraints = DB->getForeignKeyConstraints(pilotId,
  46. OPL::DbTable::Pilots);
  47. QList<OPL::FlightEntry> constrained_flights;
  48. for (const auto &row_id : foreign_key_constraints) {
  49. constrained_flights.append(DB->getFlightEntry(row_id));
  50. }
  51. // the error is a database error
  52. if (constrained_flights.isEmpty()) {
  53. return(tr("<br>Unable to delete.<br><br>The following error has ocurred:<br>%1"
  54. ).arg(DB->lastError.text()));
  55. } else {
  56. // the error is a foreign key constraint
  57. QString constrained_flights_string;
  58. for (int i=0; i<constrained_flights.length(); i++) {
  59. constrained_flights_string.append(constrained_flights[i].getFlightSummary() + QStringLiteral("&nbsp;&nbsp;&nbsp;&nbsp;<br>"));
  60. if (i>10) {
  61. constrained_flights_string.append("<br>[...]<br>");
  62. break;
  63. }
  64. }
  65. return(tr("Unable to delete.<br><br>"
  66. "This is most likely the case because a flight exists with the Pilot "
  67. "you are trying to delete as PIC.<br><br>"
  68. "%1 flight(s) with this pilot have been found:<br><br><br><b><tt>"
  69. "%2"
  70. "</b></tt><br><br>You have to change or remove the conflicting flight(s) "
  71. "before removing this pilot from the database.<br><br>"
  72. ).arg(QString::number(constrained_flights.length()),
  73. constrained_flights_string));
  74. }
  75. }
  76. QString PilotTableEditWidget::confirmDeleteString(int rowId)
  77. {
  78. const auto entry = DB->getPilotEntry(rowId);
  79. return tr("You are deleting the following pilot:<br><br><b><tt>"
  80. "%1, %2</b></tt><br><br>Are you sure?").arg(entry.getLastName(), entry.getFirstName());
  81. }
  82. void PilotTableEditWidget::filterTextChanged(const QString &filterText)
  83. {
  84. if(filterText.isEmpty()) {
  85. model->setFilter(QStringLiteral("%1 > 1").arg(OPL::PilotEntry::ROWID)); // hide self
  86. return;
  87. }
  88. int i = filterSelectionComboBox->currentIndex();
  89. const QString filter =
  90. QLatin1Char('\"')
  91. + FILTER_COLUMN_NAMES.at(i)
  92. + QLatin1String("\" LIKE '%")
  93. + filterText
  94. + QLatin1String("%' AND ")
  95. + OPL::PilotEntry::ROWID
  96. + QLatin1String(" > 1");
  97. model->setFilter(filter);
  98. }