Merge branch 'develop' into feature/JAL-2611
[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   /**
62    * Create an OverviewDimensions object
63    * 
64    * @param ranges
65    *          positional properties of the viewport
66    * @param showAnnotationPanel
67    *          true if the annotation panel is to be shown, false otherwise
68    */
69   public OverviewDimensions(ViewportRanges ranges,
70           boolean showAnnotationPanel)
71   {
72     // scale the initial size of overviewpanel to shape of alignment
73     float initialScale = (float) ranges.getAbsoluteAlignmentWidth()
74             / (float) ranges.getAbsoluteAlignmentHeight();
75
76     if (!showAnnotationPanel)
77     {
78       graphHeight = 0;
79     }
80
81     if (ranges.getAbsoluteAlignmentWidth() > ranges
82             .getAbsoluteAlignmentHeight())
83     {
84       // wider
85       width = MAX_WIDTH;
86       sequencesHeight = Math.round(MAX_WIDTH / initialScale);
87       if (sequencesHeight < MIN_SEQ_HEIGHT)
88       {
89         sequencesHeight = MIN_SEQ_HEIGHT;
90       }
91     }
92     else
93     {
94       // taller
95       width = Math.round(MAX_WIDTH * initialScale);
96       sequencesHeight = MAX_SEQ_HEIGHT;
97
98       if (width < MIN_WIDTH)
99       {
100         width = MIN_WIDTH;
101       }
102     }
103   }
104
105   /**
106    * Draw the overview panel's viewport box on a graphics object
107    * 
108    * @param g
109    *          the graphics object to draw on
110    */
111   public void drawBox(Graphics g)
112   {
113     g.drawRect(boxX, boxY, boxWidth, boxHeight);
114     g.drawRect(boxX + 1, boxY + 1, boxWidth - 2, boxHeight - 2);
115   }
116
117   public int getBoxX()
118   {
119     return boxX;
120   }
121
122   public int getBoxY()
123   {
124     return boxY;
125   }
126
127   public int getBoxWidth()
128   {
129     return boxWidth;
130   }
131
132   public int getBoxHeight()
133   {
134     return boxHeight;
135   }
136
137   public int getWidth()
138   {
139     return width;
140   }
141
142   public int getHeight()
143   {
144     return sequencesHeight + graphHeight;
145   }
146
147   public int getSequencesHeight()
148   {
149     return sequencesHeight;
150   }
151
152   public int getGraphHeight()
153   {
154     return graphHeight;
155   }
156
157   public float getPixelsPerCol()
158   {
159     resetAlignmentDims();
160     return (float) width / alwidth;
161   }
162
163   public float getPixelsPerSeq()
164   {
165     resetAlignmentDims();
166     return (float) sequencesHeight / alheight;
167   }
168
169   public void setWidth(int w)
170   {
171     width = w;
172   }
173
174   public void setHeight(int h)
175   {
176     sequencesHeight = h - graphHeight;
177   }
178
179   /**
180    * Update the viewport location from a mouse click in the overview panel
181    * 
182    * @param mousex
183    *          x location of mouse
184    * @param mousey
185    *          y location of mouse
186    * @param hiddenSeqs
187    *          the alignment's hidden sequences
188    * @param hiddenCols
189    *          the alignment's hidden columns
190    */
191   public abstract void updateViewportFromMouse(int mousex, int mousey,
192           HiddenSequences hiddenSeqs, HiddenColumns hiddenCols);
193
194   /**
195    * Update the viewport location from a mouse drag within the overview's box
196    * 
197    * @param mousex
198    *          x location of mouse
199    * @param mousey
200    *          y location of mouse
201    * @param hiddenSeqs
202    *          the alignment's hidden sequences
203    * @param hiddenCols
204    *          the alignment's hidden columns
205    */
206   public abstract void adjustViewportFromMouse(int mousex, int mousey,
207           HiddenSequences hiddenSeqs, HiddenColumns hiddenCols);
208
209   /**
210    * Initialise dragging from the mouse - must be called on initial mouse click
211    * before using adjustViewportFromMouse in drag operations
212    * 
213    * @param mousex
214    *          x location of mouse
215    * @param mousey
216    *          y location of mouse
217    * @param hiddenSeqs
218    *          the alignment's hidden sequences
219    * @param hiddenCols
220    *          the alignment's hidden columns
221    */
222   public abstract void setDragPoint(int x, int y,
223           HiddenSequences hiddenSeqs, HiddenColumns hiddenCols);
224
225   /*
226    * Move the viewport so that the top left corner of the overview's box 
227    * is at the mouse position (leftx, topy)
228    */
229   protected abstract void updateViewportFromTopLeft(int leftx, int topy,
230           HiddenSequences hiddenSeqs, HiddenColumns hiddenCols);
231
232   /**
233    * Set the overview panel's box position to match the viewport
234    * 
235    * @param hiddenSeqs
236    *          the alignment's hidden sequences
237    * @param hiddenCols
238    *          the alignment's hidden columns
239    */
240   public abstract void setBoxPosition(HiddenSequences hiddenSeqs,
241           HiddenColumns hiddenCols);
242
243   /**
244    * Get the collection of columns used by this overview dimensions object
245    * 
246    * @param hiddenCols
247    *          the alignment's hidden columns
248    * @return a column collection
249    */
250   public abstract AlignmentColsCollectionI getColumns(AlignmentI al);
251
252   /**
253    * Get the collection of rows used by this overview dimensions object
254    * 
255    * @param al
256    *          the alignment
257    * @return a row collection
258    */
259   public abstract AlignmentRowsCollectionI getRows(AlignmentI al);
260
261   /**
262    * Updates overview dimensions to account for current alignment dimensions
263    */
264   protected abstract void resetAlignmentDims();
265
266   /*
267    * Given the box coordinates in residues and sequences, set the box dimensions in the overview window
268    */
269   protected void setBoxPosition(int startRes, int startSeq, int vpwidth,
270           int vpheight)
271   {
272     resetAlignmentDims();
273
274     // boxX, boxY is the x,y location equivalent to startRes, startSeq
275     int xPos = Math.min(startRes, alwidth - vpwidth + 1);
276     boxX = Math.round((float) xPos * width / alwidth);
277     boxY = Math.round((float) startSeq * sequencesHeight / alheight);
278
279     // boxWidth is the width in residues translated to pixels
280     boxWidth = Math.round((float) vpwidth * width / alwidth);
281
282     // boxHeight is the height in sequences translated to pixels
283     boxHeight = Math.round((float) vpheight * sequencesHeight / alheight);
284   }
285
286   /**
287    * Answers if a mouse position is in the overview's red box
288    * 
289    * @param x
290    *          mouse x position
291    * @param y
292    *          mouse y position
293    * @return true if (x,y) is inside the box
294    */
295   public boolean isPositionInBox(int x, int y)
296   {
297     return (x > boxX && y > boxY && x < boxX + boxWidth
298             && y < boxY + boxHeight);
299   }
300
301   /*
302    * Given the centre x position, calculate the box's left x position
303    */
304   protected abstract int getLeftXFromCentreX(int mousex, HiddenColumns hidden);
305
306   /*
307    * Given the centre y position, calculate the box's top y position
308    */
309   protected abstract int getTopYFromCentreY(int mousey,
310           HiddenSequences hidden);
311
312 }