Ticket #17050: refactor_geoimagelayer_plugin.patch
| File refactor_geoimagelayer_plugin.patch, 21.0 KB (added by , 7 years ago) |
|---|
-
plugins/photoadjust/src/org/openstreetmap/josm/plugins/photoadjust/PhotoAdjustWorker.java
diff --git a/plugins/photoadjust/src/org/openstreetmap/josm/plugins/photoadjust/PhotoAdjustWorker.java b/plugins/photoadjust/src/org/openstreetmap/josm/plugins/photoadjust/PhotoAdjustWorker.java index 931b749b7..9fba87e2e 100644
a b import java.awt.event.MouseEvent; 6 6 import java.awt.geom.Point2D; 7 7 import java.util.List; 8 8 9 import org.openstreetmap.josm.data.ImageData; 9 10 import org.openstreetmap.josm.data.coor.LatLon; 10 11 import org.openstreetmap.josm.gui.MainApplication; 11 12 import org.openstreetmap.josm.gui.layer.geoimage.GeoImageLayer; … … import org.openstreetmap.josm.gui.layer.geoimage.ImageViewerDialog; 18 19 public class PhotoAdjustWorker { 19 20 20 21 private ImageEntry dragPhoto; 21 private GeoImageLayer dragLayer;22 private ImageData dragData; 22 23 // Offset between center of the photo and point where it is 23 24 // clicked. This must be in pixels to maintain the same offset if 24 25 // the photo is moved very far. … … public class PhotoAdjustWorker { 31 32 */ 32 33 public void reset() { 33 34 dragPhoto = null; 34 drag Layer= null;35 dragData = null; 35 36 dragOffset = null; 36 37 } 37 38 … … public class PhotoAdjustWorker { 72 73 * @param imageLayers List of GeoImageLayers to be considered. 73 74 */ 74 75 public void doMousePressed(MouseEvent evt, 75 List<GeoImageLayer> imageLayers) {76 List<GeoImageLayer> imageLayers) { 76 77 reset(); 77 78 78 79 if (evt.getButton() == MouseEvent.BUTTON1 79 && imageLayers != null && !imageLayers.isEmpty()) {80 && imageLayers != null && !imageLayers.isEmpty()) { 80 81 // Check if modifier key is pressed and change to 81 82 // image viewer photo if it is. 82 83 final boolean isShift = (evt.getModifiers() & InputEvent.SHIFT_MASK) != 0; 83 84 final boolean isCtrl = (evt.getModifiers() & InputEvent.CTRL_MASK) != 0; 84 85 if (isShift || isCtrl) { 85 final GeoImageLayer viewerLayer = ImageViewerDialog.getCurrentLayer(); 86 final ImageEntry img = ImageViewerDialog.getCurrentImage(); 87 if (img != null && viewerLayer != null 88 && viewerLayer.isVisible() 89 && imageLayers.contains(viewerLayer)) { 90 // Change direction if control is pressed, position 91 // otherwise. Shift+control changes direction, similar to 92 // rotate in select mode. 93 // 94 // Combinations: 95 // S ... shift pressed 96 // C ... control pressed 97 // pos ... photo has a position set == is displayed on the map 98 // nopos ... photo has no position set 99 // 100 // S + pos: position at mouse 101 // S + nopos: position at mouse 102 // C + pos: change orientation 103 // C + nopos: ignored 104 // S + C + pos: change orientation 105 // S + C + nopos: ignore 106 if (isCtrl) { 107 if (img.getPos() != null) { 108 changeDirection(img, viewerLayer, evt); 86 for (GeoImageLayer layer: imageLayers) { 87 if (layer.isVisible()) { 88 final ImageEntry img = layer.getImageData().getSelectedImage(); 89 if (img != null) { 90 // Change direction if control is pressed, position 91 // otherwise. Shift+control changes direction, similar to 92 // rotate in select mode. 93 // 94 // Combinations: 95 // S ... shift pressed 96 // C ... control pressed 97 // pos ... photo has a position set == is displayed on the map 98 // nopos ... photo has no position set 99 // 100 // S + pos: position at mouse 101 // S + nopos: position at mouse 102 // C + pos: change orientation 103 // C + nopos: ignored 104 // S + C + pos: change orientation 105 // S + C + nopos: ignore 106 if (isCtrl) { 107 if (img.getPos() != null) { 108 changeDirection(img, layer.getImageData(), evt); 109 } 110 } else { // shift pressed 111 movePhoto(img, layer.getImageData(), evt); 112 } 113 dragPhoto = img; 114 dragData = layer.getImageData(); 115 break; 109 116 } 110 } else { // shift pressed111 movePhoto(img, viewerLayer, evt);112 117 } 113 dragPhoto = img;114 dragLayer = viewerLayer;115 118 } 116 119 } else { 117 120 // Start with the top layer. … … public class PhotoAdjustWorker { 119 122 if (layer.isVisible()) { 120 123 dragPhoto = layer.getPhotoUnderMouse(evt); 121 124 if (dragPhoto != null) { 122 drag Layer = layer;125 dragData = layer.getImageData(); 123 126 setDragOffset(dragPhoto, evt); 124 127 disableCenterView(); 125 128 break; … … public class PhotoAdjustWorker { 149 152 * @param evt Mouse event from MouseMotionAdapter mouseDragged(). 150 153 */ 151 154 public void doMouseDragged(MouseEvent evt) { 152 if (dragLayer != null && dragLayer.isVisible() 153 && dragPhoto != null) { 155 if (dragData != null && dragPhoto != null) { 154 156 if ((evt.getModifiers() & InputEvent.CTRL_MASK) != 0) { 155 changeDirection(dragPhoto, drag Layer, evt);157 changeDirection(dragPhoto, dragData, evt); 156 158 } else { 157 159 disableCenterView(); 158 movePhoto(dragPhoto, drag Layer, evt);160 movePhoto(dragPhoto, dragData, evt); 159 161 } 160 162 } 161 163 } … … public class PhotoAdjustWorker { 176 178 * Move the photo to the mouse position. 177 179 * 178 180 * @param photo The photo to move. 179 * @param layer GeoImageLayerof the photo.181 * @param data ImageData of the photo. 180 182 * @param evt Mouse event from one of the mouse adapters. 181 183 */ 182 private void movePhoto(ImageEntry photo, GeoImageLayer layer,183 MouseEvent evt) {184 private void movePhoto(ImageEntry photo, ImageData data, 185 MouseEvent evt) { 184 186 LatLon newPos; 185 187 if (dragOffset != null) { 186 188 newPos = MainApplication.getMap().mapView.getLatLon( … … public class PhotoAdjustWorker { 189 191 } else { 190 192 newPos = MainApplication.getMap().mapView.getLatLon(evt.getX(), evt.getY()); 191 193 } 192 photo.setPos(newPos); 193 photo.flagNewGpsData(); 194 layer.updateBufferAndRepaint(); 194 data.updateImagePosition(photo, newPos); 195 195 // Re-display the photo because the OSD data might change (new 196 196 // coordinates). Or do that in doMouseReleased(). 197 197 //ImageViewerDialog.showImage(layer, photo); … … public class PhotoAdjustWorker { 201 201 * Set the image direction, i.e. let it point to where the mouse is. 202 202 * 203 203 * @param photo The photo to move. 204 * @param layer GeoImageLayerof the photo.204 * @param data ImageData of the photo. 205 205 * @param evt Mouse event from one of the mouse adapters. 206 206 */ 207 private void changeDirection(ImageEntry photo, GeoImageLayer layer,208 MouseEvent evt) {207 private void changeDirection(ImageEntry photo, ImageData data, 208 MouseEvent evt) { 209 209 final LatLon photoLL = photo.getPos(); 210 210 if (photoLL == null) { 211 211 // Direction cannot be set if image doesn't have a position. … … public class PhotoAdjustWorker { 219 219 } else if (direction >= 360.0) { 220 220 direction -= 360.0; 221 221 } 222 photo.setExifImgDir(direction); 223 photo.flagNewGpsData(); 224 layer.updateBufferAndRepaint(); 225 ImageViewerDialog.showImage(layer, photo); 222 data.updateImageDirection(photo, direction); 226 223 setDragOffset(photo, evt); 227 224 } 228 225 } -
plugins/photoadjust/src/org/openstreetmap/josm/plugins/photoadjust/PhotoPropertyEditor.java
diff --git a/plugins/photoadjust/src/org/openstreetmap/josm/plugins/photoadjust/PhotoPropertyEditor.java b/plugins/photoadjust/src/org/openstreetmap/josm/plugins/photoadjust/PhotoPropertyEditor.java index 508c41c86..61837be1e 100644
a b import static org.openstreetmap.josm.tools.I18n.tr; 6 6 import java.awt.Color; 7 7 import java.awt.GridBagLayout; 8 8 import java.awt.event.ActionEvent; 9 import java.util.List; 10 import java.util.Optional; 9 11 10 12 import javax.swing.AbstractAction; 11 import javax.swing.Action;12 13 import javax.swing.BorderFactory; 13 14 import javax.swing.ImageIcon; 14 15 import javax.swing.JButton; 15 16 import javax.swing.JLabel; 16 import javax.swing.JOptionPane;17 17 import javax.swing.JPanel; 18 18 import javax.swing.JSeparator; 19 19 import javax.swing.UIManager; … … import javax.swing.event.DocumentEvent; 21 21 import javax.swing.event.DocumentListener; 22 22 23 23 import org.openstreetmap.josm.actions.JosmAction; 24 import org.openstreetmap.josm.data.ImageData; 25 import org.openstreetmap.josm.data.ImageData.ImageDataUpdateListener; 24 26 import org.openstreetmap.josm.data.coor.LatLon; 25 27 import org.openstreetmap.josm.data.coor.conversion.CoordinateFormatManager; 26 28 import org.openstreetmap.josm.data.coor.conversion.LatLonParser; … … import org.openstreetmap.josm.gui.ExtendedDialog; 29 31 import org.openstreetmap.josm.gui.MainApplication; 30 32 import org.openstreetmap.josm.gui.MainMenu; 31 33 import org.openstreetmap.josm.gui.dialogs.LatLonDialog; 34 import org.openstreetmap.josm.gui.layer.Layer; 35 import org.openstreetmap.josm.gui.layer.LayerManager.LayerAddEvent; 36 import org.openstreetmap.josm.gui.layer.LayerManager.LayerChangeListener; 37 import org.openstreetmap.josm.gui.layer.LayerManager.LayerOrderChangeEvent; 38 import org.openstreetmap.josm.gui.layer.LayerManager.LayerRemoveEvent; 32 39 import org.openstreetmap.josm.gui.layer.geoimage.GeoImageLayer; 33 40 import org.openstreetmap.josm.gui.layer.geoimage.ImageEntry; 34 import org.openstreetmap.josm.gui.layer.geoimage.ImageViewerDialog;35 41 import org.openstreetmap.josm.gui.widgets.JosmTextField; 36 42 import org.openstreetmap.josm.tools.GBC; 37 43 import org.openstreetmap.josm.tools.ImageProvider; … … public class PhotoPropertyEditor { 56 62 MainMenu.add(MainApplication.getMenu().editMenu, new PropertyEditorAction()); 57 63 } 58 64 59 /**60 * Update the geo image layer and the image viewer.61 *62 * @param layer GeoImageLayer of the photo.63 * @param photo The photo that is updated.64 */65 private static void updateLayer(GeoImageLayer layer, ImageEntry photo) {66 layer.updateBufferAndRepaint();67 ImageViewerDialog.showImage(layer, photo);68 }69 70 65 /** 71 66 * Action if the menu entry is selected. 72 67 */ 73 private static class PropertyEditorAction extends JosmAction {68 private static class PropertyEditorAction extends JosmAction implements LayerChangeListener, ImageDataUpdateListener { 74 69 public PropertyEditorAction() { 75 70 super(tr("Edit photo GPS data"), // String name 76 (String)null, // String iconName 77 tr("Edit GPS data of selected photo."), // String tooltip 78 null, // Shortcut shortcut 79 true, // boolean registerInToolbar 80 "photoadjust/propertyeditor", // String toolbarId 81 true // boolean installAdapters 82 ); 71 (String)null, // String iconName 72 tr("Edit GPS data of selected photo."), // String tooltip 73 null, // Shortcut shortcut 74 true, // boolean registerInToolbar 75 "photoadjust/propertyeditor", // String toolbarId 76 false // boolean installAdapters 77 ); 78 this.installAdapters(); 79 } 80 81 @Override 82 protected void installAdapters() { 83 MainApplication.getLayerManager().addLayerChangeListener(this); 84 initEnabledState(); 83 85 } 84 86 85 87 @Override 86 88 public void actionPerformed(ActionEvent evt) { 87 try { 88 final ImageEntry photo = ImageViewerDialog.getCurrentImage(); 89 final GeoImageLayer layer = ImageViewerDialog.getCurrentLayer(); 90 if (photo == null) { 91 throw new AssertionError("No image selected."); 92 } 93 StringBuilder title = 89 final ImageData data = getLayerWithSelectedImage().get().getImageData(); 90 final ImageEntry photo = data.getSelectedImage(); 91 92 StringBuilder title = 94 93 new StringBuilder(tr("Edit Photo GPS Data")); 95 if (photo.getFile() != null) { 96 title.append(" - "); 97 title.append(photo.getFile().getName()); 98 } 99 PropertyEditorDialog dialog = 100 new PropertyEditorDialog(title.toString(), photo, layer); 101 if (dialog.getValue() == 1) { 102 dialog.updateImageTmp(); 103 // There are cases where isNewGpsData is not set but there 104 // is still new data, e.g. if the EXIF data was re-read 105 // from the image file. 106 photo.applyTmp(); 107 } else { 108 photo.discardTmp(); 109 } 110 updateLayer(layer, photo); 111 } catch (AssertionError err) { 112 JOptionPane.showMessageDialog(MainApplication.getMainFrame(), 113 tr("Please select an image first."), 114 tr("No image selected"), 115 JOptionPane.INFORMATION_MESSAGE); 116 return; 94 if (photo.getFile() != null) { 95 title.append(" - "); 96 title.append(photo.getFile().getName()); 97 } 98 PropertyEditorDialog dialog = 99 new PropertyEditorDialog(title.toString(), photo, data); 100 if (dialog.getValue() == 1) { 101 dialog.updateImageTmp(); 102 // There are cases where isNewGpsData is not set but there 103 // is still new data, e.g. if the EXIF data was re-read 104 // from the image file. 105 photo.applyTmp(); 106 } else { 107 photo.discardTmp(); 117 108 } 118 } 109 data.notifyImageUpdate(); 110 } 119 111 120 112 /** 121 113 * Check if there is a selected image. … … public class PhotoPropertyEditor { 124 116 * image shown, {@code false} otherwise. 125 117 */ 126 118 private static boolean enabled() { 127 try { 128 //return ImageViewerDialog.getInstance().hasImage(); 129 ImageViewerDialog.getInstance().hasImage(); 130 return true; 131 } catch (AssertionError err) { 132 return false; 133 } 119 return getLayerWithSelectedImage().isPresent(); 120 } 121 122 private static Optional<GeoImageLayer> getLayerWithSelectedImage() { 123 List<GeoImageLayer> list = MainApplication.getLayerManager().getLayersOfType(GeoImageLayer.class); 124 return list.stream().filter(l -> l.getImageData().getSelectedImage() != null).findFirst(); 134 125 } 135 126 136 127 @Override 137 128 protected void updateEnabledState() { 138 129 setEnabled(enabled()); 139 130 } 131 132 @Override 133 public void layerAdded(LayerAddEvent e) { 134 Layer layer = e.getAddedLayer(); 135 if (layer instanceof GeoImageLayer) { 136 ((GeoImageLayer) layer).getImageData().addImageDataUpdateListener(this); 137 } 138 } 139 140 @Override 141 public void layerRemoving(LayerRemoveEvent e) { 142 Layer layer = e.getRemovedLayer(); 143 144 if (layer instanceof GeoImageLayer) { 145 ((GeoImageLayer) layer).getImageData().removeImageDataUpdateListener(this); 146 } 147 this.updateEnabledState(); 148 } 149 150 @Override 151 public void layerOrderChanged(LayerOrderChangeEvent e) { 152 // ignored 153 } 154 155 @Override 156 public void imageDataUpdated(ImageData data) { 157 // ignored 158 } 159 160 @Override 161 public void selectedImageChanged(ImageData data) { 162 this.updateEnabledState(); 163 } 140 164 } 141 165 142 166 /** … … public class PhotoPropertyEditor { 149 173 private final JosmTextField direction = new JosmTextField(); 150 174 // Image that is to be updated. 151 175 private final ImageEntry image; 152 private final GeoImageLayer layer;176 private final ImageData data; 153 177 // Image as it was when the dialog was opened. 154 178 private final GpxImageEntry imgOrig; 155 179 private static final Color BG_COLOR_ERROR = new Color(255, 224, 224); 156 180 157 181 public PropertyEditorDialog(String title, final ImageEntry image, 158 final GeoImageLayer layer) {182 final ImageData data) { 159 183 super(MainApplication.getMainFrame(), title, tr("Ok"), tr("Cancel")); 160 184 this.image = image; 161 this. layer = layer;185 this.data = data; 162 186 imgOrig = image.clone(); 163 187 setButtonIcons("ok", "cancel"); 164 188 final JPanel content = new JPanel(new GridBagLayout()); … … public class PhotoPropertyEditor { 199 223 DoubleInputVerifier altVerif = new DoubleInputVerifier(altitude) { 200 224 @Override public void updateValue(Double value) { 201 225 image.getTmp().setElevation(value); 202 updateLayer(layer, image);226 data.notifyImageUpdate(); 203 227 } 204 228 }; 205 229 altitude.getDocument().addDocumentListener(altVerif); … … public class PhotoPropertyEditor { 213 237 DoubleInputVerifier speedVerif = new DoubleInputVerifier(speed) { 214 238 @Override public void updateValue(Double value) { 215 239 image.getTmp().setSpeed(value); 216 updateLayer(layer, image);240 data.notifyImageUpdate(); 217 241 } 218 242 }; 219 243 speedVerif.setMinMax(0.0, null); … … public class PhotoPropertyEditor { 229 253 DoubleInputVerifier dirVerif = new DoubleInputVerifier(direction) { 230 254 @Override public void updateValue(Double value) { 231 255 image.getTmp().setExifImgDir(value); 232 updateLayer(layer, image);256 data.notifyImageUpdate(); 233 257 } 234 258 }; 235 259 dirVerif.setMinMax(-360.0, 360.0); … … public class PhotoPropertyEditor { 572 596 @Override 573 597 public void updateLatLon(LatLon latLon) { 574 598 image.getTmp().setPos(latLon); 575 updateLayer(layer, image);599 data.notifyImageUpdate(); 576 600 } 577 601 578 602 @Override -
plugins/photoadjust/src/org/openstreetmap/josm/plugins/photoadjust/UntaggedGeoImageLayerAction.java
diff --git a/plugins/photoadjust/src/org/openstreetmap/josm/plugins/photoadjust/UntaggedGeoImageLayerAction.java b/plugins/photoadjust/src/org/openstreetmap/josm/plugins/photoadjust/UntaggedGeoImageLayerAction.java index 69c056680..d8cbd40b4 100644
a b public class UntaggedGeoImageLayerAction 53 53 // Move this image to the new layer and delete it 54 54 // from the original layer. 55 55 untagged.add(img); 56 layer. removePhotoByIdx(idx);56 layer.getImageData().removeImage(img); 57 57 } 58 58 } 59 59 MainApplication.getLayerManager()
