]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/gui/MapChart.java
Version 1, September 2006
[GpsPrune.git] / tim / prune / gui / MapChart.java
1 package tim.prune.gui;
2
3 import java.awt.Color;
4 import java.awt.Dimension;
5 import java.awt.FontMetrics;
6 import java.awt.Graphics;
7 import java.awt.event.ActionEvent;
8 import java.awt.event.ActionListener;
9 import java.awt.event.KeyEvent;
10 import java.awt.event.KeyListener;
11 import java.awt.event.MouseEvent;
12 import java.awt.event.MouseMotionListener;
13 import java.awt.event.MouseWheelEvent;
14 import java.awt.event.MouseWheelListener;
15 import java.awt.image.BufferedImage;
16
17 import javax.swing.JCheckBoxMenuItem;
18 import javax.swing.JMenuItem;
19 import javax.swing.JPopupMenu;
20
21 import tim.prune.App;
22 import tim.prune.I18nManager;
23 import tim.prune.data.DataPoint;
24 import tim.prune.data.Field;
25 import tim.prune.data.TrackInfo;
26
27
28 /**
29  * Display component for the main map
30  */
31 public class MapChart extends GenericChart implements MouseWheelListener, KeyListener, MouseMotionListener
32 {
33         // Constants
34         private static final int POINT_RADIUS = 4;
35         private static final int CLICK_SENSITIVITY = 10;
36         private static final double ZOOM_SCALE_FACTOR = 1.2;
37         private static final int PAN_DISTANCE = 10;
38         private static final int LIMIT_WAYPOINT_NAMES = 40;
39
40         // Colours
41         private static final Color COLOR_BG         = Color.WHITE;
42         private static final Color COLOR_POINT      = Color.BLUE;
43         private static final Color COLOR_CURR_RANGE = Color.GREEN;
44         private static final Color COLOR_CROSSHAIRS = Color.RED;
45         private static final Color COLOR_WAYPT_NAME = Color.BLACK;
46
47         // Instance variables
48         private App _app = null;
49         private BufferedImage _image = null;
50         private JPopupMenu _popup = null;
51         private JCheckBoxMenuItem _autoPanMenuItem = null;
52         private String _trackString = null;
53         private int _numPoints = -1;
54         private double _scale;
55         private double _offsetX, _offsetY, _zoomScale;
56         private int _lastSelectedPoint = -1;
57         private int _dragStartX = -1, _dragStartY = -1;
58         private int _zoomDragFromX = -1, _zoomDragFromY = -1;
59         private int _zoomDragToX = -1, _zoomDragToY = -1;
60         private boolean _zoomDragging = false;
61
62
63         /**
64          * Constructor
65          * @param inApp App object for callbacks
66          * @param inTrackInfo track info object
67          */
68         public MapChart(App inApp, TrackInfo inTrackInfo)
69         {
70                 super(inTrackInfo);
71                 _app = inApp;
72                 makePopup();
73                 addMouseListener(this);
74                 addMouseWheelListener(this);
75                 addMouseMotionListener(this);
76                 setFocusable(true);
77                 addKeyListener(this);
78                 MINIMUM_SIZE = new Dimension(200, 250);
79                 _zoomScale = 1.0;
80         }
81
82
83         /**
84          * Override track updating to refresh image
85          */
86         public void dataUpdated()
87         {
88                 // Check if number of points has changed or Track
89                 // object has a different signature
90                 if (_track.getNumPoints() != _numPoints)
91                 {
92                         _image = null;
93                         _numPoints = _track.getNumPoints();
94                 }
95                 super.dataUpdated();
96         }
97
98
99         /**
100          * Override paint method to draw map
101          */
102         public void paint(Graphics g)
103         {
104                 if (_track == null)
105                 {
106                         super.paint(g);
107                         return;
108                 }
109
110                 int width = getWidth();
111                 int height = getHeight();
112                 int x, y;
113
114                 // Find x and y ranges, and scale to fit
115                 double scaleX = (_track.getXRange().getMaximum() - _track.getXRange().getMinimum())
116                   / (width - 2 * (BORDER_WIDTH + POINT_RADIUS));
117                 double scaleY = (_track.getYRange().getMaximum() - _track.getYRange().getMinimum())
118                   / (height - 2 * (BORDER_WIDTH + POINT_RADIUS));
119                 _scale = scaleX;
120                 if (scaleY > _scale) _scale = scaleY;
121
122                 // Autopan if necessary
123                 int selectedPoint = _trackInfo.getSelection().getCurrentPointIndex();
124                 if (_autoPanMenuItem.isSelected() && selectedPoint >= 0 && selectedPoint != _lastSelectedPoint)
125                 {
126                         // Autopan is enabled and a point is selected - work out x and y to see if it's within range
127                         x = width/2 + (int) ((_track.getX(selectedPoint) - _offsetX) / _scale * _zoomScale);
128                         y = height/2 - (int) ((_track.getY(selectedPoint) - _offsetY) / _scale * _zoomScale);
129                         if (x < BORDER_WIDTH)
130                         {
131                                 // autopan left
132                                 _offsetX -= (width / 4 - x) * _scale / _zoomScale;
133                                 _image = null;
134                         }
135                         else if (x > (width - BORDER_WIDTH))
136                         {
137                                 // autopan right
138                                 _offsetX += (x - width * 3/4) * _scale / _zoomScale;
139                                 _image = null;
140                         }
141                         if (y < BORDER_WIDTH)
142                         {
143                                 // autopan up
144                                 _offsetY += (height / 4 - y) * _scale / _zoomScale;
145                                 _image = null;
146                         }
147                         else if (y > (height - BORDER_WIDTH))
148                         {
149                                 // autopan down
150                                 _offsetY -= (y - height * 3/4) * _scale / _zoomScale;
151                                 _image = null;
152                         }
153                 }
154                 _lastSelectedPoint = selectedPoint;
155
156                 if (_image == null || width != _image.getWidth() || height != _image.getHeight())
157                 {
158                         createBackgroundImage();
159                 }
160                 // draw buffered image onto g
161                 g.drawImage(_image, 0, 0, width, height, COLOR_BG, null);
162
163                 // draw selected range, if any
164                 if (_trackInfo.getSelection().hasRangeSelected() && !_zoomDragging)
165                 {
166                         int rangeStart = _trackInfo.getSelection().getStart();
167                         int rangeEnd = _trackInfo.getSelection().getEnd();
168                         g.setColor(COLOR_CURR_RANGE);
169                         for (int i=rangeStart; i<=rangeEnd; i++)
170                         {
171                                 x = width/2 + (int) ((_track.getX(i) - _offsetX) / _scale * _zoomScale);
172                                 y = height/2 - (int) ((_track.getY(i) - _offsetY) / _scale * _zoomScale);
173                                 if (x > BORDER_WIDTH && x < (width - BORDER_WIDTH)
174                                         && y < (height - BORDER_WIDTH) && y > BORDER_WIDTH)
175                                 {
176                                         g.drawOval(x - 2, y - 2, 4, 4);
177                                 }
178                         }
179                 }
180
181                 // Highlight selected point
182                 if (selectedPoint >= 0 && !_zoomDragging)
183                 {
184                         g.setColor(COLOR_CROSSHAIRS);
185                         x = width/2 + (int) ((_track.getX(selectedPoint) - _offsetX) / _scale * _zoomScale);
186                         y = height/2 - (int) ((_track.getY(selectedPoint) - _offsetY) / _scale * _zoomScale);
187                         if (x > BORDER_WIDTH && x < (width - BORDER_WIDTH)
188                                 && y < (height - BORDER_WIDTH) && y > BORDER_WIDTH)
189                         {
190                                 // Draw cross-hairs for current point
191                                 g.drawLine(x, BORDER_WIDTH, x, height - BORDER_WIDTH);
192                                 g.drawLine(BORDER_WIDTH, y, width - BORDER_WIDTH, y);
193
194                                 // Show selected point afterwards to make sure it's on top
195                                 g.drawOval(x - 2, y - 2, 4, 4);
196                                 g.drawOval(x - 3, y - 3, 6, 6);
197                         }
198                 }
199
200                 if (_zoomDragging)
201                 {
202                         g.setColor(COLOR_CROSSHAIRS);
203                         g.drawLine(_zoomDragFromX, _zoomDragFromY, _zoomDragFromX, _zoomDragToY);
204                         g.drawLine(_zoomDragFromX, _zoomDragFromY, _zoomDragToX, _zoomDragFromY);
205                         g.drawLine(_zoomDragToX, _zoomDragFromY, _zoomDragToX, _zoomDragToY);
206                         g.drawLine(_zoomDragFromX, _zoomDragToY, _zoomDragToX, _zoomDragToY);
207                 }
208         }
209
210
211         /**
212          * Draw the map onto an offscreen image
213          */
214         private void createBackgroundImage()
215         {
216                 int width = getWidth();
217                 int height = getHeight();
218                 int x, y;
219                 // Make a new image and initialise it
220                 _image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
221                 Graphics bufferedG = _image.getGraphics();
222                 super.paint(bufferedG);
223
224                 // Loop and show all points
225                 int numPoints = _track.getNumPoints();
226                 bufferedG.setColor(COLOR_POINT);
227                 int halfWidth = width/2;
228                 int halfHeight = height/2;
229                 for (int i=0; i<numPoints; i++)
230                 {
231                         x = halfWidth + (int) ((_track.getX(i) - _offsetX) / _scale * _zoomScale);
232                         y = halfHeight - (int) ((_track.getY(i) - _offsetY) / _scale * _zoomScale);
233                         if (x > BORDER_WIDTH && x < (width - BORDER_WIDTH)
234                                 && y < (height - BORDER_WIDTH) && y > BORDER_WIDTH)
235                         {
236                                 bufferedG.drawOval(x - 2, y - 2, 4, 4);
237                         }
238                 }
239
240                 // Loop again and show waypoints with names
241                 bufferedG.setColor(COLOR_WAYPT_NAME);
242                 FontMetrics fm = bufferedG.getFontMetrics();
243                 int nameHeight = fm.getHeight();
244                 int numWaypointNamesShown = 0;
245                 for (int i=0; i<numPoints; i++)
246                 {
247                         DataPoint point = _track.getPoint(i);
248                         String waypointName = point.getFieldValue(Field.WAYPT_NAME);
249                         if (waypointName != null && !waypointName.equals("") && numWaypointNamesShown < LIMIT_WAYPOINT_NAMES)
250                         {
251                                 x = halfWidth + (int) ((_track.getX(i) - _offsetX) / _scale * _zoomScale);
252                                 y = halfHeight - (int) ((_track.getY(i) - _offsetY) / _scale * _zoomScale);
253                                 if (x > BORDER_WIDTH && x < (width - BORDER_WIDTH)
254                                                 && y < (height - BORDER_WIDTH) && y > BORDER_WIDTH)
255                                 {
256                                         bufferedG.fillOval(x - 3, y - 3, 6, 6);
257                                         // Figure out where to draw name so it doesn't obscure track
258                                         int nameWidth = fm.stringWidth(waypointName);
259                                         if (nameWidth < (width - 2 * BORDER_WIDTH))
260                                         {
261                                                 double nameAngle = 0.3;
262                                                 double nameRadius = 1.0;
263                                                 boolean drawnName = false;
264                                                 while (!drawnName)
265                                                 {
266                                                         int nameX = x + (int) (nameRadius * Math.cos(nameAngle)) - (nameWidth/2);
267                                                         int nameY = y + (int) (nameRadius * Math.sin(nameAngle)) + (nameHeight/2);
268                                                         if (nameX > BORDER_WIDTH && (nameX + nameWidth) < (width - BORDER_WIDTH)
269                                                                 && nameY < (height - BORDER_WIDTH) && (nameY - nameHeight) > BORDER_WIDTH)
270                                                         {
271                                                                 // name can fit in grid - does it overlap data points?
272                                                                 if (!overlapsPoints(nameX, nameY, nameWidth, nameHeight) || nameRadius > 50.0)
273                                                                 {
274                                                                         bufferedG.drawString(waypointName, nameX, nameY);
275                                                                         drawnName = true;
276                                                                         numWaypointNamesShown++;
277                                                                 }
278                                                         }
279                                                         nameAngle += 0.08;
280                                                         nameRadius += 0.2;
281                                                         // wasn't room within the radius, so don't print name
282                                                         if (nameRadius > 50.0)
283                                                         {
284                                                                 drawnName = true;
285                                                         }
286                                                 }
287                                         }
288                                 }
289                         }
290                 }
291         }
292
293
294         /**
295          * Tests whether there are any data points within the specified x,y rectangle
296          * @param inX left X coordinate
297          * @param inY bottom Y coordinate
298          * @param inWidth width of rectangle
299          * @param inHeight height of rectangle
300          * @return true if there's at least one data point in the rectangle
301          */
302         private boolean overlapsPoints(int inX, int inY, int inWidth, int inHeight)
303         {
304                 // if (true) return true;
305                 for (int x=0; x<inWidth; x++)
306                 {
307                         for (int y=0; y<inHeight; y++)
308                         {
309                                 int pixelColor = _image.getRGB(inX + x, inY - y);
310                                 if (pixelColor != -1) return true;
311                         }
312                 }
313                 return false;
314         }
315
316
317         /**
318          * Make the popup menu for right-clicking the map
319          */
320         private void makePopup()
321         {
322                 _popup = new JPopupMenu();
323                 JMenuItem zoomIn = new JMenuItem(I18nManager.getText("menu.map.zoomin"));
324                 zoomIn.addActionListener(new ActionListener() {
325                         public void actionPerformed(ActionEvent e)
326                         {
327                                 zoomMap(true);
328                         }});
329                 zoomIn.setEnabled(true);
330                 _popup.add(zoomIn);
331                 JMenuItem zoomOut = new JMenuItem(I18nManager.getText("menu.map.zoomout"));
332                 zoomOut.addActionListener(new ActionListener() {
333                         public void actionPerformed(ActionEvent e)
334                         {
335                                 zoomMap(false);
336                         }});
337                 zoomOut.setEnabled(true);
338                 _popup.add(zoomOut);
339                 JMenuItem zoomFull = new JMenuItem(I18nManager.getText("menu.map.zoomfull"));
340                 zoomFull.addActionListener(new ActionListener() {
341                         public void actionPerformed(ActionEvent e)
342                         {
343                                 zoomToFullScale();
344                         }});
345                 zoomFull.setEnabled(true);
346                 _popup.add(zoomFull);
347                 _autoPanMenuItem = new JCheckBoxMenuItem(I18nManager.getText("menu.map.autopan"));
348                 _autoPanMenuItem.setSelected(true);
349                 _popup.add(_autoPanMenuItem);
350         }
351
352
353         /**
354          * Zoom map to full scale
355          */
356         private void zoomToFullScale()
357         {
358                 _zoomScale = 1.0;
359                 _offsetX = 0.0;
360                 _offsetY = 0.0;
361                 _numPoints = 0;
362                 dataUpdated();
363         }
364
365
366         /**
367          * Zoom map either in or out by one step
368          * @param inZoomIn true to zoom in, false for out
369          */
370         private void zoomMap(boolean inZoomIn)
371         {
372                 if (inZoomIn)
373                 {
374                         // Zoom in
375                         _zoomScale *= ZOOM_SCALE_FACTOR;
376                 }
377                 else
378                 {
379                         // Zoom out
380                         _zoomScale /= ZOOM_SCALE_FACTOR;
381                         if (_zoomScale < 0.5) _zoomScale = 0.5;
382                 }
383                 _numPoints = 0;
384                 dataUpdated();
385         }
386
387
388         /**
389          * Pan the map by the specified amounts
390          * @param inUp upwards pan
391          * @param inRight rightwards pan
392          */
393         private void panMap(int inUp, int inRight)
394         {
395                 double panFactor = _scale / _zoomScale;
396                 _offsetY = _offsetY + (inUp * panFactor);
397                 _offsetX = _offsetX - (inRight * panFactor);
398                 // Limit pan to sensible range??
399                 _numPoints = 0;
400                 dataUpdated();
401         }
402
403
404         /**
405          * React to click on map display
406          */
407         public void mouseClicked(MouseEvent e)
408         {
409                 this.requestFocus();
410                 if (_track != null)
411                 {
412                         int xClick = e.getX();
413                         int yClick = e.getY();
414                         // Check click is within main area (not in border)
415                         if (xClick > BORDER_WIDTH && yClick > BORDER_WIDTH && xClick < (getWidth() - BORDER_WIDTH)
416                                 && yClick < (getHeight() - BORDER_WIDTH))
417                         {
418                                 // Check left click or right click
419                                 if (e.isMetaDown())
420                                 {
421                                         // Only show popup if track has data
422                                         if (_track != null && _track.getNumPoints() > 0)
423                                                 _popup.show(this, e.getX(), e.getY());
424                                 }
425                                 else
426                                 {
427                                         // Find point within range of click point
428                                         double pointX = (xClick - getWidth()/2) * _scale / _zoomScale + _offsetX;
429                                         double pointY = (getHeight()/2 - yClick) * _scale / _zoomScale + _offsetY;
430                                         int selectedPointIndex = _track.getNearestPointIndex(
431                                                 pointX, pointY, CLICK_SENSITIVITY * _scale, false);
432                                         // Select the given point (or deselect if no point was found)
433                                         _trackInfo.getSelection().selectPoint(selectedPointIndex);
434                                 }
435                         }
436                 }
437         }
438
439
440         /**
441          * Respond to mouse released to reset dragging
442          * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
443          */
444         public void mouseReleased(MouseEvent e)
445         {
446                 _dragStartX = _dragStartY = -1;
447                 if (e.isMetaDown())
448                 {
449                         if (_zoomDragFromX >= 0 || _zoomDragFromY >= 0)
450                         {
451                                 // zoom area marked out - calculate offset and zoom
452                                 int xPan = (getWidth() - _zoomDragFromX - e.getX()) / 2;
453                                 int yPan = (getHeight() - _zoomDragFromY - e.getY()) / 2;
454                                 double xZoom = Math.abs(getWidth() * 1.0 / (e.getX() - _zoomDragFromX));
455                                 double yZoom = Math.abs(getHeight() * 1.0 / (e.getY() - _zoomDragFromY));
456                                 double extraZoom = (xZoom>yZoom?yZoom:xZoom);
457                                 // Pan first to ensure pan occurs with correct scale
458                                 panMap(yPan, xPan);
459                                 // Then zoom in and request repaint
460                                 _zoomScale = _zoomScale * extraZoom;
461                                 _image = null;
462                                 repaint();
463                         }
464                         _zoomDragFromX = _zoomDragFromY = -1;
465                         _zoomDragging = false;
466                 }
467         }
468
469
470         /**
471          * Respond to mouse wheel events to zoom the map
472          * @see java.awt.event.MouseWheelListener#mouseWheelMoved(java.awt.event.MouseWheelEvent)
473          */
474         public void mouseWheelMoved(MouseWheelEvent e)
475         {
476                 zoomMap(e.getWheelRotation() < 0);
477         }
478
479
480         /**
481          * @see java.awt.event.KeyListener#keyPressed(java.awt.event.KeyEvent)
482          */
483         public void keyPressed(KeyEvent e)
484         {
485                 int code = e.getKeyCode();
486                 // Check for meta key
487                 if (e.isControlDown())
488                 {
489                         // Check for arrow keys to zoom in and out
490                         if (code == KeyEvent.VK_UP)
491                                 zoomMap(true);
492                         else if (code == KeyEvent.VK_DOWN)
493                                 zoomMap(false);
494                         // Key nav for next/prev point
495                         else if (code == KeyEvent.VK_LEFT)
496                                 _trackInfo.getSelection().selectPreviousPoint();
497                         else if (code == KeyEvent.VK_RIGHT)
498                                 _trackInfo.getSelection().selectNextPoint();
499                 }
500                 else
501                 {
502                         // Check for arrow keys to pan
503                         int upwardsPan = 0;
504                         if (code == KeyEvent.VK_UP)
505                                 upwardsPan = PAN_DISTANCE;
506                         else if (code == KeyEvent.VK_DOWN)
507                                 upwardsPan = -PAN_DISTANCE;
508                         int rightwardsPan = 0;
509                         if (code == KeyEvent.VK_RIGHT)
510                                 rightwardsPan = -PAN_DISTANCE;
511                         else if (code == KeyEvent.VK_LEFT)
512                                 rightwardsPan = PAN_DISTANCE;
513                         panMap(upwardsPan, rightwardsPan);
514                         // Check for delete key to delete current point
515                         if (code == KeyEvent.VK_DELETE && _trackInfo.getSelection().getCurrentPointIndex() >= 0)
516                                 _app.deleteCurrentPoint();
517                 }
518         }
519
520
521         /**
522          * @see java.awt.event.KeyListener#keyReleased(java.awt.event.KeyEvent)
523          */
524         public void keyReleased(KeyEvent e)
525         {
526                 // ignore
527         }
528
529
530         /**
531          * @see java.awt.event.KeyListener#keyTyped(java.awt.event.KeyEvent)
532          */
533         public void keyTyped(KeyEvent e)
534         {
535                 // ignore
536         }
537
538
539         /**
540          * @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
541          */
542         public void mouseDragged(MouseEvent e)
543         {
544                 if (!e.isMetaDown())
545                 {
546                         if (_dragStartX > 0)
547                         {
548                                 int xShift = e.getX() - _dragStartX;
549                                 int yShift = e.getY() - _dragStartY;
550                                 panMap(yShift, xShift);
551                         }
552                         _dragStartX = e.getX();
553                         _dragStartY = e.getY();
554                 }
555                 else
556                 {
557                         // Right click-and-drag for zoom
558                         if (_zoomDragFromX < 0 || _zoomDragFromY < 0)
559                         {
560                                 _zoomDragFromX = e.getX();
561                                 _zoomDragFromY = e.getY();
562                         }
563                         else
564                         {
565                                 _zoomDragToX = e.getX();
566                                 _zoomDragToY = e.getY();
567                                 _zoomDragging = true;
568                         }
569                         repaint();
570                 }
571         }
572
573
574         /**
575          * @see java.awt.event.MouseMotionListener#mouseMoved(java.awt.event.MouseEvent)
576          */
577         public void mouseMoved(MouseEvent e)
578         {
579                 // ignore
580         }
581 }