settingswidget.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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 "settingswidget.h"
  19. #include "ui_settingswidget.h"
  20. #include "src/testing/adebug.h"
  21. #include "src/classes/astyle.h"
  22. #include "src/classes/asettings.h"
  23. #include "src/database/adatabase.h"
  24. #include "src/classes/apilotentry.h"
  25. #include "src/oplconstants.h"
  26. #include <QStyleFactory>
  27. static const auto FIRSTNAME_VALID = QPair<QString, QRegularExpression> {
  28. QStringLiteral("firstnameLineEdit"), QRegularExpression("[a-zA-Z]+")};
  29. static const auto LASTNAME_VALID = QPair<QString, QRegularExpression> {
  30. QStringLiteral("lastnameLineEdit"), QRegularExpression("\\w+")};
  31. static const auto PHONE_VALID = QPair<QString, QRegularExpression> {
  32. QStringLiteral("phoneLineEdit"), QRegularExpression("^[+]{0,1}[0-9\\-\\s]+")};
  33. static const auto EMAIL_VALID = QPair<QString, QRegularExpression> {
  34. QStringLiteral("emailLineEdit"), QRegularExpression("\\A[a-z0-9!#$%&'*+/=?^_‘{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_‘{|}~-]+)*@"
  35. "(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\z")};
  36. static const auto COMPANY_VALID = QPair<QString, QRegularExpression> {
  37. QStringLiteral("companyLineEdit"), QRegularExpression("\\w+")};
  38. static const auto EMPLOYEENR_VALID = QPair<QString, QRegularExpression> {
  39. QStringLiteral("employeeidLineEdit"), QRegularExpression("\\w+")};
  40. static const auto PREFIX_VALID = QPair<QString, QRegularExpression> {
  41. QStringLiteral("prefixLineEdit"), QRegularExpression("[a-zA-Z0-9]?[a-zA-Z0-9]?[a-zA-Z0-9]")};
  42. static const auto LINE_EDIT_VALIDATORS = QVector<QPair<QString, QRegularExpression>>{
  43. FIRSTNAME_VALID, LASTNAME_VALID, PHONE_VALID, EMAIL_VALID,
  44. COMPANY_VALID, EMPLOYEENR_VALID, PREFIX_VALID};
  45. SettingsWidget::SettingsWidget(QWidget *parent) :
  46. QWidget(parent),
  47. ui(new Ui::SettingsWidget)
  48. {
  49. ui->setupUi(this);
  50. ui->tabWidget->setCurrentIndex(0);
  51. setupComboBoxes();
  52. setupValidators();
  53. readSettings();
  54. }
  55. SettingsWidget::~SettingsWidget()
  56. {
  57. delete ui;
  58. }
  59. void SettingsWidget::setupComboBoxes(){
  60. {
  61. // Style combo box
  62. const QSignalBlocker blocker_style(ui->styleComboBox);
  63. ui->styleComboBox->addItems(AStyle::styles);
  64. for (const auto &style_sheet_name : AStyle::styleSheets) {
  65. ui->styleComboBox->addItem(style_sheet_name.first);
  66. }
  67. ui->styleComboBox->model()->sort(0);
  68. // Approach Combo Box
  69. const QSignalBlocker blocker_approach(ui->approachComboBox);
  70. for (const auto &approach : Opl::ApproachTypes) {
  71. ui->approachComboBox->addItem(approach);
  72. }
  73. }
  74. }
  75. void SettingsWidget::readSettings()
  76. {
  77. /*
  78. * Personal Tab
  79. */
  80. {
  81. const QSignalBlocker blocker(this); // don't emit editing finished for setting these values
  82. auto user_data = aDB->getPilotEntry(1).getData();
  83. ui->lastnameLineEdit->setText(user_data.value(Opl::Db::PILOTS_LASTNAME).toString());
  84. ui->firstnameLineEdit->setText(user_data.value(Opl::Db::PILOTS_FIRSTNAME).toString());
  85. ui->companyLineEdit->setText(user_data.value(Opl::Db::PILOTS_COMPANY).toString());
  86. ui->employeeidLineEdit->setText(user_data.value(Opl::Db::PILOTS_EMPLOYEEID).toString());
  87. ui->phoneLineEdit->setText(user_data.value(Opl::Db::PILOTS_PHONE).toString());
  88. ui->emailLineEdit->setText(user_data.value(Opl::Db::PILOTS_EMAIL).toString());
  89. }
  90. /*
  91. * Flight Logging Tab
  92. */
  93. ui->aliasComboBox->setCurrentIndex(ASettings::read(ASettings::UserData::DisplaySelfAs).toInt());
  94. ui->functionComboBox->setCurrentText(ASettings::read(ASettings::FlightLogging::Function).toString());
  95. ui->rulesComboBox->setCurrentText(ASettings::read(ASettings::FlightLogging::Rules).toString());
  96. ui->approachComboBox->setCurrentText(ASettings::read(ASettings::FlightLogging::Approach).toString());
  97. ui->nightComboBox->setCurrentIndex(ASettings::read(ASettings::FlightLogging::NightLogging).toInt());
  98. ui->prefixLineEdit->setText(ASettings::read(ASettings::FlightLogging::FlightNumberPrefix).toString());
  99. ui->logbookViewComboBox->setCurrentIndex(ASettings::read(ASettings::LogBook::View).toInt());
  100. /*
  101. * Misc Tab
  102. */
  103. ui->acSortComboBox->setCurrentIndex(ASettings::read(ASettings::UserData::AcftSortColumn).toInt());
  104. ui->pilotSortComboBox->setCurrentIndex(ASettings::read(ASettings::UserData::PilSortColumn).toInt());
  105. ui->acAllowIncompleteComboBox->setCurrentIndex(ASettings::read(ASettings::UserData::AcAllowIncomplete).toInt());
  106. ui->styleComboBox->setCurrentText(ASettings::read(ASettings::Main::Style).toString());
  107. }
  108. void SettingsWidget::setupValidators()
  109. {
  110. DEB << "Setting up Validators...";
  111. for(const auto& pair : LINE_EDIT_VALIDATORS){
  112. auto line_edit = parent()->findChild<QLineEdit*>(pair.first);
  113. if(line_edit != nullptr){
  114. auto validator = new QRegularExpressionValidator(pair.second,line_edit);
  115. line_edit->setValidator(validator);
  116. }else{
  117. DEB << "Error: Line Edit not found: "<< pair.first << " - skipping.";
  118. }
  119. }
  120. }
  121. void SettingsWidget::updatePersonalDetails()
  122. {
  123. RowData_T user_data;
  124. switch (ui->aliasComboBox->currentIndex()) {
  125. case 0:
  126. user_data.insert(Opl::Db::PILOTS_ALIAS, QStringLiteral("self"));
  127. break;
  128. case 1:
  129. user_data.insert(Opl::Db::PILOTS_ALIAS, QStringLiteral("SELF"));
  130. break;
  131. case 2:{
  132. QString name;
  133. name.append(ui->lastnameLineEdit->text());
  134. name.append(QLatin1String(", "));
  135. name.append(ui->firstnameLineEdit->text().left(1));
  136. name.append(QLatin1Char('.'));
  137. user_data.insert(Opl::Db::PILOTS_ALIAS, name);
  138. }
  139. break;
  140. default:
  141. break;
  142. }
  143. user_data.insert(Opl::Db::PILOTS_LASTNAME, ui->lastnameLineEdit->text());
  144. user_data.insert(Opl::Db::PILOTS_FIRSTNAME, ui->firstnameLineEdit->text());
  145. user_data.insert(Opl::Db::PILOTS_COMPANY, ui->companyLineEdit->text());
  146. user_data.insert(Opl::Db::PILOTS_EMPLOYEEID, ui->employeeidLineEdit->text());
  147. user_data.insert(Opl::Db::PILOTS_PHONE, ui->phoneLineEdit->text());
  148. user_data.insert(Opl::Db::PILOTS_EMAIL, ui->emailLineEdit->text());
  149. auto user = APilotEntry(1);
  150. user.setData(user_data);
  151. aDB->commit(user);
  152. }
  153. /*
  154. * Slots
  155. */
  156. /*
  157. * Personal Tab
  158. */
  159. void SettingsWidget::on_lastnameLineEdit_editingFinished()
  160. {
  161. updatePersonalDetails();
  162. }
  163. void SettingsWidget::on_firstnameLineEdit_editingFinished()
  164. {
  165. updatePersonalDetails();
  166. }
  167. void SettingsWidget::on_companyLineEdit_editingFinished()
  168. {
  169. updatePersonalDetails();
  170. }
  171. void SettingsWidget::on_employeeidLineEdit_editingFinished()
  172. {
  173. updatePersonalDetails();
  174. }
  175. void SettingsWidget::on_emailLineEdit_editingFinished()
  176. {
  177. updatePersonalDetails();
  178. }
  179. void SettingsWidget::on_phoneLineEdit_editingFinished()
  180. {
  181. updatePersonalDetails();
  182. }
  183. /*
  184. * Flight Logging Tab
  185. */
  186. void SettingsWidget::on_aliasComboBox_currentIndexChanged(int index)
  187. {
  188. ASettings::write(ASettings::UserData::DisplaySelfAs, index);
  189. updatePersonalDetails();
  190. }
  191. void SettingsWidget::on_functionComboBox_currentIndexChanged(const QString &arg1)
  192. {
  193. ASettings::write(ASettings::FlightLogging::Function, arg1);
  194. }
  195. void SettingsWidget::on_rulesComboBox_currentIndexChanged(const QString &arg1)
  196. {
  197. ASettings::write(ASettings::FlightLogging::Rules, arg1);
  198. }
  199. void SettingsWidget::on_approachComboBox_currentIndexChanged(const QString &arg1)
  200. {
  201. ASettings::write(ASettings::FlightLogging::Approach, arg1);
  202. }
  203. void SettingsWidget::on_nightComboBox_currentIndexChanged(int index)
  204. {
  205. ASettings::write(ASettings::FlightLogging::NightLogging, index);
  206. switch (index) {
  207. case 1:
  208. ASettings::write(ASettings::FlightLogging::NightAngle, -6);
  209. break;
  210. case 2:
  211. ASettings::write(ASettings::FlightLogging::NightAngle, 0);
  212. break;
  213. default:
  214. ASettings::write(ASettings::FlightLogging::NightAngle, -6);
  215. }
  216. }
  217. void SettingsWidget::on_prefixLineEdit_textChanged(const QString &arg1)
  218. {
  219. ASettings::write(ASettings::FlightLogging::FlightNumberPrefix, arg1);
  220. }
  221. /*
  222. * Misc Tab
  223. */
  224. void SettingsWidget::on_logbookViewComboBox_currentIndexChanged(int index)
  225. {
  226. ASettings::write(ASettings::LogBook::View, index);
  227. emit viewSelectionChanged(index);
  228. }
  229. void SettingsWidget::on_pilotSortComboBox_currentIndexChanged(int index)
  230. {
  231. ASettings::write(ASettings::UserData::PilSortColumn, index);
  232. }
  233. void SettingsWidget::on_acSortComboBox_currentIndexChanged(int index)
  234. {
  235. ASettings::write(ASettings::UserData::AcftSortColumn, index);
  236. }
  237. void SettingsWidget::on_acAllowIncompleteComboBox_currentIndexChanged(int index)
  238. {
  239. ASettings::write(ASettings::UserData::AcAllowIncomplete, index);
  240. if (index) {
  241. QMessageBox::StandardButton reply;
  242. reply = QMessageBox::warning(this, tr("Warning"),
  243. tr("Enabling incomplete logging will enable you to add aircraft with incomplete data.<br><br>"
  244. "If you do not fill out the aircraft details, "
  245. "it will be impossible to automatically determine Single/Multi Pilot Times or Single/Multi Engine Time. "
  246. "This will also impact statistics and auto-logging capabilites as well as jeopardise database integrity.<br><br>"
  247. "It is highly recommended to keep this option off unless you have a specific reason not to.<br><br>"
  248. "Are you sure you want to proceed?"),
  249. QMessageBox::Yes | QMessageBox::No);
  250. if (reply == QMessageBox::Yes) {
  251. ASettings::write(ASettings::UserData::AcAllowIncomplete, index);
  252. } else {
  253. ui->acAllowIncompleteComboBox->setCurrentIndex(0);
  254. }
  255. }
  256. }
  257. /*
  258. * About Tab
  259. */
  260. void SettingsWidget::on_aboutPushButton_clicked()
  261. {
  262. QMessageBox message_box(this);
  263. QString SQLITE_VERSION = aDB->sqliteVersion();
  264. QString text = QMessageBox::tr(
  265. "<h3><center>About openPilotLog</center></h3>"
  266. "<br>"
  267. "(c) 2020-2021 Felix Turowsky"
  268. "<br>"
  269. "<p>This is a collaboratively developed Free and Open Source Application. "
  270. "Visit us <a href=\"https://%1/\">here</a> for more information.</p>"
  271. "<p>This program is free software: you can redistribute it and/or modify "
  272. "it under the terms of the GNU General Public License as published by "
  273. "the Free Software Foundation, either version 3 of the License, or "
  274. "(at your option) any later version.</p>"
  275. "<p>This program is distributed in the hope that it will be useful, "
  276. "but WITHOUT ANY WARRANTY; without even the implied warranty of "
  277. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the "
  278. "GNU General Public License for more details.</p> "
  279. "<p>You should have received a copy of the GNU General Public License "
  280. "along with this program. If not, "
  281. "please click <a href=\"https://%2\">here</a>.</p>"
  282. "<br>"
  283. "<p>This program uses <a href=\"http://%3/\">Qt</a> version %4 and "
  284. "<a href=\"https://%5/\">SQLite</a> version %6</p>"
  285. ).arg(QStringLiteral("github.com/fiffty-50/openpilotlog"),
  286. QStringLiteral("gnu.org/licenses/"),
  287. QStringLiteral("qt.io"),
  288. QT_VERSION_STR,
  289. QStringLiteral("sqlite.org/about.html"),
  290. SQLITE_VERSION);
  291. message_box.setText(text);
  292. message_box.exec();
  293. }
  294. void SettingsWidget::on_styleComboBox_currentTextChanged(const QString& new_style_setting)
  295. {
  296. for (const auto &style_name : AStyle::styles) {
  297. if (new_style_setting == style_name) {
  298. AStyle::setStyle(style_name);
  299. ASettings::write(ASettings::Main::Style, new_style_setting);
  300. return;
  301. }
  302. }
  303. for (const auto &style_sheet_name : AStyle::styleSheets) {
  304. if (new_style_setting == style_sheet_name.first) {
  305. AStyle::setStyle(style_sheet_name);
  306. ASettings::write(ASettings::Main::Style, new_style_setting);
  307. return;
  308. }
  309. }
  310. }