JAL-2388 Reverted to previous
[jalview.git] / src / jalview / viewmodel / ViewportPositionProps.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.ViewStyleI;
24 import jalview.datamodel.AlignmentI;
25
26 /**
27  * Supplies and updates viewport properties relating to position such as: start
28  * and end residues and sequences, hidden column/row adjustments, ratio of
29  * viewport to alignment etc
30  */
31 public class ViewportPositionProps extends ViewportProperties
32 {
33   // start residue of viewport
34   private int startRes;
35
36   // end residue of viewport
37   private int endRes;
38
39   // start sequence of viewport
40   private int startSeq;
41
42   // end sequence of viewport
43   private int endSeq;
44
45   // character height
46   private int charHeight;
47
48   // character width
49   private int charWidth;
50
51   // alignment
52   private AlignmentI al;
53
54   // viewstyle
55   private ViewStyleI viewstyle;
56
57   /**
58    * Constructor
59    * @param alignment TODO
60    */
61   public ViewportPositionProps(AlignmentI alignment, ViewStyleI vstyle)
62   {
63     // initial values of viewport settings
64     this.startRes = 0;
65     this.endRes = alignment.getWidth() - 1;
66     this.startSeq = 0;
67     this.endSeq = alignment.getHeight() - 1;
68     this.al = alignment;
69     this.viewstyle = vstyle;
70   }
71
72   public void setCharHeight(int h)
73   {
74     viewstyle.setCharHeight(h);
75   }
76
77   public void setCharWidth(int w)
78   {
79     viewstyle.setCharWidth(w);
80   }
81
82   // ways to update values
83
84   // ways to notify of changes
85
86   // ways to supply positional information
87
88   /**
89    * Get alignment width
90    */
91   public int getAlignmentWidthInCols()
92   {
93     return al.getWidth();
94   }
95
96   /**
97    * Get alignment height
98    */
99   public int getAlignmentHeightInRows()
100   {
101     return al.getHeight();
102   }
103
104   public void setStartRes(int res)
105   {
106     if (res > al.getWidth() - 1)
107     {
108       res = al.getWidth() - 1;
109     }
110     else if (res < 0)
111     {
112       res = 0;
113     }
114     this.startRes = res;
115   }
116
117   public void setEndRes(int res)
118   {
119     if (res > al.getWidth() - 1)
120     {
121       res = al.getWidth() - 1;
122     }
123     else if (res < 0)
124     {
125       res = 0;
126     }
127     this.endRes = res;
128   }
129
130   public void setStartSeq(int seq)
131   {
132     if (seq > al.getHeight())
133     {
134       seq = al.getHeight();
135     }
136     else if (seq < 0)
137     {
138       seq = 0;
139     }
140     this.startSeq = seq;
141   }
142
143   public void setEndSeq(int seq)
144   {
145     if (seq > al.getHeight())
146     {
147       seq = al.getHeight();
148     }
149     else if (seq < 0)
150     {
151       seq = 0;
152     }
153     this.endSeq = seq;
154   }
155
156   /**
157    * Get start residue of viewport
158    */
159   public int getStartRes()
160   {
161     return startRes;
162   }
163
164   /**
165    * Get end residue of viewport
166    */
167   public int getEndRes()
168   {
169     return endRes;
170   }
171
172   /**
173    * Get start sequence of viewport
174    */
175   public int getStartSeq()
176   {
177     return startSeq;
178   }
179
180   /**
181    * Get end sequence of viewport
182    */
183   public int getEndSeq()
184   {
185     return endSeq;
186   }
187   /**
188    * Get start residue of viewport
189    */
190   public int getStartRes(boolean countHidden)
191   {
192     if (countHidden)
193     {
194       return 0; // av.getColumnSelection().adjustForHiddenColumns(startRes);
195     }
196     else
197     {
198       return startRes;
199     }
200   }
201
202   /**
203    * Convert distance x in viewport pixels to a distance in number of residues
204    * 
205    * @param x
206    *          number of pixels
207    * @return number of residues
208    */
209   public int convertPixelsToResidues(int x)
210   {
211     return Math.round((float) x / viewstyle.getCharWidth());
212     // return (int) ((float) x / viewstyle.getCharWidth());
213   }
214
215   /**
216    * Convert distance y in viewport pixels to a distance in number of sequences
217    * 
218    * @param y
219    *          number of pixels
220    * @return number of sequences
221    */
222   public int convertPixelsToSequences(int y)
223   {
224     return Math.round((float) y / viewstyle.getCharHeight());
225     // return (int) ((float) y / viewstyle.getCharHeight());
226   }
227   
228   /**
229    * Convert number of sequences s to a height in viewport pixels
230    * 
231    * @param s
232    *          number of sequences
233    * @return number of pixels
234    */
235   public int convertSequencesToPixels(int s)
236   {
237     return (s * viewstyle.getCharHeight());
238   }
239
240   /**
241    * Convert number of residues r to a width in viewport pixels
242    * 
243    * @param r
244    *          number of residues
245    * @return number of pixels
246    */
247   public int convertResiduesToPixels(int r)
248   {
249     return (r * viewstyle.getCharWidth());
250   }
251 }