2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
21 package jalview.viewmodel;
23 import jalview.datamodel.ColumnSelection;
24 import jalview.datamodel.HiddenSequences;
26 import java.awt.Graphics;
28 public class OverviewDimensions
30 // Default width and height values
31 private static final int DEFAULT_GRAPH_HEIGHT = 20;
33 private static final int MAX_WIDTH = 400;
35 private static final int MIN_WIDTH = 120;
37 private static final int MIN_SEQ_HEIGHT = 40;
39 private static final int MAX_SEQ_HEIGHT = 300;
41 // width of the overview panel
44 // height of sequences part of the overview panel
45 private int sequencesHeight;
47 // height of the graphs part of the overview panel
48 private int graphHeight = DEFAULT_GRAPH_HEIGHT;
50 // dimensions of box outlining current extent of view in alignment panel
51 // location of left side of box
52 private int boxX = -1;
54 // location of bottom of box
55 private int boxY = -1;
58 private int boxWidth = -1;
61 private int boxHeight = -1;
63 // scroll position in viewport corresponding to boxX
64 private int scrollCol = -1;
66 // scroll position in viewport corresponding to boxY
67 private int scrollRow = -1;
70 * Create an OverviewDimensions object
73 * positional properties of the viewport
74 * @param showAnnotationPanel
75 * true if the annotation panel is to be shown, false otherwise
77 public OverviewDimensions(ViewportPositionProps props,
78 boolean showAnnotationPanel)
80 // scale the initial size of overviewpanel to shape of alignment
81 float initialScale = (float) props.getAbsoluteAlignmentWidth()
82 / (float) props.getAbsoluteAlignmentHeight();
84 if (!showAnnotationPanel)
89 if (props.getAbsoluteAlignmentWidth() > props
90 .getAbsoluteAlignmentHeight())
94 sequencesHeight = (int) (MAX_WIDTH / initialScale);
95 if (sequencesHeight < MIN_SEQ_HEIGHT)
97 sequencesHeight = MIN_SEQ_HEIGHT;
103 width = (int) (MAX_WIDTH * initialScale);
104 sequencesHeight = MAX_SEQ_HEIGHT;
106 if (width < MIN_WIDTH)
114 * Check box dimensions and scroll positions and correct if necessary
117 * x position in overview panel
119 * y position in overview panel
125 * viewport position properties
127 public void updateViewportFromMouse(int mousex, int mousey,
128 HiddenSequences hiddenSeqs, ColumnSelection hiddenCols,
129 ViewportPositionProps props)
134 int alwidth = props.getAbsoluteAlignmentWidth();
135 int alheight = props.getAbsoluteAlignmentHeight();
148 // Convert x value to residue position
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
155 // convert x to residues - this is an absolute position
156 int xAsRes = Math.round((float) x * alwidth / width);
158 // get viewport width in residues
159 int vpwidth = props.getEndRes() - props.getStartRes() + 1;
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
164 // so convert back after getting visible region position
165 xAsRes = hiddenCols.adjustForHiddenColumns(hiddenCols
166 .findColumnPosition(xAsRes));
168 // get where end res should be by adding the viewport width on
169 // subtract 1 because the width includes endRes
170 int endRes = xAsRes + vpwidth - 1;
172 // check in case we went off the edge of the alignment
173 int visAlignWidth = hiddenCols.findColumnPosition(alwidth);
174 if (endRes > visAlignWidth - 1)
176 // went past the end of the alignment, adjust backwards
177 endRes = visAlignWidth - 1;
178 // recalc xAsRes backwards from endRes
179 // add 1 because width includes xAsRes
180 xAsRes = hiddenCols.adjustForHiddenColumns(endRes - vpwidth + 1);
184 // Convert y value to sequence position
187 // convert y to residues
188 int yAsSeq = Math.round((float) y * alheight / sequencesHeight);
190 // get viewport height in sequences
191 // add 1 because height includes both endSeq and startSeq
192 int vpheight = props.getEndSeq() - props.getStartSeq() + 1;
194 // get where y should be when accounting for hidden rows
195 // if y is in a hidden row region, shift up - but we still need absolute
197 // so convert back after getting visible region position
198 yAsSeq = hiddenSeqs.adjustForHiddenSeqs(hiddenSeqs
199 .findIndexWithoutHiddenSeqs(yAsSeq));
201 // get where end seq should be by adding the viewport height on
202 int endSeq = yAsSeq + vpheight - 1;
204 // check in case we went off the edge of the alignment
205 int visAlignHeight = hiddenSeqs.findIndexWithoutHiddenSeqs(alheight);
206 if (hiddenSeqs.findIndexWithoutHiddenSeqs(endSeq) > visAlignHeight - 1)
208 // went past the end of the alignment, adjust backwards
209 endSeq = visAlignHeight - 1;
210 // recalc yAsSeq backwards from endSeq
211 yAsSeq = endSeq - vpheight + 1;
214 // convert absolute positions back to visible alignment positions for
215 // viewport scrolling
216 scrollCol = hiddenCols.findColumnPosition(xAsRes);
217 scrollRow = hiddenSeqs.findIndexWithoutHiddenSeqs(yAsSeq);
221 * Update the overview panel box when the associated alignment panel is
229 * viewport position properties
231 public void setBoxPosition(HiddenSequences hiddenSeqs,
232 ColumnSelection hiddenCols, ViewportPositionProps props)
234 int alwidth = props.getAbsoluteAlignmentWidth();
235 int alheight = props.getAbsoluteAlignmentHeight();
237 // work with absolute values of startRes and endRes
238 int startRes = hiddenCols.adjustForHiddenColumns(props.getStartRes());
239 int endRes = hiddenCols.adjustForHiddenColumns(props.getEndRes());
241 // work with absolute values of startSeq and endSeq
242 int startSeq = hiddenSeqs.adjustForHiddenSeqs(props.getStartSeq());
243 int endSeq = hiddenSeqs.adjustForHiddenSeqs(props.getEndSeq());
245 // boxX, boxY is the x,y location equivalent to startRes, startSeq
246 boxX = Math.round((float) startRes * width / alwidth);
247 boxY = Math.round((float) startSeq * sequencesHeight / alheight);
249 // boxWidth is the width in residues translated to pixels
250 // since the box includes both the start and end residues, add 1 to the
253 .round((float) (endRes - startRes + 1) * width / alwidth);
254 // boxHeight is the height in sequences translated to pixels
255 // since the box includes both the start and end sequences, add 1 to the
257 boxHeight = Math.round((float) (endSeq - startSeq + 1)
263 * Draw the overview panel's viewport box on a graphics object
266 * the graphics object to draw on
268 public void drawBox(Graphics g)
270 g.drawRect(boxX, boxY, boxWidth, boxHeight);
271 g.drawRect(boxX + 1, boxY + 1, boxWidth - 2, boxHeight - 2);
274 // don't like this, scroll vals are separate from setting code
275 public int getScrollCol()
280 public int getScrollRow()
285 // TODO should be removed, when unit test has mock Graphics object available
286 // to check boxX/boxY
292 // TODO should be removed, when unit test has mock Graphics object available
293 // to check boxX/boxY
299 // TODO should be removed, when unit test has mock Graphics object available
300 public int getBoxWidth()
305 // TODO should be removed, when unit test has mock Graphics object available
306 public int getBoxHeight()
311 public void setWidth(int w)
316 public void setHeight(int h)
318 sequencesHeight = h - graphHeight;
321 public int getWidth()
326 public int getHeight()
328 return sequencesHeight + graphHeight;
331 public int getSequencesHeight()
333 return sequencesHeight;
336 public int getGraphHeight()