File: MergeSegmentsFunction.java

package info (click to toggle)
gpsprune 26.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,824 kB
  • sloc: java: 52,154; sh: 25; makefile: 21; python: 15
file content (83 lines) | stat: -rw-r--r-- 2,201 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package tim.prune.function.segments;

import java.util.ArrayList;
import java.util.List;

import tim.prune.App;
import tim.prune.GenericFunction;
import tim.prune.I18nManager;
import tim.prune.cmd.PointFlag;
import tim.prune.cmd.SetSegmentsCmd;
import tim.prune.data.DataPoint;
import tim.prune.data.Track;

/**
 * Function to merge the track segments of the currently selected range
 */
public class MergeSegmentsFunction extends GenericFunction
{
	public MergeSegmentsFunction(App inApp) {
		super(inApp);
	}

	@Override
	public String getNameKey() {
		return "menu.range.mergetracksegments";
	}

	@Override
	public void begin()
	{
		final int selectionStart = _app.getTrackInfo().getSelection().getStart();
		final int selectionEnd = _app.getTrackInfo().getSelection().getEnd();
		if (selectionStart < 0 || selectionEnd <= selectionStart) {
			return;
		}
		List<PointFlag> flags = getFlags(_app.getTrackInfo().getTrack(), selectionStart, selectionEnd);
		if (!flags.isEmpty())
		{
			SetSegmentsCmd command = new SetSegmentsCmd(flags);
			command.setConfirmText(I18nManager.getText("confirm.mergetracksegments"));
			command.setDescription(getName());
			_app.execute(command);
		}
	}

	/**
	 * Compile the list of point flags which need to be applied
	 * @param inTrack track object
	 * @param inRangeStart index of range start
	 * @param inRangeEnd index of range end, inclusive
	 * @return list of points to modify (if any)
	 */
	private List<PointFlag> getFlags(Track inTrack, int inRangeStart, int inRangeEnd)
	{
		ArrayList<PointFlag> flags = new ArrayList<>();
		boolean firstTrackPoint = true;
		for (int i=inRangeStart; i<=inRangeEnd; i++)
		{
			DataPoint point = inTrack.getPoint(i);
			if (point.isWaypoint()) {
				continue;
			}
			boolean segment = point.getSegmentStart();
			if (firstTrackPoint)
			{
				if (!segment) {
					flags.add(new PointFlag(point, true));
				}
				firstTrackPoint = false;
			}
			else if (segment) {
				flags.add(new PointFlag(point, false));
			}
		}
		if (!flags.isEmpty()) {
			DataPoint nextPoint = inTrack.getNextTrackPoint(inRangeEnd + 1);
			if (nextPoint != null) {
				flags.add(new PointFlag(nextPoint, true));
			}
		}
		return flags;
	}
}