Statistics
| Revision:

root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / layout / FLayoutZooms.java @ 7304

History | View | Annotate | Download (15.2 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.project.documents.layout;
46

    
47
import com.iver.cit.gvsig.fmap.MapContext;
48
import com.iver.cit.gvsig.gui.layout.Layout;
49
import com.iver.cit.gvsig.project.documents.layout.fframes.IFFrame;
50
import com.iver.cit.gvsig.project.documents.layout.fframes.IFFrameUseFMap;
51

    
52
import java.awt.Point;
53
import java.awt.Toolkit;
54
import java.awt.geom.Point2D;
55
import java.awt.geom.Rectangle2D;
56

    
57

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

    
66
    public FLayoutZooms(Layout l) {
67
        layout = l;
68
    }
69

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

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

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

    
99
                        double xmin;
100
                        double xmax;
101
                        double ymin;
102
                        double ymax = 0;
103

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

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

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

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

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

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

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

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

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

    
192
        double difw = dif;
193

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

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

    
212
        rScreen.setRect(FLayoutUtilities.fromSheetRect(rSheet, layout.getAT()));
213

    
214
        if (FLayoutUtilities.isPosible(rScreen)) {
215
            layout.getRect().setRect(rScreen);
216
        }
217

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

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

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

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

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

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

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

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

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

    
316

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

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

    
327
                        cX = p2.getX();
328
                        cY = p2.getY();
329

    
330
                        double factor = 1/MapContext.ZOOMINFACTOR;
331

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

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

    
349
                        r.setFrameFromDiagonal(p1, p2);
350

    
351
                        //fframeAux.setNewExtent(r);
352
                    }
353

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

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

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

    
394
                    cX = pWorld.getX();
395
                    cY = pWorld.getY();
396

    
397
                    double factor = 1/MapContext.ZOOMOUTFACTOR;
398
                    Rectangle2D.Double r = new Rectangle2D.Double();
399

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

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

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

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