Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show inidcators axis affiliation and allow changing label name and removing #253

Merged
merged 2 commits into from
Sep 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion chartfx-chart/src/main/java/de/gsi/chart/axes/Axis.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.ObservableList;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Paint;
import javafx.scene.text.Font;
Expand All @@ -17,7 +18,6 @@
import de.gsi.chart.axes.spi.TickMark;
import de.gsi.chart.ui.geometry.Side;
import de.gsi.dataset.AxisDescription;
import de.gsi.dataset.event.EventSource;
import de.gsi.dataset.event.UpdateEvent;

public interface Axis extends AxisDescription {
Expand Down Expand Up @@ -346,4 +346,9 @@ default void invokeListener(final UpdateEvent updateEvent, final boolean execute
* @return the primary unit label property
*/
DoubleProperty unitScalingProperty();

/**
* @return the canvas of the axis
*/
Canvas getCanvas();
}
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ public AxisLabelFormatter getAxisLabelFormatter() {
return axisFormatter.get();
}

@Override
public Canvas getCanvas() {
return canvas;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package de.gsi.chart.plugins;

import de.gsi.chart.axes.Axis;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.geometry.Bounds;
import javafx.scene.shape.Rectangle;

import de.gsi.chart.axes.Axis;

/**
* Plugin indicating a value range as a rectangle drawn on the plot area, with an optional {@link #textProperty() text
* label} describing the range.
Expand All @@ -22,15 +23,13 @@ public abstract class AbstractRangeValueIndicator extends AbstractValueIndicator
protected final Rectangle rectangle = new Rectangle(0, 0, 0, 0);

private final DoubleProperty lowerBound = new SimpleDoubleProperty(this, "lowerBound") {

@Override
protected void invalidated() {
layoutChildren();
}
};

private final DoubleProperty upperBound = new SimpleDoubleProperty(this, "upperBound") {

@Override
protected void invalidated() {
layoutChildren();
Expand All @@ -39,7 +38,6 @@ protected void invalidated() {

private final DoubleProperty labelHorizontalPosition = new SimpleDoubleProperty(this, "labelHorizontalPosition",
0.5) {

@Override
protected void invalidated() {
if (get() < 0 || get() > 1) {
Expand All @@ -50,7 +48,6 @@ protected void invalidated() {
};

private final DoubleProperty labelVerticalPosition = new SimpleDoubleProperty(this, "labelVerticalPosition", 0.5) {

@Override
protected void invalidated() {
if (get() < 0 || get() > 1) {
Expand Down Expand Up @@ -141,13 +138,13 @@ public final DoubleProperty labelVerticalPositionProperty() {
*/
protected void layout(final Bounds bounds) {
if (bounds.intersects(getChart().getCanvas().getBoundsInLocal())) {
layoutLabel(bounds, getLabelHorizontalPosition(), getLabelVerticalPosition());
rectangle.setX(bounds.getMinX());
rectangle.setY(bounds.getMinY());
rectangle.setWidth(bounds.getWidth());
rectangle.setHeight(bounds.getHeight());
addChildNodeIfNotPresent(rectangle);

layoutLabel(bounds, getLabelHorizontalPosition(), getLabelVerticalPosition());
} else {
getChartChildren().clear();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,17 @@ protected AbstractSingleValueIndicator(Axis axis, final double value, final Stri
editableIndicatorProperty().addListener((ch, o, n) -> updateMouseListener(n));
updateMouseListener(isEditable());

// remove triangle from chart foregrond when the chart is removed
chartProperty().addListener((p, o, n) -> {
if (o != null) {
o.getPlotForeground().getChildren().remove(triangle);
}
});

// Need to add them so that at initialization of the stage the CCS is
// applied and we can calculate label's
// width and height
getChartChildren().addAll(line, triangle, label);
getChartChildren().addAll(line, label);
this.value.addListener(
(ch, o, n) -> invokeListener(new UpdateEvent(this, "value changed to " + n + " for axis " + axis)));
}
Expand Down Expand Up @@ -146,22 +153,25 @@ private void initLine() {
pickLine.setStrokeWidth(getPickingDistance());
pickLine.mouseTransparentProperty().bind(editableIndicatorProperty().not());
pickLine.setOnMousePressed(mouseEvent -> {
/*
* Record a delta distance for the drag and drop operation. Because layoutLine() sets the start/end points
* we have to use these here. It is enough to use the start point. For X indicators, start x and end x are
* identical and for Y indicators start y and end y are identical.
*/
dragDelta.x = pickLine.getStartX() - mouseEvent.getX();
dragDelta.y = pickLine.getStartY() - mouseEvent.getY();
pickLine.setCursor(Cursor.MOVE);
mouseEvent.consume();
if (mouseEvent.isPrimaryButtonDown()) {
/*
* Record a delta distance for the drag and drop operation. Because layoutLine() sets the start/end points
* we have to use these here. It is enough to use the start point. For X indicators, start x and end x are
* identical and for Y indicators start y and end y are identical.
*/
dragDelta.x = pickLine.getStartX() - mouseEvent.getX();
dragDelta.y = pickLine.getStartY() - mouseEvent.getY();
pickLine.setCursor(Cursor.MOVE);
mouseEvent.consume();
}
});
}

private void initTriangle() {
triangle.visibleProperty().bind(editableIndicatorProperty());
triangle.mouseTransparentProperty().bind(editableIndicatorProperty().not());
triangle.setPickOnBounds(true);
triangle.setOpacity(0.7);
final double a = AbstractSingleValueIndicator.triangleHalfWidth;
triangle.getPoints().setAll(-a, -a, -a, +a, +a, +a, +a, -a);
triangle.setOnMousePressed(mouseEvent -> {
Expand Down Expand Up @@ -214,9 +224,8 @@ protected void layoutLine(final double startX, final double startY, final double
pickLine.setEndX(endX);
pickLine.setEndY(endY);

addChildNodeIfNotPresent(line);
addChildNodeIfNotPresent(pickLine);
// pickLine.toBack();
addChildNodeIfNotPresent(line);
}

/**
Expand All @@ -234,7 +243,13 @@ protected void layoutMarker(final double startX, final double startY, final doub

triangle.setTranslateX(startX);
triangle.setTranslateY(startY);
addChildNodeIfNotPresent(triangle);
triangle.toFront();
// triangle has to be put onto the plot foreground to be able to put it on top of axes
// is removed when the chart is changed
//addChildNodeIfNotPresent(triangle);
if (!getChart().getPlotForeground().getChildren().contains(triangle)) {
getChart().getPlotForeground().getChildren().add(triangle);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
import javafx.scene.Cursor;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.input.MouseButton;

import de.gsi.chart.Chart;
import de.gsi.chart.axes.Axis;
Expand All @@ -39,6 +42,7 @@ public abstract class AbstractValueIndicator extends ChartPlugin {
private double yOffset;

protected final Label label = new Label();
protected final TextField labelEdit = new TextField();
/* Difference between the mouse press position and the indicators center */
protected final Delta dragDelta = new Delta();

Expand Down Expand Up @@ -76,19 +80,49 @@ protected AbstractValueIndicator(Axis axis, final String text) {
this.axis = axis;
setText(text);

label.mouseTransparentProperty().bind(editableIndicatorProperty().not());
label.pickOnBoundsProperty().set(true);
label.toFront();

label.setOnMousePressed(mouseEvent -> {
/*
* Record a delta distance for the drag and drop operation. PROBLEM: At this point, we need to know the
* relative position of the label with respect to the indicator value.
*/
Point2D c = label.sceneToLocal(mouseEvent.getSceneX(), mouseEvent.getSceneY());
dragDelta.x = -(c.getX() + xOffset);
dragDelta.y = c.getY() + yOffset;
label.setCursor(Cursor.MOVE);
mouseEvent.consume();
if (mouseEvent.isPrimaryButtonDown() && isEditable()) {
/*
* Record a delta distance for the drag and drop operation. PROBLEM: At this point, we need to know the
* relative position of the label with respect to the indicator value.
*/
Point2D c = label.sceneToLocal(mouseEvent.getSceneX(), mouseEvent.getSceneY());
dragDelta.x = -(c.getX() + xOffset);
dragDelta.y = c.getY() + yOffset;
label.setCursor(Cursor.MOVE);
mouseEvent.consume();
}
});
// mouse handler to edit the indicator on right click
label.setOnMouseClicked(evt -> {
if (evt.getButton().equals(MouseButton.SECONDARY)) {
label.setVisible(false);
getChartChildren().add(labelEdit);
labelEdit.requestFocus();
labelEdit.setLayoutX(label.getLayoutX());
labelEdit.setLayoutY(label.getLayoutY());
labelEdit.resizeRelocate(label.getLayoutX() - 20, label.getLayoutY() - 5, label.getWidth() + 40, label.getHeight() + 10);
// change the label on exit
labelEdit.setOnAction(actionEvt -> {
label.setText(labelEdit.getText());
getChartChildren().remove(labelEdit);
label.setVisible(true);
});
labelEdit.setOnKeyPressed(keyEvt -> {
// remove the indicator when pressing ctl + delete
if (keyEvt.getCode().equals(KeyCode.DELETE) && keyEvt.isControlDown()) {
getChart().getPlugins().remove(this);
}
// restore the old label when pressing esc
if (keyEvt.getCode().equals(KeyCode.ESCAPE)) {
getChartChildren().remove(labelEdit);
label.setVisible(true);
}
});
}
});

editableIndicatorProperty().addListener((ch, o, n) -> updateMouseListener(n));
Expand Down Expand Up @@ -116,7 +150,7 @@ private void addAxisListener() {

protected void addChildNodeIfNotPresent(final Node node) {
if (!getChartChildren().contains(node)) {
getChartChildren().add(node);
getChartChildren().add(0, node); // add elements always at the bottom so they cannot steal focus
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import javafx.scene.input.MouseEvent;

import de.gsi.chart.axes.Axis;
import de.gsi.chart.ui.geometry.Side;
import de.gsi.dataset.event.EventSource;

/**
Expand Down Expand Up @@ -44,7 +45,6 @@ public XValueIndicator(final Axis axis, final double value) {
*/
public XValueIndicator(final Axis axis, final double value, final String text) {
super(axis, value, text);
triangle.getPoints().setAll(0.0, 0.0, -8.0, -8.0, 8.0, -8.0);
setLabelPosition(0.04);

pickLine.setOnMouseDragged(this::handleDragMouseEvent);
Expand Down Expand Up @@ -74,13 +74,23 @@ public void layoutChildren() {
final double maxX = plotAreaBounds.getMaxX();
final double minY = plotAreaBounds.getMinY();
final double maxY = plotAreaBounds.getMaxY();
final double xPos = minX + getChart().getFirstAxis(Orientation.HORIZONTAL).getDisplayPosition(getValue());
final double xPos = minX + getAxis().getDisplayPosition(getValue());
final double axisPos;
if (getAxis().getSide().equals(Side.BOTTOM)) {
triangle.getPoints().setAll(0.0, -8.0, -8.0, 0.0, 8.0, 0.0);
axisPos = getChart().getPlotForeground().sceneToLocal(getAxis().getCanvas().localToScene(0, 0)).getY() + 6;
} else {
triangle.getPoints().setAll(0.0, 0.0, -8.0, -8.0, 8.0, -8.0);
axisPos = getChart().getPlotForeground().sceneToLocal(getAxis().getCanvas().localToScene(0, getAxis().getHeight())).getY() - 6;
}
final double xPosGlobal = getChart().getPlotForeground().sceneToLocal(getChart().getCanvas().localToScene(xPos, 0)).getX();

if (xPos < minX || xPos > maxX) {
getChartChildren().clear();
} else {
layoutLine(xPos, minY, xPos, maxY);
layoutMarker(xPos, minY + 1.5 * AbstractSingleValueIndicator.triangleHalfWidth, xPos, maxY);
layoutMarker(xPosGlobal, axisPos + 4, xPos, maxY);

layoutLabel(new BoundingBox(xPos, minY, 0, maxY - minY), AbstractSingleValueIndicator.MIDDLE_POSITION,
getLabelPosition());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import javafx.scene.input.MouseEvent;

import de.gsi.chart.axes.Axis;
import de.gsi.chart.ui.geometry.Side;
import de.gsi.dataset.event.EventSource;

/**
Expand Down Expand Up @@ -49,7 +50,6 @@ public YValueIndicator(final Axis axis, final double value) {
*/
public YValueIndicator(final Axis axis, final double value, final String text) {
super(axis, value, text);
triangle.getPoints().setAll(0.0, 0.0, 8.0, 8.0, 8.0, -8.0);
setLabelHorizontalAnchor(HPos.RIGHT);
setLabelPosition(0.975);

Expand Down Expand Up @@ -81,13 +81,21 @@ public void layoutChildren() {
final double maxY = plotAreaBounds.getMaxY();

final double yPos = minY + getAxis().getDisplayPosition(getValue());
final double axisPos;
if (getAxis().getSide().equals(Side.RIGHT)) {
axisPos = getChart().getPlotForeground().sceneToLocal(getAxis().getCanvas().localToScene(0, 0)).getX() + 2;
triangle.getPoints().setAll(0.0, 0.0, 8.0, 8.0, 8.0, -8.0);
} else {
axisPos = getChart().getPlotForeground().sceneToLocal(getAxis().getCanvas().localToScene(getAxis().getWidth(), 0)).getX() - 2;
triangle.getPoints().setAll(0.0, 0.0, -8.0, 8.0, -8.0, -8.0);
}
final double yPosGlobal = getChart().getPlotForeground().sceneToLocal(getChart().getCanvas().localToScene(0, yPos)).getY();

if (yPos < minY || yPos > maxY) {
getChartChildren().clear();
} else {
layoutLine(minX, yPos, maxX, yPos);
layoutMarker(maxX - 1.5 * AbstractSingleValueIndicator.triangleHalfWidth, yPos, minX, yPos); // +
// 1.5*TRIANGLE_HALF_WIDTH
layoutMarker(axisPos, yPosGlobal, minX, yPos);
layoutLabel(new BoundingBox(minX, yPos, maxX - minX, 0), getLabelPosition(),
AbstractSingleValueIndicator.MIDDLE_POSITION);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,12 @@ public ValueIndicatorSelector(final ParameterMeasurements plugin, final AxisMode
if (plugin.getChart() != null) {
plugin.getChart().getPlugins().addListener(pluginsChanged);
plugin.getChart().getPlugins().forEach(this::addNewIndicators);
reuseIndicators.setSelected(plugin.getChart().getAxes().size() <= 2);
}

final Label label = new Label("re-use inidcators: ");
GridPane.setConstraints(label, 0, 0);
GridPane.setConstraints(reuseIndicators, 1, 0);
reuseIndicators.selectedProperty().addListener((ch, o, n) -> indicatorListView.setDisable(!n));

indicatorListView.disableProperty().bind(reuseIndicators.selectedProperty().not());
indicatorListView.setOrientation(Orientation.VERTICAL);
indicatorListView.setPrefSize(-1, DEFAULT_SELECTOR_HEIGHT);
indicatorListView.setCellFactory(list -> new DataSelectorLabel());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import java.util.List;

import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
Expand Down Expand Up @@ -374,5 +375,10 @@ public boolean isValueOnAxis(double value) {
public void requestAxisLayout() {
// deliberately not implemented
}

@Override
public Canvas getCanvas() {
return null;
}
}
}
Loading