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
21import org.kde.quickcharts.controls 1.0 as ChartControls
22
23Faces.SensorFace {
24    id: root
25
26    readonly property bool showLegend: controller.faceConfiguration.showLegend
27    readonly property bool showGridLines: root.controller.faceConfiguration.showGridLines
28    readonly property bool showYAxisLabels: root.controller.faceConfiguration.showYAxisLabels
29
30    // Arbitrary minimumWidth to make easier to align plasmoids in a predictable way
31    Layout.minimumWidth: Math.max(Kirigami.Units.gridUnit * compactRepresentation.barCount, Kirigami.Units.gridUnit * 8)
32    Layout.preferredWidth: titleMetrics.width
33
34    contentItem: ColumnLayout {
35        spacing: Kirigami.Units.largeSpacing
36
37        Kirigami.Heading {
38            id: heading
39            Layout.fillWidth: true
40            horizontalAlignment: Text.AlignHCenter
41            elide: Text.ElideRight
42            text: root.controller.title
43            visible: root.controller.showTitle && text.length > 0
44            level: 2
45            TextMetrics {
46                id: titleMetrics
47                font: heading.font
48                text: heading.text
49            }
50        }
51        RowLayout {
52            spacing: Kirigami.Units.smallSpacing
53            Layout.fillHeight: true
54            Layout.topMargin: showYAxisLabels ? axisMetrics.height / 2 : 0
55            Layout.bottomMargin: Layout.topMargin
56            Charts.AxisLabels {
57                id: axisLabels
58                visible: showYAxisLabels
59                Layout.fillHeight: true
60                constrainToBounds: false
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                direction: Charts.AxisLabels.VerticalBottomTop
69                source: Charts.ChartAxisSource {
70                    chart: compactRepresentation
71                    axis: Charts.ChartAxisSource.YAxis
72                    itemCount: 5
73                }
74                TextMetrics {
75                    id: axisMetrics
76                    font: Kirigami.Theme.smallFont
77                    text: Formatter.Formatter.formatValueShowNull("0",
78                        compactRepresentation.sensorsModel.data(compactRepresentation.sensorsModel.index(0, 0), Sensors.SensorDataModel.Unit))
79                }
80            }
81            BarChart {
82                id: compactRepresentation
83                Layout.fillWidth: true
84                Layout.fillHeight: true
85                Layout.minimumHeight: root.formFactor === Faces.SensorFace.Constrained
86                    ? Kirigami.Units.gridUnit
87                    : 5 * Kirigami.Units.gridUnit
88                Layout.preferredHeight: 8 * Kirigami.Units.gridUnit
89
90                updateRateLimit: root.controller.updateRateLimit
91
92                controller: root.controller
93
94                Charts.GridLines {
95                    id: horizontalLines
96                    visible: showGridLines
97                    direction: Charts.GridLines.Vertical
98                    anchors.fill: compactRepresentation
99                    z: compactRepresentation.z - 1
100                    chart: compactRepresentation
101
102                    major.count: 3
103                    major.lineWidth: 1
104                    // The same color as Kirigami.Separator
105                    major.color: Kirigami.ColorUtils.linearInterpolation(Kirigami.Theme.backgroundColor, Kirigami.Theme.textColor, 0.2)
106                    minor.visible: false
107                }
108            }
109        }
110
111        Faces.ExtendedLegend {
112            Layout.fillWidth: true
113            Layout.fillHeight: true
114            Layout.minimumHeight: root.formFactor === Faces.SensorFace.Horizontal
115                || root.formFactor === Faces.SensorFace.Vertical
116                ? implicitHeight
117                : Kirigami.Units.gridUnit
118            visible: root.showLegend
119            chart: compactRepresentation
120            sourceModel: root.showLegend ? compactRepresentation.sensorsModel : null
121            sensorIds: root.showLegend ? root.controller.lowPrioritySensorIds : []
122            updateRateLimit: root.controller.updateRateLimit
123        }
124    }
125}
126