fuzbal/Event.qml
Timotej Lazar cb76fedcbc
Implement event model in C++
Filtering events in JS is too slow with >20,000 events. This moves the
event data model into C++.
2021-09-16 20:33:05 +02:00

123 lines
3.6 KiB
QML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// SPDX-License-Identifier: Unlicense
import QtQuick 2.12
import QtQuick.Controls 2.13
import QtQuick.Layouts 1.6
import 'util.js' as Util
// This is the delegate for event list items.
ItemDelegate {
required property var model
required property int index
required property int time
property alias fields: inputs.model // field definitions
property bool editing: false
clip: true
padding: 2
background: Rectangle {
anchors.fill: parent
color: highlighted ? Util.alphize(border.color, 0.1) :
(index % 2 === 0 ? palette.base : palette.alternateBase)
border {
color: editing ? palette.highlight : palette.dark
width: highlighted ? 1 : 0
}
radius: border.width
}
// Set inputs to current model values.
function reset() {
for (var i = 0; i < fields.length; i++) {
const child = inputs.itemAt(i)
if (child && child.item)
child.item.set(model.values[fields[i].name])
}
}
// Store current inputs in model.
function store() {
var values = {}
for (var i = 0; i < fields.length; i++)
values[fields[i].name] = inputs.itemAt(i).item.value
model.values = values
}
Component.onCompleted: reset()
onEditingChanged: {
if (editing)
forceActiveFocus()
}
// Try passing key to each field input in order.
Keys.forwardTo: Array.from({ length: inputs.count }, (_, i) => inputs.itemAt(i).item)
contentItem: ColumnLayout {
anchors { left: parent.left; right: parent.right; margins: 5 }
// Event time, tag and summary.
RowLayout {
Label {
text: new Date(model.time).toISOString().substr(12, 9)
font.pixelSize: 10
Layout.alignment: Qt.AlignBaseline
}
Label {
text: model.tag
font.weight: Font.DemiBold
Layout.alignment: Qt.AlignBaseline
}
Label {
text: {
var str = ''
for (var i = 0; i < inputs.count; i++) {
const field = inputs.model[i]
const value = inputs.itemAt(i).item.value
if (value && field.type !== 'TextArea')
str += (field.type === 'Bool' ? field.name : value) + ' '
}
return str
}
elide: Text.ElideRight
textFormat: Text.PlainText
Layout.fillWidth: true
Layout.alignment: Qt.AlignBaseline
}
}
// Eventspecific input fields.
GridLayout {
flow: GridLayout.TopToBottom
rows: inputs.count
columnSpacing: 10
visible: editing
// Labels.
Repeater {
model: inputs.model
delegate: Label {
text: Util.addShortcut(modelData.name, modelData.key)
Layout.alignment: Qt.AlignRight
}
}
// Inputs.
Repeater {
id: inputs
delegate: Loader {
source: 'qrc:/Fields/' + modelData.type + '.qml'
Layout.fillHeight: true
Layout.fillWidth: true
Binding {
target: item; property: 'model'
value: modelData
}
}
}
}
}
}