Merge remote-tracking branch
[jalview.git] / src / jalview / viewmodel / OverviewDimensionsWithHidden.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 OverviewDimensionsWithHidden extends OverviewDimensions
32 {
33   /**
34    * Create an OverviewDimensions object
35    * 
36    * @param ranges
37    *          positional properties of the viewport
38    * @param showAnnotationPanel
39    *          true if the annotation panel is to be shown, false otherwise
40    */
41   public OverviewDimensionsWithHidden(ViewportRanges ranges,
42           boolean showAnnotationPanel)
43   {
44     super(ranges, showAnnotationPanel);
45
46     alwidth = ranges.getAbsoluteAlignmentWidth();
47     alheight = ranges.getAbsoluteAlignmentHeight();
48   }
49
50   /**
51    * Check box dimensions and scroll positions and correct if necessary
52    * 
53    * @param mousex
54    *          x position in overview panel
55    * @param mousey
56    *          y position in overview panel
57    * @param hiddenSeqs
58    *          hidden sequences
59    * @param hiddenCols
60    *          hidden columns
61    * @param ranges
62    *          viewport position properties
63    */
64   @Override
65   public void updateViewportFromMouse(int mousex, int mousey,
66           HiddenSequences hiddenSeqs, HiddenColumns hiddenCols,
67           ViewportRanges ranges)
68   {
69     int x = mousex;
70     int y = mousey;
71
72     alwidth = ranges.getAbsoluteAlignmentWidth();
73     alheight = ranges.getAbsoluteAlignmentHeight();
74
75     if (x < 0)
76     {
77       x = 0;
78     }
79
80     if (y < 0)
81     {
82       y = 0;
83     }
84
85     //
86     // Convert x value to residue position
87     //
88
89     // need to determine where scrollCol should be, given x
90     // to do this also need to know width of viewport, and some hidden column
91     // correction
92
93     // convert x to residues - this is an absolute position
94     int xAsRes = Math.round((float) x * alwidth / width);
95
96     // get viewport width in residues
97     int vpwidth = ranges.getEndRes() - ranges.getStartRes() + 1;
98
99     // get where x should be when accounting for hidden cols
100     // if x is in a hidden col region, shift to left - but we still need
101     // absolute position
102     // so convert back after getting visible region position
103     int visXAsRes = hiddenCols.findColumnPosition(xAsRes);
104
105     // check in case we went off the edge of the alignment
106     int visAlignWidth = hiddenCols.findColumnPosition(alwidth - 1);
107     if (visXAsRes + vpwidth - 1 > visAlignWidth)
108     {
109       // went past the end of the alignment, adjust backwards
110
111       // if last position was before the end of the alignment, need to update
112       if ((scrollCol + vpwidth - 1) < visAlignWidth)
113       {
114         visXAsRes = hiddenCols.findColumnPosition(hiddenCols
115                 .subtractVisibleColumns(vpwidth - 1, alwidth - 1));
116       }
117       else
118       {
119         visXAsRes = scrollCol;
120       }
121     }
122
123     //
124     // Convert y value to sequence position
125     //
126
127     // convert y to residues
128     int yAsSeq = Math.round((float) y * alheight / sequencesHeight);
129
130     // get viewport height in sequences
131     // add 1 because height includes both endSeq and startSeq
132     int vpheight = ranges.getEndSeq() - ranges.getStartSeq() + 1;
133
134     // get where y should be when accounting for hidden rows
135     // if y is in a hidden row region, shift up - but we still need absolute
136     // position,
137     // so convert back after getting visible region position
138     yAsSeq = hiddenSeqs.adjustForHiddenSeqs(hiddenSeqs
139             .findIndexWithoutHiddenSeqs(yAsSeq));
140
141     // check in case we went off the edge of the alignment
142     int visAlignHeight = hiddenSeqs.findIndexWithoutHiddenSeqs(alheight);
143     int visYAsSeq = hiddenSeqs.findIndexWithoutHiddenSeqs(yAsSeq);
144     if (visYAsSeq + vpheight - 1 > visAlignHeight)
145     {
146       // went past the end of the alignment, adjust backwards
147       if ((scrollRow + vpheight - 1) < visAlignHeight)
148       {
149         visYAsSeq = hiddenSeqs.findIndexWithoutHiddenSeqs(hiddenSeqs
150                 .subtractVisibleRows(vpheight - 1, alheight - 1));
151       }
152       else
153       {
154         visYAsSeq = scrollRow;
155       }
156     }
157
158     // update scroll values
159     scrollCol = visXAsRes;
160     scrollRow = visYAsSeq;
161
162   }
163
164   /**
165    * Update the overview panel box when the associated alignment panel is
166    * changed
167    * 
168    * @param hiddenSeqs
169    *          hidden sequences
170    * @param hiddenCols
171    *          hidden columns
172    * @param ranges
173    *          viewport position properties
174    */
175   @Override
176   public void setBoxPosition(HiddenSequences hiddenSeqs,
177           HiddenColumns hiddenCols, ViewportRanges ranges)
178   {
179     alwidth = ranges.getAbsoluteAlignmentWidth();
180     alheight = ranges.getAbsoluteAlignmentHeight();
181
182     // work with absolute values of startRes and endRes
183     int startRes = hiddenCols.adjustForHiddenColumns(ranges.getStartRes());
184     int endRes = hiddenCols.adjustForHiddenColumns(ranges.getEndRes());
185
186     // work with absolute values of startSeq and endSeq
187     int startSeq = hiddenSeqs.adjustForHiddenSeqs(ranges.getStartSeq());
188     int endSeq = hiddenSeqs.adjustForHiddenSeqs(ranges.getEndSeq());
189
190     // boxX, boxY is the x,y location equivalent to startRes, startSeq
191     boxX = Math.round((float) startRes * width / alwidth);
192     boxY = Math.round((float) startSeq * sequencesHeight / alheight);
193
194     // boxWidth is the width in residues translated to pixels
195     // since the box includes both the start and end residues, add 1 to the
196     // difference
197     boxWidth = Math
198             .round((float) (endRes - startRes + 1) * width / alwidth);
199     // boxHeight is the height in sequences translated to pixels
200     boxHeight = Math.round((float) (endSeq - startSeq + 1)
201             * sequencesHeight
202             / alheight);
203   }
204
205   @Override
206   public AlignmentColsCollectionI getColumns(ViewportRanges ranges,
207           HiddenColumns hiddenCols)
208   {
209     return new AllColsCollection(0,
210             ranges.getAbsoluteAlignmentWidth() - 1,
211             hiddenCols);
212   }
213
214   @Override
215   public AlignmentRowsCollectionI getRows(ViewportRanges ranges,
216           AlignmentI al)
217   {
218     return new AllRowsCollection(0,
219             ranges.getAbsoluteAlignmentHeight() - 1,
220             al);
221   }
222 }