From cba8e9495b9785fd22e30f41ddcf790e98901086 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Fr=C3=A9d=C3=A9ric=20Perrin?= Date: Sun, 6 Oct 2019 16:22:35 +0100 Subject: [PATCH] Add menu item to remove altitudes from track --- src/tim/prune/App.java | 16 +++++ src/tim/prune/FunctionLibrary.java | 3 + src/tim/prune/data/DataPoint.java | 10 +++ src/tim/prune/data/Track.java | 25 ++++++++ src/tim/prune/function/RemoveAltitudes.java | 60 ++++++++++++++++++ src/tim/prune/gui/MenuManager.java | 4 ++ src/tim/prune/lang/prune-texts_en.properties | 3 + src/tim/prune/undo/UndoRemoveAltitudes.java | 65 ++++++++++++++++++++ 8 files changed, 186 insertions(+) create mode 100644 src/tim/prune/function/RemoveAltitudes.java create mode 100644 src/tim/prune/undo/UndoRemoveAltitudes.java diff --git a/src/tim/prune/App.java b/src/tim/prune/App.java index 3a77858..1bef98b 100644 --- a/src/tim/prune/App.java +++ b/src/tim/prune/App.java @@ -454,6 +454,22 @@ public class App } + /** + * Remove altitudes from selected points + */ + public void removeAltitudes(int selStart, int selEnd) + { + UndoRemoveAltitudes undo = new UndoRemoveAltitudes(_trackInfo, selStart, selEnd); + if (_trackInfo.getTrack().removeAltitudes(selStart, selEnd)) + { + _undoStack.add(undo); + _trackInfo.getSelection().markInvalid(); + UpdateMessageBroker.informSubscribers(DataSubscriber.DATA_EDITED); + UpdateMessageBroker.informSubscribers(I18nManager.getText("confirm.removealtitudes")); + } + } + + /** * Merge the track segments within the current selection */ diff --git a/src/tim/prune/FunctionLibrary.java b/src/tim/prune/FunctionLibrary.java index 0441826..0245b11 100644 --- a/src/tim/prune/FunctionLibrary.java +++ b/src/tim/prune/FunctionLibrary.java @@ -28,6 +28,7 @@ import tim.prune.function.PhotoPopupFunction; import tim.prune.function.PlayAudioFunction; import tim.prune.function.RearrangePhotosFunction; import tim.prune.function.RearrangeWaypointsFunction; +import tim.prune.function.RemoveAltitudes; import tim.prune.function.RemoveAudioFunction; import tim.prune.function.RemovePhotoFunction; import tim.prune.function.RotatePhoto; @@ -110,6 +111,7 @@ public abstract class FunctionLibrary public static GenericFunction FUNCTION_DOWNLOAD_OSM = null; public static GenericFunction FUNCTION_ADD_TIME_OFFSET = null; public static GenericFunction FUNCTION_ADD_ALTITUDE_OFFSET = null; + public static GenericFunction FUNCTION_REMOVE_ALTITUDES = null; public static GenericFunction FUNCTION_CONVERT_NAMES_TO_TIMES = null; public static GenericFunction FUNCTION_DELETE_FIELD_VALUES = null; public static GenericFunction FUNCTION_PASTE_COORDINATES = null; @@ -190,6 +192,7 @@ public abstract class FunctionLibrary FUNCTION_DOWNLOAD_OSM = new DownloadOsmFunction(inApp); FUNCTION_ADD_TIME_OFFSET = new AddTimeOffset(inApp); FUNCTION_ADD_ALTITUDE_OFFSET = new AddAltitudeOffset(inApp); + FUNCTION_REMOVE_ALTITUDES = new RemoveAltitudes(inApp); FUNCTION_CONVERT_NAMES_TO_TIMES = new ConvertNamesToTimes(inApp); FUNCTION_DELETE_FIELD_VALUES = new DeleteFieldValues(inApp); FUNCTION_PASTE_COORDINATES = new PasteCoordinates(inApp); diff --git a/src/tim/prune/data/DataPoint.java b/src/tim/prune/data/DataPoint.java index 84f4800..01e9975 100644 --- a/src/tim/prune/data/DataPoint.java +++ b/src/tim/prune/data/DataPoint.java @@ -354,6 +354,16 @@ public class DataPoint } } + /** + * Remove altitude from point + */ + public void removeAltitude() + { + _altitude = Altitude.NONE; + _fieldValues[_fieldList.getFieldIndex(Field.ALTITUDE)] = _altitude.getStringValue(null); + setModified(false); + } + /** * Reset the altitude to the previous value (by an undo) * @param inClone altitude object cloned from earlier diff --git a/src/tim/prune/data/Track.java b/src/tim/prune/data/Track.java index f45854a..ef099d4 100644 --- a/src/tim/prune/data/Track.java +++ b/src/tim/prune/data/Track.java @@ -377,6 +377,31 @@ public class Track } + /** + * Remove altitudes from the specified range + * @param inStart start of range + * @param inEnd end of range + */ + public boolean removeAltitudes(int inStart, int inEnd) + { + // sanity check + if (inStart < 0 || inEnd < 0 || inStart >= inEnd || inEnd >= _numPoints) { + return false; + } + + boolean anyRemoved = false; + for (int i=inStart; i<=inEnd; i++) + { + DataPoint p = _dataPoints[i]; + if (p != null && p.hasAltitude()) + { + p.removeAltitude(); + anyRemoved = true; + } + } + return anyRemoved; + } + /** * Interleave all waypoints by each nearest track point * @return true if successful, false if no change diff --git a/src/tim/prune/function/RemoveAltitudes.java b/src/tim/prune/function/RemoveAltitudes.java new file mode 100644 index 0000000..1c8b05c --- /dev/null +++ b/src/tim/prune/function/RemoveAltitudes.java @@ -0,0 +1,60 @@ +package tim.prune.function; + +import java.awt.BorderLayout; +import java.awt.FlowLayout; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.KeyAdapter; +import java.awt.event.KeyEvent; +import java.awt.event.MouseAdapter; + +import javax.swing.BorderFactory; +import javax.swing.JButton; +import javax.swing.JDialog; +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.JTextField; + +import tim.prune.App; +import tim.prune.GenericFunction; +import tim.prune.I18nManager; +import tim.prune.config.Config; +import tim.prune.data.Field; +import tim.prune.data.Unit; +import tim.prune.data.UnitSetLibrary; + +/** + * Class to provide the function to add remove the altitude from points in a + * track range + */ +public class RemoveAltitudes extends GenericFunction +{ + /** + * Constructor + * @param inApp application object for callback + */ + public RemoveAltitudes(App inApp) + { + super(inApp); + } + + /** Get the name key */ + public String getNameKey() { + return "function.removealtitudes"; + } + + /** + * Begin the function + */ + public void begin() + { + int selStart = _app.getTrackInfo().getSelection().getStart(); + int selEnd = _app.getTrackInfo().getSelection().getEnd(); + if (!_app.getTrackInfo().getTrack().hasData(Field.ALTITUDE, selStart, selEnd)) + { + _app.showErrorMessage(getNameKey(), "dialog.addaltitude.noaltitudes"); + return; + } + _app.removeAltitudes(selStart, selEnd); + } +} diff --git a/src/tim/prune/gui/MenuManager.java b/src/tim/prune/gui/MenuManager.java index bce3b4d..404a4fa 100644 --- a/src/tim/prune/gui/MenuManager.java +++ b/src/tim/prune/gui/MenuManager.java @@ -77,6 +77,7 @@ public class MenuManager implements DataSubscriber private JMenuItem _reverseItem = null; private JMenuItem _addTimeOffsetItem = null; private JMenuItem _addAltitudeOffsetItem = null; + private JMenuItem _removeAltitudesItem = null; private JMenuItem _mergeSegmentsItem = null; private JMenuItem _rearrangeWaypointsItem = null; private JMenuItem _splitSegmentsItem = null; @@ -425,6 +426,8 @@ public class MenuManager implements DataSubscriber rangeMenu.add(_addTimeOffsetItem); _addAltitudeOffsetItem = makeMenuItem(FunctionLibrary.FUNCTION_ADD_ALTITUDE_OFFSET, false); rangeMenu.add(_addAltitudeOffsetItem); + _removeAltitudesItem = makeMenuItem(FunctionLibrary.FUNCTION_REMOVE_ALTITUDES, false); + rangeMenu.add(_removeAltitudesItem); _mergeSegmentsItem = new JMenuItem(I18nManager.getText("menu.range.mergetracksegments")); _mergeSegmentsItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { @@ -956,6 +959,7 @@ public class MenuManager implements DataSubscriber _reverseItem.setEnabled(hasRange); _addTimeOffsetItem.setEnabled(hasRange); _addAltitudeOffsetItem.setEnabled(hasRange); + _removeAltitudesItem.setEnabled(hasRange); _convertNamesToTimesItem.setEnabled(hasRange && _track.hasWaypoints()); _deleteFieldValuesItem.setEnabled(hasRange); _fullRangeDetailsItem.setEnabled(hasRange); diff --git a/src/tim/prune/lang/prune-texts_en.properties b/src/tim/prune/lang/prune-texts_en.properties index 7693926..63bb73f 100644 --- a/src/tim/prune/lang/prune-texts_en.properties +++ b/src/tim/prune/lang/prune-texts_en.properties @@ -94,6 +94,7 @@ function.interpolate=Interpolate points function.deletebydate=Delete points by date function.addtimeoffset=Add time offset function.addaltitudeoffset=Add altitude offset +function.removealtitudes=Remove altitudes function.findwaypoint=Find waypoint function.rearrangewaypoints=Rearrange waypoints function.convertnamestotimes=Convert waypoint names to times @@ -632,6 +633,7 @@ confirm.mergetracksegments=Track segments merged confirm.reverserange=Range reversed confirm.addtimeoffset=Time offset added confirm.addaltitudeoffset=Altitude offset added +confirm.removealtitudes=Altitudes removed confirm.rearrangewaypoints=Waypoints rearranged confirm.rearrangephotos=Photos rearranged confirm.splitsegments=%d segment splits were made @@ -852,6 +854,7 @@ undo.splitsegments=split track segments undo.sewsegments=sew track segments undo.addtimeoffset=add time offset undo.addaltitudeoffset=add altitude offset +undo.removealtitudes=remove altitudes undo.rearrangewaypoints=rearrange waypoints undo.cutandmove=move section undo.connect=connect diff --git a/src/tim/prune/undo/UndoRemoveAltitudes.java b/src/tim/prune/undo/UndoRemoveAltitudes.java new file mode 100644 index 0000000..becf2d7 --- /dev/null +++ b/src/tim/prune/undo/UndoRemoveAltitudes.java @@ -0,0 +1,65 @@ +package tim.prune.undo; + +import tim.prune.I18nManager; +import tim.prune.UpdateMessageBroker; +import tim.prune.data.Altitude; +import tim.prune.data.DataPoint; +import tim.prune.data.TrackInfo; + +/** + * Undo removing (ie: restore the original) altitude from points + */ +public class UndoRemoveAltitudes implements UndoOperation +{ + /** Start index of section */ + private int _startIndex; + /** altitude values before operation */ + private Altitude[] _altitudes; + + + /** + * Constructor + * @param inTrackInfo track info object + */ + public UndoRemoveAltitudes(TrackInfo inTrackInfo, int inStart, int inEnd) + { + _startIndex = inStart; + final int numPoints = inEnd - inStart + 1; + // Make array of cloned altitude objects + _altitudes = new Altitude[numPoints]; + for (int i=0; i