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