logbookwidget.cpp 5.2 KB

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