|
6 | 6 | package com.archimatetool.editor.diagram.util; |
7 | 7 |
|
8 | 8 | import org.eclipse.draw2d.FreeformFigure; |
| 9 | +import org.eclipse.draw2d.Graphics; |
9 | 10 | import org.eclipse.draw2d.IFigure; |
| 11 | +import org.eclipse.draw2d.ImagePrintFigureOperation; |
10 | 12 | import org.eclipse.draw2d.SWTGraphics; |
| 13 | +import org.eclipse.draw2d.geometry.Dimension; |
11 | 14 | import org.eclipse.draw2d.geometry.Rectangle; |
12 | 15 | import org.eclipse.gef.EditPartFactory; |
13 | 16 | import org.eclipse.gef.GraphicalViewer; |
|
16 | 19 | import org.eclipse.gef.editparts.FreeformGraphicalRootEditPart; |
17 | 20 | import org.eclipse.gef.editparts.LayerManager; |
18 | 21 | import org.eclipse.gef.ui.parts.GraphicalViewerImpl; |
19 | | -import org.eclipse.swt.graphics.AutoscalingMode; |
20 | 22 | import org.eclipse.swt.graphics.GC; |
21 | 23 | import org.eclipse.swt.graphics.Image; |
| 24 | +import org.eclipse.swt.graphics.ImageData; |
22 | 25 | import org.eclipse.swt.layout.FillLayout; |
23 | 26 | import org.eclipse.swt.widgets.Composite; |
| 27 | +import org.eclipse.swt.widgets.Control; |
24 | 28 | import org.eclipse.swt.widgets.Display; |
25 | 29 | import org.eclipse.swt.widgets.Shell; |
26 | 30 |
|
27 | 31 | import com.archimatetool.editor.diagram.DiagramEditorFactoryExtensionHandler; |
28 | 32 | import com.archimatetool.editor.diagram.IDiagramEditorFactory; |
29 | 33 | import com.archimatetool.editor.diagram.editparts.ArchimateDiagramEditPartFactory; |
| 34 | +import com.archimatetool.editor.diagram.figures.FigureUtils; |
30 | 35 | import com.archimatetool.editor.diagram.sketch.editparts.SketchEditPartFactory; |
31 | 36 | import com.archimatetool.model.IArchimateDiagramModel; |
32 | 37 | import com.archimatetool.model.IDiagramModel; |
@@ -70,7 +75,7 @@ else if(model instanceof ISketchModel) { |
70 | 75 | } |
71 | 76 |
|
72 | 77 | GraphicalViewerImpl viewer = new GraphicalViewerImpl(); |
73 | | - viewer.createControl(parent).setAutoscalingMode(AutoscalingMode.ENABLED); // Stops text clipping on Windows |
| 78 | + viewer.createControl(parent); |
74 | 79 |
|
75 | 80 | viewer.setEditPartFactory(editPartFactory); |
76 | 81 |
|
@@ -155,6 +160,21 @@ private static ModelReferencedImage createModelReferencedImage(IFigure figure, d |
155 | 160 | bounds.expand(margin / scale, margin / scale); |
156 | 161 | } |
157 | 162 |
|
| 163 | + // Use Image Operation |
| 164 | + // TODO: Use ImageGraphics to set scale |
| 165 | + if(FigureUtils.isAutoScaleEnabled()) { |
| 166 | + Shell shell = new Shell(); |
| 167 | + try { |
| 168 | + ImagePrintDiagramOperation op = new ImagePrintDiagramOperation(shell, figure, scale, bounds); |
| 169 | + Image image = op.run(); |
| 170 | + return new ModelReferencedImage(image, bounds); |
| 171 | + } |
| 172 | + finally { |
| 173 | + shell.dispose(); |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + // Use manual method |
158 | 178 | Image image = new Image(Display.getDefault(), (int)(bounds.width * scale), (int)(bounds.height * scale)); |
159 | 179 | GC gc = new GC(image); |
160 | 180 | SWTGraphics graphics = new ImageGraphics(gc); // Use ImageGraphics so we can get actual scale |
@@ -220,4 +240,42 @@ public static Rectangle getMinimumBounds(IFigure figure) { |
220 | 240 |
|
221 | 241 | return minimumBounds; |
222 | 242 | } |
| 243 | + |
| 244 | + /** |
| 245 | + * ImagePrintDiagramOperation |
| 246 | + * TODO: Use ImageGraphics to set scale or get scale some other way |
| 247 | + */ |
| 248 | + private static class ImagePrintDiagramOperation extends ImagePrintFigureOperation { |
| 249 | + private final Rectangle bounds; |
| 250 | + private final double scale; |
| 251 | + |
| 252 | + public ImagePrintDiagramOperation(Control control, IFigure printSource, double scale, Rectangle bounds) { |
| 253 | + super(control, printSource); |
| 254 | + this.scale = scale; |
| 255 | + this.bounds = bounds.getCopy(); // Important to use a copy in case bounds is changed elsewhere |
| 256 | + } |
| 257 | + |
| 258 | + @Override |
| 259 | + protected ImageData getImageDataAtZoom(int zoom) { |
| 260 | + // Zoom can be < 100 see https://github.com/eclipse-gef/gef-classic/issues/1146 |
| 261 | + if(zoom < 100) { |
| 262 | + return null; |
| 263 | + } |
| 264 | + return super.getImageDataAtZoom(zoom); |
| 265 | + } |
| 266 | + |
| 267 | + @Override |
| 268 | + protected Dimension getImageSize() { |
| 269 | + // Scale the size (width and height) of the bounds |
| 270 | + return bounds.getSize().scale(scale); |
| 271 | + } |
| 272 | + |
| 273 | + @Override |
| 274 | + protected void preparePrintSource(Graphics graphics) { |
| 275 | + // Scale |
| 276 | + graphics.scale(scale); |
| 277 | + // Compensate for negative co-ordinates |
| 278 | + graphics.translate(bounds.x * -1, bounds.y * -1); |
| 279 | + } |
| 280 | + } |
223 | 281 | } |
0 commit comments