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

228 lines
6.2 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: 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
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-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: CppWidgets
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() {
if (CppWidgets.accelerometerGroupCount() > root.groupIndex) {
2020-12-27 23:42:23 -06:00
root.accX = CppWidgets.accelerometerX(root.groupIndex)
root.accY = CppWidgets.accelerometerY(root.groupIndex)
root.accZ = CppWidgets.accelerometerZ(root.groupIndex)
root.title = CppWidgets.accelerometerGroupAt(root.groupIndex).title
}
else {
2020-12-27 23:42:23 -06:00
root.accX = 0
root.accY = 0
root.accZ = 0
root.title = 0
}
var gX = root.accX / root.gConstant
var gY = root.accY / root.gConstant
var gZ = root.accZ / root.gConstant
root.meanGForce = Math.sqrt(Math.pow(gX, 2) + Math.pow(gY, 2) + Math.pow(gZ, 2))
2020-12-25 15:33:12 -06:00
if (root.meanGForce > root.max)
root.max = root.meanGForce
else if (root.meanGForce < root.min)
root.min = root.meanGForce
calculateGaugeSize()
}
2020-12-27 17:21:19 -06:00
//
// Redraws accelerator gauge
//
function calculateGaugeSize() {
if (root.width < root.height)
root.gaugeSize = Math.max(120, root.width - controls.implicitWidth - 12 * app.spacing)
2020-12-27 17:21:19 -06:00
else
root.gaugeSize = Math.max(120, root.height - controls.implicitWidth - 4 * app.spacing)
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
//
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: root.min
maximumValue: root.max
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
}
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
color: gauge.valueColor
font.family: app.monoFont
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
color: gauge.valueColor
font.family: app.monoFont
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
color: gauge.valueColor
font.family: app.monoFont
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
border.color: gauge.valueColor
}
}
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
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
}
}