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.api.AlignmentColsCollectionI;
24 import jalview.api.AlignmentRowsCollectionI;
25 import jalview.datamodel.AlignmentI;
26 import jalview.datamodel.AllColsCollection;
27 import jalview.datamodel.AllRowsCollection;
28 import jalview.datamodel.HiddenColumns;
29 import jalview.datamodel.HiddenSequences;
31 public class OverviewDimensionsShowHidden extends OverviewDimensions
33 private ViewportRanges ranges;
35 private int xdiff; // when dragging, difference in alignment units between
36 // start residue and original mouse click position
38 private int ydiff; // when dragging, difference in alignment units between
39 // start sequence and original mouse click position
42 * Create an OverviewDimensions object
45 * positional properties of the viewport
46 * @param showAnnotationPanel
47 * true if the annotation panel is to be shown, false otherwise
49 public OverviewDimensionsShowHidden(ViewportRanges vpranges,
50 boolean showAnnotationPanel)
52 super(vpranges, showAnnotationPanel);
58 * Check box dimensions and scroll positions and correct if necessary
61 * x position in overview panel
63 * y position in overview panel
69 * viewport position properties
72 public void updateViewportFromMouse(int mousex, int mousey,
73 HiddenSequences hiddenSeqs, HiddenColumns hiddenCols)
77 // convert mousex and mousey to alignment units as well as
78 // translating to top left corner of viewport - this is an absolute position
79 int xAsRes = getLeftXFromCentreX(mousex, hiddenCols);
80 int yAsSeq = getTopYFromCentreY(mousey, hiddenSeqs);
82 // convert to visible positions
83 int visXAsRes = hiddenCols.absoluteToVisibleColumn(xAsRes);
84 yAsSeq = hiddenSeqs.adjustForHiddenSeqs(
85 hiddenSeqs.findIndexWithoutHiddenSeqs(yAsSeq));
86 yAsSeq = Math.max(yAsSeq, 0); // -1 if before first visible sequence
87 int visYAsSeq = hiddenSeqs.findIndexWithoutHiddenSeqs(yAsSeq);
88 visYAsSeq = Math.max(visYAsSeq, 0); // -1 if before first visible sequence
90 // update viewport accordingly
91 updateViewportFromTopLeft(visXAsRes, visYAsSeq, hiddenSeqs, hiddenCols);
95 public void adjustViewportFromMouse(int mousex, int mousey,
96 HiddenSequences hiddenSeqs, HiddenColumns hiddenCols)
100 // calculate translation in pixel terms:
101 // get mouse location in viewport coords, add translation in viewport
103 // convert back to pixel coords
104 int vpx = Math.round((float) mousex * alwidth / width);
105 int visXAsRes = hiddenCols.absoluteToVisibleColumn(vpx) + xdiff;
107 int vpy = Math.round(mousey * heightRatio);
108 int visYAsRes = hiddenSeqs.findIndexWithoutHiddenSeqs(vpy) + ydiff;
110 // update viewport accordingly
111 updateViewportFromTopLeft(visXAsRes, visYAsRes, hiddenSeqs, hiddenCols);
115 * {@inheritDoc} Callers should have already called resetAlignmentDims to
116 * refresh alwidth, alheight and width/height ratios
119 protected void updateViewportFromTopLeft(int leftx, int topy,
120 HiddenSequences hiddenSeqs, HiddenColumns hiddenCols)
122 int visXAsRes = leftx;
123 int visYAsSeq = topy;
135 if (ranges.isWrappedMode())
137 visYAsSeq = 0; // sorry, no vertical scroll when wrapped
140 // Determine where scrollCol should be, given visXAsRes
142 // get viewport width in residues
143 int vpwidth = ranges.getViewportWidth();
145 // check in case we went off the edge of the alignment
146 int visAlignWidth = hiddenCols.absoluteToVisibleColumn(alwidth - 1);
147 if (visXAsRes + vpwidth - 1 > visAlignWidth)
149 // went past the end of the alignment, adjust backwards
151 // if last position was before the end of the alignment, need to update
152 if (ranges.getEndRes() < visAlignWidth)
154 visXAsRes = hiddenCols.absoluteToVisibleColumn(hiddenCols
155 .offsetByVisibleColumns(-(vpwidth - 1), alwidth - 1));
159 visXAsRes = ranges.getStartRes();
163 // Determine where scrollRow should be, given visYAsSeq
165 // get viewport height in sequences
166 int vpheight = ranges.getViewportHeight();
168 // check in case we went off the edge of the alignment
169 int visAlignHeight = hiddenSeqs.findIndexWithoutHiddenSeqs(alheight);
171 if (visYAsSeq + vpheight - 1 > visAlignHeight)
173 // went past the end of the alignment, adjust backwards
174 if (ranges.getEndSeq() < visAlignHeight)
176 visYAsSeq = hiddenSeqs.findIndexWithoutHiddenSeqs(
177 hiddenSeqs.subtractVisibleRows(vpheight - 1, alheight - 1));
181 visYAsSeq = ranges.getStartSeq();
186 ranges.setStartResAndSeq(visXAsRes, visYAsSeq);
190 * Update the overview panel box when the associated alignment panel is
198 * viewport position properties
201 public void setBoxPosition(HiddenSequences hiddenSeqs,
202 HiddenColumns hiddenCols)
204 // work with absolute values of startRes and endRes
205 int startRes = hiddenCols.visibleToAbsoluteColumn(ranges.getStartRes());
206 int endRes = hiddenCols.visibleToAbsoluteColumn(ranges.getEndRes());
208 // work with absolute values of startSeq and endSeq
209 int startSeq = hiddenSeqs.adjustForHiddenSeqs(ranges.getStartSeq());
210 int endSeq = hiddenSeqs.adjustForHiddenSeqs(ranges.getEndSeq());
212 setBoxPosition(startRes, startSeq, endRes - startRes + 1,
213 endSeq - startSeq + 1);
217 public AlignmentColsCollectionI getColumns(AlignmentI al)
219 return new AllColsCollection(0, ranges.getAbsoluteAlignmentWidth() - 1,
224 public AlignmentRowsCollectionI getRows(AlignmentI al)
226 return new AllRowsCollection(0, ranges.getAbsoluteAlignmentHeight() - 1,
231 protected void resetAlignmentDims()
233 alwidth = ranges.getAbsoluteAlignmentWidth();
234 alheight = ranges.getAbsoluteAlignmentHeight();
236 widthRatio = (float) alwidth / width;
237 heightRatio = (float) alheight / sequencesHeight;
241 * {@inheritDoc} Callers should have already called resetAlignmentDims to
245 protected int getLeftXFromCentreX(int mousex, HiddenColumns hidden)
247 int vpx = Math.round((float) mousex * alwidth / width);
248 return hidden.offsetByVisibleColumns(-ranges.getViewportWidth() / 2,
253 * {@inheritDoc} Callers should have already called resetAlignmentDims to
254 * refresh heightRatio
257 protected int getTopYFromCentreY(int mousey, HiddenSequences hidden)
259 int vpy = Math.round(mousey * heightRatio);
260 return hidden.subtractVisibleRows(ranges.getViewportHeight() / 2, vpy);
264 public void setDragPoint(int x, int y, HiddenSequences hiddenSeqs,
265 HiddenColumns hiddenCols)
267 resetAlignmentDims();
269 // get alignment position of x and box (can get directly from vpranges) and
270 // calculate difference between the positions
271 int vpx = Math.round(x * widthRatio);
272 int vpy = Math.round(y * heightRatio);
274 xdiff = ranges.getStartRes() - hiddenCols.absoluteToVisibleColumn(vpx);
275 ydiff = ranges.getStartSeq()
276 - hiddenSeqs.findIndexWithoutHiddenSeqs(vpy);