diff --git a/src/contents/ui/main.qml b/src/contents/ui/main.qml index 827984e404d1cc3684bc1eb0b363f672807534d5..1bcdc621b643aa9443951fb2fce08ad0c7ee4aca 100644 --- a/src/contents/ui/main.qml +++ b/src/contents/ui/main.qml @@ -71,7 +71,7 @@ Kirigami.ApplicationWindow { text: qsTr("Examine Logs") iconName: "edit-find" onTriggered: { - // + router.navigateToRoute(["main", {"route": "servicerecords", "data": {"id": id, "name": name}}]); } } ] @@ -129,7 +129,7 @@ Kirigami.ApplicationWindow { Controls.TextField { id: nameField selectByMouse: true - Kirigami.FormData.label: qsTr("Name")+":" + Kirigami.FormData.label: qsTr("Vehicle Name")+":" } Controls.TextField { @@ -161,12 +161,143 @@ Kirigami.ApplicationWindow { Kirigami.FormData.label: qsTr("VIN")+":" } - Controls.Button { - text: qsTr("Add") - highlighted: true - onClicked: { - vehicleModel.addNewVehicle(nameField.text, makeField.text, modelField.text, parseInt(yearField.text), "", vinField.text); - router.popRoute(); + Layouts.RowLayout { + + Controls.Button { + text: qsTr("Add") + highlighted: true + onClicked: { + vehicleModel.addNewVehicle(nameField.text, makeField.text, modelField.text, parseInt(yearField.text), "", vinField.text); + router.popRoute(); + } + } + + Controls.Button { + text: qsTr("Cancel") + onClicked: { + router.popRoute(); + } + } + } + } + } + } + } + } + + Kirigami.PageRoute { + name: "servicerecords" + Component { + Kirigami.ScrollablePage { + property var serviceId: Kirigami.PageRouter.data.id + Component.onCompleted: { + recordModel.changeVehicleFilter(serviceId); + } + id: serviceRecordPage + title: qsTr("Service Records for ") + Kirigami.PageRouter.data.name + mainAction: Kirigami.Action { + text: qsTr("Add Service Record") + iconName: "list-add" + onTriggered: { + router.pushRoute({"route": "addrecord", "data": serviceId}); + } + } + ListView { + id: serviceView + model: recordModel + delegate: Kirigami.SwipeListItem { + contentItem: Layouts.ColumnLayout { + Controls.Label { + text: serviceprovider + } + Controls.Label { + text: servicetype + } + Controls.Label { + text: servicedate + } + Controls.Label { + text: miles + } + Controls.Label { + text: notes + } + } + } + } + } + } + } + + Kirigami.PageRoute { + name: "addrecord" + Component { + Kirigami.Page { + id: recordAddPage + title: qsTr("Add Service Record") + Layouts.ColumnLayout { + anchors.fill: parent + Kirigami.FormLayout { + Layouts.Layout.alignment: Qt.AlignHCenter + Layouts.Layout.fillWidth: true + width: recordAddPage.width + + Item { + Kirigami.FormData.isSection: true + Kirigami.FormData.label: "Service Information" + } + + Controls.TextField { + id: providerField + selectByMouse: true + Kirigami.FormData.label: qsTr("Service Provider")+":" + } + + Controls.TextField { + id: typeField + selectByMouse: true + Kirigami.FormData.label: qsTr("Service Type")+":" + } + + Controls.TextField { + id: dateField + selectByMouse: true + Kirigami.FormData.label: qsTr("Date")+":" + inputMethodHints: Qt.ImhDate //TODO: implement better input + } + + Controls.TextField { + id: milesField + selectByMouse: true + Kirigami.FormData.label: qsTr("Miles")+":" + inputMethodHints: Qt.ImhDigitsOnly + validator: IntValidator { + bottom: 0 + top: 400000 + } + } + + Controls.TextField { + id: notesField + selectByMouse: true + Kirigami.FormData.label: qsTr("Notes")+":" + } + + Layouts.RowLayout { + + Controls.Button { + text: qsTr("Add") + highlighted: true + onClicked: { + recordModel.addNewRecord(providerField.text, typeField.text, -1, parseInt(milesField.text), notesField.text); //TODO: add proper date + router.popRoute(); + } + } + Controls.Button { + text: qsTr("Cancel") + onClicked: { + router.popRoute(); + } } } } diff --git a/src/db/sqlservicerecord.cpp b/src/db/sqlservicerecord.cpp index 9a9473d147161006ac556896ee9c3f544882aaae..938f1de1a577a7ee84ab69ea121f53ef8b0c5696 100644 --- a/src/db/sqlservicerecord.cpp +++ b/src/db/sqlservicerecord.cpp @@ -51,4 +51,47 @@ QVariant SqlServiceRecord::data ( const QModelIndex& index, int role ) const return value; } +void SqlServiceRecord::addNewRecord(QString serviceProvider, QString serviceType, qlonglong dateEpoch, int miles, QString notes) +{ + QSqlRecord newItem = this->record(); + newItem.setValue("vehicle", this->currentVehicleID); + newItem.setValue("serviceprovider", serviceProvider); + newItem.setValue("servicetype", serviceType); + newItem.setValue("servicedate", dateEpoch); + newItem.setValue("miles", miles); + newItem.setValue("notes", notes); + this->insertRecord(-1, newItem); + if(this->submitAll()) { + printf("inserted new service record"); + this->database().commit(); + this->select(); + } else { + this->database().rollback(); + printf("database error"); + } +} + +void SqlServiceRecord::removeRecord(int index) +{ + this->removeRow(index); + if(this->submitAll()) { + printf("removed service record"); + this->database().commit(); + this->select(); + } else { + this->database().rollback(); + printf("database error"); + } +} + +void SqlServiceRecord::changeVehicleFilter(int id) +{ + this->setFilter("vehicle='"+QString::number(id)+"'"); + this->select(); + this->currentVehicleID = id; +} + + + + diff --git a/src/db/sqlservicerecord.h b/src/db/sqlservicerecord.h index 5cf1c73b91c86cba0fbc3c208461d891bfd6ffbf..ba418c3ddf46e248ad05c150f6795fa01a97a3e6 100644 --- a/src/db/sqlservicerecord.h +++ b/src/db/sqlservicerecord.h @@ -30,6 +30,12 @@ public: explicit SqlServiceRecord(QObject *parent = nullptr, QSqlDatabase db = QSqlDatabase()); QHash roleNames() const override; Q_INVOKABLE QVariant data(const QModelIndex &index, int role) const override; + Q_INVOKABLE void addNewRecord(QString serviceProvider, QString serviceType, qlonglong dateEpoch, int miles, QString notes); + Q_INVOKABLE void removeRecord(int index); + Q_INVOKABLE void changeVehicleFilter(int id); + +private: + int currentVehicleID = 0; }; diff --git a/src/db/sqlvehicle.cpp b/src/db/sqlvehicle.cpp index 0da4526ec11973b6b93491a70f5e13468ae37eaf..63d894959a21564cd233104168e146b85f5d0598 100644 --- a/src/db/sqlvehicle.cpp +++ b/src/db/sqlvehicle.cpp @@ -70,14 +70,14 @@ void SqlVehicle::addNewVehicle ( const QString name, const QString maker, const this->database().rollback(); printf("database error"); } - } void SqlVehicle::removeVehicle(int index) { + //TODO: remove any service records related this->removeRow(index); if(this->submitAll()) { - printf("inserted new vehicle record"); + printf("removed vehicle record"); this->database().commit(); this->select(); } else { diff --git a/src/main.cpp b/src/main.cpp index 05ec9d1ad002eb962ce2c07a803cd89b91c34181..b0801b6d61079be5284f2c2b70cf89e11aedc1f7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -59,7 +59,7 @@ Q_DECL_EXPORT int main(int argc, char *argv[]) //Create table structure db.open(); db.exec("CREATE TABLE vehicles (id INTEGER PRIMARY KEY, name TEXT, maker TEXT, vmodel TEXT, year INTEGER, image TEXT, vin TEXT);"); - db.exec("CREATE TABLE records (id INTEGER PRIMARY KEY, servicetype TEXT, date INTEGER, miles INTEGER, notes TEXT);"); + db.exec("CREATE TABLE records (id INTEGER PRIMARY KEY, vehicle INTEGER, serviceprovider TEXT, servicetype TEXT, servicedate INTEGER, miles INTEGER, notes TEXT);"); db.commit(); db.close(); }