JAL-2611 First pass, not thrilled with hidden column behaviour
[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
35   protected static final int MIN_WIDTH = 120;
36
37   protected static final int MIN_SEQ_HEIGHT = 40;
38
39   protected static final int MAX_SEQ_HEIGHT = 300;
40
41   private static final int DEFAULT_GRAPH_HEIGHT = 20;
42
43   protected int width;
44
45   protected int sequencesHeight;
46
47   protected int graphHeight = DEFAULT_GRAPH_HEIGHT;
48
49   protected int boxX = -1;
50
51   protected int boxY = -1;
52
53   protected int boxWidth = -1;
54
55   protected int boxHeight = -1;
56
57   protected int alwidth;
58
59   protected int alheight;
60
61   protected int transX;
62
63   protected int transY;
64
65   /**
66    * Create an OverviewDimensions object
67    * 
68    * @param ranges
69    *          positional properties of the viewport
70    * @param showAnnotationPanel
71    *          true if the annotation panel is to be shown, false otherwise
72    */
73   public OverviewDimensions(ViewportRanges ranges,
74           boolean showAnnotationPanel)
75   {
76     // scale the initial size of overviewpanel to shape of alignment
77     float initialScale = (float) ranges.getAbsoluteAlignmentWidth()
78             / (float) ranges.getAbsoluteAlignmentHeight();
79
80     if (!showAnnotationPanel)
81     {
82       graphHeight = 0;
83     }
84
85     if (ranges.getAbsoluteAlignmentWidth() > ranges
86             .getAbsoluteAlignmentHeight())
87     {
88       // wider
89       width = MAX_WIDTH;
90       sequencesHeight = Math.round(MAX_WIDTH / initialScale);
91       if (sequencesHeight < MIN_SEQ_HEIGHT)
92       {
93         sequencesHeight = MIN_SEQ_HEIGHT;
94       }
95     }
96     else
97     {
98       // taller
99       width = Math.round(MAX_WIDTH * initialScale);
100       sequencesHeight = MAX_SEQ_HEIGHT;
101
102       if (width < MIN_WIDTH)
103       {
104         width = MIN_WIDTH;
105       }
106     }
107   }
108
109   /**
110    * Draw the overview panel's viewport box on a graphics object
111    * 
112    * @param g
113    *          the graphics object to draw on
114    */
115   public void drawBox(Graphics g)
116   {
117     g.drawRect(boxX, boxY, boxWidth, boxHeight);
118     g.drawRect(boxX + 1, boxY + 1, boxWidth - 2, boxHeight - 2);
119   }
120
121   public int getBoxX()
122   {
123     return boxX;
124   }
125
126   public int getBoxY()
127   {
128     return boxY;
129   }
130
131   public int getBoxWidth()
132   {
133     return boxWidth;
134   }
135
136   public int getBoxHeight()
137   {
138     return boxHeight;
139   }
140
141   public int getWidth()
142   {
143     return width;
144   }
145
146   public int getHeight()
147   {
148     return sequencesHeight + graphHeight;
149   }
150
151   public int getSequencesHeight()
152   {
153     return sequencesHeight;
154   }
155
156   public int getGraphHeight()
157   {
158     return graphHeight;
159   }
160
161   public float getPixelsPerCol()
162   {
163     resetAlignmentDims();
164     return (float) width / alwidth;
165   }
166
167   public float getPixelsPerSeq()
168   {
169     resetAlignmentDims();
170     return (float) sequencesHeight / alheight;
171   }
172
173   public void setWidth(int w)
174   {
175     width = w;
176   }
177
178   public void setHeight(int h)
179   {
180     sequencesHeight = h - graphHeight;
181   }
182
183   /**
184    * Update the viewport location from a mouse click in the overview panel
185    * 
186    * @param mousex
187    *          x location of mouse
188    * @param mousey
189    *          y location of mouse
190    * @param hiddenSeqs
191    *          the alignment's hidden sequences
192    * @param hiddenCols
193    *          the alignment's hidden columns
194    */
195   public abstract void updateViewportFromMouse(int mousex, int mousey,
196           HiddenSequences hiddenSeqs, HiddenColumns hiddenCols);
197
198   public abstract void adjustViewportFromMouse(int mousex, int mousey,
199           HiddenSequences hiddenSeqs, HiddenColumns hiddenCols);
200
201   public abstract void setDragPoint(int x, int y,
202           HiddenSequences hiddenSeqs, HiddenColumns hiddenCols);
203
204   protected abstract void updateViewportFromTopLeft(int mousex, int mousey,
205           HiddenSequences hiddenSeqs, HiddenColumns hiddenCols);
206
207   /**
208    * Set the overview panel's box position to match the viewport
209    * 
210    * @param hiddenSeqs
211    *          the alignment's hidden sequences
212    * @param hiddenCols
213    *          the alignment's hidden columns
214    */
215   public abstract void setBoxPosition(HiddenSequences hiddenSeqs,
216           HiddenColumns hiddenCols);
217
218   /**
219    * Get the collection of columns used by this overview dimensions object
220    * 
221    * @param hiddenCols
222    *          the alignment's hidden columns
223    * @return a column collection
224    */
225   public abstract AlignmentColsCollectionI getColumns(AlignmentI al);
226
227   /**
228    * Get the collection of rows used by this overview dimensions object
229    * 
230    * @param al
231    *          the alignment
232    * @return a row collection
233    */
234   public abstract AlignmentRowsCollectionI getRows(AlignmentI al);
235
236   /**
237    * Updates overview dimensions to account for current alignment dimensions
238    */
239   protected abstract void resetAlignmentDims();
240
241   protected void setBoxPosition(int startRes, int startSeq, int vpwidth,
242           int vpheight)
243   {
244     resetAlignmentDims();
245
246     // boxX, boxY is the x,y location equivalent to startRes, startSeq
247     boxX = Math.round((float) startRes * width / alwidth);
248     boxY = Math.round((float) startSeq * sequencesHeight / alheight);
249
250     // boxWidth is the width in residues translated to pixels
251     boxWidth = Math.round((float) vpwidth * width / alwidth);
252
253     // boxHeight is the height in sequences translated to pixels
254     boxHeight = Math.round((float) vpheight * sequencesHeight / alheight);
255
256     System.out.println("Update box: x: " + boxX);
257   }
258
259   /**
260    * Answers if a mouse position is in the overview's red box
261    * 
262    * @param x
263    *          mouse x position
264    * @param y
265    *          mouse y position
266    * @return true if (x,y) is inside the box
267    */
268   public boolean isPositionInBox(int x, int y)
269   {
270     return (x > boxX && y > boxY && x < boxX + boxWidth
271             && y < boxY + boxHeight);
272   }
273
274   protected abstract int getLeftXFromCentreX(int mousex, HiddenColumns hidden);
275
276   protected abstract int getTopYFromCentreY(int mousey,
277           HiddenSequences hidden);
278
279 }