JAL-2665 Fixed wrapped mode group over >1 panel; and hidden cols
[jalview.git] / src / jalview / gui / SeqCanvas.java
index 9bd38c5..646ecf8 100755 (executable)
@@ -683,6 +683,52 @@ public class SeqCanvas extends JComponent implements ViewportListenerI
     }
   }
 
+  public void drawWrappedSelection(Graphics2D g, SequenceGroup group,
+          int canvasWidth,
+          int canvasHeight, int startRes)
+  {
+    int hgap = charHeight;
+    if (av.getScaleAboveWrapped())
+    {
+      hgap += charHeight;
+    }
+
+    int cWidth = (canvasWidth - LABEL_EAST - LABEL_WEST) / charWidth;
+    int cHeight = av.getAlignment().getHeight() * charHeight;
+
+    int endx;
+    int ypos = hgap;
+    int maxwidth = av.getAlignment().getWidth();
+
+    if (av.hasHiddenColumns())
+    {
+      maxwidth = av.getAlignment().getHiddenColumns()
+              .findColumnPosition(maxwidth);
+    }
+
+    while ((ypos <= canvasHeight) && (startRes < maxwidth))
+    {
+      endx = startRes + cWidth - 1;
+
+      if (endx > maxwidth)
+      {
+        endx = maxwidth;
+      }
+
+      g.translate(LABEL_WEST, 0);
+
+      drawUnwrappedSelection(g, group, startRes, endx, 0,
+              av.getAlignment().getHeight() - 1,
+              ypos);
+
+      g.translate(-LABEL_WEST, 0);
+
+      ypos += cHeight + getAnnotationHeight() + hgap;
+
+      startRes += cWidth;
+    }
+  }
+
   AnnotationPanel annotations;
 
   int getAnnotationHeight()
@@ -781,8 +827,6 @@ public class SeqCanvas extends JComponent implements ViewportListenerI
 
   }
 
-  // int startRes, int endRes, int startSeq, int endSeq, int x, int y,
-  // int x1, int x2, int y1, int y2, int startx, int starty,
   private void draw(Graphics g, int startRes, int endRes, int startSeq,
           int endSeq, int offset)
   {
@@ -884,7 +928,8 @@ public class SeqCanvas extends JComponent implements ViewportListenerI
           sx = (group.getStartRes() - startRes) * charWidth;
           sy = offset + ((i - startSeq) * charHeight);
           // width of group in pixels
-          ex = (((group.getEndRes() + 1) - group.getStartRes()) * charWidth) - 1;
+          ex = (((group.getEndRes() + 1) - group.getStartRes()) * charWidth)
+                  - 1;
 
           if (sx + ex < 0 || sx > visWidth)
           {
@@ -946,7 +991,6 @@ public class SeqCanvas extends JComponent implements ViewportListenerI
               {
                 ex = visWidth;
               }
-
               else if (sx + ex >= (endRes - startRes + 1) * charWidth)
               {
                 ex = (endRes - startRes + 1) * charWidth;
@@ -1052,10 +1096,10 @@ public class SeqCanvas extends JComponent implements ViewportListenerI
       // nothing to draw
       return null;
     }
-    
+
     // set up drawing colour
     Graphics2D g = (Graphics2D) selectionImage.getGraphics();
-    g.translate(LABEL_WEST, 0);
+    // g.translate(LABEL_WEST, 0);
     // set background to transparent
     g.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR, 0.0f));
     g.fillRect(0, 0, selectionImage.getWidth(), selectionImage.getHeight());
@@ -1066,9 +1110,28 @@ public class SeqCanvas extends JComponent implements ViewportListenerI
     { 5f, 3f }, 0f));
     g.setColor(Color.RED);
 
+    if (!av.getWrapAlignment())
+    {
+      drawUnwrappedSelection(g, group, startRes, endRes, startSeq, endSeq,
+              0);
+    }
+    else
+    {
+      drawWrappedSelection(g, group, getWidth(), getHeight(),
+              av.getRanges().getStartRes());
+    }
+
+    g.dispose();
+    return selectionImage;
+  }
+
+  private void drawUnwrappedSelection(Graphics2D g, SequenceGroup group,
+          int startRes, int endRes, int startSeq, int endSeq, int offset)
+  {
     if (!av.hasHiddenColumns())
     {
-      drawSelectionGroupPart(g, group, startRes, endRes, startSeq, endSeq);
+      drawSelectionGroupPart(g, group, startRes, endRes, startSeq, endSeq,
+              offset);
     }
     else
     {
@@ -1092,9 +1155,8 @@ public class SeqCanvas extends JComponent implements ViewportListenerI
         blockEnd = hideStart - 1;
 
         g.translate(screenY * charWidth, 0);
-
         drawSelectionGroupPart(g, group,
-                blockStart, blockEnd, startSeq, endSeq);
+                blockStart, blockEnd, startSeq, endSeq, offset);
 
         g.translate(-screenY * charWidth, 0);
         screenY += blockEnd - blockStart + 1;
@@ -1113,47 +1175,20 @@ public class SeqCanvas extends JComponent implements ViewportListenerI
         blockEnd = blockStart + (endRes - startRes) - screenY;
         g.translate(screenY * charWidth, 0);
         drawSelectionGroupPart(g, group,
-                blockStart, blockEnd, startSeq, endSeq);
+                blockStart, blockEnd, startSeq, endSeq, offset);
         
         g.translate(-screenY * charWidth, 0);
       }
     }
-    g.translate(-LABEL_WEST, 0);
-    return selectionImage;
   }
 
   /*
    * Draw the selection group as a separate image and overlay
    */
   private void drawSelectionGroupPart(Graphics2D g, SequenceGroup group,
-          int startRes, int endRes, int startSeq, int endSeq)
+          int startRes, int endRes, int startSeq, int endSeq,
+          int verticalOffset)
   {
-    // set up values in case the alignment is wrapped
-    int verticalOffset = 0;
-    int horizontalOffset = 0;
-    if (av.getWrapAlignment())
-    {
-      int hgap = charHeight;
-      if (av.getScaleAboveWrapped())
-      {
-        hgap += charHeight;
-      }
-      
-      // get the start res of the group and work out the offsets for it in the wrapped alignment
-      int groupstart = group.getStartRes();
-      int cWidth = (getWidth() - LABEL_EAST - LABEL_WEST) / charWidth;
-      
-      // group is in which slice of alignment? res position / width in residues
-      int slice = groupstart / cWidth;
-      // vertical offset is increased by slice number * number of sequences * height of each sequence
-      verticalOffset = slice * (av.getAlignment().getHeight() * charHeight
-              + getAnnotationHeight() + hgap) + hgap;
-
-      // horizontal offset is number of residues to subtract from group residue
-      // position
-      horizontalOffset = (slice * cWidth);
-    }
-
     int visWidth = (endRes - startRes + 1) * charWidth;
 
     int oldY = -1;
@@ -1164,20 +1199,20 @@ public class SeqCanvas extends JComponent implements ViewportListenerI
 
     int sx = -1;
     int sy = -1;
-    int ex = -1;
-
-    // position of start residue of group relative to startRes, in pixels
-    sx = (group.getStartRes() - startRes - horizontalOffset) * charWidth;
-
-    // width of group in pixels
-    ex = (((group.getEndRes() + 1) - group.getStartRes() - horizontalOffset)
-            * charWidth) - 1;
+    int xwidth = -1;
 
     for (i = startSeq; i <= endSeq; i++)
     {
+      // position of start residue of group relative to startRes, in pixels
+      sx = (group.getStartRes() - startRes) * charWidth;
+
+      // width of group in pixels
+      xwidth = (((group.getEndRes() + 1) - group.getStartRes()) * charWidth)
+              - 1;
+
       sy = verticalOffset + (i - startSeq) * charHeight;
 
-      if (sx + ex < 0 || sx > visWidth)
+      if (sx + xwidth < 0 || sx > visWidth)
       {
         continue;
       }
@@ -1217,38 +1252,33 @@ public class SeqCanvas extends JComponent implements ViewportListenerI
 
           // if end position is visible, draw vertical line to right of
           // group
-          if (sx + ex < visWidth)
+          if (sx + xwidth < visWidth)
           {
-            g.drawLine(sx + ex, oldY, sx + ex, sy);
+            g.drawLine(sx + xwidth, oldY, sx + xwidth, sy);
           }
 
           if (sx < 0)
           {
-            ex += sx;
+            xwidth += sx;
             sx = 0;
           }
 
-          if (sx + ex > visWidth)
-          {
-            ex = visWidth;
-          }
-
-          else if (sx + ex >= (endRes - startRes + 1) * charWidth)
+          if (sx + xwidth >= (endRes - startRes + 1) * charWidth)
           {
-            ex = (endRes - startRes + 1) * charWidth;
+            xwidth = (endRes - startRes + 1) * charWidth - sx;
           }
-
+          
           // draw horizontal line at top of group
           if (top != -1)
           {
-            g.drawLine(sx, top, sx + ex, top);
+            g.drawLine(sx, top, sx + xwidth, top);
             top = -1;
           }
 
           // draw horizontal line at bottom of group
           if (bottom != -1)
           {
-            g.drawLine(sx, bottom, sx + ex, bottom);
+            g.drawLine(sx, bottom, sx + xwidth, bottom);
             bottom = -1;
           }
 
@@ -1265,35 +1295,35 @@ public class SeqCanvas extends JComponent implements ViewportListenerI
         g.drawLine(sx, oldY, sx, sy);
       }
 
-      if (sx + ex < visWidth)
+      if (sx + xwidth < visWidth)
       {
-        g.drawLine(sx + ex, oldY, sx + ex, sy);
+        g.drawLine(sx + xwidth, oldY, sx + xwidth, sy);
       }
 
       if (sx < 0)
       {
-        ex += sx;
+        xwidth += sx;
         sx = 0;
       }
 
-      if (sx + ex > visWidth)
+      if (sx + xwidth > visWidth)
       {
-        ex = visWidth;
+        xwidth = visWidth;
       }
-      else if (sx + ex >= (endRes - startRes + 1) * charWidth)
+      else if (sx + xwidth >= (endRes - startRes + 1) * charWidth)
       {
-        ex = (endRes - startRes + 1) * charWidth;
+        xwidth = (endRes - startRes + 1) * charWidth;
       }
 
       if (top != -1)
       {
-        g.drawLine(sx, top, sx + ex, top);
+        g.drawLine(sx, top, sx + xwidth, top);
         top = -1;
       }
 
       if (bottom != -1)
       {
-        g.drawLine(sx, bottom - 1, sx + ex, bottom - 1);
+        g.drawLine(sx, bottom - 1, sx + xwidth, bottom - 1);
         bottom = -1;
       }