#include "pilottableeditwidget.h" #include "src/database/database.h" #include "src/gui/dialogues/entryeditdialog.h" #include "src/gui/dialogues/newpilotdialog.h" #include "src/opl.h" #include PilotTableEditWidget::PilotTableEditWidget(QWidget *parent) : TableEditWidget(Horizontal, parent) {} void PilotTableEditWidget::setupModelAndView() { model = new QSqlTableModel(this, DB->database()); model->setTable(OPL::GLOBALS->getDbTableName(OPL::DbTable::Pilots)); model->select(); model->setHeaderData(COL_LASTNAME, Qt::Horizontal, tr("Last Name")); model->setHeaderData(COL_FIRSTNAME, Qt::Horizontal, tr("First Name")); model->setHeaderData(COL_COMPANY, Qt::Horizontal, tr("Company")); model->setFilter(QStringLiteral("%1 > 1").arg(OPL::PilotEntry::ROWID)); // hide self view->setModel(model); view->setSelectionMode(QAbstractItemView::SingleSelection); view->setSelectionBehavior(QAbstractItemView::SelectRows); view->setEditTriggers(QAbstractItemView::NoEditTriggers); view->horizontalHeader()->setStretchLastSection(QHeaderView::Stretch); view->resizeColumnsToContents(); view->verticalHeader()->hide(); view->setAlternatingRowColors(true); for(const int i : COLS_TO_HIDE) view->hideColumn(i); } void PilotTableEditWidget::setupUI() { // the base class does most of the setup TableEditWidget::setupUI(); // only need to set the table specific labels and combo box items addNewEntryPushButton->setText(tr("Add New Pilot")); deleteEntryPushButton->setText(tr("Delete Selected Pilot")); filterSelectionComboBox->addItems(FILTER_COLUMNS); } EntryEditDialog *PilotTableEditWidget::getEntryEditDialog(QWidget *parent) { return new NewPilotDialog(parent); } QString PilotTableEditWidget::deleteErrorString(int pilotId) { const QList foreign_key_constraints = DB->getForeignKeyConstraints(pilotId, OPL::DbTable::Pilots); QList constrained_flights; for (const auto &row_id : foreign_key_constraints) { constrained_flights.append(DB->getFlightEntry(row_id)); } // the error is a database error if (constrained_flights.isEmpty()) { return(tr("
Unable to delete.

The following error has ocurred:
%1" ).arg(DB->lastError.text())); } else { // the error is a foreign key constraint QString constrained_flights_string; for (int i=0; i")); if (i>10) { constrained_flights_string.append("
[...]
"); break; } } return(tr("Unable to delete.

" "This is most likely the case because a flight exists with the Pilot " "you are trying to delete as PIC.

" "%1 flight(s) with this pilot have been found:


" "%2" "

You have to change or remove the conflicting flight(s) " "before removing this pilot from the database.

" ).arg(QString::number(constrained_flights.length()), constrained_flights_string)); } } QString PilotTableEditWidget::confirmDeleteString(int rowId) { const auto entry = DB->getPilotEntry(rowId); return tr("You are deleting the following pilot:

" "%1, %2

Are you sure?").arg(entry.getLastName(), entry.getFirstName()); } void PilotTableEditWidget::filterTextChanged(const QString &filterText) { if(filterText.isEmpty()) { model->setFilter(QStringLiteral("%1 > 1").arg(OPL::PilotEntry::ROWID)); // hide self return; } int i = filterSelectionComboBox->currentIndex(); const QString filter = QLatin1Char('\"') + FILTER_COLUMN_NAMES.at(i) + QLatin1String("\" LIKE '%") + filterText + QLatin1String("%' AND ") + OPL::PilotEntry::ROWID + QLatin1String(" > 1"); model->setFilter(filter); }