Statistics
| Revision:

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

History | View | Annotate | Download (13.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.gui.layout;
46

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

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

    
56

    
57
/**
58
 * Clase contenedora de los m?todos para cambiar los zooms.
59
 *
60
 * @author Vicente Caballero Navarro
61
 */
62
public class FLayoutZooms {
63
        private Layout layout = null;
64
        private FLayoutFunctions functions = null;
65

    
66
        /**
67
         * Crea un nuevo Zooms.
68
         *
69
         * @param l Mapa sobre el que se actua.
70
         */
71
        public FLayoutZooms(Layout l) {
72
                layout = l;
73
                functions = new FLayoutFunctions(layout);
74
        }
75

    
76
        /**
77
         * Realiza un zoom por rect?ngulo o por punto con un escalado por defecto
78
         * sobre el Layout que se le pasa como par?metro.
79
         *
80
         * @param p1 punto de inicio del rect?ngulo.
81
         * @param p2 punto final del rec?ngulo.
82
         */
83
        public void setZoomIn(Point p1, Point p2) {
84
                /*if (p1.y != p2.y) {
85
                        double points = (p2.getX() - p1.getX()) / (p2.getY() - p1.getY());
86
                        double window = (double) layout.getWidth() / (double)layout.getHeight();
87

88
                        if (points < window) {
89
                                p2.x = (int) (p1.x + ((p2.x - p1.x) * window));
90
                        }
91
                }
92
                */
93
                
94
                if (p1.getX()>p2.getX()){
95
                        int aux=p2.x;
96
                        p2.x=p1.x;
97
                        p1.x=aux;
98
                }
99
                if (p1.getY()>p2.getY()){
100
                        int aux=p2.y;
101
                        p2.y=p1.y;
102
                        p1.y=aux;
103
                }
104
                Point2D.Double pSheet1 = FLayoutUtilities.toSheetPoint(new Point2D.Double(
105
                                        p1.getX(), p1.getY()), layout.getAT());
106
                Point2D.Double pSheet2 = FLayoutUtilities.toSheetPoint(new Point2D.Double(
107
                                        p2.getX(), p2.getY()), layout.getAT());
108

    
109
                double xmin;
110
                double xmax;
111
                double ymin;
112
                double ymax = 0;
113

    
114
                if (pSheet1.x > pSheet2.x) {
115
                        xmin = pSheet2.x;
116
                        xmax = pSheet1.x;
117
                } else {
118
                        xmin = pSheet1.x;
119
                        xmax = pSheet2.x;
120
                }
121

    
122
                if (pSheet1.y > pSheet2.y) {
123
                        ymin = pSheet2.y;
124
                        ymax = pSheet1.y;
125
                } else {
126
                        ymin = pSheet1.y;
127
                        ymax = pSheet2.y;
128
                }
129

    
130
                Rectangle2D.Double rScreen = new Rectangle2D.Double();
131
                Rectangle2D.Double rSheet = new Rectangle2D.Double();
132
                double x = FLayoutUtilities.toSheetDistance(layout.getRect().getX(),
133
                                layout.getAT());
134
                double y = FLayoutUtilities.toSheetDistance(layout.getRect().getY(),
135
                                layout.getAT());
136
                double w = FLayoutUtilities.toSheetDistance(layout.getRect().getWidth(),
137
                                layout.getAT());
138
                double h = FLayoutUtilities.toSheetDistance(layout.getRect().getHeight(),
139
                                layout.getAT());
140

    
141
                if (java.lang.Math.abs(layout.getFirstPoint().x - p2.x) < 4) {
142
                        double difw = 2;
143

    
144
                        rSheet.x = (-pSheet2.getX() * difw) - x +
145
                                FLayoutUtilities.toSheetDistance(layout.getWidth() / 2,
146
                                        layout.getAT());
147
                        rSheet.y = (-pSheet2.getY() * difw) - y +
148
                                FLayoutUtilities.toSheetDistance(layout.getHeight() / 2,
149
                                        layout.getAT());
150

    
151
                        rSheet.width = w * difw;
152
                        rSheet.height = h * difw;
153

    
154
                        rScreen.setRect(FLayoutUtilities.fromSheetRect(rSheet,
155
                                        layout.getAT()));
156
                        IFFrame[] fframes=layout.getFFrames();
157
                        for (int i=0;i<fframes.length;i++){
158
                                if (fframes[i] instanceof FFrameView){
159
                                        Point2D point1=new Point2D.Double(p2.getX()-(layout.getWidth()/2)*difw,p2.getY()-(layout.getHeight()/2)*difw);
160
                                        Point2D point2=new Point2D.Double(p2.getX()+(layout.getWidth()/2)*difw,p2.getY()+(layout.getHeight()/2)*difw);
161
                                        ((FFrameView)fframes[i]).getRectView(point1,point2);
162
                                }
163
                        }
164
                        
165
                        
166
                } else {
167
                        double wv = FLayoutUtilities.toSheetDistance(layout.getVisibleRect()
168
                                                                                                                           .getWidth(),
169
                                        layout.getAT());
170
                        double hv = FLayoutUtilities.toSheetDistance(layout.getVisibleRect()
171
                                                                                                                           .getHeight(),
172
                                        layout.getAT());
173
                        double mw = xmax - xmin;
174
                        double mh = ymax - ymin;
175
                        double difw = wv / mw;
176
                        double difh = hv / mh;
177

    
178
                        if (difw < difh) {
179
                                rSheet.x = (-xmin * difw) - x +
180
                                        ((wv - ((pSheet2.getX() - pSheet1.getX()) * difw)) / 2);
181
                                rSheet.y = (-ymin * difw) - y +
182
                                        ((hv - ((pSheet2.getY() - pSheet1.getY()) * difw)) / 2);
183

    
184
                                rSheet.width = w * difw;
185
                                rSheet.height = h * difw;
186
                        } else {
187
                                rSheet.x = (-xmin * difh) - x +
188
                                        ((wv - ((pSheet2.getX() - pSheet1.getX()) * difh)) / 2);
189
                                rSheet.y = (-ymin * difh) - y +
190
                                        ((hv - ((pSheet2.getY() - pSheet1.getY()) * difh)) / 2);
191

    
192
                                rSheet.width = w * difh;
193
                                rSheet.height = h * difh;
194
                        }
195

    
196
                        rScreen.setRect(FLayoutUtilities.fromSheetRect(rSheet,
197
                                        layout.getAT()));
198
                        
199
                        IFFrame[] fframes=layout.getFFrames();
200
                        for (int i=0;i<fframes.length;i++){
201
                                if (fframes[i] instanceof FFrameView){
202
                                        ((FFrameView)fframes[i]).getRectView(p1,p2);
203
                                }
204
                        }
205
                }
206

    
207
                if (FLayoutUtilities.isPosible(rScreen)) {
208
                        layout.getRect().setRect(rScreen);
209
                }
210
                
211
        }
212

    
213
        /**
214
         * Realiza un zoom out sobre el Layout que se le pasa como par?metro.
215
         *
216
         * @param p2 punto final del rec?ngulo.
217
         */
218
        public void setZoomOut(Point p2) {
219
                Point2D.Double pSheet2 = FLayoutUtilities.toSheetPoint(new Point2D.Double(
220
                                        p2.getX(), p2.getY()), layout.getAT());
221

    
222
                Rectangle2D.Double rScreen = new Rectangle2D.Double();
223
                Rectangle2D.Double rSheet = new Rectangle2D.Double();
224

    
225
                double difw = 0.5;
226

    
227
                rSheet.x = (-pSheet2.getX() * difw) -
228
                        FLayoutUtilities.toSheetDistance(layout.getRect().getX(),
229
                                layout.getAT()) +
230
                        FLayoutUtilities.toSheetDistance(layout.getWidth() / 2,
231
                                layout.getAT());
232
                rSheet.y = (-pSheet2.getY() * difw) -
233
                        FLayoutUtilities.toSheetDistance(layout.getRect().getY(),
234
                                layout.getAT()) +
235
                        FLayoutUtilities.toSheetDistance(layout.getHeight() / 2,
236
                                layout.getAT());
237

    
238
                rSheet.width = FLayoutUtilities.toSheetDistance(layout.getRect()
239
                                                                                                                          .getWidth(),
240
                                layout.getAT()) * difw;
241
                rSheet.height = FLayoutUtilities.toSheetDistance(layout.getRect()
242
                                                                                                                           .getHeight(),
243
                                layout.getAT()) * difw;
244

    
245
                rScreen.setRect(FLayoutUtilities.fromSheetRect(rSheet, layout.getAT()));
246

    
247
                if (FLayoutUtilities.isPosible(rScreen)) {
248
                        layout.getRect().setRect(rScreen);
249
                }
250
                IFFrame[] fframes=layout.getFFrames();
251
                for (int i=0;i<fframes.length;i++){
252
                        if (fframes[i] instanceof FFrameView){
253
                                Point2D point1=new Point2D.Double(p2.getX()-(layout.getWidth()/2)*difw,p2.getY()-(layout.getHeight()/2)*difw);
254
                                Point2D point2=new Point2D.Double(p2.getX()+(layout.getWidth()/2)*difw,p2.getY()+(layout.getHeight()/2)*difw);
255
                                ((FFrameView)fframes[i]).getRectView(point1,point2);
256
                        }
257
                }
258
        }
259

    
260
        /**
261
         * Realiza un zoom out sobre el Layout que se le pasa como par?metro.
262
         *
263
         * @param dif punto de inicio del rect?ngulo.
264
         * @param p2 punto final del rec?ngulo.
265
         */
266
        public void setZoom(double dif, Point p2) {
267
                Point2D.Double pSheet2 = FLayoutUtilities.toSheetPoint(new Point2D.Double(
268
                                        p2.getX(), p2.getY()), layout.getAT());
269
                Rectangle2D.Double rScreen = new Rectangle2D.Double();
270
                Rectangle2D.Double rSheet = new Rectangle2D.Double();
271

    
272
                double difw = dif;
273

    
274
                rSheet.x = (-pSheet2.getX() * difw) -
275
                        FLayoutUtilities.toSheetDistance(layout.getRect().getX(),
276
                                layout.getAT()) +
277
                        FLayoutUtilities.toSheetDistance(layout.getWidth() / 2,
278
                                layout.getAT());
279
                rSheet.y = (-pSheet2.getY() * difw) -
280
                        FLayoutUtilities.toSheetDistance(layout.getRect().getY(),
281
                                layout.getAT()) +
282
                        FLayoutUtilities.toSheetDistance(layout.getHeight() / 2,
283
                                layout.getAT());
284

    
285
                rSheet.width = FLayoutUtilities.toSheetDistance(layout.getRect()
286
                                                                                                                          .getWidth(),
287
                                layout.getAT()) * difw;
288
                rSheet.height = FLayoutUtilities.toSheetDistance(layout.getRect()
289
                                                                                                                           .getHeight(),
290
                                layout.getAT()) * difw;
291

    
292
                rScreen.setRect(FLayoutUtilities.fromSheetRect(rSheet, layout.getAT()));
293

    
294
                if (FLayoutUtilities.isPosible(rScreen)) {
295
                        layout.getRect().setRect(rScreen);
296
                }
297
        }
298

    
299
        /**
300
         * Realiza un zoom a escala 1:1.
301
         */
302
        public void realZoom() {
303
                double cm = layout.getAtributes().getPixXCm(layout.getRect());
304
                Toolkit kit = Toolkit.getDefaultToolkit();
305
                int resolution = kit.getScreenResolution();
306
                double dif = (cm * Attributes.PULGADA) / resolution;
307
                setZoom(1 / dif,
308
                        new Point(layout.getWidth() / 2, layout.getHeight() / 2));
309
                layout.setStatus(Layout.DESACTUALIZADO);
310
                layout.repaint();
311
        }
312

    
313
        /**
314
         * Realiza un zoom m?s a partir del punto central de pantalla.
315
         */
316
        public void zoomIn() {
317
                setZoom(2, new Point(layout.getWidth() / 2, layout.getHeight() / 2));
318
                layout.setStatus(Layout.DESACTUALIZADO);
319
                layout.repaint();
320
        }
321

    
322
        /**
323
         * Realiza un zoom menos a partir del punto central de pantalla.
324
         */
325
        public void zoomOut() {
326
                setZoom(0.5, new Point(layout.getWidth() / 2, layout.getHeight() / 2));
327
                layout.setStatus(Layout.DESACTUALIZADO);
328
                layout.repaint();
329
        }
330

    
331
        /**
332
         * Realiza un zoom a los elementos que esten seleccionados, si no hay
333
         * ning?n elemento seleccionado no realiza ning?n zoom
334
         */
335
        public void zoomSelect() {
336
                Rectangle2D.Double recaux = null;
337

    
338
                for (int i = 0; i < layout.getFFrames().length; i++) {
339
                        if (((IFFrame) layout.getFFrame(i)).getSelected() != IFFrame.NOSELECT) {
340
                                if (recaux == null) {
341
                                        recaux = ((IFFrame) layout.getFFrame(i)).getBoundingBox(layout.getAT());
342
                                } else {
343
                                        recaux.add(((IFFrame) layout.getFFrame(i)).getBoundingBox(
344
                                                        layout.getAT()));
345
                                }
346
                        }
347
                }
348

    
349
                if (recaux != null) {
350
                        Point p1 = new Point((int) recaux.x, (int) recaux.y);
351
                        Point p2 = new Point((int) recaux.getMaxX(), (int) recaux.getMaxY());
352
                        setZoomIn(p1, p2);
353
                        layout.setStatus(Layout.DESACTUALIZADO);
354
                        layout.repaint();
355
                }
356
        }
357

    
358
        /**
359
         * Realiza un zoom m?s sobre la vista del FFrameView seleccionado.
360
         *
361
         * @param p1 punto inicial.
362
         * @param p2 punto final.
363
         */
364
        public void setViewZoomIn(Point2D p1, Point2D p2) {
365
                for (int i = 0; i < layout.getFFrames().length; i++) {
366
                        if (layout.getFFrame(i) instanceof IFFrameUseFMap) {
367
                                IFFrameUseFMap fframe = (IFFrameUseFMap) layout.getFFrame(i);
368

    
369
                                if (((IFFrame)fframe).getSelected() != IFFrame.NOSELECT) {
370
                                        p1 = functions.toMapPoint(p1, fframe.getATMap());
371
                                        p2 = functions.toMapPoint(p2, fframe.getATMap());
372

    
373
                                        // Borramos el anterior
374
                                        layout.setStatus(Layout.DESACTUALIZADO);
375

    
376
                                        if (java.lang.Math.abs(p1.getX() - p2.getX()) <= 3) {
377
                                                double nuevoX;
378
                                                double nuevoY;
379
                                                double cX;
380
                                                double cY;
381

    
382
                                                cX = p2.getX();
383
                                                cY = p2.getY();
384

    
385
                                                double factor = 0.6;
386
                                                Rectangle2D.Double r = new Rectangle2D.Double();
387

    
388
                                                nuevoX = cX -
389
                                                        ((fframe.getFMap().getViewPort().getExtent()
390
                                                                        .getWidth() * factor) / 2.0);
391
                                                nuevoY = cY -
392
                                                        ((fframe.getFMap().getViewPort().getExtent()
393
                                                                        .getHeight() * factor) / 2.0);
394
                                                r.x = nuevoX;
395
                                                r.y = nuevoY;
396
                                                r.width = fframe.getFMap().getViewPort().getExtent()
397
                                                                                .getWidth() * factor;
398
                                                r.height = fframe.getFMap().getViewPort().getExtent()
399
                                                                                 .getHeight() * factor;
400

    
401
                                                fframe.setNewExtent(r);
402
                                        } else {
403
                                                //        Fijamos el nuevo extent
404
                                                Rectangle2D.Double r = new Rectangle2D.Double();
405

    
406
                                                r.setFrameFromDiagonal(p1, p2);
407

    
408
                                                fframe.setNewExtent(r);
409
                                        }
410
                                        fframe.refresh();
411
                                        // Fin del else
412
                                        //layout.repaint();
413
                                }
414
                        }
415
                }
416
        }
417

    
418
        /**
419
         * Realiza un zoom menos sobre la vista del FFrameView seleccionado.
420
         *
421
         * @param p2 Punto sobre el que realizar el zoom menos.
422
         */
423
        public void setViewZoomOut(Point p2) {
424
                Point2D.Double pWorld;
425

    
426
                for (int i = 0; i < layout.getFFrames().length; i++) {
427
                        if (layout.getFFrame(i) instanceof IFFrameUseFMap) {
428
                                IFFrameUseFMap fframe = (IFFrameUseFMap) layout.getFFrame(i);
429

    
430
                                if (((IFFrame)fframe).getSelected() != IFFrame.NOSELECT) {
431
                                        double nuevoX;
432
                                        double nuevoY;
433
                                        double cX;
434
                                        double cY;
435
                                        Point pScreen = new Point((int) p2.getX(), (int) p2.getY());
436
                                        pWorld = functions.toMapPoint(pScreen, fframe.getATMap());
437

    
438
                                        cX = pWorld.getX();
439
                                        cY = pWorld.getY();
440

    
441
                                        double factor = 1.8;
442
                                        Rectangle2D.Double r = new Rectangle2D.Double();
443

    
444
                                        nuevoX = cX -
445
                                                ((fframe.getFMap().getViewPort().getExtent().getWidth() * factor) / 2.0);
446
                                        nuevoY = cY -
447
                                                ((fframe.getFMap().getViewPort().getExtent().getHeight() * factor) / 2.0);
448
                                        r.x = nuevoX;
449
                                        r.y = nuevoY;
450
                                        r.width = fframe.getFMap().getViewPort().getExtent()
451
                                                                        .getWidth() * factor;
452
                                        r.height = fframe.getFMap().getViewPort().getExtent()
453
                                                                         .getHeight() * factor;
454

    
455
                                        fframe.setNewExtent(r);
456
                                        fframe.refresh();
457
                                        ///fframe.getFMap().setCancelDrawing(false);
458
                                }
459
                        }
460
                }
461
        }
462
}