aboutsummaryrefslogtreecommitdiffstats
path: root/examples/quickcontrols2/ios/todolist/ProjectPage.qml
blob: fd33e73980e30f2a0d207916d91798da389048be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
import QtQuick.Controls.iOS

Page {
    id: root

    required property int projectIndex
    required property int projectId
    required property string projectName
    required property string projectNote

    required property int completedTasks
    required property int totalTasks

    required property ListModel projectsModel

    header: ColumnLayout {
        id: titleRow
        spacing: 4

        TextField {
            id: projectNameLabel
            Layout.fillWidth: true
            text: root.projectName
            font.pointSize: AppSettings.fontSize + 10
            font.styleName: "Bold"
            padding: 0
            background: null
            wrapMode: TextField.Wrap

            Layout.topMargin: 10
            Layout.leftMargin: 20
            Layout.rightMargin: 20

            onEditingFinished: {
                Database.updateProjectName(root.projectId, text)
                projectsModel.setProperty(projectIndex, "projectName", projectNameLabel.text)
            }
        }

        Label {
            text: root.completedTasks + " / " + root.totalTasks
            Layout.leftMargin: 20
        }

        ProgressBar {
            id: progressBar
            from: 0
            value: root.completedTasks
            to: root.totalTasks
            Layout.leftMargin: 20
        }
    }

    ColumnLayout {
        anchors.fill: parent

        TextArea {
            id: textArea
            leftPadding: 15
            rightPadding: doneButton.width
            bottomPadding: 10
            topPadding: 10
            font.pointSize: AppSettings.fontSize
            placeholderText: qsTr("Write a note...")
            text: root.projectNote
            wrapMode: TextArea.Wrap
            clip: true

            Layout.fillWidth: true
            Layout.preferredHeight: 80
            Layout.topMargin: 20

            onEditingFinished: {
                Database.updateProjectNote(root.projectId, text)
                projectsModel.setProperty(projectIndex, "projectNote", textArea.text)
            }

            Button {
                id: doneButton
                text: qsTr("Done")
                flat: true
                visible: textArea.focus
                onClicked: textArea.editingFinished()
                anchors.right: parent.right
                anchors.bottom: parent.bottom
            }
        }

        ListView {
            id: taskListView
            model: taskModel
            clip: true

            Layout.fillWidth: true
            Layout.fillHeight: true
            Layout.topMargin: 20

            ListModel {
                id: taskModel

                Component.onCompleted: {
                    let tasks = Database.getTaskByProject(projectId)
                    for (let task of tasks)
                        append(task)
                }
            }

            delegate: CheckDelegate {
                id: taskList
                width: taskListView.width
                height: visible ? 40 : 0
                checked: done
                visible: !done || (done && AppSettings.showDoneTasks)

                required property bool done
                required property int taskId
                required property string taskName
                required property int index

                onClicked: {
                    Database.updateDoneState(taskId, checked ? 1 : 0)
                    taskModel.setProperty(index, "done", checked)
                    root.completedTasks = Math.max(Database.countDoneTasksByProject(root.projectId).rows.length, 0)
                    root.totalTasks = Math.max(Database.countTaskByProject(root.projectId).rows.length, 0)
                    root.projectsModel.setProperty(projectIndex, "completedTasks", root.completedTasks)
                }

                TextField {
                    id: taskNameLabel
                    text: taskList.taskName
                    anchors.left: deleteTaskButton.right
                    anchors.leftMargin: 10
                    padding: 0
                    font.pointSize: AppSettings.fontSize
                    anchors.verticalCenter: parent.verticalCenter
                    background: null

                    onEditingFinished: {
                        Database.updateTaskName(taskList.taskId, taskNameLabel.text)
                        taskModel.setProperty(index, "taskName", taskNameLabel.text)
                    }
                }

                Button {
                    id: deleteTaskButton
                    anchors.left: parent.left
                    width: 15
                    height: 15
                    flat: true
                    topPadding: 0
                    bottomPadding: 0
                    rightPadding: 0
                    leftPadding: 0
                    anchors.leftMargin: 10
                    anchors.verticalCenter: parent.verticalCenter
                    icon.source: "images/close.png"
                    icon.color: IOS.theme === IOS.Dark ? "white" : "black"

                    onClicked: {
                        Database.deleteTask(taskList.taskId)
                        taskModel.remove(index, 1)
                        root.totalTasks--
                        if (taskList.done)
                            root.completedTasks--
                    }
                }
            }
        }

        Popup {
            id: addTaskPopup
            parent: root
            anchors.centerIn: parent
            height: 200
            modal: true
            focus: true

            ColumnLayout {
                anchors.fill: parent

                Label {
                    text: qsTr("Add New Task")
                    font.pointSize: AppSettings.fontSize + 2.0
                    font.bold: true

                    Layout.alignment: Qt.AlignHCenter
                }

                TextField {
                    id: newTaskNameTextField
                    placeholderText: qsTr("Enter task name")
                    font.pointSize: AppSettings.fontSize

                    Keys.onReturnPressed: {
                        if (addTaskButton.enabled)
                            addTaskButton.clicked()
                    }

                    Layout.fillWidth: true
                    Layout.leftMargin: 10
                    Layout.rightMargin: 10
                    Layout.alignment: Qt.AlignHCenter
                }

                Button {
                    id: addTaskButton
                    text: qsTr("Add task")
                    flat: true
                    font.pointSize: AppSettings.fontSize
                    enabled: newTaskNameTextField.length > 0

                    Layout.alignment: Qt.AlignHCenter

                    onClicked: {
                        const result = Database.newTask(root.projectId, newTaskNameTextField.text)
                        taskModel.append({
                            "taskId": parseInt(result.insertId),
                            "taskName": newTaskNameTextField.text, "done": false
                        })
                        root.projectsModel.setProperty(projectIndex, "totalTasks", taskListView.count)
                        newTaskNameTextField.text = ""
                        root.totalTasks++
                        addTaskPopup.close()
                    }
                }
            }
        }

        Label {
            text: qsTr("Task limit reached.")
            Layout.alignment: Qt.AlignHCenter
            Layout.bottomMargin: 20
            font.pointSize: AppSettings.fontSize
            visible: taskListView.count >= AppSettings.maxTasks
        }
    }

    footer: ToolBar {
        ToolButton {
            anchors.right: parent.right
            anchors.rightMargin: 5
            text: qsTr("New task")
            font.pointSize: AppSettings.fontSize - 2
            icon.source: "images/add-new.png"
            enabled: taskListView.count < AppSettings.maxTasks

            onClicked: addTaskPopup.open()
        }
    }
}