source: josm/trunk/src/org/openstreetmap/josm/actions/mapmode/ZoomAction.java

Last change on this file was 16900, checked in by simon04, 4 years ago

fix #19692 - Zoom mode: zoom in/out depending on rectangle

  • Property svn:eol-style set to native
File size: 2.8 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions.mapmode;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.Rectangle;
7import java.awt.event.KeyEvent;
8import java.awt.event.MouseEvent;
9
10import org.openstreetmap.josm.gui.MainApplication;
11import org.openstreetmap.josm.gui.MapFrame;
12import org.openstreetmap.josm.gui.MapView;
13import org.openstreetmap.josm.gui.SelectionManager;
14import org.openstreetmap.josm.gui.SelectionManager.SelectionEnded;
15import org.openstreetmap.josm.tools.ImageProvider;
16import org.openstreetmap.josm.tools.Shortcut;
17
18/**
19 * Enable the zoom mode within the MapFrame.
20 *
21 * Holding down the left mouse button select a rectangle with the same aspect
22 * ratio than the current map view.
23 * Holding down left and right let the user move the former selected rectangle.
24 * Releasing the left button zoom to the selection.
25 *
26 * Rectangle selections with either height or width smaller than 3 pixels
27 * are ignored.
28 *
29 * @author imi
30 * @since 1
31 */
32public class ZoomAction extends MapMode implements SelectionEnded {
33
34 /**
35 * Manager that manages the selection rectangle with the aspect ratio of the MapView.
36 */
37 private final transient SelectionManager selectionManager;
38
39 /**
40 * Construct a ZoomAction without a label.
41 * @param mapFrame The MapFrame, whose zoom mode should be enabled.
42 */
43 public ZoomAction(MapFrame mapFrame) {
44 super(tr("Zoom mode"), "zoom", tr("Zoom and move map"),
45 Shortcut.registerShortcut("mapmode:zoom", tr("Mode: {0}", tr("Zoom mode")), KeyEvent.CHAR_UNDEFINED, Shortcut.NONE),
46 ImageProvider.getCursor("normal", "zoom"));
47 selectionManager = new SelectionManager(this, true, mapFrame.mapView);
48 }
49
50 /**
51 * Zoom to the rectangle on the map.
52 */
53 @Override
54 public void selectionEnded(Rectangle r, MouseEvent e) {
55 if (r.width >= 3 && r.height >= 3 && MainApplication.isDisplayingMapView()) {
56 MapView mv = MainApplication.getMap().mapView;
57 final double factor;
58 if (r.x == e.getPoint().x || r.y == e.getPoint().y) {
59 factor = mv.getWidth() / r.getWidth(); // zoom out
60 } else {
61 factor = r.getWidth() / mv.getWidth(); // zoom in
62 }
63 mv.zoomToFactor(mv.getEastNorth(r.x + r.width / 2, r.y + r.height / 2), factor);
64 }
65 }
66
67 @Override public void enterMode() {
68 super.enterMode();
69 selectionManager.register(MainApplication.getMap().mapView, false);
70 }
71
72 @Override public void exitMode() {
73 super.exitMode();
74 selectionManager.unregister(MainApplication.getMap().mapView);
75 }
76
77 @Override public String getModeHelpText() {
78 return tr("Zoom by dragging or Ctrl+. or Ctrl+,; move with Ctrl+up, left, down, right; move zoom with right button");
79 }
80}
Note: See TracBrowser for help on using the repository browser.