2
0

logbookwidget.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. // Debug Makro
  21. #define DEB(expr) \
  22. qDebug() << __PRETTY_FUNCTION__ << "\t" << expr
  23. LogbookWidget::LogbookWidget(QWidget *parent) :
  24. QWidget(parent),
  25. ui(new Ui::LogbookWidget)
  26. {
  27. ui->setupUi(this);
  28. ui->filterDateEdit->setDate(QDate::currentDate());
  29. ui->filterDateEdit_2->setDate(QDate::currentDate());
  30. ui->newFlightButton->setFocus();
  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. DEB("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. LogbookWidget::~LogbookWidget()
  63. {
  64. delete ui;
  65. }
  66. void LogbookWidget::setSelectedFlight(const qint32 &value)
  67. {
  68. selectedFlight = value;
  69. }
  70. void LogbookWidget::tableView_selectionChanged(const QItemSelection &index,
  71. const QItemSelection &)// TO DO
  72. {
  73. setSelectedFlight(index.indexes()[0].data().toInt());
  74. DEB("Selected flight with ID#: " << selectedFlight);
  75. }
  76. void LogbookWidget::on_newFlightButton_clicked()
  77. {
  78. //NewFlight nf(this);
  79. //nf.exec();
  80. QMessageBox *nope = new QMessageBox(this);
  81. nope->setText("This feature is temporarily INOP.");
  82. nope->exec();
  83. }
  84. void LogbookWidget::on_editFlightButton_clicked() // To Do: Fix! - use new flight, pre-filled with entry loaded from DB
  85. {
  86. QMessageBox *nope = new QMessageBox(this); // edit widget currently INOP
  87. nope->setText("This feature is temporarily INOP.");
  88. nope->exec();
  89. //EditFlight ef(this);
  90. //ef.exec();
  91. }
  92. void LogbookWidget::on_deleteFlightPushButton_clicked()
  93. {
  94. if (selectedFlight > 0) {
  95. QVector<QString> columns = {
  96. "doft", "dept", "dest"
  97. };
  98. QVector<QString> details = Db::multiSelect(columns, "flights", "id",
  99. QString::number(selectedFlight), Db::exactMatch);
  100. QString detailsstring = "The following flight will be deleted:\n\n";
  101. for (const auto &item : details) {
  102. detailsstring.append(item);
  103. detailsstring.append(QLatin1Char(' '));
  104. }
  105. detailsstring.append("\n\nAre you sure?");
  106. QMessageBox::StandardButton reply;
  107. reply = QMessageBox::question(this, "Delete Flight", detailsstring,
  108. QMessageBox::Yes | QMessageBox::No);
  109. if (reply == QMessageBox::Yes) {
  110. DEB("Deleting flight with ID# " << selectedFlight);
  111. auto en = new Flight("flights", selectedFlight);
  112. en->remove();
  113. QSqlTableModel *ShowAllModel = new QSqlTableModel; //refresh view
  114. ShowAllModel->setTable("Logbook");
  115. ShowAllModel->select();
  116. ui->tableView->setModel(ShowAllModel);
  117. connect(ui->tableView->selectionModel(),
  118. SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
  119. SLOT(tableView_selectionChanged(const QItemSelection &, const QItemSelection &)));
  120. }
  121. } else {
  122. QMessageBox NoFlight;
  123. NoFlight.setText("No flight selected.");
  124. NoFlight.exec();
  125. }
  126. }
  127. void LogbookWidget::on_filterFlightsByDateButton_clicked()
  128. {
  129. QDate date(ui->filterDateEdit->date());
  130. QString startdate = date.toString("yyyy-MM-dd");
  131. date = ui->filterDateEdit_2->date();
  132. QString enddate = date.toString("yyyy-MM-dd");
  133. QString datefilter = "Date BETWEEN '" + startdate + "' AND '" + enddate + QLatin1Char('\'');
  134. QSqlTableModel *DateFilteredModel = new QSqlTableModel;
  135. DateFilteredModel ->setTable("Logbook");
  136. DateFilteredModel ->setFilter(datefilter);
  137. DateFilteredModel->select();
  138. ui->tableView->setModel(DateFilteredModel);
  139. }
  140. void LogbookWidget::on_showAllButton_clicked()
  141. {
  142. QSqlTableModel *ShowAllModel = new QSqlTableModel;
  143. ShowAllModel->setTable("Logbook");
  144. ShowAllModel->select();
  145. ui->tableView->setModel(ShowAllModel);
  146. }