Changeset 12157 in josm for trunk


Ignore:
Timestamp:
2017-05-15T14:41:08+02:00 (7 years ago)
Author:
michael2402
Message:

See #13124: Refresh the heat map on every invalidation (visible tacks changed, ...)

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/layer/GpxLayer.java

    r12156 r12157  
    1010import java.text.DateFormat;
    1111import java.util.Arrays;
    12 import java.util.Collection;
    1312import java.util.Date;
    14 import java.util.LinkedList;
    15 import java.util.List;
    1613
    1714import javax.swing.Action;
     
    2017import javax.swing.SwingUtilities;
    2118
    22 import org.openstreetmap.josm.Main;
    2319import org.openstreetmap.josm.actions.RenameLayerAction;
    2420import org.openstreetmap.josm.actions.SaveActionBase;
     
    2824import org.openstreetmap.josm.data.gpx.GpxData;
    2925import org.openstreetmap.josm.data.gpx.GpxTrack;
    30 import org.openstreetmap.josm.data.gpx.WayPoint;
    3126import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
    3227import org.openstreetmap.josm.data.preferences.ColorProperty;
     
    6661    public boolean[] trackVisibility = new boolean[0];
    6762
    68     private final GpxDrawHelper drawHelper;
    69 
    7063    /**
    7164     * Constructs a new {@code GpxLayer} without name.
     
    9588        data = d;
    9689        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);
    10092        setName(name);
    10193        isLocalFile = isLocal;
     
    280272            throw new IllegalArgumentException("not a GpxLayer: " + from);
    281273        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();
    326275    }
    327276
     
    339288    public void setAssociatedFile(File file) {
    340289        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();
    356290    }
    357291
     
    383317
    384318    @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);
    388326    }
    389327}
  • trunk/src/org/openstreetmap/josm/gui/layer/gpx/GpxDrawHelper.java

    r12131 r12157  
    2727import java.util.Collections;
    2828import java.util.Date;
     29import java.util.LinkedList;
    2930import java.util.List;
    3031import java.util.Random;
     
    3334
    3435import org.openstreetmap.josm.Main;
     36import org.openstreetmap.josm.data.Bounds;
    3537import org.openstreetmap.josm.data.SystemOfMeasurement;
    3638import org.openstreetmap.josm.data.SystemOfMeasurement.SoMChangeListener;
     
    4244import org.openstreetmap.josm.gui.MapView;
    4345import org.openstreetmap.josm.gui.MapViewState;
     46import org.openstreetmap.josm.gui.layer.GpxLayer;
     47import org.openstreetmap.josm.gui.layer.MapViewGraphics;
     48import org.openstreetmap.josm.gui.layer.MapViewPaintable;
     49import org.openstreetmap.josm.gui.layer.MapViewPaintable.MapViewEvent;
     50import org.openstreetmap.josm.gui.layer.MapViewPaintable.PaintableInvalidationEvent;
     51import org.openstreetmap.josm.gui.layer.MapViewPaintable.PaintableInvalidationListener;
    4452import org.openstreetmap.josm.io.CachedFile;
    4553import org.openstreetmap.josm.tools.ColorScale;
     
    5159 * @since 7319
    5260 */
    53 public class GpxDrawHelper implements SoMChangeListener {
     61public class GpxDrawHelper implements SoMChangeListener, MapViewPaintable.LayerPainter, PaintableInvalidationListener {
    5462
    5563    /**
     
    6068
    6169    private final GpxData data;
     70    private final GpxLayer layer;
    6271
    6372    // draw lines between points belonging to different segments
     
    167176    private Color[] heatMapLutColor = createColorLut(0, Color.BLACK, Color.WHITE);
    168177
     178    // The heat map was invalidated since the last draw.
     179    private boolean gpxLayerInvalidated;
     180
     181
    169182    private void setupColors() {
    170183        hdopAlpha = Main.pref.getInteger("hdop.color.alpha", -1);
     
    204217    /**
    205218     * 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);
    211228        setupColors();
    212229    }
     
    301318
    302319        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        }
    303378    }
    304379
     
    12031278
    12041279        // 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;
    12071283
    12081284        // need re-generation of gray image ?
     
    12281304            heatMapMapViewState = mapViewState;
    12291305            heatMapCacheLineWith = globalLineWidth;
     1306            gpxLayerInvalidated = false;
    12301307        }
    12311308
     
    14021479        }
    14031480    }
     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    }
    14041492}
  • trunk/test/unit/org/openstreetmap/josm/gui/layer/gpx/GpxDrawHelperTest.java

    r12156 r12157  
    1515import org.openstreetmap.josm.TestUtils;
    1616import org.openstreetmap.josm.data.gpx.GpxData;
     17import org.openstreetmap.josm.gui.layer.GpxLayer;
    1718import org.openstreetmap.josm.io.GpxReaderTest;
    1819import org.openstreetmap.josm.tools.ColorHelper;
     
    123124    static List<String> calculateColors(String fileName, String layerName, int n) throws IOException, SAXException {
    124125        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);
    126128        gdh.readPreferences(layerName);
    127129        gdh.calculateColors();
Note: See TracChangeset for help on using the changeset viewer.