JAL-2388 Alignment col and row collections with iterators
[jalview.git] / src / jalview / viewmodel / OverviewDimensionsAllVisible.java
1 package jalview.viewmodel;
2
3 import jalview.api.AlignmentColsCollectionI;
4 import jalview.api.AlignmentRowsCollectionI;
5 import jalview.datamodel.AlignmentI;
6 import jalview.datamodel.AllColsCollection;
7 import jalview.datamodel.AllRowsCollection;
8 import jalview.datamodel.ColumnSelection;
9 import jalview.datamodel.HiddenSequences;
10
11 public class OverviewDimensionsAllVisible extends OverviewDimensions
12 {
13   private float pixelsPerCol;
14
15   private float pixelsPerSeq;
16
17   public OverviewDimensionsAllVisible(ViewportRanges ranges,
18           boolean showAnnotationPanel)
19   {
20     super(ranges, showAnnotationPanel);
21
22     int alwidth = ranges.getVisibleAlignmentWidth();
23     int alheight = ranges.getVisibleAlignmentHeight();
24
25     pixelsPerCol = (float) width / alwidth;
26     pixelsPerSeq = (float) sequencesHeight / alheight;
27   }
28
29   @Override
30   public void updateViewportFromMouse(int mousex, int mousey,
31           HiddenSequences hiddenSeqs, ColumnSelection hiddenCols,
32           ViewportRanges ranges)
33   {
34     int x = mousex;
35     int y = mousey;
36
37     int alwidth = ranges.getVisibleAlignmentWidth();
38     int alheight = ranges.getVisibleAlignmentHeight();
39
40     if (x < 0)
41     {
42       x = 0;
43     }
44
45     if (y < 0)
46     {
47       y = 0;
48     }
49
50     //
51     // Convert x value to residue position
52     //
53
54     // need to determine where scrollCol should be, given x
55     // to do this also need to know width of viewport, and some hidden column
56     // correction
57
58     // convert x to residues - this is an absolute position
59     int xAsRes = Math.round((float) x * alwidth / width);
60
61     // get viewport width in residues
62     int vpwidth = ranges.getEndRes() - ranges.getStartRes() + 1;
63
64     if (xAsRes + vpwidth > alwidth)
65     {
66       // went past the end of the alignment, adjust backwards
67
68       // if last position was before the end of the alignment, need to update
69       if ((scrollCol + vpwidth - 1) < alwidth)
70       {
71         xAsRes = alwidth - vpwidth;
72       }
73       else
74       {
75         xAsRes = scrollCol;
76       }
77     }
78
79
80     //
81     // Convert y value to sequence position
82     //
83
84     // convert y to residues
85     int yAsSeq = Math.round((float) y * alheight / sequencesHeight);
86
87     // get viewport height in sequences
88     // add 1 because height includes both endSeq and startSeq
89     int vpheight = ranges.getEndSeq() - ranges.getStartSeq() + 1;
90
91     if (yAsSeq + vpheight > alheight)
92     {
93       // went past the end of the alignment, adjust backwards
94       if ((scrollRow + vpheight - 1) < alheight)
95       {
96         yAsSeq = alheight - vpheight;
97       }
98       else
99       {
100         yAsSeq = scrollRow;
101       }
102     }
103
104     // update scroll values
105     scrollCol = xAsRes;
106     scrollRow = yAsSeq;
107
108   }
109
110   @Override
111   public void setBoxPosition(HiddenSequences hiddenSeqs,
112           ColumnSelection hiddenCols, ViewportRanges ranges)
113   {
114     int alwidth = ranges.getVisibleAlignmentWidth();
115     int alheight = ranges.getVisibleAlignmentHeight();
116
117     // work with visible values of startRes and endRes
118     int startRes = ranges.getStartRes();
119     int endRes = ranges.getEndRes();
120
121     // work with visible values of startSeq and endSeq
122     int startSeq = ranges.getStartSeq();
123     int endSeq = ranges.getEndSeq();
124
125     // boxX, boxY is the x,y location equivalent to startRes, startSeq
126     boxX = Math.round((float) startRes * width / alwidth);
127     boxY = Math.round((float) startSeq * sequencesHeight / alheight);
128
129     // boxWidth is the width in residues translated to pixels
130     // since the box includes both the start and end residues, add 1 to the
131     // difference
132     boxWidth = Math
133             .round((float) (endRes - startRes + 1) * width / alwidth);
134     // boxHeight is the height in sequences translated to pixels
135     boxHeight = Math.round((float) (endSeq - startSeq + 1)
136             * sequencesHeight / alheight);
137
138   }
139
140   @Override
141   public AlignmentColsCollectionI getColumns(ViewportRanges ranges,
142           ColumnSelection hiddenCols)
143   {
144     return new AllColsCollection(0,
145             ranges.getVisibleAlignmentWidth() - 1, hiddenCols);
146   }
147
148   @Override
149   public AlignmentRowsCollectionI getRows(ViewportRanges ranges,
150           AlignmentI al)
151   {
152     return new AllRowsCollection(0,
153             ranges.getVisibleAlignmentHeight() - 1, al);
154   }
155
156   @Override
157   public float getPixelsPerCol()
158   {
159     return pixelsPerCol;
160   }
161
162   @Override
163   public float getPixelsPerSeq()
164   {
165     return pixelsPerSeq;
166   }
167 }