diff --git a/app/display/model/src/main/java/org/csstudio/display/builder/model/Messages.java b/app/display/model/src/main/java/org/csstudio/display/builder/model/Messages.java index ceddb44768..1207ed334d 100644 --- a/app/display/model/src/main/java/org/csstudio/display/builder/model/Messages.java +++ b/app/display/model/src/main/java/org/csstudio/display/builder/model/Messages.java @@ -252,6 +252,7 @@ public class Messages WidgetProperties_Insets, WidgetProperties_Interactive, WidgetProperties_Interpolation, + WidgetProperties_InvertedScale, WidgetProperties_ItemsFromPV, WidgetProperties_KnobColor, WidgetProperties_KnobPosition, diff --git a/app/display/model/src/main/java/org/csstudio/display/builder/model/widgets/TankWidget.java b/app/display/model/src/main/java/org/csstudio/display/builder/model/widgets/TankWidget.java index 48666c55da..1c9ef78915 100644 --- a/app/display/model/src/main/java/org/csstudio/display/builder/model/widgets/TankWidget.java +++ b/app/display/model/src/main/java/org/csstudio/display/builder/model/widgets/TankWidget.java @@ -105,6 +105,13 @@ public Widget createWidget() public static final WidgetPropertyDescriptor propOppositeScaleVisible = newBooleanPropertyDescriptor(WidgetPropertyCategory.DISPLAY, "opposite_scale_visible", Messages.WidgetProperties_OppositeScaleVisible); + /** 'inverted_scale' — run the scale top-down (right to left + * when horizontal), with the tank filling from the top. + * A log scale always runs bottom-up. + */ + public static final WidgetPropertyDescriptor propInvertedScale = + newBooleanPropertyDescriptor(WidgetPropertyCategory.DISPLAY, "inverted_scale", Messages.WidgetProperties_InvertedScale); + /** Widget configurator to read legacy *.opi files*/ private static class CustomConfigurator extends WidgetConfigurator { @@ -171,6 +178,7 @@ public WidgetConfigurator getConfigurator(final Version persisted_version) private volatile WidgetProperty perpendicular_tick_labels; private volatile WidgetProperty opposite_scale_visible; private volatile WidgetProperty log_scale; + private volatile WidgetProperty invertedScale; private volatile WidgetProperty horizontal; private volatile WidgetProperty border_width_prop; @@ -195,6 +203,7 @@ protected void defineProperties(final List> properties) properties.add(show_minor_ticks = propShowMinorTicks.createProperty(this, true)); properties.add(perpendicular_tick_labels = propPerpendicularTickLabels.createProperty(this, false)); properties.add(log_scale = propLogscale.createProperty(this, false)); + properties.add(invertedScale = propInvertedScale.createProperty(this, false)); properties.add(horizontal = propHorizontal.createProperty(this, false)); properties.add(border_width_prop = propTankBorderWidth.createProperty(this, 0)); } @@ -268,6 +277,12 @@ public WidgetProperty propLogScale() return log_scale; } + /** @return 'inverted_scale' property */ + public WidgetProperty propInvertedScale() + { + return invertedScale; + } + /** @return 'horizontal' property */ public WidgetProperty propHorizontal() { diff --git a/app/display/model/src/main/resources/examples/monitors_tank.bob b/app/display/model/src/main/resources/examples/monitors_tank.bob index 8eb073d847..3926ca80de 100644 --- a/app/display/model/src/main/resources/examples/monitors_tank.bob +++ b/app/display/model/src/main/resources/examples/monitors_tank.bob @@ -26,8 +26,9 @@ 50 310 false - 102.0 - -2.0 + -2.0 + 102.0 + true Label diff --git a/app/display/model/src/main/resources/org/csstudio/display/builder/model/messages.properties b/app/display/model/src/main/resources/org/csstudio/display/builder/model/messages.properties index c2f634193a..0a71e4c403 100644 --- a/app/display/model/src/main/resources/org/csstudio/display/builder/model/messages.properties +++ b/app/display/model/src/main/resources/org/csstudio/display/builder/model/messages.properties @@ -238,6 +238,7 @@ WidgetProperties_InitialIndex=Initial Index WidgetProperties_Insets=Insets WidgetProperties_Interactive=Interactive WidgetProperties_Interpolation=Interpolation +WidgetProperties_InvertedScale=Inverted Scale WidgetProperties_ItemsFromPV=Items from PV WidgetProperties_KnobColor=Knob Color WidgetProperties_KnobPosition=Knob Position diff --git a/app/display/model/src/main/resources/org/csstudio/display/builder/model/messages_fr.properties b/app/display/model/src/main/resources/org/csstudio/display/builder/model/messages_fr.properties index 4777d2579f..4de6faaca0 100644 --- a/app/display/model/src/main/resources/org/csstudio/display/builder/model/messages_fr.properties +++ b/app/display/model/src/main/resources/org/csstudio/display/builder/model/messages_fr.properties @@ -238,6 +238,7 @@ WidgetProperties_InitialIndex=Index initial WidgetProperties_Insets=Marges WidgetProperties_Interactive=Interactif WidgetProperties_Interpolation=Interpolation +WidgetProperties_InvertedScale=\u00C9chelle invers\u00E9e WidgetProperties_ItemsFromPV=\u00C9l\u00E9ments de la PV WidgetProperties_KnobColor=Couleur du bouton WidgetProperties_KnobPosition=Position du bouton diff --git a/app/display/model/src/test/java/org/csstudio/display/builder/model/widgets/TankWidgetUnitTest.java b/app/display/model/src/test/java/org/csstudio/display/builder/model/widgets/TankWidgetUnitTest.java index f544fa699e..b83aa0a917 100644 --- a/app/display/model/src/test/java/org/csstudio/display/builder/model/widgets/TankWidgetUnitTest.java +++ b/app/display/model/src/test/java/org/csstudio/display/builder/model/widgets/TankWidgetUnitTest.java @@ -81,6 +81,7 @@ public void testTankWidgetDefaults() assertThat(tank.propFormat().getValue(), equalTo(ScaleFormat.DEFAULT)); assertThat(tank.propPrecision().getValue(), equalTo(2)); assertThat(tank.propLogScale().getValue(), equalTo(false)); + assertThat(tank.propInvertedScale().getValue(), equalTo(false)); assertThat(tank.propHorizontal().getValue(), equalTo(false)); assertThat(tank.propBorderWidth().getValue(), equalTo(0)); } @@ -140,6 +141,7 @@ public void testXmlRoundTrip() throws Exception original.propPerpendicularTickLabels().setValue(true); original.propFormat().setValue(ScaleFormat.DECIMAL); original.propPrecision().setValue(3); + original.propInvertedScale().setValue(true); // Serialize — disable skip_defaults so ALL properties appear final DisplayModel model = new DisplayModel(); @@ -165,6 +167,7 @@ public void testXmlRoundTrip() throws Exception assertThat(xml, containsString("")); assertThat(xml, containsString("")); assertThat(xml, containsString("")); + assertThat(xml, containsString("")); // Deserialize final ModelReader reader = new ModelReader(new ByteArrayInputStream(xml.getBytes())); @@ -186,6 +189,7 @@ public void testXmlRoundTrip() throws Exception assertThat(tank.propPerpendicularTickLabels().getValue(), equalTo(true)); assertThat(tank.propFormat().getValue(), equalTo(ScaleFormat.DECIMAL)); assertThat(tank.propPrecision().getValue(), equalTo(3)); + assertThat(tank.propInvertedScale().getValue(), equalTo(true)); } /** Verify that XML produced by this version can be read by a stock @@ -213,5 +217,6 @@ public void testNewPropertiesAreOptional() throws Exception assertThat(xml, not(containsString(""))); assertThat(xml, not(containsString(""))); assertThat(xml, not(containsString(""))); + assertThat(xml, not(containsString(""))); } } diff --git a/app/display/representation-javafx/src/main/java/org/csstudio/display/builder/representation/javafx/widgets/TankRepresentation.java b/app/display/representation-javafx/src/main/java/org/csstudio/display/builder/representation/javafx/widgets/TankRepresentation.java index 8e3d373914..f072395a69 100644 --- a/app/display/representation-javafx/src/main/java/org/csstudio/display/builder/representation/javafx/widgets/TankRepresentation.java +++ b/app/display/representation-javafx/src/main/java/org/csstudio/display/builder/representation/javafx/widgets/TankRepresentation.java @@ -75,6 +75,7 @@ protected void registerListeners() model_widget.propLimitsFromPV().addUntypedPropertyListener(valueListener); model_widget.propMinimum().addUntypedPropertyListener(valueListener); model_widget.propMaximum().addUntypedPropertyListener(valueListener); + model_widget.propInvertedScale().addUntypedPropertyListener(valueListener); model_widget.runtimePropValue().addUntypedPropertyListener(valueListener); // Alarm limits; only need re-evaluation when limit properties change. // When alarm_limits_from_pv=true, valueChanged() calls applyAlarmLimits() too. @@ -114,6 +115,7 @@ protected void unregisterListeners() model_widget.propLimitsFromPV().removePropertyListener(valueListener); model_widget.propMinimum().removePropertyListener(valueListener); model_widget.propMaximum().removePropertyListener(valueListener); + model_widget.propInvertedScale().removePropertyListener(valueListener); model_widget.runtimePropValue().removePropertyListener(valueListener); model_widget.propShowAlarmLimits().removePropertyListener(limitsListener); model_widget.propAlarmLimitsFromPV().removePropertyListener(limitsListener); @@ -151,7 +153,9 @@ private void valueChanged(final WidgetProperty property, final Object old_val max_val = display_info.getDisplayRange().getMaximum(); } } - tank.setRange(min_val, max_val); + // Displays from before 'inverted_scale' invert the scale with minimum > maximum + tank.setInverted(model_widget.propInvertedScale().getValue() || min_val > max_val); + tank.setRange(Math.min(min_val, max_val), Math.max(min_val, max_val)); // Alarm metadata is embedded in the VType, so re-check it on every update. // When using widget-configured limits, limitsChanged() handles updates instead. diff --git a/app/rtplot/src/main/java/org/csstudio/javafx/rtplot/RTTank.java b/app/rtplot/src/main/java/org/csstudio/javafx/rtplot/RTTank.java index 8c294c7b05..68c13f9435 100644 --- a/app/rtplot/src/main/java/org/csstudio/javafx/rtplot/RTTank.java +++ b/app/rtplot/src/main/java/org/csstudio/javafx/rtplot/RTTank.java @@ -97,6 +97,20 @@ public class RTTank extends Canvas /** Current value, i.e. fill level */ private volatile double value = 5.0; + /** Guards rangeLow, rangeHigh and inverted, which are set from + * different threads and pushed to both scales together + */ + private final Object rangeLock = new Object(); + + /** Value range from {@link #setRange}, always ascending. + * Starts as the initial range of the scales. + */ + private double rangeLow = 0.0; + private double rangeHigh = 10.0; + + /** Does the scale run top-down, filling the tank from the top? */ + private boolean inverted = false; + /** Does layout need to be re-computed? */ protected final AtomicBoolean need_layout = new AtomicBoolean(true); @@ -271,8 +285,15 @@ public void setScaleVisible (boolean visible) /** @param logscale Use log scale for y-axis? */ public void setLogScale(final boolean logscale) { - scale.setLogarithmic(logscale); - right_scale.setLogarithmic(logscale); + synchronized (rangeLock) + { // A log scale only takes an ascending range, + // so the range changes before the scales switch to log + if (logscale) + applyRange(true); + scale.setLogarithmic(logscale); + right_scale.setLogarithmic(logscale); + applyRange(logscale); + } requestUpdate(); } @@ -442,78 +463,168 @@ public void setPerpendicularTickLabels(final boolean perpendicular) } /** Set value range + * + *

To run the scale top-down, use {@link #setInverted}. + * * @param low Lower limit - * @param high Upper limit + * @param high Upper limit, must be above the lower limit */ public void setRange(final double low, final double high) { // Guard against NaN, Infinite, or inverted/flat range if (!Double.isFinite(low) || !Double.isFinite(high) || low >= high) return; - scale.setValueRange(low, high); - right_scale.setValueRange(low, high); + synchronized (rangeLock) + { + rangeLow = low; + rangeHigh = high; + applyRange(scale.isLogarithmic()); + } + } + + /** Run the scale top-down, with the lower limit at the top + * and the tank filling from the top. + * + *

A logarithmic scale always runs bottom-up. + * + * @param inverted Invert the scale? + */ + public void setInverted(final boolean inverted) + { + synchronized (rangeLock) + { + if (this.inverted == inverted) + return; + this.inverted = inverted; + applyRange(scale.isLogarithmic()); + } + requestUpdate(); + } + + /** @return Value range of the scale, from bottom to top */ + AxisRange getValueRange() + { + return scale.getValueRange(); } - /** @param value Set value */ + /** Push the range to both scales, high to low when inverted. + * Caller holds {@link #rangeLock}. + * @param logscale Is the range for a log scale, which only runs bottom-up? + */ + private void applyRange(final boolean logscale) + { + final boolean topDown = inverted && !logscale; + final double bottom = topDown ? rangeHigh : rangeLow; + final double top = topDown ? rangeLow : rangeHigh; + scale.setValueRange(bottom, top); + right_scale.setValueRange(bottom, top); + } + + /** @param value Set value; a non-finite value shows an empty tank */ public void setValue(final double value) { if (Double.isFinite(value)) this.value = value; else - this.value = scale.getValueRange().getLow(); + { + final AxisRange range = scale.getValueRange(); + this.value = Math.min(range.getLow(), range.getHigh()); + } requestUpdate(); } - /** Map a value to a Y pixel within the plot bounds (low value at bottom). - * Returns -1 when the mapping is undefined (e.g. log scale with non-positive inputs). + /** Value range of the scale as drawn in one image + * @param min Lower value + * @param max Upper value, above min + * @param logscale Is the scale logarithmic? + * @param normal Does the scale run bottom-up? Otherwise min is at the top */ - private int valueToY(final Rectangle pb, final double min, final double max, - final double v, final boolean logscale) + record DrawnRange(double min, double max, boolean logscale, boolean normal) { - final double frac; - if (logscale) + /** Map a value to a Y pixel within the plot bounds + * @param pb Plot bounds + * @param v Value + * @return Y pixel, or -1 when the mapping is undefined + * (log scale with non-positive inputs) + */ + int valueToY(final Rectangle pb, final double v) { - if (min <= 0 || max <= 0 || v <= 0) - return -1; - frac = Math.log(v / min) / Math.log(max / min); + final double frac; + if (logscale) + { + if (min <= 0 || max <= 0 || v <= 0) + return -1; + frac = Math.log(v / min) / Math.log(max / min); + } + else + frac = (v - min) / (max - min); + return (int) (pb.y + pb.height * (normal ? 1.0 - frac : frac)); } - else - frac = (v - min) / (max - min); - return (int) (pb.y + pb.height * (1.0 - frac)); } - /** Draw a single horizontal limit line across the tank area at the given value. */ - private void drawLimitLineAt(final Graphics2D gc, final Rectangle pb, - final double min, final double max, - final double limit, final Color color) + /** Draw a single horizontal limit line across the tank area at the given value. + * @param gc Graphics context + * @param pb Plot bounds + * @param range Value range as drawn + * @param limit Limit value; nothing is drawn outside the range + * @param color Line color + */ + private void drawLimitLineAt(final Graphics2D gc, final Rectangle pb, final DrawnRange range, + final double limit, final Color color) { - if (!Double.isFinite(limit) || limit <= min || limit >= max) + if (!Double.isFinite(limit) || limit <= range.min() || limit >= range.max()) return; - final int y = valueToY(pb, min, max, limit, scale.isLogarithmic()); + final int y = range.valueToY(pb, limit); if (y < pb.y || y > pb.y + pb.height) return; gc.setColor(color); gc.drawLine(pb.x, y, pb.x + pb.width, y); } + /** Draw the alarm and warning limit lines over the tank body, + * solid for limits from the PV, dashed for limits from the widget + * @param gc Graphics context + * @param pb Plot bounds + * @param range Value range as drawn + */ + private void drawLimitLines(final Graphics2D gc, final Rectangle pb, final DrawnRange range) + { + final double lolo = limit_lolo; + final double lo = limit_lo; + final double hi = limit_hi; + final double hihi = limit_hihi; + if (Double.isNaN(lolo) && Double.isNaN(lo) && + Double.isNaN(hi) && Double.isNaN(hihi)) + return; + if (limits_from_pv) + gc.setStroke(new BasicStroke(2f)); + else + gc.setStroke(new BasicStroke(2f, BasicStroke.CAP_BUTT, + BasicStroke.JOIN_MITER, 10f, new float[]{6f, 4f}, 0f)); + drawLimitLineAt(gc, pb, range, lolo, limit_major_color); + drawLimitLineAt(gc, pb, range, lo, limit_minor_color); + drawLimitLineAt(gc, pb, range, hi, limit_minor_color); + drawLimitLineAt(gc, pb, range, hihi, limit_major_color); + gc.setStroke(new BasicStroke(1f)); + } + /** Compute the fill height in pixels for the current value. * Handles both linear and logarithmic scales. * * @param plotHeight Pixel height of the plot area - * @param min Scale minimum (< max) - * @param max Scale maximum + * @param range Value range as drawn * @param current Current PV value - * @param logscale Whether the scale uses log spacing * @return Fill level in pixels: 0 = empty, plotHeight = full */ - private static int computeFillLevel(final int plotHeight, final double min, final double max, - final double current, final boolean logscale) + private static int computeFillLevel(final int plotHeight, final DrawnRange range, final double current) { + final double min = range.min(); + final double max = range.max(); if (current <= min) return 0; if (current >= max) return plotHeight; - if (logscale) // by mellguth2, https://github.com/ControlSystemStudio/phoebus/issues/2726 + if (range.logscale()) // by mellguth2, https://github.com/ControlSystemStudio/phoebus/issues/2726 { // Refuse to map if any input is non-positive (log undefined) if (min <= 0 || max <= 0 || current <= 0) return 0; @@ -605,8 +716,9 @@ protected Image updateImageBuffer() final boolean normal = range.getLow() <= range.getHigh(); final double min = Math.min(range.getLow(), range.getHigh()); final double max = Math.max(range.getLow(), range.getHigh()); + final DrawnRange drawn = new DrawnRange(min, max, scale.isLogarithmic(), normal); final double current = value; - final int level = computeFillLevel(plot_bounds.height, min, max, current, scale.isLogarithmic()); + final int level = computeFillLevel(plot_bounds.height, drawn, current); final int arc = Math.min(plot_bounds.width, plot_bounds.height) / 10; gc.setPaint(new GradientPaint(plot_bounds.x, 0, empty, plot_bounds.x+plot_bounds.width/2, 0, empty_shadow, true)); @@ -637,25 +749,7 @@ protected Image updateImageBuffer() gc.setStroke(new BasicStroke(1f)); } - // Draw alarm / warning limit lines over the tank body - final double lim_lolo = limit_lolo; - final double lim_lo = limit_lo; - final double lim_hi = limit_hi; - final double lim_hihi = limit_hihi; - if (normal && (!Double.isNaN(lim_lolo) || !Double.isNaN(lim_lo) || - !Double.isNaN(lim_hi) || !Double.isNaN(lim_hihi))) - { - if (limits_from_pv) - gc.setStroke(new BasicStroke(2f)); - else - gc.setStroke(new BasicStroke(2f, BasicStroke.CAP_BUTT, - BasicStroke.JOIN_MITER, 10f, new float[]{6f, 4f}, 0f)); - drawLimitLineAt(gc, plot_bounds, min, max, lim_lolo, limit_major_color); - drawLimitLineAt(gc, plot_bounds, min, max, lim_lo, limit_minor_color); - drawLimitLineAt(gc, plot_bounds, min, max, lim_hi, limit_minor_color); - drawLimitLineAt(gc, plot_bounds, min, max, lim_hihi, limit_major_color); - gc.setStroke(new BasicStroke(1f)); - } + drawLimitLines(gc, plot_bounds, drawn); gc.dispose(); diff --git a/app/rtplot/src/test/java/org/csstudio/javafx/rtplot/RTTankTest.java b/app/rtplot/src/test/java/org/csstudio/javafx/rtplot/RTTankTest.java index c9e5177051..337fdb5b7a 100644 --- a/app/rtplot/src/test/java/org/csstudio/javafx/rtplot/RTTankTest.java +++ b/app/rtplot/src/test/java/org/csstudio/javafx/rtplot/RTTankTest.java @@ -7,6 +7,8 @@ *******************************************************************************/ package org.csstudio.javafx.rtplot; +import java.awt.Rectangle; + import org.junit.jupiter.api.Test; import org.phoebus.ui.vtype.ScaleFormat; @@ -50,6 +52,55 @@ public void testSetRangeRejectsInvalid() tank.setRange(100, 100); // flat tank.setRange(100, 0); // inverted tank.setRange(Double.POSITIVE_INFINITY, 100); + // Initial range of the scale is unchanged + assertThat(tank.getValueRange().getLow(), equalTo(0.0)); + assertThat(tank.getValueRange().getHigh(), equalTo(10.0)); + } + + /** An inverted scale runs top-down, except on a log scale */ + @Test + public void testInvertedScale() + { + final RTTank tank = new RTTank(); + tank.setRange(1, 100); + tank.setInverted(true); + assertThat(tank.getValueRange().getLow(), equalTo(100.0)); + assertThat(tank.getValueRange().getHigh(), equalTo(1.0)); + + // Inverted scale keeps the order when the range changes + tank.setRange(2, 50); + assertThat(tank.getValueRange().getLow(), equalTo(50.0)); + assertThat(tank.getValueRange().getHigh(), equalTo(2.0)); + + tank.setLogScale(true); + assertThat(tank.getValueRange().getLow(), equalTo(2.0)); + assertThat(tank.getValueRange().getHigh(), equalTo(50.0)); + + tank.setLogScale(false); + assertThat(tank.getValueRange().getLow(), equalTo(50.0)); + assertThat(tank.getValueRange().getHigh(), equalTo(2.0)); + + tank.setInverted(false); + assertThat(tank.getValueRange().getLow(), equalTo(2.0)); + assertThat(tank.getValueRange().getHigh(), equalTo(50.0)); + } + + /** Values map to pixels bottom-up, or top-down when inverted */ + @Test + public void testDrawnRangeValueToY() + { + final Rectangle plot = new Rectangle(0, 10, 20, 100); + final RTTank.DrawnRange normal = new RTTank.DrawnRange(0, 100, false, true); + assertThat(normal.valueToY(plot, 0), equalTo(110)); + assertThat(normal.valueToY(plot, 25), equalTo(85)); + + final RTTank.DrawnRange inverted = new RTTank.DrawnRange(0, 100, false, false); + assertThat(inverted.valueToY(plot, 0), equalTo(10)); + assertThat(inverted.valueToY(plot, 25), equalTo(35)); + + final RTTank.DrawnRange log = new RTTank.DrawnRange(1, 100, true, true); + assertThat(log.valueToY(plot, 10), equalTo(60)); + assertThat(log.valueToY(plot, 0), equalTo(-1)); } /** setValue should handle NaN and Infinity */