JAL-2491 Renamed comp scrolling flag for consistency; small tweaks
[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 ranges
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(ViewportRanges ranges,
78           boolean showAnnotationPanel)
79   {
80     // scale the initial size of overviewpanel to shape of alignment
81     float initialScale = (float) ranges.getAbsoluteAlignmentWidth()
82             / (float) ranges.getAbsoluteAlignmentHeight();
83
84     if (!showAnnotationPanel)
85     {
86       graphHeight = 0;
87     }
88
89     if (ranges.getAbsoluteAlignmentWidth() > ranges
90             .getAbsoluteAlignmentHeight())
91     {
92       // wider
93       width = MAX_WIDTH;
94       sequencesHeight = Math.round(MAX_WIDTH / initialScale);
95       if (sequencesHeight < MIN_SEQ_HEIGHT)
96       {
97         sequencesHeight = MIN_SEQ_HEIGHT;
98       }
99     }
100     else
101     {
102       // taller
103       width = Math.round(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 ranges
125    *          viewport position properties
126    */
127   public void updateViewportFromMouse(int mousex, int mousey,
128           HiddenSequences hiddenSeqs, ColumnSelection hiddenCols,
129           ViewportRanges ranges)
130   {
131     int x = mousex;
132     int y = mousey;
133
134     int alwidth = ranges.getAbsoluteAlignmentWidth();
135     int alheight = ranges.getAbsoluteAlignmentHeight();
136
137     if (x < 0)
138     {
139       x = 0;
140     }
141
142     if (y < 0)
143     {
144       y = 0;
145     }
146
147     //
148     // Convert x value to residue position
149     //
150
151     // need to determine where scrollCol should be, given x
152     // to do this also need to know width of viewport, and some hidden column
153     // correction
154
155     // convert x to residues - this is an absolute position
156     int xAsRes = Math.round((float) x * alwidth / width);
157
158     // get viewport width in residues
159     int vpwidth = ranges.getViewportWidth();
160
161     // get where x should be when accounting for hidden cols
162     // if x is in a hidden col region, shift to left - but we still need
163     // absolute position
164     // so convert back after getting visible region position
165     int visXAsRes = hiddenCols.findColumnPosition(xAsRes);
166
167     // check in case we went off the edge of the alignment
168     int visAlignWidth = hiddenCols.findColumnPosition(alwidth - 1);
169     if (visXAsRes + vpwidth - 1 > visAlignWidth)
170     {
171       // went past the end of the alignment, adjust backwards
172
173       // if last position was before the end of the alignment, need to update
174       if ((scrollCol + vpwidth - 1) < visAlignWidth)
175       {
176         visXAsRes = hiddenCols.findColumnPosition(hiddenCols
177                 .subtractVisibleColumns(vpwidth - 1, alwidth - 1));
178       }
179       else
180       {
181         visXAsRes = scrollCol;
182       }
183     }
184
185     //
186     // Convert y value to sequence position
187     //
188
189     // convert y to residues
190     int yAsSeq = Math.round((float) y * alheight / sequencesHeight);
191
192     // get viewport height in sequences
193     int vpheight = ranges.getViewportHeight();
194
195     // get where y should be when accounting for hidden rows
196     // if y is in a hidden row region, shift up - but we still need absolute
197     // position,
198     // so convert back after getting visible region position
199     yAsSeq = hiddenSeqs.adjustForHiddenSeqs(hiddenSeqs
200             .findIndexWithoutHiddenSeqs(yAsSeq));
201
202     // check in case we went off the edge of the alignment
203     int visAlignHeight = hiddenSeqs.findIndexWithoutHiddenSeqs(alheight);
204     int visYAsRes = hiddenSeqs.findIndexWithoutHiddenSeqs(yAsSeq);
205     if (visYAsRes + vpheight - 1 > visAlignHeight)
206     {
207       // went past the end of the alignment, adjust backwards
208       if ((scrollRow + vpheight - 1) < visAlignHeight)
209       {
210         visYAsRes = hiddenSeqs.findIndexWithoutHiddenSeqs(hiddenSeqs
211                 .subtractVisibleRows(vpheight - 1, alheight - 1));
212       }
213       else
214       {
215         visYAsRes = scrollRow;
216       }
217     }
218
219     // update scroll values
220     // scrollCol = visXAsRes;
221     // scrollRow = visYAsRes;
222     ranges.setStartRes(visXAsRes);
223
224     ranges.setStartSeq(visYAsRes);
225   }
226
227   /**
228    * Update the overview panel box when the associated alignment panel is
229    * changed
230    * 
231    * @param hiddenSeqs
232    *          hidden sequences
233    * @param hiddenCols
234    *          hidden columns
235    * @param ranges
236    *          viewport position properties
237    */
238   public void setBoxPosition(HiddenSequences hiddenSeqs,
239           ColumnSelection hiddenCols, ViewportRanges ranges)
240   {
241     int alwidth = ranges.getAbsoluteAlignmentWidth();
242     int alheight = ranges.getAbsoluteAlignmentHeight();
243
244     // work with absolute values of startRes and endRes
245     int startRes = hiddenCols.adjustForHiddenColumns(ranges.getStartRes());
246     int endRes = hiddenCols.adjustForHiddenColumns(ranges.getEndRes());
247
248     // work with absolute values of startSeq and endSeq
249     int startSeq = hiddenSeqs.adjustForHiddenSeqs(ranges.getStartSeq());
250     int endSeq = hiddenSeqs.adjustForHiddenSeqs(ranges.getEndSeq());
251
252     // boxX, boxY is the x,y location equivalent to startRes, startSeq
253     boxX = Math.round((float) startRes * width / alwidth);
254     boxY = Math.round((float) startSeq * sequencesHeight / alheight);
255
256     // boxWidth is the width in residues translated to pixels
257     // since the box includes both the start and end residues, add 1 to the
258     // difference
259     boxWidth = Math
260             .round((float) (endRes - startRes + 1) * width / alwidth);
261     // boxHeight is the height in sequences translated to pixels
262     boxHeight = Math.round((float) (endSeq - startSeq + 1)
263             * sequencesHeight
264             / alheight);
265   }
266
267   /**
268    * Draw the overview panel's viewport box on a graphics object
269    * 
270    * @param g
271    *          the graphics object to draw on
272    */
273   public void drawBox(Graphics g)
274   {
275     g.drawRect(boxX, boxY, boxWidth, boxHeight);
276     g.drawRect(boxX + 1, boxY + 1, boxWidth - 2, boxHeight - 2);
277   }
278
279   /*public int getScrollCol()
280   {
281     return scrollCol;
282   }
283
284   public int getScrollRow()
285   {
286     return scrollRow;
287   }*/
288
289   // TODO should be removed, when unit test has mock Graphics object available
290   // to check boxX/boxY
291   public int getBoxX()
292   {
293     return boxX;
294   }
295
296   // TODO should be removed, when unit test has mock Graphics object available
297   // to check boxX/boxY
298   public int getBoxY()
299   {
300     return boxY;
301   }
302
303   // TODO should be removed, when unit test has mock Graphics object available
304   public int getBoxWidth()
305   {
306     return boxWidth;
307   }
308
309   // TODO should be removed, when unit test has mock Graphics object available
310   public int getBoxHeight()
311   {
312     return boxHeight;
313   }
314
315   public void setWidth(int w)
316   {
317     width = w;
318   }
319
320   public void setHeight(int h)
321   {
322     sequencesHeight = h - graphHeight;
323   }
324
325   public int getWidth()
326   {
327     return width;
328   }
329
330   public int getHeight()
331   {
332     return sequencesHeight + graphHeight;
333   }
334
335   public int getSequencesHeight()
336   {
337     return sequencesHeight;
338   }
339
340   public int getGraphHeight()
341   {
342     return graphHeight;
343   }
344 }