Serial-Studio/assets/qml/Widgets/GaugeDelegate.qml

168 lines
5.5 KiB
QML
Raw Normal View History

/*
* Copyright (c) 2020-2021 Alex Spataru <https://github.com/alex-spataru>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
import QtQuick 2.12
2021-09-03 13:51:43 -05:00
import QtQuick.Extras 1.4
import QtQuick.Layouts 1.12
2020-12-25 22:25:31 -06:00
import QtQuick.Controls 2.12
2021-09-03 13:51:43 -05:00
import QtQuick.Controls.Styles 1.4
2021-09-03 13:51:43 -05:00
import SerialStudio 1.0
import "."
Window {
id: root
2020-12-25 22:25:31 -06:00
//
// Custom properties
//
2021-09-03 13:51:43 -05:00
property string units: ""
property int datasetIndex: 0
2020-12-25 22:25:31 -06:00
property real currentValue: 0
property real minimumValue: 0
2021-09-03 13:51:43 -05:00
property real maximumValue: 0
2020-12-25 22:25:31 -06:00
//
2021-09-03 13:51:43 -05:00
// Window properties
2020-12-25 22:25:31 -06:00
//
2021-09-03 13:51:43 -05:00
spacing: -1
implicitWidth: 260
visible: opacity > 0
opacity: enabled ? 1 : 0
2021-09-14 15:02:31 -05:00
icon.source: "qrc:/icons/gauge.svg"
2021-09-03 13:51:43 -05:00
implicitHeight: implicitWidth + 96
borderColor: root.headerVisible ? Cpp_ThemeManager.datasetWindowBorder : "transparent"
2020-12-25 22:25:31 -06:00
//
2021-09-03 13:51:43 -05:00
// Connections with widget manager
2020-12-25 22:25:31 -06:00
//
2021-09-03 13:51:43 -05:00
Connections {
target: Cpp_UI_WidgetProvider
function onDataChanged() {
root.updateValues()
}
2020-12-25 22:25:31 -06:00
}
//
2021-09-03 13:51:43 -05:00
// Updates the internal values of the gauge widget
2020-12-25 22:25:31 -06:00
//
2021-09-03 13:51:43 -05:00
function updateValues() {
if (Cpp_UI_WidgetProvider.gaugeDatasetCount() > root.datasetIndex) {
root.minimumValue = Cpp_UI_WidgetProvider.gaugeMin(root.datasetIndex)
root.maximumValue = Cpp_UI_WidgetProvider.gaugeMax(root.datasetIndex)
root.currentValue = Cpp_UI_WidgetProvider.gauge(root.datasetIndex)
root.title = Cpp_UI_WidgetProvider.gaugeDatasetAt(root.datasetIndex).title
root.units = Cpp_UI_WidgetProvider.gaugeDatasetAt(root.datasetIndex).units
}
2020-12-25 22:25:31 -06:00
2021-09-03 13:51:43 -05:00
else {
root.title = ""
root.units = ""
root.currentValue = 0
root.minimumValue = 0
root.maximumValue = 0
2020-12-25 22:25:31 -06:00
}
}
//
2021-09-03 13:51:43 -05:00
// Layout
2020-12-25 22:25:31 -06:00
//
2021-09-03 13:51:43 -05:00
ColumnLayout {
spacing: app.spacing * 4
2020-12-25 22:25:31 -06:00
2021-09-03 13:51:43 -05:00
anchors {
fill: parent
margins: app.spacing * 4
leftMargin: app.spacing * 8
rightMargin: app.spacing * 8
}
2020-12-25 22:25:31 -06:00
2021-09-03 13:51:43 -05:00
//
// Gauge
//
CircularGauge {
id: gauge
tickmarksVisible: true
Layout.fillHeight: true
value: root.currentValue
minimumValue: root.minimumValue
maximumValue: root.maximumValue
Layout.alignment: Qt.AlignHCenter
Layout.minimumWidth: root.width * 0.6
Layout.maximumWidth: root.width * 0.6
style: CircularGaugeStyle {
maximumValueAngle: 144
minimumValueAngle: -144
labelStepSize: (root.maximumValue - root.minimumValue) / 10
tickmarkStepSize: (root.maximumValue - root.minimumValue) / 20
2021-09-05 15:37:11 -05:00
tickmark: Rectangle {
antialiasing: true
implicitWidth: outerRadius * 0.02
implicitHeight: outerRadius * 0.06
visible: styleData.value % labelStepSize == 0
color: Cpp_ThemeManager.widgetForegroundSecondary
}
minorTickmark: Rectangle {
antialiasing: true
implicitWidth: outerRadius * 0.01
implicitHeight: outerRadius * 0.03
color: Cpp_ThemeManager.widgetForegroundSecondary
}
tickmarkLabel: Text {
antialiasing: true
2021-09-17 19:44:59 -05:00
text: {
var value = styleData.value
if (value.toString().indexOf('.') !== -1)
return value.toFixed(2)
return value
}
2021-09-05 15:37:11 -05:00
font.pixelSize: Math.max(6, outerRadius * 0.1)
color: Cpp_ThemeManager.widgetForegroundSecondary
}
2020-12-25 22:25:31 -06:00
}
2021-09-03 13:51:43 -05:00
Behavior on value {NumberAnimation{duration: 200}}
//
// Value text
//
Label {
font.bold: true
anchors.centerIn: parent
2021-09-04 23:26:04 -05:00
font.family: app.monoFont
2021-09-05 03:20:08 -05:00
color: Cpp_ThemeManager.widgetForegroundPrimary
2021-09-03 13:51:43 -05:00
font.pixelSize: Math.max(12, gauge.height / 15)
anchors.verticalCenterOffset: parent.height * 0.17
text: (root.currentValue > root.maximumValue ? root.maximumValue.toFixed(2) :
root.currentValue.toFixed(2)) + " " + root.units
}
2020-12-25 22:25:31 -06:00
}
}
}