Ticket #15369: 0003-SlippyMapBBoxChooser-paint-extents-of-downloaded-are.patch

File 0003-SlippyMapBBoxChooser-paint-extents-of-downloaded-are.patch, 5.1 KB (added by ris, 9 years ago)
  • src/org/openstreetmap/josm/gui/bbox/SlippyMapBBoxChooser.java

    From b01d3088f6ee27433a6e70a97e27de78dcb1b808 Mon Sep 17 00:00:00 2001
    From: Robert Scott <code@humanleg.org.uk>
    Date: Thu, 28 Sep 2017 23:37:02 +0100
    Subject: [PATCH 3/3] SlippyMapBBoxChooser: paint extents of downloaded area in
     similar style to OsmDataLayer
    
    using a technique which is almost directly lifted therefrom.
    ---
     .../josm/gui/bbox/SlippyMapBBoxChooser.java        | 56 +++++++++++++++++++++-
     1 file changed, 55 insertions(+), 1 deletion(-)
    
    diff --git a/src/org/openstreetmap/josm/gui/bbox/SlippyMapBBoxChooser.java b/src/org/openstreetmap/josm/gui/bbox/SlippyMapBBoxChooser.java
    index 6451dd8..1f653b4 100644
    a b import static org.openstreetmap.josm.tools.I18n.tr;  
    66import java.awt.Color;
    77import java.awt.Dimension;
    88import java.awt.Graphics;
     9import java.awt.Graphics2D;
    910import java.awt.Point;
    1011import java.awt.Rectangle;
     12import java.awt.TexturePaint;
     13import java.awt.geom.Area;
     14import java.awt.geom.Path2D;
     15import java.awt.image.BufferedImage;
    1116import java.util.ArrayList;
    1217import java.util.Arrays;
    1318import java.util.Collections;
    import org.openstreetmap.josm.data.imagery.TMSCachedTileLoader;  
    4146import org.openstreetmap.josm.data.imagery.TileLoaderFactory;
    4247import org.openstreetmap.josm.data.osm.BBox;
    4348import org.openstreetmap.josm.data.preferences.StringProperty;
     49import org.openstreetmap.josm.gui.MainApplication;
    4450import org.openstreetmap.josm.gui.layer.AbstractCachedTileSourceLayer;
     51import org.openstreetmap.josm.gui.layer.MainLayerManager;
     52import org.openstreetmap.josm.gui.layer.OsmDataLayer;
    4553import org.openstreetmap.josm.gui.layer.TMSLayer;
    4654import org.openstreetmap.josm.spi.preferences.Config;
    4755import org.openstreetmap.josm.tools.Logging;
    import org.openstreetmap.josm.tools.Logging;  
    4957/**
    5058 * This panel displays a map and lets the user chose a {@link BBox}.
    5159 */
    52 public class SlippyMapBBoxChooser extends JMapViewer implements BBoxChooser {
     60public class SlippyMapBBoxChooser extends JMapViewer implements BBoxChooser, MainLayerManager.ActiveLayerChangeListener {
    5361
    5462    /**
    5563     * A list of tile sources that can be used for displaying the map.
    public class SlippyMapBBoxChooser extends JMapViewer implements BBoxChooser {  
    189197            iSourceButton.setCurrentMap(tileSources.get(0));
    190198        }
    191199
     200        MainApplication.getLayerManager().addActiveLayerChangeListener(this);
     201
    192202        new SlippyMapControler(this, this);
    193203    }
    194204
    public class SlippyMapBBoxChooser extends JMapViewer implements BBoxChooser {  
    216226    @Override
    217227    public void paintComponent(Graphics g) {
    218228        super.paintComponent(g);
     229        Graphics2D g2d = (Graphics2D)g;
     230
     231        // draw hatched area for non-downloaded region of current "edit layer", but only if there *is* a current "edit layer",
     232        // and it has defined bounds. routime is analogous to that in OsmDataLayer's paint routine (but just different
     233        // enough to make sharing code impractical)
     234        final OsmDataLayer editLayer = MainApplication.getLayerManager().getEditLayer();
     235        if (editLayer != null && Config.getPref().getBoolean("draw.data.downloaded_area", true) && !editLayer.data.getDataSources().isEmpty()) {
     236            // initialize area with current viewport
     237            Rectangle b = this.getBounds();
     238            // ensure we comfortably cover full area
     239            b.grow(100, 100);
     240            Path2D p = new Path2D.Float();
     241
     242            // combine successively downloaded areas after converting to screen-space
     243            for (Bounds bounds : editLayer.data.getDataSourceBounds()) {
     244                if (bounds.isCollapsed()) {
     245                    continue;
     246                }
     247                Rectangle r = new Rectangle(this.getMapPosition(bounds.getMinLat(), bounds.getMinLon(), false));
     248                r.add(this.getMapPosition(bounds.getMaxLat(), bounds.getMaxLon(), false));
     249                p.append(r, false);
     250            }
     251            // subtract combined areas
     252            Area a = new Area(b);
     253            a.subtract(new Area(p));
     254
     255            // paint remainder
     256            BufferedImage hatchedTexture = editLayer.getHatchedTexture();
     257            int hatchedSize = hatchedTexture.getWidth();
     258            Rectangle anchorRect = new Rectangle(
     259                hatchedSize-(this.center.x % hatchedSize),
     260                hatchedSize-(this.center.y % hatchedSize),
     261                hatchedSize,
     262                hatchedSize
     263            );
     264            g2d.setPaint(new TexturePaint(hatchedTexture, anchorRect));
     265
     266            g2d.fill(a);
     267        }
    219268
    220269        // draw selection rectangle
    221270        if (iSelectionRectStart != null && iSelectionRectEnd != null) {
    public class SlippyMapBBoxChooser extends JMapViewer implements BBoxChooser {  
    230279        }
    231280    }
    232281
     282    @Override
     283    public void activeOrEditLayerChanged(MainLayerManager.ActiveLayerChangeEvent e) {
     284        this.repaint();
     285    }
     286
    233287    /**
    234288     * Enables the disk tile cache.
    235289     * @param enabled true to enable, false to disable