diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 91d8ab0..7590c82 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,5 +1,5 @@ set(vehiclevoyage_SRCS - db/sqlcar.cpp + db/sqlvehicle.cpp db/sqlservicerecord.cpp main.cpp ) diff --git a/src/db/sqlcar.cpp b/src/db/sqlcar.cpp deleted file mode 100644 index e502ea6..0000000 --- a/src/db/sqlcar.cpp +++ /dev/null @@ -1,18 +0,0 @@ -/* - * Copyright (C) 2020 - * - * 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 . - */ - -#include "sqlcar.h" diff --git a/src/db/sqlvehicle.cpp b/src/db/sqlvehicle.cpp new file mode 100644 index 0000000..212501c --- /dev/null +++ b/src/db/sqlvehicle.cpp @@ -0,0 +1,56 @@ +/* + * Copyright (C) 2020 + * + * 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 . + */ + +#include "sqlvehicle.h" +#include + +SqlVehicle::SqlVehicle(QObject* parent) +{ + setTable("vehicles"); +} + + +QHash SqlVehicle::roleNames() const +{ + QHash roles; + // record() returns an empty QSqlRecord + for (int i = 0; i < this->record().count(); i ++) { + roles.insert(Qt::UserRole + i + 1, record().fieldName(i).toUtf8()); + } + return roles; +} + + +QVariant SqlVehicle::data(const QModelIndex& index, int role) const +{ + QVariant value = QSqlQueryModel::data(index, role); + if(role < Qt::UserRole) + { + value = QSqlQueryModel::data(index, role); + } + else + { + int columnIdx = role - Qt::UserRole - 1; + QModelIndex modelIndex = this->index(index.row(), columnIdx); + value = QSqlQueryModel::data(modelIndex, Qt::DisplayRole); + } + return value; +} + + + + diff --git a/src/db/sqlcar.h b/src/db/sqlvehicle.h similarity index 68% rename from src/db/sqlcar.h rename to src/db/sqlvehicle.h index 2bb39fb..15a4432 100644 --- a/src/db/sqlcar.h +++ b/src/db/sqlvehicle.h @@ -15,18 +15,24 @@ * along with this program. If not, see . */ -#ifndef SQLCAR_H -#define SQLCAR_H +#ifndef SQLVEHICLE_H +#define SQLVEHICLE_H -#include +#include /** - * @todo write docs + * Model to access list of vehicles */ -class SqlCar : public QSqlQueryModel +class SqlVehicle : public QSqlTableModel { Q_OBJECT +public: + explicit SqlVehicle(QObject *parent); + QHash roleNames() const override; + QVariant data(const QModelIndex &index, int role) const; + +private: }; -#endif // SQLCAR_H +#endif // SQLVEHICLE_H diff --git a/src/main.cpp b/src/main.cpp index 882d288..5b1de7a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -25,6 +25,7 @@ #include #include #include +#include Q_DECL_EXPORT int main(int argc, char *argv[]) { @@ -51,6 +52,12 @@ Q_DECL_EXPORT int main(int argc, char *argv[]) if(!QFile(db.databaseName()).exists()) { QFile(db.databaseName()).open(QIODevice::ReadWrite); + //Create table structure + db.open(); + db.exec("CREATE TABLE vehicles (id INTEGER PRIMARY KEY, name TEXT, maker TEXT, model TEXT, year INTEGER, image TEXT);"); + db.exec("CREATE TABLE records (id INTEGER PRIMARY KEY, servicetype TEXT, date INTEGER, miles INTEGER, notes TEXT);"); + db.commit(); + db.close(); } if(!db.open()) {