logbookwidget.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. *openPilotLog - A FOSS Pilot Logbook Application
  3. *Copyright (C) 2020-2021 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 "src/classes/aflightentry.h"
  21. #include "src/database/adatabase.h"
  22. #include "src/classes/asettings.h"
  23. #include "src/gui/dialogues/newflightdialog.h"
  24. #include "src/functions/alog.h"
  25. #include "src/functions/alog.h"
  26. const QMap<int, QString> FILTER_MAP = {
  27. {0, QStringLiteral("Date LIKE \"%")},
  28. {1, QStringLiteral("Dept LIKE \"%")},
  29. {2, QStringLiteral("Dest LIKE \"%")},
  30. {3, QStringLiteral("Registration LIKE \"%")},
  31. {4, QStringLiteral("\"Name PIC\" LIKE \"%")}
  32. };
  33. const auto NON_WORD_CHAR = QRegularExpression("\\W");
  34. LogbookWidget::LogbookWidget(QWidget *parent) :
  35. QWidget(parent),
  36. ui(new Ui::LogbookWidget)
  37. {
  38. ui->setupUi(this);
  39. ui->newFlightButton->setFocus();
  40. //customContextMenu for tablewidget
  41. menu = new QMenu(this);
  42. menu->addAction(ui->actionEdit_Flight);
  43. menu->addAction(ui->actionDelete_Flight);
  44. //Initialise message Box
  45. messageBox = new QMessageBox(this);
  46. // Initalise the display Model and view
  47. displayModel = new QSqlTableModel(this);
  48. view = ui->tableView;
  49. setupModelAndView(ASettings::read(ASettings::Main::LogbookView).toInt());
  50. connectSignalsAndSlots();
  51. }
  52. LogbookWidget::~LogbookWidget()
  53. {
  54. delete ui;
  55. }
  56. /*
  57. * Functions
  58. */
  59. void LogbookWidget::setupModelAndView(int view_id)
  60. {
  61. switch (view_id) {
  62. case 0:
  63. LOG << "Loading Default View...\n";
  64. displayModel->setTable(QStringLiteral("viewDefault"));
  65. displayModel->select();
  66. break;
  67. case 1:
  68. LOG << "Loading EASA View...\n";
  69. displayModel->setTable(QStringLiteral("viewEASA"));
  70. displayModel->select();
  71. break;
  72. default:
  73. LOG << "Loading Default View...\n";
  74. displayModel->setTable(QStringLiteral("viewDefault"));
  75. displayModel->select();
  76. }
  77. view->setModel(displayModel);
  78. view->setSelectionBehavior(QAbstractItemView::SelectRows);
  79. view->setSelectionMode(QAbstractItemView::ExtendedSelection);
  80. view->setEditTriggers(QAbstractItemView::NoEditTriggers);
  81. view->setContextMenuPolicy(Qt::CustomContextMenu);
  82. view->horizontalHeader()->setStretchLastSection(QHeaderView::Stretch);
  83. view->verticalHeader()->hide();
  84. view->setAlternatingRowColors(true);
  85. view->hideColumn(0);
  86. view->resizeColumnsToContents();
  87. view->show();
  88. }
  89. void LogbookWidget::connectSignalsAndSlots()
  90. {
  91. selectionModel = view->selectionModel();
  92. QObject::connect(view->selectionModel(), &QItemSelectionModel::selectionChanged,
  93. this, &LogbookWidget::flightsTableView_selectionChanged);
  94. }
  95. /*
  96. * Slots
  97. */
  98. void LogbookWidget::flightsTableView_selectionChanged()
  99. {
  100. selectedFlights.clear();
  101. for (const auto& row : selectionModel->selectedRows()) {
  102. selectedFlights.append(row.data().toInt());
  103. DEB << "Selected Flight(s) with ID: " << selectedFlights;
  104. }
  105. }
  106. void LogbookWidget::on_newFlightButton_clicked()
  107. {
  108. auto nf = new NewFlightDialog(this);
  109. nf->setAttribute(Qt::WA_DeleteOnClose);
  110. nf->exec();
  111. displayModel->select();
  112. }
  113. void LogbookWidget::on_editFlightButton_clicked()
  114. {
  115. if(selectedFlights.length() == 1){
  116. auto ef = new NewFlightDialog(selectedFlights.first(), this);
  117. ef->setAttribute(Qt::WA_DeleteOnClose);
  118. ef->exec();
  119. displayModel->select();
  120. } else if (selectedFlights.isEmpty()) {
  121. messageBox->setText(tr("<br>No flight selected.<br>"));
  122. messageBox->exec();
  123. } else {
  124. messageBox->setText(tr("<br>More than one flight selected."
  125. "<br><br>Editing multiple entries is not yet supported."));
  126. messageBox->exec();
  127. }
  128. }
  129. void LogbookWidget::on_deleteFlightPushButton_clicked()
  130. {
  131. DEB << "Flights selected: " << selectedFlights.length();
  132. if (selectedFlights.length() == 0) {
  133. messageBox->setIcon(QMessageBox::Information);
  134. messageBox->setText(tr("<br>No flight selected.<br>"));
  135. messageBox->exec();
  136. return;
  137. } else if (selectedFlights.length() > 0 && selectedFlights.length() <= 10) {
  138. QList<AFlightEntry> flights_list;
  139. for (const auto &flight_id : selectedFlights) {
  140. flights_list.append(aDB->getFlightEntry(flight_id));
  141. }
  142. QString flights_list_string;
  143. for (auto &flight : flights_list) {
  144. flights_list_string.append(flight.summary());
  145. flights_list_string.append(QStringLiteral("&nbsp;&nbsp;&nbsp;&nbsp;<br>"));
  146. }
  147. QMessageBox confirm;
  148. confirm.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
  149. confirm.setDefaultButton(QMessageBox::No);
  150. confirm.setIcon(QMessageBox::Question);
  151. confirm.setWindowTitle("Delete Flight");
  152. confirm.setText(tr("The following flight(s) will be deleted:<br><br><b><tt>"
  153. "%1<br></b></tt>"
  154. "Deleting flights is irreversible.<br>Do you want to proceed?"
  155. ).arg(flights_list_string));
  156. if (confirm.exec() == QMessageBox::Yes) {
  157. for (auto& flight : flights_list) {
  158. DEB << "Deleting flight: " << flight.summary();
  159. if(!aDB->remove(flight)) {
  160. confirm.setText(tr("<br>Unable to delete.<br><br>The following error has ocurred: %1"
  161. ).arg(aDB->lastError.text()));
  162. messageBox->exec();
  163. return;
  164. }
  165. }
  166. messageBox->setText(tr("%1 flights have been deleted successfully."
  167. ).arg(QString::number(selectedFlights.length())));
  168. messageBox->exec();
  169. displayModel->select();
  170. }
  171. } else if (selectedFlights.length() > 10) {
  172. QMessageBox confirm;
  173. confirm.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
  174. confirm.setDefaultButton(QMessageBox::No);
  175. confirm.setIcon(QMessageBox::Warning);
  176. confirm.setWindowTitle("Delete Flight");
  177. confirm.setText(tr("You have selected %1 flights.<br><br>"
  178. "Deleting flights is irreversible.<br><br>"
  179. "Are you sure you want to proceed?"
  180. ).arg(QString::number(selectedFlights.length())));
  181. if(confirm.exec() == QMessageBox::Yes) {
  182. QList<DataPosition> selected_flights;
  183. for (const auto& flight_id : selectedFlights) {
  184. selected_flights.append({QStringLiteral("flights"), flight_id});
  185. }
  186. if (!aDB->removeMany(selected_flights)) {
  187. messageBox->setText(aDB->lastError.text()); // [F]: To Do: error info
  188. messageBox->exec();
  189. return;
  190. }
  191. messageBox->setText(tr("%1 flights have been deleted successfully."
  192. ).arg(QString::number(selectedFlights.length())));
  193. messageBox->exec();
  194. displayModel->select();
  195. }
  196. displayModel->select();
  197. }
  198. }
  199. void LogbookWidget::on_tableView_customContextMenuRequested(const QPoint &pos)
  200. {
  201. menu->popup(ui->tableView->viewport()->mapToGlobal(pos));
  202. }
  203. void LogbookWidget::on_actionDelete_Flight_triggered()
  204. {
  205. emit ui->deleteFlightPushButton->clicked();
  206. }
  207. void LogbookWidget::on_actionEdit_Flight_triggered()
  208. {
  209. emit ui->editFlightButton->clicked();
  210. }
  211. void LogbookWidget::on_tableView_doubleClicked()
  212. {
  213. emit ui->editFlightButton->clicked();
  214. }
  215. void LogbookWidget::on_flightSearchComboBox_currentIndexChanged(int)
  216. {
  217. emit ui->showAllButton->clicked();
  218. }
  219. void LogbookWidget::refresh()
  220. {
  221. //refresh view to reflect changes the user has made via a dialog.
  222. displayModel->select();
  223. view->resizeColumnsToContents();
  224. }
  225. void LogbookWidget::onLogbookWidget_viewSelectionChanged(SettingsWidget::SettingSignal signal)
  226. {
  227. if (signal == SettingsWidget::SettingSignal::LogbookWidget)
  228. setupModelAndView(ASettings::read(ASettings::Main::LogbookView).toInt());
  229. }
  230. void LogbookWidget::on_showAllButton_clicked()
  231. {
  232. ui->flightSearchLlineEdit->setText(QString());
  233. displayModel->setFilter(QString());
  234. displayModel->select();
  235. }
  236. void LogbookWidget::on_flightSearchLlineEdit_textChanged(const QString &arg1)
  237. {
  238. if(arg1.length() == 0) {
  239. displayModel->setFilter("");
  240. displayModel->select();
  241. return;
  242. }
  243. if (ui->flightSearchComboBox->currentIndex() < 3) {
  244. displayModel->setFilter(FILTER_MAP.value(ui->flightSearchComboBox->currentIndex())
  245. + arg1 + QStringLiteral("%\""));
  246. return;
  247. } else if (ui->flightSearchComboBox->currentIndex() == 3) { // registration
  248. displayModel->setFilter(FILTER_MAP.value(ui->flightSearchComboBox->currentIndex())
  249. + arg1 + QStringLiteral("%\""));
  250. return;
  251. } else if (ui->flightSearchComboBox->currentIndex() == 4) { // Name Pic
  252. displayModel->setFilter(FILTER_MAP.value(ui->flightSearchComboBox->currentIndex())
  253. + arg1 + QStringLiteral("%\""));
  254. return;
  255. }
  256. }
  257. void LogbookWidget::repopulateModel()
  258. {
  259. // unset the current model and delete it to avoid leak
  260. view->setModel(nullptr);
  261. delete displayModel;
  262. // create a new model and populate it
  263. displayModel = new QSqlTableModel(this);
  264. setupModelAndView(ASettings::read(ASettings::Main::LogbookView).toInt());
  265. connectSignalsAndSlots();
  266. }