]> gitweb.fperrin.net Git - GpsPrune.git/blobdiff - tim/prune/data/Selection.java
Version 18.1, September 2015
[GpsPrune.git] / tim / prune / data / Selection.java
index c375e820b4a05b022682deef2b675ee27a82b8d8..7b63d70838e660c9af142aa1b341f5c2bd40ccce 100644 (file)
@@ -1,5 +1,6 @@
 package tim.prune.data;
 
+import tim.prune.DataSubscriber;
 import tim.prune.UpdateMessageBroker;
 
 /**
@@ -9,84 +10,36 @@ import tim.prune.UpdateMessageBroker;
 public class Selection
 {
        private Track _track = null;
-       private UpdateMessageBroker _broker = null;
        private int _currentPoint = -1;
        private boolean _valid = false;
+       private int _prevNumPoints = 0;
        private int _startIndex = -1, _endIndex = -1;
-       private IntegerRange _altitudeRange = null;
-       private int _climb = -1, _descent = -1;
-       private int _altitudeFormat = Altitude.FORMAT_NONE;
-       private long _seconds = 0L;
-       private double _angDistance = -1.0; //, _averageSpeed = -1.0;
+       private int _currentPhotoIndex = -1;
+       private int _currentAudioIndex = -1;
+       private AltitudeRange _altitudeRange = null;
+       private long _movingMilliseconds = 0L;
+       private double _angMovingDistance = -1.0;
 
 
        /**
         * Constructor
         * @param inTrack track object
-        * @param inBroker broker object
         */
-       public Selection(Track inTrack, UpdateMessageBroker inBroker)
+       public Selection(Track inTrack)
        {
                _track = inTrack;
-               _broker = inBroker;
        }
 
 
        /**
-        * Reset selection to be recalculated
+        * Mark selection invalid so it will be recalculated
         */
-       private void reset()
+       public void markInvalid()
        {
                _valid = false;
        }
 
 
-       /**
-        * Select the point at the given index
-        * @param inIndex index number of selected point
-        */
-       public void selectPoint(int inIndex)
-       {
-               if (inIndex >= -1)
-               {
-                       _currentPoint = inIndex;
-                       check();
-               }
-       }
-
-       /**
-        * Select the specified point and range in one go
-        * @param inPointIndex point selection
-        * @param inStart range start
-        * @param inEnd range end
-        */
-       public void select(int inPointIndex, int inStart, int inEnd)
-       {
-               _currentPoint = inPointIndex;
-               _startIndex = inStart;
-               _endIndex = inEnd;
-               reset();
-               check();
-       }
-
-
-       /**
-        * Select the previous point
-        */
-       public void selectPreviousPoint()
-       {
-               if (_currentPoint > 0)
-                       selectPoint(_currentPoint - 1);
-       }
-
-       /**
-        * Select the next point
-        */
-       public void selectNextPoint()
-       {
-               selectPoint(_currentPoint + 1);
-       }
-
        /**
         * @return the current point index
         */
@@ -110,19 +63,22 @@ public class Selection
         */
        private void recalculate()
        {
-               _altitudeFormat = Altitude.FORMAT_NONE;
-               if (_track.getNumPoints() > 0 && hasRangeSelected())
+               final int numPoints = _track.getNumPoints();
+               // Recheck if the number of points has changed
+               if (numPoints != _prevNumPoints)
                {
-                       _altitudeRange = new IntegerRange();
-                       _climb = 0;
-                       _descent = 0;
+                       _prevNumPoints = numPoints;
+                       check();
+               }
+               if (numPoints > 0 && hasRangeSelected())
+               {
+                       _altitudeRange = new AltitudeRange();
                        Altitude altitude = null;
-                       Timestamp time = null, startTime = null, endTime = null;
+                       Timestamp time = null, previousTime = null;
                        DataPoint lastPoint = null, currPoint = null;
-                       _angDistance = 0.0;
-                       int altValue = 0;
-                       int lastAltValue = 0;
-                       boolean foundAlt = false;
+                       _angMovingDistance = 0.0;
+                       _movingMilliseconds = 0L;
+                       // Loop over points in selection
                        for (int i=_startIndex; i<=_endIndex; i++)
                        {
                                currPoint = _track.getPoint(i);
@@ -130,45 +86,36 @@ public class Selection
                                // Ignore waypoints in altitude calculations
                                if (!currPoint.isWaypoint() && altitude.isValid())
                                {
-                                       altValue = altitude.getValue(_altitudeFormat);
-                                       if (_altitudeFormat == Altitude.FORMAT_NONE)
-                                               _altitudeFormat = altitude.getFormat();
-                                       _altitudeRange.addValue(altValue);
-                                       if (foundAlt)
-                                       {
-                                               if (altValue > lastAltValue)
-                                                       _climb += (altValue - lastAltValue);
-                                               else
-                                                       _descent += (lastAltValue - altValue);
+                                       if (currPoint.getSegmentStart()) {
+                                               _altitudeRange.ignoreValue(altitude);
+                                       }
+                                       else {
+                                               _altitudeRange.addValue(altitude);
                                        }
-                                       lastAltValue = altValue;
-                                       foundAlt = true;
                                }
-                               // Store the first and last timestamp in the range
+                               // Compare timestamps within the segments
                                time = currPoint.getTimestamp();
                                if (time.isValid())
                                {
-                                       if (startTime == null) startTime = time;
-                                       endTime = time;
+                                       // add moving time
+                                       if (!currPoint.getSegmentStart() && previousTime != null && time.isAfter(previousTime)) {
+                                               _movingMilliseconds += time.getMillisecondsSince(previousTime);
+                                       }
+                                       previousTime = time;
                                }
                                // Calculate distances, again ignoring waypoints
                                if (!currPoint.isWaypoint())
                                {
                                        if (lastPoint != null)
                                        {
-                                               _angDistance += DataPoint.calculateRadiansBetween(lastPoint, currPoint);
+                                               double radians = DataPoint.calculateRadiansBetween(lastPoint, currPoint);
+                                               if (!currPoint.getSegmentStart()) {
+                                                       _angMovingDistance += radians;
+                                               }
                                        }
                                        lastPoint = currPoint;
                                }
                        }
-                       if (endTime != null)
-                       {
-                               _seconds = endTime.getSecondsSince(startTime);
-                       }
-                       else
-                       {
-                               _seconds = 0L;
-                       }
                }
                _valid = true;
        }
@@ -193,19 +140,10 @@ public class Selection
                return _endIndex;
        }
 
-
-       /**
-        * @return the altitude format, ie feet or metres
-        */
-       public int getAltitudeFormat()
-       {
-               return _altitudeFormat;
-       }
-
        /**
         * @return altitude range
         */
-       public IntegerRange getAltitudeRange()
+       public AltitudeRange getAltitudeRange()
        {
                if (!_valid) recalculate();
                return _altitudeRange;
@@ -213,61 +151,45 @@ public class Selection
 
 
        /**
-        * @return climb
-        */
-       public int getClimb()
-       {
-               if (!_valid) recalculate();
-               return _climb;
-       }
-
-       /**
-        * @return descent
-        */
-       public int getDescent()
-       {
-               if (!_valid) recalculate();
-               return _descent;
-       }
-
-
-       /**
-        * @return number of seconds spanned by selection
+        * @return number of seconds spanned by segments within selection
         */
-       public long getNumSeconds()
+       public long getMovingSeconds()
        {
                if (!_valid) recalculate();
-               return _seconds;
+               return _movingMilliseconds / 1000L;
        }
 
-
        /**
-        * @param inFormat distance units to use, from class Distance
-        * @return distance of Selection in specified units
+        * @return moving distance of Selection in current units
         */
-       public double getDistance(int inUnits)
+       public double getMovingDistance()
        {
-               return Distance.convertRadians(_angDistance, inUnits);
+               return Distance.convertRadiansToDistance(_angMovingDistance);
        }
 
-
        /**
-        * Clear selected point and range
+        * Clear selected point, range, photo and audio
         */
        public void clearAll()
        {
                _currentPoint = -1;
-               deselectRange();
+               selectRange(-1, -1);
+               _currentPhotoIndex = -1;
+               _currentAudioIndex = -1;
+               check();
        }
 
 
        /**
-        * Deselect range
+        * Select range from start to end
+        * @param inStartIndex index of start of range
+        * @param inEndIndex index of end of range
         */
-       public void deselectRange()
+       public void selectRange(int inStartIndex, int inEndIndex)
        {
-               _startIndex = _endIndex = -1;
-               reset();
+               _startIndex = inStartIndex;
+               _endIndex = inEndIndex;
+               markInvalid();
                check();
        }
 
@@ -285,7 +207,7 @@ public class Selection
         * Set the index for the start of the range selection
         * @param inStartIndex start index
         */
-       public void selectRangeStart(int inStartIndex)
+       private void selectRangeStart(int inStartIndex)
        {
                if (inStartIndex < 0)
                {
@@ -300,8 +222,8 @@ public class Selection
                                _endIndex = _track.getNumPoints() - 1;
                        }
                }
-               reset();
-               _broker.informSubscribers();
+               markInvalid();
+               UpdateMessageBroker.informSubscribers();
        }
 
 
@@ -328,13 +250,12 @@ public class Selection
                {
                        _endIndex = inEndIndex;
                        // Move start of selection to min if necessary
-                       if (_startIndex > _endIndex || _startIndex < 0)
-                       {
+                       if (_startIndex > _endIndex || _startIndex < 0) {
                                _startIndex = 0;
                        }
                }
-               reset();
-               _broker.informSubscribers();
+               markInvalid();
+               UpdateMessageBroker.informSubscribers();
        }
 
 
@@ -371,12 +292,43 @@ public class Selection
                        _endIndex--;
                        if (_currentPoint < _startIndex)
                                _startIndex--;
-                       reset();
+                       markInvalid();
                }
                check();
        }
 
 
+       /**
+        * Select the specified photo and point
+        * @param inPointIndex index of selected point
+        * @param inPhotoIndex index of selected photo in PhotoList
+        * @param inAudioIndex index of selected audio item
+        */
+       public void selectPointPhotoAudio(int inPointIndex, int inPhotoIndex, int inAudioIndex)
+       {
+               _currentPoint = inPointIndex;
+               _currentPhotoIndex = inPhotoIndex;
+               _currentAudioIndex = inAudioIndex;
+               check();
+       }
+
+
+       /**
+        * @return currently selected photo index
+        */
+       public int getCurrentPhotoIndex()
+       {
+               return _currentPhotoIndex;
+       }
+
+       /**
+        * @return currently selected audio index
+        */
+       public int getCurrentAudioIndex()
+       {
+               return _currentAudioIndex;
+       }
+
        /**
         * Check that the selection still makes sense
         * and fire update message to listeners
@@ -399,6 +351,11 @@ public class Selection
                                _startIndex = maxIndex;
                        }
                }
-               _broker.informSubscribers();
+               else
+               {
+                       // track is empty, clear selections
+                       _currentPoint = _startIndex = _endIndex = -1;
+               }
+               UpdateMessageBroker.informSubscribers(DataSubscriber.SELECTION_CHANGED);
        }
 }