debugwidget.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 "debugwidget.h"
  19. #include "ui_debugwidget.h"
  20. #include "src/classes/astandardpaths.h"
  21. #include "src/gui/widgets/logbookwidget.h"
  22. #include "src/gui/widgets/pilotswidget.h"
  23. #include "src/gui/widgets/aircraftwidget.h"
  24. #include "src/gui/dialogues/firstrundialog.h"
  25. #include <QtGlobal>
  26. #include "src/functions/atime.h"
  27. #include "src/functions/astat.h"
  28. #include "src/classes/acurrencyentry.h"
  29. #include "src/classes/atranslator.h"
  30. DebugWidget::DebugWidget(QWidget *parent) :
  31. QWidget(parent),
  32. ui(new Ui::DebugWidget)
  33. {
  34. ui->setupUi(this);
  35. for (const auto& table : aDB->getTableNames()) {
  36. if( table != "sqlite_sequence") {
  37. ui->tableComboBox->addItem(table);
  38. }
  39. }
  40. }
  41. DebugWidget::~DebugWidget()
  42. {
  43. delete ui;
  44. }
  45. void DebugWidget::on_resetUserTablesPushButton_clicked()
  46. {
  47. ATimer timer(this);
  48. QMessageBox message_box(this);
  49. if (ADataBaseSetup::resetToDefault()){
  50. message_box.setText("Database successfully reset");
  51. message_box.exec();
  52. emit aDB->dataBaseUpdated();
  53. } else {
  54. message_box.setText("Errors have occurred. Check console for Debug output. ");
  55. message_box.exec();
  56. }
  57. }
  58. void DebugWidget::on_resetDatabasePushButton_clicked()
  59. {
  60. ATimer timer(this);
  61. QMessageBox message_box(this);
  62. // download latest csv
  63. QString link_stub = "https://raw.githubusercontent.com/fiffty-50/openpilotlog/";
  64. link_stub.append(ui->branchLineEdit->text()); // optionally select branch for development
  65. link_stub.append("/assets/database/templates/");
  66. QStringList template_tables = {"aircraft", "airports", "changelog"};
  67. QDir template_dir(AStandardPaths::directory(AStandardPaths::Templates));
  68. for (const auto& table : template_tables) {
  69. QEventLoop loop;
  70. ADownload* dl = new ADownload;
  71. QObject::connect(dl, &ADownload::done, &loop, &QEventLoop::quit );
  72. dl->setTarget(QUrl(link_stub % table % QStringLiteral(".csv")));
  73. dl->setFileName(template_dir.filePath(table % QStringLiteral(".csv")));
  74. dl->download();
  75. loop.exec(); // event loop waits for download done signal before allowing loop to continue
  76. dl->deleteLater();
  77. }
  78. // back up old db
  79. aDB->disconnect();
  80. ADataBaseSetup::backupOldData();
  81. // re-connct and create new database
  82. aDB->connect();
  83. if (ADataBaseSetup::createDatabase()) {
  84. DEB << "Database has been successfully created.";
  85. } else {
  86. message_box.setText("Errors have ocurred creating the database.<br>"
  87. "Check console for details.");
  88. message_box.exec();
  89. }
  90. if (ADataBaseSetup::importDefaultData(false)) {
  91. message_box.setText("Database has been successfully reset.");
  92. emit aDB->dataBaseUpdated();
  93. message_box.exec();
  94. } else {
  95. message_box.setText("Errors have ocurred while importing templates.<br>"
  96. "Check console for details.");
  97. message_box.exec();
  98. }
  99. }
  100. void DebugWidget::downloadFinished()
  101. {
  102. }
  103. void DebugWidget::on_fillUserDataPushButton_clicked()
  104. {
  105. ATimer timer(this);
  106. QMessageBox message_box(this);
  107. // download latest csv
  108. QStringList userTables = {"pilots", "tails", "flights"};
  109. QString linkStub = "https://raw.githubusercontent.com/fiffty-50/openpilotlog/";
  110. linkStub.append(ui->branchLineEdit->text());
  111. linkStub.append("/assets/database/templates/sample_");
  112. QDir template_dir(AStandardPaths::directory(AStandardPaths::Templates));
  113. for (const auto& table : userTables) {
  114. QEventLoop loop;
  115. ADownload* dl = new ADownload;
  116. connect(dl, &ADownload::done, &loop, &QEventLoop::quit );
  117. dl->setTarget(QUrl(linkStub + table + ".csv"));
  118. dl->setFileName(template_dir.filePath("sample_" % table % QStringLiteral(".csv")));
  119. dl->download();
  120. loop.exec(); // event loop waits for download done signal before allowing loop to continue
  121. dl->deleteLater();
  122. }
  123. QBitArray allGood;
  124. allGood.resize(userTables.size());
  125. for (const auto& table : userTables) {
  126. auto data = aReadCsv(AStandardPaths::directory(AStandardPaths::Templates).absoluteFilePath(
  127. + "sample_" + table + ".csv"));
  128. allGood.setBit(userTables.indexOf(table), ADataBaseSetup::commitData(data, table));
  129. }
  130. if (allGood.count(true) != userTables.size()) {
  131. message_box.setText("Errors have ocurred. Check console for details.");
  132. message_box.exec();
  133. return;
  134. }
  135. message_box.setText("User tables successfully populated.");
  136. message_box.exec();
  137. emit aDB->dataBaseUpdated();
  138. }
  139. void DebugWidget::on_selectCsvPushButton_clicked()
  140. {
  141. auto fileName = QFileDialog::getOpenFileName(this,
  142. tr("Open CSV File for import"),
  143. AStandardPaths::directory(AStandardPaths::Templates).absolutePath(),
  144. tr("CSV files (*.csv)"));
  145. ui->importCsvLineEdit->setText(fileName);
  146. }
  147. void DebugWidget::on_importCsvPushButton_clicked()
  148. {
  149. ATimer timer(this);
  150. auto file = QFileInfo(ui->importCsvLineEdit->text());
  151. DEB << "File exists/is file:" << file.exists() << file.isFile() << " Path:" << file.absoluteFilePath();
  152. if (file.exists() && file.isFile()) {
  153. if (ADataBaseSetup::commitData(aReadCsv(file.absoluteFilePath()), ui->tableComboBox->currentText())) {
  154. QMessageBox message_box(this);
  155. message_box.setText("Data inserted successfully.");
  156. message_box.exec();
  157. } else {
  158. QMessageBox message_box(this);
  159. message_box.setText("Errors have ocurred. Check console for details.");
  160. message_box.exec();
  161. }
  162. } else {
  163. QMessageBox message_box(this);
  164. message_box.setText("Please select a valid file.");
  165. message_box.exec();
  166. }
  167. }
  168. #include "src/functions/adate.h"
  169. void DebugWidget::on_debugPushButton_clicked()
  170. {
  171. //auto frdl = new FirstRunDialog(this);
  172. //frdl->exec();
  173. ATranslator::installTranslator(Opl::Translations::Spanish);
  174. //ui->retranslateUi(this);
  175. }
  176. void DebugWidget::changeEvent(QEvent *event)
  177. {
  178. if (event != nullptr)
  179. if(event->type() == QEvent::LanguageChange)
  180. ui->retranslateUi(this);
  181. }
  182. /* //Comparing two functions template
  183. qlonglong number_of_runs = 5000;
  184. long time1 = 0;
  185. long time2 = 0;
  186. {
  187. ATimer timer;
  188. for (int i = 0; i < number_of_runs; i++) {
  189. // first block, do stuff here...
  190. }
  191. time1 = timer.timeNow();
  192. }
  193. {
  194. ATimer timer;
  195. for (int i = 0; i < number_of_runs; i++) {
  196. // second block, do stuff here...
  197. }
  198. time2 = timer.timeNow();
  199. }
  200. DEB << "First block executed " << number_of_runs << " times for a total of " << time1 << " milliseconds.");
  201. DEB << "Second block executed " << number_of_runs << " times for a total of " << time2 << " milliseconds.");
  202. */
  203. /*
  204. *#if QT_VERSION >= QT_VERSION_CHECK(5, 12, 0)
  205. * DEB << QT_VERSION_MAJOR << QT_VERSION_MINOR << "At least 5.12";
  206. *#else
  207. * DEB << QT_VERSION_MAJOR << QT_VERSION_MINOR << "Less than 5.12";
  208. * #endif
  209. */