summaryrefslogtreecommitdiffstats
path: root/Log.qml
blob: 3a174969d05978514456d1a4f553563c09457187 (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
// Copyright (C) 2023 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 Qt.labs.qmlmodels
import QtCore

GroupBox {
    implicitWidth: parent.width
    implicitHeight: 200
    title: "Log"

    Connections {
        target: window
        function onGeneratingChanged() {
            if (window.generating) {
                outputModel.clear()
                filteredModel.clear()
            }
        }
    }

    ListModel {
        id: outputModel
    }

    ListModel {
        id: filteredModel
    }

    Connections {
        target: bridge

        function onDebug(msg) {
            output(msg)
        }

        function onWarning(msg) {
            outputFilter.text = "(warning|error)"
            updateOutputFilter();
            output("Warning: " + msg)
        }

        function onError(msg) {
            outputFilter.text = "(warning|error)"
            updateOutputFilter();
            output("Error: " + msg)
        }

        function onProgressLabelChanged(label) {
            output(label)
        }

        function onFinished() {
            output(window.stopping ? "Stopped!" : "Finished!")
        }

        function onFigmaFileNameChanged(name) {
            output("Figma name: " + name)
        }
    }

    function output(msg)
    {
        outputModel.append({"msg": msg})
        const regex = new RegExp(outputFilter.text, "i");
        if (regex.test(msg))
            filteredModel.append({"msg" : msg})
    }

    function updateOutputFilter() {
        filteredModel.clear()
        const regex = new RegExp(outputFilter.text, "i");
        let rows = outputModel.rowCount()
        for (let row = 0; row < rows; ++row) {
            let index = outputModel.index(row, 0)
            let msg = outputModel.data(index)
            if (regex.test(msg))
                filteredModel.append({"msg" : msg})
        }
    }

    ColumnLayout {
        anchors.fill: parent
        ScrollView {
            id: outputScrollView
            Layout.preferredWidth: parent.width
            Layout.fillHeight: true

            property bool sticky: true

            TableView {
                id: outputView
                clip: true
                animate: false
                model: filteredModel

                delegate: Label {
                    text: msg
                    onImplicitWidthChanged: {
                        if (implicitWidth > outputView.contentWidth)
                            outputView.contentWidth = implicitWidth
                    }
                }

                onRowsChanged: {
                    if (outputScrollView.sticky)
                        positionViewAtRow(rows - 1, TableView.AlignBottom)
                }
            }

            Connections {
                target: outputScrollView.ScrollBar.vertical
                function onPressedChanged() {
                    outputScrollView.sticky = target.pressed ? false : target.position > 0.9
                }
            }
        }

        TextField {
            id: outputFilter
            placeholderText: "Filter (regexp)"
            Layout.preferredWidth: parent.width
            onAccepted: updateOutputFilter()
        }
    }
}