2
0

logbookwidget.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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 "src/testing/adebug.h"
  21. const QMap<int, QString> FILTER_MAP = {
  22. {0, "Date LIKE \"%"},
  23. {1, "Dept LIKE \"%"},
  24. {2, "Dest LIKE \"%"},
  25. {3, "Registration LIKE \"%"},
  26. {4, "\"Name PIC\" LIKE \"%"}
  27. };
  28. const auto NON_WORD_CHAR = QRegularExpression("\\W");
  29. LogbookWidget::LogbookWidget(QWidget *parent) :
  30. QWidget(parent),
  31. ui(new Ui::LogbookWidget)
  32. {
  33. ui->setupUi(this);
  34. ui->newFlightButton->setFocus();
  35. //customContextMenu for tablewidget
  36. menu = new QMenu(this);
  37. menu->addAction(ui->actionEdit_Flight);
  38. menu->addAction(ui->actionDelete_Flight);
  39. //Initialise message Box
  40. messageBox = new QMessageBox(this);
  41. prepareModelAndView(ASettings::read("logbook/view").toInt());
  42. connectSignalsAndSlots();
  43. }
  44. LogbookWidget::~LogbookWidget()
  45. {
  46. delete ui;
  47. }
  48. /*
  49. * Functions
  50. */
  51. void LogbookWidget::prepareModelAndView(int view_id)
  52. {
  53. switch (view_id) {
  54. case 0:
  55. setupDefaultView();
  56. break;
  57. case 1:
  58. setupEasaView();
  59. break;
  60. default:
  61. setupDefaultView();
  62. }
  63. }
  64. void LogbookWidget::connectSignalsAndSlots()
  65. {
  66. selection = view->selectionModel();
  67. QObject::connect(view->selectionModel(), &QItemSelectionModel::selectionChanged,
  68. this, &LogbookWidget::flightsTableView_selectionChanged);
  69. using namespace experimental;
  70. QObject::connect(aDB(), &ADataBase::sqlSuccessful,
  71. this, &LogbookWidget::onDeletedSuccessfully);
  72. QObject::connect(aDB(), &ADataBase::sqlError,
  73. this, &LogbookWidget::onDeleteUnsuccessful);
  74. }
  75. void LogbookWidget::setupDefaultView()
  76. {
  77. DEB("Loading Default View...");
  78. displayModel = new QSqlTableModel;
  79. displayModel->setTable("viewDefault");
  80. displayModel->select();
  81. view = ui->tableView;
  82. view->setModel(displayModel);
  83. view->setColumnWidth(1, 120);
  84. view->setColumnWidth(2, 60);
  85. view->setColumnWidth(3, 60);
  86. view->setColumnWidth(4, 60);
  87. view->setColumnWidth(5, 60);
  88. view->setColumnWidth(6, 60);
  89. view->setColumnWidth(7, 180);
  90. view->setColumnWidth(8, 180);
  91. view->setColumnWidth(9, 120);
  92. view->setColumnWidth(10, 90);
  93. view->setSelectionBehavior(QAbstractItemView::SelectRows);
  94. view->setSelectionMode(QAbstractItemView::ExtendedSelection);
  95. view->setEditTriggers(QAbstractItemView::NoEditTriggers);
  96. view->setContextMenuPolicy(Qt::CustomContextMenu);
  97. view->horizontalHeader()->setStretchLastSection(QHeaderView::Stretch);
  98. view->verticalHeader()->hide();
  99. view->setAlternatingRowColors(true);
  100. view->hideColumn(0);
  101. view->show();
  102. }
  103. void LogbookWidget::setupEasaView()
  104. {
  105. DEB("Loading EASA View...");
  106. displayModel = new QSqlTableModel;
  107. displayModel->setTable("viewEASA");
  108. displayModel->select();
  109. view = ui->tableView;
  110. view->setModel(displayModel);
  111. view->setColumnWidth(1,120);
  112. view->setColumnWidth(2,60);
  113. view->setColumnWidth(3,60);
  114. view->setColumnWidth(4,60);
  115. view->setColumnWidth(5,60);
  116. view->setColumnWidth(6,180);
  117. view->setColumnWidth(7,120);
  118. view->setColumnWidth(8,30);
  119. view->setColumnWidth(9,30);
  120. view->setColumnWidth(10,30);
  121. view->setColumnWidth(11,30);
  122. view->setColumnWidth(12,120);
  123. view->setColumnWidth(13,15);
  124. view->setColumnWidth(14,15);
  125. view->setColumnWidth(15,60);
  126. view->setColumnWidth(16,60);
  127. view->setColumnWidth(17,60);
  128. view->setColumnWidth(18,60);
  129. view->setColumnWidth(19,60);
  130. view->setColumnWidth(20,60);
  131. view->setColumnWidth(21,120);
  132. view->setSelectionBehavior(QAbstractItemView::SelectRows);
  133. view->setSelectionMode(QAbstractItemView::ExtendedSelection);
  134. view->setEditTriggers(QAbstractItemView::NoEditTriggers);
  135. view->setContextMenuPolicy(Qt::CustomContextMenu);
  136. view->horizontalHeader()->setStretchLastSection(QHeaderView::Stretch);
  137. view->verticalHeader()->hide();
  138. view->setAlternatingRowColors(true);
  139. view->hideColumn(0);
  140. view->show();
  141. }
  142. /*
  143. * Slots
  144. */
  145. void LogbookWidget::flightsTableView_selectionChanged()//
  146. {
  147. selectedFlights.clear();
  148. for (const auto& row : selection->selectedRows()) {
  149. selectedFlights.append(row.data().toInt());
  150. DEB("Selected Flight(s) with ID: " << selectedFlights);
  151. }
  152. }
  153. void LogbookWidget::on_newFlightButton_clicked()
  154. {
  155. auto nf = new NewFlightDialog(this, Db::createNew);
  156. nf->setAttribute(Qt::WA_DeleteOnClose);
  157. nf->exec();
  158. displayModel->select();
  159. }
  160. void LogbookWidget::on_editFlightButton_clicked()
  161. {
  162. if(selectedFlights.length() == 1){
  163. auto ef = new NewFlightDialog(this,Flight(selectedFlights.first()), Db::editExisting);
  164. ef->setAttribute(Qt::WA_DeleteOnClose);
  165. ef->exec();
  166. displayModel->select();
  167. } else if (selectedFlights.isEmpty()) {
  168. messageBox->setText("No flight selected.\n");
  169. messageBox->exec();
  170. } else {
  171. messageBox->setText("More than one flight selected.\n\nEditing multiple entries is not yet supported.");
  172. messageBox->exec();
  173. }
  174. }
  175. void LogbookWidget::on_deleteFlightPushButton_clicked()
  176. {
  177. DEB("Flights selected: " << selectedFlights.length());
  178. if (selectedFlights.length() == 0) {
  179. messageBox->setIcon(QMessageBox::Information);
  180. messageBox->setText("No Flight Selected.");
  181. messageBox->exec();
  182. return;
  183. } else if (selectedFlights.length() > 0 && selectedFlights.length() < 11) {
  184. QList<experimental::AFlightEntry> flights_list;
  185. for (const auto &flight_id : selectedFlights) {
  186. flights_list.append(experimental::aDB()->getFlightEntry(flight_id));
  187. }
  188. QString warningMsg = "The following flight(s) will be deleted:<br><br><b><tt>";
  189. for (auto &flight : flights_list) {
  190. warningMsg.append(flight.summary());
  191. warningMsg.append(QLatin1String("&nbsp;&nbsp;&nbsp;&nbsp;<br>"));
  192. }
  193. warningMsg.append("</b></tt><br>Deleting Flights is irreversible."
  194. "<br>Do you want to proceed?");
  195. QMessageBox confirm;
  196. confirm.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
  197. confirm.setDefaultButton(QMessageBox::No);
  198. confirm.setIcon(QMessageBox::Question);
  199. confirm.setWindowTitle("Delete Flight");
  200. confirm.setText(warningMsg);
  201. int reply = confirm.exec();
  202. if (reply == QMessageBox::Yes) {
  203. for (auto& flight : flights_list) {
  204. DEB("Deleting flight: " << flight.summary());
  205. experimental::aDB()->remove(flight);
  206. }
  207. displayModel->select();
  208. }
  209. } else if (selectedFlights.length() > 10) {
  210. auto& warningMsg = "You have selected " + QString::number(selectedFlights.length())
  211. + " flights.\n\n Deleting flights is irreversible.\n\n"
  212. "Are you sure you want to proceed?";
  213. QMessageBox confirm;
  214. confirm.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
  215. confirm.setDefaultButton(QMessageBox::No);
  216. confirm.setIcon(QMessageBox::Warning);
  217. confirm.setWindowTitle("Delete Flight");
  218. confirm.setText(warningMsg);
  219. int reply = confirm.exec();
  220. if(reply == QMessageBox::Yes) {
  221. QList<experimental::DataPosition> selected_flights;
  222. for (const auto& flight_id : selectedFlights) {
  223. selected_flights.append({"flights", flight_id});
  224. }
  225. experimental::aDB()->removeMany(selected_flights);
  226. displayModel->select();
  227. }
  228. }
  229. }
  230. void LogbookWidget::on_tableView_customContextMenuRequested(const QPoint &pos)
  231. {
  232. menu->popup(ui->tableView->viewport()->mapToGlobal(pos));
  233. }
  234. void LogbookWidget::on_actionDelete_Flight_triggered()
  235. {
  236. emit ui->deleteFlightPushButton->clicked();
  237. }
  238. void LogbookWidget::onDeletedSuccessfully()
  239. {
  240. messageBox->setText(QString::number(selectedFlights.length()) + " flights have been deleted.");
  241. messageBox->exec();
  242. }
  243. void LogbookWidget::onDeleteUnsuccessful(const QSqlError error)
  244. {
  245. messageBox->setText("Error deleting " + QString::number(selectedFlights.length())
  246. + " flights.\n\nThe following error has ocurred:\n\n" + error.text());
  247. messageBox->exec();
  248. }
  249. void LogbookWidget::on_actionEdit_Flight_triggered()
  250. {
  251. emit ui->editFlightButton->clicked();
  252. }
  253. void LogbookWidget::on_tableView_doubleClicked()
  254. {
  255. emit ui->editFlightButton->clicked();
  256. }
  257. void LogbookWidget::on_flightSearchComboBox_currentIndexChanged()
  258. {
  259. emit ui->showAllButton->clicked();
  260. }
  261. void LogbookWidget::on_showAllButton_clicked()
  262. {
  263. ui->flightSearchLlineEdit->setText(QString());
  264. displayModel->setFilter(QString());
  265. displayModel->select();
  266. }
  267. void LogbookWidget::on_flightSearchLlineEdit_textChanged(const QString &arg1)
  268. {
  269. if(arg1.length() == 0) {
  270. displayModel->setFilter("");
  271. displayModel->select();
  272. return;
  273. }
  274. if (ui->flightSearchComboBox->currentIndex() < 3) {
  275. displayModel->setFilter(FILTER_MAP.value(ui->flightSearchComboBox->currentIndex())
  276. + arg1 + "%\"");
  277. return;
  278. } else if (ui->flightSearchComboBox->currentIndex() == 3) { // registration
  279. displayModel->setFilter(FILTER_MAP.value(ui->flightSearchComboBox->currentIndex())
  280. + arg1 + "%\"");
  281. return;
  282. } else if (ui->flightSearchComboBox->currentIndex() == 4) { // Name Pic
  283. displayModel->setFilter(FILTER_MAP.value(ui->flightSearchComboBox->currentIndex())
  284. + arg1 + "%\"");
  285. return;
  286. }
  287. }