JAL-2611 unit tests
[jalview.git] / src / jalview / viewmodel / OverviewDimensionsShowHidden.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.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;
30
31 public class OverviewDimensionsShowHidden extends OverviewDimensions
32 {
33   private ViewportRanges ranges;
34
35   private int xdiff; // when dragging, difference in alignment units between
36                      // start residue and original mouse click position
37
38   private int ydiff; // when dragging, difference in alignment units between
39                      // start sequence and original mouse click position
40
41   /**
42    * Create an OverviewDimensions object
43    * 
44    * @param ranges
45    *          positional properties of the viewport
46    * @param showAnnotationPanel
47    *          true if the annotation panel is to be shown, false otherwise
48    */
49   public OverviewDimensionsShowHidden(ViewportRanges vpranges,
50           boolean showAnnotationPanel)
51   {
52     super(vpranges, showAnnotationPanel);
53     ranges = vpranges;
54     resetAlignmentDims();
55   }
56
57   /**
58    * Check box dimensions and scroll positions and correct if necessary
59    * 
60    * @param mousex
61    *          x position in overview panel
62    * @param mousey
63    *          y position in overview panel
64    * @param hiddenSeqs
65    *          hidden sequences
66    * @param hiddenCols
67    *          hidden columns
68    * @param ranges
69    *          viewport position properties
70    */
71   @Override
72   public void updateViewportFromMouse(int mousex, int mousey,
73           HiddenSequences hiddenSeqs, HiddenColumns hiddenCols)
74   {
75     // convert mousex and mousey to alignment units as well as
76     // translating to top left corner of viewport - this is an absolute position
77     int xAsRes = getLeftXFromCentreX(mousex, hiddenCols);
78     int yAsSeq = getTopYFromCentreY(mousey, hiddenSeqs);
79
80     // convert to visible positions
81     int visXAsRes = hiddenCols.findColumnPosition(xAsRes);
82     yAsSeq = hiddenSeqs.adjustForHiddenSeqs(
83             hiddenSeqs.findIndexWithoutHiddenSeqs(yAsSeq));
84     int visYAsSeq = hiddenSeqs.findIndexWithoutHiddenSeqs(yAsSeq);
85
86     // update viewport accordingly
87     updateViewportFromTopLeft(visXAsRes, visYAsSeq, hiddenSeqs, hiddenCols);
88   }
89
90   @Override
91   public void adjustViewportFromMouse(int mousex, int mousey,
92           HiddenSequences hiddenSeqs, HiddenColumns hiddenCols)
93   {
94     // calculate translation in pixel terms:
95     // get mouse location in viewport coords, add translation in viewport
96     // coords,
97     // convert back to pixel coords
98     int vpx = Math.round((float) mousex * alwidth / width);
99     int visXAsRes = hiddenCols.findColumnPosition(vpx) + xdiff;
100
101     int vpy = Math.round((float) mousey * alheight / sequencesHeight);
102     int visYAsRes = hiddenSeqs.findIndexWithoutHiddenSeqs(vpy) + ydiff;
103
104     // update viewport accordingly
105     updateViewportFromTopLeft(visXAsRes, visYAsRes,
106             hiddenSeqs,
107             hiddenCols);
108   }
109
110   @Override
111   protected void updateViewportFromTopLeft(int leftx, int topy,
112           HiddenSequences hiddenSeqs, HiddenColumns hiddenCols)
113   {
114     int visXAsRes = leftx;
115     int visYAsSeq = topy;
116     resetAlignmentDims();
117
118     if (visXAsRes < 0)
119     {
120       visXAsRes = 0;
121     }
122
123     if (visYAsSeq < 0)
124     {
125       visYAsSeq = 0;
126     }
127
128     // Determine where scrollCol should be, given visXAsRes
129
130     // get viewport width in residues
131     int vpwidth = ranges.getViewportWidth();
132
133     // check in case we went off the edge of the alignment
134     int visAlignWidth = hiddenCols.findColumnPosition(alwidth - 1);
135     if (visXAsRes + vpwidth - 1 > visAlignWidth)
136     {
137       // went past the end of the alignment, adjust backwards
138
139       // if last position was before the end of the alignment, need to update
140       if (ranges.getEndRes() < visAlignWidth)
141       {
142         visXAsRes = hiddenCols.findColumnPosition(hiddenCols
143                 .subtractVisibleColumns(vpwidth - 1, alwidth - 1));
144       }
145       else
146       {
147         visXAsRes = ranges.getStartRes();
148       }
149     }
150
151     // Determine where scrollRow should be, given visYAsSeq
152
153     // get viewport height in sequences
154     int vpheight = ranges.getViewportHeight();
155
156     // check in case we went off the edge of the alignment
157     int visAlignHeight = hiddenSeqs.findIndexWithoutHiddenSeqs(alheight);
158
159     if (visYAsSeq + vpheight - 1 > visAlignHeight)
160     {
161       // went past the end of the alignment, adjust backwards
162       if (ranges.getEndSeq() < visAlignHeight)
163       {
164         visYAsSeq = hiddenSeqs.findIndexWithoutHiddenSeqs(hiddenSeqs
165                 .subtractVisibleRows(vpheight - 1, alheight - 1));
166       }
167       else
168       {
169         visYAsSeq = ranges.getStartSeq();
170       }
171     }
172
173     // update viewport
174     ranges.setStartRes(visXAsRes);
175     ranges.setStartSeq(visYAsSeq);
176   }
177
178   /**
179    * Update the overview panel box when the associated alignment panel is
180    * changed
181    * 
182    * @param hiddenSeqs
183    *          hidden sequences
184    * @param hiddenCols
185    *          hidden columns
186    * @param ranges
187    *          viewport position properties
188    */
189   @Override
190   public void setBoxPosition(HiddenSequences hiddenSeqs,
191           HiddenColumns hiddenCols)
192   {
193     // work with absolute values of startRes and endRes
194     int startRes = hiddenCols.adjustForHiddenColumns(ranges.getStartRes());
195     int endRes = hiddenCols.adjustForHiddenColumns(ranges.getEndRes());
196
197     // work with absolute values of startSeq and endSeq
198     int startSeq = hiddenSeqs.adjustForHiddenSeqs(ranges.getStartSeq());
199     int endSeq = hiddenSeqs.adjustForHiddenSeqs(ranges.getEndSeq());
200
201     setBoxPosition(startRes, startSeq, endRes - startRes + 1, endSeq
202             - startSeq + 1);
203   }
204
205   @Override
206   public AlignmentColsCollectionI getColumns(AlignmentI al)
207   {
208     return new AllColsCollection(0,
209             ranges.getAbsoluteAlignmentWidth() - 1, al);
210   }
211
212   @Override
213   public AlignmentRowsCollectionI getRows(AlignmentI al)
214   {
215     return new AllRowsCollection(0,
216             ranges.getAbsoluteAlignmentHeight() - 1,
217             al);
218   }
219
220   @Override
221   protected void resetAlignmentDims()
222   {
223     alwidth = ranges.getAbsoluteAlignmentWidth();
224     alheight = ranges.getAbsoluteAlignmentHeight();
225   }
226
227   @Override
228   protected int getLeftXFromCentreX(int mousex, HiddenColumns hidden)
229   {
230     int vpx = Math.round((float) mousex * alwidth / width);
231     return hidden.subtractVisibleColumns(ranges.getViewportWidth() / 2,
232             vpx);
233   }
234
235   @Override
236   protected int getTopYFromCentreY(int mousey, HiddenSequences hidden)
237   {
238     int vpy = Math.round((float) mousey * alheight / sequencesHeight);
239     return hidden.subtractVisibleRows(ranges.getViewportHeight() / 2, vpy);
240   }
241
242   @Override
243   public void setDragPoint(int x, int y, HiddenSequences hiddenSeqs,
244           HiddenColumns hiddenCols)
245   {
246     // get alignment position of x and box (can get directly from vpranges) and
247     // calculate difference between the positions
248     int vpx = Math.round((float) x * alwidth / width);
249     int vpy = Math.round((float) y * alheight / sequencesHeight);
250
251     xdiff = ranges.getStartRes() - hiddenCols.findColumnPosition(vpx);
252     ydiff = ranges.getStartSeq()
253             - hiddenSeqs.findIndexWithoutHiddenSeqs(vpy);
254   }
255
256 }