1/*
2    SPDX-FileCopyrightText: 2019 Marco Martin <mart@kde.org>
3    SPDX-FileCopyrightText: 2019 David Edmundson <davidedmundson@kde.org>
4    SPDX-FileCopyrightText: 2019 Arjen Hiemstra <ahiemstra@heimr.nl>
5    SPDX-FileCopyrightText: 2020 David Redondo <kde@david-redondo.de>
6
7    SPDX-License-Identifier: LGPL-2.0-or-later
8*/
9
10import QtQuick 2.9
11import QtQuick.Controls 2.9 as QQC2
12import QtQuick.Layouts 1.1
13
14import org.kde.kirigami 2.12 as Kirigami
15
16import org.kde.ksysguard.sensors 1.0 as Sensors
17import org.kde.ksysguard.faces 1.0 as Faces
18import org.kde.ksysguard.formatter 1.0 as Formatter
19
20import org.kde.quickcharts 1.0 as Charts
21
22Faces.SensorFace {
23    id: root
24    readonly property bool showLegend: controller.faceConfiguration.showLegend
25    readonly property bool showGridLines: root.controller.faceConfiguration.showGridLines
26    readonly property bool showYAxisLabels: root.controller.faceConfiguration.showYAxisLabels
27    // Arbitrary minimumWidth to make easier to align plasmoids in a predictable way
28    Layout.minimumWidth: Kirigami.Units.gridUnit * 8
29    Layout.preferredWidth: titleMetrics.width
30
31    contentItem: ColumnLayout {
32        spacing: Kirigami.Units.largeSpacing
33
34        Kirigami.Heading {
35            id: heading
36            Layout.fillWidth: true
37            horizontalAlignment: Text.AlignHCenter
38            elide: Text.ElideRight
39            text: root.controller.title
40            visible: root.controller.showTitle && text.length > 0
41            level: 2
42            TextMetrics {
43                id: titleMetrics
44                font: heading.font
45                text: heading.text
46            }
47        }
48
49        RowLayout {
50            spacing: Kirigami.Units.smallSpacing
51            Layout.fillHeight: true
52            Layout.topMargin: showYAxisLabels ? axisMetrics.height / 2 : 0
53            Layout.bottomMargin: Layout.topMargin
54            Layout.minimumHeight: compactRepresentation.Layout.minimumHeight
55            Charts.AxisLabels {
56                id: axisLabels
57                visible: showYAxisLabels
58                Layout.fillHeight: true
59                constrainToBounds: false
60                direction: Charts.AxisLabels.VerticalBottomTop
61                delegate: QQC2.Label {
62                    anchors.right: parent.right
63                    font: Kirigami.Theme.smallFont
64                    text: Formatter.Formatter.formatValueShowNull(Charts.AxisLabels.label,
65                        compactRepresentation.sensorsModel.unit)
66                    color: Kirigami.Theme.disabledTextColor
67                }
68                source: Charts.ChartAxisSource {
69                    chart: compactRepresentation
70                    axis: Charts.ChartAxisSource.YAxis
71                    itemCount: 5
72                }
73                TextMetrics {
74                    id: axisMetrics
75                    font: Kirigami.Theme.smallFont
76                    text: Formatter.Formatter.formatValueShowNull("0",
77                        compactRepresentation.sensorsModel.unit)
78                }
79            }
80            LineChart {
81                id: compactRepresentation
82                Layout.fillWidth: true
83                Layout.fillHeight: true
84                Layout.minimumHeight: root.formFactor === Faces.SensorFace.Constrained
85                    ? Kirigami.Units.gridUnit
86                    : 3 * Kirigami.Units.gridUnit
87                Layout.preferredHeight: 5 * Kirigami.Units.gridUnit
88
89                controller: root.controller
90
91                Charts.GridLines {
92                    id: horizontalLines
93                    visible: showGridLines
94                    direction: Charts.GridLines.Vertical
95                    anchors.fill: compactRepresentation
96                    z: compactRepresentation.z - 1
97                    chart: compactRepresentation
98
99                    major.count: 3
100                    major.lineWidth: 1
101                    // The same color as a Kirigami.Separator
102                    major.color: Kirigami.ColorUtils.linearInterpolation(Kirigami.Theme.backgroundColor, Kirigami.Theme.textColor, 0.2)
103                    minor.visible: false
104
105                }
106            }
107        }
108
109        Faces.ExtendedLegend {
110            Layout.fillWidth: true
111            Layout.fillHeight: true
112            Layout.minimumHeight: root.formFactor === Faces.SensorFace.Horizontal
113                || root.formFactor === Faces.SensorFace.Vertical
114                ? implicitHeight
115                : Kirigami.Units.gridUnit
116            visible: root.showLegend
117            chart: compactRepresentation
118            sourceModel: root.showLegend ? compactRepresentation.sensorsModel : null
119            sensorIds: root.showLegend ? root.controller.lowPrioritySensorIds : []
120            updateRateLimit: root.controller.updateRateLimit
121        }
122    }
123}
124