Browse Source

Removed CompletionList class

CompletionList class removed. Instead, experimental::DataBase now has a function that returns a QStringList to be used for QCompleters.
Felix Turo 4 years ago
parent
commit
1ba8baab8c

+ 0 - 2
openPilotLog.pro

@@ -20,7 +20,6 @@ SOURCES += \
     mainwindow.cpp \
     src/classes/acalc.cpp \
     src/classes/aircraft.cpp \
-    src/classes/completionlist.cpp \
     src/classes/csv.cpp \
     src/classes/download.cpp \
     src/classes/flight.cpp \
@@ -52,7 +51,6 @@ HEADERS += \
     mainwindow.h \
     src/classes/acalc.h \
     src/classes/aircraft.h \
-    src/classes/completionlist.h \
     src/classes/csv.h \
     src/classes/download.h \
     src/classes/flight.h \

+ 0 - 63
src/classes/completionlist.cpp

@@ -1,63 +0,0 @@
-/*
- *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 "completionlist.h"
-
-CompletionList::CompletionList()
-{
-
-}
-
-/*!
- * \brief completionList::completionList construcs a completionList object.
- * Access object->list for the list.
- * \param type see enum completerTarget::targets
- */
-CompletionList::CompletionList(CompleterTarget::targets type)
-{
-    QString query;
-    QVector<QString> columns;
-    QVector<QString> result;
-
-    switch (type) {
-    case CompleterTarget::airports:
-        columns.append("icao");
-        columns.append("iata");
-        result = Db::multiSelect(columns, "airports");
-        break;
-    case CompleterTarget::registrations:
-        columns.append("registration");
-        result = Db::multiSelect(columns, "tails");
-        break;
-    case CompleterTarget::companies:
-        columns.append("company");
-        result = Db::multiSelect(columns, "pilots");
-        break;
-    case CompleterTarget::pilots:
-        query.append("SELECT piclastname||','||picfirstname FROM pilots");
-        result = Db::customQuery(query, 1);
-        break;
-    case CompleterTarget::aircraft:
-        query.append("SELECT make||' '||model||'-'||variant FROM aircraft");
-        result = Db::customQuery(query, 1);
-        break;
-    }
-
-    list = result.toList();
-    list.removeAll(QString(""));
-    list.removeDuplicates();
-}

+ 0 - 44
src/classes/completionlist.h

@@ -1,44 +0,0 @@
-/*
- *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/>.
- */
-#ifndef COMPLETIONLIST_H
-#define COMPLETIONLIST_H
-
-#include <QCoreApplication>
-#include "src/database/db.h"
-
-class CompleterTarget
-{
-public:
-    enum targets {airports, pilots, registrations, aircraft, companies};
-};
-
-
-/*!
- * \brief The completionList class provides QStringLists to be used by a QCompleter
- */
-class CompletionList
-{
-public:
-    QStringList list;
-
-    CompletionList();
-
-    CompletionList(CompleterTarget::targets);
-};
-
-#endif // COMPLETIONLIST_H

+ 40 - 0
src/experimental/DataBase.cpp

@@ -238,6 +238,46 @@ PilotEntry DataBase::getPilotEntry(RowId row_id)
     return pilotEntry;
 }
 
+QStringList DataBase::getCompletionList(DataBase::CompleterTarget target)
+{
+    QString statement;
+
+    switch (target) {
+    case pilots:
+        statement.append("SELECT piclastname||\",\"||picfirstname FROM pilots");
+        break;
+    case aircraft:
+        statement.append("SELECT make||\" \"||model FROM aircraft WHERE model IS NOT NULL "
+                         "UNION "
+                         "SELECT make||\" \"||model||\"-\"||variant FROM aircraft WHERE variant IS NOT NULL");
+        break;
+    case airports:
+        statement.append("SELECT icao FROM airports UNION SELECT iata FROM airports");
+        break;
+    case registrations:
+        statement.append("SELECT registration FROM tails");
+        break;
+    case companies:
+        statement.append("SELECT company FROM pilots");
+        break;
+    }
+
+    QSqlQuery query(statement);
+    if(!query.first())
+        emit sqlError(query.lastError(), statement);
+
+    query.previous();
+    QStringList completer_list;
+    while (query.next())
+        completer_list.append(query.value(0).toString());
+
+    completer_list.sort();
+    completer_list.removeAll(QString(""));
+    completer_list.removeDuplicates();
+
+    return completer_list;
+}
+
 DataBase* DB() { return DataBase::getInstance(); }
 
 }

+ 13 - 0
src/experimental/DataBase.h

@@ -31,6 +31,12 @@ public:
     void operator=(const DataBase&) = delete;
     static DataBase* getInstance();
 
+    /*!
+     * \brief The CompleterTarget enum provides the items for which QCompleter
+     * completion lists are provided from the database.
+     */
+    enum CompleterTarget {airports, pilots, registrations, aircraft, companies};
+
     /*!
      * \brief Connect to the database and populate database information.
      */
@@ -95,6 +101,13 @@ public:
     PilotEntry getPilotEntry(RowId row_id);
     // [G] TODO: Ensure PilotDialog works great and slowly move to
     // other dialogs
+
+    /*!
+     * \brief getCompletionList returns a QStringList of values for a
+     * QCompleter based on database values
+     * \return
+     */
+    QStringList getCompletionList(CompleterTarget);
 signals:
     void commitSuccessful();
 

+ 8 - 7
src/gui/dialogues/newflightdialog.cpp

@@ -183,9 +183,10 @@ void NewFlightDialog::setup(){
         }
     }
     //fill Lists
-    pilots = CompletionList(CompleterTarget::pilots).list;
-    tails = CompletionList(CompleterTarget::registrations).list;
-    airports = CompletionList(CompleterTarget::airports).list;
+    pilots   = experimental::DB()->getCompletionList(experimental::DataBase::pilots);
+    tails    = experimental::DB()->getCompletionList(experimental::DataBase::registrations);
+    airports = experimental::DB()->getCompletionList(experimental::DataBase::airports);
+
     QString statement = "SELECT iata, icao FROM airports";
     auto result = Db::customQuery(statement,2);
     for(int i=0; i<result.length()-2; i += 2){
@@ -1209,7 +1210,7 @@ void NewFlightDialog::on_acftLineEdit_inputRejected()
 
 void NewFlightDialog::on_acftLineEdit_editingFinished()
 {
-    auto registrationList = CompletionList(CompleterTarget::registrations).list;
+    auto registrationList = experimental::DB()->getCompletionList(experimental::DataBase::registrations);
     auto line_edit = ui->acftLineEdit;
     auto text = ui->acftLineEdit->text();
 
@@ -1249,7 +1250,7 @@ void NewFlightDialog::on_picNameLineEdit_editingFinished()
         return;
     }else //check if entry is in pilotList
     {
-        QStringList pilotList = CompletionList(CompleterTarget::pilots).list;
+        QStringList pilotList = experimental::DB()->getCompletionList(experimental::DataBase::pilots);
         QStringList match = pilotList.filter(line_edit->text().remove(" "), Qt::CaseInsensitive);
 
         if(match.length()!= 0)
@@ -1294,7 +1295,7 @@ void NewFlightDialog::on_secondPilotNameLineEdit_editingFinished()
         return;
     }else //check if entry is in pilotList
     {
-        QStringList pilotList = CompletionList(CompleterTarget::pilots).list;
+        QStringList pilotList = experimental::DB()->getCompletionList(experimental::DataBase::pilots);
         QStringList match = pilotList.filter(line_edit->text().remove(" "), Qt::CaseInsensitive);
 
         if(match.length()!= 0)
@@ -1328,7 +1329,7 @@ void NewFlightDialog::on_thirdPilotNameLineEdit_editingFinished()
         return;
     }else //check if entry is in pilotList
     {
-        QStringList pilotList = CompletionList(CompleterTarget::pilots).list;
+        QStringList pilotList = experimental::DB()->getCompletionList(experimental::DataBase::pilots);
         QStringList match = pilotList.filter(line_edit->text().remove(" "), Qt::CaseInsensitive);
 
         if(match.length()!= 0)

+ 1 - 1
src/gui/dialogues/newflightdialog.h

@@ -40,8 +40,8 @@
 #include "src/classes/aircraft.h"
 #include "src/classes/strictrxvalidator.h"
 #include "src/classes/settings.h"
-#include "src/classes/completionlist.h"
 #include "src/classes/acalc.h"
+#include "src/experimental/DataBase.h"
 
 #include "src/gui/dialogues/newpilotdialog.h"
 #include "src/gui/dialogues/newtaildialog.h"

+ 1 - 4
src/gui/dialogues/newpilotdialog.cpp

@@ -19,8 +19,6 @@
 #include "ui_newpilot.h"
 #include "debug.h"
 
-#include "src/experimental/DataBase.h"
-#include "src/experimental/Entry.h"
 /* Examples for names around the world:
  * José Eduardo Santos Tavares Melo Silva
  * María José Carreño Quiñones
@@ -110,8 +108,7 @@ void NewPilotDialog::setup()
     }
 
     DEB("Setting up completer...");
-    auto companies = new CompletionList(CompleterTarget::companies);
-    auto completer = new QCompleter(companies->list, ui->companyLineEdit);
+    auto completer = new QCompleter(DB()->getCompletionList(DataBase::companies), ui->companyLineEdit);
     completer->setCompletionMode(QCompleter::InlineCompletion);
     completer->setCaseSensitivity(Qt::CaseSensitive);
     ui->companyLineEdit->setCompleter(completer);

+ 0 - 1
src/gui/dialogues/newpilotdialog.h

@@ -24,7 +24,6 @@
 #include <QRegularExpressionValidator>
 #include <QCompleter>
 #include "src/classes/pilot.h"
-#include "src/classes/completionlist.h"
 
 #include "src/experimental/DataBase.h"
 #include "src/experimental/Entry.h"

+ 2 - 3
src/gui/dialogues/newtaildialog.cpp

@@ -88,11 +88,10 @@ void NewTailDialog::setupCompleter()
     }
     //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;
 
+    auto aircraftlist = experimental::DB()->getCompletionList(experimental::DataBase::aircraft);
+    idMap = map;
     QCompleter *completer = new QCompleter(aircraftlist, ui->searchLineEdit);
     completer->setCaseSensitivity(Qt::CaseInsensitive);
     completer->setCompletionMode(QCompleter::PopupCompletion);

+ 1 - 1
src/gui/dialogues/newtaildialog.h

@@ -24,11 +24,11 @@
 #include <QRegularExpression>
 
 #include "src/classes/settings.h"
-#include "src/classes/completionlist.h"
 #include "src/classes/aircraft.h"
 #include "src/classes/strictrxvalidator.h"
 #include "src/classes/acalc.h"
 #include "src/database/entry.h"
+#include "src/experimental/DataBase.h"
 
 namespace Ui {
 class NewTail;

+ 0 - 1
src/gui/widgets/homewidget.h

@@ -25,7 +25,6 @@
 #include "src/database/db.h"
 #include "src/classes/stat.h"
 #include "src/classes/acalc.h"
-#include "src/classes/completionlist.h"
 #include "src/gui/dialogues/newtaildialog.h"
 #include "src/classes/aircraft.h"
 #include "src/gui/dialogues/newpilotdialog.h"