JAL-2418 add GPL to main sources
[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 jalview.api.AlignmentColsCollectionI;
24 import jalview.api.AlignmentRowsCollectionI;
25 import jalview.datamodel.AlignmentI;
26 import jalview.datamodel.HiddenColumns;
27 import jalview.datamodel.HiddenSequences;
28 import jalview.datamodel.VisibleColsCollection;
29 import jalview.datamodel.VisibleRowsCollection;
30
31 public class OverviewDimensionsHideHidden 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   public OverviewDimensionsHideHidden(ViewportRanges vpranges,
42           boolean showAnnotationPanel)
43   {
44     super(vpranges, showAnnotationPanel);
45     ranges = vpranges;
46     resetAlignmentDims();
47   }
48
49   @Override
50   public void updateViewportFromMouse(int mousex, int mousey,
51           HiddenSequences hiddenSeqs, HiddenColumns hiddenCols)
52   {
53     int xAsRes = getLeftXFromCentreX(mousex, hiddenCols);
54     int yAsSeq = getTopYFromCentreY(mousey, hiddenSeqs);
55
56     updateViewportFromTopLeft(xAsRes, yAsSeq, hiddenSeqs, hiddenCols);
57
58   }
59
60   @Override
61   public void adjustViewportFromMouse(int mousex, int mousey,
62           HiddenSequences hiddenSeqs, HiddenColumns hiddenCols)
63   {
64     // calculate translation in pixel terms:
65     // get mouse location in viewport coords, add translation in viewport
66     // coords, and update viewport as usual
67     int vpx = Math.round((float) mousex * alwidth / width);
68     int vpy = Math.round((float) mousey * alheight / sequencesHeight);
69
70     updateViewportFromTopLeft(vpx + xdiff, vpy + ydiff, hiddenSeqs,
71             hiddenCols);
72
73   }
74
75   @Override
76   protected void updateViewportFromTopLeft(int leftx, int topy,
77           HiddenSequences hiddenSeqs, HiddenColumns hiddenCols)
78   {
79     int xAsRes = leftx;
80     int yAsSeq = topy;
81     resetAlignmentDims();
82
83     if (xAsRes < 0)
84     {
85       xAsRes = 0;
86     }
87
88     if (yAsSeq < 0)
89     {
90       yAsSeq = 0;
91     }
92
93     if (ranges.isWrappedMode())
94     {
95       yAsSeq = 0; // sorry, no vertical scroll when wrapped
96     }
97
98     // get viewport width in residues
99     int vpwidth = ranges.getViewportWidth();
100
101     if (xAsRes + vpwidth > alwidth)
102     {
103       // went past the end of the alignment, adjust backwards
104
105       // if last position was before the end of the alignment, need to update
106       if (ranges.getStartRes() < alwidth)
107       {
108         xAsRes = alwidth - vpwidth;
109       }
110       else
111       {
112         xAsRes = ranges.getStartRes();
113       }
114     }
115
116     // Determine where scrollRow should be, given visYAsSeq
117
118     // get viewport height in sequences
119     // add 1 because height includes both endSeq and startSeq
120     int vpheight = ranges.getViewportHeight();
121
122     if (yAsSeq + vpheight > alheight)
123     {
124       // went past the end of the alignment, adjust backwards
125       if (ranges.getEndSeq() < alheight)
126       {
127         yAsSeq = alheight - vpheight;
128       }
129       else
130       {
131         yAsSeq = ranges.getStartSeq();
132       }
133     }
134
135     // update viewport
136     ranges.setStartRes(xAsRes);
137     ranges.setStartSeq(yAsSeq);
138   }
139
140   @Override
141   public void setBoxPosition(HiddenSequences hiddenSeqs,
142           HiddenColumns hiddenCols)
143   {
144     setBoxPosition(ranges.getStartRes(), ranges.getStartSeq(),
145             ranges.getViewportWidth(), ranges.getViewportHeight());
146   }
147
148   @Override
149   public AlignmentColsCollectionI getColumns(AlignmentI al)
150   {
151     return new VisibleColsCollection(0,
152             ranges.getAbsoluteAlignmentWidth() - 1, al);
153   }
154
155   @Override
156   public AlignmentRowsCollectionI getRows(AlignmentI al)
157   {
158     return new VisibleRowsCollection(0,
159             ranges.getAbsoluteAlignmentHeight() - 1, al);
160   }
161
162   @Override
163   protected void resetAlignmentDims()
164   {
165     alwidth = ranges.getVisibleAlignmentWidth();
166     alheight = ranges.getVisibleAlignmentHeight();
167   }
168
169   @Override
170   protected int getLeftXFromCentreX(int mousex, HiddenColumns hidden)
171   {
172     int vpx = Math.round((float) mousex * alwidth / width);
173     return vpx - ranges.getViewportWidth() / 2;
174   }
175
176   @Override
177   protected int getTopYFromCentreY(int mousey, HiddenSequences hidden)
178   {
179     int vpy = Math.round((float) mousey * alheight / sequencesHeight);
180     return vpy - ranges.getViewportHeight() / 2;
181   }
182
183   @Override
184   public void setDragPoint(int x, int y, HiddenSequences hiddenSeqs,
185           HiddenColumns hiddenCols)
186   {
187     // get alignment position of x and box (can get directly from vpranges) and
188     // calculate difference between the positions
189     int vpx = Math.round((float) x * alwidth / width);
190     int vpy = Math.round((float) y * alheight / sequencesHeight);
191
192     xdiff = ranges.getStartRes() - vpx;
193     ydiff = ranges.getStartSeq() - vpy;
194   }
195
196 }