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

Introducing chart tools manager including new crosshair tool #3466

Closed
wants to merge 2 commits into from
Closed
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
Binary file added name.abuchen.portfolio.ui/icons/crosshair_off.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added name.abuchen.portfolio.ui/icons/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added name.abuchen.portfolio.ui/icons/crosshair_on.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added name.abuchen.portfolio.ui/icons/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ public enum Images
CLOUD("cloud.png"), //$NON-NLS-1$
MEASUREMENT_ON("measurement_on.png"), //$NON-NLS-1$
MEASUREMENT_OFF("measurement_off.png"), //$NON-NLS-1$
CROSSHAIR_ON("crosshair_on.png"), //$NON-NLS-1$
CROSSHAIR_OFF("crosshair_off.png"), //$NON-NLS-1$

// views

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,7 @@ public class Messages extends NLS
public static String LabelConvertBuySellIntoDeliveryTransactions;
public static String LabelCopyToClipboard;
public static String LabelCountry;
public static String LabelCrosshair;
public static String LabelCurrencies;
public static String LabelCurrencyConverter;
public static String LabelCurrentDate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1192,6 +1192,8 @@ LabelCopyToClipboard = Copy to Clipboard

LabelCountry = Country

LabelCrosshair=Crosshair

LabelCurrencies = Currencies

LabelCurrencyConverter = Currency Converter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1186,6 +1186,8 @@ LabelCopyToClipboard = Zkop\u00EDrovat do schr\u00E1nky

LabelCountry = Zem\u011B

LabelCrosshair = Crosshair

LabelCurrencies = M\u011Bny

LabelCurrencyConverter = P\u0159evodn\u00EDk m\u011Bn
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1185,6 +1185,8 @@ LabelCopyToClipboard = Kopier til udklipsholder

LabelCountry = Land

LabelCrosshair = Tr\u00E5dkors

LabelCurrencies = Valutaer

LabelCurrencyConverter = Valuta
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1185,6 +1185,8 @@ LabelCopyToClipboard = In Zwischenablage kopieren

LabelCountry = Land

LabelCrosshair = Fadenkreuz

LabelCurrencies = W\u00E4hrungen

LabelCurrencyConverter = W\u00E4hrungsrechner
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1185,6 +1185,8 @@ LabelCopyToClipboard = Copiar al portapapeles

LabelCountry = Pa\u00EDs

LabelCrosshair = Crosshair

LabelCurrencies = Monedas

LabelCurrencyConverter = Conversor de Moneda
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1186,6 +1186,8 @@ LabelCopyToClipboard = Copier dans le presse-papier

LabelCountry = Pays

LabelCrosshair = R\u00E9ticule

LabelCurrencies = Devises

LabelCurrencyConverter = Convertisseur de devise
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1185,6 +1185,8 @@ LabelCopyToClipboard = Copia negli appunti

LabelCountry = Nazione

LabelCrosshair = Mirino

LabelCurrencies = Valute

LabelCurrencyConverter = Convertitore valuta
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1185,6 +1185,8 @@ LabelCopyToClipboard = Kopi\u00EBren naar klembord

LabelCountry = Land

LabelCrosshair = Dradenkruis

LabelCurrencies = Valuta

LabelCurrencyConverter = Valuta Converter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1185,6 +1185,8 @@ LabelCopyToClipboard = Skopiuj do schowka

LabelCountry = Kraj

LabelCrosshair = Celownik

LabelCurrencies = Waluty

LabelCurrencyConverter = Przelicznik walut
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1185,6 +1185,8 @@ LabelCopyToClipboard = Copiar para a \u00E1rea de transfer\u00EAncia

LabelCountry = Pa\u00EDs

LabelCrosshair = Mira

LabelCurrencies = Moedas

LabelCurrencyConverter = Conversor de Moedas
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1185,6 +1185,8 @@ LabelCopyToClipboard = \u041A\u043E\u043F\u0438\u0440\u043E\u0432\u0430\u0442\u0

LabelCountry = \u0421\u0442\u0440\u0430\u043D\u0430

LabelCrosshair = \u041F\u0435\u0440\u0435\u043A\u0440\u0435\u0441\u0442\u0438\u0435

LabelCurrencies = \u0412\u0430\u043B\u044E\u0442\u044B

LabelCurrencyConverter = \u041A\u043E\u043D\u0432\u0435\u0440\u0442\u0435\u0440 \u0432\u0430\u043B\u044E\u0442
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1185,6 +1185,8 @@ LabelCopyToClipboard = Kop\u00EDrovanie do schr\u00E1nky

LabelCountry = Krajina

LabelCrosshair = Kri\u017Eovatka

LabelCurrencies = Meny

LabelCurrencyConverter = Konverzn\u00E1 kalkula\u010Dka
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1185,6 +1185,8 @@ LabelCopyToClipboard = \u590D\u5236\u81F3\u526A\u8D34\u677F

LabelCountry = \u56FD\u5BB6\u6216\u5730\u533A

LabelCrosshair = \u5341\u5B57\u51C6\u7EBF

LabelCurrencies = \u8D27\u5E01

LabelCurrencyConverter = \u6C47\u7387\u8F6C\u6362\u5668
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ else if (MEASUREMENT_COLOR.equalsIgnoreCase(property)
&& chart instanceof TimelineChart timelineChart)
{
Color newColor = (Color) engine.convert(value, Color.class, control.getDisplay());
timelineChart.getMeasurementTool().setColor(newColor);
timelineChart.getChartToolsManager().setColor(newColor);
}
}

Expand Down Expand Up @@ -116,7 +116,7 @@ else if (BACKGROUND_COLOR.equalsIgnoreCase(property))
else if (MEASUREMENT_COLOR.equalsIgnoreCase(property) && chart instanceof TimelineChart timelineChart)
{
ICSSValueConverter cssValueConverter = engine.getCSSValueConverter(String.class);
return cssValueConverter.convert(timelineChart.getMeasurementTool().getColor(), engine, null);
return cssValueConverter.convert(timelineChart.getChartToolsManager().getColor(), engine, null);
}

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ private void configMenuAboutToShow(IMenuManager manager)
{
if (chart instanceof TimelineChart timelineChart)
{
timelineChart.getMeasurementTool().addContextMenu(manager);
timelineChart.getChartToolsManager().addContextMenu(manager);
manager.add(new Separator());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
package name.abuchen.portfolio.ui.util.chart;

import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.List;
import java.util.function.BiFunction;

import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Event;

import name.abuchen.portfolio.ui.Images;
import name.abuchen.portfolio.ui.Messages;
import name.abuchen.portfolio.ui.util.Colors;
import name.abuchen.portfolio.ui.util.SimpleAction;
import name.abuchen.portfolio.util.Pair;

public class ChartToolsManager
{
interface ChartTool
{
void onMouseDown(Event e);

void onMouseMove(Event e);

void onMouseUp(Event e);

void paintControl(PaintEvent e);
}

public enum ChartTools
{
NONE("", (ch, co) -> null, null, null), // //$NON-NLS-1$
CROSSHAIR(Messages.LabelCrosshair, CrosshairTool::new, Images.CROSSHAIR_ON, Images.CROSSHAIR_OFF), //
MEASUREMENT(Messages.LabelMeasureDistance, MeasurementTool::new, Images.MEASUREMENT_ON, Images.MEASUREMENT_OFF); //

private String actionText;
private BiFunction<TimelineChart, Color, ChartTool> createToolFactory;
private Images imageON;
private Images imageOFF;

private ChartTools(String actionText,
BiFunction<TimelineChart, Color, ChartTool> createToolFactory,
Images imageON,
Images imageOFF)
{
this.actionText = actionText;
this.createToolFactory = createToolFactory;
this.imageON = imageON;
this.imageOFF = imageOFF;
}

String getActionText()
{
return actionText;
}

Images getImageOFF()
{
return imageOFF;
}

Images getImageON()
{
return imageON;
}

ChartTool createTool(TimelineChart chart, Color color)
{
return createToolFactory.apply(chart, color);
}
}

record Spot(int time, LocalDate date, double xCoordinate, double value)
{
public static Spot from(Event e, TimelineChart chart)
{
double xCoordinate = chart.getAxisSet().getXAxis(0).getDataCoordinate(e.x);
LocalDate date = Instant.ofEpochMilli((long) xCoordinate).atZone(ZoneId.systemDefault()).toLocalDate();
double value = chart.getAxisSet().getYAxis(0).getDataCoordinate(e.y);

return new Spot(e.time, date, xCoordinate, value);
}

public Point toPoint(TimelineChart chart)
{
return new Point(chart.getAxisSet().getXAxis(0).getPixelCoordinate(xCoordinate),
chart.getAxisSet().getYAxis(0).getPixelCoordinate(value));
}
}

public static final int PADDING = 5;
private ChartTools activeTool = ChartTools.NONE;
private ChartTool currentTool = null;
private Color color = Colors.BLACK;
private TimelineChart chart;
private ArrayList<Pair<ChartTools, IAction>> buttons = new ArrayList<>();

public ChartToolsManager(TimelineChart chart)
{
this.chart = chart;
chart.getPlotArea().addListener(SWT.MouseDown, this::onMouseDown);
chart.getPlotArea().addListener(SWT.MouseMove, this::onMouseMove);
chart.getPlotArea().addListener(SWT.MouseUp, this::onMouseUp);

chart.getPlotArea().addPaintListener(this::paintControl);
}

private void onMouseDown(Event e)
{
if (currentTool == null)
return;

currentTool.onMouseDown(e);
}

private void onMouseMove(Event e)
{
if (currentTool == null)
return;

currentTool.onMouseMove(e);
}

private void onMouseUp(Event e)
{
if (currentTool == null)
return;

currentTool.onMouseUp(e);
}

private void paintControl(PaintEvent e)
{
if (currentTool == null)
return;

currentTool.paintControl(e);
}

public Color getColor()
{
return color;
}

public void setColor(Color color)
{
this.color = color;
}

public void addButtons(ToolBarManager toolBar)
{
createActions().forEach(p -> {
toolBar.add(p.getRight());
buttons.add(p);
});
}

private List<Pair<ChartTools, IAction>> createActions()
{
return List.of(createAction(ChartTools.CROSSHAIR), createAction(ChartTools.MEASUREMENT));
}

private Pair<ChartTools, IAction> createAction(ChartTools tool)
{
var action = new SimpleAction(tool.getActionText(), //
this.activeTool == tool ? tool.getImageON() : tool.getImageOFF(), //
a -> {
if (this.activeTool == tool)
this.activeTool = ChartTools.NONE;
else
this.activeTool = tool;

currentTool = this.activeTool.createTool(chart, color);

// update images of tool bar buttons
updateButtonImages(this.activeTool);

chart.getToolTip().setActive(this.activeTool == ChartTools.NONE);
chart.redraw();
});

return new Pair<>(tool, action);
}

private void updateButtonImages(ChartTools activeTool)
{
buttons.forEach(p -> p.getRight().setImageDescriptor(
(p.getLeft() == activeTool ? p.getLeft().getImageON() : p.getLeft().getImageOFF())
.descriptor()));
}

public void addContextMenu(IMenuManager manager)
{
createActions().forEach(p -> manager.add(p.getRight()));
}
}
Loading