Statistics
| Revision:

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

History | View | Annotate | Download (13.3 KB)

1 312 fernando
/*
2
 * Created on 17-sep-2004
3
 *
4
 */
5 1103 fjp
/* 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 312 fernando
package com.iver.cit.gvsig.gui.layout;
46
47 4453 caballero
import com.iver.cit.gvsig.gui.layout.fframes.FFrameView;
48 312 fernando
import com.iver.cit.gvsig.gui.layout.fframes.IFFrame;
49 3676 caballero
import com.iver.cit.gvsig.gui.layout.fframes.IFFrameUseFMap;
50 312 fernando
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 1074 vcaballero
        private Layout layout = null;
64 4453 caballero
        private FLayoutFunctions functions = null;
65 312 fernando
66 1074 vcaballero
        /**
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 4453 caballero
                functions = new FLayoutFunctions(layout);
74 1074 vcaballero
        }
75 312 fernando
76 1074 vcaballero
        /**
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 4453 caballero
                /*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 4774 caballero
94 3121 caballero
                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 1074 vcaballero
                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 312 fernando
109 2425 caballero
                double xmin;
110
                double xmax;
111
                double ymin;
112
                double ymax = 0;
113 312 fernando
114 1074 vcaballero
                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 312 fernando
122 1074 vcaballero
                if (pSheet1.y > pSheet2.y) {
123
                        ymin = pSheet2.y;
124 2425 caballero
                        ymax = pSheet1.y;
125 1074 vcaballero
                } else {
126
                        ymin = pSheet1.y;
127 2425 caballero
                        ymax = pSheet2.y;
128 1074 vcaballero
                }
129 4453 caballero
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 1074 vcaballero
                if (java.lang.Math.abs(layout.getFirstPoint().x - p2.x) < 4) {
142 4453 caballero
                        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 5474 caballero
                        /*IFFrame[] fframes=layout.getFFrames();
157 4453 caballero
                        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 5474 caballero
                        }*/
164 4774 caballero
165
166 1074 vcaballero
                } else {
167 2425 caballero
                        double wv = FLayoutUtilities.toSheetDistance(layout.getVisibleRect()
168
                                                                                                                           .getWidth(),
169 1074 vcaballero
                                        layout.getAT());
170 2425 caballero
                        double hv = FLayoutUtilities.toSheetDistance(layout.getVisibleRect()
171
                                                                                                                           .getHeight(),
172
                                        layout.getAT());
173 1074 vcaballero
                        double mw = xmax - xmin;
174 2425 caballero
                        double mh = ymax - ymin;
175
                        double difw = wv / mw;
176
                        double difh = hv / mh;
177 312 fernando
178 2425 caballero
                        if (difw < difh) {
179 4453 caballero
                                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 2425 caballero
                        } else {
187 4453 caballero
                                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 2425 caballero
                        }
195 4453 caballero
196
                        rScreen.setRect(FLayoutUtilities.fromSheetRect(rSheet,
197
                                        layout.getAT()));
198 4774 caballero
199
        /*                IFFrame[] fframes=layout.getFFrames();
200 4453 caballero
                        for (int i=0;i<fframes.length;i++){
201
                                if (fframes[i] instanceof FFrameView){
202
                                        ((FFrameView)fframes[i]).getRectView(p1,p2);
203
                                }
204
                        }
205 4774 caballero
                        */
206 1074 vcaballero
                }
207 4453 caballero
208
                if (FLayoutUtilities.isPosible(rScreen)) {
209
                        layout.getRect().setRect(rScreen);
210
                }
211 4774 caballero
212 1074 vcaballero
        }
213 312 fernando
214 1074 vcaballero
        /**
215
         * Realiza un zoom out sobre el Layout que se le pasa como par?metro.
216
         *
217
         * @param p2 punto final del rec?ngulo.
218
         */
219
        public void setZoomOut(Point p2) {
220 4453 caballero
                Point2D.Double pSheet2 = FLayoutUtilities.toSheetPoint(new Point2D.Double(
221
                                        p2.getX(), p2.getY()), layout.getAT());
222
223
                Rectangle2D.Double rScreen = new Rectangle2D.Double();
224
                Rectangle2D.Double rSheet = new Rectangle2D.Double();
225
226
                double difw = 0.5;
227
228
                rSheet.x = (-pSheet2.getX() * difw) -
229
                        FLayoutUtilities.toSheetDistance(layout.getRect().getX(),
230
                                layout.getAT()) +
231
                        FLayoutUtilities.toSheetDistance(layout.getWidth() / 2,
232
                                layout.getAT());
233
                rSheet.y = (-pSheet2.getY() * difw) -
234
                        FLayoutUtilities.toSheetDistance(layout.getRect().getY(),
235
                                layout.getAT()) +
236
                        FLayoutUtilities.toSheetDistance(layout.getHeight() / 2,
237
                                layout.getAT());
238
239
                rSheet.width = FLayoutUtilities.toSheetDistance(layout.getRect()
240
                                                                                                                          .getWidth(),
241
                                layout.getAT()) * difw;
242
                rSheet.height = FLayoutUtilities.toSheetDistance(layout.getRect()
243
                                                                                                                           .getHeight(),
244
                                layout.getAT()) * difw;
245
246
                rScreen.setRect(FLayoutUtilities.fromSheetRect(rSheet, layout.getAT()));
247
248
                if (FLayoutUtilities.isPosible(rScreen)) {
249
                        layout.getRect().setRect(rScreen);
250
                }
251
                IFFrame[] fframes=layout.getFFrames();
252
                for (int i=0;i<fframes.length;i++){
253
                        if (fframes[i] instanceof FFrameView){
254
                                Point2D point1=new Point2D.Double(p2.getX()-(layout.getWidth()/2)*difw,p2.getY()-(layout.getHeight()/2)*difw);
255
                                Point2D point2=new Point2D.Double(p2.getX()+(layout.getWidth()/2)*difw,p2.getY()+(layout.getHeight()/2)*difw);
256
                                ((FFrameView)fframes[i]).getRectView(point1,point2);
257
                        }
258
                }
259 1074 vcaballero
        }
260 312 fernando
261 1074 vcaballero
        /**
262
         * Realiza un zoom out sobre el Layout que se le pasa como par?metro.
263
         *
264
         * @param dif punto de inicio del rect?ngulo.
265
         * @param p2 punto final del rec?ngulo.
266
         */
267
        public void setZoom(double dif, Point p2) {
268
                Point2D.Double pSheet2 = FLayoutUtilities.toSheetPoint(new Point2D.Double(
269
                                        p2.getX(), p2.getY()), layout.getAT());
270
                Rectangle2D.Double rScreen = new Rectangle2D.Double();
271
                Rectangle2D.Double rSheet = new Rectangle2D.Double();
272 312 fernando
273 1074 vcaballero
                double difw = dif;
274 312 fernando
275 4453 caballero
                rSheet.x = (-pSheet2.getX() * difw) -
276
                        FLayoutUtilities.toSheetDistance(layout.getRect().getX(),
277
                                layout.getAT()) +
278 1074 vcaballero
                        FLayoutUtilities.toSheetDistance(layout.getWidth() / 2,
279
                                layout.getAT());
280 4453 caballero
                rSheet.y = (-pSheet2.getY() * difw) -
281
                        FLayoutUtilities.toSheetDistance(layout.getRect().getY(),
282
                                layout.getAT()) +
283 1074 vcaballero
                        FLayoutUtilities.toSheetDistance(layout.getHeight() / 2,
284
                                layout.getAT());
285 312 fernando
286 4453 caballero
                rSheet.width = FLayoutUtilities.toSheetDistance(layout.getRect()
287
                                                                                                                          .getWidth(),
288
                                layout.getAT()) * difw;
289
                rSheet.height = FLayoutUtilities.toSheetDistance(layout.getRect()
290
                                                                                                                           .getHeight(),
291
                                layout.getAT()) * difw;
292 312 fernando
293 1074 vcaballero
                rScreen.setRect(FLayoutUtilities.fromSheetRect(rSheet, layout.getAT()));
294 312 fernando
295 1074 vcaballero
                if (FLayoutUtilities.isPosible(rScreen)) {
296 4453 caballero
                        layout.getRect().setRect(rScreen);
297 1074 vcaballero
                }
298
        }
299 312 fernando
300 1074 vcaballero
        /**
301
         * Realiza un zoom a escala 1:1.
302
         */
303
        public void realZoom() {
304
                double cm = layout.getAtributes().getPixXCm(layout.getRect());
305
                Toolkit kit = Toolkit.getDefaultToolkit();
306
                int resolution = kit.getScreenResolution();
307
                double dif = (cm * Attributes.PULGADA) / resolution;
308
                setZoom(1 / dif,
309
                        new Point(layout.getWidth() / 2, layout.getHeight() / 2));
310
                layout.setStatus(Layout.DESACTUALIZADO);
311
                layout.repaint();
312
        }
313 312 fernando
314 1074 vcaballero
        /**
315
         * Realiza un zoom m?s a partir del punto central de pantalla.
316
         */
317
        public void zoomIn() {
318 4453 caballero
                setZoom(2, new Point(layout.getWidth() / 2, layout.getHeight() / 2));
319 1074 vcaballero
                layout.setStatus(Layout.DESACTUALIZADO);
320
                layout.repaint();
321
        }
322 312 fernando
323 1074 vcaballero
        /**
324
         * Realiza un zoom menos a partir del punto central de pantalla.
325
         */
326
        public void zoomOut() {
327 4453 caballero
                setZoom(0.5, new Point(layout.getWidth() / 2, layout.getHeight() / 2));
328 1074 vcaballero
                layout.setStatus(Layout.DESACTUALIZADO);
329
                layout.repaint();
330
        }
331 312 fernando
332 1074 vcaballero
        /**
333
         * Realiza un zoom a los elementos que esten seleccionados, si no hay
334
         * ning?n elemento seleccionado no realiza ning?n zoom
335
         */
336
        public void zoomSelect() {
337
                Rectangle2D.Double recaux = null;
338 312 fernando
339 3550 caballero
                for (int i = 0; i < layout.getFFrames().length; i++) {
340
                        if (((IFFrame) layout.getFFrame(i)).getSelected() != IFFrame.NOSELECT) {
341 1074 vcaballero
                                if (recaux == null) {
342 3550 caballero
                                        recaux = ((IFFrame) layout.getFFrame(i)).getBoundingBox(layout.getAT());
343 1074 vcaballero
                                } else {
344 3550 caballero
                                        recaux.add(((IFFrame) layout.getFFrame(i)).getBoundingBox(
345 1074 vcaballero
                                                        layout.getAT()));
346
                                }
347
                        }
348
                }
349 312 fernando
350 1074 vcaballero
                if (recaux != null) {
351
                        Point p1 = new Point((int) recaux.x, (int) recaux.y);
352
                        Point p2 = new Point((int) recaux.getMaxX(), (int) recaux.getMaxY());
353
                        setZoomIn(p1, p2);
354
                        layout.setStatus(Layout.DESACTUALIZADO);
355
                        layout.repaint();
356
                }
357
        }
358 312 fernando
359 1074 vcaballero
        /**
360
         * Realiza un zoom m?s sobre la vista del FFrameView seleccionado.
361
         *
362
         * @param p1 punto inicial.
363
         * @param p2 punto final.
364
         */
365
        public void setViewZoomIn(Point2D p1, Point2D p2) {
366 3550 caballero
                for (int i = 0; i < layout.getFFrames().length; i++) {
367 3676 caballero
                        if (layout.getFFrame(i) instanceof IFFrameUseFMap) {
368
                                IFFrameUseFMap fframe = (IFFrameUseFMap) layout.getFFrame(i);
369 312 fernando
370 3676 caballero
                                if (((IFFrame)fframe).getSelected() != IFFrame.NOSELECT) {
371 4453 caballero
                                        p1 = functions.toMapPoint(p1, fframe.getATMap());
372
                                        p2 = functions.toMapPoint(p2, fframe.getATMap());
373 312 fernando
374 1074 vcaballero
                                        // Borramos el anterior
375
                                        layout.setStatus(Layout.DESACTUALIZADO);
376 312 fernando
377 1074 vcaballero
                                        if (java.lang.Math.abs(p1.getX() - p2.getX()) <= 3) {
378
                                                double nuevoX;
379
                                                double nuevoY;
380
                                                double cX;
381
                                                double cY;
382 312 fernando
383 1074 vcaballero
                                                cX = p2.getX();
384
                                                cY = p2.getY();
385 312 fernando
386 1074 vcaballero
                                                double factor = 0.6;
387
                                                Rectangle2D.Double r = new Rectangle2D.Double();
388 312 fernando
389 1074 vcaballero
                                                nuevoX = cX -
390
                                                        ((fframe.getFMap().getViewPort().getExtent()
391
                                                                        .getWidth() * factor) / 2.0);
392
                                                nuevoY = cY -
393
                                                        ((fframe.getFMap().getViewPort().getExtent()
394
                                                                        .getHeight() * factor) / 2.0);
395
                                                r.x = nuevoX;
396
                                                r.y = nuevoY;
397
                                                r.width = fframe.getFMap().getViewPort().getExtent()
398
                                                                                .getWidth() * factor;
399
                                                r.height = fframe.getFMap().getViewPort().getExtent()
400
                                                                                 .getHeight() * factor;
401 312 fernando
402
                                                fframe.setNewExtent(r);
403 1074 vcaballero
                                        } else {
404
                                                //        Fijamos el nuevo extent
405
                                                Rectangle2D.Double r = new Rectangle2D.Double();
406 312 fernando
407 1074 vcaballero
                                                r.setFrameFromDiagonal(p1, p2);
408 312 fernando
409 1074 vcaballero
                                                fframe.setNewExtent(r);
410
                                        }
411 3676 caballero
                                        fframe.refresh();
412 1074 vcaballero
                                        // Fin del else
413 3550 caballero
                                        //layout.repaint();
414 1074 vcaballero
                                }
415
                        }
416
                }
417
        }
418 312 fernando
419 1074 vcaballero
        /**
420
         * Realiza un zoom menos sobre la vista del FFrameView seleccionado.
421
         *
422
         * @param p2 Punto sobre el que realizar el zoom menos.
423
         */
424
        public void setViewZoomOut(Point p2) {
425
                Point2D.Double pWorld;
426 312 fernando
427 3550 caballero
                for (int i = 0; i < layout.getFFrames().length; i++) {
428 3676 caballero
                        if (layout.getFFrame(i) instanceof IFFrameUseFMap) {
429
                                IFFrameUseFMap fframe = (IFFrameUseFMap) layout.getFFrame(i);
430 312 fernando
431 3676 caballero
                                if (((IFFrame)fframe).getSelected() != IFFrame.NOSELECT) {
432 1074 vcaballero
                                        double nuevoX;
433
                                        double nuevoY;
434
                                        double cX;
435
                                        double cY;
436
                                        Point pScreen = new Point((int) p2.getX(), (int) p2.getY());
437 4453 caballero
                                        pWorld = functions.toMapPoint(pScreen, fframe.getATMap());
438 312 fernando
439 1074 vcaballero
                                        cX = pWorld.getX();
440
                                        cY = pWorld.getY();
441 312 fernando
442 1074 vcaballero
                                        double factor = 1.8;
443
                                        Rectangle2D.Double r = new Rectangle2D.Double();
444 312 fernando
445 1074 vcaballero
                                        nuevoX = cX -
446
                                                ((fframe.getFMap().getViewPort().getExtent().getWidth() * factor) / 2.0);
447
                                        nuevoY = cY -
448
                                                ((fframe.getFMap().getViewPort().getExtent().getHeight() * factor) / 2.0);
449
                                        r.x = nuevoX;
450
                                        r.y = nuevoY;
451
                                        r.width = fframe.getFMap().getViewPort().getExtent()
452
                                                                        .getWidth() * factor;
453
                                        r.height = fframe.getFMap().getViewPort().getExtent()
454
                                                                         .getHeight() * factor;
455 312 fernando
456
                                        fframe.setNewExtent(r);
457 3676 caballero
                                        fframe.refresh();
458 1074 vcaballero
                                        ///fframe.getFMap().setCancelDrawing(false);
459
                                }
460
                        }
461
                }
462
        }
463 312 fernando
}