|
@@ -19,13 +19,20 @@
|
|
|
#include "ui_logbookwidget.h"
|
|
|
#include "src/testing/adebug.h"
|
|
|
|
|
|
+const QMap<int, QString> FILTER_MAP = {
|
|
|
+ {0, "Date LIKE \"%"},
|
|
|
+ {1, "Dept LIKE \"%"},
|
|
|
+ {2, "Dest LIKE \"%"},
|
|
|
+ {3, "Registration LIKE \"%"},
|
|
|
+ {4, "\"Name PIC\" LIKE \"%"}
|
|
|
+};
|
|
|
+const auto NON_WORD_CHAR = QRegularExpression("\\W");
|
|
|
+
|
|
|
LogbookWidget::LogbookWidget(QWidget *parent) :
|
|
|
QWidget(parent),
|
|
|
ui(new Ui::LogbookWidget)
|
|
|
{
|
|
|
ui->setupUi(this);
|
|
|
- ui->filterDateEdit->setDate(QDate::currentDate());
|
|
|
- ui->filterDateEdit_2->setDate(QDate::currentDate());
|
|
|
ui->newFlightButton->setFocus();
|
|
|
|
|
|
//customContextMenu for tablewidget
|
|
@@ -33,10 +40,11 @@ LogbookWidget::LogbookWidget(QWidget *parent) :
|
|
|
menu->addAction(ui->actionEdit_Flight);
|
|
|
menu->addAction(ui->actionDelete_Flight);
|
|
|
|
|
|
- //message Box
|
|
|
- nope = new QMessageBox(this);
|
|
|
+ //Initialise message Box
|
|
|
+ messageBox = new QMessageBox(this);
|
|
|
|
|
|
- refreshView(ASettings::read("logbook/view").toInt());
|
|
|
+ prepareModelAndView(ASettings::read("logbook/view").toInt());
|
|
|
+ connectSignalsAndSlots();
|
|
|
}
|
|
|
|
|
|
LogbookWidget::~LogbookWidget()
|
|
@@ -48,40 +56,41 @@ LogbookWidget::~LogbookWidget()
|
|
|
* Functions
|
|
|
*/
|
|
|
|
|
|
-void LogbookWidget::refreshView(int view_id)
|
|
|
+void LogbookWidget::prepareModelAndView(int view_id)
|
|
|
{
|
|
|
switch (view_id) {
|
|
|
case 0:
|
|
|
- defaultView();
|
|
|
+ setupDefaultView();
|
|
|
break;
|
|
|
case 1:
|
|
|
- easaView();
|
|
|
+ setupEasaView();
|
|
|
break;
|
|
|
default:
|
|
|
- defaultView();
|
|
|
+ setupDefaultView();
|
|
|
}
|
|
|
- QTableView *view = ui->tableView;
|
|
|
- view->setSelectionBehavior(QAbstractItemView::SelectRows);
|
|
|
- view->setSelectionMode(QAbstractItemView::ExtendedSelection);
|
|
|
- view->setEditTriggers(QAbstractItemView::NoEditTriggers);
|
|
|
- view->setContextMenuPolicy(Qt::CustomContextMenu);
|
|
|
- view->horizontalHeader()->setStretchLastSection(QHeaderView::Stretch);
|
|
|
- view->verticalHeader()->hide();
|
|
|
- view->setAlternatingRowColors(true);
|
|
|
- view->hideColumn(0);
|
|
|
- connect(ui->tableView->selectionModel(), SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
|
|
|
- this, SLOT(tableView_selectionChanged()));
|
|
|
}
|
|
|
|
|
|
-void LogbookWidget::defaultView()
|
|
|
+void LogbookWidget::connectSignalsAndSlots()
|
|
|
+{
|
|
|
+ selection = view->selectionModel();
|
|
|
+ QObject::connect(view->selectionModel(), &QItemSelectionModel::selectionChanged,
|
|
|
+ this, &LogbookWidget::flightsTableView_selectionChanged);
|
|
|
+ using namespace experimental;
|
|
|
+ QObject::connect(aDB(), &ADataBase::sqlSuccessful,
|
|
|
+ this, &LogbookWidget::onDeletedSuccessfully);
|
|
|
+ QObject::connect(aDB(), &ADataBase::sqlError,
|
|
|
+ this, &LogbookWidget::onDeleteUnsuccessful);
|
|
|
+}
|
|
|
+
|
|
|
+void LogbookWidget::setupDefaultView()
|
|
|
{
|
|
|
DEB("Loading Default View...");
|
|
|
- QSqlTableModel *model = new QSqlTableModel;
|
|
|
- model->setTable("viewDefault");
|
|
|
- model->select();
|
|
|
+ displayModel = new QSqlTableModel;
|
|
|
+ displayModel->setTable("viewDefault");
|
|
|
+ displayModel->select();
|
|
|
|
|
|
- QTableView *view = ui->tableView;
|
|
|
- view->setModel(model);
|
|
|
+ view = ui->tableView;
|
|
|
+ view->setModel(displayModel);
|
|
|
|
|
|
view->setColumnWidth(1, 120);
|
|
|
view->setColumnWidth(2, 60);
|
|
@@ -94,18 +103,27 @@ void LogbookWidget::defaultView()
|
|
|
view->setColumnWidth(9, 120);
|
|
|
view->setColumnWidth(10, 90);
|
|
|
|
|
|
+ view->setSelectionBehavior(QAbstractItemView::SelectRows);
|
|
|
+ view->setSelectionMode(QAbstractItemView::ExtendedSelection);
|
|
|
+ view->setEditTriggers(QAbstractItemView::NoEditTriggers);
|
|
|
+ view->setContextMenuPolicy(Qt::CustomContextMenu);
|
|
|
+ view->horizontalHeader()->setStretchLastSection(QHeaderView::Stretch);
|
|
|
+ view->verticalHeader()->hide();
|
|
|
+ view->setAlternatingRowColors(true);
|
|
|
+ view->hideColumn(0);
|
|
|
+
|
|
|
view->show();
|
|
|
}
|
|
|
|
|
|
-void LogbookWidget::easaView()
|
|
|
+void LogbookWidget::setupEasaView()
|
|
|
{
|
|
|
DEB("Loading EASA View...");
|
|
|
- QSqlTableModel *model = new QSqlTableModel;
|
|
|
- model->setTable("viewEASA");
|
|
|
- model->select();
|
|
|
+ displayModel = new QSqlTableModel;
|
|
|
+ displayModel->setTable("viewEASA");
|
|
|
+ displayModel->select();
|
|
|
|
|
|
- QTableView *view = ui->tableView;
|
|
|
- view->setModel(model);
|
|
|
+ view = ui->tableView;
|
|
|
+ view->setModel(displayModel);
|
|
|
|
|
|
view->setColumnWidth(1,120);
|
|
|
view->setColumnWidth(2,60);
|
|
@@ -129,6 +147,15 @@ void LogbookWidget::easaView()
|
|
|
view->setColumnWidth(20,60);
|
|
|
view->setColumnWidth(21,120);
|
|
|
|
|
|
+ view->setSelectionBehavior(QAbstractItemView::SelectRows);
|
|
|
+ view->setSelectionMode(QAbstractItemView::ExtendedSelection);
|
|
|
+ view->setEditTriggers(QAbstractItemView::NoEditTriggers);
|
|
|
+ view->setContextMenuPolicy(Qt::CustomContextMenu);
|
|
|
+ view->horizontalHeader()->setStretchLastSection(QHeaderView::Stretch);
|
|
|
+ view->verticalHeader()->hide();
|
|
|
+ view->setAlternatingRowColors(true);
|
|
|
+ view->hideColumn(0);
|
|
|
+
|
|
|
view->show();
|
|
|
}
|
|
|
|
|
@@ -136,13 +163,11 @@ void LogbookWidget::easaView()
|
|
|
* Slots
|
|
|
*/
|
|
|
|
|
|
-void LogbookWidget::tableView_selectionChanged()//
|
|
|
+void LogbookWidget::flightsTableView_selectionChanged()//
|
|
|
{
|
|
|
- auto *selection = ui->tableView->selectionModel();
|
|
|
-
|
|
|
selectedFlights.clear();
|
|
|
for (const auto& row : selection->selectedRows()) {
|
|
|
- selectedFlights << row.data().toInt();
|
|
|
+ selectedFlights.append(row.data().toInt());
|
|
|
DEB("Selected Flight(s) with ID: " << selectedFlights);
|
|
|
}
|
|
|
}
|
|
@@ -152,7 +177,7 @@ void LogbookWidget::on_newFlightButton_clicked()
|
|
|
auto nf = new NewFlightDialog(this, Db::createNew);
|
|
|
nf->setAttribute(Qt::WA_DeleteOnClose);
|
|
|
nf->exec();
|
|
|
- refreshView(ASettings::read("logbook/view").toInt());
|
|
|
+ displayModel->select();
|
|
|
}
|
|
|
|
|
|
void LogbookWidget::on_editFlightButton_clicked()
|
|
@@ -161,33 +186,36 @@ void LogbookWidget::on_editFlightButton_clicked()
|
|
|
auto ef = new NewFlightDialog(this,Flight(selectedFlights.first()), Db::editExisting);
|
|
|
ef->setAttribute(Qt::WA_DeleteOnClose);
|
|
|
ef->exec();
|
|
|
- refreshView(ASettings::read("logbook/view").toInt());
|
|
|
+ displayModel->select();
|
|
|
} else if (selectedFlights.isEmpty()) {
|
|
|
- nope->setText("No flight selected.\n");
|
|
|
- nope->exec();
|
|
|
+ messageBox->setText("No flight selected.\n");
|
|
|
+ messageBox->exec();
|
|
|
} else {
|
|
|
- nope->setText("More than one flight selected.\n\nEditing multiple entries is not yet supported.");
|
|
|
- nope->exec();
|
|
|
+ messageBox->setText("More than one flight selected.\n\nEditing multiple entries is not yet supported.");
|
|
|
+ messageBox->exec();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
void LogbookWidget::on_deleteFlightPushButton_clicked()
|
|
|
{
|
|
|
DEB("Flights selected: " << selectedFlights.length());
|
|
|
- if (selectedFlights.length() > 0 && selectedFlights.length() < 11) {
|
|
|
- QVector<QString> columns = {
|
|
|
- "doft", "dept", "dest"
|
|
|
- };
|
|
|
- QVector<QString> details;
|
|
|
+ if (selectedFlights.length() == 0) {
|
|
|
+ messageBox->setIcon(QMessageBox::Information);
|
|
|
+ messageBox->setText("No Flight Selected.");
|
|
|
+ messageBox->exec();
|
|
|
+ return;
|
|
|
+ } else if (selectedFlights.length() > 0 && selectedFlights.length() < 11) {
|
|
|
+ QList<experimental::AFlightEntry> flights_list;
|
|
|
+
|
|
|
+ for (const auto &flight_id : selectedFlights) {
|
|
|
+ flights_list.append(experimental::aDB()->getFlightEntry(flight_id));
|
|
|
+ }
|
|
|
+
|
|
|
QString warningMsg = "The following flight(s) will be deleted:<br><br><b><tt>";
|
|
|
- for(const auto& selectedFlight : selectedFlights){
|
|
|
- details = Db::multiSelect(columns, "flights", "flight_id",
|
|
|
- QString::number(selectedFlight), Db::exactMatch);
|
|
|
- for (const auto &item : details) {
|
|
|
- warningMsg.append(item);
|
|
|
- warningMsg.append(' ');
|
|
|
- }
|
|
|
- warningMsg.append("<br>");
|
|
|
+
|
|
|
+ for (auto &flight : flights_list) {
|
|
|
+ warningMsg.append(flight.summary());
|
|
|
+ warningMsg.append(QLatin1String(" <br>"));
|
|
|
}
|
|
|
warningMsg.append("</b></tt><br>Deleting Flights is irreversible."
|
|
|
"<br>Do you want to proceed?");
|
|
@@ -200,21 +228,16 @@ void LogbookWidget::on_deleteFlightPushButton_clicked()
|
|
|
confirm.setText(warningMsg);
|
|
|
int reply = confirm.exec();
|
|
|
if (reply == QMessageBox::Yes) {
|
|
|
- for (const auto& selectedFlight : selectedFlights) {
|
|
|
- DEB("Deleting flight with ID# " << selectedFlight);
|
|
|
- auto entry = Flight(selectedFlight);
|
|
|
- entry.remove();
|
|
|
+ for (auto& flight : flights_list) {
|
|
|
+ DEB("Deleting flight: " << flight.summary());
|
|
|
+ experimental::aDB()->remove(flight);
|
|
|
}
|
|
|
- refreshView(ASettings::read("logbook/view").toInt());
|
|
|
+ displayModel->select();
|
|
|
}
|
|
|
- } else if (selectedFlights.length() == 0) {
|
|
|
- nope->setIcon(QMessageBox::Information);
|
|
|
- nope->setText("No Flight Selected.");
|
|
|
- nope->exec();
|
|
|
} else if (selectedFlights.length() > 10) {
|
|
|
auto& warningMsg = "You have selected " + QString::number(selectedFlights.length())
|
|
|
- + " flights.\n\n Deleting these flights is irreversible.\n\n"
|
|
|
- "Are you sure you want to proceed?";
|
|
|
+ + " flights.\n\n Deleting flights is irreversible.\n\n"
|
|
|
+ "Are you sure you want to proceed?";
|
|
|
QMessageBox confirm;
|
|
|
confirm.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
|
|
|
confirm.setDefaultButton(QMessageBox::No);
|
|
@@ -223,45 +246,37 @@ void LogbookWidget::on_deleteFlightPushButton_clicked()
|
|
|
confirm.setText(warningMsg);
|
|
|
int reply = confirm.exec();
|
|
|
if(reply == QMessageBox::Yes) {
|
|
|
- for (const auto& selectedFlight : selectedFlights) {
|
|
|
- DEB("Deleting flight with ID# " << selectedFlight);
|
|
|
- auto entry = Flight(selectedFlight);
|
|
|
- entry.remove();
|
|
|
+ QList<experimental::DataPosition> selected_flights;
|
|
|
+ for (const auto& flight_id : selectedFlights) {
|
|
|
+ selected_flights.append({"flights", flight_id});
|
|
|
}
|
|
|
- refreshView(ASettings::read("logbook/view").toInt());
|
|
|
+ experimental::aDB()->removeMany(selected_flights);
|
|
|
+ displayModel->select();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-void LogbookWidget::on_filterFlightsByDateButton_clicked()
|
|
|
+void LogbookWidget::on_tableView_customContextMenuRequested(const QPoint &pos)
|
|
|
{
|
|
|
- QDate date(ui->filterDateEdit->date());
|
|
|
- QString startdate = date.toString("yyyy-MM-dd");
|
|
|
- date = ui->filterDateEdit_2->date();
|
|
|
- QString enddate = date.toString("yyyy-MM-dd");
|
|
|
- QString datefilter = "Date BETWEEN '" + startdate + "' AND '" + enddate + QLatin1Char('\'');
|
|
|
-
|
|
|
- QSqlTableModel *DateFilteredModel = new QSqlTableModel;
|
|
|
- DateFilteredModel ->setTable("Logbook");
|
|
|
- DateFilteredModel ->setFilter(datefilter);
|
|
|
- DateFilteredModel->select();
|
|
|
-
|
|
|
- ui->tableView->setModel(DateFilteredModel);
|
|
|
+ menu->popup(ui->tableView->viewport()->mapToGlobal(pos));
|
|
|
}
|
|
|
|
|
|
-void LogbookWidget::on_showAllButton_clicked()
|
|
|
+void LogbookWidget::on_actionDelete_Flight_triggered()
|
|
|
{
|
|
|
- refreshView(ASettings::read("logbook/view").toInt());
|
|
|
+ emit ui->deleteFlightPushButton->clicked();
|
|
|
}
|
|
|
|
|
|
-void LogbookWidget::on_tableView_customContextMenuRequested(const QPoint &pos)
|
|
|
+void LogbookWidget::onDeletedSuccessfully()
|
|
|
{
|
|
|
- menu->popup(ui->tableView->viewport()->mapToGlobal(pos));
|
|
|
+ messageBox->setText(QString::number(selectedFlights.length()) + " flights have been deleted.");
|
|
|
+ messageBox->exec();
|
|
|
}
|
|
|
|
|
|
-void LogbookWidget::on_actionDelete_Flight_triggered()
|
|
|
+void LogbookWidget::onDeleteUnsuccessful(const QSqlError error)
|
|
|
{
|
|
|
- emit ui->deleteFlightPushButton->clicked();
|
|
|
+ messageBox->setText("Error deleting " + QString::number(selectedFlights.length())
|
|
|
+ + " flights.\n\nThe following error has ocurred:\n\n" + error.text());
|
|
|
+ messageBox->exec();
|
|
|
}
|
|
|
|
|
|
void LogbookWidget::on_actionEdit_Flight_triggered()
|
|
@@ -273,3 +288,38 @@ void LogbookWidget::on_tableView_doubleClicked()
|
|
|
{
|
|
|
emit ui->editFlightButton->clicked();
|
|
|
}
|
|
|
+
|
|
|
+void LogbookWidget::on_flightSearchComboBox_currentIndexChanged()
|
|
|
+{
|
|
|
+ emit ui->showAllButton->clicked();
|
|
|
+}
|
|
|
+
|
|
|
+void LogbookWidget::on_showAllButton_clicked()
|
|
|
+{
|
|
|
+ ui->flightSearchLlineEdit->setText(QString());
|
|
|
+ displayModel->setFilter(QString());
|
|
|
+ displayModel->select();
|
|
|
+}
|
|
|
+
|
|
|
+void LogbookWidget::on_flightSearchLlineEdit_textChanged(const QString &arg1)
|
|
|
+{
|
|
|
+ if(arg1.length() == 0) {
|
|
|
+ displayModel->setFilter("");
|
|
|
+ displayModel->select();
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (ui->flightSearchComboBox->currentIndex() < 3) {
|
|
|
+ displayModel->setFilter(FILTER_MAP.value(ui->flightSearchComboBox->currentIndex())
|
|
|
+ + arg1 + "%\"");
|
|
|
+ return;
|
|
|
+ } else if (ui->flightSearchComboBox->currentIndex() == 3) { // registration
|
|
|
+ displayModel->setFilter(FILTER_MAP.value(ui->flightSearchComboBox->currentIndex())
|
|
|
+ + arg1 + "%\"");
|
|
|
+ return;
|
|
|
+ } else if (ui->flightSearchComboBox->currentIndex() == 4) { // Name Pic
|
|
|
+ displayModel->setFilter(FILTER_MAP.value(ui->flightSearchComboBox->currentIndex())
|
|
|
+ + arg1 + "%\"");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+}
|