svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / layout / FLayoutZooms.java @ 28368
History | View | Annotate | Download (16.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 java.awt.Point; |
48 |
import java.awt.Toolkit; |
49 |
import java.awt.geom.AffineTransform; |
50 |
import java.awt.geom.Point2D; |
51 |
import java.awt.geom.Rectangle2D; |
52 |
import java.util.prefs.Preferences; |
53 |
|
54 |
import com.iver.cit.gvsig.fmap.MapContext; |
55 |
import com.iver.cit.gvsig.project.documents.layout.fframes.IFFrame; |
56 |
import com.iver.cit.gvsig.project.documents.layout.fframes.IFFrameUseFMap; |
57 |
import com.iver.cit.gvsig.project.documents.layout.gui.Layout; |
58 |
|
59 |
|
60 |
/**
|
61 |
* Clase encargada de realizar los zooms al Layout.
|
62 |
*
|
63 |
* @author Vicente Caballero Navarro
|
64 |
*/
|
65 |
public class FLayoutZooms { |
66 |
private Layout layout = null; |
67 |
|
68 |
public FLayoutZooms(Layout l) {
|
69 |
layout = l; |
70 |
} |
71 |
|
72 |
/**
|
73 |
* Realiza un zoom por rect?ngulo o por punto con un escalado por defecto
|
74 |
* sobre el Layout que se le pasa como par?metro.
|
75 |
*
|
76 |
* @param p1 punto de inicio del rect?ngulo.
|
77 |
* @param p2 punto final del rec?ngulo.
|
78 |
*/
|
79 |
public void setZoomIn(Point p1, Point p2) { |
80 |
if (java.lang.Math.abs(layout.getLayoutControl().getFirstPoint().x - p2.x) < 4) { |
81 |
double difw = 2; |
82 |
setZoom(difw, p2); |
83 |
} else {
|
84 |
if (p1.getX() > p2.getX()) {
|
85 |
int aux = p2.x;
|
86 |
p2.x = p1.x; |
87 |
p1.x = aux; |
88 |
} |
89 |
|
90 |
if (p1.getY() > p2.getY()) {
|
91 |
int aux = p2.y;
|
92 |
p2.y = p1.y; |
93 |
p1.y = aux; |
94 |
} |
95 |
|
96 |
Point2D.Double pSheet1 = FLayoutUtilities.toSheetPoint(
|
97 |
new Point2D.Double(p1.getX(), p1.getY()), layout.getLayoutControl().getAT()); |
98 |
Point2D.Double pSheet2 = FLayoutUtilities.toSheetPoint(
|
99 |
new Point2D.Double(p2.getX(), p2.getY()), layout.getLayoutControl().getAT()); |
100 |
|
101 |
double xmin;
|
102 |
double xmax;
|
103 |
double ymin;
|
104 |
double ymax = 0; |
105 |
|
106 |
if (pSheet1.x > pSheet2.x) {
|
107 |
xmin = pSheet2.x; |
108 |
xmax = pSheet1.x; |
109 |
} else {
|
110 |
xmin = pSheet1.x; |
111 |
xmax = pSheet2.x; |
112 |
} |
113 |
|
114 |
if (pSheet1.y > pSheet2.y) {
|
115 |
ymin = pSheet2.y; |
116 |
ymax = pSheet1.y; |
117 |
} else {
|
118 |
ymin = pSheet1.y; |
119 |
ymax = pSheet2.y; |
120 |
} |
121 |
|
122 |
Rectangle2D.Double rScreen = new Rectangle2D.Double(); |
123 |
Rectangle2D.Double rSheet = new Rectangle2D.Double(); |
124 |
double x = FLayoutUtilities.toSheetDistance(
|
125 |
layout.getLayoutControl().getRect().getX(), layout.getLayoutControl().getAT()); |
126 |
double y = FLayoutUtilities.toSheetDistance(
|
127 |
layout.getLayoutControl().getRect().getY(), layout.getLayoutControl().getAT()); |
128 |
double w = FLayoutUtilities.toSheetDistance(layout.getLayoutControl().getRect()
|
129 |
.getWidth(), layout.getLayoutControl().getAT()); |
130 |
double h = FLayoutUtilities.toSheetDistance(layout.getLayoutControl().getRect()
|
131 |
.getHeight(), layout.getLayoutControl().getAT()); |
132 |
|
133 |
double wv = FLayoutUtilities.toSheetDistance(layout
|
134 |
.getVisibleRect().getWidth(), layout.getLayoutControl().getAT()); |
135 |
double hv = FLayoutUtilities.toSheetDistance(layout
|
136 |
.getVisibleRect().getHeight(), layout.getLayoutControl().getAT()); |
137 |
double mw = xmax - xmin;
|
138 |
double mh = ymax - ymin;
|
139 |
double difw = wv / mw;
|
140 |
double difh = hv / mh;
|
141 |
|
142 |
if (difw < difh) {
|
143 |
rSheet.x = (-xmin * difw) |
144 |
- x |
145 |
+ ((wv - ((pSheet2.getX() - pSheet1.getX()) * difw)) / 2);
|
146 |
rSheet.y = (-ymin * difw) |
147 |
- y |
148 |
+ ((hv - ((pSheet2.getY() - pSheet1.getY()) * difw)) / 2);
|
149 |
|
150 |
rSheet.width = w * difw; |
151 |
rSheet.height = h * difw; |
152 |
} else {
|
153 |
rSheet.x = (-xmin * difh) |
154 |
- x |
155 |
+ ((wv - ((pSheet2.getX() - pSheet1.getX()) * difh)) / 2);
|
156 |
rSheet.y = (-ymin * difh) |
157 |
- y |
158 |
+ ((hv - ((pSheet2.getY() - pSheet1.getY()) * difh)) / 2);
|
159 |
|
160 |
rSheet.width = w * difh; |
161 |
rSheet.height = h * difh; |
162 |
} |
163 |
setPointsToZoom(p1, p2); |
164 |
rScreen.setRect(FLayoutUtilities.fromSheetRect(rSheet, layout.getLayoutControl() |
165 |
.getAT())); |
166 |
if (FLayoutUtilities.isPosible(rScreen)) {
|
167 |
layout.getLayoutControl().getRect().setRect(rScreen); |
168 |
} |
169 |
} |
170 |
} |
171 |
|
172 |
/**
|
173 |
* Realiza un zoom out sobre el Layout que se le pasa como par?metro.
|
174 |
*
|
175 |
* @param p2 punto central del rect?ngulo.
|
176 |
*/
|
177 |
public void setZoomOut(Point p2) { |
178 |
double difw = 0.5; |
179 |
setZoom(difw,p2); |
180 |
} |
181 |
|
182 |
/**
|
183 |
* Realiza un zoom out sobre el Layout que se le pasa como par?metro.
|
184 |
*
|
185 |
* @param dif factor.
|
186 |
* @param p2 punto final del rec?ngulo.
|
187 |
*/
|
188 |
public void setZoom(double dif, Point p2) { |
189 |
Point2D.Double pSheet2 = FLayoutUtilities.toSheetPoint(new Point2D.Double( |
190 |
p2.getX(), p2.getY()), layout.getLayoutControl().getAT()); |
191 |
Rectangle2D.Double rScreen = new Rectangle2D.Double(); |
192 |
Rectangle2D.Double rSheet = new Rectangle2D.Double(); |
193 |
|
194 |
double difw = dif;
|
195 |
|
196 |
rSheet.x = (-pSheet2.getX() * difw) - |
197 |
FLayoutUtilities.toSheetDistance(layout.getLayoutControl().getRect().getX(), |
198 |
layout.getLayoutControl().getAT()) + |
199 |
FLayoutUtilities.toSheetDistance(layout.getWidth() / 2,
|
200 |
layout.getLayoutControl().getAT()); |
201 |
rSheet.y = (-pSheet2.getY() * difw) - |
202 |
FLayoutUtilities.toSheetDistance(layout.getLayoutControl().getRect().getY(), |
203 |
layout.getLayoutControl().getAT()) + |
204 |
FLayoutUtilities.toSheetDistance(layout.getHeight() / 2,
|
205 |
layout.getLayoutControl().getAT()); |
206 |
|
207 |
rSheet.width = FLayoutUtilities.toSheetDistance(layout.getLayoutControl().getRect() |
208 |
.getWidth(), |
209 |
layout.getLayoutControl().getAT()) * difw; |
210 |
rSheet.height = FLayoutUtilities.toSheetDistance(layout.getLayoutControl().getRect() |
211 |
.getHeight(), |
212 |
layout.getLayoutControl().getAT()) * difw; |
213 |
|
214 |
rScreen.setRect(FLayoutUtilities.fromSheetRect(rSheet, layout.getLayoutControl().getAT())); |
215 |
|
216 |
if (FLayoutUtilities.isPosible(rScreen)) {
|
217 |
layout.getLayoutControl().getRect().setRect(rScreen); |
218 |
} |
219 |
|
220 |
// Para realizar el zoom a partir de un punto.
|
221 |
Point p1 = new Point((int) (p2.getX() - |
222 |
(layout.getWidth() / (difw * 2))),
|
223 |
(int) (p2.getY() - (layout.getHeight() / (difw * 2)))); |
224 |
p2 = new Point((int) (p2.getX() + (layout.getWidth() / (difw * 2))), |
225 |
(int) (p2.getY() + (layout.getHeight() / (difw * 2)))); |
226 |
setPointsToZoom(p1, p2); |
227 |
} |
228 |
/**
|
229 |
* Introduce los puntos de control para controlar el zoom del Layout.
|
230 |
*/
|
231 |
private void setPointsToZoom(Point p1, Point p2) { |
232 |
IFFrame[] fframes = layout.getLayoutContext().getFFrames();
|
233 |
|
234 |
for (int i = 0; i < fframes.length; i++) { |
235 |
if (fframes[i] instanceof IFFrameUseFMap) { |
236 |
IFFrameUseFMap fframe = (IFFrameUseFMap) fframes[i]; |
237 |
if (fframe.getATMap()!=null) { |
238 |
Point2D px1 = FLayoutFunctions.toMapPoint(p1, fframe.getATMap());
|
239 |
Point2D px2 = FLayoutFunctions.toMapPoint(p2, fframe.getATMap());
|
240 |
fframe.setPointsToZoom(px1, px2); |
241 |
} |
242 |
} |
243 |
} |
244 |
} |
245 |
|
246 |
/**
|
247 |
* Aplica el zoom real teniendo en cuenta la resoluci?n de pantalla.
|
248 |
*/
|
249 |
public void realZoom() { |
250 |
Preferences prefsResolution = Preferences.userRoot().node( "gvsig.configuration.screen" ); |
251 |
double cm = layout.getLayoutContext().getAtributes().getPixXCm(layout.getLayoutControl().getRect());
|
252 |
Toolkit kit = Toolkit.getDefaultToolkit(); |
253 |
double dpi = prefsResolution.getInt("dpi",kit.getScreenResolution()); |
254 |
double dif = (cm * Attributes.PULGADA) / dpi; |
255 |
setZoom(1 / dif,
|
256 |
new Point(layout.getWidth() / 2, layout.getHeight() / 2)); |
257 |
layout.getLayoutControl().refresh(); |
258 |
} |
259 |
|
260 |
/**
|
261 |
* Realiza un zoom in a partir del zoom actual de la vista.
|
262 |
*/
|
263 |
public void zoomIn() { |
264 |
setZoom(2, new Point(layout.getWidth() / 2, layout.getHeight() / 2)); |
265 |
layout.getLayoutControl().refresh(); |
266 |
} |
267 |
|
268 |
/**
|
269 |
* Realiza un zoom out a partir del zoom actual de la vista.
|
270 |
*/
|
271 |
public void zoomOut() { |
272 |
setZoom(0.5, new Point(layout.getWidth() / 2, layout.getHeight() / 2)); |
273 |
layout.getLayoutControl().refresh(); |
274 |
} |
275 |
|
276 |
/**
|
277 |
* Realiza un zoom a los elementos que esten seleccionados, si no hay
|
278 |
* ning?n elemento seleccionado no realiza ning?n zoom
|
279 |
*/
|
280 |
public void zoomSelect() { |
281 |
Rectangle2D.Double recaux = null; |
282 |
IFFrame[] fframes=layout.getLayoutContext().getFFrames();
|
283 |
for (int i = 0; i < fframes.length; i++) { |
284 |
if (fframes[i].getSelected() != IFFrame.NOSELECT) {
|
285 |
if (recaux == null) { |
286 |
recaux = fframes[i].getBoundingBox(layout.getLayoutControl().getAT()); |
287 |
} else {
|
288 |
recaux.add(fframes[i].getBoundingBox( |
289 |
layout.getLayoutControl().getAT())); |
290 |
} |
291 |
} |
292 |
} |
293 |
|
294 |
if (recaux != null) { |
295 |
Point p1 = new Point((int) recaux.x, (int) recaux.y); |
296 |
Point p2 = new Point((int) recaux.getMaxX(), (int) recaux.getMaxY()); |
297 |
setZoomIn(p1, p2); |
298 |
layout.getLayoutControl().refresh(); |
299 |
} |
300 |
} |
301 |
/**
|
302 |
* Realiza un zoom a todos los elementos del layout.
|
303 |
*/
|
304 |
public void zoomAllFrames() { |
305 |
Rectangle2D.Double recaux = null; |
306 |
IFFrame[] fframes=layout.getLayoutControl().getLayoutContext().getFFrames();
|
307 |
for (int i = 0; i < fframes.length; i++) { |
308 |
if (recaux == null) { |
309 |
recaux = fframes[i].getBoundingBox(layout.getLayoutControl().getAT()); |
310 |
} else {
|
311 |
recaux.add(fframes[i].getBoundingBox( |
312 |
layout.getLayoutControl().getAT())); |
313 |
} |
314 |
} |
315 |
|
316 |
if (recaux != null) { |
317 |
Point p1 = new Point((int) recaux.x, (int) recaux.y); |
318 |
Point p2 = new Point((int) recaux.getMaxX(), (int) recaux.getMaxY()); |
319 |
setZoomIn(p1, p2); |
320 |
layout.getLayoutControl().refresh(); |
321 |
} |
322 |
} |
323 |
|
324 |
/**
|
325 |
* Realiza un zoom in a las vista a?adidas al Layout que esten seleccionadas
|
326 |
*
|
327 |
* @param p1 Punto inicial del rect?ngulo
|
328 |
* @param p2 Punto final del rect?ngulo
|
329 |
*/
|
330 |
public void setViewZoomIn(Point2D p1, Point2D p2) { |
331 |
IFFrame[] fframes=layout.getLayoutContext().getFFrames();
|
332 |
for (int i = 0; i < fframes.length; i++) { |
333 |
if (fframes[i] instanceof IFFrameUseFMap) { |
334 |
IFFrameUseFMap fframe = (IFFrameUseFMap) fframes[i]; |
335 |
|
336 |
if (((IFFrame) fframe).getSelected() != IFFrame.NOSELECT) {
|
337 |
//IFFrameUseFMap fframeAux=(IFFrameUseFMap)((IFFrame)fframe).cloneFFrame(layout);
|
338 |
p1 = FLayoutFunctions.toMapPoint(p1, fframe.getATMap()); |
339 |
p2 = FLayoutFunctions.toMapPoint(p2, fframe.getATMap()); |
340 |
|
341 |
|
342 |
// Borramos el anterior
|
343 |
layout.getLayoutControl().setStatus(LayoutControl.DESACTUALIZADO); |
344 |
Rectangle2D.Double r = new Rectangle2D.Double(); |
345 |
|
346 |
if (java.lang.Math.abs(p1.getX() - p2.getX()) <= 3) { |
347 |
double nuevoX;
|
348 |
double nuevoY;
|
349 |
double cX;
|
350 |
double cY;
|
351 |
|
352 |
cX = p2.getX(); |
353 |
cY = p2.getY(); |
354 |
|
355 |
double factor = 1/MapContext.ZOOMINFACTOR; |
356 |
|
357 |
Rectangle2D extent=fframe.getMapContext().getViewPort().getExtent();
|
358 |
if (extent!=null){ |
359 |
nuevoX = cX - |
360 |
((extent.getWidth() * factor) / 2.0);
|
361 |
nuevoY = cY - |
362 |
((extent.getHeight() * factor) / 2.0);
|
363 |
r.x = nuevoX; |
364 |
r.y = nuevoY; |
365 |
r.width = extent.getWidth() * factor; |
366 |
r.height = extent.getHeight() * factor; |
367 |
} |
368 |
|
369 |
//fframeAux.setNewExtent(r);
|
370 |
} else {
|
371 |
// Fijamos el nuevo extent
|
372 |
|
373 |
r.setFrameFromDiagonal(p1, p2); |
374 |
|
375 |
//fframeAux.setNewExtent(r);
|
376 |
} |
377 |
|
378 |
/*if (fframe.getTypeScale()!=IFFrameUseFMap.AUTOMATICO) {
|
379 |
fframeAux.setNewExtent(r);
|
380 |
fframeAux.refresh();
|
381 |
layout.getEFS().modifyFFrame((IFFrame)fframe,(IFFrame)fframeAux);
|
382 |
((IFFrame)fframeAux).getBoundingBox(layout.getAT());
|
383 |
layout.updateFFrames();
|
384 |
layout.setIsReSel(true);
|
385 |
}else {*/
|
386 |
fframe.setNewExtent(r); |
387 |
fframe.refresh(); |
388 |
///}
|
389 |
// Fin del else
|
390 |
//layout.repaint();
|
391 |
} |
392 |
} |
393 |
} |
394 |
} |
395 |
|
396 |
/**
|
397 |
* Realiza un zoom out a las vistas a?adidas al Layout y que est?n seleccionadas
|
398 |
*
|
399 |
* @param p2 Punto central
|
400 |
*/
|
401 |
public void setViewZoomOut(Point p2) { |
402 |
Point2D.Double pWorld;
|
403 |
IFFrame[] fframes=layout.getLayoutContext().getFFrames();
|
404 |
for (int i = 0; i < fframes.length; i++) { |
405 |
if (fframes[i] instanceof IFFrameUseFMap) { |
406 |
IFFrameUseFMap fframe = (IFFrameUseFMap) fframes[i]; |
407 |
|
408 |
if (((IFFrame) fframe).getSelected() != IFFrame.NOSELECT) {
|
409 |
//IFFrameUseFMap fframeAux=(IFFrameUseFMap)((IFFrame)fframe).cloneFFrame(layout);
|
410 |
double nuevoX;
|
411 |
double nuevoY;
|
412 |
double cX;
|
413 |
double cY;
|
414 |
Point pScreen = new Point((int) p2.getX(), (int) p2.getY()); |
415 |
pWorld = FLayoutFunctions.toMapPoint(pScreen, |
416 |
fframe.getATMap()); |
417 |
|
418 |
cX = pWorld.getX(); |
419 |
cY = pWorld.getY(); |
420 |
|
421 |
double factor = 1/MapContext.ZOOMOUTFACTOR; |
422 |
Rectangle2D.Double r = new Rectangle2D.Double(); |
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 |
r.x = nuevoX; |
429 |
r.y = nuevoY; |
430 |
r.width = extent.getWidth() * factor; |
431 |
r.height = extent.getHeight() * factor; |
432 |
fframe.setNewExtent(r); |
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 |
AffineTransform at=fframe.getATMap();
|
453 |
if (at!=null) { |
454 |
Point2D px1 = FLayoutFunctions.toMapPoint(p1, at);
|
455 |
Point2D px2 = FLayoutFunctions.toMapPoint(p2, at);
|
456 |
fframe.movePoints(px1, px2); |
457 |
} |
458 |
} |
459 |
} |
460 |
} |
461 |
} |