598b9899a3228372b2697a010efc9912144891c4
[jalview.git] / src / jalview / viewmodel / OverviewDimensionsHideHidden.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 java.awt.Dimension;
24
25 import jalview.api.AlignmentColsCollectionI;
26 import jalview.api.AlignmentRowsCollectionI;
27 import jalview.datamodel.AlignmentI;
28 import jalview.datamodel.HiddenColumns;
29 import jalview.datamodel.HiddenSequences;
30 import jalview.datamodel.VisibleColsCollection;
31 import jalview.datamodel.VisibleRowsCollection;
32
33 public class OverviewDimensionsHideHidden extends OverviewDimensions
34 {
35   private ViewportRanges ranges;
36
37   private int xdiff; // when dragging, difference in alignment units between
38                      // start residue and original mouse click position
39
40   private int ydiff; // when dragging, difference in alignment units between
41                      // start sequence and original mouse click position
42
43   /**
44    * for testng only
45    * 
46    * @param vpranges
47    * @param showAnnotationPanel
48    */  
49   @Deprecated
50   public OverviewDimensionsHideHidden(ViewportRanges vpranges, boolean showAnnotationPanel) {
51     this(vpranges, showAnnotationPanel, null);
52   }
53
54   public OverviewDimensionsHideHidden(ViewportRanges vpranges,
55           boolean showAnnotationPanel, Dimension dim)
56   {
57     super(vpranges, showAnnotationPanel, dim);
58     ranges = vpranges;
59     resetAlignmentDims();
60   }
61
62   @Override
63   public void updateViewportFromMouse(int mousex, int mousey,
64           HiddenSequences hiddenSeqs, HiddenColumns hiddenCols)
65   {
66     resetAlignmentDims();
67
68     int xAsRes = getLeftXFromCentreX(mousex, hiddenCols);
69     int yAsSeq = getTopYFromCentreY(mousey, hiddenSeqs);
70
71     updateViewportFromTopLeft(xAsRes, yAsSeq, hiddenSeqs, hiddenCols);
72
73   }
74
75   @Override
76   public void adjustViewportFromMouse(int mousex, int mousey,
77           HiddenSequences hiddenSeqs, HiddenColumns hiddenCols)
78   {
79     resetAlignmentDims();
80
81     // calculate translation in pixel terms:
82     // get mouse location in viewport coords, add translation in viewport
83     // coords, and update viewport as usual
84     int vpx = Math.round(mousex * widthRatio);
85     int vpy = Math.round(mousey * heightRatio);
86
87     updateViewportFromTopLeft(vpx + xdiff, vpy + ydiff, hiddenSeqs,
88             hiddenCols);
89
90   }
91
92   /**
93    * {@inheritDoc} Callers should have already called resetAlignmentDims to
94    * refresh alwidth, alheight and width/height ratios
95    */
96   @Override
97   protected void updateViewportFromTopLeft(int leftx, int topy,
98           HiddenSequences hiddenSeqs, HiddenColumns hiddenCols)
99   {
100     int xAsRes = leftx;
101     int yAsSeq = topy;
102
103     if (xAsRes < 0)
104     {
105       xAsRes = 0;
106     }
107
108     if (yAsSeq < 0)
109     {
110       yAsSeq = 0;
111     }
112
113     if (ranges.isWrappedMode())
114     {
115       yAsSeq = 0; // sorry, no vertical scroll when wrapped
116     }
117
118     // get viewport width in residues
119     int vpwidth = ranges.getViewportWidth();
120
121     if (xAsRes + vpwidth > alwidth)
122     {
123       // went past the end of the alignment, adjust backwards
124
125       // if last position was before the end of the alignment, need to update
126       if (ranges.getStartRes() < alwidth)
127       {
128         xAsRes = alwidth - vpwidth;
129       }
130       else
131       {
132         xAsRes = ranges.getStartRes();
133       }
134     }
135
136     // Determine where scrollRow should be, given visYAsSeq
137
138     // get viewport height in sequences
139     // add 1 because height includes both endSeq and startSeq
140     int vpheight = ranges.getViewportHeight();
141
142     if (yAsSeq + vpheight > alheight)
143     {
144       // went past the end of the alignment, adjust backwards
145       if (ranges.getEndSeq() < alheight)
146       {
147         yAsSeq = alheight - vpheight;
148       }
149       else
150       {
151         yAsSeq = ranges.getStartSeq();
152       }
153     }
154
155     ranges.setStartResAndSeq(xAsRes, yAsSeq);
156   }
157
158   @Override
159   public void setBoxPosition(HiddenSequences hiddenSeqs,
160           HiddenColumns hiddenCols)
161   {
162     setBoxPosition(ranges.getStartRes(), ranges.getStartSeq(),
163             ranges.getViewportWidth(), ranges.getViewportHeight());
164   }
165
166   @Override
167   public AlignmentColsCollectionI getColumns(AlignmentI al)
168   {
169     return new VisibleColsCollection(0,
170             ranges.getAbsoluteAlignmentWidth() - 1, al.getHiddenColumns());
171   }
172
173   @Override
174   public AlignmentRowsCollectionI getRows(AlignmentI al)
175   {
176     return new VisibleRowsCollection(0,
177             ranges.getAbsoluteAlignmentHeight() - 1, al);
178   }
179
180   @Override
181   protected void resetAlignmentDims()
182   {
183     alwidth = ranges.getVisibleAlignmentWidth();
184     alheight = ranges.getVisibleAlignmentHeight();
185
186     widthRatio = (float) alwidth / width;
187     heightRatio = (float) alheight / sequencesHeight;
188   }
189
190   /**
191    * {@inheritDoc} Callers should have already called resetAlignmentDims to
192    * refresh widthRatio
193    */
194   @Override
195   protected int getLeftXFromCentreX(int mousex, HiddenColumns hidden)
196   {
197     int vpx = Math.round(mousex * widthRatio);
198     return vpx - ranges.getViewportWidth() / 2;
199   }
200
201   /**
202    * {@inheritDoc} Callers should have already called resetAlignmentDims to
203    * refresh heightRatio
204    */
205   @Override
206   protected int getTopYFromCentreY(int mousey, HiddenSequences hidden)
207   {
208     int vpy = Math.round(mousey * heightRatio);
209     return vpy - ranges.getViewportHeight() / 2;
210   }
211
212   @Override
213   public void setDragPoint(int x, int y, HiddenSequences hiddenSeqs,
214           HiddenColumns hiddenCols)
215   {
216     resetAlignmentDims();
217
218     // get alignment position of x and box (can get directly from vpranges) and
219     // calculate difference between the positions
220     int vpx = Math.round(x * widthRatio);
221     int vpy = Math.round(y * heightRatio);
222
223     xdiff = ranges.getStartRes() - vpx;
224     ydiff = ranges.getStartSeq() - vpy;
225   }
226
227 }