JAL-2388 Working hidden regions hide/show in desktop
[jalview.git] / src / jalview / viewmodel / OverviewDimensions.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
29 import java.awt.Graphics;
30
31 public abstract class OverviewDimensions
32 {
33   protected static final int MAX_WIDTH = 400;
34   protected static final int MIN_WIDTH = 120;
35   protected static final int MIN_SEQ_HEIGHT = 40;
36   protected static final int MAX_SEQ_HEIGHT = 300;
37
38   private static final int DEFAULT_GRAPH_HEIGHT = 20;
39
40   protected int width;
41   protected int sequencesHeight;
42   protected int graphHeight = DEFAULT_GRAPH_HEIGHT;
43   protected int boxX = -1;
44   protected int boxY = -1;
45   protected int boxWidth = -1;
46   protected int boxHeight = -1;
47   protected int scrollCol = -1;
48   protected int scrollRow = -1;
49   protected int alwidth;
50   protected int alheight;
51
52   public OverviewDimensions(ViewportRanges ranges,
53           boolean showAnnotationPanel)
54   {
55     // scale the initial size of overviewpanel to shape of alignment
56     float initialScale = (float) ranges.getAbsoluteAlignmentWidth()
57             / (float) ranges.getAbsoluteAlignmentHeight();
58
59     if (!showAnnotationPanel)
60     {
61       graphHeight = 0;
62     }
63
64     if (ranges.getAbsoluteAlignmentWidth() > ranges
65             .getAbsoluteAlignmentHeight())
66     {
67       // wider
68       width = MAX_WIDTH;
69       sequencesHeight = Math.round(MAX_WIDTH / initialScale);
70       if (sequencesHeight < MIN_SEQ_HEIGHT)
71       {
72         sequencesHeight = MIN_SEQ_HEIGHT;
73       }
74     }
75     else
76     {
77       // taller
78       width = Math.round(MAX_WIDTH * initialScale);
79       sequencesHeight = MAX_SEQ_HEIGHT;
80
81       if (width < MIN_WIDTH)
82       {
83         width = MIN_WIDTH;
84       }
85     }
86   }
87
88   /**
89    * Draw the overview panel's viewport box on a graphics object
90    * 
91    * @param g
92    *          the graphics object to draw on
93    */
94   public void drawBox(Graphics g)
95   {
96     g.drawRect(boxX, boxY, boxWidth, boxHeight);
97     g.drawRect(boxX + 1, boxY + 1, boxWidth - 2, boxHeight - 2);
98   }
99
100   public int getScrollCol()
101   {
102     return scrollCol;
103   }
104
105   public int getScrollRow()
106   {
107     return scrollRow;
108   }
109
110   public int getBoxX()
111   {
112     return boxX;
113   }
114
115   public int getBoxY()
116   {
117     return boxY;
118   }
119
120   public int getBoxWidth()
121   {
122     return boxWidth;
123   }
124
125   public int getBoxHeight()
126   {
127     return boxHeight;
128   }
129
130   public int getWidth()
131   {
132     return width;
133   }
134
135   public int getHeight()
136   {
137     return sequencesHeight + graphHeight;
138   }
139
140   public int getSequencesHeight()
141   {
142     return sequencesHeight;
143   }
144
145   public int getGraphHeight()
146   {
147     return graphHeight;
148   }
149
150   public float getPixelsPerCol()
151   {
152     resetAlignmentDims();
153     return (float) width / alwidth;
154   }
155
156   public float getPixelsPerSeq()
157   {
158     resetAlignmentDims();
159     return (float) sequencesHeight / alheight;
160   }
161
162   public void setWidth(int w)
163   {
164     width = w;
165   }
166
167   public void setHeight(int h)
168   {
169     sequencesHeight = h - graphHeight;
170   }
171
172   /**
173    * Update the viewport location from a mouse click in the overview panel
174    * 
175    * @param mousex
176    *          x location of mouse
177    * @param mousey
178    *          y location of mouse
179    * @param hiddenSeqs
180    *          the alignment's hidden sequences
181    * @param hiddenCols
182    *          the alignment's hidden columns
183    */
184   public abstract void updateViewportFromMouse(int mousex, int mousey,
185           HiddenSequences hiddenSeqs, HiddenColumns hiddenCols);
186
187   /**
188    * Set the overview panel's box position to match the viewport
189    * 
190    * @param hiddenSeqs
191    *          the alignment's hidden sequences
192    * @param hiddenCols
193    *          the alignment's hidden columns
194    */
195   public abstract void setBoxPosition(HiddenSequences hiddenSeqs,
196           HiddenColumns hiddenCols);
197
198   /**
199    * Get the collection of columns used by this overview dimensions object
200    * 
201    * @param hiddenCols
202    *          the alignment's hidden columns
203    * @return a column collection
204    */
205   public abstract AlignmentColsCollectionI getColumns(
206           HiddenColumns hiddenCols);
207
208   /**
209    * Get the collection of rows used by this overview dimensions object
210    * 
211    * @param al
212    *          the alignment
213    * @return a row collection
214    */
215   public abstract AlignmentRowsCollectionI getRows(AlignmentI al);
216
217   /**
218    * Updates overview dimensions to account for current alignment dimensions
219    */
220   protected abstract void resetAlignmentDims();
221 }