Changeset 29412 in osm for applications/editors/josm


Ignore:
Timestamp:
2013-03-27T20:56:51+01:00 (11 years ago)
Author:
zverik
Message:

iodb: save last layer offset, update min josm version, reduce max button count

Location:
applications/editors/josm/plugins/imagery_offset_db
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/imagery_offset_db/build.xml

    r29390 r29412  
    3232    <property name="commit.message" value="Imagery Offset Database"/>
    3333    <!-- enter the *lowest* JOSM version this plugin is currently compatible with -->
    34     <property name="plugin.main.version" value="4549"/>
     34    <property name="plugin.main.version" value="4666"/>
    3535    <!--
    3636      ************************************************
  • applications/editors/josm/plugins/imagery_offset_db/src/iodb/ImageryOffsetPlugin.java

    r29408 r29412  
    55import javax.swing.JMenu;
    66import org.openstreetmap.josm.Main;
     7import org.openstreetmap.josm.data.Version;
    78import org.openstreetmap.josm.plugins.Plugin;
    89import org.openstreetmap.josm.plugins.PluginInformation;
     
    3132        storeAction = new StoreImageryOffsetAction();
    3233       
    33         JMenu offsetMenu = Main.main.menu.addMenu(marktr("Offset"), KeyEvent.VK_O, 6, "help");
     34        // before 5803 imagery menu was constantly regenerated, erasing extra items
     35        // before 5729 it was regenerated only when the imagery list was modified (also bad)
     36        int version = Version.getInstance().getVersion();
     37        JMenu offsetMenu = version < 5803
     38                ? Main.main.menu.addMenu(marktr("Offset"), KeyEvent.VK_O, 6, "help")
     39                : Main.main.menu.imageryMenu;
    3440        offsetMenu.add(getAction);
    3541        offsetMenu.add(storeAction);
     42        if( version >= 5803 ) // todo: check if this is needed
     43            offsetMenu.addSeparator();
    3644
    3745        // an ugly hack to add this plugin to the toolbar
    38         Collection<String> toolbar = new LinkedList<String>(Main.toolbar.getToolString());
    39         if( !toolbar.contains("getoffset") && Main.pref.getBoolean("iodb.modify.toolbar", true) ) {
    40             toolbar.add("getoffset");
    41             Main.pref.putCollection("toolbar", toolbar);
     46        if( Main.pref.getBoolean("iodb.modify.toolbar", true) ) {
     47            Collection<String> toolbar = new LinkedList<String>(Main.toolbar.getToolString());
     48            if( !toolbar.contains("getoffset") ) {
     49                toolbar.add("getoffset");
     50                Main.pref.putCollection("toolbar", toolbar);
     51                Main.toolbar.refreshToolbarControl();
     52            }
    4253            Main.pref.put("iodb.modify.toolbar", false);
    43             Main.toolbar.refreshToolbarControl();
    4454        }
    4555    }
  • applications/editors/josm/plugins/imagery_offset_db/src/iodb/ImageryOffsetWatcher.java

    r29402 r29412  
    77import org.openstreetmap.josm.gui.layer.ImageryLayer;
    88import org.openstreetmap.josm.gui.layer.Layer;
     9import org.openstreetmap.josm.tools.Destroyable;
    910
    1011/**
     
    1516 * @license WTFPL
    1617 */
    17 public class ImageryOffsetWatcher implements MapView.ZoomChangeListener, MapView.LayerChangeListener {
     18public class ImageryOffsetWatcher implements MapView.ZoomChangeListener, MapView.LayerChangeListener, Destroyable {
    1819    private static final double THRESHOLD = 1e-8;
    1920    private static ImageryOffsetWatcher instance;
     
    118119                data.lastDy = layer.getDy();
    119120                data.lastChecked = center;
     121                storeLayerOffset(layer);
    120122                setOffsetGood(true);
    121123            } else {
     
    125127    }
    126128
     129    /**
     130     * Mark the current offset as good. This method is called by {@link OffsetDialog}
     131     * to notify the watcher that an offset button has been clicked, and regardless of
     132     * whether it has changed an offset, the currect imagery alignment is ok.
     133     */
    127134    public void markGood() {
    128135        ImageryLayer layer = ImageryOffsetTools.getTopImageryLayer();
     
    143150                data.lastChecked = center;
    144151            }
     152            storeLayerOffset(layer);
    145153        }
    146154        setOffsetGood(true);
     
    166174
    167175    public void layerAdded( Layer newLayer ) {
     176        if( newLayer instanceof ImageryLayer )
     177            loadLayerOffset((ImageryLayer)newLayer);
    168178        checkOffset();
    169179    }
     
    171181    public void layerRemoved( Layer oldLayer ) {
    172182        checkOffset();
     183    }
     184
     185    /**
     186     * Saves the current imagery layer offset to preferences. It is stored as a
     187     * collection of ':'-separated strings: imagery_id:lat:lon:dx:dy. No need for
     188     * projections: nobody uses them anyway.
     189     */
     190    private void storeLayerOffset( ImageryLayer layer ) {
     191        String id = ImageryOffsetTools.getImageryID(layer);
     192        if( !Main.pref.getBoolean("iodb.remember.offsets", true) || id == null )
     193            return;
     194        Collection<String> offsets = new LinkedList<String>(Main.pref.getCollection("iodb.stored.offsets"));
     195        for( Iterator<String> iter = offsets.iterator(); iter.hasNext(); ) {
     196            String[] offset = iter.next().split(":");
     197            if( offset.length == 5 && offset[0].equals(id) )
     198                iter.remove();
     199        }
     200        LatLon center = ImageryOffsetTools.getMapCenter();
     201        offsets.add(id + ":" + center.lat() + ":" + center.lon() + ":" + layer.getDx() + ":" + layer.getDy());
     202        Main.pref.putCollection("iodb.stored.offsets", offsets);
     203    }
     204
     205    /**
     206     * Loads the current imagery layer offset from preferences.
     207     */
     208    private void loadLayerOffset( ImageryLayer layer ) {
     209        String id = ImageryOffsetTools.getImageryID(layer);
     210        if( !Main.pref.getBoolean("iodb.remember.offsets", true) || id == null )
     211            return;
     212        Collection<String> offsets = Main.pref.getCollection("iodb.stored.offsets");
     213        for( String offset : offsets ) {
     214            String[] parts = offset.split(":");
     215            if( parts.length == 5 && parts[0].equals(id) ) {
     216                double[] dparts = new double[4];
     217                try {
     218                    for( int i = 0; i < 4; i++ )
     219                        dparts[i] = Double.parseDouble(parts[i+1]);
     220                } catch( Exception e ) {
     221                    continue;
     222                }
     223                LatLon lastPos = new LatLon(dparts[0], dparts[1]);
     224                if( lastPos.greatCircleDistance(ImageryOffsetTools.getMapCenter()) < Math.max(maxDistance, 3.0) * 1000 ) {
     225                    // apply offset
     226                    layer.setOffset(dparts[2], dparts[3]);
     227                    return;
     228                }
     229            }
     230        }
    173231    }
    174232
  • applications/editors/josm/plugins/imagery_offset_db/src/iodb/OffsetDialog.java

    r29402 r29412  
    3131    protected static final String PREF_CALIBRATION = "iodb.show.calibration";
    3232    protected static final String PREF_DEPRECATED = "iodb.show.deprecated";
    33     private static final int MAX_OFFSETS = Main.main.pref.getInteger("iodb.max.offsets", 5);
     33    private static final int MAX_OFFSETS = Main.main.pref.getInteger("iodb.max.offsets", 4);
    3434
    3535    /**
Note: See TracChangeset for help on using the changeset viewer.