logbookwidget.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 "logbookwidget.h"
  19. #include "ui_logbookwidget.h"
  20. #include <QSqlTableModel>
  21. #include <QMessageBox>
  22. #include "dbman.cpp"
  23. #include "dbflight.h"
  24. #include "newflight.h"
  25. #include "editflight.h"
  26. #include <chrono>
  27. #include <QDebug>
  28. //To Do: Update Selection in Tableview on arrow key press.
  29. qint32 SelectedFlight = -1; // Variable to store selected row in QTableView
  30. logbookWidget::logbookWidget(QWidget *parent) :
  31. QWidget(parent),
  32. ui(new Ui::logbookWidget)
  33. {
  34. ui->setupUi(this);
  35. ui->filterDateEdit->setDate(QDate::currentDate());
  36. auto start = std::chrono::high_resolution_clock::now(); // timer for performance testing
  37. QSqlTableModel *model = new QSqlTableModel;
  38. model->setTable("Logbook");
  39. model->select();
  40. QTableView *view = ui->tableView;
  41. view->setModel(model);
  42. view->setSelectionBehavior(QAbstractItemView::SelectRows);
  43. view->setSelectionMode(QAbstractItemView::SingleSelection);
  44. view->setEditTriggers(QAbstractItemView::NoEditTriggers);
  45. view->setColumnWidth(1,120);
  46. view->setColumnWidth(2,60);
  47. view->setColumnWidth(3,60);
  48. view->setColumnWidth(4,60);
  49. view->setColumnWidth(5,60);
  50. view->setColumnWidth(6,60);
  51. view->setColumnWidth(7,120);
  52. view->setColumnWidth(8,180);
  53. view->setColumnWidth(9,120);
  54. view->setColumnWidth(10,90);
  55. view->horizontalHeader()->setStretchLastSection(QHeaderView::Stretch);
  56. view->verticalHeader()->hide();
  57. view->setAlternatingRowColors(true);
  58. view->hideColumn(0);
  59. view->show();
  60. auto stop = std::chrono::high_resolution_clock::now();
  61. auto duration = std::chrono::duration_cast<std::chrono::microseconds>(stop - start);
  62. qDebug() << "logbookWidget: Time taken for lookup and rendering: " << duration.count() << " microseconds";
  63. connect(ui->tableView->selectionModel(),
  64. SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
  65. SLOT(tableView_selectionChanged(const QItemSelection &, const QItemSelection &)));
  66. }
  67. void logbookWidget::tableView_selectionChanged(const QItemSelection &index, const QItemSelection &)// TO DO
  68. {
  69. SelectedFlight = index.indexes()[0].data().toInt();
  70. qDebug() << "Selected Flight with ID#(selectionChanged): " << SelectedFlight;
  71. }
  72. logbookWidget::~logbookWidget()
  73. {
  74. delete ui;
  75. }
  76. void logbookWidget::on_newFlightButton_clicked()
  77. {
  78. NewFlight nf(this);
  79. nf.exec();
  80. }
  81. void logbookWidget::on_editFlightButton_clicked() // To Do: Fix! - use new flight, pre-filled with entry loaded from DB
  82. {
  83. QMessageBox *nope = new QMessageBox(this); // edit widget currently INOP
  84. nope->setText("This feature is temporarily INOP.");
  85. nope->exec();
  86. //EditFlight ef(this);
  87. //ef.exec();
  88. }
  89. void logbookWidget::on_deleteFlightPushButton_clicked()
  90. {
  91. if(SelectedFlight > 0)
  92. {
  93. qDebug() << "Valid Flight Selected";
  94. // To Do: Confirmation Dialog
  95. QMessageBox::StandardButton reply;
  96. reply = QMessageBox::question(this, "Delete Flight", "The following flight will be deleted:\n{retreive Flight Details}\nAre you sure?",
  97. QMessageBox::Yes|QMessageBox::No);
  98. if (reply == QMessageBox::Yes)
  99. {
  100. qDebug() << "Deleting Flight with ID# " << SelectedFlight;
  101. dbFlight::deleteFlightById(QString::number(SelectedFlight));
  102. QSqlTableModel *ShowAllModel = new QSqlTableModel; //refresh view
  103. ShowAllModel->setTable("Logbook");
  104. ShowAllModel->select();
  105. ui->tableView->setModel(ShowAllModel);
  106. }
  107. }else
  108. {
  109. QMessageBox NoFlight;
  110. NoFlight.setText("No flight selected.");
  111. NoFlight.exec();
  112. }
  113. }
  114. void logbookWidget::on_filterFlightsByDateButton_clicked()
  115. {
  116. QDate selection(ui->filterDateEdit->date());
  117. QString selecteddate = selection.toString("yyyy-MM-dd");
  118. QString datefilter = "Date = '" + selecteddate +"'";
  119. QSqlTableModel *DateFilteredModel = new QSqlTableModel;
  120. DateFilteredModel ->setTable("Logbook");
  121. DateFilteredModel ->setFilter(datefilter);
  122. DateFilteredModel->select();
  123. ui->tableView->setModel(DateFilteredModel);
  124. }
  125. void logbookWidget::on_showAllButton_clicked()
  126. {
  127. QSqlTableModel *ShowAllModel = new QSqlTableModel;
  128. ShowAllModel->setTable("Logbook");
  129. ShowAllModel->select();
  130. ui->tableView->setModel(ShowAllModel);
  131. }
  132. /*void logbookWidget::on_tableView_pressed(const QModelIndex &index)
  133. {
  134. auto NewIndex = ui->tableView->model()->index(index.row(), 0);
  135. SelectedFlight = ui->tableView->model()->data(NewIndex).toInt();
  136. qDebug() << "Selected Flight with ID#(pressed): " << SelectedFlight;
  137. }*/