logbookwidget.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. // Initalise the display Model and view
  45. displayModel = new QSqlTableModel(this);
  46. view = ui->tableView;
  47. setupModelAndView(ASettings::read(ASettings::Main::LogbookView).toInt());
  48. connectSignalsAndSlots();
  49. }
  50. LogbookWidget::~LogbookWidget()
  51. {
  52. delete ui;
  53. }
  54. /*
  55. * Functions
  56. */
  57. /*!
  58. * \brief LogbookWidget::setupModelAndView configures the QTableView and populates the model with data
  59. * according to the current view.
  60. * \param view_id - retreived from ASettings::Main::LogbookView
  61. */
  62. void LogbookWidget::setupModelAndView(int view_id)
  63. {
  64. switch (view_id) {
  65. case 0:
  66. LOG << "Loading Default View...";
  67. displayModel->setTable(QStringLiteral("viewDefault"));
  68. displayModel->select();
  69. break;
  70. case 1:
  71. LOG << "Loading EASA View...";
  72. displayModel->setTable(QStringLiteral("viewEASA"));
  73. displayModel->select();
  74. break;
  75. default:
  76. LOG << "Loading Default View...";
  77. displayModel->setTable(QStringLiteral("viewDefault"));
  78. displayModel->select();
  79. }
  80. view->setModel(displayModel);
  81. view->setSelectionBehavior(QAbstractItemView::SelectRows);
  82. view->setSelectionMode(QAbstractItemView::ExtendedSelection);
  83. view->setEditTriggers(QAbstractItemView::NoEditTriggers);
  84. view->setContextMenuPolicy(Qt::CustomContextMenu);
  85. view->horizontalHeader()->setStretchLastSection(QHeaderView::Stretch);
  86. view->verticalHeader()->hide();
  87. view->setAlternatingRowColors(true);
  88. view->hideColumn(0);
  89. view->resizeColumnsToContents();
  90. view->show();
  91. }
  92. void LogbookWidget::connectSignalsAndSlots()
  93. {
  94. selectionModel = view->selectionModel();
  95. QObject::connect(view->selectionModel(), &QItemSelectionModel::selectionChanged,
  96. this, &LogbookWidget::flightsTableView_selectionChanged);
  97. }
  98. void LogbookWidget::changeEvent(QEvent *event)
  99. {
  100. if (event != nullptr)
  101. if(event->type() == QEvent::LanguageChange)
  102. ui->retranslateUi(this);
  103. }
  104. /*
  105. * Slots
  106. */
  107. /*!
  108. * \brief LogbookWidget::flightsTableView_selectionChanged saves the selected row(s)
  109. * to the selectedFlights member variable.
  110. */
  111. void LogbookWidget::flightsTableView_selectionChanged()
  112. {
  113. selectedFlights.clear();
  114. for (const auto& row : selectionModel->selectedRows()) {
  115. selectedFlights.append(row.data().toInt());
  116. DEB << "Selected Flight(s) with ID: " << selectedFlights;
  117. }
  118. }
  119. /*!
  120. * \brief LogbookWidget::on_newFlightButton_clicked opens a NewFlightDialog
  121. */
  122. void LogbookWidget::on_newFlightButton_clicked()
  123. {
  124. auto nf = new NewFlightDialog(this);
  125. nf->setAttribute(Qt::WA_DeleteOnClose);
  126. nf->exec();
  127. displayModel->select();
  128. }
  129. /*!
  130. * \brief LogbookWidget::on_editFlightButton_clicked opens a NewFlightDialog and
  131. * pre-fills the data from the selected flight.
  132. */
  133. void LogbookWidget::on_editFlightButton_clicked()
  134. {
  135. if(selectedFlights.length() == 1){
  136. auto ef = new NewFlightDialog(selectedFlights.first(), this);
  137. ef->setAttribute(Qt::WA_DeleteOnClose);
  138. ef->exec();
  139. displayModel->select();
  140. } else if (selectedFlights.isEmpty()) {
  141. WARN(tr("<br>No flight selected.<br>"));
  142. } else {
  143. WARN(tr("<br>More than one flight selected."
  144. "<br><br>Editing multiple entries is not yet supported."));
  145. }
  146. }
  147. /*!
  148. * \brief LogbookWidget::on_deleteFlightPushButton_clicked If a row is selected, query information
  149. * about the affected row(s) and ask the user to confirm deletion.
  150. */
  151. void LogbookWidget::on_deleteFlightPushButton_clicked()
  152. {
  153. DEB << "Flights selected: " << selectedFlights.length();
  154. if (selectedFlights.length() == 0) {
  155. WARN(tr("<br>No flight selected.<br>"));
  156. return;
  157. } else if (selectedFlights.length() > 0 && selectedFlights.length() <= 10) {
  158. QVector<AFlightEntry> flights_list;
  159. for (const auto &flight_id : qAsConst(selectedFlights)) {
  160. flights_list.append(aDB->getFlightEntry(flight_id));
  161. }
  162. QString flights_list_string;
  163. for (auto &flight : flights_list) {
  164. flights_list_string.append(flight.summary());
  165. flights_list_string.append(QStringLiteral("&nbsp;&nbsp;&nbsp;&nbsp;<br>"));
  166. }
  167. QMessageBox confirm(this);
  168. confirm.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
  169. confirm.setDefaultButton(QMessageBox::No);
  170. confirm.setIcon(QMessageBox::Question);
  171. confirm.setWindowTitle("Delete Flight");
  172. confirm.setText(tr("The following flight(s) will be deleted:<br><br><b><tt>"
  173. "%1<br></b></tt>"
  174. "Deleting flights is irreversible.<br>Do you want to proceed?"
  175. ).arg(flights_list_string));
  176. if (confirm.exec() == QMessageBox::Yes) {
  177. for (auto& flight : flights_list) {
  178. DEB << "Deleting flight: " << flight.summary();
  179. if(!aDB->remove(flight)) {
  180. WARN(tr("<br>Unable to delete.<br><br>The following error has ocurred: %1"
  181. ).arg(aDB->lastError.text()));
  182. return;
  183. }
  184. }
  185. INFO(tr("%1 flights have been deleted successfully."
  186. ).arg(QString::number(selectedFlights.length())));
  187. displayModel->select();
  188. }
  189. } else if (selectedFlights.length() > 10) {
  190. QMessageBox confirm;
  191. confirm.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
  192. confirm.setDefaultButton(QMessageBox::No);
  193. confirm.setIcon(QMessageBox::Warning);
  194. confirm.setWindowTitle("Delete Flight");
  195. confirm.setText(tr("You have selected %1 flights.<br><br>"
  196. "Deleting flights is irreversible.<br><br>"
  197. "Are you sure you want to proceed?"
  198. ).arg(QString::number(selectedFlights.length())));
  199. if(confirm.exec() == QMessageBox::Yes) {
  200. QList<DataPosition> selected_flights;
  201. for (const auto& flight_id : selectedFlights) {
  202. selected_flights.append({QStringLiteral("flights"), flight_id});
  203. }
  204. if (!aDB->removeMany(selected_flights)) {
  205. WARN(tr("Unable to delete. The following error has ocurred:<br><br>%1").arg(aDB->lastError.text()));
  206. return;
  207. }
  208. INFO(tr("%1 flights have been deleted successfully."
  209. ).arg(QString::number(selectedFlights.length())));
  210. displayModel->select();
  211. }
  212. displayModel->select();
  213. }
  214. }
  215. void LogbookWidget::on_tableView_customContextMenuRequested(const QPoint &pos)
  216. {
  217. menu->popup(ui->tableView->viewport()->mapToGlobal(pos));
  218. }
  219. void LogbookWidget::on_actionDelete_Flight_triggered()
  220. {
  221. emit ui->deleteFlightPushButton->clicked();
  222. }
  223. void LogbookWidget::on_actionEdit_Flight_triggered()
  224. {
  225. emit ui->editFlightButton->clicked();
  226. }
  227. void LogbookWidget::on_tableView_doubleClicked()
  228. {
  229. emit ui->editFlightButton->clicked();
  230. }
  231. void LogbookWidget::on_flightSearchComboBox_currentIndexChanged(int)
  232. {
  233. emit ui->showAllButton->clicked();
  234. }
  235. /*!
  236. * \brief LogbookWidget::refresh Refreshes the view to reflect changes in the database.
  237. */
  238. void LogbookWidget::refresh()
  239. {
  240. displayModel->select();
  241. view->resizeColumnsToContents();
  242. }
  243. void LogbookWidget::onLogbookWidget_viewSelectionChanged(SettingsWidget::SettingSignal signal)
  244. {
  245. if (signal == SettingsWidget::SettingSignal::LogbookWidget)
  246. setupModelAndView(ASettings::read(ASettings::Main::LogbookView).toInt());
  247. }
  248. void LogbookWidget::on_showAllButton_clicked()
  249. {
  250. ui->flightSearchLlineEdit->setText(QString());
  251. displayModel->setFilter(QString());
  252. displayModel->select();
  253. }
  254. /*!
  255. * \brief LogbookWidget::on_flightSearchLlineEdit_textChanged applies a filter to the
  256. * display model allowing the user to search for flights by specified elements (date, aircraft,
  257. * Pilot Name)
  258. */
  259. void LogbookWidget::on_flightSearchLlineEdit_textChanged(const QString &arg1)
  260. {
  261. if(arg1.length() == 0) {
  262. displayModel->setFilter("");
  263. displayModel->select();
  264. return;
  265. }
  266. if (ui->flightSearchComboBox->currentIndex() < 3) {
  267. displayModel->setFilter(FILTER_MAP.value(ui->flightSearchComboBox->currentIndex())
  268. + arg1 + QLatin1String("%\""));
  269. return;
  270. } else if (ui->flightSearchComboBox->currentIndex() == 3) { // registration
  271. displayModel->setFilter(FILTER_MAP.value(ui->flightSearchComboBox->currentIndex())
  272. + arg1 + QLatin1String("%\""));
  273. return;
  274. } else if (ui->flightSearchComboBox->currentIndex() == 4) { // Name Pic
  275. displayModel->setFilter(FILTER_MAP.value(ui->flightSearchComboBox->currentIndex())
  276. + arg1 + QLatin1String("%\""));
  277. return;
  278. }
  279. }
  280. /*!
  281. * \brief LogbookWidget::repopulateModel (public slot) - cleanly re-populates the model to cater for a change
  282. * to the database connection (for example, when a backup is created or restored)
  283. */
  284. void LogbookWidget::repopulateModel()
  285. {
  286. // unset the current model and delete it to avoid leak
  287. view->setModel(nullptr);
  288. delete displayModel;
  289. // create a new model and populate it
  290. displayModel = new QSqlTableModel(this);
  291. setupModelAndView(ASettings::read(ASettings::Main::LogbookView).toInt());
  292. connectSignalsAndSlots();
  293. }