Skip to content

Commit

Permalink
Support both voltage and percentage in battery indicator
Browse files Browse the repository at this point in the history
Fix

Fix
  • Loading branch information
DonLakeFlyer committed Dec 14, 2023
1 parent ecfea2c commit 008acaf
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 10 deletions.
2 changes: 2 additions & 0 deletions qgroundcontrol.pro
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,7 @@ HEADERS += \
src/Settings/ADSBVehicleManagerSettings.h \
src/Settings/AppSettings.h \
src/Settings/AutoConnectSettings.h \
src/Settings/BatteryIndicatorSettings.h \
src/Settings/BrandImageSettings.h \
src/Settings/RemoteIDSettings.h \
src/Settings/FirmwareUpgradeSettings.h \
Expand Down Expand Up @@ -941,6 +942,7 @@ SOURCES += \
src/Settings/ADSBVehicleManagerSettings.cc \
src/Settings/AppSettings.cc \
src/Settings/AutoConnectSettings.cc \
src/Settings/BatteryIndicatorSettings.cc \
src/Settings/BrandImageSettings.cc \
src/Settings/RemoteIDSettings.cc \
src/Settings/FirmwareUpgradeSettings.cc \
Expand Down
1 change: 1 addition & 0 deletions qgroundcontrol.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@
<file alias="APMMavlinkStreamRate.SettingsGroup.json">src/Settings/APMMavlinkStreamRate.SettingsGroup.json</file>
<file alias="App.SettingsGroup.json">src/Settings/App.SettingsGroup.json</file>
<file alias="AutoConnect.SettingsGroup.json">src/Settings/AutoConnect.SettingsGroup.json</file>
<file alias="BatteryIndicator.SettingsGroup.json">src/Settings/BatteryIndicator.SettingsGroup.json</file>
<file alias="BrandImage.SettingsGroup.json">src/Settings/BrandImage.SettingsGroup.json</file>
<file alias="BreachReturn.FactMetaData.json">src/MissionManager/BreachReturn.FactMetaData.json</file>
<file alias="CameraCalc.FactMetaData.json">src/MissionManager/CameraCalc.FactMetaData.json</file>
Expand Down
1 change: 0 additions & 1 deletion src/QmlControls/QGCTextField.qml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ TextField {
color: qgcPal.textField
implicitWidth: ScreenTools.implicitTextFieldWidth
implicitHeight: ScreenTools.implicitTextFieldHeight
implicitWidth: ScreenTools.implicitTextFieldWidth

RowLayout {
id: unitsHelpLayout
Expand Down
3 changes: 1 addition & 2 deletions src/QmlControls/ScreenTools.qml
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,8 @@ Item {
property real implicitButtonHeight: Math.round(defaultFontPixelHeight * (isMobile ? 2.0 : 1.6))
property real implicitCheckBoxHeight: Math.round(defaultFontPixelHeight * (isMobile ? 2.0 : 1.0))
property real implicitRadioButtonHeight: implicitCheckBoxHeight
property real implicitTextFieldWidth: defaultFontPixelWidth * 15
property real implicitTextFieldWidth: defaultFontPixelWidth * 13
property real implicitTextFieldHeight: Math.round(defaultFontPixelHeight * (isMobile ? 2.0 : 1.6))
property real implicitTextFieldWidth: ScreenTools.defaultFontPixelWidth * 13
property real implicitComboBoxHeight: Math.round(defaultFontPixelHeight * (isMobile ? 2.0 : 1.6))
property real implicitComboBoxWidth: Math.round(defaultFontPixelWidth * (isMobile ? 7.0 : 5.0))
property real comboBoxPadding: defaultFontPixelWidth
Expand Down
15 changes: 15 additions & 0 deletions src/Settings/BatteryIndicator.SettingsGroup.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"version": 1,
"fileType": "FactMetaData",
"QGC.MetaData.Facts":
[
{
"name": "display",
"shortDesc": "Select values to display in indicator",
"enumStrings": "Percentage,Voltage,Both",
"enumValues": "0,1,2",
"type": "uint32",
"default": false
}
]
}
20 changes: 20 additions & 0 deletions src/Settings/BatteryIndicatorSettings.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/****************************************************************************
*
* (c) 2009-2020 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
*
* QGroundControl is licensed according to the terms in the file
* COPYING.md in the root of the source code directory.
*
****************************************************************************/

#include "BatteryIndicatorSettings.h"

#include <QQmlEngine>
#include <QtQml>

DECLARE_SETTINGGROUP(BatteryIndicator, "BatteryIndicator")
{
qmlRegisterUncreatableType<BatteryIndicatorSettings>("QGroundControl.SettingsManager", 1, 0, "BatteryIndicatorSettings", "Reference only");
}

DECLARE_SETTINGSFACT(BatteryIndicatorSettings, display)
22 changes: 22 additions & 0 deletions src/Settings/BatteryIndicatorSettings.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/****************************************************************************
*
* (c) 2009-2020 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
*
* QGroundControl is licensed according to the terms in the file
* COPYING.md in the root of the source code directory.
*
****************************************************************************/

#pragma once

#include "SettingsGroup.h"

class BatteryIndicatorSettings : public SettingsGroup
{
Q_OBJECT
public:
BatteryIndicatorSettings(QObject* parent = nullptr);
DEFINE_SETTING_NAME_GROUP()

DEFINE_SETTINGFACT(display)
};
2 changes: 2 additions & 0 deletions src/Settings/SettingsManager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ SettingsManager::SettingsManager(QGCApplication* app, QGCToolbox* toolbox)
, _offlineMapsSettings (nullptr)
, _firmwareUpgradeSettings (nullptr)
, _adsbVehicleManagerSettings (nullptr)
, _batteryIndicatorSettings (nullptr)
#if !defined(NO_ARDUPILOT_DIALECT)
, _apmMavlinkStreamRateSettings (nullptr)
#endif
Expand Down Expand Up @@ -54,6 +55,7 @@ void SettingsManager::setToolbox(QGCToolbox *toolbox)
_offlineMapsSettings = new OfflineMapsSettings (this);
_firmwareUpgradeSettings = new FirmwareUpgradeSettings (this);
_adsbVehicleManagerSettings = new ADSBVehicleManagerSettings (this);
_batteryIndicatorSettings = new BatteryIndicatorSettings (this);
#if !defined(NO_ARDUPILOT_DIALECT)
_apmMavlinkStreamRateSettings = new APMMavlinkStreamRateSettings(this);
#endif
Expand Down
4 changes: 4 additions & 0 deletions src/Settings/SettingsManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "APMMavlinkStreamRateSettings.h"
#include "FirmwareUpgradeSettings.h"
#include "ADSBVehicleManagerSettings.h"
#include "BatteryIndicatorSettings.h"
#include <QVariantList>
#include "RemoteIDSettings.h"

Expand All @@ -52,6 +53,7 @@ class SettingsManager : public QGCTool
Q_PROPERTY(QObject* offlineMapsSettings READ offlineMapsSettings CONSTANT)
Q_PROPERTY(QObject* firmwareUpgradeSettings READ firmwareUpgradeSettings CONSTANT)
Q_PROPERTY(QObject* adsbVehicleManagerSettings READ adsbVehicleManagerSettings CONSTANT)
Q_PROPERTY(QObject* batteryIndicatorSettings READ batteryIndicatorSettings CONSTANT)
#if !defined(NO_ARDUPILOT_DIALECT)
Q_PROPERTY(QObject* apmMavlinkStreamRateSettings READ apmMavlinkStreamRateSettings CONSTANT)
#endif
Expand All @@ -72,6 +74,7 @@ class SettingsManager : public QGCTool
OfflineMapsSettings* offlineMapsSettings (void) { return _offlineMapsSettings; }
FirmwareUpgradeSettings* firmwareUpgradeSettings (void) { return _firmwareUpgradeSettings; }
ADSBVehicleManagerSettings* adsbVehicleManagerSettings (void) { return _adsbVehicleManagerSettings; }
BatteryIndicatorSettings* batteryIndicatorSettings (void) { return _batteryIndicatorSettings; }
#if !defined(NO_ARDUPILOT_DIALECT)
APMMavlinkStreamRateSettings* apmMavlinkStreamRateSettings(void) { return _apmMavlinkStreamRateSettings; }
#endif
Expand All @@ -90,6 +93,7 @@ class SettingsManager : public QGCTool
OfflineMapsSettings* _offlineMapsSettings;
FirmwareUpgradeSettings* _firmwareUpgradeSettings;
ADSBVehicleManagerSettings* _adsbVehicleManagerSettings;
BatteryIndicatorSettings* _batteryIndicatorSettings;
#if !defined(NO_ARDUPILOT_DIALECT)
APMMavlinkStreamRateSettings* _apmMavlinkStreamRateSettings;
#endif
Expand Down
63 changes: 56 additions & 7 deletions src/ui/toolbar/BatteryIndicator.qml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ Item {
property bool waitForParameters: false // UI won't show until parameters are ready
property Component expandedPageComponent

property var _activeVehicle: QGroundControl.multiVehicleManager.activeVehicle
property var _activeVehicle: QGroundControl.multiVehicleManager.activeVehicle
property Fact _indicatorDisplay: QGroundControl.settingsManager.batteryIndicatorSettings.display
property bool _showPercentage: _indicatorDisplay.rawValue === 0
property bool _showVoltage: _indicatorDisplay.rawValue === 1
property bool _showBoth: _indicatorDisplay.rawValue === 2

Row {
id: batteryIndicatorRow
Expand Down Expand Up @@ -103,7 +107,16 @@ Item {
} else if (battery.chargeState.rawValue !== MAVLink.MAV_BATTERY_CHARGE_STATE_UNDEFINED) {
return battery.chargeState.enumStringValue
}
return ""
return qsTr("n/a")
}

function getBatteryVoltageText() {
if (!isNaN(battery.voltage.rawValue)) {
return battery.voltage.valueString + battery.voltage.units
} else if (battery.chargeState.rawValue !== MAVLink.MAV_BATTERY_CHARGE_STATE_UNDEFINED) {
return battery.chargeState.enumStringValue
}
return qsTr("n/a")
}

QGCColoredImage {
Expand All @@ -116,11 +129,28 @@ Item {
color: getBatteryColor()
}

QGCLabel {
text: getBatteryPercentageText()
font.pointSize: ScreenTools.mediumFontPointSize
color: getBatteryColor()
anchors.verticalCenter: parent.verticalCenter
ColumnLayout {
id: batteryInfoColumn
anchors.top: parent.top
anchors.bottom: parent.bottom
spacing: 0

QGCLabel {
Layout.alignment: Qt.AlignHCenter
verticalAlignment: Text.AlignVCenter
color: getBatteryColor()
text: getBatteryPercentageText()
font.pointSize: _showBoth ? ScreenTools.defaultFontPointSize : ScreenTools.mediumFontPointSize
visible: _showBoth || _showPercentage
}

QGCLabel {
Layout.alignment: Qt.AlignHCenter
font.pointSize: _showBoth ? ScreenTools.defaultFontPointSize : ScreenTools.mediumFontPointSize
color: getBatteryColor()
text: getBatteryVoltageText()
visible: _showBoth || _showVoltage
}
}
}
}
Expand Down Expand Up @@ -243,6 +273,25 @@ Item {
sourceComponent: expandedPageComponent
}

IndicatorPageGroupLayout {
Layout.fillWidth: true

RowLayout {
Layout.fillWidth: true

QGCLabel {
Layout.fillWidth: true
text: qsTr("Battery Display")
}

FactComboBox {
id: editModeCheckBox
fact: QGroundControl.settingsManager.batteryIndicatorSettings.display
sizeToContents: true
}
}
}

IndicatorPageGroupLayout {
Layout.fillWidth: true
showDivider: false
Expand Down

0 comments on commit 008acaf

Please sign in to comment.