Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / layout / FLayoutZooms.java @ 28368

History | View | Annotate | Download (16.2 KB)

1 7304 caballero
/*
2
 * Created on 17-sep-2004
3
 *
4
 */
5
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
6
 *
7
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
8
 *
9
 * This program is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU General Public License
11
 * as published by the Free Software Foundation; either version 2
12
 * of the License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
22
 *
23
 * For more information, contact:
24
 *
25
 *  Generalitat Valenciana
26
 *   Conselleria d'Infraestructures i Transport
27
 *   Av. Blasco Ib??ez, 50
28
 *   46010 VALENCIA
29
 *   SPAIN
30
 *
31
 *      +34 963862235
32
 *   gvsig@gva.es
33
 *      www.gvsig.gva.es
34
 *
35
 *    or
36
 *
37
 *   IVER T.I. S.A
38
 *   Salamanca 50
39
 *   46005 Valencia
40
 *   Spain
41
 *
42
 *   +34 963163400
43
 *   dac@iver.es
44
 */
45
package com.iver.cit.gvsig.project.documents.layout;
46
47
import java.awt.Point;
48
import java.awt.Toolkit;
49 25996 vcaballero
import java.awt.geom.AffineTransform;
50 7304 caballero
import java.awt.geom.Point2D;
51
import java.awt.geom.Rectangle2D;
52 7680 caballero
import java.util.prefs.Preferences;
53 7304 caballero
54 7738 jaume
import com.iver.cit.gvsig.fmap.MapContext;
55
import com.iver.cit.gvsig.project.documents.layout.fframes.IFFrame;
56
import com.iver.cit.gvsig.project.documents.layout.fframes.IFFrameUseFMap;
57 9392 caballero
import com.iver.cit.gvsig.project.documents.layout.gui.Layout;
58 7304 caballero
59 7738 jaume
60 7304 caballero
/**
61
 * Clase encargada de realizar los zooms al Layout.
62
 *
63
 * @author Vicente Caballero Navarro
64
 */
65
public class FLayoutZooms {
66
    private Layout layout = null;
67
68
    public FLayoutZooms(Layout l) {
69
        layout = l;
70
    }
71
72
    /**
73
         * Realiza un zoom por rect?ngulo o por punto con un escalado por defecto
74
         * sobre el Layout que se le pasa como par?metro.
75
         *
76
         * @param p1 punto de inicio del rect?ngulo.
77
         * @param p2 punto final del rec?ngulo.
78
         */
79
    public void setZoomIn(Point p1, Point p2) {
80 9392 caballero
            if (java.lang.Math.abs(layout.getLayoutControl().getFirstPoint().x - p2.x) < 4) {
81 7304 caballero
                        double difw = 2;
82
                        setZoom(difw, p2);
83
                } else {
84
                        if (p1.getX() > p2.getX()) {
85
                                int aux = p2.x;
86
                                p2.x = p1.x;
87
                                p1.x = aux;
88
                        }
89
90
                        if (p1.getY() > p2.getY()) {
91
                                int aux = p2.y;
92
                                p2.y = p1.y;
93
                                p1.y = aux;
94
                        }
95
96
                        Point2D.Double pSheet1 = FLayoutUtilities.toSheetPoint(
97 9392 caballero
                                        new Point2D.Double(p1.getX(), p1.getY()), layout.getLayoutControl().getAT());
98 7304 caballero
                        Point2D.Double pSheet2 = FLayoutUtilities.toSheetPoint(
99 9392 caballero
                                        new Point2D.Double(p2.getX(), p2.getY()), layout.getLayoutControl().getAT());
100 7304 caballero
101
                        double xmin;
102
                        double xmax;
103
                        double ymin;
104
                        double ymax = 0;
105
106
                        if (pSheet1.x > pSheet2.x) {
107
                                xmin = pSheet2.x;
108
                                xmax = pSheet1.x;
109
                        } else {
110
                                xmin = pSheet1.x;
111
                                xmax = pSheet2.x;
112
                        }
113
114
                        if (pSheet1.y > pSheet2.y) {
115
                                ymin = pSheet2.y;
116
                                ymax = pSheet1.y;
117
                        } else {
118
                                ymin = pSheet1.y;
119
                                ymax = pSheet2.y;
120
                        }
121
122
                        Rectangle2D.Double rScreen = new Rectangle2D.Double();
123
                        Rectangle2D.Double rSheet = new Rectangle2D.Double();
124
                        double x = FLayoutUtilities.toSheetDistance(
125 9392 caballero
                                        layout.getLayoutControl().getRect().getX(), layout.getLayoutControl().getAT());
126 7304 caballero
                        double y = FLayoutUtilities.toSheetDistance(
127 9392 caballero
                                        layout.getLayoutControl().getRect().getY(), layout.getLayoutControl().getAT());
128
                        double w = FLayoutUtilities.toSheetDistance(layout.getLayoutControl().getRect()
129
                                        .getWidth(), layout.getLayoutControl().getAT());
130
                        double h = FLayoutUtilities.toSheetDistance(layout.getLayoutControl().getRect()
131
                                        .getHeight(), layout.getLayoutControl().getAT());
132 7304 caballero
133
                        double wv = FLayoutUtilities.toSheetDistance(layout
134 9392 caballero
                                        .getVisibleRect().getWidth(), layout.getLayoutControl().getAT());
135 7304 caballero
                        double hv = FLayoutUtilities.toSheetDistance(layout
136 9392 caballero
                                        .getVisibleRect().getHeight(), layout.getLayoutControl().getAT());
137 7304 caballero
                        double mw = xmax - xmin;
138
                        double mh = ymax - ymin;
139
                        double difw = wv / mw;
140
                        double difh = hv / mh;
141
142
                        if (difw < difh) {
143
                                rSheet.x = (-xmin * difw)
144
                                                - x
145
                                                + ((wv - ((pSheet2.getX() - pSheet1.getX()) * difw)) / 2);
146
                                rSheet.y = (-ymin * difw)
147
                                                - y
148
                                                + ((hv - ((pSheet2.getY() - pSheet1.getY()) * difw)) / 2);
149
150
                                rSheet.width = w * difw;
151
                                rSheet.height = h * difw;
152
                        } else {
153
                                rSheet.x = (-xmin * difh)
154
                                                - x
155
                                                + ((wv - ((pSheet2.getX() - pSheet1.getX()) * difh)) / 2);
156
                                rSheet.y = (-ymin * difh)
157
                                                - y
158
                                                + ((hv - ((pSheet2.getY() - pSheet1.getY()) * difh)) / 2);
159
160
                                rSheet.width = w * difh;
161
                                rSheet.height = h * difh;
162
                        }
163
                        setPointsToZoom(p1, p2);
164 9392 caballero
                        rScreen.setRect(FLayoutUtilities.fromSheetRect(rSheet, layout.getLayoutControl()
165 7304 caballero
                                        .getAT()));
166
                        if (FLayoutUtilities.isPosible(rScreen)) {
167 9392 caballero
                                layout.getLayoutControl().getRect().setRect(rScreen);
168 7304 caballero
                        }
169
                }
170
    }
171
172
   /**
173
         * Realiza un zoom out sobre el Layout que se le pasa como par?metro.
174
         *
175
         * @param p2 punto central del rect?ngulo.
176
         */
177
    public void setZoomOut(Point p2) {
178
            double difw = 0.5;
179
            setZoom(difw,p2);
180
    }
181
182
    /**
183
         * Realiza un zoom out sobre el Layout que se le pasa como par?metro.
184
         *
185
         * @param dif factor.
186
         * @param p2 punto final del rec?ngulo.
187
         */
188
    public void setZoom(double dif, Point p2) {
189
        Point2D.Double pSheet2 = FLayoutUtilities.toSheetPoint(new Point2D.Double(
190 9392 caballero
                    p2.getX(), p2.getY()), layout.getLayoutControl().getAT());
191 7304 caballero
        Rectangle2D.Double rScreen = new Rectangle2D.Double();
192
        Rectangle2D.Double rSheet = new Rectangle2D.Double();
193
194
        double difw = dif;
195
196
        rSheet.x = (-pSheet2.getX() * difw) -
197 9392 caballero
            FLayoutUtilities.toSheetDistance(layout.getLayoutControl().getRect().getX(),
198
                            layout.getLayoutControl().getAT()) +
199 7304 caballero
            FLayoutUtilities.toSheetDistance(layout.getWidth() / 2,
200 9392 caballero
                            layout.getLayoutControl().getAT());
201 7304 caballero
        rSheet.y = (-pSheet2.getY() * difw) -
202 9392 caballero
            FLayoutUtilities.toSheetDistance(layout.getLayoutControl().getRect().getY(),
203
                            layout.getLayoutControl().getAT()) +
204 7304 caballero
            FLayoutUtilities.toSheetDistance(layout.getHeight() / 2,
205 9392 caballero
                            layout.getLayoutControl().getAT());
206 7304 caballero
207 9392 caballero
        rSheet.width = FLayoutUtilities.toSheetDistance(layout.getLayoutControl().getRect()
208 7304 caballero
                                                              .getWidth(),
209 9392 caballero
                                                              layout.getLayoutControl().getAT()) * difw;
210
        rSheet.height = FLayoutUtilities.toSheetDistance(layout.getLayoutControl().getRect()
211 7304 caballero
                                                               .getHeight(),
212 9392 caballero
                                                               layout.getLayoutControl().getAT()) * difw;
213 7304 caballero
214 9392 caballero
        rScreen.setRect(FLayoutUtilities.fromSheetRect(rSheet, layout.getLayoutControl().getAT()));
215 7304 caballero
216
        if (FLayoutUtilities.isPosible(rScreen)) {
217 9392 caballero
                layout.getLayoutControl().getRect().setRect(rScreen);
218 7304 caballero
        }
219
220
        //                Para realizar el zoom a partir de un punto.
221
        Point p1 = new Point((int) (p2.getX() -
222
                (layout.getWidth() / (difw * 2))),
223
                (int) (p2.getY() - (layout.getHeight() / (difw * 2))));
224
        p2 = new Point((int) (p2.getX() + (layout.getWidth() / (difw * 2))),
225
                (int) (p2.getY() + (layout.getHeight() / (difw * 2))));
226
        setPointsToZoom(p1, p2);
227
    }
228
    /**
229
     * Introduce los puntos de control para controlar el zoom del Layout.
230
     */
231
    private void setPointsToZoom(Point p1, Point p2) {
232 9392 caballero
        IFFrame[] fframes = layout.getLayoutContext().getFFrames();
233 7304 caballero
234
        for (int i = 0; i < fframes.length; i++) {
235
            if (fframes[i] instanceof IFFrameUseFMap) {
236
                IFFrameUseFMap fframe = (IFFrameUseFMap) fframes[i];
237
                if (fframe.getATMap()!=null) {
238
                        Point2D px1 = FLayoutFunctions.toMapPoint(p1, fframe.getATMap());
239
                        Point2D px2 = FLayoutFunctions.toMapPoint(p2, fframe.getATMap());
240
                        fframe.setPointsToZoom(px1, px2);
241
                }
242
            }
243
        }
244
    }
245
246
    /**
247
     * Aplica el zoom real teniendo en cuenta la resoluci?n de pantalla.
248
     */
249
    public void realZoom() {
250 7680 caballero
            Preferences prefsResolution = Preferences.userRoot().node( "gvsig.configuration.screen" );
251 9392 caballero
            double cm = layout.getLayoutContext().getAtributes().getPixXCm(layout.getLayoutControl().getRect());
252 7304 caballero
        Toolkit kit = Toolkit.getDefaultToolkit();
253 7680 caballero
        double dpi = prefsResolution.getInt("dpi",kit.getScreenResolution());
254 7676 caballero
        double dif = (cm * Attributes.PULGADA) / dpi;
255 7304 caballero
        setZoom(1 / dif,
256
            new Point(layout.getWidth() / 2, layout.getHeight() / 2));
257 9392 caballero
        layout.getLayoutControl().refresh();
258 7304 caballero
    }
259
260
    /**
261
     * Realiza un zoom in a partir del zoom actual de la vista.
262
     */
263
    public void zoomIn() {
264
        setZoom(2, new Point(layout.getWidth() / 2, layout.getHeight() / 2));
265 9392 caballero
        layout.getLayoutControl().refresh();
266 7304 caballero
    }
267
268
    /**
269
     * Realiza un zoom out a partir del zoom actual de la vista.
270
     */
271
    public void zoomOut() {
272
        setZoom(0.5, new Point(layout.getWidth() / 2, layout.getHeight() / 2));
273 9392 caballero
        layout.getLayoutControl().refresh();
274 7304 caballero
    }
275
276
    /**
277
         * Realiza un zoom a los elementos que esten seleccionados, si no hay
278
         * ning?n elemento seleccionado no realiza ning?n zoom
279
         */
280
    public void zoomSelect() {
281
        Rectangle2D.Double recaux = null;
282 9392 caballero
        IFFrame[] fframes=layout.getLayoutContext().getFFrames();
283 7304 caballero
        for (int i = 0; i < fframes.length; i++) {
284
            if (fframes[i].getSelected() != IFFrame.NOSELECT) {
285
                if (recaux == null) {
286 9392 caballero
                    recaux = fframes[i].getBoundingBox(layout.getLayoutControl().getAT());
287 7304 caballero
                } else {
288
                    recaux.add(fframes[i].getBoundingBox(
289 9392 caballero
                                    layout.getLayoutControl().getAT()));
290 7304 caballero
                }
291
            }
292
        }
293
294
        if (recaux != null) {
295
            Point p1 = new Point((int) recaux.x, (int) recaux.y);
296
            Point p2 = new Point((int) recaux.getMaxX(), (int) recaux.getMaxY());
297
            setZoomIn(p1, p2);
298 9392 caballero
            layout.getLayoutControl().refresh();
299 7304 caballero
        }
300
    }
301 8872 caballero
    /**
302
         * Realiza un zoom a todos los elementos del layout.
303
         */
304
    public void zoomAllFrames() {
305
        Rectangle2D.Double recaux = null;
306 9392 caballero
        IFFrame[] fframes=layout.getLayoutControl().getLayoutContext().getFFrames();
307 8872 caballero
        for (int i = 0; i < fframes.length; i++) {
308
            if (recaux == null) {
309 9392 caballero
                recaux = fframes[i].getBoundingBox(layout.getLayoutControl().getAT());
310 8872 caballero
            } else {
311
                recaux.add(fframes[i].getBoundingBox(
312 9392 caballero
                                layout.getLayoutControl().getAT()));
313 8872 caballero
            }
314
        }
315 7304 caballero
316 8872 caballero
        if (recaux != null) {
317
            Point p1 = new Point((int) recaux.x, (int) recaux.y);
318
            Point p2 = new Point((int) recaux.getMaxX(), (int) recaux.getMaxY());
319
            setZoomIn(p1, p2);
320 9392 caballero
            layout.getLayoutControl().refresh();
321 8872 caballero
        }
322
    }
323
324 7304 caballero
    /**
325
     * Realiza un zoom in a las vista a?adidas al Layout que esten seleccionadas
326
     *
327
     * @param p1 Punto inicial del rect?ngulo
328
     * @param p2 Punto final del rect?ngulo
329
     */
330
    public void setViewZoomIn(Point2D p1, Point2D p2) {
331 9392 caballero
            IFFrame[] fframes=layout.getLayoutContext().getFFrames();
332 7304 caballero
        for (int i = 0; i < fframes.length; i++) {
333
            if (fframes[i] instanceof IFFrameUseFMap) {
334
                IFFrameUseFMap fframe = (IFFrameUseFMap) fframes[i];
335
336
                if (((IFFrame) fframe).getSelected() != IFFrame.NOSELECT) {
337
                        //IFFrameUseFMap fframeAux=(IFFrameUseFMap)((IFFrame)fframe).cloneFFrame(layout);
338
                        p1 = FLayoutFunctions.toMapPoint(p1, fframe.getATMap());
339
                    p2 = FLayoutFunctions.toMapPoint(p2, fframe.getATMap());
340
341
342
                    // Borramos el anterior
343 9392 caballero
                    layout.getLayoutControl().setStatus(LayoutControl.DESACTUALIZADO);
344 7304 caballero
                    Rectangle2D.Double r = new Rectangle2D.Double();
345
346
                    if (java.lang.Math.abs(p1.getX() - p2.getX()) <= 3) {
347
                        double nuevoX;
348
                        double nuevoY;
349
                        double cX;
350
                        double cY;
351
352
                        cX = p2.getX();
353
                        cY = p2.getY();
354
355
                        double factor = 1/MapContext.ZOOMINFACTOR;
356
357 10503 caballero
                        Rectangle2D extent=fframe.getMapContext().getViewPort().getExtent();
358
                        if (extent!=null){
359
                                nuevoX = cX -
360
                                    ((extent.getWidth() * factor) / 2.0);
361
                                nuevoY = cY -
362
                                    ((extent.getHeight() * factor) / 2.0);
363
                                r.x = nuevoX;
364
                                r.y = nuevoY;
365
                                r.width = extent.getWidth() * factor;
366
                                r.height = extent.getHeight() * factor;
367
                        }
368 7304 caballero
369
                        //fframeAux.setNewExtent(r);
370
                    } else {
371
                        //        Fijamos el nuevo extent
372
373
                        r.setFrameFromDiagonal(p1, p2);
374
375
                        //fframeAux.setNewExtent(r);
376
                    }
377
378
                    /*if (fframe.getTypeScale()!=IFFrameUseFMap.AUTOMATICO) {
379
                            fframeAux.setNewExtent(r);
380
                            fframeAux.refresh();
381
                            layout.getEFS().modifyFFrame((IFFrame)fframe,(IFFrame)fframeAux);
382
                            ((IFFrame)fframeAux).getBoundingBox(layout.getAT());
383
                            layout.updateFFrames();
384
                            layout.setIsReSel(true);
385
                    }else {*/
386
                            fframe.setNewExtent(r);
387
                            fframe.refresh();
388
                    ///}
389
                                    // Fin del else
390
                    //layout.repaint();
391
                }
392
            }
393
        }
394
    }
395
396
    /**
397
     * Realiza un zoom out a las vistas a?adidas al Layout y que est?n seleccionadas
398
     *
399
     * @param p2 Punto central
400
     */
401
    public void setViewZoomOut(Point p2) {
402
        Point2D.Double pWorld;
403 9392 caballero
        IFFrame[] fframes=layout.getLayoutContext().getFFrames();
404 7304 caballero
        for (int i = 0; i < fframes.length; i++) {
405
            if (fframes[i] instanceof IFFrameUseFMap) {
406
                IFFrameUseFMap fframe = (IFFrameUseFMap) fframes[i];
407
408
                if (((IFFrame) fframe).getSelected() != IFFrame.NOSELECT) {
409
                        //IFFrameUseFMap fframeAux=(IFFrameUseFMap)((IFFrame)fframe).cloneFFrame(layout);
410
                        double nuevoX;
411
                    double nuevoY;
412
                    double cX;
413
                    double cY;
414
                    Point pScreen = new Point((int) p2.getX(), (int) p2.getY());
415
                    pWorld = FLayoutFunctions.toMapPoint(pScreen,
416
                            fframe.getATMap());
417
418
                    cX = pWorld.getX();
419
                    cY = pWorld.getY();
420
421
                    double factor = 1/MapContext.ZOOMOUTFACTOR;
422
                    Rectangle2D.Double r = new Rectangle2D.Double();
423 10503 caballero
                    Rectangle2D extent = fframe.getMapContext()
424
                                                .getViewPort().getExtent();
425
                    if (extent != null) {
426
                                                nuevoX = cX - ((extent.getWidth() * factor) / 2.0);
427
                                                nuevoY = cY - ((extent.getHeight() * factor) / 2.0);
428
                                                r.x = nuevoX;
429
                                                r.y = nuevoY;
430
                                                r.width = extent.getWidth() * factor;
431
                                                r.height = extent.getHeight() * factor;
432
                                                fframe.setNewExtent(r);
433
                                                fframe.refresh();
434
                                        }
435 7304 caballero
                }
436
            }
437
        }
438
    }
439
440
    /**
441
     * Modifica los puntos de control para generar el zoom del Layout
442
     *
443
     * @param p1 Punto inicial
444
     * @param p2 Punto final
445
     */
446
    public void setPan(Point p1, Point p2) {
447 9392 caballero
        IFFrame[] fframes = layout.getLayoutContext().getFFrames();
448 7304 caballero
449
        for (int i = 0; i < fframes.length; i++) {
450
            if (fframes[i] instanceof IFFrameUseFMap) {
451
                IFFrameUseFMap fframe = (IFFrameUseFMap) fframes[i];
452 25996 vcaballero
                AffineTransform at=fframe.getATMap();
453
                if (at!=null) {
454
                        Point2D px1 = FLayoutFunctions.toMapPoint(p1, at);
455
                        Point2D px2 = FLayoutFunctions.toMapPoint(p2, at);
456 9433 caballero
                        fframe.movePoints(px1, px2);
457
                }
458 7304 caballero
            }
459
        }
460
    }
461
}