Changeset 12157 in josm
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/layer/GpxLayer.java
r12156 r12157 10 10 import java.text.DateFormat; 11 11 import java.util.Arrays; 12 import java.util.Collection;13 12 import java.util.Date; 14 import java.util.LinkedList;15 import java.util.List;16 13 17 14 import javax.swing.Action; … … 20 17 import javax.swing.SwingUtilities; 21 18 22 import org.openstreetmap.josm.Main;23 19 import org.openstreetmap.josm.actions.RenameLayerAction; 24 20 import org.openstreetmap.josm.actions.SaveActionBase; … … 28 24 import org.openstreetmap.josm.data.gpx.GpxData; 29 25 import org.openstreetmap.josm.data.gpx.GpxTrack; 30 import org.openstreetmap.josm.data.gpx.WayPoint;31 26 import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor; 32 27 import org.openstreetmap.josm.data.preferences.ColorProperty; … … 66 61 public boolean[] trackVisibility = new boolean[0]; 67 62 68 private final GpxDrawHelper drawHelper;69 70 63 /** 71 64 * Constructs a new {@code GpxLayer} without name. … … 95 88 data = d; 96 89 data.addWeakChangeListener(e -> this.invalidate()); 97 drawHelper = new GpxDrawHelper(data); 98 SystemOfMeasurement.addSoMChangeListener(drawHelper); 99 ensureTrackVisibilityLength(); 90 trackVisibility = new boolean[data.getTracks().size()]; 91 Arrays.fill(trackVisibility, true); 100 92 setName(name); 101 93 isLocalFile = isLocal; … … 280 272 throw new IllegalArgumentException("not a GpxLayer: " + from); 281 273 data.mergeFrom(((GpxLayer) from).data); 282 drawHelper.dataChanged(); 283 } 284 285 @Override 286 public void paint(Graphics2D g, MapView mv, Bounds box) { 287 List<WayPoint> visibleSegments = listVisibleSegments(box); 288 if (!visibleSegments.isEmpty()) { 289 drawHelper.readPreferences(getName()); 290 drawHelper.drawAll(g, mv, visibleSegments); 291 if (Main.getLayerManager().getActiveLayer() == this) { 292 drawHelper.drawColorBar(g, mv); 293 } 294 } 295 } 296 297 private List<WayPoint> listVisibleSegments(Bounds box) { 298 WayPoint last = null; 299 LinkedList<WayPoint> visibleSegments = new LinkedList<>(); 300 301 ensureTrackVisibilityLength(); 302 for (Collection<WayPoint> segment : data.getLinesIterable(trackVisibility)) { 303 304 for (WayPoint pt : segment) { 305 Bounds b = new Bounds(pt.getCoor()); 306 if (pt.drawLine && last != null) { 307 b.extend(last.getCoor()); 308 } 309 if (b.intersects(box)) { 310 if (last != null && (visibleSegments.isEmpty() 311 || visibleSegments.getLast() != last)) { 312 if (last.drawLine) { 313 WayPoint l = new WayPoint(last); 314 l.drawLine = false; 315 visibleSegments.add(l); 316 } else { 317 visibleSegments.add(last); 318 } 319 } 320 visibleSegments.add(pt); 321 } 322 last = pt; 323 } 324 } 325 return visibleSegments; 274 invalidate(); 326 275 } 327 276 … … 339 288 public void setAssociatedFile(File file) { 340 289 data.storageFile = file; 341 }342 343 /** ensures the trackVisibility array has the correct length without losing data.344 * additional entries are initialized to true;345 */346 private void ensureTrackVisibilityLength() {347 final int l = data.getTracks().size();348 if (l == trackVisibility.length)349 return;350 final int m = Math.min(l, trackVisibility.length);351 trackVisibility = Arrays.copyOf(trackVisibility, l);352 for (int i = m; i < l; i++) {353 trackVisibility[i] = true;354 }355 invalidate();356 290 } 357 291 … … 383 317 384 318 @Override 385 public synchronized void destroy() { 386 super.destroy(); 387 SystemOfMeasurement.removeSoMChangeListener(drawHelper); 319 public void paint(Graphics2D g, MapView mv, Bounds bbox) { 320 // unused - we use a painter so this is not called. 321 } 322 323 @Override 324 protected LayerPainter createMapViewPainter(MapViewEvent event) { 325 return new GpxDrawHelper(this); 388 326 } 389 327 } -
trunk/src/org/openstreetmap/josm/gui/layer/gpx/GpxDrawHelper.java
r12131 r12157 27 27 import java.util.Collections; 28 28 import java.util.Date; 29 import java.util.LinkedList; 29 30 import java.util.List; 30 31 import java.util.Random; … … 33 34 34 35 import org.openstreetmap.josm.Main; 36 import org.openstreetmap.josm.data.Bounds; 35 37 import org.openstreetmap.josm.data.SystemOfMeasurement; 36 38 import org.openstreetmap.josm.data.SystemOfMeasurement.SoMChangeListener; … … 42 44 import org.openstreetmap.josm.gui.MapView; 43 45 import org.openstreetmap.josm.gui.MapViewState; 46 import org.openstreetmap.josm.gui.layer.GpxLayer; 47 import org.openstreetmap.josm.gui.layer.MapViewGraphics; 48 import org.openstreetmap.josm.gui.layer.MapViewPaintable; 49 import org.openstreetmap.josm.gui.layer.MapViewPaintable.MapViewEvent; 50 import org.openstreetmap.josm.gui.layer.MapViewPaintable.PaintableInvalidationEvent; 51 import org.openstreetmap.josm.gui.layer.MapViewPaintable.PaintableInvalidationListener; 44 52 import org.openstreetmap.josm.io.CachedFile; 45 53 import org.openstreetmap.josm.tools.ColorScale; … … 51 59 * @since 7319 52 60 */ 53 public class GpxDrawHelper implements SoMChangeListener {61 public class GpxDrawHelper implements SoMChangeListener, MapViewPaintable.LayerPainter, PaintableInvalidationListener { 54 62 55 63 /** … … 60 68 61 69 private final GpxData data; 70 private final GpxLayer layer; 62 71 63 72 // draw lines between points belonging to different segments … … 167 176 private Color[] heatMapLutColor = createColorLut(0, Color.BLACK, Color.WHITE); 168 177 178 // The heat map was invalidated since the last draw. 179 private boolean gpxLayerInvalidated; 180 181 169 182 private void setupColors() { 170 183 hdopAlpha = Main.pref.getInteger("hdop.color.alpha", -1); … … 204 217 /** 205 218 * Constructs a new {@code GpxDrawHelper}. 206 * @param gpxData GPX data 207 * @since 11713 208 */ 209 public GpxDrawHelper(GpxData gpxData) { 210 data = gpxData; 219 * @param gpxLayer The layer to draw 220 * @since 12157 221 */ 222 public GpxDrawHelper(GpxLayer gpxLayer) { 223 layer = gpxLayer; 224 data = gpxLayer.data; 225 226 layer.addInvalidationListener(this); 227 SystemOfMeasurement.addSoMChangeListener(this); 211 228 setupColors(); 212 229 } … … 301 318 302 319 largesize += lineWidth; 320 } 321 322 @Override 323 public void paint(MapViewGraphics graphics) { 324 List<WayPoint> visibleSegments = listVisibleSegments(graphics.getClipBounds().getLatLonBoundsBox()); 325 if (!visibleSegments.isEmpty()) { 326 readPreferences(layer.getName()); 327 drawAll(graphics.getDefaultGraphics(), graphics.getMapView(), visibleSegments); 328 if (graphics.getMapView().getLayerManager().getActiveLayer() == layer) { 329 drawColorBar(graphics.getDefaultGraphics(), graphics.getMapView()); 330 } 331 } 332 } 333 334 private List<WayPoint> listVisibleSegments(Bounds box) { 335 WayPoint last = null; 336 LinkedList<WayPoint> visibleSegments = new LinkedList<>(); 337 338 ensureTrackVisibilityLength(); 339 for (Collection<WayPoint> segment : data.getLinesIterable(layer.trackVisibility)) { 340 341 for (WayPoint pt : segment) { 342 Bounds b = new Bounds(pt.getCoor()); 343 if (pt.drawLine && last != null) { 344 b.extend(last.getCoor()); 345 } 346 if (b.intersects(box)) { 347 if (last != null && (visibleSegments.isEmpty() 348 || visibleSegments.getLast() != last)) { 349 if (last.drawLine) { 350 WayPoint l = new WayPoint(last); 351 l.drawLine = false; 352 visibleSegments.add(l); 353 } else { 354 visibleSegments.add(last); 355 } 356 } 357 visibleSegments.add(pt); 358 } 359 last = pt; 360 } 361 } 362 return visibleSegments; 363 } 364 365 /** ensures the trackVisibility array has the correct length without losing data. 366 * TODO: Make this nicer by syncing the trackVisibility automatically. 367 * additional entries are initialized to true; 368 */ 369 private void ensureTrackVisibilityLength() { 370 final int l = data.getTracks().size(); 371 if (l == layer.trackVisibility.length) 372 return; 373 final int m = Math.min(l, layer.trackVisibility.length); 374 layer.trackVisibility = Arrays.copyOf(layer.trackVisibility, l); 375 for (int i = m; i < l; i++) { 376 layer.trackVisibility[i] = true; 377 } 303 378 } 304 379 … … 1203 1278 1204 1279 // recalculation of image needed 1205 final boolean imageRecalc = !mapViewState.equalsInWindow(heatMapMapViewState) || 1206 heatMapCacheLineWith != globalLineWidth; 1280 final boolean imageRecalc = !mapViewState.equalsInWindow(heatMapMapViewState) 1281 || gpxLayerInvalidated 1282 || heatMapCacheLineWith != globalLineWidth; 1207 1283 1208 1284 // need re-generation of gray image ? … … 1228 1304 heatMapMapViewState = mapViewState; 1229 1305 heatMapCacheLineWith = globalLineWidth; 1306 gpxLayerInvalidated = false; 1230 1307 } 1231 1308 … … 1402 1479 } 1403 1480 } 1481 1482 @Override 1483 public void paintableInvalidated(PaintableInvalidationEvent event) { 1484 gpxLayerInvalidated = true; 1485 } 1486 1487 @Override 1488 public void detachFromMapView(MapViewEvent event) { 1489 SystemOfMeasurement.removeSoMChangeListener(this); 1490 layer.removeInvalidationListener(this); 1491 } 1404 1492 } -
trunk/test/unit/org/openstreetmap/josm/gui/layer/gpx/GpxDrawHelperTest.java
r12156 r12157 15 15 import org.openstreetmap.josm.TestUtils; 16 16 import org.openstreetmap.josm.data.gpx.GpxData; 17 import org.openstreetmap.josm.gui.layer.GpxLayer; 17 18 import org.openstreetmap.josm.io.GpxReaderTest; 18 19 import org.openstreetmap.josm.tools.ColorHelper; … … 123 124 static List<String> calculateColors(String fileName, String layerName, int n) throws IOException, SAXException { 124 125 final GpxData data = GpxReaderTest.parseGpxData(fileName); 125 final GpxDrawHelper gdh = new GpxDrawHelper(data); 126 final GpxLayer layer = new GpxLayer(data); 127 final GpxDrawHelper gdh = new GpxDrawHelper(layer); 126 128 gdh.readPreferences(layerName); 127 129 gdh.calculateColors();
Note:
See TracChangeset
for help on using the changeset viewer.