123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320 |
- /*
- *openPilot Log - A FOSS Pilot Logbook Application
- *Copyright (C) 2020 Felix Turowsky
- *
- *This program is free software: you can redistribute it and/or modify
- *it under the terms of the GNU General Public License as published by
- *the Free Software Foundation, either version 3 of the License, or
- *(at your option) any later version.
- *
- *This program is distributed in the hope that it will be useful,
- *but WITHOUT ANY WARRANTY; without even the implied warranty of
- *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- *GNU General Public License for more details.
- *
- *You should have received a copy of the GNU General Public License
- *along with this program. If not, see <https://www.gnu.org/licenses/>.
- */
- #include "newtail.h"
- #include "ui_newtail.h"
- // Debug Makro
- #define DEB(expr) \
- qDebug() << __PRETTY_FUNCTION__<< "\t" << expr
- const auto MAKE_VALID = QPair<QString, QRegularExpression> {
- "makeLineEdit", QRegularExpression("[a-zA-Z]+")};
- const auto MODEL_VALID = QPair<QString, QRegularExpression> {
- "modelLineEdit", QRegularExpression("\\w+")};
- const auto VARIANT_VALID = QPair<QString, QRegularExpression> {
- "variantLineEdit", QRegularExpression("\\w+")};
- const auto LINE_EDIT_VALIDATORS = QVector({MAKE_VALID, MODEL_VALID, VARIANT_VALID});
- //Dialog to be used to create a new tail
- NewTail::NewTail(QString newreg, Db::editRole edRole, QWidget *parent) :
- QDialog(parent),
- ui(new Ui::NewTail)
- {
- ui->setupUi(this);
- role = edRole;
- setupCompleter();
- setupValidators();
- ui->editLabel->hide();
- ui->registrationLineEdit->setText(newreg);
- ui->searchLineEdit->setStyleSheet("border: 1px solid blue");
- ui->searchLineEdit->setFocus();
- }
- //Dialog to be used to edit an existing tail
- NewTail::NewTail(Aircraft dbentry, Db::editRole edRole, QWidget *parent) :
- QDialog(parent),
- ui(new Ui::NewTail)
- {
- oldEntry = dbentry;
- role = edRole;
- ui->setupUi(this);
- ui->searchLabel->hide();
- ui->searchLineEdit->hide();
- ui->line->hide();
- setupValidators();
- formFiller(oldEntry);
- }
- NewTail::~NewTail()
- {
- delete ui;
- }
- /// Functions
- /*!
- * \brief NewTail::setupCompleter creates a QMap<aircaft_id,searchstring> for auto completion,
- * obtains a QStringList for QCompleter and sets up search line edit accordingly
- */
- void NewTail::setupCompleter()
- {
- auto query = QLatin1String("SELECT make||' '||model||'-'||variant, aircraft_id FROM aircraft");
- auto vector = Db::customQuery(query, 2);
- QMap<QString, int> map;
- for (int i = 0; i < vector.length() - 2 ; i += 2) {
- if (vector[i] != QLatin1String("")) {
- map.insert(vector[i], vector[i + 1].toInt());
- }
- }
- //creating QStringlist for QCompleter. This list is identical to a list of map<key>,
- //but creating it like this is faster.
- auto cl = new CompletionList(CompleterTarget::aircraft);
- aircraftlist = cl->list;
- idMap = map;
- QCompleter *completer = new QCompleter(aircraftlist, ui->searchLineEdit);
- completer->setCaseSensitivity(Qt::CaseInsensitive);
- completer->setCompletionMode(QCompleter::PopupCompletion);
- completer->setFilterMode(Qt::MatchContains);
- ui->searchLineEdit->setCompleter(completer);
- }
- void NewTail::setupValidators()
- {
- for(const auto& pair : LINE_EDIT_VALIDATORS){
- auto line_edit = parent()->findChild<QLineEdit*>(pair.first);
- auto validator = new QRegularExpressionValidator(pair.second,line_edit);
- line_edit->setValidator(validator);
- }
- }
- /*!
- * \brief NewTail::formFiller populates the Dialog with the
- * information contained in an aircraft object.
- * \param db - entry retreived from database
- */
- void NewTail::formFiller(Aircraft entry)
- {
- DEB("Filling Form for a/c" << entry);
- //fill Line Edits
- auto line_edits = parent()->findChildren<QLineEdit *>();
- for (const auto &le : line_edits) {
- QString name = le->objectName();
- name.chop(8);//remove "LineEdit"
- QString value = entry.data.value(name);
- if (!value.isEmpty()) {
- le->setText(value);
- };
- }
- //select comboboxes
- QVector<QString> operation = {entry.data.value("singleengine"), entry.data.value("multiengine")};
- QVector<QString> ppNumber = {entry.data.value("singlepilot"), entry.data.value("multipilot")};
- QVector<QString> ppType = {entry.data.value("unpowered"), entry.data.value("piston"),
- entry.data.value("turboprop"), entry.data.value("jet")
- };
- QVector<QString> weight = {entry.data.value("light"), entry.data.value("medium"),
- entry.data.value("heavy"), entry.data.value("super")
- };
- ui->operationComboBox->setCurrentIndex(operation.indexOf("1") + 1);
- ui->ppNumberComboBox->setCurrentIndex(ppNumber.indexOf("1") + 1);
- ui->ppTypeComboBox->setCurrentIndex(ppType.indexOf("1") + 1);
- ui->weightComboBox->setCurrentIndex(weight.indexOf("1") + 1);
- }
- /*!
- * \brief NewTail::verify A simple check for empty recommended fields in the form
- * \return true if all reconmmended fields are populated
- */
- bool NewTail::verify()
- {
- auto recommended_line_edits = parent()->findChildren<QLineEdit *>("registrationLineEdit");
- recommended_line_edits.append(parent()->findChild<QLineEdit *>("makeLineEdit"));
- recommended_line_edits.append(parent()->findChild<QLineEdit *>("modelLineEdit"));
- auto recommended_combo_boxes = parent()->findChildren<QComboBox *>("operationComboBox");
- recommended_combo_boxes.append(parent()->findChild<QComboBox *>("ppNumberComboBox"));
- recommended_combo_boxes.append(parent()->findChild<QComboBox *>("ppTypeComboBox"));
- for (const auto le : recommended_line_edits) {
- if (le->text() != "") {
- DEB("Good: " << le);
- recommended_line_edits.removeOne(le);
- le->setStyleSheet("");
- } else {
- le->setStyleSheet("border: 1px solid red");
- DEB("Not Good: " << le);
- }
- }
- for (const auto cb : recommended_combo_boxes) {
- if (cb->currentIndex() != 0) {
- recommended_combo_boxes.removeOne(cb);
- cb->setStyleSheet("");
- } else {
- cb->setStyleSheet("background: orange");
- DEB("Not Good: " << cb);
- }
- }
- if (recommended_line_edits.isEmpty() && recommended_combo_boxes.isEmpty()) {
- return true;
- } else {
- return false;
- }
- }
- /*!
- * \brief NewTail::submitForm collects input from Line Edits and creates
- * or updates a database entry and commits or updates the database
- * \param edRole editExisting or createNew
- */
- void NewTail::submitForm(Db::editRole edRole)
- {
- DEB("Creating Database Object...");
- QMap<QString, QString> newData;
- //retreive Line Edits
- auto line_edits = parent()->findChildren<QLineEdit *>();
- for (const auto &le : line_edits) {
- QString name = le->objectName();
- name.chop(8);//remove "LineEdit"
- if (!le->text().isEmpty()) {
- newData.insert(name, le->text());
- }
- }
- //prepare comboboxes
- QVector<QString> operation = {"singlepilot", "multipilot"};
- QVector<QString> ppNumber = {"singleengine", "multiengine"};
- QVector<QString> ppType = {"unpowered", "piston",
- "turboprop", "jet"
- };
- QVector<QString> weight = {"light", "medium",
- "heavy", "super"
- };
- if (ui->operationComboBox->currentIndex() != 0) {
- newData.insert(operation[ui->operationComboBox->currentIndex() - 1], QLatin1String("1"));
- }
- if (ui->ppNumberComboBox->currentIndex() != 0) {
- newData.insert(ppNumber[ui->ppNumberComboBox->currentIndex() - 1], QLatin1String("1"));
- }
- if (ui->ppTypeComboBox->currentIndex() != 0) {
- newData.insert(ppType[ui->ppTypeComboBox->currentIndex() - 1], QLatin1String("1"));
- }
- if (ui->weightComboBox->currentIndex() != 0) {
- newData.insert(weight[ui->weightComboBox->currentIndex() - 1], QLatin1String("1"));
- }
- //create db object
- switch (edRole) {
- case Db::createNew: {
- auto newEntry = Aircraft("tails", newData);;
- newEntry.commit();
- break;
- }
- case Db::editExisting:
- oldEntry.setData(newData);
- oldEntry.commit();
- Calc::updateAutoTimes(oldEntry.position.second);
- break;
- }
- }
- /// Slots
- void NewTail::on_searchLineEdit_textChanged(const QString &arg1)
- {
- if (aircraftlist.contains(
- arg1)) { //equivalent to editing finished for this purpose. todo: consider connecing qcompleter activated signal with editing finished slot.
- DEB("Template Selected. aircraft_id is: " << idMap.value(arg1));
- //call autofiller for dialog
- formFiller(Aircraft("aircraft", idMap.value(arg1)));
- ui->searchLineEdit->setStyleSheet("border: 1px solid green");
- } else {
- //for example, editing finished without selecting a result from Qcompleter
- ui->searchLineEdit->setStyleSheet("border: 1px solid orange");
- }
- }
- void NewTail::on_operationComboBox_currentIndexChanged(int index)
- {
- if (index != 0) {
- ui->operationComboBox->setStyleSheet("");
- }
- }
- void NewTail::on_ppTypeComboBox_currentIndexChanged(int index)
- {
- if (index != 0) {
- ui->ppTypeComboBox->setStyleSheet("");
- }
- }
- void NewTail::on_ppNumberComboBox_currentIndexChanged(int index)
- {
- if (index != 0) {
- ui->ppNumberComboBox->setStyleSheet("");
- }
- }
- void NewTail::on_weightComboBox_currentIndexChanged(int index)
- {
- if (index != 0) {
- ui->weightComboBox->setStyleSheet("");
- }
- }
- void NewTail::on_buttonBox_accepted()
- {
- DEB("Button Box Accepted.");
- if (ui->registrationLineEdit->text().isEmpty()) {
- auto nope = new QMessageBox(this);
- nope->setText("Registration cannot be empty.");
- nope->show();
- } else {
- if (verify()) {
- DEB("Form verified");
- submitForm(role);
- accept();
- } else {
- QSettings setting;
- if (!setting.value("userdata/acAllowIncomplete").toInt()) {
- auto nope = new QMessageBox(this);
- nope->setText("Some or all fields are empty.\nPlease go back and "
- "complete the form.\n\nYou can allow logging incomplete entries on the settings page.");
- nope->show();
- } else {
- QMessageBox::StandardButton reply;
- reply = QMessageBox::question(this, "Warning",
- "Some recommended fields are empty.\n\n"
- "If you do not fill out the aircraft details, "
- "it will be impossible to automatically determine Single/Multi Pilot Times or Single/Multi Engine Time."
- "This will also impact statistics and auto-logging capabilites.\n\n"
- "It is highly recommended to fill in all the details.\n\n"
- "Are you sure you want to proceed?",
- QMessageBox::Yes | QMessageBox::No);
- if (reply == QMessageBox::Yes) {
- submitForm(role);
- accept();
- }
- }
- }
- }
- }
|