From: kiramt Date: Tue, 18 Jul 2017 13:45:38 +0000 (+0100) Subject: JAL-2611 Tidies, initial unit tests X-Git-Tag: Release_2_10_3b1~132^2~30^2~2 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=58fb14c48ecb086be7422f92addf14f930656523;p=jalview.git JAL-2611 Tidies, initial unit tests --- diff --git a/src/jalview/viewmodel/OverviewDimensions.java b/src/jalview/viewmodel/OverviewDimensions.java index caa36fc..ee3b511 100644 --- a/src/jalview/viewmodel/OverviewDimensions.java +++ b/src/jalview/viewmodel/OverviewDimensions.java @@ -58,10 +58,6 @@ public abstract class OverviewDimensions protected int alheight; - protected int fixedX; - - protected int fixedY; - /** * Create an OverviewDimensions object * @@ -195,13 +191,42 @@ public abstract class OverviewDimensions public abstract void updateViewportFromMouse(int mousex, int mousey, HiddenSequences hiddenSeqs, HiddenColumns hiddenCols); + /** + * Update the viewport location from a mouse drag within the overview's box + * + * @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 adjustViewportFromMouse(int mousex, int mousey, HiddenSequences hiddenSeqs, HiddenColumns hiddenCols); + /** + * Initialise dragging from the mouse - must be called on initial mouse click + * before using adjustViewportFromMouse in drag operations + * + * @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 setDragPoint(int x, int y, HiddenSequences hiddenSeqs, HiddenColumns hiddenCols); - protected abstract void updateViewportFromTopLeft(int mousex, int mousey, + /* + * Move the viewport so that the top left corner of the overview's box + * is at the mouse position (leftx, topy) + */ + protected abstract void updateViewportFromTopLeft(int leftx, int topy, HiddenSequences hiddenSeqs, HiddenColumns hiddenCols); /** @@ -238,6 +263,9 @@ public abstract class OverviewDimensions */ protected abstract void resetAlignmentDims(); + /* + * Given the box coordinates in residues and sequences, set the box dimensions in the overview window + */ protected void setBoxPosition(int startRes, int startSeq, int vpwidth, int vpheight) { @@ -252,8 +280,6 @@ public abstract class OverviewDimensions // boxHeight is the height in sequences translated to pixels boxHeight = Math.round((float) vpheight * sequencesHeight / alheight); - - System.out.println("Update box: x: " + boxX); } /** @@ -271,8 +297,14 @@ public abstract class OverviewDimensions && y < boxY + boxHeight); } + /* + * Given the centre x position, calculate the box's left x position + */ protected abstract int getLeftXFromCentreX(int mousex, HiddenColumns hidden); + /* + * Given the centre y position, calculate the box's top y position + */ protected abstract int getTopYFromCentreY(int mousey, HiddenSequences hidden); diff --git a/src/jalview/viewmodel/OverviewDimensionsHideHidden.java b/src/jalview/viewmodel/OverviewDimensionsHideHidden.java index 513596e..7eec343 100644 --- a/src/jalview/viewmodel/OverviewDimensionsHideHidden.java +++ b/src/jalview/viewmodel/OverviewDimensionsHideHidden.java @@ -12,6 +12,12 @@ public class OverviewDimensionsHideHidden extends OverviewDimensions { private ViewportRanges ranges; + private int xdiff; // when dragging, difference in alignment units between + // start residue and original mouse click position + + private int ydiff; // when dragging, difference in alignment units between + // start sequence and original mouse click position + public OverviewDimensionsHideHidden(ViewportRanges vpranges, boolean showAnnotationPanel) { @@ -41,16 +47,17 @@ public class OverviewDimensionsHideHidden extends OverviewDimensions int vpx = Math.round((float) mousex * alwidth / width); int vpy = Math.round((float) mousey * alheight / sequencesHeight); - updateViewportFromTopLeft(vpx + fixedX, vpy + fixedY, hiddenSeqs, + updateViewportFromTopLeft(vpx + xdiff, vpy + ydiff, hiddenSeqs, hiddenCols); } @Override - protected void updateViewportFromTopLeft(int xAsRes, int yAsSeq, + protected void updateViewportFromTopLeft(int leftx, int topy, HiddenSequences hiddenSeqs, HiddenColumns hiddenCols) { - + int xAsRes = leftx; + int yAsSeq = topy; resetAlignmentDims(); if (xAsRes < 0) @@ -63,16 +70,7 @@ public class OverviewDimensionsHideHidden extends OverviewDimensions yAsSeq = 0; } - // - // Convert x value to residue position - // - - // need to determine where scrollCol should be, given x - // to do this also need to know width of viewport, and some hidden column - // correction - - // convert x to residues - this is an absolute position - // int xAsRes = Math.round((float) x * alwidth / width); + // Determine where scrollCol should be, given visXAsRes // get viewport width in residues int vpwidth = ranges.getViewportWidth(); @@ -92,10 +90,7 @@ public class OverviewDimensionsHideHidden extends OverviewDimensions } } - - // - // Convert y value to sequence position - // + // Determine where scrollRow should be, given visYAsSeq // get viewport height in sequences // add 1 because height includes both endSeq and startSeq @@ -117,7 +112,6 @@ public class OverviewDimensionsHideHidden extends OverviewDimensions // update viewport ranges.setStartRes(xAsRes); ranges.setStartSeq(yAsSeq); - } @Override @@ -168,14 +162,12 @@ public class OverviewDimensionsHideHidden extends OverviewDimensions HiddenColumns hiddenCols) { // get alignment position of x and box (can get directly from vpranges) and - // calc difference + // calculate difference between the positions int vpx = Math.round((float) x * alwidth / width); - int vpy = Math.round((float) y * alheight / sequencesHeight); - fixedX = ranges.getStartRes() - vpx; - fixedY = ranges.getStartSeq() - vpy; - + xdiff = ranges.getStartRes() - vpx; + ydiff = ranges.getStartSeq() - vpy; } } diff --git a/src/jalview/viewmodel/OverviewDimensionsShowHidden.java b/src/jalview/viewmodel/OverviewDimensionsShowHidden.java index efffe6f..c4b69a8 100644 --- a/src/jalview/viewmodel/OverviewDimensionsShowHidden.java +++ b/src/jalview/viewmodel/OverviewDimensionsShowHidden.java @@ -32,6 +32,12 @@ public class OverviewDimensionsShowHidden extends OverviewDimensions { private ViewportRanges ranges; + private int xdiff; // when dragging, difference in alignment units between + // start residue and original mouse click position + + private int ydiff; // when dragging, difference in alignment units between + // start sequence and original mouse click position + /** * Create an OverviewDimensions object * @@ -66,14 +72,18 @@ public class OverviewDimensionsShowHidden extends OverviewDimensions public void updateViewportFromMouse(int mousex, int mousey, HiddenSequences hiddenSeqs, HiddenColumns hiddenCols) { + // convert mousex and mousey to alignment units as well as + // translating to top left corner of viewport - this is an absolute position int xAsRes = getLeftXFromCentreX(mousex, hiddenCols); int yAsSeq = getTopYFromCentreY(mousey, hiddenSeqs); + // convert to visible positions int visXAsRes = hiddenCols.findColumnPosition(xAsRes); yAsSeq = hiddenSeqs.adjustForHiddenSeqs( hiddenSeqs.findIndexWithoutHiddenSeqs(yAsSeq)); int visYAsSeq = hiddenSeqs.findIndexWithoutHiddenSeqs(yAsSeq); + // update viewport accordingly updateViewportFromTopLeft(visXAsRes, visYAsSeq, hiddenSeqs, hiddenCols); } @@ -86,21 +96,23 @@ public class OverviewDimensionsShowHidden extends OverviewDimensions // coords, // convert back to pixel coords int vpx = Math.round((float) mousex * alwidth / width); - int visXAsRes = hiddenCols.findColumnPosition(vpx) + fixedX; + int visXAsRes = hiddenCols.findColumnPosition(vpx) + xdiff; int vpy = Math.round((float) mousey * alheight / sequencesHeight); - int visYAsRes = hiddenSeqs.findIndexWithoutHiddenSeqs(vpy) + fixedY; + int visYAsRes = hiddenSeqs.findIndexWithoutHiddenSeqs(vpy) + ydiff; + // update viewport accordingly updateViewportFromTopLeft(visXAsRes, visYAsRes, hiddenSeqs, hiddenCols); } @Override - protected void updateViewportFromTopLeft(int visXAsRes, int visYAsSeq, + protected void updateViewportFromTopLeft(int leftx, int topy, HiddenSequences hiddenSeqs, HiddenColumns hiddenCols) { - + int visXAsRes = leftx; + int visYAsSeq = topy; resetAlignmentDims(); if (visXAsRes < 0) @@ -113,26 +125,11 @@ public class OverviewDimensionsShowHidden extends OverviewDimensions visYAsSeq = 0; } - // - // Convert x value to residue position - // - - // need to determine where scrollCol should be, given x - // to do this also need to know width of viewport, and some hidden column - // correction - - // convert x to residues - this is an absolute position - // int xAsRes = Math.round((float) x * alwidth / width); + // Determine where scrollCol should be, given visXAsRes // get viewport width in residues int vpwidth = ranges.getViewportWidth(); - // get where x should be when accounting for hidden cols - // if x is in a hidden col region, shift to left - but we still need - // absolute position - // so convert back after getting visible region position - - // check in case we went off the edge of the alignment int visAlignWidth = hiddenCols.findColumnPosition(alwidth - 1); if (visXAsRes + vpwidth - 1 > visAlignWidth) @@ -151,22 +148,11 @@ public class OverviewDimensionsShowHidden extends OverviewDimensions } } - // - // Convert y value to sequence position - // - - // convert y to residues - // int yAsSeq = Math.round((float) y * alheight / sequencesHeight); + // Determine where scrollRow should be, given visYAsSeq // get viewport height in sequences int vpheight = ranges.getViewportHeight(); - // get where y should be when accounting for hidden rows - // if y is in a hidden row region, shift up - but we still need absolute - // position, - // so convert back after getting visible region position - - // check in case we went off the edge of the alignment int visAlignHeight = hiddenSeqs.findIndexWithoutHiddenSeqs(alheight); @@ -187,7 +173,6 @@ public class OverviewDimensionsShowHidden extends OverviewDimensions // update viewport ranges.setStartRes(visXAsRes); ranges.setStartSeq(visYAsSeq); - } /** @@ -205,7 +190,6 @@ public class OverviewDimensionsShowHidden extends OverviewDimensions public void setBoxPosition(HiddenSequences hiddenSeqs, HiddenColumns hiddenCols) { - // work with absolute values of startRes and endRes int startRes = hiddenCols.adjustForHiddenColumns(ranges.getStartRes()); int endRes = hiddenCols.adjustForHiddenColumns(ranges.getEndRes()); @@ -214,8 +198,6 @@ public class OverviewDimensionsShowHidden extends OverviewDimensions int startSeq = hiddenSeqs.adjustForHiddenSeqs(ranges.getStartSeq()); int endSeq = hiddenSeqs.adjustForHiddenSeqs(ranges.getEndSeq()); - System.out.println("Update box: startres: " + startRes); - setBoxPosition(startRes, startSeq, endRes - startRes + 1, endSeq - startSeq + 1); } @@ -261,15 +243,14 @@ public class OverviewDimensionsShowHidden extends OverviewDimensions public void setDragPoint(int x, int y, HiddenSequences hiddenSeqs, HiddenColumns hiddenCols) { - { - // get alignment position of x and box (can get directly from vpranges) and calc difference - int vpx = Math.round((float) x * alwidth / width); - fixedX = ranges.getStartRes() - hiddenCols.findColumnPosition(vpx); - - int vpy = Math.round((float) y * alheight / sequencesHeight); - fixedY = ranges.getStartSeq() - - hiddenSeqs.findIndexWithoutHiddenSeqs(vpy); - } + // get alignment position of x and box (can get directly from vpranges) and + // calculate difference between the positions + int vpx = Math.round((float) x * alwidth / width); + int vpy = Math.round((float) y * alheight / sequencesHeight); + + xdiff = ranges.getStartRes() - hiddenCols.findColumnPosition(vpx); + ydiff = ranges.getStartSeq() + - hiddenSeqs.findIndexWithoutHiddenSeqs(vpy); } } diff --git a/test/jalview/viewmodel/OverviewDimensionsHideHiddenTest.java b/test/jalview/viewmodel/OverviewDimensionsHideHiddenTest.java index ebafba6..32d0ab8 100644 --- a/test/jalview/viewmodel/OverviewDimensionsHideHiddenTest.java +++ b/test/jalview/viewmodel/OverviewDimensionsHideHiddenTest.java @@ -954,6 +954,28 @@ public class OverviewDimensionsHideHiddenTest assertFalse(od.isPositionInBox(75, 20)); } + /** + * Test the dragging functionality + */ + @Test(groups = { "Functional" }) + public void testDragging() + { + od.updateViewportFromMouse(0, 0, al.getHiddenSequences(), hiddenCols); + od.setDragPoint(4, 16, al.getHiddenSequences(), + hiddenCols); + od.adjustViewportFromMouse(20, 22, + al.getHiddenSequences(), hiddenCols); + + // updates require an OverviewPanel to exist which it doesn't here + // so call setBoxPosition() as it would be called by the AlignmentPanel + // normally + od.setBoxPosition(al.getHiddenSequences(), hiddenCols); + + // corner moves 16 (20-4) right and 6 (22-16) up + assertEquals(od.getBoxX(), 16); + assertEquals(od.getBoxY(), 6); + } + /* * Move viewport horizontally: startRes + previous width gives new horizontal extent. Vertical extent stays the same. */ diff --git a/test/jalview/viewmodel/OverviewDimensionsShowHiddenTest.java b/test/jalview/viewmodel/OverviewDimensionsShowHiddenTest.java index 0c975cb..716659c 100644 --- a/test/jalview/viewmodel/OverviewDimensionsShowHiddenTest.java +++ b/test/jalview/viewmodel/OverviewDimensionsShowHiddenTest.java @@ -991,6 +991,26 @@ public class OverviewDimensionsShowHiddenTest assertTrue(od.isPositionInBox(47, 10)); } + /** + * Test the dragging functionality + */ + @Test(groups = { "Functional" }) + public void testDragging() + { + od.updateViewportFromMouse(0, 0, al.getHiddenSequences(), hiddenCols); + od.setDragPoint(4, 16, al.getHiddenSequences(), hiddenCols); + od.adjustViewportFromMouse(20, 22, al.getHiddenSequences(), hiddenCols); + + // updates require an OverviewPanel to exist which it doesn't here + // so call setBoxPosition() as it would be called by the AlignmentPanel + // normally + od.setBoxPosition(al.getHiddenSequences(), hiddenCols); + + // corner moves 16 (20-4) right and 6 (22-16) up + assertEquals(od.getBoxX(), 16); + assertEquals(od.getBoxY(), 6); + } + /* * Move viewport horizontally: startRes + previous width gives new horizontal extent. Vertical extent stays the same. */