Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / gui / layout / FLayoutZooms.java @ 6630

History | View | Annotate | Download (15 KB)

1
/*
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.gui.layout;
46

    
47
import com.iver.cit.gvsig.gui.layout.fframes.IFFrame;
48
import com.iver.cit.gvsig.gui.layout.fframes.IFFrameUseFMap;
49

    
50
import java.awt.Point;
51
import java.awt.Toolkit;
52
import java.awt.geom.Point2D;
53
import java.awt.geom.Rectangle2D;
54

    
55

    
56
/**
57
 * Clase encargada de realizar los zooms al Layout.
58
 *
59
 * @author Vicente Caballero Navarro
60
 */
61
public class FLayoutZooms {
62
    private Layout layout = null;
63

    
64
    public FLayoutZooms(Layout l) {
65
        layout = l;
66
    }
67

    
68
    /**
69
         * Realiza un zoom por rect?ngulo o por punto con un escalado por defecto
70
         * sobre el Layout que se le pasa como par?metro.
71
         *
72
         * @param p1 punto de inicio del rect?ngulo.
73
         * @param p2 punto final del rec?ngulo.
74
         */
75
    public void setZoomIn(Point p1, Point p2) {
76
            if (java.lang.Math.abs(layout.getFirstPoint().x - p2.x) < 4) {
77
                        double difw = 2;
78
                        setZoom(difw, p2);
79
                } else {
80
                        if (p1.getX() > p2.getX()) {
81
                                int aux = p2.x;
82
                                p2.x = p1.x;
83
                                p1.x = aux;
84
                        }
85

    
86
                        if (p1.getY() > p2.getY()) {
87
                                int aux = p2.y;
88
                                p2.y = p1.y;
89
                                p1.y = aux;
90
                        }
91

    
92
                        Point2D.Double pSheet1 = FLayoutUtilities.toSheetPoint(
93
                                        new Point2D.Double(p1.getX(), p1.getY()), layout.getAT());
94
                        Point2D.Double pSheet2 = FLayoutUtilities.toSheetPoint(
95
                                        new Point2D.Double(p2.getX(), p2.getY()), layout.getAT());
96

    
97
                        double xmin;
98
                        double xmax;
99
                        double ymin;
100
                        double ymax = 0;
101

    
102
                        if (pSheet1.x > pSheet2.x) {
103
                                xmin = pSheet2.x;
104
                                xmax = pSheet1.x;
105
                        } else {
106
                                xmin = pSheet1.x;
107
                                xmax = pSheet2.x;
108
                        }
109

    
110
                        if (pSheet1.y > pSheet2.y) {
111
                                ymin = pSheet2.y;
112
                                ymax = pSheet1.y;
113
                        } else {
114
                                ymin = pSheet1.y;
115
                                ymax = pSheet2.y;
116
                        }
117

    
118
                        Rectangle2D.Double rScreen = new Rectangle2D.Double();
119
                        Rectangle2D.Double rSheet = new Rectangle2D.Double();
120
                        double x = FLayoutUtilities.toSheetDistance(
121
                                        layout.getRect().getX(), layout.getAT());
122
                        double y = FLayoutUtilities.toSheetDistance(
123
                                        layout.getRect().getY(), layout.getAT());
124
                        double w = FLayoutUtilities.toSheetDistance(layout.getRect()
125
                                        .getWidth(), layout.getAT());
126
                        double h = FLayoutUtilities.toSheetDistance(layout.getRect()
127
                                        .getHeight(), layout.getAT());
128

    
129
                        double wv = FLayoutUtilities.toSheetDistance(layout
130
                                        .getVisibleRect().getWidth(), layout.getAT());
131
                        double hv = FLayoutUtilities.toSheetDistance(layout
132
                                        .getVisibleRect().getHeight(), layout.getAT());
133
                        double mw = xmax - xmin;
134
                        double mh = ymax - ymin;
135
                        double difw = wv / mw;
136
                        double difh = hv / mh;
137

    
138
                        if (difw < difh) {
139
                                rSheet.x = (-xmin * difw)
140
                                                - x
141
                                                + ((wv - ((pSheet2.getX() - pSheet1.getX()) * difw)) / 2);
142
                                rSheet.y = (-ymin * difw)
143
                                                - y
144
                                                + ((hv - ((pSheet2.getY() - pSheet1.getY()) * difw)) / 2);
145

    
146
                                rSheet.width = w * difw;
147
                                rSheet.height = h * difw;
148
                        } else {
149
                                rSheet.x = (-xmin * difh)
150
                                                - x
151
                                                + ((wv - ((pSheet2.getX() - pSheet1.getX()) * difh)) / 2);
152
                                rSheet.y = (-ymin * difh)
153
                                                - y
154
                                                + ((hv - ((pSheet2.getY() - pSheet1.getY()) * difh)) / 2);
155

    
156
                                rSheet.width = w * difh;
157
                                rSheet.height = h * difh;
158
                        }
159
                        setPointsToZoom(p1, p2);
160
                        rScreen.setRect(FLayoutUtilities.fromSheetRect(rSheet, layout
161
                                        .getAT()));
162
                        if (FLayoutUtilities.isPosible(rScreen)) {
163
                                layout.getRect().setRect(rScreen);
164
                        }
165
                }
166
    }
167

    
168
   /**
169
         * Realiza un zoom out sobre el Layout que se le pasa como par?metro.
170
         *
171
         * @param p2 punto central del rect?ngulo.
172
         */
173
    public void setZoomOut(Point p2) {
174
            double difw = 0.5;
175
            setZoom(difw,p2);
176
    }
177

    
178
    /**
179
         * Realiza un zoom out sobre el Layout que se le pasa como par?metro.
180
         *
181
         * @param dif factor.
182
         * @param p2 punto final del rec?ngulo.
183
         */
184
    public void setZoom(double dif, Point p2) {
185
        Point2D.Double pSheet2 = FLayoutUtilities.toSheetPoint(new Point2D.Double(
186
                    p2.getX(), p2.getY()), layout.getAT());
187
        Rectangle2D.Double rScreen = new Rectangle2D.Double();
188
        Rectangle2D.Double rSheet = new Rectangle2D.Double();
189

    
190
        double difw = dif;
191

    
192
        rSheet.x = (-pSheet2.getX() * difw) -
193
            FLayoutUtilities.toSheetDistance(layout.getRect().getX(),
194
                layout.getAT()) +
195
            FLayoutUtilities.toSheetDistance(layout.getWidth() / 2,
196
                layout.getAT());
197
        rSheet.y = (-pSheet2.getY() * difw) -
198
            FLayoutUtilities.toSheetDistance(layout.getRect().getY(),
199
                layout.getAT()) +
200
            FLayoutUtilities.toSheetDistance(layout.getHeight() / 2,
201
                layout.getAT());
202

    
203
        rSheet.width = FLayoutUtilities.toSheetDistance(layout.getRect()
204
                                                              .getWidth(),
205
                layout.getAT()) * difw;
206
        rSheet.height = FLayoutUtilities.toSheetDistance(layout.getRect()
207
                                                               .getHeight(),
208
                layout.getAT()) * difw;
209

    
210
        rScreen.setRect(FLayoutUtilities.fromSheetRect(rSheet, layout.getAT()));
211

    
212
        if (FLayoutUtilities.isPosible(rScreen)) {
213
            layout.getRect().setRect(rScreen);
214
        }
215

    
216
        //                Para realizar el zoom a partir de un punto.
217
        Point p1 = new Point((int) (p2.getX() -
218
                (layout.getWidth() / (difw * 2))),
219
                (int) (p2.getY() - (layout.getHeight() / (difw * 2))));
220
        p2 = new Point((int) (p2.getX() + (layout.getWidth() / (difw * 2))),
221
                (int) (p2.getY() + (layout.getHeight() / (difw * 2))));
222
        setPointsToZoom(p1, p2);
223
    }
224
    /**
225
     * Introduce los puntos de control para controlar el zoom del Layout.
226
     */
227
    private void setPointsToZoom(Point p1, Point p2) {
228
        IFFrame[] fframes = layout.getFFrames();
229

    
230
        for (int i = 0; i < fframes.length; i++) {
231
            if (fframes[i] instanceof IFFrameUseFMap) {
232
                IFFrameUseFMap fframe = (IFFrameUseFMap) fframes[i];
233
                if (fframe.getATMap()!=null) {
234
                        Point2D px1 = FLayoutFunctions.toMapPoint(p1, fframe.getATMap());
235
                        Point2D px2 = FLayoutFunctions.toMapPoint(p2, fframe.getATMap());
236
                        fframe.setPointsToZoom(px1, px2);
237
                }
238
            }
239
        }
240
    }
241

    
242
    /**
243
     * Aplica el zoom real teniendo en cuenta la resoluci?n de pantalla.
244
     */
245
    public void realZoom() {
246
        double cm = layout.getAtributes().getPixXCm(layout.getRect());
247
        Toolkit kit = Toolkit.getDefaultToolkit();
248
        int resolution = kit.getScreenResolution();
249
        double dif = (cm * Attributes.PULGADA) / resolution;
250
        setZoom(1 / dif,
251
            new Point(layout.getWidth() / 2, layout.getHeight() / 2));
252
        layout.refresh();
253
    }
254

    
255
    /**
256
     * Realiza un zoom in a partir del zoom actual de la vista.
257
     */
258
    public void zoomIn() {
259
        setZoom(2, new Point(layout.getWidth() / 2, layout.getHeight() / 2));
260
       layout.refresh();
261
    }
262

    
263
    /**
264
     * Realiza un zoom out a partir del zoom actual de la vista.
265
     */
266
    public void zoomOut() {
267
        setZoom(0.5, new Point(layout.getWidth() / 2, layout.getHeight() / 2));
268
       layout.refresh();
269
    }
270

    
271
    /**
272
         * Realiza un zoom a los elementos que esten seleccionados, si no hay
273
         * ning?n elemento seleccionado no realiza ning?n zoom
274
         */
275
    public void zoomSelect() {
276
        Rectangle2D.Double recaux = null;
277
        IFFrame[] fframes=layout.getFFrames();
278
        for (int i = 0; i < fframes.length; i++) {
279
            if (fframes[i].getSelected() != IFFrame.NOSELECT) {
280
                if (recaux == null) {
281
                    recaux = fframes[i].getBoundingBox(layout.getAT());
282
                } else {
283
                    recaux.add(fframes[i].getBoundingBox(
284
                            layout.getAT()));
285
                }
286
            }
287
        }
288

    
289
        if (recaux != null) {
290
            Point p1 = new Point((int) recaux.x, (int) recaux.y);
291
            Point p2 = new Point((int) recaux.getMaxX(), (int) recaux.getMaxY());
292
            setZoomIn(p1, p2);
293
            layout.refresh();
294
        }
295
    }
296

    
297
    /**
298
     * Realiza un zoom in a las vista a?adidas al Layout que esten seleccionadas
299
     *
300
     * @param p1 Punto inicial del rect?ngulo
301
     * @param p2 Punto final del rect?ngulo
302
     */
303
    public void setViewZoomIn(Point2D p1, Point2D p2) {
304
            IFFrame[] fframes=layout.getFFrames();
305
        for (int i = 0; i < fframes.length; i++) {
306
            if (fframes[i] instanceof IFFrameUseFMap) {
307
                IFFrameUseFMap fframe = (IFFrameUseFMap) fframes[i];
308

    
309
                if (((IFFrame) fframe).getSelected() != IFFrame.NOSELECT) {
310
                        //IFFrameUseFMap fframeAux=(IFFrameUseFMap)((IFFrame)fframe).cloneFFrame(layout);
311
                        p1 = FLayoutFunctions.toMapPoint(p1, fframe.getATMap());
312
                    p2 = FLayoutFunctions.toMapPoint(p2, fframe.getATMap());
313

    
314

    
315
                    // Borramos el anterior
316
                    layout.setStatus(Layout.DESACTUALIZADO);
317
                    Rectangle2D.Double r = new Rectangle2D.Double();
318

    
319
                    if (java.lang.Math.abs(p1.getX() - p2.getX()) <= 3) {
320
                        double nuevoX;
321
                        double nuevoY;
322
                        double cX;
323
                        double cY;
324

    
325
                        cX = p2.getX();
326
                        cY = p2.getY();
327

    
328
                        double factor = 0.6;
329

    
330
                        nuevoX = cX -
331
                            ((fframe.getFMap().getViewPort().getExtent()
332
                                    .getWidth() * factor) / 2.0);
333
                        nuevoY = cY -
334
                            ((fframe.getFMap().getViewPort().getExtent()
335
                                    .getHeight() * factor) / 2.0);
336
                        r.x = nuevoX;
337
                        r.y = nuevoY;
338
                        r.width = fframe.getFMap().getViewPort().getExtent()
339
                                        .getWidth() * factor;
340
                        r.height = fframe.getFMap().getViewPort().getExtent()
341
                                         .getHeight() * factor;
342

    
343
                        //fframeAux.setNewExtent(r);
344
                    } else {
345
                        //        Fijamos el nuevo extent
346

    
347
                        r.setFrameFromDiagonal(p1, p2);
348

    
349
                        //fframeAux.setNewExtent(r);
350
                    }
351

    
352
                    /*if (fframe.getTypeScale()!=IFFrameUseFMap.AUTOMATICO) {
353
                            fframeAux.setNewExtent(r);
354
                            fframeAux.refresh();
355
                            layout.getEFS().modifyFFrame((IFFrame)fframe,(IFFrame)fframeAux);
356
                            ((IFFrame)fframeAux).getBoundingBox(layout.getAT());
357
                            layout.updateFFrames();
358
                            layout.setIsReSel(true);
359
                    }else {*/
360
                            fframe.setNewExtent(r);
361
                            fframe.refresh();
362
                    ///}
363
                                    // Fin del else
364
                    //layout.repaint();
365
                }
366
            }
367
        }
368
    }
369

    
370
    /**
371
     * Realiza un zoom out a las vistas a?adidas al Layout y que est?n seleccionadas
372
     *
373
     * @param p2 Punto central
374
     */
375
    public void setViewZoomOut(Point p2) {
376
        Point2D.Double pWorld;
377
        IFFrame[] fframes=layout.getFFrames();
378
        for (int i = 0; i < fframes.length; i++) {
379
            if (fframes[i] instanceof IFFrameUseFMap) {
380
                IFFrameUseFMap fframe = (IFFrameUseFMap) fframes[i];
381

    
382
                if (((IFFrame) fframe).getSelected() != IFFrame.NOSELECT) {
383
                        //IFFrameUseFMap fframeAux=(IFFrameUseFMap)((IFFrame)fframe).cloneFFrame(layout);
384
                        double nuevoX;
385
                    double nuevoY;
386
                    double cX;
387
                    double cY;
388
                    Point pScreen = new Point((int) p2.getX(), (int) p2.getY());
389
                    pWorld = FLayoutFunctions.toMapPoint(pScreen,
390
                            fframe.getATMap());
391

    
392
                    cX = pWorld.getX();
393
                    cY = pWorld.getY();
394

    
395
                    double factor = 1.8;
396
                    Rectangle2D.Double r = new Rectangle2D.Double();
397

    
398
                    nuevoX = cX -
399
                        ((fframe.getFMap().getViewPort().getExtent().getWidth() * factor) / 2.0);
400
                    nuevoY = cY -
401
                        ((fframe.getFMap().getViewPort().getExtent().getHeight() * factor) / 2.0);
402
                    r.x = nuevoX;
403
                    r.y = nuevoY;
404
                    r.width = fframe.getFMap().getViewPort().getExtent()
405
                                    .getWidth() * factor;
406
                    r.height = fframe.getFMap().getViewPort().getExtent()
407
                                     .getHeight() * factor;
408

    
409
                    /*if (fframe.getTypeScale()!=IFFrameUseFMap.AUTOMATICO) {
410
                            fframeAux.setNewExtent(r);
411
                            fframeAux.refresh();
412
                            layout.getEFS().modifyFFrame((IFFrame)fframe,(IFFrame)fframeAux);
413
                            ((IFFrame)fframeAux).getBoundingBox(layout.getAT());
414
                            layout.updateFFrames();
415
                            layout.setIsReSel(true);
416
                    }else {*/
417
                            fframe.setNewExtent(r);
418
                            fframe.refresh();
419
                    ///}
420
                    ///fframe.getFMap().setCancelDrawing(false);
421
                }
422
            }
423
        }
424
    }
425

    
426
    /**
427
     * Modifica los puntos de control para generar el zoom del Layout
428
     *
429
     * @param p1 Punto inicial
430
     * @param p2 Punto final
431
     */
432
    public void setPan(Point p1, Point p2) {
433
        IFFrame[] fframes = layout.getFFrames();
434

    
435
        for (int i = 0; i < fframes.length; i++) {
436
            if (fframes[i] instanceof IFFrameUseFMap) {
437
                IFFrameUseFMap fframe = (IFFrameUseFMap) fframes[i];
438
                Point2D px1 = FLayoutFunctions.toMapPoint(p1, fframe.getATMap());
439
                Point2D px2 = FLayoutFunctions.toMapPoint(p2, fframe.getATMap());
440
                fframe.movePoints(px1, px2);
441
            }
442
        }
443
    }
444
}