settingswidget.cpp 13 KB

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