2022-05-13 13:12:05 +00:00
|
|
|
// Copyright (C) 2021 The Qt Company Ltd.
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
2012-03-06 02:03:36 +00:00
|
|
|
|
2021-02-10 11:52:50 +00:00
|
|
|
import QtQuick
|
|
|
|
import QtQuick.Controls
|
|
|
|
import QtQuick.Layouts
|
|
|
|
import QtQuick.LocalStorage
|
2016-10-27 14:11:01 +00:00
|
|
|
import "Database.js" as JS
|
|
|
|
|
2023-02-20 18:36:40 +00:00
|
|
|
pragma ComponentBehavior: Bound
|
|
|
|
|
2016-10-27 14:11:01 +00:00
|
|
|
Window {
|
2019-09-17 16:10:59 +00:00
|
|
|
id: window
|
2023-02-20 18:36:40 +00:00
|
|
|
|
|
|
|
property bool creatingNewEntry: false
|
|
|
|
property bool editingEntry: false
|
|
|
|
|
2016-10-27 14:11:01 +00:00
|
|
|
visible: true
|
|
|
|
width: Screen.width / 2
|
|
|
|
height: Screen.height / 1.8
|
|
|
|
color: "#161616"
|
2012-03-06 02:03:36 +00:00
|
|
|
|
2016-10-27 14:11:01 +00:00
|
|
|
Rectangle {
|
2012-03-06 02:03:36 +00:00
|
|
|
anchors.fill: parent
|
2016-10-27 14:11:01 +00:00
|
|
|
|
|
|
|
ColumnLayout {
|
|
|
|
anchors.fill: parent
|
2021-02-10 13:51:42 +00:00
|
|
|
anchors.margins: 10
|
2016-10-27 14:11:01 +00:00
|
|
|
|
|
|
|
Header {
|
|
|
|
id: input
|
|
|
|
Layout.fillWidth: true
|
2019-09-17 16:10:59 +00:00
|
|
|
listView: listView
|
2021-02-10 13:51:42 +00:00
|
|
|
enabled: window.creatingNewEntry || window.editingEntry
|
2016-10-27 14:11:01 +00:00
|
|
|
}
|
2021-02-10 13:51:42 +00:00
|
|
|
|
2016-10-27 14:11:01 +00:00
|
|
|
RowLayout {
|
2021-02-10 11:52:50 +00:00
|
|
|
Button {
|
2021-02-10 13:51:42 +00:00
|
|
|
text: qsTr("New")
|
2024-01-19 17:06:58 +00:00
|
|
|
Layout.fillWidth: false
|
2016-10-27 14:11:01 +00:00
|
|
|
onClicked: {
|
|
|
|
input.initrec_new()
|
2019-09-17 16:10:59 +00:00
|
|
|
window.creatingNewEntry = true
|
2016-10-27 14:11:01 +00:00
|
|
|
listView.model.setProperty(listView.currentIndex, "id", 0)
|
|
|
|
}
|
|
|
|
}
|
2021-02-10 11:52:50 +00:00
|
|
|
Button {
|
2016-10-27 14:11:01 +00:00
|
|
|
id: saveButton
|
2023-02-20 18:36:40 +00:00
|
|
|
enabled: (window.creatingNewEntry || window.editingEntry) && listView.currentIndex !== -1
|
2021-02-10 13:51:42 +00:00
|
|
|
text: qsTr("Save")
|
2024-01-19 17:06:58 +00:00
|
|
|
Layout.fillWidth: false
|
2016-10-27 14:11:01 +00:00
|
|
|
onClicked: {
|
2023-02-20 18:36:40 +00:00
|
|
|
let insertedRow = false;
|
2016-10-27 14:11:01 +00:00
|
|
|
if (listView.model.get(listView.currentIndex).id < 1) {
|
|
|
|
//insert mode
|
|
|
|
if (input.insertrec()) {
|
|
|
|
// Successfully inserted a row.
|
|
|
|
input.setlistview()
|
|
|
|
insertedRow = true
|
|
|
|
} else {
|
|
|
|
// Failed to insert a row; display an error message.
|
2021-02-10 13:51:42 +00:00
|
|
|
statustext.displayWarning(qsTr("Failed to insert row"))
|
2016-10-27 14:11:01 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// edit mode
|
|
|
|
input.setlistview()
|
|
|
|
JS.dbUpdate(listView.model.get(listView.currentIndex).date,
|
|
|
|
listView.model.get(listView.currentIndex).trip_desc,
|
|
|
|
listView.model.get(listView.currentIndex).distance,
|
|
|
|
listView.model.get(listView.currentIndex).id)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (insertedRow) {
|
|
|
|
input.initrec()
|
2019-09-17 16:10:59 +00:00
|
|
|
window.creatingNewEntry = false
|
|
|
|
window.editingEntry = false
|
2016-10-27 14:11:01 +00:00
|
|
|
listView.forceLayout()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-02-10 11:52:50 +00:00
|
|
|
Button {
|
2016-10-27 14:11:01 +00:00
|
|
|
id: editButton
|
2021-02-10 13:51:42 +00:00
|
|
|
text: qsTr("Edit")
|
2023-02-20 18:36:40 +00:00
|
|
|
enabled: !window.creatingNewEntry && !window.editingEntry && listView.currentIndex !== -1
|
2024-01-19 17:06:58 +00:00
|
|
|
Layout.fillWidth: false
|
2016-10-27 14:11:01 +00:00
|
|
|
onClicked: {
|
|
|
|
input.editrec(listView.model.get(listView.currentIndex).date,
|
|
|
|
listView.model.get(listView.currentIndex).trip_desc,
|
|
|
|
listView.model.get(listView.currentIndex).distance,
|
|
|
|
listView.model.get(listView.currentIndex).id)
|
|
|
|
|
2019-09-17 16:10:59 +00:00
|
|
|
window.editingEntry = true
|
2016-10-27 14:11:01 +00:00
|
|
|
}
|
|
|
|
}
|
2021-02-10 11:52:50 +00:00
|
|
|
Button {
|
2016-10-27 14:11:01 +00:00
|
|
|
id: deleteButton
|
2021-02-10 13:51:42 +00:00
|
|
|
text: qsTr("Delete")
|
2023-02-20 18:36:40 +00:00
|
|
|
enabled: !window.creatingNewEntry && listView.currentIndex !== -1
|
2024-01-19 17:06:58 +00:00
|
|
|
Layout.fillWidth: false
|
2016-10-27 14:11:01 +00:00
|
|
|
onClicked: {
|
|
|
|
JS.dbDeleteRow(listView.model.get(listView.currentIndex).id)
|
|
|
|
listView.model.remove(listView.currentIndex, 1)
|
2023-02-20 18:36:40 +00:00
|
|
|
if (listView.count === 0) {
|
2016-10-27 14:11:01 +00:00
|
|
|
// ListView doesn't automatically set its currentIndex to -1
|
|
|
|
// when the count becomes 0.
|
|
|
|
listView.currentIndex = -1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-02-10 11:52:50 +00:00
|
|
|
Button {
|
2016-10-27 14:11:01 +00:00
|
|
|
id: cancelButton
|
2021-02-10 13:51:42 +00:00
|
|
|
text: qsTr("Cancel")
|
2023-02-20 18:36:40 +00:00
|
|
|
enabled: (window.creatingNewEntry || window.editingEntry) && listView.currentIndex !== -1
|
2024-01-19 17:06:58 +00:00
|
|
|
Layout.fillWidth: false
|
2016-10-27 14:11:01 +00:00
|
|
|
onClicked: {
|
|
|
|
if (listView.model.get(listView.currentIndex).id === 0) {
|
|
|
|
// This entry had an id of 0, which means it was being created and hadn't
|
|
|
|
// been saved to the database yet, so we can safely remove it from the model.
|
|
|
|
listView.model.remove(listView.currentIndex, 1)
|
|
|
|
}
|
|
|
|
listView.forceLayout()
|
2019-09-17 16:10:59 +00:00
|
|
|
window.creatingNewEntry = false
|
|
|
|
window.editingEntry = false
|
2016-10-27 14:11:01 +00:00
|
|
|
input.initrec()
|
|
|
|
}
|
|
|
|
}
|
2021-02-10 11:52:50 +00:00
|
|
|
Button {
|
2021-02-10 13:51:42 +00:00
|
|
|
text: qsTr("Exit")
|
2024-01-19 17:06:58 +00:00
|
|
|
Layout.fillWidth: false
|
2016-10-27 14:11:01 +00:00
|
|
|
onClicked: Qt.quit()
|
|
|
|
}
|
|
|
|
}
|
2021-02-10 13:51:42 +00:00
|
|
|
Item {
|
|
|
|
Layout.fillWidth: true
|
2023-02-20 18:36:40 +00:00
|
|
|
Layout.preferredHeight: 5
|
2021-02-10 13:51:42 +00:00
|
|
|
}
|
|
|
|
Label {
|
|
|
|
Layout.alignment: Qt.AlignCenter
|
|
|
|
text: qsTr("Saved activities")
|
|
|
|
font.pointSize: 15
|
|
|
|
}
|
2016-10-27 14:11:01 +00:00
|
|
|
Component {
|
|
|
|
id: highlightBar
|
|
|
|
Rectangle {
|
2023-02-20 18:36:40 +00:00
|
|
|
width: listView.currentItem?.width ?? implicitWidth
|
|
|
|
height: listView.currentItem?.height ?? implicitHeight
|
2016-10-27 14:11:01 +00:00
|
|
|
color: "lightgreen"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ListView {
|
|
|
|
id: listView
|
|
|
|
Layout.fillWidth: true
|
|
|
|
Layout.fillHeight: true
|
|
|
|
model: MyModel {}
|
2019-09-17 16:10:59 +00:00
|
|
|
delegate: MyDelegate {
|
2021-02-10 13:51:42 +00:00
|
|
|
width: listView.width
|
|
|
|
onClicked: ()=> listView.currentIndex = index
|
2019-09-17 16:10:59 +00:00
|
|
|
}
|
2016-10-27 14:11:01 +00:00
|
|
|
// Don't allow changing the currentIndex while the user is creating/editing values.
|
2019-09-17 16:10:59 +00:00
|
|
|
enabled: !window.creatingNewEntry && !window.editingEntry
|
2016-10-27 14:11:01 +00:00
|
|
|
|
|
|
|
highlight: highlightBar
|
|
|
|
highlightFollowsCurrentItem: true
|
|
|
|
focus: true
|
2021-02-10 13:51:42 +00:00
|
|
|
clip: true
|
2016-10-27 14:11:01 +00:00
|
|
|
|
|
|
|
header: Component {
|
2021-02-10 13:51:42 +00:00
|
|
|
RowLayout {
|
|
|
|
width: ListView.view.width
|
|
|
|
Repeater {
|
2023-02-20 18:36:40 +00:00
|
|
|
model: [qsTr("Date"), qsTr("Description"), qsTr("Distance")]
|
2021-02-10 13:51:42 +00:00
|
|
|
delegate: Label {
|
|
|
|
id: headerTitleDelegate
|
2023-02-20 18:36:40 +00:00
|
|
|
|
|
|
|
required property string modelData
|
|
|
|
|
2021-02-10 13:51:42 +00:00
|
|
|
Layout.fillWidth: true
|
|
|
|
Layout.fillHeight: true
|
|
|
|
Layout.preferredWidth: 1
|
|
|
|
text: modelData
|
2023-02-20 18:36:40 +00:00
|
|
|
font {
|
|
|
|
pointSize: 15
|
|
|
|
bold: true
|
|
|
|
underline: true
|
|
|
|
}
|
2021-02-10 13:51:42 +00:00
|
|
|
padding: 12
|
|
|
|
horizontalAlignment: Label.AlignHCenter
|
|
|
|
}
|
|
|
|
}
|
2016-10-27 14:11:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-02-10 11:52:50 +00:00
|
|
|
Label {
|
2016-10-27 14:11:01 +00:00
|
|
|
id: statustext
|
|
|
|
color: "red"
|
|
|
|
font.bold: true
|
|
|
|
font.pointSize: 20
|
2021-02-10 13:51:42 +00:00
|
|
|
opacity: 0.0
|
2023-02-20 18:36:40 +00:00
|
|
|
visible: opacity > 0 // properly cull item if effectively invisible
|
2021-02-10 13:51:42 +00:00
|
|
|
Layout.alignment: Layout.Center
|
|
|
|
|
|
|
|
function displayWarning(text) {
|
|
|
|
statustext.text = text
|
|
|
|
statusAnim.restart()
|
|
|
|
}
|
2016-10-27 14:11:01 +00:00
|
|
|
|
2021-02-10 13:51:42 +00:00
|
|
|
Connections {
|
|
|
|
target: input
|
|
|
|
function onStatusMessage(msg) { statustext.displayWarning(msg); }
|
|
|
|
}
|
|
|
|
|
|
|
|
SequentialAnimation {
|
|
|
|
id: statusAnim
|
|
|
|
|
|
|
|
OpacityAnimator {
|
|
|
|
target: statustext
|
|
|
|
from: 0.0
|
|
|
|
to: 1.0
|
|
|
|
duration: 50
|
|
|
|
}
|
|
|
|
|
|
|
|
PauseAnimation {
|
|
|
|
duration: 2000
|
|
|
|
}
|
|
|
|
|
|
|
|
OpacityAnimator {
|
|
|
|
target: statustext
|
|
|
|
from: 1.0
|
|
|
|
to: 0.0
|
|
|
|
duration: 50
|
|
|
|
}
|
|
|
|
}
|
2016-10-27 14:11:01 +00:00
|
|
|
}
|
2012-03-06 02:03:36 +00:00
|
|
|
}
|
|
|
|
}
|
2016-10-27 14:11:01 +00:00
|
|
|
Component.onCompleted: {
|
|
|
|
JS.dbInit()
|
|
|
|
}
|
2012-03-20 07:57:07 +00:00
|
|
|
}
|