Skip to content

Conversation

@ybott-qinetic
Copy link
Contributor

Fixes #2708

src/units.cpp Outdated

static UnitMetadata UnitTable[] {
// unit label defaultPrec scalable veUnit
{ Victron::VenusOS::Enums::Units_None, "", 1, false, Unit::Default },
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd use 0 as the default precision for Units_None to avoid confusion. Even if it's a behavioural change it is never used with a precision anyway.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, this table could be sorted in alphabetical order (aside from putting Units_None first), to make it easier to read and find values. And for consistency, could do the same for the Units_Type enum in enums.h - this should be fine since there are no dependencies on particular numerical values; the user-preferred units are saved as strings or veutil equivalent values.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, but whatever the order is in this data structure, the order in the enums.h should be the same. If the table is sorted differently to the enum values, then we can't use the enum value as the index into the array. We could change the datastructure to be a map instead of an array, but honestly for perf reasons I think an array with index lookup is preferable. (We can discuss using std::vector instead of a plain array, shrug.)

Copy link
Contributor Author

@ybott-qinetic ybott-qinetic Jan 11, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

src/units.cpp Outdated
{ Victron::VenusOS::Enums::Units_Speed_MetresPerSecond, "m/s", 0, false, Unit::MetresPerSecond },
{ Victron::VenusOS::Enums::Units_Speed_KilometresPerHour, "k/h", 0, false, Unit::KilometresPerHour },
{ Victron::VenusOS::Enums::Units_Speed_MilesPerHour, "m/h", 0, false, Unit::MilesPerHour },
{ Victron::VenusOS::Enums::Units_Speed_Knots, "k", 0, false, Unit::Knots },
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"k" -> "kn"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

src/units.cpp Outdated
{ Victron::VenusOS::Enums::Units_Hertz, "Hz", 1, true, Unit::Default },
{ Victron::VenusOS::Enums::Units_Energy_KiloWattHour, "kWh", 3, true, Unit::Default },
{ Victron::VenusOS::Enums::Units_AmpHour, "", 1, true, Unit::Default },
{ Victron::VenusOS::Enums::Units_WattsPerSquareMetre, "W/m2", 0, false, Unit::Default },
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you check the scalable values? In main, isScalingSupported() returns true for these:

  • Units_WattsPerSquareMetre
  • Units_RevolutionsPerMinute
  • Units_Speed_MetresPerSecond
  • Units_Speed_KilometresPerHour
  • Units_Speed_MilesPerHour
  • Units_Speed_Knots
  • Units_Volume_Litre
  • Units_Volume_GallonUS
  • Units_Volume_GallonImperial

But in this table, they are false.

I see that you added scaling support for Units_Altitude_Metre, which makes sense, but maybe do that in an extra commit on top to be clear.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I make a logic judgement on if all the above actually make sense being able to scale in the way the software provides. I have changed it so it replicates behavior.

Copy link
Contributor

@blammit blammit Jan 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, I didn't realise those changes were all deliberate. In that case, can you mention the changes in the commit so that it's clear that the commit doesn't only make refactoring changes, but also makes changes in the scaling behaviour?

Or alternatively, combine those changes with the Units_Altitude_Metre commit.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ybott-qinetic ^ I don't see those changes in the latest commit - are you still planning to add them?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

return QStringLiteral("W/m2");
case VenusOS::Enums::Units_Percentage:
return QStringLiteral("%");
case VenusOS::Enums::Units_Temperature_Celsius:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You still need to check the formatHint for these temperature cases. Otherwise with this PR the "Environment" tab shows the C/F unnecessarily, below the gauges:

image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm seeing some compass directions here now - do you see that in your build?

image

Also the Boat page has some extra precision now:

image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@blammit Is that a build artifact issue? Should I look at fixing how gui_v2 builds?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did a clean build and I'm still seeing these issues. Hmm - wonder what is going on.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I modified the code manually and reverted it, and now everything is fine, so seems like there was some caching issue there.

src/units.cpp Outdated
namespace {

// \u33A5 is not supported by the font, so use two characters \u006D\u00B3 instead.
static const QString CubicMetre = QStringLiteral("m³");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer the unicode points to be spelled out, e.g. "\u0060\u00B3" in code, rather than the comment. That way the comment can be trimmed to just // \u33A5 is not supported by the font and the code itself becomes self-documenting.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

src/units.cpp Outdated
std::string label;
int precision;
bool scalable;
Unit::Type veUnit;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

super nitpicky, and maybe a bad suggestion if it degrades readability, but it might be worth considering changing the order pf the struct a bit differently, with std::string first, then the two enums, then the int, then the bool, just for data packing reasons.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

src/units.cpp Outdated

struct UnitMetadata {
Victron::VenusOS::Enums::Units_Type unit;
std::string label;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not store as a QString OOI? When we return it, I think we always convert to QString, and it's better to pay the cost once (when constructing the table) rather than on every read, IMO.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@ybott-qinetic ybott-qinetic force-pushed the ttomkins/unit-refactor branch 5 times, most recently from 79ef992 to 61c336e Compare January 12, 2026 07:34
@ybott-qinetic ybott-qinetic force-pushed the ttomkins/unit-refactor branch from 61c336e to 7f96c87 Compare January 13, 2026 21:28
src/units.cpp Outdated
bool scalable;
};

static UnitMetadata UnitTable[] {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

static const, maybe?
also, could be a static const std::vector { ... };

Copy link
Contributor

@chriadam chriadam left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One nitpick, aside from that, LGTM.
But I don't think this should be merged yet, as it's not for v1.2.x series.

@ybott-qinetic ybott-qinetic force-pushed the ttomkins/unit-refactor branch from 7f96c87 to 68e8b8b Compare January 14, 2026 04:53
@ybott-qinetic ybott-qinetic force-pushed the ttomkins/unit-refactor branch from 68e8b8b to c7f8db0 Compare January 22, 2026 07:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

units.cpp should be refactored to avoid large switch statements

4 participants