Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / layout / FLayoutZooms.java @ 21530

History | View | Annotate | Download (16.3 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 java.awt.Point;
48
import java.awt.Toolkit;
49
import java.awt.geom.Point2D;
50
import java.awt.geom.Rectangle2D;
51
import java.util.prefs.Preferences;
52

    
53
import org.gvsig.fmap.geom.primitive.DefaultEnvelope;
54
import org.gvsig.fmap.mapcontext.MapContext;
55

    
56
import com.iver.cit.gvsig.project.documents.layout.fframes.IFFrame;
57
import com.iver.cit.gvsig.project.documents.layout.fframes.IFFrameUseFMap;
58
import com.iver.cit.gvsig.project.documents.layout.gui.Layout;
59

    
60

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

    
69
    public FLayoutZooms(Layout l) {
70
        layout = l;
71
    }
72

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

    
91
                        if (p1.getY() > p2.getY()) {
92
                                int aux = p2.y;
93
                                p2.y = p1.y;
94
                                p1.y = aux;
95
                        }
96

    
97
                        Point2D.Double pSheet1 = FLayoutUtilities.toSheetPoint(
98
                                        new Point2D.Double(p1.getX(), p1.getY()), layout.getLayoutControl().getAT());
99
                        Point2D.Double pSheet2 = FLayoutUtilities.toSheetPoint(
100
                                        new Point2D.Double(p2.getX(), p2.getY()), layout.getLayoutControl().getAT());
101

    
102
                        double xmin;
103
                        double xmax;
104
                        double ymin;
105
                        double ymax = 0;
106

    
107
                        if (pSheet1.x > pSheet2.x) {
108
                                xmin = pSheet2.x;
109
                                xmax = pSheet1.x;
110
                        } else {
111
                                xmin = pSheet1.x;
112
                                xmax = pSheet2.x;
113
                        }
114

    
115
                        if (pSheet1.y > pSheet2.y) {
116
                                ymin = pSheet2.y;
117
                                ymax = pSheet1.y;
118
                        } else {
119
                                ymin = pSheet1.y;
120
                                ymax = pSheet2.y;
121
                        }
122

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

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

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

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

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

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

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

    
195
        double difw = dif;
196

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

    
208
        rSheet.width = FLayoutUtilities.toSheetDistance(layout.getLayoutControl().getRect()
209
                                                              .getWidth(),
210
                                                              layout.getLayoutControl().getAT()) * difw;
211
        rSheet.height = FLayoutUtilities.toSheetDistance(layout.getLayoutControl().getRect()
212
                                                               .getHeight(),
213
                                                               layout.getLayoutControl().getAT()) * difw;
214

    
215
        rScreen.setRect(FLayoutUtilities.fromSheetRect(rSheet, layout.getLayoutControl().getAT()));
216

    
217
        if (FLayoutUtilities.isPosible(rScreen)) {
218
                layout.getLayoutControl().getRect().setRect(rScreen);
219
        }
220

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

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

    
247
    /**
248
     * Aplica el zoom real teniendo en cuenta la resoluci?n de pantalla.
249
     */
250
    public void realZoom() {
251
            Preferences prefsResolution = Preferences.userRoot().node( "gvsig.configuration.screen" );
252
            double cm = layout.getLayoutContext().getAtributes().getPixXCm(layout.getLayoutControl().getRect());
253
        Toolkit kit = Toolkit.getDefaultToolkit();
254
        double dpi = prefsResolution.getInt("dpi",kit.getScreenResolution());
255
        double dif = (cm * Attributes.PULGADA) / dpi;
256
        setZoom(1 / dif,
257
            new Point(layout.getWidth() / 2, layout.getHeight() / 2));
258
        layout.getLayoutControl().refresh();
259
    }
260

    
261
    /**
262
     * Realiza un zoom in a partir del zoom actual de la vista.
263
     */
264
    public void zoomIn() {
265
        setZoom(2, new Point(layout.getWidth() / 2, layout.getHeight() / 2));
266
        layout.getLayoutControl().refresh();
267
    }
268

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

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

    
295
        if (recaux != null) {
296
            Point p1 = new Point((int) recaux.x, (int) recaux.y);
297
            Point p2 = new Point((int) recaux.getMaxX(), (int) recaux.getMaxY());
298
            setZoomIn(p1, p2);
299
            layout.getLayoutControl().refresh();
300
        }
301
    }
302
    /**
303
         * Realiza un zoom a todos los elementos del layout.
304
         */
305
    public void zoomAllFrames() {
306
        Rectangle2D.Double recaux = null;
307
        IFFrame[] fframes=layout.getLayoutControl().getLayoutContext().getFFrames();
308
        for (int i = 0; i < fframes.length; i++) {
309
            if (recaux == null) {
310
                recaux = fframes[i].getBoundingBox(layout.getLayoutControl().getAT());
311
            } else {
312
                recaux.add(fframes[i].getBoundingBox(
313
                                layout.getLayoutControl().getAT()));
314
            }
315
        }
316

    
317
        if (recaux != null) {
318
            Point p1 = new Point((int) recaux.x, (int) recaux.y);
319
            Point p2 = new Point((int) recaux.getMaxX(), (int) recaux.getMaxY());
320
            setZoomIn(p1, p2);
321
            layout.getLayoutControl().refresh();
322
        }
323
    }
324

    
325
    /**
326
     * Realiza un zoom in a las vista a?adidas al Layout que esten seleccionadas
327
     *
328
     * @param p1 Punto inicial del rect?ngulo
329
     * @param p2 Punto final del rect?ngulo
330
     */
331
    public void setViewZoomIn(Point2D p1, Point2D p2) {
332
            IFFrame[] fframes=layout.getLayoutContext().getFFrames();
333
        for (int i = 0; i < fframes.length; i++) {
334
            if (fframes[i] instanceof IFFrameUseFMap) {
335
                IFFrameUseFMap fframe = (IFFrameUseFMap) fframes[i];
336

    
337
                if (((IFFrame) fframe).getSelected() != IFFrame.NOSELECT) {
338
                        //IFFrameUseFMap fframeAux=(IFFrameUseFMap)((IFFrame)fframe).cloneFFrame(layout);
339
                        p1 = FLayoutFunctions.toMapPoint(p1, fframe.getATMap());
340
                    p2 = FLayoutFunctions.toMapPoint(p2, fframe.getATMap());
341

    
342

    
343
                    // Borramos el anterior
344
                    layout.getLayoutControl().setStatus(LayoutControl.DESACTUALIZADO);
345
                    Rectangle2D.Double r = new Rectangle2D.Double();
346

    
347
                    if (java.lang.Math.abs(p1.getX() - p2.getX()) <= 3) {
348
                        double nuevoX;
349
                        double nuevoY;
350
                        double cX;
351
                        double cY;
352

    
353
                        cX = p2.getX();
354
                        cY = p2.getY();
355

    
356
                        double factor = 1/MapContext.ZOOMINFACTOR;
357

    
358
                        Rectangle2D extent=fframe.getMapContext().getViewPort().getExtent();
359
                        if (extent!=null){
360
                                nuevoX = cX -
361
                                    ((extent.getWidth() * factor) / 2.0);
362
                                nuevoY = cY -
363
                                    ((extent.getHeight() * factor) / 2.0);
364
                                r.x = nuevoX;
365
                                r.y = nuevoY;
366
                                r.width = extent.getWidth() * factor;
367
                                r.height = extent.getHeight() * factor;
368
                        }
369

    
370
                        //fframeAux.setNewExtent(r);
371
                    } else {
372
                        //        Fijamos el nuevo extent
373

    
374
                        r.setFrameFromDiagonal(p1, p2);
375

    
376
                        //fframeAux.setNewExtent(r);
377
                    }
378

    
379
                    /*if (fframe.getTypeScale()!=IFFrameUseFMap.AUTOMATICO) {
380
                            fframeAux.setNewExtent(r);
381
                            fframeAux.refresh();
382
                            layout.getEFS().modifyFFrame((IFFrame)fframe,(IFFrame)fframeAux);
383
                            ((IFFrame)fframeAux).getBoundingBox(layout.getAT());
384
                            layout.updateFFrames();
385
                            layout.setIsReSel(true);
386
                    }else {*/
387
                            fframe.setNewEnvelope(new DefaultEnvelope(2,new double[]{r.getX(),r.getY()},new double[]{r.getMaxX(),r.getMaxY()}));
388
                            fframe.refresh();
389
                    ///}
390
                                    // Fin del else
391
                    //layout.repaint();
392
                }
393
            }
394
        }
395
    }
396

    
397
    /**
398
     * Realiza un zoom out a las vistas a?adidas al Layout y que est?n seleccionadas
399
     *
400
     * @param p2 Punto central
401
     */
402
    public void setViewZoomOut(Point p2) {
403
        Point2D.Double pWorld;
404
        IFFrame[] fframes=layout.getLayoutContext().getFFrames();
405
        for (int i = 0; i < fframes.length; i++) {
406
            if (fframes[i] instanceof IFFrameUseFMap) {
407
                IFFrameUseFMap fframe = (IFFrameUseFMap) fframes[i];
408

    
409
                if (((IFFrame) fframe).getSelected() != IFFrame.NOSELECT) {
410
                        //IFFrameUseFMap fframeAux=(IFFrameUseFMap)((IFFrame)fframe).cloneFFrame(layout);
411
                        double nuevoX;
412
                    double nuevoY;
413
                    double cX;
414
                    double cY;
415
                    Point pScreen = new Point((int) p2.getX(), (int) p2.getY());
416
                    pWorld = FLayoutFunctions.toMapPoint(pScreen,
417
                            fframe.getATMap());
418

    
419
                    cX = pWorld.getX();
420
                    cY = pWorld.getY();
421

    
422
                    double factor = 1/MapContext.ZOOMOUTFACTOR;
423
                    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
                                                double x = nuevoX;
429
                                                double y = nuevoY;
430
                                                double width = extent.getWidth() * factor;
431
                                                double height = extent.getHeight() * factor;
432
                                                fframe.setNewEnvelope(new DefaultEnvelope(2,new double[]{x,y},new double[]{x+width,y+height}));
433
                                                fframe.refresh();
434
                                        }
435
                }
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
        IFFrame[] fframes = layout.getLayoutContext().getFFrames();
448

    
449
        for (int i = 0; i < fframes.length; i++) {
450
            if (fframes[i] instanceof IFFrameUseFMap) {
451
                IFFrameUseFMap fframe = (IFFrameUseFMap) fframes[i];
452
                if (fframe.getATMap()!=null) {
453
                        Point2D px1 = FLayoutFunctions.toMapPoint(p1, fframe.getATMap());
454
                        Point2D px2 = FLayoutFunctions.toMapPoint(p2, fframe.getATMap());
455
                        fframe.movePoints(px1, px2);
456
                }
457
            }
458
        }
459
    }
460
}