2
0

tailtableeditwidget.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #include "tailtableeditwidget.h"
  2. #include "src/database/database.h"
  3. #include "src/gui/dialogues/newtaildialog.h"
  4. TailTableEditWidget::TailTableEditWidget(QWidget *parent)
  5. : TableEditWidget(Horizontal, parent)
  6. {}
  7. void TailTableEditWidget::setupModelAndView()
  8. {
  9. model = new QSqlTableModel(this, DB->database());
  10. model->setTable(OPL::GLOBALS->getDbTableName(OPL::DbTable::Tails));
  11. model->select();
  12. model->setHeaderData(COL_REGISTRATION, Qt::Horizontal, COLUMN_NAME_REGISTRATION);
  13. model->setHeaderData(COL_TYPE, Qt::Horizontal, COLUMN_NAME_TYPE);
  14. model->setHeaderData(COL_COMPANY, Qt::Horizontal, COLUMN_NAME_COMPANY);
  15. view->setModel(model);
  16. view->setSelectionMode(QAbstractItemView::SingleSelection);
  17. view->setSelectionBehavior(QAbstractItemView::SelectRows);
  18. view->setEditTriggers(QAbstractItemView::NoEditTriggers);
  19. view->horizontalHeader()->setStretchLastSection(QHeaderView::Stretch);
  20. view->resizeColumnsToContents();
  21. view->verticalHeader()->hide();
  22. view->setAlternatingRowColors(true);
  23. for(const int i : COLS_TO_HIDE)
  24. view->hideColumn(i);
  25. }
  26. void TailTableEditWidget::setupUI()
  27. {
  28. // the base class does most of the setup
  29. TableEditWidget::setupUI();
  30. // only need to set the table specific labels and combo box items
  31. addNewEntryPushButton->setText(tr("Add New Tail"));
  32. deleteEntryPushButton->setText(tr("Delete Selected Tail"));
  33. filterSelectionComboBox->addItems(FILTER_COLUMNS);
  34. }
  35. QString TailTableEditWidget::deleteErrorString(int rowId)
  36. {
  37. QList<int> foreign_key_constraints = DB->getForeignKeyConstraints(rowId,
  38. OPL::DbTable::Tails);
  39. QList<OPL::FlightEntry> constrained_flights;
  40. for (const auto &row_id : qAsConst(foreign_key_constraints)) {
  41. constrained_flights.append(DB->getFlightEntry(row_id));
  42. }
  43. QMessageBox message_box(this);
  44. if (constrained_flights.isEmpty()) {
  45. // error is a database error
  46. return tr("<br>Unable to delete.<br><br>The following error has ocurred: %1"
  47. ).arg(DB->lastError.text());
  48. } else {
  49. QString constrained_flights_string;
  50. for (int i=0; i<constrained_flights.length(); i++) {
  51. constrained_flights_string.append(constrained_flights[i].getFlightSummary()
  52. + QLatin1String("&nbsp;&nbsp;&nbsp;&nbsp;<br>"));
  53. if (i>10) {
  54. constrained_flights_string.append(QLatin1String("<br>[...]<br>"));
  55. break;
  56. }
  57. }
  58. return (tr("Unable to delete.<br><br>"
  59. "This is most likely the case because a flight exists with the aircraft "
  60. "you are trying to delete.<br><br>"
  61. "%1 flight(s) with this aircraft have been found:<br><br><br><b><tt>"
  62. "%2"
  63. "</b></tt><br><br>You have to change or remove the conflicting flight(s) "
  64. "before removing this aircraft from the database.<br><br>"
  65. ).arg(
  66. QString::number(constrained_flights.length()),
  67. constrained_flights_string)
  68. );
  69. }
  70. }
  71. QString TailTableEditWidget::confirmDeleteString(int rowId)
  72. {
  73. const auto entry = DB->getTailEntry(rowId);
  74. return tr("You are deleting the following aircraft:<br><br><b><tt>"
  75. "%1 (%2)</b></tt><br><br>Are you sure?"
  76. ).arg(
  77. entry.getData().value(OPL::TailEntry::REGISTRATION).toString(),
  78. entry.type()
  79. );
  80. }
  81. EntryEditDialog *TailTableEditWidget::getEntryEditDialog(QWidget *parent)
  82. {
  83. QString empty;
  84. return new NewTailDialog(empty, parent);
  85. }
  86. void TailTableEditWidget::filterTextChanged(const QString &filterString)
  87. {
  88. TODO << "not implemented.";
  89. }