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

248 lines
7.1 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
2020-12-25 15:33:12 -06:00
import QtQuick.Layouts 1.12
import QtQuick.Controls 2.12
import SerialStudio 1.0
2020-12-25 15:33:12 -06:00
import "."
Window {
id: root
2020-12-25 15:33:12 -06:00
//
// Properties
//
spacing: -1
2020-12-30 16:11:51 -06:00
gradient: true
2020-12-25 19:23:37 -06:00
implicitWidth: 260
2020-12-25 15:33:12 -06:00
visible: opacity > 0
opacity: enabled ? 1 : 0
2020-12-25 19:45:29 -06:00
icon.source: "qrc:/icons/tab.svg"
2020-12-25 19:23:37 -06:00
implicitHeight: implicitWidth + 96
2021-09-05 02:41:48 -05:00
backgroundColor: Cpp_ThemeManager.widgetBackground
2020-12-25 15:33:12 -06:00
//
// Custom properties
//
2020-12-25 19:23:37 -06:00
property real max: 0
property real min: 0
property real accX: 0
property real accY: 0
property real accZ: 0
2020-12-27 00:00:31 -06:00
property int groupIndex: 0
property real meanGForce: 0
property real gConstant: 9.80665
property bool firstReading: true
2020-12-27 17:21:19 -06:00
property real gaugeSize: calculateGaugeSize()
//
// Update gauge size automatically
//
onWidthChanged: calculateGaugeSize()
onHeightChanged: calculateGaugeSize()
2020-12-25 15:33:12 -06:00
//
// Connections with widget manager
//
Connections {
target: Cpp_UI_WidgetProvider
2020-12-25 15:33:12 -06:00
function onDataChanged() {
root.calculateMeanGForce()
2020-12-25 15:33:12 -06:00
}
}
//
// Calculates the mean g force for all three axes using the pythagorean theorem
//
function calculateMeanGForce() {
// Update values
if (Cpp_UI_WidgetProvider.accelerometerGroupCount() > root.groupIndex) {
root.accX = Cpp_UI_WidgetProvider.accelerometerX(root.groupIndex)
root.accY = Cpp_UI_WidgetProvider.accelerometerY(root.groupIndex)
root.accZ = Cpp_UI_WidgetProvider.accelerometerZ(root.groupIndex)
root.title = Cpp_UI_WidgetProvider.accelerometerGroupAt(root.groupIndex).title
}
// Invalid widget index, reset everything
else {
2020-12-27 23:42:23 -06:00
root.accX = 0
root.accY = 0
root.accZ = 0
root.title = 0
root.firstReading = true
}
// Obtain g-forces for each axis
var gX = root.accX / root.gConstant
var gY = root.accY / root.gConstant
var gZ = root.accZ / root.gConstant
// Calculate mean force by squared normalization
root.meanGForce = Math.sqrt(Math.pow(gX, 2) + Math.pow(gY, 2) + Math.pow(gZ, 2))
2020-12-25 15:33:12 -06:00
// Update min/max values
if (!firstReading) {
if (root.meanGForce > root.max)
root.max = root.meanGForce
else if (root.meanGForce < root.min)
root.min = root.meanGForce
}
// This is first reading, set min/max to current value
else {
// Update min/max and ensure that this is called only once
if (root.meanGForce != 0) {
root.max = root.meanGForce
root.min = root.meanGForce
root.firstReading = false
}
// Just to be sure re-calculate gauge size on first reading
calculateGaugeSize()
}
}
2020-12-27 17:21:19 -06:00
//
// Redraws accelerator gauge
//
function calculateGaugeSize() {
if (root.width < root.height)
2021-03-12 09:56:17 -05:00
root.gaugeSize = Math.max(120, root.width - controls.implicitWidth - 12 * app.spacing) * 0.8
2020-12-27 17:21:19 -06:00
else
2021-03-12 09:56:17 -05:00
root.gaugeSize = Math.max(120, root.height - controls.implicitWidth - 4 * app.spacing) * 0.8
2020-12-27 17:21:19 -06:00
}
//
2020-12-25 19:23:37 -06:00
// Gauge & reset button
//
2020-12-27 00:00:31 -06:00
RowLayout {
2020-12-25 19:23:37 -06:00
spacing: 0
2020-12-25 15:33:12 -06:00
anchors.fill: parent
2020-12-27 00:00:31 -06:00
anchors.margins: app.spacing
2020-12-25 19:23:37 -06:00
//
// Spacer
//
Item {
2020-12-27 00:00:31 -06:00
Layout.fillWidth: true
}
2020-12-25 19:23:37 -06:00
//
// Gauge object
//
2021-09-03 13:51:43 -05:00
AccelerometerGaugeDelegate {
2020-12-27 00:00:31 -06:00
id: gauge
2020-12-25 22:25:31 -06:00
lastNumber: 8
2020-12-27 00:00:31 -06:00
firstNumber: 0
2021-09-05 15:37:11 -05:00
minimumValue: root.min * 0.5
maximumValue: root.max * 2
2020-12-27 00:00:31 -06:00
valueLabelVisible: false
currentValue: root.meanGForce
2020-12-25 19:23:37 -06:00
Layout.alignment: Qt.AlignHCenter
Layout.minimumWidth: root.gaugeSize
Layout.maximumWidth: root.gaugeSize
Layout.minimumHeight: root.gaugeSize
Layout.maximumHeight: root.gaugeSize
title: qsTr("G Units")
}
2020-12-25 15:33:12 -06:00
2020-12-25 19:23:37 -06:00
//
// Spacer
//
Item {
2020-12-27 00:00:31 -06:00
Layout.fillWidth: true
2020-12-25 19:23:37 -06:00
}
//
2020-12-27 00:00:31 -06:00
// Reset button & values
2020-12-25 19:23:37 -06:00
//
2020-12-27 00:00:31 -06:00
ColumnLayout {
2020-12-27 17:21:19 -06:00
id: controls
2020-12-27 00:00:31 -06:00
spacing: app.spacing
Layout.alignment: Qt.AlignVCenter
Label {
font.pixelSize: 12
font.family: app.monoFont
2021-09-05 03:20:08 -05:00
color: Cpp_ThemeManager.widgetForegroundPrimary
2020-12-27 20:20:01 -06:00
Layout.alignment: Qt.AlignHCenter
text: qsTr("%1 G MAX").arg(root.max.toFixed(2))
2020-12-27 00:00:31 -06:00
}
Label {
font.pixelSize: 12
font.family: app.monoFont
2021-09-05 03:20:08 -05:00
color: Cpp_ThemeManager.widgetForegroundPrimary
2020-12-27 20:20:01 -06:00
Layout.alignment: Qt.AlignHCenter
text: qsTr("%1 G MIN").arg(root.min.toFixed(2))
2020-12-27 00:00:31 -06:00
}
Item {
height: app.spacing
}
Label {
font.bold: true
font.pixelSize: 12
font.family: app.monoFont
2021-09-05 03:20:08 -05:00
color: Cpp_ThemeManager.widgetForegroundPrimary
2020-12-27 20:20:01 -06:00
Layout.alignment: Qt.AlignHCenter
text: qsTr("%1 G ACT").arg(root.meanGForce.toFixed(2))
2020-12-27 00:00:31 -06:00
Rectangle {
border.width: 1
color: "transparent"
anchors.fill: parent
anchors.margins: -app.spacing
2021-09-05 03:20:08 -05:00
border.color: Cpp_ThemeManager.widgetForegroundPrimary
2020-12-27 00:00:31 -06:00
}
}
Item {
height: app.spacing
}
Button {
text: qsTr("Reset")
2020-12-27 20:20:01 -06:00
Layout.alignment: Qt.AlignHCenter
2020-12-27 00:00:31 -06:00
onClicked: {
root.max = 0
root.min = 0
root.firstReading = true
2020-12-27 00:00:31 -06:00
}
2020-12-25 19:23:37 -06:00
}
}
//
// Spacer
//
Item {
2020-12-27 00:00:31 -06:00
Layout.fillWidth: true
2020-12-25 19:23:37 -06:00
}
2020-12-25 15:33:12 -06:00
}
}