JAL-2388 Fixed inherited&new out-by-one errors; further unit tests
[jalview.git] / src / jalview / viewmodel / OverviewDimensions.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.viewmodel;
22
23 import jalview.datamodel.ColumnSelection;
24 import jalview.datamodel.HiddenSequences;
25
26 import java.awt.Graphics;
27
28 public class OverviewDimensions
29 {
30   // Default width and height values
31   private static final int DEFAULT_GRAPH_HEIGHT = 20;
32
33   private static final int MAX_WIDTH = 400;
34
35   private static final int MIN_WIDTH = 120;
36
37   private static final int MIN_SEQ_HEIGHT = 40;
38
39   private static final int MAX_SEQ_HEIGHT = 300;
40
41   // width of the overview panel
42   private int width;
43
44   // height of sequences part of the overview panel
45   private int sequencesHeight;
46
47   // height of the graphs part of the overview panel
48   private int graphHeight = DEFAULT_GRAPH_HEIGHT;
49
50   // dimensions of box outlining current extent of view in alignment panel
51   // location of left side of box
52   private int boxX = -1;
53
54   // location of bottom of box
55   private int boxY = -1;
56
57   // width of box
58   private int boxWidth = -1;
59
60   // height of box
61   private int boxHeight = -1;
62
63   // scroll position in viewport corresponding to boxX
64   private int scrollCol = -1;
65
66   // scroll position in viewport corresponding to boxY
67   private int scrollRow = -1;
68
69   /**
70    * Create an OverviewDimensions object
71    * 
72    * @param props
73    *          positional properties of the viewport
74    * @param showAnnotationPanel
75    *          true if the annotation panel is to be shown, false otherwise
76    */
77   public OverviewDimensions(ViewportPositionProps props,
78           boolean showAnnotationPanel)
79   {
80     // scale the initial size of overviewpanel to shape of alignment
81     float initialScale = (float) props.getAbsoluteAlignmentWidth()
82             / (float) props.getAbsoluteAlignmentHeight();
83
84     if (!showAnnotationPanel)
85     {
86       graphHeight = 0;
87     }
88
89     if (props.getAbsoluteAlignmentWidth() > props
90             .getAbsoluteAlignmentHeight())
91     {
92       // wider
93       width = MAX_WIDTH;
94       sequencesHeight = (int) (MAX_WIDTH / initialScale);
95       if (sequencesHeight < MIN_SEQ_HEIGHT)
96       {
97         sequencesHeight = MIN_SEQ_HEIGHT;
98       }
99     }
100     else
101     {
102       // taller
103       width = (int) (MAX_WIDTH * initialScale);
104       sequencesHeight = MAX_SEQ_HEIGHT;
105
106       if (width < MIN_WIDTH)
107       {
108         width = MIN_WIDTH;
109       }
110     }
111   }
112
113   /**
114    * Check box dimensions and scroll positions and correct if necessary
115    * 
116    * @param mousex
117    *          x position in overview panel
118    * @param mousey
119    *          y position in overview panel
120    * @param hiddenSeqs
121    *          hidden sequences
122    * @param hiddenCols
123    *          hidden columns
124    * @param props
125    *          viewport position properties
126    */
127   public void updateViewportFromMouse(int mousex, int mousey,
128           HiddenSequences hiddenSeqs, ColumnSelection hiddenCols,
129           ViewportPositionProps props)
130   {
131     int x = mousex;
132     int y = mousey;
133
134     int alwidth = props.getAbsoluteAlignmentWidth();
135     int alheight = props.getAbsoluteAlignmentHeight();
136
137     if (x < 0)
138     {
139       x = 0;
140     }
141     else if (x >= width)
142     {
143       x = alwidth - 1;
144     }
145
146     if (y < 0)
147     {
148       y = 0;
149     }
150     else if (y >= sequencesHeight)
151     {
152       y = alheight - 1;
153     }
154
155     //
156     // Convert x value to residue position
157     //
158
159     // need to determine where scrollCol should be, given x
160     // to do this also need to know width of viewport, and some hidden column
161     // correction
162
163     // convert x to residues - this is an absolute position
164     int xAsRes = Math.round((float) x * alwidth / width);
165
166     // get viewport width in residues
167     int vpwidth = props.getEndRes() - props.getStartRes() + 1;
168
169     // get where x should be when accounting for hidden cols
170     // if x is in a hidden col region, shift to left - but we still need
171     // absolute position
172     // so convert back after getting visible region position
173     xAsRes = hiddenCols.adjustForHiddenColumns(hiddenCols
174             .findColumnPosition(xAsRes));
175
176     // get where end res should be by adding the viewport width on
177     // subtract 1 because the width includes endRes
178     int endRes = xAsRes + vpwidth - 1;
179
180     // check in case we went off the edge of the alignment
181     if (endRes > alwidth)
182     {
183       // went past the end of the alignment, adjust backwards
184       endRes = alwidth;
185       // recalc xAsRes backwards from endRes
186       // add 1 because width includes xAsRes
187       xAsRes = endRes - vpwidth + 1;
188     }
189
190     //
191     // Convert y value to sequence position
192     //
193
194     // convert y to residues
195     int yAsSeq = Math.round((float) y * alheight / sequencesHeight);
196
197     // get viewport height in sequences
198     // add 1 because height includes both endSeq and startSeq
199     int vpheight = props.getEndSeq() - props.getStartSeq() + 1;
200
201     // get where y should be when accounting for hidden rows
202     // if y is in a hidden row region, shift up - but we still need absolute
203     // position,
204     // so convert back after getting visible region position
205     yAsSeq = hiddenSeqs.adjustForHiddenSeqs(hiddenSeqs
206             .findIndexWithoutHiddenSeqs(yAsSeq));
207
208     // get where end seq should be by adding the viewport height on
209     int endSeq = yAsSeq + vpheight - 1;
210
211     // check in case we went off the edge of the alignment
212     if (endSeq > alheight)
213     {
214       // went past the end of the alignment, adjust backwards
215       endSeq = alheight;
216       // recalc yAsSeq backwards from endSeq
217       yAsSeq = endSeq - vpheight + 1;
218     }
219
220     // convert absolute positions back to visible alignment positions for
221     // viewport scrolling
222     scrollCol = hiddenCols.findColumnPosition(xAsRes);
223     scrollRow = hiddenSeqs.findIndexWithoutHiddenSeqs(yAsSeq);
224   }
225
226   /**
227    * Update the overview panel box when the associated alignment panel is
228    * changed
229    * 
230    * @param hiddenSeqs
231    *          hidden sequences
232    * @param hiddenCols
233    *          hidden columns
234    * @param props
235    *          viewport position properties
236    */
237   public void setBoxPosition(HiddenSequences hiddenSeqs,
238           ColumnSelection hiddenCols, ViewportPositionProps props)
239   {
240     int alwidth = props.getAbsoluteAlignmentWidth();
241     int alheight = props.getAbsoluteAlignmentHeight();
242
243     // work with absolute values of startRes and endRes
244     int startRes = hiddenCols.adjustForHiddenColumns(props.getStartRes());
245     int endRes = hiddenCols.adjustForHiddenColumns(props.getEndRes());
246
247     // work with absolute values of startSeq and endSeq
248     int startSeq = hiddenSeqs.adjustForHiddenSeqs(props.getStartSeq());
249     int endSeq = hiddenSeqs.adjustForHiddenSeqs(props.getEndSeq());
250
251     // boxX, boxY is the x,y location equivalent to startRes, startSeq
252     boxX = Math.round((float) startRes * width / alwidth);
253     boxY = Math.round((float) startSeq * sequencesHeight / alheight);
254
255     // boxWidth is the width in residues translated to pixels
256     // since the box includes both the start and end residues, add 1 to the
257     // difference
258     boxWidth = Math
259             .round((float) (endRes - startRes + 1) * width / alwidth);
260     // boxHeight is the height in sequences translated to pixels
261     // since the box includes both the start and end sequences, add 1 to the
262     // difference
263     boxHeight = Math.round((float) (endSeq - startSeq + 1)
264             * sequencesHeight
265             / alheight);
266   }
267
268   /**
269    * Draw the overview panel's viewport box on a graphics object
270    * 
271    * @param g
272    *          the graphics object to draw on
273    */
274   public void drawBox(Graphics g)
275   {
276     g.drawRect(boxX, boxY, boxWidth, boxHeight);
277     g.drawRect(boxX + 1, boxY + 1, boxWidth - 2, boxHeight - 2);
278   }
279
280   // don't like this, scroll vals are separate from setting code
281   public int getScrollCol()
282   {
283     return scrollCol;
284   }
285
286   public int getScrollRow()
287   {
288     return scrollRow;
289   }
290
291   // TODO should be removed, when unit test has mock Graphics object available
292   // to check boxX/boxY
293   public int getBoxX()
294   {
295     return boxX;
296   }
297
298   // TODO should be removed, when unit test has mock Graphics object available
299   // to check boxX/boxY
300   public int getBoxY()
301   {
302     return boxY;
303   }
304
305   // TODO should be removed, when unit test has mock Graphics object available
306   public int getBoxWidth()
307   {
308     return boxWidth;
309   }
310
311   // TODO should be removed, when unit test has mock Graphics object available
312   public int getBoxHeight()
313   {
314     return boxHeight;
315   }
316
317   public void setWidth(int w)
318   {
319     width = w;
320   }
321
322   public void setHeight(int h)
323   {
324     sequencesHeight = h - graphHeight;
325   }
326
327   public int getWidth()
328   {
329     return width;
330   }
331
332   public int getHeight()
333   {
334     return sequencesHeight + graphHeight;
335   }
336
337   public int getSequencesHeight()
338   {
339     return sequencesHeight;
340   }
341
342   public int getGraphHeight()
343   {
344     return graphHeight;
345   }
346 }