Skip to content

Commit

Permalink
Added suffix definition for gauges (#68)
Browse files Browse the repository at this point in the history
* Added suffix definition for number gauges
* Added value suffix feature to SimpleGaugeInstrument
* SimpleNumberInstrument now uses DrawLabel for the text display. The suffix can have a different size.

---------

Co-authored-by: Nicholas John Koch <koch@njk-it.de>
  • Loading branch information
aviatorhh and Nicholas John Koch authored Sep 30, 2024
1 parent 9a8e546 commit 6980d72
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 45 deletions.
2 changes: 2 additions & 0 deletions include/instrument.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ PLUGIN_BEGIN_NAMESPACE
#define DSK_SETTING_INSTR_WIDTH "instrument_width"
#define DSK_SETTING_INSTR_HEIGHT "instrument_height"
#define DSK_SETTING_MEAN_FG "mean_color"
#define DSK_SETTING_VALUE_SUFFIX "value_suffix"
#define DSK_SETTING_SUFFIX_FONT "suffix_font"
#define DSK_SETTING_TIME_FG "time_color"
#define DSK_SETTING_ORDER "instrument_order"
#define DSK_SETTING_HISTORY "history_length"
Expand Down
44 changes: 24 additions & 20 deletions include/simplegaugeinstrument.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,48 +88,50 @@
ConcatChoiceStrings(m_gauge_types), AsInt, GetIntSetting) \
X(2, DSK_SETTING_FORMAT, 0, _("Format"), ChoiceCtrl, \
ConcatChoiceStrings(m_supported_formats), AsInt, GetIntSetting) \
X(3, DSK_SETTING_TRANSFORMATION, 0, _("Transformation"), ChoiceCtrl, \
X(3, DSK_SETTING_VALUE_SUFFIX, wxString(wxEmptyString), _("Value suffix"), \
TextCtrl, wxEmptyString, AsString, GetStringSetting) \
X(4, DSK_SETTING_TRANSFORMATION, 0, _("Transformation"), ChoiceCtrl, \
ConcatChoiceStrings(m_supported_transforms), AsInt, GetIntSetting) \
X(4, DSK_SETTING_ZONES, wxString(wxEmptyString), _("Zones"), \
X(5, DSK_SETTING_ZONES, wxString(wxEmptyString), _("Zones"), \
SignalKZonesCtrl, wxEmptyString, AsString, GetStringSetting) \
X(5, DSK_SETTING_SMOOTHING, m_smoothing, _("Data smoothing"), SpinCtrl, \
X(6, DSK_SETTING_SMOOTHING, m_smoothing, _("Data smoothing"), SpinCtrl, \
"0;" STRINGIFY(DSK_SGI_SMOOTHING_MAX), AsInt, GetIntSetting) \
X(6, DSK_SETTING_INSTR_SIZE, m_instrument_size, _("Instrument size"), \
X(7, DSK_SETTING_INSTR_SIZE, m_instrument_size, _("Instrument size"), \
SpinCtrl, \
STRINGIFY(DSK_SGI_INSTR_MIN_SIZE) ";" STRINGIFY( \
DSK_SGI_INSTR_MAX_SIZE), \
AsInt, GetIntSetting) \
X(7, DSK_SGI_NEEDLE_FG, DSK_SGI_COLOR_NEEDLE, _("Needle color"), \
X(8, DSK_SGI_NEEDLE_FG, DSK_SGI_COLOR_NEEDLE, _("Needle color"), \
ColourPickerCtrl, wxEmptyString, AsString, GetStringSetting) \
X(7, DSK_SGI_RIM_NOMINAL, DSK_SGI_COLOR_RIM, _("Rim color"), \
X(9, DSK_SGI_RIM_NOMINAL, DSK_SGI_COLOR_RIM, _("Rim color"), \
ColourPickerCtrl, wxEmptyString, AsString, GetStringSetting) \
X(8, DSK_SGI_RIM_PORT, DSK_SGI_COLOR_RIM_P, _("Port color"), \
X(10, DSK_SGI_RIM_PORT, DSK_SGI_COLOR_RIM_P, _("Port color"), \
ColourPickerCtrl, wxEmptyString, AsString, GetStringSetting) \
X(9, DSK_SGI_RIM_STBD, DSK_SGI_COLOR_RIM_S, _("Starboard color"), \
X(11, DSK_SGI_RIM_STBD, DSK_SGI_COLOR_RIM_S, _("Starboard color"), \
ColourPickerCtrl, wxEmptyString, AsString, GetStringSetting) \
X(10, DSK_SGI_RIM_DEAD, DSK_SGI_COLOR_RIM_D, _("Dead angle color"), \
X(12, DSK_SGI_RIM_DEAD, DSK_SGI_COLOR_RIM_D, _("Dead angle color"), \
ColourPickerCtrl, wxEmptyString, AsString, GetStringSetting) \
X(11, DSK_SGI_DIAL_COLOR, DSK_SGI_COLOR_DIAL, _("Dial color"), \
X(13, DSK_SGI_DIAL_COLOR, DSK_SGI_COLOR_DIAL, _("Dial color"), \
ColourPickerCtrl, wxEmptyString, AsString, GetStringSetting) \
X(12, DSK_SETTING_TITLE_FG, DSK_SGI_COLOR_TITLE, _("Title color"), \
X(14, DSK_SETTING_TITLE_FG, DSK_SGI_COLOR_TITLE, _("Title color"), \
ColourPickerCtrl, wxEmptyString, AsString, GetStringSetting) \
X(13, DSK_SETTING_NOMINAL_FG, DSK_SGI_COLOR_VALUE, _("Value color"), \
X(15, DSK_SETTING_NOMINAL_FG, DSK_SGI_COLOR_VALUE, _("Value color"), \
ColourPickerCtrl, wxEmptyString, AsString, GetStringSetting) \
X(14, DSK_SGI_TICK_FG, DSK_SGI_COLOR_TICK, _("Tick color"), \
X(16, DSK_SGI_TICK_FG, DSK_SGI_COLOR_TICK, _("Tick color"), \
ColourPickerCtrl, wxEmptyString, AsString, GetStringSetting) \
X(15, DSK_SGI_TICK_LEGEND, DSK_SGI_COLOR_TICK_TXT, _("Dial values"), \
X(17, DSK_SGI_TICK_LEGEND, DSK_SGI_COLOR_TICK_TXT, _("Dial values"), \
ColourPickerCtrl, wxEmptyString, AsString, GetStringSetting) \
X(16, DSK_SETTING_NORMAL_FG, DSK_SGI_COLOR_NORMAL, _("Normal color"), \
X(18, DSK_SETTING_NORMAL_FG, DSK_SGI_COLOR_NORMAL, _("Normal color"), \
ColourPickerCtrl, wxEmptyString, AsString, GetStringSetting) \
X(17, DSK_SETTING_ALERT_FG, DSK_SGI_COLOR_ALERT, _("Alert color"), \
X(19, DSK_SETTING_ALERT_FG, DSK_SGI_COLOR_ALERT, _("Alert color"), \
ColourPickerCtrl, wxEmptyString, AsString, GetStringSetting) \
X(18, DSK_SETTING_WARN_FG, DSK_SGI_COLOR_WARN, _("Warn color"), \
X(20, DSK_SETTING_WARN_FG, DSK_SGI_COLOR_WARN, _("Warn color"), \
ColourPickerCtrl, wxEmptyString, AsString, GetStringSetting) \
X(19, DSK_SETTING_ALRM_FG, DSK_SGI_COLOR_ALARM, _("Alarm color"), \
X(21, DSK_SETTING_ALRM_FG, DSK_SGI_COLOR_ALARM, _("Alarm color"), \
ColourPickerCtrl, wxEmptyString, AsString, GetStringSetting) \
X(20, DSK_SETTING_EMERG_FG, DSK_SGI_COLOR_EMERG, _("Emergency color"), \
X(22, DSK_SETTING_EMERG_FG, DSK_SGI_COLOR_EMERG, _("Emergency color"), \
ColourPickerCtrl, wxEmptyString, AsString, GetStringSetting) \
X(21, DSK_SETTING_BORDER_COLOR, DSK_SGI_COLOR_BORDER, _("Border color"), \
X(23, DSK_SETTING_BORDER_COLOR, DSK_SGI_COLOR_BORDER, _("Border color"), \
ColourPickerCtrl, wxEmptyString, AsString, GetStringSetting)
PLUGIN_BEGIN_NAMESPACE
Expand Down Expand Up @@ -210,6 +212,8 @@ class SimpleGaugeInstrument : public Instrument {
double m_min_val;
/// Size of the instrument in device independent pixels
wxCoord m_instrument_size;
/// add a suffix to the value if applicable
wxString m_value_suffix;
/// Draw arc sector. The arc is drawn counterclockwise from \c start_angle
/// to \c end_angle
Expand Down
44 changes: 26 additions & 18 deletions include/simplenumberinstrument.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,42 +61,46 @@
SignalKKeyCtrl, wxEmptyString, AsString, GetStringSetting) \
X(1, DSK_SETTING_FORMAT, 0, _("Format"), ChoiceCtrl, \
ConcatChoiceStrings(m_supported_formats), AsInt, GetIntSetting) \
X(2, DSK_SETTING_TRANSFORMATION, 0, _("Transformation"), ChoiceCtrl, \
X(2, DSK_SETTING_VALUE_SUFFIX, wxString(wxEmptyString), _("Value suffix"), \
TextCtrl, wxEmptyString, AsString, GetStringSetting) \
X(3, DSK_SETTING_TRANSFORMATION, 0, _("Transformation"), ChoiceCtrl, \
ConcatChoiceStrings(m_supported_transforms), AsInt, GetIntSetting) \
X(3, DSK_SETTING_ZONES, wxString(wxEmptyString), _("Zones"), \
X(4, DSK_SETTING_ZONES, wxString(wxEmptyString), _("Zones"), \
SignalKZonesCtrl, wxEmptyString, AsString, GetStringSetting) \
X(4, DSK_SETTING_SMOOTHING, m_smoothing, _("Data smoothing"), SpinCtrl, \
X(5, DSK_SETTING_SMOOTHING, m_smoothing, _("Data smoothing"), SpinCtrl, \
"0;" STRINGIFY(DSK_SNI_SMOOTHING_MAX), AsInt, GetIntSetting) \
X(5, DSK_SETTING_TITLE_FONT, m_title_font.GetPointSize(), _("Title size"), \
X(6, DSK_SETTING_TITLE_FONT, m_title_font.GetPointSize(), _("Title size"), \
SpinCtrl, "5;40", AsInt, GetIntSetting) \
X(6, DSK_SETTING_BODY_FONT, m_body_font.GetPointSize(), _("Body size"), \
X(7, DSK_SETTING_BODY_FONT, m_body_font.GetPointSize(), _("Body size"), \
SpinCtrl, "5;40", AsInt, GetIntSetting) \
X(7, DSK_SETTING_TITLE_BG, DSK_SNI_COLOR_TITLE_BG, _("Title background"), \
X(8, DSK_SETTING_SUFFIX_FONT, m_suffix_font.GetPointSize(), \
_("Suffix size"), SpinCtrl, "5;40", AsInt, GetIntSetting) \
X(9, DSK_SETTING_TITLE_BG, DSK_SNI_COLOR_TITLE_BG, _("Title background"), \
ColourPickerCtrl, wxEmptyString, AsString, GetStringSetting) \
X(8, DSK_SETTING_TITLE_FG, DSK_SNI_COLOR_TITLE_FG, _("Title color"), \
X(10, DSK_SETTING_TITLE_FG, DSK_SNI_COLOR_TITLE_FG, _("Title color"), \
ColourPickerCtrl, wxEmptyString, AsString, GetStringSetting) \
X(9, DSK_SETTING_BODY_BG, DSK_SNI_COLOR_BODY_BG, _("Body background"), \
X(11, DSK_SETTING_BODY_BG, DSK_SNI_COLOR_BODY_BG, _("Body background"), \
ColourPickerCtrl, wxEmptyString, AsString, GetStringSetting) \
X(10, DSK_SETTING_BODY_FG, DSK_SNI_COLOR_BODY_FG, _("Body color"), \
X(12, DSK_SETTING_BODY_FG, DSK_SNI_COLOR_BODY_FG, _("Body color"), \
ColourPickerCtrl, wxEmptyString, AsString, GetStringSetting) \
X(11, DSK_SETTING_ALERT_BG, DSK_SNI_COLOR_ALERT_BG, _("Alert background"), \
X(13, DSK_SETTING_ALERT_BG, DSK_SNI_COLOR_ALERT_BG, _("Alert background"), \
ColourPickerCtrl, wxEmptyString, AsString, GetStringSetting) \
X(12, DSK_SETTING_ALERT_FG, DSK_SNI_COLOR_ALERT_FG, _("Alert color"), \
X(14, DSK_SETTING_ALERT_FG, DSK_SNI_COLOR_ALERT_FG, _("Alert color"), \
ColourPickerCtrl, wxEmptyString, AsString, GetStringSetting) \
X(13, DSK_SETTING_WARN_BG, DSK_SNI_COLOR_WARN_BG, _("Warning background"), \
X(15, DSK_SETTING_WARN_BG, DSK_SNI_COLOR_WARN_BG, _("Warning background"), \
ColourPickerCtrl, wxEmptyString, AsString, GetStringSetting) \
X(14, DSK_SETTING_WARN_FG, DSK_SNI_COLOR_WARN_FG, _("Warning color"), \
X(16, DSK_SETTING_WARN_FG, DSK_SNI_COLOR_WARN_FG, _("Warning color"), \
ColourPickerCtrl, wxEmptyString, AsString, GetStringSetting) \
X(15, DSK_SETTING_ALRM_BG, DSK_SNI_COLOR_ALRM_BG, _("Alarm background"), \
X(17, DSK_SETTING_ALRM_BG, DSK_SNI_COLOR_ALRM_BG, _("Alarm background"), \
ColourPickerCtrl, wxEmptyString, AsString, GetStringSetting) \
X(16, DSK_SETTING_ALRM_FG, DSK_SNI_COLOR_ALRM_FG, _("Alarm color"), \
X(18, DSK_SETTING_ALRM_FG, DSK_SNI_COLOR_ALRM_FG, _("Alarm color"), \
ColourPickerCtrl, wxEmptyString, AsString, GetStringSetting) \
X(17, DSK_SETTING_EMERG_BG, DSK_SNI_COLOR_EMERG_BG, \
X(19, DSK_SETTING_EMERG_BG, DSK_SNI_COLOR_EMERG_BG, \
_("Emergency background"), ColourPickerCtrl, wxEmptyString, AsString, \
GetStringSetting) \
X(18, DSK_SETTING_EMERG_FG, DSK_SNI_COLOR_EMERG_FG, _("Emergency color"), \
X(20, DSK_SETTING_EMERG_FG, DSK_SNI_COLOR_EMERG_FG, _("Emergency color"), \
ColourPickerCtrl, wxEmptyString, AsString, GetStringSetting) \
X(19, DSK_SETTING_BORDER_COLOR, DSK_SNI_COLOR_BORDER, _("Border color"), \
X(21, DSK_SETTING_BORDER_COLOR, DSK_SNI_COLOR_BORDER, _("Border color"), \
ColourPickerCtrl, wxEmptyString, AsString, GetStringSetting)
PLUGIN_BEGIN_NAMESPACE
Expand Down Expand Up @@ -126,6 +130,8 @@ class SimpleNumberInstrument : public Instrument {

/// Font used for the title of the instrument
wxFont m_title_font;
/// Font used for the suffix of the instrument value
wxFont m_suffix_font;
/// Font used for the body of the instrument
wxFont m_body_font;
/// SignalK fully quakified path whose value is to be displayed by the
Expand Down Expand Up @@ -154,6 +160,8 @@ class SimpleNumberInstrument : public Instrument {
/// (DSK_SNI_SMOOTHING_MAX-m_smoothing+1)*data(i+1)) /
/// (DSK_SNI_SMOOTHING_MAX + 1)
size_t m_smoothing;
/// add a suffix to the value if applicable
wxString m_value_suffix;
/// Previous value displayed by the instrument
double m_old_value;

Expand Down
6 changes: 6 additions & 0 deletions src/simplegaugeinstrument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ void SimpleGaugeInstrument::SetSetting(
i = wxAtoi(value);
#endif
SetSetting(key, i);
} else if (key.IsSameAs(DSK_SETTING_VALUE_SUFFIX)) {
m_value_suffix = value;
}
}

Expand Down Expand Up @@ -212,6 +214,7 @@ wxBitmap SimpleGaugeInstrument::RenderAngle(double scale, bool relative)
&& !m_supported_formats[m_format_index].StartsWith("ABS")) {
value.Prepend("-");
}
value = value.Append(m_value_suffix);
}
} else {
if (!m_timed_out && m_bmp.IsOk()) {
Expand Down Expand Up @@ -322,6 +325,7 @@ wxBitmap SimpleGaugeInstrument::RenderAdaptive(double scale)
&& !m_supported_formats[m_format_index].StartsWith("ABS")) {
value.Prepend("-");
}
value = value.Append(m_value_suffix);
}
} else {
if (!m_timed_out && m_bmp.IsOk()) {
Expand Down Expand Up @@ -502,6 +506,7 @@ wxBitmap SimpleGaugeInstrument::RenderFixed(double scale)
&& !m_supported_formats[m_format_index].StartsWith("ABS")) {
value.Prepend("-");
}
value = value.Append(m_value_suffix);
}
} else {
if (!m_timed_out && m_bmp.IsOk()) {
Expand Down Expand Up @@ -680,6 +685,7 @@ wxBitmap SimpleGaugeInstrument::RenderPercent(double scale)
&& !m_supported_formats[m_format_index].StartsWith("ABS")) {
value.Prepend("-");
}
value = value.Append(m_value_suffix);
}
} else {
if (!m_timed_out && m_bmp.IsOk()) {
Expand Down
37 changes: 30 additions & 7 deletions src/simplenumberinstrument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ void SimpleNumberInstrument::Init()
15, wxFONTFAMILY_SWISS, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL);
m_body_font
= wxFont(30, wxFONTFAMILY_SWISS, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_BOLD);
m_suffix_font
= wxFont(30, wxFONTFAMILY_SWISS, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_BOLD);
m_smoothing = 0;
m_old_value = std::numeric_limits<double>::min();

Expand Down Expand Up @@ -84,7 +86,8 @@ void SimpleNumberInstrument::SetSetting(
|| key.IsSameAs(DSK_SETTING_TRANSFORMATION)
|| key.IsSameAs(DSK_SETTING_SMOOTHING)
|| key.IsSameAs(DSK_SETTING_BODY_FONT)
|| key.IsSameAs(DSK_SETTING_TITLE_FONT)) {
|| key.IsSameAs(DSK_SETTING_TITLE_FONT)
|| key.IsSameAs(DSK_SETTING_SUFFIX_FONT)) {
// TODO: The above manually maintained list should be replaced with
// something using the information from the DSK_SNI_SETTINGS macro
int i;
Expand All @@ -96,6 +99,8 @@ void SimpleNumberInstrument::SetSetting(
i = wxAtoi(value);
#endif
SetSetting(key, i);
} else if (key.IsSameAs(DSK_SETTING_VALUE_SUFFIX)) {
m_value_suffix = value;
}
}

Expand All @@ -108,6 +113,8 @@ void SimpleNumberInstrument::SetSetting(const wxString& key, const int& value)
m_transformation = static_cast<transformation>(value);
} else if (key.IsSameAs(DSK_SETTING_TITLE_FONT)) {
m_title_font.SetPointSize(value);
} else if (key.IsSameAs(DSK_SETTING_SUFFIX_FONT)) {
m_suffix_font.SetPointSize(value);
} else if (key.IsSameAs(DSK_SETTING_BODY_FONT)) {
m_body_font.SetPointSize(value);
} else if (key.IsSameAs(DSK_SETTING_SMOOTHING)) {
Expand Down Expand Up @@ -225,21 +232,27 @@ wxBitmap SimpleNumberInstrument::Render(double scale)
"9999"); // dummy string to size the instrument consistently
wxCoord size_x, size_y;
wxCoord title_x, title_y;
wxCoord body_x, body_y;
wxCoord body_x, body_y, body_d;
wxCoord suffix_x, suffix_y, suffix_d;
wxCoord dummy_x, dummy_y;
wxMemoryDC mdc;

wxFont tf = m_title_font;
wxFont sf = m_suffix_font;
wxFont bf = m_body_font;
tf.SetPointSize(m_title_font.GetPointSize() * scale);
sf.SetPointSize(m_suffix_font.GetPointSize() * scale);
mdc.SetFont(tf);
mdc.GetTextExtent(m_title, &title_x, &title_y);
wxFont bf = m_body_font;
bf.SetPointSize(m_body_font.GetPointSize() * scale);
mdc.SetFont(bf);
mdc.GetTextExtent(dummy_str, &dummy_x, &dummy_y);
mdc.GetTextExtent(value, &body_x, &body_y);
size_x = (wxMax(title_x, wxMax(body_x, dummy_x)) + 2 * BORDER_SIZE);
size_y = (title_y + body_y + 4 * BORDER_SIZE);
mdc.GetTextExtent(value, &body_x, &body_y, &body_d);
mdc.GetTextExtent(
m_value_suffix, &suffix_x, &suffix_y, &suffix_d, NULL, &sf);
size_x
= wxMax(title_x, wxMax(body_x + suffix_x, dummy_x)) + 2 * BORDER_SIZE;
size_y = title_y + wxMax(body_y, suffix_y) + 4 * BORDER_SIZE;
#if defined(__WXGTK__) || defined(__WXQT__)
m_bmp = wxBitmap(size_x, size_y, 32);
#else
Expand All @@ -264,7 +277,17 @@ wxBitmap SimpleNumberInstrument::Render(double scale)
dc.DrawText(m_title, (size_x - title_x) / 2, BORDER_SIZE);
dc.SetFont(bf);
dc.SetTextForeground(cbf);
dc.DrawText(value, (size_x - body_x) / 2, title_y + 3 * BORDER_SIZE);
dc.DrawLabel(value,
wxRect((size_x - (body_x + suffix_x)) / 2,
title_y + 3 * BORDER_SIZE + wxMin(body_d, suffix_d), body_x,
wxMax(body_y, suffix_y) - suffix_d),
wxALIGN_CENTER_HORIZONTAL | wxALIGN_BOTTOM);
dc.SetFont(sf);
dc.DrawLabel(m_value_suffix,
wxRect((size_x - (body_x + suffix_x)) / 2 + body_x,
title_y + 3 * BORDER_SIZE + wxMin(body_d, suffix_d), suffix_x,
wxMax(body_y, suffix_y) - body_d),
wxALIGN_CENTER_HORIZONTAL | wxALIGN_BOTTOM);
dc.SetPen(wxPen(cb, BORDER_LINE_WIDTH, wxPENSTYLE_SOLID));
dc.SetBrush(*wxTRANSPARENT_BRUSH);
dc.DrawRectangle(BORDER_LINE_WIDTH / 2, BORDER_LINE_WIDTH / 2,
Expand Down

0 comments on commit 6980d72

Please sign in to comment.