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

202 lines
5.7 KiB
QML
Raw Normal View History

/*
* Copyright (c) 2020 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 Group 1.0
import Dataset 1.0
2020-12-25 15:33:12 -06:00
import "."
Window {
id: accel
2020-12-25 15:33:12 -06:00
//
// Properties
//
spacing: -1
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
2020-12-25 15:33:12 -06:00
borderColor: Qt.rgba(81/255, 116/255, 151/255, 1)
2020-12-25 19:32:26 -06:00
backgroundColor: Qt.rgba(9 / 255, 9 / 255, 12 / 255, 1)
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
2020-12-25 15:33:12 -06:00
//
// Connections with widget manager
//
Connections {
target: CppWidgets
function onDataChanged() {
accel.calculateMeanGForce()
}
}
//
// Calculates the mean g force for all three axes using the pythagorean theorem
//
function calculateMeanGForce() {
2020-12-27 00:00:31 -06:00
if (CppWidgets.accelerometerGroupCount() > accel.groupIndex) {
accel.accX = CppWidgets.accelerometerX(accel.groupIndex)
accel.accY = CppWidgets.accelerometerY(accel.groupIndex)
accel.accZ = CppWidgets.accelerometerZ(accel.groupIndex)
accel.title = CppWidgets.accelerometerGroupAt(accel.groupIndex).title
}
else {
accel.accX = 0
accel.accY = 0
accel.accZ = 0
accel.title = 0
}
var gX = accel.accX / accel.gConstant
var gY = accel.accY / accel.gConstant
var gZ = accel.accZ / accel.gConstant
accel.meanGForce = Math.sqrt(Math.pow(gX, 2) + Math.pow(gY, 2) + Math.pow(gZ, 2))
2020-12-25 15:33:12 -06:00
if (accel.meanGForce > accel.max)
accel.max = accel.meanGForce
else if (accel.meanGForce < accel.min)
accel.min = accel.meanGForce
}
//
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
//
2020-12-25 22:25:31 -06:00
GaugeDelegate {
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
2020-12-25 22:25:31 -06:00
title: qsTr("G Units")
minimumValue: accel.min
maximumValue: accel.max
2020-12-27 00:00:31 -06:00
valueLabelVisible: false
2020-12-25 22:25:31 -06:00
currentValue: accel.meanGForce
2020-12-25 19:23:37 -06:00
Layout.alignment: Qt.AlignHCenter
2020-12-27 00:00:31 -06:00
Layout.minimumWidth: Math.min(accel.height, accel.width) - 12 * app.spacing
Layout.maximumWidth: Math.min(accel.height, accel.width) - 12 * app.spacing
Layout.minimumHeight: Math.min(accel.height, accel.width) - 12 * app.spacing
Layout.maximumHeight: Math.min(accel.height, accel.width) - 12 * app.spacing
}
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 {
spacing: app.spacing
Layout.alignment: Qt.AlignVCenter
Label {
font.pixelSize: 12
color: gauge.valueColor
font.family: app.monoFont
text: qsTr("Maximum: %1 G").arg(accel.max.toFixed(2))
}
Label {
font.pixelSize: 12
color: gauge.valueColor
font.family: app.monoFont
text: qsTr("Minimum: %1 G").arg(accel.min.toFixed(2))
}
Item {
height: app.spacing
}
Label {
font.bold: true
font.pixelSize: 12
color: gauge.valueColor
font.family: app.monoFont
text: qsTr("Current: %1 G").arg(accel.meanGForce.toFixed(2))
Rectangle {
border.width: 1
color: "transparent"
anchors.fill: parent
anchors.margins: -app.spacing
border.color: gauge.valueColor
}
}
Item {
height: app.spacing
}
Button {
text: qsTr("Reset")
onClicked: {
accel.max = 0
accel.min = 0
}
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
}
}