mainwindow.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. *openPilot Log - A FOSS Pilot Logbook Application
  3. *Copyright (C) 2020 Felix Turowsky
  4. *
  5. *This program is free software: you can redistribute it and/or modify
  6. *it under the terms of the GNU General Public License as published by
  7. *the Free Software Foundation, either version 3 of the License, or
  8. *(at your option) any later version.
  9. *
  10. *This program is distributed in the hope that it will be useful,
  11. *but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. *GNU General Public License for more details.
  14. *
  15. *You should have received a copy of the GNU General Public License
  16. *along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. #include "mainwindow.h"
  19. #include "ui_mainwindow.h"
  20. #include "newflight.h"
  21. #include "editflight.h"
  22. #include "newacft.h"
  23. #include "easaview.h"
  24. #include "dbman.cpp"
  25. #include "calc.h"
  26. #include <QTime>
  27. #include <QSqlDatabase>
  28. #include <QSqlDriver>
  29. #include <QSqlError>
  30. #include <QSqlQuery>
  31. #include <QSqlTableModel>
  32. #include <QTableView>
  33. #include <chrono>
  34. #include <QMessageBox>
  35. qlonglong SelectedFlight = -1;
  36. MainWindow::MainWindow(QWidget *parent)
  37. : QMainWindow(parent)
  38. , ui(new Ui::MainWindow)
  39. {
  40. ui->setupUi(this);
  41. db::connect();
  42. ui->FilterDateEdit->setDate(QDate::currentDate());
  43. auto start = std::chrono::high_resolution_clock::now();
  44. QSqlTableModel *model = new QSqlTableModel;
  45. model->setTable("Logbook");
  46. model->select();
  47. QTableView *view = ui->tableView;
  48. view->setModel(model);
  49. view->setSelectionBehavior(QAbstractItemView::SelectRows);
  50. view->setSelectionMode(QAbstractItemView::SingleSelection);
  51. view->setEditTriggers(QAbstractItemView::NoEditTriggers);
  52. view->setColumnWidth(1,120);
  53. view->setColumnWidth(2,60);
  54. view->setColumnWidth(3,60);
  55. view->setColumnWidth(4,60);
  56. view->setColumnWidth(5,60);
  57. view->setColumnWidth(6,60);
  58. view->setColumnWidth(7,120);
  59. view->setColumnWidth(8,180);
  60. view->setColumnWidth(9,120);
  61. view->setColumnWidth(10,90);
  62. view->horizontalHeader()->setStretchLastSection(QHeaderView::Stretch);
  63. //view->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
  64. view->verticalHeader()->hide();
  65. view->setAlternatingRowColors(true);
  66. view->hideColumn(0); // don't show the ID
  67. view->show();
  68. auto stop = std::chrono::high_resolution_clock::now();
  69. auto duration = std::chrono::duration_cast<std::chrono::microseconds>(stop - start);
  70. qDebug() << "Time taken for lookup and rendering: " << duration.count() << " microseconds";
  71. }
  72. MainWindow::~MainWindow()
  73. {
  74. delete ui;
  75. }
  76. /*
  77. * Functions
  78. */
  79. void MainWindow::nope()
  80. {
  81. QMessageBox nope; //error box
  82. nope.setText("This feature is not yet available!");
  83. nope.exec();
  84. }
  85. /*
  86. * Slots
  87. */
  88. void MainWindow::on_newflightButton_clicked()
  89. {
  90. NewFlight nf(this);
  91. nf.exec();
  92. }
  93. void MainWindow::on_actionNew_Flight_triggered()
  94. {
  95. NewFlight nf(this);
  96. nf.exec();
  97. }
  98. void MainWindow::on_actionQuit_triggered()
  99. {
  100. QApplication::quit();
  101. }
  102. void MainWindow::on_quitButton_clicked()
  103. {
  104. QApplication::quit();
  105. }
  106. void MainWindow::on_ShowAllButton_clicked()
  107. {
  108. QSqlTableModel *ShowAllModel = new QSqlTableModel;
  109. ShowAllModel->setTable("Logbook");
  110. ShowAllModel->select();
  111. ui->tableView->setModel(ShowAllModel);
  112. }
  113. void MainWindow::on_filterComboBox_activated(const QString &arg1)
  114. {
  115. if (arg1 == "Pilot Name")
  116. {
  117. nope();
  118. }else if (arg1 == "Aircraft Registration")
  119. {
  120. nope();
  121. }else if (arg1 == "Airline")
  122. {
  123. nope();
  124. }else if (arg1 == "Multi-Engine")
  125. {
  126. nope();
  127. }
  128. }
  129. void MainWindow::on_editflightButton_clicked()
  130. {
  131. QVector<QString> flight;
  132. flight = db::SelectFlightById(QString::number(SelectedFlight));
  133. if(flight.length() > 0)
  134. {
  135. qDebug() << "Valid Flight Selected";
  136. db::CommitToScratchpad(flight); // commits flight to scratchpad
  137. EditFlight ef(this);
  138. ef.exec();
  139. }
  140. }
  141. void MainWindow::on_deleteFlightPushButton_clicked()
  142. {
  143. if(SelectedFlight > 0)
  144. {
  145. qDebug() << "Valid Flight Selected";
  146. db::DeleteFlightById(QString::number(SelectedFlight));
  147. QSqlTableModel *ShowAllModel = new QSqlTableModel; //refresh view
  148. ShowAllModel->setTable("Logbook");
  149. ShowAllModel->select();
  150. ui->tableView->setModel(ShowAllModel);
  151. }else
  152. {
  153. QMessageBox NoFlight;
  154. NoFlight.setText("No flight selected.");
  155. NoFlight.exec();
  156. }
  157. }
  158. void MainWindow::on_FilterDateEdit_editingFinished()
  159. {
  160. }
  161. void MainWindow::on_filterFlightsByDateButton_clicked()
  162. {
  163. QDate selection(ui->FilterDateEdit->date());
  164. QString selecteddate = selection.toString("yyyy-MM-dd");
  165. QString datefilter = "Date = '" + selecteddate +"'";
  166. QSqlTableModel *DateFilteredModel = new QSqlTableModel;
  167. DateFilteredModel ->setTable("Logbook");
  168. DateFilteredModel ->setFilter(datefilter);
  169. DateFilteredModel->select();
  170. ui->tableView->setModel(DateFilteredModel);
  171. }
  172. void MainWindow::on_EasaViewButton_clicked()
  173. {
  174. EasaView ev(this);
  175. ev.exec();
  176. }
  177. void MainWindow::on_tableView_pressed(const QModelIndex &index)
  178. {
  179. auto NewIndex = ui->tableView->model()->index(index.row(), 0);
  180. SelectedFlight = ui->tableView->model()->data(NewIndex).toInt();
  181. qDebug() << "Selected Flight with ID#:(presssed) " << SelectedFlight;
  182. }
  183. void MainWindow::on_tableView_entered(const QModelIndex &index)
  184. {
  185. auto NewIndex = ui->tableView->model()->index(index.row(), 0);
  186. SelectedFlight = ui->tableView->model()->data(NewIndex).toInt();
  187. qDebug() << "Selected Flight with ID#(entered): " << SelectedFlight;
  188. }