summaryrefslogtreecommitdiff
path: root/Sidebar.qml
blob: f07f5ac30afa57f344dc7dab147184ea2be9173f (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
257
258
259
260
// SPDX-License-Identifier: Unlicense

import QtQuick 2.12
import QtQuick.Controls 2.13
import QtQuick.Layouts 1.6
import Qt.labs.platform 1.1

import fuzbal 1
import 'util.js' as Util

Page {
    id: control

    required property Video video
    property bool modified: false

    EventList {
        id: eventList
        onDataChanged: modified = true
        onRowsInserted: modified = true
        onRowsRemoved: modified = true
    }

    EventFilter {
        id: eventFilter
        sourceModel: eventList
    }

    FileDialog {
        id: dialog

        title: qsTr('Load video or tags')
        nameFilters: [qsTr('all files (*)'), qsTr('events and tags (*.events *.json)')]

        onAccepted: {
            const path = file.toString()
            if (path.endsWith('.json')) {
                eventList.load({ 'tags': JSON.parse(io.read(file)) })
                modified = true
            } else {
                video.source = path.endsWith('.events') ? path.substr(0, path.length-7) : path
                const json = JSON.parse(io.read(video.source+'.events') || '{}')
                eventList.load(json)
                description.text = json['description'] || ''
                modified = false
            }
        }
    }

    Keys.forwardTo: [video, tags]

    // Save / load buttons.
    header: ToolBar {
        horizontalPadding: 0
        RowLayout {
            anchors.fill: parent
            spacing: 0

            Label {
                text: video.loaded ? video.source : qsTr('(no video)')
                elide: Text.ElideLeft
                leftPadding: 5
                Layout.fillWidth: true
            }

            ToolButton {
                action: Action {
                    icon.name: 'document-save'
                    shortcut: StandardKey.Save
                    enabled: video.loaded && control.modified
                    onTriggered: {
                        var json = eventList.save()
                        json['description'] = description.text
                        json['video'] = video.source
                        json['version'] = Qt.application.version
                        io.write(video.source+'.events', JSON.stringify(json))
                        modified = false
                    }
                }
                visible: video.loaded
                opacity: enabled ? 1 : 0.25
                focusPolicy: Qt.NoFocus
            }

            ToolButton {
                action: Action {
                    icon.name: 'document-open'
                    shortcut: StandardKey.Open
                    onTriggered: dialog.open()
                }
                focusPolicy: Qt.NoFocus
            }
        }
    }

    ColumnLayout {
        anchors.fill: parent

        // Description box.
        Rectangle {
            Layout.fillWidth: true
            Layout.maximumHeight: 100
            Layout.preferredHeight: description.implicitHeight

            border.color: description.activeFocus ? palette.highlight : palette.dark
            color: palette.base
            radius: 2

            ScrollView {
                anchors.fill: parent
                ScrollBar.horizontal.policy: ScrollBar.AlwaysOff
                padding: 0

                TextArea {
                    id: description

                    placeholderText: qsTr('Description')
                    padding: filter.padding
                    leftPadding: filter.leftPadding
                    selectByMouse: true
                    wrapMode: Text.Wrap

                    onTextChanged: modified = true
                    KeyNavigation.priority: KeyNavigation.BeforeItem
                    KeyNavigation.tab: nextItemInFocusChain()
                }
            }

            Hotkey {
                control: description
                sequence: qsTr('Ctrl+D')
                anchors { right: parent.right; bottom: parent.bottom; margins: 4 }
                font.pixelSize: description.font.pixelSize * 0.75
            }
        }

        // Filter box.
        TextField {
            id: filter

            Layout.fillWidth: true
            placeholderText: qsTr('Filter')
            background: Rectangle {
                border.color: parent.activeFocus ? palette.highlight : palette.dark
                color: palette.base
                radius: 2
            }

            onTextChanged: eventFilter.setFilter(text)
            Keys.onEscapePressed: text = ''

            Hotkey {
                control: filter
                sequence: StandardKey.Find
                anchors { right: parent.right; bottom: parent.bottom; margins: 4 }
                font.pixelSize: filter.font.pixelSize * 0.75
            }
        }

        // Event list.
        Frame {
            Layout.fillWidth: true
            Layout.fillHeight: true
            Layout.rightMargin: -control.padding // fill to window edge for easier scrolling

            focusPolicy: Qt.StrongFocus

            padding: 1
            rightPadding: 0
            background: Rectangle {
                border.color: parent.activeFocus ? palette.highlight : palette.dark
                color: 'transparent'
                radius: 2
            }

            Events {
                id: events

                model: eventFilter
                tags: eventList.tags

                anchors.fill: parent
                focus: true

                onEditingChanged: video.pause(editing)
                onSelected: {
                    video.pause(true)
                    video.seek(event.time)
                }
                Keys.forwardTo: control
            }

            Hotkey {
                control: events
                sequence: qsTr('Ctrl+E')
                anchors {
                    right: parent.right
                    top: parent.top
                    margins: 4
                    rightMargin: control.padding + anchors.margins
                }
                font.pixelSize: filter.font.pixelSize * 0.75
            }
        }

        // Tags box.
        Flow {
            id: tags

            Layout.fillWidth: true
            enabled: video.loaded && !events.editing

            // Try passing key to each tag button in order.
            Keys.forwardTo: Array.from({ length: buttons.count }, (_, i) => buttons.itemAt(i))
            Keys.enabled: enabled

            spacing: 5

            Label {
                text: qsTr('(no tags)')
                visible: buttons.count === 0
            }

            Repeater {
                id: buttons
                model: eventList.tagsOrder.map(name => eventList.tags[name])
                delegate: Button {
                    readonly property string name: modelData.name || modelData.tag

                    text: Util.addShortcut(name, modelData.key)
                    focusPolicy: Qt.NoFocus
                    implicitWidth: implicitContentWidth + 2*padding

                    onClicked: {
                        // Create a new event with this tag and current time.
                        const index = eventList.insert(name, video.time)
                        // Reset filter if new event doesn’t match.
                        var row = eventFilter.mapFromSource(eventList.index(index, 0)).row
                        if (row === -1) {
                            filter.text = ''
                            row = index
                        }
                        events.currentIndex = row
                        events.forceActiveFocus()
                        const event = events.currentItem
                        if (event.fields.length > 0)
                            events.editing = true
                    }

                    Keys.onPressed: {
                        if (event.text === modelData.key) {
                            clicked()
                            event.accepted = true
                        }
                    }
                }
            }
        }
    }
}