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