]> 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 6924d133abca3c541a44d5bf257955a7e1bd3440..7b63d70838e660c9af142aa1b341f5c2bd40ccce 100644 (file)
@@ -12,14 +12,13 @@ public class Selection
        private Track _track = null;
        private int _currentPoint = -1;
        private boolean _valid = false;
+       private int _prevNumPoints = 0;
        private int _startIndex = -1, _endIndex = -1;
        private int _currentPhotoIndex = -1;
-       private IntegerRange _altitudeRange = null;
-       private int _climb = -1, _descent = -1;
-       private Altitude.Format _altitudeFormat = Altitude.Format.NO_FORMAT;
-       private long _totalSeconds = 0L, _movingSeconds = 0L;
-       private double _angDistance = -1.0, _angMovingDistance = -1.0;
-       private boolean _multipleSegments = false;
+       private int _currentAudioIndex = -1;
+       private AltitudeRange _altitudeRange = null;
+       private long _movingMilliseconds = 0L;
+       private double _angMovingDistance = -1.0;
 
 
        /**
@@ -64,22 +63,22 @@ public class Selection
         */
        private void recalculate()
        {
-               _altitudeFormat = Altitude.Format.NO_FORMAT;
-               _multipleSegments = false;
-               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 previousTime = null;
+                       Timestamp time = null, previousTime = null;
                        DataPoint lastPoint = null, currPoint = null;
-                       _angDistance = 0.0; _angMovingDistance = 0.0;
-                       _totalSeconds = 0L; _movingSeconds = 0L;
-                       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);
@@ -87,29 +86,20 @@ public class Selection
                                // Ignore waypoints in altitude calculations
                                if (!currPoint.isWaypoint() && altitude.isValid())
                                {
-                                       altValue = altitude.getValue(_altitudeFormat);
-                                       if (_altitudeFormat == Altitude.Format.NO_FORMAT)
-                                               _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.isAfter(time)) startTime = time;
-                                       if (endTime == null || time.isAfter(endTime)) endTime = time;
                                        // add moving time
                                        if (!currPoint.getSegmentStart() && previousTime != null && time.isAfter(previousTime)) {
-                                               _movingSeconds += time.getSecondsSince(previousTime);
+                                               _movingMilliseconds += time.getMillisecondsSince(previousTime);
                                        }
                                        previousTime = time;
                                }
@@ -119,20 +109,13 @@ public class Selection
                                        if (lastPoint != null)
                                        {
                                                double radians = DataPoint.calculateRadiansBetween(lastPoint, currPoint);
-                                               _angDistance += radians;
-                                               if (currPoint.getSegmentStart()) {
-                                                       _multipleSegments = true;
-                                               }
-                                               else {
+                                               if (!currPoint.getSegmentStart()) {
                                                        _angMovingDistance += radians;
                                                }
                                        }
                                        lastPoint = currPoint;
                                }
                        }
-                       if (endTime != null) {
-                               _totalSeconds = endTime.getSecondsSince(startTime);
-                       }
                }
                _valid = true;
        }
@@ -157,102 +140,48 @@ public class Selection
                return _endIndex;
        }
 
-
-       /**
-        * @return the altitude format, ie feet or metres
-        */
-       public Altitude.Format getAltitudeFormat()
-       {
-               return _altitudeFormat;
-       }
-
        /**
         * @return altitude range
         */
-       public IntegerRange getAltitudeRange()
+       public AltitudeRange getAltitudeRange()
        {
                if (!_valid) recalculate();
                return _altitudeRange;
        }
 
 
-       /**
-        * @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
-        */
-       public long getNumSeconds()
-       {
-               if (!_valid) recalculate();
-               return _totalSeconds;
-       }
-
        /**
         * @return number of seconds spanned by segments within selection
         */
        public long getMovingSeconds()
        {
                if (!_valid) recalculate();
-               return _movingSeconds;
+               return _movingMilliseconds / 1000L;
        }
 
        /**
-        * @param inUnits 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(Distance.Units inUnits)
+       public double getMovingDistance()
        {
-               return Distance.convertRadiansToDistance(_angDistance, inUnits);
+               return Distance.convertRadiansToDistance(_angMovingDistance);
        }
 
        /**
-        * @param inUnits distance units to use, from class Distance
-        * @return moving distance of Selection in specified units
-        */
-       public double getMovingDistance(Distance.Units inUnits)
-       {
-               return Distance.convertRadiansToDistance(_angMovingDistance, inUnits);
-       }
-
-       /**
-        * @return true if track has multiple segments
-        */
-       public boolean getHasMultipleSegments()
-       {
-               return _multipleSegments;
-       }
-
-       /**
-        * Clear selected point, range and photo
+        * Clear selected point, range, photo and audio
         */
        public void clearAll()
        {
                _currentPoint = -1;
                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
         */
@@ -371,13 +300,15 @@ public class Selection
 
        /**
         * Select the specified photo and point
-        * @param inPhotoIndex index of selected photo in PhotoList
         * @param inPointIndex index of selected point
+        * @param inPhotoIndex index of selected photo in PhotoList
+        * @param inAudioIndex index of selected audio item
         */
-       public void selectPhotoAndPoint(int inPhotoIndex, int inPointIndex)
+       public void selectPointPhotoAudio(int inPointIndex, int inPhotoIndex, int inAudioIndex)
        {
-               _currentPhotoIndex = inPhotoIndex;
                _currentPoint = inPointIndex;
+               _currentPhotoIndex = inPhotoIndex;
+               _currentAudioIndex = inAudioIndex;
                check();
        }
 
@@ -390,36 +321,41 @@ public class Selection
                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
         */
        private void check()
        {
-               if (_track != null)
+               if (_track != null && _track.getNumPoints() > 0)
                {
-                       if (_track.getNumPoints() > 0)
+                       int maxIndex = _track.getNumPoints() - 1;
+                       if (_currentPoint > maxIndex)
                        {
-                               int maxIndex = _track.getNumPoints() - 1;
-                               if (_currentPoint > maxIndex)
-                               {
-                                       _currentPoint = maxIndex;
-                               }
-                               if (_endIndex > maxIndex)
-                               {
-                                       _endIndex = maxIndex;
-                               }
-                               if (_startIndex > maxIndex)
-                               {
-                                       _startIndex = maxIndex;
-                               }
+                               _currentPoint = maxIndex;
+                       }
+                       if (_endIndex > maxIndex)
+                       {
+                               _endIndex = maxIndex;
                        }
-                       else
+                       if (_startIndex > maxIndex)
                        {
-                               // track is empty, clear selections
-                               _currentPoint = _startIndex = _endIndex = -1;
+                               _startIndex = maxIndex;
                        }
                }
+               else
+               {
+                       // track is empty, clear selections
+                       _currentPoint = _startIndex = _endIndex = -1;
+               }
                UpdateMessageBroker.informSubscribers(DataSubscriber.SELECTION_CHANGED);
        }
 }