d7f2e61b641b1853a5dec85ffa270a83ffa11985
[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 float widthRatio;
62
63   protected float heightRatio;
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   /**
199    * Update the viewport location from a mouse drag within the overview's box
200    * 
201    * @param mousex
202    *          x location of mouse
203    * @param mousey
204    *          y location of mouse
205    * @param hiddenSeqs
206    *          the alignment's hidden sequences
207    * @param hiddenCols
208    *          the alignment's hidden columns
209    */
210   public abstract void adjustViewportFromMouse(int mousex, int mousey,
211           HiddenSequences hiddenSeqs, HiddenColumns hiddenCols);
212
213   /**
214    * Initialise dragging from the mouse - must be called on initial mouse click
215    * before using adjustViewportFromMouse in drag operations
216    * 
217    * @param mousex
218    *          x location of mouse
219    * @param mousey
220    *          y location of mouse
221    * @param hiddenSeqs
222    *          the alignment's hidden sequences
223    * @param hiddenCols
224    *          the alignment's hidden columns
225    */
226   public abstract void setDragPoint(int x, int y,
227           HiddenSequences hiddenSeqs, HiddenColumns hiddenCols);
228
229   /*
230    * Move the viewport so that the top left corner of the overview's box 
231    * is at the mouse position (leftx, topy)
232    */
233   protected abstract void updateViewportFromTopLeft(int leftx, int topy,
234           HiddenSequences hiddenSeqs, HiddenColumns hiddenCols);
235
236   /**
237    * Set the overview panel's box position to match the viewport
238    * 
239    * @param hiddenSeqs
240    *          the alignment's hidden sequences
241    * @param hiddenCols
242    *          the alignment's hidden columns
243    */
244   public abstract void setBoxPosition(HiddenSequences hiddenSeqs,
245           HiddenColumns hiddenCols);
246
247   /**
248    * Get the collection of columns used by this overview dimensions object
249    * 
250    * @param hiddenCols
251    *          the alignment's hidden columns
252    * @return a column collection
253    */
254   public abstract AlignmentColsCollectionI getColumns(AlignmentI al);
255
256   /**
257    * Get the collection of rows used by this overview dimensions object
258    * 
259    * @param al
260    *          the alignment
261    * @return a row collection
262    */
263   public abstract AlignmentRowsCollectionI getRows(AlignmentI al);
264
265   /**
266    * Updates overview dimensions to account for current alignment dimensions
267    */
268   protected abstract void resetAlignmentDims();
269
270   /*
271    * Given the box coordinates in residues and sequences, set the box dimensions in the overview window
272    */
273   protected void setBoxPosition(int startRes, int startSeq, int vpwidth,
274           int vpheight)
275   {
276     resetAlignmentDims();
277
278     // boxX, boxY is the x,y location equivalent to startRes, startSeq
279     int xPos = Math.min(startRes, alwidth - vpwidth + 1);
280     boxX = Math.round((float) xPos * width / alwidth);
281     boxY = Math.round((float) startSeq * sequencesHeight / alheight);
282
283     // boxWidth is the width in residues translated to pixels
284     boxWidth = Math.round((float) vpwidth * width / alwidth);
285
286     // boxHeight is the height in sequences translated to pixels
287     boxHeight = Math.round((float) vpheight * sequencesHeight / alheight);
288   }
289
290   /**
291    * Answers if a mouse position is in the overview's red box
292    * 
293    * @param x
294    *          mouse x position
295    * @param y
296    *          mouse y position
297    * @return true if (x,y) is inside the box
298    */
299   public boolean isPositionInBox(int x, int y)
300   {
301     return (x > boxX && y > boxY && x < boxX + boxWidth
302             && y < boxY + boxHeight);
303   }
304
305   /*
306    * Given the centre x position, calculate the box's left x position
307    */
308   protected abstract int getLeftXFromCentreX(int mousex,
309           HiddenColumns hidden);
310
311   /*
312    * Given the centre y position, calculate the box's top y position
313    */
314   protected abstract int getTopYFromCentreY(int mousey,
315           HiddenSequences hidden);
316
317 }