|  | @@ -27,7 +27,8 @@ AircraftWidget::AircraftWidget(QWidget *parent) :
 | 
	
		
			
				|  |  |      ui(new Ui::AircraftWidget)
 | 
	
		
			
				|  |  |  {
 | 
	
		
			
				|  |  |      ui->setupUi(this);
 | 
	
		
			
				|  |  | -    refreshView();
 | 
	
		
			
				|  |  | +    sortColumn = Settings::read("userdata/acSortColumn").toInt();
 | 
	
		
			
				|  |  | +    refreshModelAndView();
 | 
	
		
			
				|  |  |  }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  AircraftWidget::~AircraftWidget()
 | 
	
	
		
			
				|  | @@ -35,55 +36,88 @@ AircraftWidget::~AircraftWidget()
 | 
	
		
			
				|  |  |      delete ui;
 | 
	
		
			
				|  |  |  }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -void AircraftWidget::setSelectedAircraft(const qint32 &value)
 | 
	
		
			
				|  |  | -{
 | 
	
		
			
				|  |  | -    selectedAircraft = value;
 | 
	
		
			
				|  |  | -}
 | 
	
		
			
				|  |  | +/*
 | 
	
		
			
				|  |  | + * Functions
 | 
	
		
			
				|  |  | + */
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -void AircraftWidget::tableView_selectionChanged(const QItemSelection &index, const QItemSelection &)
 | 
	
		
			
				|  |  | +void AircraftWidget::refreshModelAndView()
 | 
	
		
			
				|  |  |  {
 | 
	
		
			
				|  |  | -    setSelectedAircraft(index.indexes()[0].data().toInt());
 | 
	
		
			
				|  |  | -    DEB("Selected aircraft with ID#: " << selectedAircraft);
 | 
	
		
			
				|  |  | +    ui->stackedWidget->setCurrentIndex(0);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    model->setTable("viewTails");
 | 
	
		
			
				|  |  | +    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);
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -    auto nt = new NewTail(Aircraft(selectedAircraft), Db::editExisting, this);
 | 
	
		
			
				|  |  | -    connect(nt, SIGNAL(accepted()), this, SLOT(acft_accepted()));
 | 
	
		
			
				|  |  | -    connect(nt, SIGNAL(rejected()), this, SLOT(acft_accepted()));
 | 
	
		
			
				|  |  | +    view->show();
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -    nt->setWindowFlag(Qt::Widget);
 | 
	
		
			
				|  |  | -    ui->stackedWidget->addWidget(nt);
 | 
	
		
			
				|  |  | -    ui->stackedWidget->setCurrentWidget(nt);
 | 
	
		
			
				|  |  | +    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)));
 | 
	
		
			
				|  |  |  }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +/*
 | 
	
		
			
				|  |  | + * Slots
 | 
	
		
			
				|  |  | + */
 | 
	
		
			
				|  |  |  void AircraftWidget::on_deleteButton_clicked()
 | 
	
		
			
				|  |  |  {
 | 
	
		
			
				|  |  | -    if (selectedAircraft > 0) {
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -        auto ac = new Aircraft(selectedAircraft);
 | 
	
		
			
				|  |  | -        if(!ac->remove()) {
 | 
	
		
			
				|  |  | -            QVector<QString> columns = {"doft","dept","dest"};
 | 
	
		
			
				|  |  | -            QVector<QString> details = Db::multiSelect(columns, "flights", "acft",
 | 
	
		
			
				|  |  | -                                                       QString::number(selectedAircraft), Db::exactMatch);
 | 
	
		
			
				|  |  | -            auto mb = new QMessageBox(this);
 | 
	
		
			
				|  |  | -            QString message = "\nUnable to delete. The following error has ocurred:\n\n";
 | 
	
		
			
				|  |  | -            if(!details.isEmpty()){
 | 
	
		
			
				|  |  | -                message += ac->error + QLatin1String("\n\n");
 | 
	
		
			
				|  |  | -                message += "This is most likely the case because a flight exists with the aircaft "
 | 
	
		
			
				|  |  | -                           "you are trying to delete. You have to change or remove this flight "
 | 
	
		
			
				|  |  | -                           "before being able to remove this aircraft.\n\n"
 | 
	
		
			
				|  |  | -                           "The following flight(s) with this tail 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");
 | 
	
		
			
				|  |  | +    if (selectedTails.length() > 0) {
 | 
	
		
			
				|  |  | +        for(const auto& selectedTail : selectedTails){
 | 
	
		
			
				|  |  | +            auto ac = Aircraft(selectedTail);
 | 
	
		
			
				|  |  | +            auto& warningMsg = "Deleting Aircraft with registration:<br><center><b>"
 | 
	
		
			
				|  |  | +                    + ac.data.value("registration")
 | 
	
		
			
				|  |  | +                    + "<br></center></b>Do you want to proceed?";
 | 
	
		
			
				|  |  | +            QMessageBox confirm;
 | 
	
		
			
				|  |  | +            confirm.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
 | 
	
		
			
				|  |  | +            confirm.setDefaultButton(QMessageBox::No);
 | 
	
		
			
				|  |  | +            confirm.setIcon(QMessageBox::Question);
 | 
	
		
			
				|  |  | +            confirm.setWindowTitle("Delete Aircraft");
 | 
	
		
			
				|  |  | +            confirm.setText(warningMsg);
 | 
	
		
			
				|  |  | +            int reply = confirm.exec();
 | 
	
		
			
				|  |  | +            if(reply == QMessageBox::Yes) {
 | 
	
		
			
				|  |  | +                if(!ac.remove()) {
 | 
	
		
			
				|  |  | +                    QVector<QString> columns = {"doft","dept","dest"};
 | 
	
		
			
				|  |  | +                    QVector<QString> details = Db::multiSelect(columns, "flights", "acft",
 | 
	
		
			
				|  |  | +                                                               QString::number(selectedTail), Db::exactMatch);
 | 
	
		
			
				|  |  | +                    auto mb = new QMessageBox(this);
 | 
	
		
			
				|  |  | +                    QString message = "\nUnable to delete. The following error has ocurred:\n\n";
 | 
	
		
			
				|  |  | +                    if(!details.isEmpty()){
 | 
	
		
			
				|  |  | +                        message += ac.error + QLatin1String("\n\n");
 | 
	
		
			
				|  |  | +                        message += "This is most likely the case because a flight exists with the aircaft "
 | 
	
		
			
				|  |  | +                                   "you are trying to delete. In order to maintain database integrity, you have to\n"
 | 
	
		
			
				|  |  | +                                   "change or remove this flight before being able to remove this aircraft.\n\n"
 | 
	
		
			
				|  |  | +                                   "The following flight(s) with this tail 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->show();
 | 
	
		
			
				|  |  |                  }
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  | -            mb->setText(message);
 | 
	
		
			
				|  |  | -            mb->setIcon(QMessageBox::Critical);
 | 
	
		
			
				|  |  | -            mb->show();
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  | -        refreshView();
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | +        refreshModelAndView();
 | 
	
		
			
				|  |  |      } else {
 | 
	
		
			
				|  |  |          auto mb = new QMessageBox(this);
 | 
	
		
			
				|  |  |          mb->setText("No aircraft selected.");
 | 
	
	
		
			
				|  | @@ -94,53 +128,44 @@ void AircraftWidget::on_deleteButton_clicked()
 | 
	
		
			
				|  |  |  void AircraftWidget::on_newButton_clicked()
 | 
	
		
			
				|  |  |  {
 | 
	
		
			
				|  |  |      auto nt = new NewTail(QString(), Db::createNew, this);
 | 
	
		
			
				|  |  | -    connect(nt,
 | 
	
		
			
				|  |  | -            SIGNAL(accepted()), this,
 | 
	
		
			
				|  |  | -            SLOT(acft_accepted()));
 | 
	
		
			
				|  |  | +    connect(nt, SIGNAL(accepted()), this, SLOT(acft_editing_finished()));
 | 
	
		
			
				|  |  | +    connect(nt, SIGNAL(rejected()), this, SLOT(acft_editing_finished()));
 | 
	
		
			
				|  |  |      nt->show();
 | 
	
		
			
				|  |  |  }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -void AircraftWidget::acft_accepted()
 | 
	
		
			
				|  |  | +void AircraftWidget::on_searchLineEdit_textChanged(const QString &arg1)
 | 
	
		
			
				|  |  |  {
 | 
	
		
			
				|  |  | -    DEB("Refreshing View...");
 | 
	
		
			
				|  |  | -    refreshView();
 | 
	
		
			
				|  |  | +    if(ui->searchComboBox->currentIndex() == 0){
 | 
	
		
			
				|  |  | +        ui->searchLineEdit->setText(arg1.toUpper());
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +    model->setFilter(ui->searchComboBox->currentText() + " LIKE \"%" + arg1 + "%\"");
 | 
	
		
			
				|  |  |  }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -void AircraftWidget::refreshView()
 | 
	
		
			
				|  |  | +void AircraftWidget::tableView_selectionChanged()
 | 
	
		
			
				|  |  |  {
 | 
	
		
			
				|  |  | -    QString welcomeMessage = "Select an Aircraft to show or edit details.";
 | 
	
		
			
				|  |  | -    QWidget *start = new QWidget();
 | 
	
		
			
				|  |  | -    start->setObjectName("welcomeAC");
 | 
	
		
			
				|  |  | -    QLabel *label = new QLabel(welcomeMessage);
 | 
	
		
			
				|  |  | -    label->setAlignment(Qt::AlignCenter);
 | 
	
		
			
				|  |  | -    QHBoxLayout *layout = new QHBoxLayout();
 | 
	
		
			
				|  |  | -    layout->addWidget(label);
 | 
	
		
			
				|  |  | -    start->setLayout(layout);
 | 
	
		
			
				|  |  | -    ui->stackedWidget->addWidget(start);
 | 
	
		
			
				|  |  | -    ui->stackedWidget->setCurrentWidget(start);
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -    QSqlTableModel *model = new QSqlTableModel;
 | 
	
		
			
				|  |  | -    model->setTable("viewTails");
 | 
	
		
			
				|  |  | -    model->select();
 | 
	
		
			
				|  |  | +    auto *selection = ui->tableView->selectionModel();
 | 
	
		
			
				|  |  | +    selectedTails.clear();
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -    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->setColumnWidth(0, 60);
 | 
	
		
			
				|  |  | -    view->setColumnWidth(1, 120);
 | 
	
		
			
				|  |  | -    view->setColumnWidth(2, 180);
 | 
	
		
			
				|  |  | -    view->verticalHeader()->hide();
 | 
	
		
			
				|  |  | -    view->setAlternatingRowColors(true);
 | 
	
		
			
				|  |  | -    view->setSortingEnabled(true);
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -    view->sortByColumn(Settings::read("userdata/acSortColumn").toInt(), Qt::AscendingOrder);
 | 
	
		
			
				|  |  | +    for (const auto& row : selection->selectedRows()) {
 | 
	
		
			
				|  |  | +        selectedTails << row.data().toInt();
 | 
	
		
			
				|  |  | +        DEB("Selected Tails(s) with ID: " << selectedTails);
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +    if(selectedTails.length() == 1) {
 | 
	
		
			
				|  |  | +        auto nt = new NewTail(Aircraft(selectedTails.first()), Db::editExisting, this);
 | 
	
		
			
				|  |  | +        connect(nt, SIGNAL(accepted()), this, SLOT(acft_editing_finished()));
 | 
	
		
			
				|  |  | +        connect(nt, SIGNAL(rejected()), this, SLOT(acft_editing_finished()));
 | 
	
		
			
				|  |  | +        nt->setWindowFlag(Qt::Widget);
 | 
	
		
			
				|  |  | +        ui->stackedWidget->addWidget(nt);
 | 
	
		
			
				|  |  | +        ui->stackedWidget->setCurrentWidget(nt);
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -    view->show();
 | 
	
		
			
				|  |  | +void AircraftWidget::tableView_headerClicked(int column)
 | 
	
		
			
				|  |  | +{
 | 
	
		
			
				|  |  | +    sortColumn = column;
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -    connect(ui->tableView->selectionModel(),
 | 
	
		
			
				|  |  | -            SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
 | 
	
		
			
				|  |  | -            SLOT(tableView_selectionChanged(const QItemSelection &, const QItemSelection &)));
 | 
	
		
			
				|  |  | +void AircraftWidget::acft_editing_finished()
 | 
	
		
			
				|  |  | +{
 | 
	
		
			
				|  |  | +    refreshModelAndView();
 | 
	
		
			
				|  |  |  }
 |