]> gitweb.fperrin.net Git - GpsPrune.git/commitdiff
Add menu item to remove altitudes from track github/allow-removing-altitudes origin/allow-removing-altitudes
authorFrédéric Perrin <fred@fperrin.net>
Sun, 6 Oct 2019 15:22:35 +0000 (16:22 +0100)
committerFrédéric Perrin <fred@fperrin.net>
Sat, 30 Nov 2019 20:49:47 +0000 (20:49 +0000)
src/tim/prune/App.java
src/tim/prune/FunctionLibrary.java
src/tim/prune/data/DataPoint.java
src/tim/prune/data/Track.java
src/tim/prune/function/RemoveAltitudes.java [new file with mode: 0644]
src/tim/prune/gui/MenuManager.java
src/tim/prune/lang/prune-texts_en.properties
src/tim/prune/undo/UndoRemoveAltitudes.java [new file with mode: 0644]

index 3a778588ab815a7f30c4b396e4de9d2c266836da..1bef98ba4497d932e331e03c4125ff73a34171c9 100644 (file)
@@ -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
         */
index 0441826164d930de8b79acb7d45fd84a742d33ce..0245b115e93d6eaadeceea290adefbdcfa324af4 100644 (file)
@@ -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);
index 84f4800bcd2c0a0d546da8a83a87868810bf3a54..01e9975786139a1736653d68dd1fd747109b3788 100644 (file)
@@ -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
index f45854a0e3d47a1fd5fc0ee6924732b3294decc0..ef099d4c171d1651bfd627a3beb71bd271943a15 100644 (file)
@@ -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 (file)
index 0000000..1c8b05c
--- /dev/null
@@ -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);
+       }
+}
index bce3b4d67b0a6df980b66882f175245af1134155..404a4fa77e72938f35191a7f656b1d1940b50849 100644 (file)
@@ -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);
index 76939263c9d8af07fa36db1f1686cf99550180dd..63bb73f66b313436e53619a2f03d40de0996139f 100644 (file)
@@ -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 (file)
index 0000000..becf2d7
--- /dev/null
@@ -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<numPoints; i++) {
+                       Altitude a = inTrackInfo.getTrack().getPoint(_startIndex+i).getAltitude();
+                       if (a != null && a.isValid()) {
+                               _altitudes[i] = a.clone();
+                       }
+               }
+       }
+
+
+       /**
+        * @return description of operation including number of points adjusted
+        */
+       public String getDescription()
+       {
+               return I18nManager.getText("undo.removealtitudes") + " (" + (_altitudes.length) + ")";
+       }
+
+
+       /**
+        * Perform the undo operation on the given Track
+        * @param inTrackInfo TrackInfo object on which to perform the operation
+        */
+       public void performUndo(TrackInfo inTrackInfo) throws UndoException
+       {
+               // Perform the inverse operation
+               final int numPoints = _altitudes.length;
+               for (int i=0; i<numPoints; i++)
+               {
+                       DataPoint point = inTrackInfo.getTrack().getPoint(i+_startIndex);
+                       point.resetAltitude(_altitudes[i]);
+               }
+               _altitudes = null;
+               inTrackInfo.getSelection().markInvalid();
+               UpdateMessageBroker.informSubscribers();
+       }
+}