|
@@ -19,13 +19,21 @@
|
|
|
#include "ui_pilotswidget.h"
|
|
|
#include "debug.h"
|
|
|
|
|
|
+using namespace experimental;
|
|
|
PilotsWidget::PilotsWidget(QWidget *parent) :
|
|
|
QWidget(parent),
|
|
|
ui(new Ui::PilotsWidget)
|
|
|
{
|
|
|
ui->setupUi(this);
|
|
|
sortColumn = Settings::read("userdata/pilSortColumn").toInt();
|
|
|
- refreshModelAndView();
|
|
|
+
|
|
|
+ setupModelAndView();
|
|
|
+ refreshModelAndView();
|
|
|
+
|
|
|
+ QObject::connect(ui->tableView->selectionModel(), &QItemSelectionModel::selectionChanged,
|
|
|
+ this, &PilotsWidget::tableView_selectionChanged);
|
|
|
+ QObject::connect(ui->tableView->horizontalHeader(), &QHeaderView::sectionClicked,
|
|
|
+ this, &PilotsWidget::tableView_headerClicked);
|
|
|
}
|
|
|
|
|
|
PilotsWidget::~PilotsWidget()
|
|
@@ -33,11 +41,44 @@ PilotsWidget::~PilotsWidget()
|
|
|
delete ui;
|
|
|
}
|
|
|
|
|
|
+void PilotsWidget::setupModelAndView()
|
|
|
+{
|
|
|
+ model->setTable("viewPilots");
|
|
|
+ model->setFilter("ID > 1");//to not allow editing of self, shall be done via settings
|
|
|
+ model->select();
|
|
|
+
|
|
|
+ view = ui->tableView;
|
|
|
+ view->setModel(model);
|
|
|
+ view->setSelectionBehavior(QAbstractItemView::SelectRows);
|
|
|
+ view->setSelectionMode(QAbstractItemView::SingleSelection);
|
|
|
+ view->setEditTriggers(QAbstractItemView::NoEditTriggers);
|
|
|
+ view->horizontalHeader()->setStretchLastSection(QHeaderView::Stretch);
|
|
|
+ view->hideColumn(0);
|
|
|
+ view->setColumnWidth(1, 180);
|
|
|
+ view->setColumnWidth(2, 180);
|
|
|
+ view->verticalHeader()->hide();
|
|
|
+ view->setAlternatingRowColors(true);
|
|
|
+ view->setSortingEnabled(true);
|
|
|
+ view->sortByColumn(sortColumn, Qt::AscendingOrder);
|
|
|
+}
|
|
|
+
|
|
|
+void PilotsWidget::refreshModelAndView()
|
|
|
+{
|
|
|
+ ui->stackedWidget->setCurrentWidget(this->findChild<QWidget*>("welcomePage"));
|
|
|
+ model->select();
|
|
|
+ view->show();
|
|
|
+}
|
|
|
+
|
|
|
+void PilotsWidget::on_searchLineEdit_textChanged(const QString &arg1)
|
|
|
+{
|
|
|
+ model->setFilter("\"" + ui->searchComboBox->currentText() + "\" LIKE \"%" + arg1 + "%\" AND ID > 1");
|
|
|
+}
|
|
|
+
|
|
|
void PilotsWidget::tableView_selectionChanged()//const QItemSelection &index, const QItemSelection &
|
|
|
{
|
|
|
if (this->findChild<NewPilotDialog*>("NewPilot") != nullptr) {
|
|
|
DEB("Selection changed. Deleting orphaned dialog.");
|
|
|
- delete this->findChild<NewPilotDialog*>("NewPilot");
|
|
|
+ delete this->findChild<NewPilotDialog*>();
|
|
|
/// [F] if the user changes the selection without making any changes,
|
|
|
/// if(selectedPilots.length() == 1) spawns a new dialog without the
|
|
|
/// old one being deleted, since neither accept() nor reject() was emitted.
|
|
@@ -54,13 +95,10 @@ void PilotsWidget::tableView_selectionChanged()//const QItemSelection &index, co
|
|
|
|
|
|
NewPilotDialog* np = new NewPilotDialog(selectedPilots.first(), this);
|
|
|
DEB("new dialog: " << np->objectName());
|
|
|
- using namespace experimental;
|
|
|
- QObject::connect(DB(), &DataBase::commitSuccessful,
|
|
|
- np, &NewPilotDialog::onCommitSuccessful);
|
|
|
- QObject::connect(DB(), &DataBase::commitUnsuccessful,
|
|
|
- np, &NewPilotDialog::onCommitUnsuccessful);
|
|
|
- connect(np, SIGNAL(accepted()), this, SLOT(pilot_editing_finished()));
|
|
|
- connect(np, SIGNAL(rejected()), this, SLOT(pilot_editing_finished()));
|
|
|
+ QObject::connect(np, &QDialog::accepted,
|
|
|
+ this, &PilotsWidget::pilot_editing_finished);
|
|
|
+ QObject::connect(np, &QDialog::rejected,
|
|
|
+ this, &PilotsWidget::pilot_editing_finished);
|
|
|
np->setWindowFlag(Qt::Widget);
|
|
|
np->setAttribute(Qt::WA_DeleteOnClose);
|
|
|
ui->stackedWidget->addWidget(np);
|
|
@@ -81,11 +119,6 @@ void PilotsWidget::on_newButton_clicked()
|
|
|
np->setAttribute(Qt::WA_DeleteOnClose);
|
|
|
connect(np, SIGNAL(accepted()), this, SLOT(pilot_editing_finished()));
|
|
|
connect(np, SIGNAL(rejected()), this, SLOT(pilot_editing_finished()));
|
|
|
- using namespace experimental;
|
|
|
- QObject::connect(DB(), &DataBase::commitSuccessful,
|
|
|
- np, &NewPilotDialog::onCommitSuccessful);
|
|
|
- QObject::connect(DB(), &DataBase::commitUnsuccessful,
|
|
|
- np, &NewPilotDialog::onCommitUnsuccessful);
|
|
|
np->exec();
|
|
|
}
|
|
|
|
|
@@ -100,77 +133,55 @@ void PilotsWidget::on_deletePushButton_clicked()
|
|
|
auto mb = QMessageBox(this);
|
|
|
mb.setText("Deleting multiple entries is currently not supported");
|
|
|
mb.exec();
|
|
|
- // to do: for (const auto& selectedPilot : selectedPilots) { do batchDelete }
|
|
|
+ /// [F] to do: for (const auto& row_id : selectedPilots) { do batchDelete }
|
|
|
+ /// I am not sure if enabling this functionality for this widget is a good idea.
|
|
|
+ /// On the one hand, deleting many entries could be useful in a scenario where
|
|
|
+ /// for example, the user has changed airlines and does not want to have his 'old'
|
|
|
+ /// colleagues polluting his logbook anymore.
|
|
|
+ /// On the other hand we could run into issues with foreign key constraints on the
|
|
|
+ /// flights table (see on_delete_unsuccessful) below
|
|
|
|
|
|
} else if (selectedPilots.length() == 1) {
|
|
|
- using namespace experimental;
|
|
|
auto entry = DB()->getPilotEntry(selectedPilots.first());
|
|
|
- if (!DB()->remove(entry)) {
|
|
|
- QVector<QString> columns = {"doft","dept","dest"};
|
|
|
- QVector<QString> details = Db::multiSelect(columns, "flights", "pic",
|
|
|
- QString::number(selectedPilots.first()), Db::exactMatch);
|
|
|
- auto mb = QMessageBox(this);
|
|
|
- QString message = "\nUnable to delete.\n\n";
|
|
|
- // [F] to do: create unsuccessful delete signal and include error info
|
|
|
- if(!details.isEmpty()){
|
|
|
- message += "This is most likely the case because a flight exists with the Pilot "
|
|
|
- "you are trying to delete. You have to change or remove this flight "
|
|
|
- "before being able to remove this pilot from the database.\n\n"
|
|
|
- "The following flight(s) with this pilot have been found:\n\n";
|
|
|
- auto space = QLatin1Char(' ');
|
|
|
- for(int i = 0; i <= 30 && i <=details.length()-3; i+=3){
|
|
|
- message += details[i] + space
|
|
|
- + details[i+1] + space
|
|
|
- + details[i+2] + QLatin1String("\n");
|
|
|
- }
|
|
|
- }
|
|
|
- mb.setText(message);
|
|
|
- mb.setIcon(QMessageBox::Critical);
|
|
|
- mb.exec();
|
|
|
+ DB()->remove(entry);
|
|
|
}
|
|
|
- }
|
|
|
-
|
|
|
refreshModelAndView();
|
|
|
|
|
|
}
|
|
|
|
|
|
-void PilotsWidget::pilot_editing_finished()
|
|
|
+void PilotsWidget::on_deleteUnsuccessful()
|
|
|
{
|
|
|
- refreshModelAndView();
|
|
|
-}
|
|
|
-
|
|
|
-void PilotsWidget::refreshModelAndView()
|
|
|
-{
|
|
|
- ui->stackedWidget->setCurrentWidget(this->findChild<QWidget*>("welcomePage"));
|
|
|
-
|
|
|
- model->setTable("viewPilots");
|
|
|
- model->setFilter("ID > 1");//to not allow editing of self, shall be done via settings
|
|
|
- model->select();
|
|
|
-
|
|
|
- QTableView *view = ui->tableView;
|
|
|
- view->setModel(model);
|
|
|
- view->setSelectionBehavior(QAbstractItemView::SelectRows);
|
|
|
- view->setSelectionMode(QAbstractItemView::SingleSelection);
|
|
|
- view->setEditTriggers(QAbstractItemView::NoEditTriggers);
|
|
|
- view->horizontalHeader()->setStretchLastSection(QHeaderView::Stretch);
|
|
|
- view->hideColumn(0);
|
|
|
- view->setColumnWidth(1, 180);
|
|
|
- view->setColumnWidth(2, 180);
|
|
|
- view->verticalHeader()->hide();
|
|
|
- view->setAlternatingRowColors(true);
|
|
|
- view->setSortingEnabled(true);
|
|
|
-
|
|
|
- view->sortByColumn(sortColumn, Qt::AscendingOrder);
|
|
|
-
|
|
|
- view->show();
|
|
|
-
|
|
|
- connect(ui->tableView->selectionModel(), SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
|
|
|
- this, SLOT(tableView_selectionChanged()));
|
|
|
- connect(ui->tableView->horizontalHeader(), SIGNAL(sectionClicked(int)),
|
|
|
- this, SLOT(tableView_headerClicked(int)));
|
|
|
+ /// [F] to do: migrate to new DB class
|
|
|
+ ///
|
|
|
+ /// This query should normally only fail because of a foreign key constraint,
|
|
|
+ /// i.e. a flight exists with this pilot. So we need to make the user aware
|
|
|
+ /// of this and need to display some flight information regarding the flight
|
|
|
+ /// causing the constraint.
|
|
|
+ ///
|
|
|
+ /// The information that needs to be displayed could be extracted from
|
|
|
+ /// a FlightEntry.
|
|
|
+ QVector<QString> columns = {"doft","dept","dest"};
|
|
|
+ QVector<QString> details = Db::multiSelect(columns, "flights", "pic",
|
|
|
+ QString::number(selectedPilots.first()), Db::exactMatch);
|
|
|
+ auto mb = QMessageBox(this);
|
|
|
+ QString message = "\nUnable to delete.\n\n";
|
|
|
+ if(!details.isEmpty()){
|
|
|
+ message += "This is most likely the case because a flight exists with the Pilot "
|
|
|
+ "you are trying to delete. You have to change or remove this flight "
|
|
|
+ "before being able to remove this pilot from the database.\n\n"
|
|
|
+ "The following flight(s) with this pilot have been found:\n\n";
|
|
|
+ for(int i = 0; i <= 30 && i <=details.length()-3; i+=3){
|
|
|
+ message += details[i] + QLatin1Char(' ')
|
|
|
+ + details[i+1] + QLatin1Char(' ')
|
|
|
+ + details[i+2] + QLatin1String("\n");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ mb.setText(message);
|
|
|
+ mb.setIcon(QMessageBox::Critical);
|
|
|
+ mb.exec();
|
|
|
}
|
|
|
|
|
|
-void PilotsWidget::on_searchLineEdit_textChanged(const QString &arg1)
|
|
|
+void PilotsWidget::pilot_editing_finished()
|
|
|
{
|
|
|
- model->setFilter("\"" + ui->searchComboBox->currentText() + "\" LIKE \"%" + arg1 + "%\" AND ID > 1");
|
|
|
+ refreshModelAndView();
|
|
|
}
|