JAL-2611 Added overview dimensions test for position in box
[jalview.git] / src / jalview / viewmodel / OverviewDimensions.java
index 50e1210..f053a95 100644 (file)
@@ -1,31 +1,71 @@
+/*
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
+ * 
+ * This file is part of Jalview.
+ * 
+ * Jalview is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License 
+ * as published by the Free Software Foundation, either version 3
+ * of the License, or (at your option) any later version.
+ *  
+ * Jalview is distributed in the hope that it will be useful, but 
+ * WITHOUT ANY WARRANTY; without even the implied warranty 
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
+ * PURPOSE.  See the GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
+ * The Jalview Authors are detailed in the 'AUTHORS' file.
+ */
 package jalview.viewmodel;
 
-import jalview.datamodel.AlignmentColsCollection;
+import jalview.api.AlignmentColsCollectionI;
+import jalview.api.AlignmentRowsCollectionI;
 import jalview.datamodel.AlignmentI;
-import jalview.datamodel.AlignmentRowsCollection;
-import jalview.datamodel.ColumnSelection;
+import jalview.datamodel.HiddenColumns;
 import jalview.datamodel.HiddenSequences;
 
 import java.awt.Graphics;
 
 public abstract class OverviewDimensions
 {
-
-  private static final int DEFAULT_GRAPH_HEIGHT = 20;
   protected static final int MAX_WIDTH = 400;
+
   protected static final int MIN_WIDTH = 120;
+
   protected static final int MIN_SEQ_HEIGHT = 40;
+
   protected static final int MAX_SEQ_HEIGHT = 300;
+
+  private static final int DEFAULT_GRAPH_HEIGHT = 20;
+
   protected int width;
+
   protected int sequencesHeight;
+
   protected int graphHeight = DEFAULT_GRAPH_HEIGHT;
+
   protected int boxX = -1;
+
   protected int boxY = -1;
+
   protected int boxWidth = -1;
+
   protected int boxHeight = -1;
-  protected int scrollCol = -1;
-  protected int scrollRow = -1;
 
+  protected int alwidth;
+
+  protected int alheight;
+
+  /**
+   * Create an OverviewDimensions object
+   * 
+   * @param ranges
+   *          positional properties of the viewport
+   * @param showAnnotationPanel
+   *          true if the annotation panel is to be shown, false otherwise
+   */
   public OverviewDimensions(ViewportRanges ranges,
           boolean showAnnotationPanel)
   {
@@ -74,16 +114,6 @@ public abstract class OverviewDimensions
     g.drawRect(boxX + 1, boxY + 1, boxWidth - 2, boxHeight - 2);
   }
 
-  public int getScrollCol()
-  {
-    return scrollCol;
-  }
-
-  public int getScrollRow()
-  {
-    return scrollRow;
-  }
-
   public int getBoxX()
   {
     return boxX;
@@ -104,16 +134,6 @@ public abstract class OverviewDimensions
     return boxHeight;
   }
 
-  public void setWidth(int w)
-  {
-    width = w;
-  }
-
-  public void setHeight(int h)
-  {
-    sequencesHeight = h - graphHeight;
-  }
-
   public int getWidth()
   {
     return width;
@@ -134,20 +154,105 @@ public abstract class OverviewDimensions
     return graphHeight;
   }
 
+  public float getPixelsPerCol()
+  {
+    resetAlignmentDims();
+    return (float) width / alwidth;
+  }
+
+  public float getPixelsPerSeq()
+  {
+    resetAlignmentDims();
+    return (float) sequencesHeight / alheight;
+  }
+
+  public void setWidth(int w)
+  {
+    width = w;
+  }
+
+  public void setHeight(int h)
+  {
+    sequencesHeight = h - graphHeight;
+  }
+
+  /**
+   * Update the viewport location from a mouse click in the overview panel
+   * 
+   * @param mousex
+   *          x location of mouse
+   * @param mousey
+   *          y location of mouse
+   * @param hiddenSeqs
+   *          the alignment's hidden sequences
+   * @param hiddenCols
+   *          the alignment's hidden columns
+   */
   public abstract void updateViewportFromMouse(int mousex, int mousey,
-          HiddenSequences hiddenSeqs, ColumnSelection hiddenCols,
-          ViewportRanges ranges);
+          HiddenSequences hiddenSeqs, HiddenColumns hiddenCols);
 
+  /**
+   * Set the overview panel's box position to match the viewport
+   * 
+   * @param hiddenSeqs
+   *          the alignment's hidden sequences
+   * @param hiddenCols
+   *          the alignment's hidden columns
+   */
   public abstract void setBoxPosition(HiddenSequences hiddenSeqs,
-          ColumnSelection hiddenCols, ViewportRanges ranges);
+          HiddenColumns hiddenCols);
 
-  public abstract AlignmentColsCollection getColumns(
-          ViewportRanges ranges, ColumnSelection hiddenCols);
+  /**
+   * Get the collection of columns used by this overview dimensions object
+   * 
+   * @param hiddenCols
+   *          the alignment's hidden columns
+   * @return a column collection
+   */
+  public abstract AlignmentColsCollectionI getColumns(AlignmentI al);
+
+  /**
+   * Get the collection of rows used by this overview dimensions object
+   * 
+   * @param al
+   *          the alignment
+   * @return a row collection
+   */
+  public abstract AlignmentRowsCollectionI getRows(AlignmentI al);
 
-  public abstract AlignmentRowsCollection getRows(
-          ViewportRanges ranges, AlignmentI al);
+  /**
+   * Updates overview dimensions to account for current alignment dimensions
+   */
+  protected abstract void resetAlignmentDims();
 
-  public abstract float getPixelsPerCol();
+  protected void setBoxPosition(int startRes, int startSeq, int vpwidth,
+          int vpheight)
+  {
+    resetAlignmentDims();
 
-  public abstract float getPixelsPerSeq();
+    // boxX, boxY is the x,y location equivalent to startRes, startSeq
+    boxX = Math.round((float) startRes * width / alwidth);
+    boxY = Math.round((float) startSeq * sequencesHeight / alheight);
+
+    // boxWidth is the width in residues translated to pixels
+    boxWidth = Math.round((float) vpwidth * width / alwidth);
+
+    // boxHeight is the height in sequences translated to pixels
+    boxHeight = Math.round((float) vpheight * sequencesHeight / alheight);
+  }
+
+  /**
+   * Answers if a mouse position is in the overview's red box
+   * 
+   * @param x
+   *          mouse x position
+   * @param y
+   *          mouse y position
+   * @return true if (x,y) is inside the box
+   */
+  protected boolean isPositionInBox(int x, int y)
+  {
+    return (x > boxX && y > boxY && boxX + x < boxWidth
+            && boxY + y < boxHeight);
+  }
 }
\ No newline at end of file