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