Преглед на файлове

added customContextMenu for tableview in logbookwidget and edited actions

fiffty-50 преди 4 години
родител
ревизия
1deba57a7b

BIN
assets/database/logbook.db


+ 41 - 8
src/gui/dialogues/newflight.cpp

@@ -45,7 +45,7 @@ static const auto TIME_VALID_RGX       = QRegularExpression("([01]?[0-9]|2[0-3])
 static const auto LOC_VALID_RGX        = QRegularExpression(IATA_RX + "|" + ICAO_RX);
 static const auto AIRCRAFT_VALID_RGX   = QRegularExpression("[A-Z0-9]+\\-?[A-Z0-9]+");
 static const auto PILOT_NAME_VALID_RGX = QRegularExpression(SELF_RX + QLatin1Char('|')
-                                                     + NAME_RX + ADD_NAME_RX + ADD_NAME_RX + ADD_NAME_RX + ",\\s?" // up to 4 first names
+                                                     + NAME_RX + ADD_NAME_RX + ADD_NAME_RX + ADD_NAME_RX + ",?\\s?" // up to 4 first names
                                                      + NAME_RX + ADD_NAME_RX + ADD_NAME_RX + ADD_NAME_RX );// up to 4 last names
 
 /// Invalid characters (validators keep text even if it returns Invalid, see `onInputRejected` below)
@@ -415,7 +415,7 @@ void NewFlight::readSettings()
  * \brief NewFlight::addNewPilotMessageBox If the user input is not in the pilotNameList, the user
  * is prompted if he wants to add a new entry to the database
  */
-void NewFlight::addNewPilotMessageBox()
+void NewFlight::addNewPilotMessageBox(QLineEdit *parent)
 {
     QMessageBox::StandardButton reply;
     reply = QMessageBox::question(this, "No Pilot found",
@@ -430,9 +430,38 @@ void NewFlight::addNewPilotMessageBox()
         // create and open new pilot dialog
         auto np = NewPilot(Db::createNew, this);
         np.exec();
+        QString statement = "SELECT MAX(pilot_id)  FROM pilots";
+        QString id = Db::customQuery(statement,1).first();
+        Pilot newPilot = Pilot(id.toInt());
+        parent->setText(newPilot.data.value("displayname"));
+        emit parent->editingFinished();
+    }
+}
+/*!
+ * \brief NewFlight::addNewAircraftMessageBox If the user input is not in the aircraftList, the user
+ * is prompted if he wants to add a new entry to the database
+ */
+void NewFlight::addNewAircraftMessageBox(QLineEdit *parent)
+{
+    QMessageBox::StandardButton reply;
+    reply = QMessageBox::question(this, "No Aircraft found",
+                                  "No aircraft with this registration found.<br>"
+                                  "If this is the first time you log a flight with this aircraft, you have to "
+                                  "add the registration to the database first.<br><br>Would you like to add a new aircraft to the database?",
+                                  QMessageBox::Yes|QMessageBox::No);
+    if (reply == QMessageBox::Yes)
+    {
+        DEBUG("Add new aircraft selected");
+        // create and open new aircraft dialog
+        auto na = NewTail(ui->acftLineEdit->text(), Db::createNew, this);
+        na.exec();
+        QString statement = "SELECT MAX(tail_id)  FROM tails";
+        QString id = Db::customQuery(statement,1).first();
+        auto newAcft = Aircraft(id.toInt());
+        parent->setText(newAcft.data.value("registration"));
+        emit parent->editingFinished();
     }
 }
-
 /// Input Verification and Collection
 
 /*!
@@ -821,11 +850,15 @@ bool NewFlight::verifyInput()
         return true;
     }
 }
+
 /*!
+ * ============================================================================
  * ============================================================================
  * Slots
  * ============================================================================
+ * ============================================================================
  */
+
 void NewFlight::on_buttonBox_accepted()
 {
     DEBUG("OK pressed");
@@ -1036,7 +1069,7 @@ void NewFlight::on_tonbTimeLineEdit_editingFinished()
 
 /// Date
 
-void NewFlight::on_newDoft_editingFinished()
+void NewFlight::on_doftTimeEdit_editingFinished()
 {
     update();
 }
@@ -1066,8 +1099,8 @@ void NewFlight::on_acftLineEdit_editingFinished()
                     Db::singleSelect(column,"tails","registration",text,Db::exactMatch));
         update();
     }else{
-        DEBUG("Registration not in List!");
         emit line_edit->inputRejected();
+        addNewAircraftMessageBox(line_edit);
     }
 }
 
@@ -1105,7 +1138,7 @@ void NewFlight::on_picNameLineEdit_editingFinished()
         {
             DEBUG("Pilot not found.");
             emit line_edit->inputRejected();
-            addNewPilotMessageBox();
+            addNewPilotMessageBox(line_edit);
         }
     }
 }
@@ -1149,7 +1182,7 @@ void NewFlight::on_secondPilotNameLineEdit_editingFinished()
         {
             DEBUG("Pilot not found.");
             emit line_edit->inputRejected();
-            addNewPilotMessageBox();
+            addNewPilotMessageBox(line_edit);
         }
     }
 }
@@ -1183,7 +1216,7 @@ void NewFlight::on_thirdPilotNameLineEdit_editingFinished()
         {
             DEBUG("Pilot not found.");
             emit line_edit->inputRejected();
-            addNewPilotMessageBox();
+            addNewPilotMessageBox(line_edit);
         }
     }
 }

+ 5 - 2
src/gui/dialogues/newflight.h

@@ -41,6 +41,7 @@
 #include "src/classes/calc.h"
 
 #include "src/gui/dialogues/newpilot.h"
+#include "src/gui/dialogues/newtail.h"
 
 class SqlColumnNum{
 public:
@@ -93,7 +94,9 @@ private:
 
     void setupLineEdit(QLineEdit* line_edit, LineEditSettings settings);
 
-    void addNewPilotMessageBox();
+    void addNewPilotMessageBox(QLineEdit *parent);
+
+    void addNewAircraftMessageBox(QLineEdit *parent);
 
     void readSettings();
 
@@ -135,7 +138,7 @@ private slots:
     void on_deptLocLineEdit_textEdited(const QString &arg1);
     void on_destLocLineEdit_textEdited(const QString &arg1);
 
-    void on_newDoft_editingFinished();
+    void on_doftTimeEdit_editingFinished();
 
     void on_acftLineEdit_inputRejected();
     void on_acftLineEdit_editingFinished();

+ 1 - 1
src/gui/dialogues/newflight.ui

@@ -17,7 +17,7 @@
    <item row="0" column="0" colspan="3">
     <widget class="QTabWidget" name="flightDataTabWidget">
      <property name="currentIndex">
-      <number>1</number>
+      <number>0</number>
      </property>
      <widget class="QWidget" name="flightDataTab">
       <attribute name="title">

+ 90 - 27
src/gui/widgets/logbookwidget.cpp

@@ -30,6 +30,11 @@ LogbookWidget::LogbookWidget(QWidget *parent) :
     ui->filterDateEdit->setDate(QDate::currentDate());
     ui->filterDateEdit_2->setDate(QDate::currentDate());
     ui->newFlightButton->setFocus();
+
+
+    menu->addAction(ui->actionEdit_Flight);
+    menu->addAction(ui->actionDelete_Flight);
+
     refreshView(Settings::read("logbook/view").toInt());
 }
 
@@ -38,16 +43,21 @@ LogbookWidget::~LogbookWidget()
     delete ui;
 }
 
-void LogbookWidget::setSelectedFlight(const qint32 &value)
-{
-    selectedFlight = value;
-}
-
-void LogbookWidget::tableView_selectionChanged(const QItemSelection &index,
+void LogbookWidget::tableView_selectionChanged(const QItemSelection &,
                                                const QItemSelection &)// TO DO
 {
-    setSelectedFlight(index.indexes()[0].data().toInt());
-    DEB("Selected flight with ID#: " << selectedFlight);
+    auto *selection = ui->tableView->selectionModel();
+    if(selection->hasSelection()){
+        selectedFlights.clear();
+        for(const auto& row : selection->selectedRows()){
+            selectedFlights << row.data().toInt();
+            DEB("Selected Flight(s) with ID: " << selectedFlights);
+        }
+    }else {
+        selectedFlights = {0};
+        DEB("No Flights selected.");
+    }
+
 }
 
 void LogbookWidget::on_newFlightButton_clicked()
@@ -57,25 +67,35 @@ void LogbookWidget::on_newFlightButton_clicked()
     refreshView(Settings::read("logbook/view").toInt());
 }
 
-void LogbookWidget::on_editFlightButton_clicked() // To Do: Fix! - use new flight, pre-filled with entry loaded from DB
+void LogbookWidget::on_editFlightButton_clicked()
 {
-    NewFlight ef(this,Flight(selectedFlight), Db::editExisting);
-    ef.exec();
-    refreshView(Settings::read("logbook/view").toInt());
+    if(selectedFlights.length() == 1){
+        NewFlight ef(this,Flight(selectedFlights.first()), Db::editExisting);
+        ef.exec();
+        refreshView(Settings::read("logbook/view").toInt());
+    } else {
+        QMessageBox nope(this);
+        nope.setText("More than one flight selected.\n\nEditing multiple entries is not yet supported.");
+        nope.exec();
+    }
 }
 
 void LogbookWidget::on_deleteFlightPushButton_clicked()
 {
-    if (selectedFlight > 0) {
+    if (selectedFlights.length() > 0 && selectedFlights.length() < 11) {
         QVector<QString> columns = {
             "doft", "dept", "dest"
         };
-        QVector<QString> details = Db::multiSelect(columns, "flights", "id",
-                                                   QString::number(selectedFlight), Db::exactMatch);
-        QString detailsstring = "The following flight will be deleted:\n\n";
-        for (const auto &item : details) {
-            detailsstring.append(item);
-            detailsstring.append(QLatin1Char(' '));
+        QVector<QString> details;
+        QString detailsstring = "The following flight(s) will be deleted:\n\n";
+        for(const auto& selectedFlight : selectedFlights){
+            details = Db::multiSelect(columns, "flights", "id",
+                                       QString::number(selectedFlight), Db::exactMatch);
+            for (const auto &item : details) {
+                detailsstring.append(item);
+                detailsstring.append(QLatin1Char(' '));
+            }
+            detailsstring.append("\n");
         }
         detailsstring.append("\n\nAre you sure?");
 
@@ -83,15 +103,35 @@ void LogbookWidget::on_deleteFlightPushButton_clicked()
         reply = QMessageBox::question(this, "Delete Flight", detailsstring,
                                       QMessageBox::Yes | QMessageBox::No);
         if (reply == QMessageBox::Yes) {
-            DEB("Deleting flight with ID# " << selectedFlight);
-            auto en = new Flight(selectedFlight);
-            en->remove();
+            for (const auto& selectedFlight : selectedFlights) {
+                DEB("Deleting flight with ID# " << selectedFlight);
+                auto entry = new Flight(selectedFlight);
+                entry->remove();
+            }
+            refreshView(Settings::read("logbook/view").toInt());
+        }
+    } else if (selectedFlights.length() == 0) {
+        QMessageBox nope(this);
+        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\nAre you sure you want to proceed?";
+        QMessageBox confirm;
+        confirm.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
+        confirm.setDefaultButton(QMessageBox::No);
+        confirm.setIcon(QMessageBox::Warning);
+        confirm.setWindowTitle("Delete Flight");
+        confirm.setText(warningMsg);
+        int reply = confirm.exec();
+        if(reply == QMessageBox::Yes) {
+            for (const auto& selectedFlight : selectedFlights) {
+                DEB("Deleting flight with ID# " << selectedFlight);
+                auto entry = new Flight(selectedFlight);
+                entry->remove();
+            }
             refreshView(Settings::read("logbook/view").toInt());
         }
-    } else {
-        QMessageBox NoFlight;
-        NoFlight.setText("No flight selected.");
-        NoFlight.exec();
     }
 }
 
@@ -141,7 +181,8 @@ void LogbookWidget::defaultView()
     QTableView *view = ui->tableView;
     view->setModel(model);
     view->setSelectionBehavior(QAbstractItemView::SelectRows);
-    view->setSelectionMode(QAbstractItemView::SingleSelection);
+    //view->setSelectionMode(QAbstractItemView::SingleSelection);
+    view->setSelectionMode(QAbstractItemView::ExtendedSelection);
     view->setEditTriggers(QAbstractItemView::NoEditTriggers);
     view->setColumnWidth(1, 120);
     view->setColumnWidth(2, 60);
@@ -159,6 +200,7 @@ void LogbookWidget::defaultView()
     view->hideColumn(0);
     view->show();
 
+    ui->tableView->setContextMenuPolicy(Qt::CustomContextMenu);
     connect(ui->tableView->selectionModel(),
             SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
             SLOT(tableView_selectionChanged(const QItemSelection &, const QItemSelection &)));
@@ -204,8 +246,29 @@ void LogbookWidget::easaView()
     view->setColumnWidth(21,120);
 
     view->show();
+    ui->tableView->setContextMenuPolicy(Qt::CustomContextMenu);
     connect(ui->tableView->selectionModel(),
             SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
             SLOT(tableView_selectionChanged(const QItemSelection &, const QItemSelection &)));
 }
 
+
+void LogbookWidget::on_tableView_customContextMenuRequested(const QPoint &pos)
+{
+    menu->popup(ui->tableView->viewport()->mapToGlobal(pos));
+}
+
+void LogbookWidget::on_actionDelete_Flight_triggered()
+{
+    emit ui->deleteFlightPushButton->clicked();
+}
+
+void LogbookWidget::on_actionEdit_Flight_triggered()
+{
+    emit ui->editFlightButton->clicked();
+}
+
+void LogbookWidget::on_tableView_doubleClicked(const QModelIndex &index)
+{
+    emit ui->editFlightButton->clicked();
+}

+ 13 - 2
src/gui/widgets/logbookwidget.h

@@ -24,6 +24,7 @@
 #include <QMessageBox>
 #include <chrono>
 #include <QDebug>
+#include <QMenu>
 
 #include "src/classes/settings.h"
 #include "src/database/db.h"
@@ -45,8 +46,7 @@ public:
     explicit LogbookWidget(QWidget *parent = nullptr);
     ~LogbookWidget();
 
-    qint32 selectedFlight = 0;
-    void setSelectedFlight(const qint32 &value);
+    QVector<qint32> selectedFlights = {0};
 
 private slots:
     void on_newFlightButton_clicked();
@@ -61,15 +61,26 @@ private slots:
 
     void tableView_selectionChanged(const QItemSelection &index, const QItemSelection &);
 
+    void on_tableView_customContextMenuRequested(const QPoint &pos);
+
+    void on_actionDelete_Flight_triggered();
+
+    void on_actionEdit_Flight_triggered();
+
+    void on_tableView_doubleClicked(const QModelIndex &index);
+
 private:
     Ui::LogbookWidget *ui;
 
+    QMenu* menu = new QMenu(this);
+
     void refreshView(int view_id);
 
     void defaultView();
 
     void easaView();
 
+
 };
 
 #endif // LOGBOOKWIDGET_H

+ 11 - 1
src/gui/widgets/logbookwidget.ui

@@ -67,7 +67,7 @@
        <second>0</second>
        <year>2019</year>
        <month>12</month>
-       <day>14</day>
+       <day>13</day>
       </datetime>
      </property>
      <property name="displayFormat">
@@ -121,6 +121,16 @@
     </widget>
    </item>
   </layout>
+  <action name="actionEdit_Flight">
+   <property name="text">
+    <string>Edit Flight</string>
+   </property>
+  </action>
+  <action name="actionDelete_Flight">
+   <property name="text">
+    <string>Delete Flight</string>
+   </property>
+  </action>
  </widget>
  <resources/>
  <connections/>