2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
21 package jalview.viewmodel;
23 import jalview.datamodel.AlignmentI;
24 import jalview.datamodel.HiddenColumns;
27 * Supplies and updates viewport properties relating to position such as: start
28 * and end residues and sequences; ideally will serve hidden columns/rows too.
29 * Intention also to support calculations for positioning, scrolling etc. such
30 * as finding the middle of the viewport, checking for scrolls off screen
32 public class ViewportRanges extends ViewportProperties
34 public static final String STARTRES = "startres";
36 public static final String ENDRES = "endres";
38 public static final String STARTSEQ = "startseq";
40 public static final String ENDSEQ = "endseq";
42 public static final String STARTRESANDSEQ = "startresandseq";
44 public static final String MOVE_VIEWPORT = "move_viewport";
46 private boolean wrappedMode = false;
48 // start residue of viewport
51 // end residue of viewport
54 // start sequence of viewport
57 // end sequence of viewport
61 private AlignmentI al;
67 * the viewport's alignment
69 public ViewportRanges(AlignmentI alignment)
71 // initial values of viewport settings
73 this.endRes = alignment.getWidth() - 1;
75 this.endSeq = alignment.getHeight() - 1;
80 * Get alignment width in cols, including hidden cols
82 public int getAbsoluteAlignmentWidth()
88 * Get alignment height in rows, including hidden rows
90 public int getAbsoluteAlignmentHeight()
92 return al.getHeight() + al.getHiddenSequences().getSize();
96 * Get alignment width in cols, excluding hidden cols
98 public int getVisibleAlignmentWidth()
100 return al.getVisibleWidth();
104 * Get alignment height in rows, excluding hidden rows
106 public int getVisibleAlignmentHeight()
108 return al.getHeight();
112 * Set first residue visible in the viewport, and retain the current width.
113 * Fires a property change event.
118 public void setStartRes(int res)
120 int width = getViewportWidth();
121 setStartEndRes(res, res + width - 1);
125 * Set start and end residues at the same time. This method only fires one
126 * event for the two changes, and should be used in preference to separate
127 * calls to setStartRes and setEndRes.
134 public void setStartEndRes(int start, int end)
136 int[] oldvalues = updateStartEndRes(start, end);
137 int oldstartres = oldvalues[0];
138 int oldendres = oldvalues[1];
140 changeSupport.firePropertyChange(STARTRES, oldstartres, startRes);
141 if (oldstartres == startRes)
143 // event won't be fired if start positions are same
144 // fire an event for the end positions in case they changed
145 changeSupport.firePropertyChange(ENDRES, oldendres, endRes);
150 * Update start and end residue values, adjusting for width constraints if
157 * @return array containing old start and end residue values
159 private int[] updateStartEndRes(int start, int end)
161 int oldstartres = this.startRes;
164 * if not wrapped, don't leave white space at the right margin
166 int lastColumn = getVisibleAlignmentWidth() - 1;
167 if (!wrappedMode && (start > lastColumn))
169 startRes = Math.max(lastColumn, 0);
180 int oldendres = this.endRes;
185 else if (!wrappedMode && (end > lastColumn))
187 endRes = Math.max(lastColumn, 0);
193 return new int[] { oldstartres, oldendres };
197 * Set the first sequence visible in the viewport, maintaining the height. If
198 * the viewport would extend past the last sequence, sets the viewport so it
199 * sits at the bottom of the alignment. Fires a property change event.
204 public void setStartSeq(int seq)
207 int height = getViewportHeight();
208 if (startseq + height - 1 > getVisibleAlignmentHeight() - 1)
210 startseq = getVisibleAlignmentHeight() - height;
212 setStartEndSeq(startseq, startseq + height - 1);
216 * Set start and end sequences at the same time. The viewport height may
217 * change. This method only fires one event for the two changes, and should be
218 * used in preference to separate calls to setStartSeq and setEndSeq.
225 public void setStartEndSeq(int start, int end)
227 // jalview.bin.Console.outPrintln("ViewportRange setStartEndSeq " + start +
229 int[] oldvalues = updateStartEndSeq(start, end);
230 int oldstartseq = oldvalues[0];
231 int oldendseq = oldvalues[1];
233 changeSupport.firePropertyChange(STARTSEQ, oldstartseq, startSeq);
234 if (oldstartseq == startSeq)
236 // event won't be fired if start positions are the same
237 // fire in case the end positions changed
238 changeSupport.firePropertyChange(ENDSEQ, oldendseq, endSeq);
243 * Update start and end sequence values, adjusting for height constraints if
250 * @return array containing old start and end sequence values
252 private int[] updateStartEndSeq(int start, int end)
254 int oldstartseq = this.startSeq;
255 int visibleHeight = getVisibleAlignmentHeight();
256 if (start > visibleHeight - 1)
258 startSeq = Math.max(visibleHeight - 1, 0);
269 int oldendseq = this.endSeq;
270 if (end >= visibleHeight)
272 endSeq = Math.max(visibleHeight - 1, 0);
282 return new int[] { oldstartseq, oldendseq };
286 * Set the last sequence visible in the viewport. Fires a property change
290 * sequence position in the range [0, height)
292 public void setEndSeq(int seq)
294 // BH 2018.04.18 added safety for seq < 0; comment about not being >= height
295 setStartEndSeq(Math.max(0, seq + 1 - getViewportHeight()), seq);
299 * Set start residue and start sequence together (fires single event). The
300 * event supplies a pair of old values and a pair of new values: [old start
301 * residue, old start sequence] and [new start residue, new start sequence]
308 public void setStartResAndSeq(int res, int seq)
310 int width = getViewportWidth();
311 int[] oldresvalues = updateStartEndRes(res, res + width - 1);
314 int height = getViewportHeight();
315 if (startseq + height - 1 > getVisibleAlignmentHeight() - 1)
317 startseq = getVisibleAlignmentHeight() - height;
319 int[] oldseqvalues = updateStartEndSeq(startseq, startseq + height - 1);
321 int[] old = new int[] { oldresvalues[0], oldseqvalues[0] };
322 int[] newresseq = new int[] { startRes, startSeq };
323 changeSupport.firePropertyChange(STARTRESANDSEQ, old, newresseq);
327 * Get start residue of viewport
329 public int getStartRes()
335 * Get end residue of viewport
337 public int getEndRes()
343 * Get start sequence of viewport
345 public int getStartSeq()
351 * Get end sequence of viewport
353 public int getEndSeq()
359 * Set viewport width in residues, without changing startRes. Use in
360 * preference to calculating endRes from the width, to avoid out by one
361 * errors! Fires a property change event.
366 public void setViewportWidth(int w)
368 setStartEndRes(startRes, startRes + w - 1);
372 * Set viewport height in residues, without changing startSeq. Use in
373 * preference to calculating endSeq from the height, to avoid out by one
374 * errors! Fires a property change event.
377 * height in sequences
379 public void setViewportHeight(int h)
381 setStartEndSeq(startSeq, startSeq + h - 1);
385 * Set viewport horizontal start position and width. Use in preference to
386 * calculating endRes from the width, to avoid out by one errors! Fires a
387 * property change event.
394 public void setViewportStartAndWidth(int start, int w)
403 * if not wrapped, don't leave white space at the right margin
407 if ((w <= getVisibleAlignmentWidth())
408 && (vpstart + w - 1 > getVisibleAlignmentWidth() - 1))
410 vpstart = getVisibleAlignmentWidth() - w;
414 setStartEndRes(vpstart, vpstart + w - 1);
418 * Set viewport vertical start position and height. Use in preference to
419 * calculating endSeq from the height, to avoid out by one errors! Fires a
420 * property change event.
425 * height in sequences
427 public void setViewportStartAndHeight(int start, int h)
431 int visHeight = getVisibleAlignmentHeight();
436 else if (h <= visHeight && vpstart + h > visHeight)
437 // viewport height is less than the full alignment and we are running off
440 vpstart = visHeight - h;
442 // jalview.bin.Console.outPrintln("ViewportRanges setviewportStartAndHeight
444 // + " " + start + " " + h + " " + getVisibleAlignmentHeight());
446 setStartEndSeq(vpstart, vpstart + h - 1);
450 * Get width of viewport in residues
452 * @return width of viewport
454 public int getViewportWidth()
456 return (endRes - startRes + 1);
460 * Get height of viewport in residues
462 * @return height of viewport
464 public int getViewportHeight()
466 return (endSeq - startSeq + 1);
470 * Scroll the viewport range vertically. Fires a property change event.
473 * true if scrolling up, false if down
475 * @return true if the scroll is valid
477 public boolean scrollUp(boolean up)
480 * if in unwrapped mode, scroll up or down one sequence row;
481 * if in wrapped mode, scroll by one visible width of columns
495 setStartSeq(startSeq - 1);
506 if (endSeq >= getVisibleAlignmentHeight() - 1)
510 setStartSeq(startSeq + 1);
517 * Scroll the viewport range horizontally. Fires a property change event.
520 * true if scrolling right, false if left
522 * @return true if the scroll is valid
524 public boolean scrollRight(boolean right)
533 setStartRes(startRes - 1);
537 if (endRes >= getVisibleAlignmentWidth() - 1)
542 setStartRes(startRes + 1);
549 * Scroll a wrapped alignment so that the specified residue is in the first
550 * repeat of the wrapped view. Fires a property change event. Answers true if
551 * the startRes changed, else false.
554 * residue position to scroll to NB visible position not absolute
558 public boolean scrollToWrappedVisible(int res)
560 int newStartRes = calcWrappedStartResidue(res);
561 if (newStartRes == startRes)
565 setStartRes(newStartRes);
571 * Calculate wrapped start residue from visible start residue
574 * visible start residue
575 * @return left column of panel res will be located in
577 private int calcWrappedStartResidue(int res)
579 int oldStartRes = startRes;
580 int width = getViewportWidth();
582 boolean up = res < oldStartRes;
583 int widthsToScroll = Math.abs((res - oldStartRes) / width);
589 int residuesToScroll = width * widthsToScroll;
590 int newStartRes = up ? oldStartRes - residuesToScroll
591 : oldStartRes + residuesToScroll;
600 * Scroll so that (x,y) is visible. Fires a property change event.
603 * x position in alignment (absolute position)
605 * y position in alignment (absolute position)
607 public void scrollToVisible(int x, int y)
618 HiddenColumns hidden = al.getHiddenColumns();
619 while (x < hidden.visibleToAbsoluteColumn(startRes))
621 if (!scrollRight(false))
626 while (x > hidden.visibleToAbsoluteColumn(endRes))
628 if (!scrollRight(true))
636 * Set the viewport location so that a position is visible
639 * column to be visible: absolute position in alignment
641 * row to be visible: absolute position in alignment
643 public boolean setViewportLocation(int x, int y)
645 boolean changedLocation = false;
647 // convert the x,y location to visible coordinates
648 int visX = al.getHiddenColumns().absoluteToVisibleColumn(x);
649 int visY = al.getHiddenSequences().findIndexWithoutHiddenSeqs(y);
651 // if (vis_x,vis_y) is already visible don't do anything
652 if (startRes > visX || visX > endRes
653 || startSeq > visY && visY > endSeq)
655 int[] old = new int[] { startRes, startSeq };
659 int newstartres = calcWrappedStartResidue(visX);
660 setStartRes(newstartres);
661 newresseq = new int[] { startRes, startSeq };
665 // set the viewport x location to contain vis_x
666 int newstartres = visX;
667 int width = getViewportWidth();
668 if (newstartres + width - 1 > getVisibleAlignmentWidth() - 1)
670 newstartres = getVisibleAlignmentWidth() - width;
672 updateStartEndRes(newstartres, newstartres + width - 1);
674 // set the viewport y location to contain vis_y
675 int newstartseq = visY;
676 int height = getViewportHeight();
677 if (newstartseq + height - 1 > getVisibleAlignmentHeight() - 1)
679 newstartseq = getVisibleAlignmentHeight() - height;
681 updateStartEndSeq(newstartseq, newstartseq + height - 1);
683 newresseq = new int[] { startRes, startSeq };
685 changedLocation = true;
686 changeSupport.firePropertyChange(MOVE_VIEWPORT, old, newresseq);
688 return changedLocation;
692 * Adjust sequence position for page up. Fires a property change event.
698 setStartRes(Math.max(0, getStartRes() - getViewportWidth()));
702 setViewportStartAndHeight(startSeq - (endSeq - startSeq),
703 getViewportHeight());
708 * Adjust sequence position for page down. Fires a property change event.
710 public void pageDown()
715 * if height is more than width (i.e. not all sequences fit on screen),
716 * increase page down to height
718 int newStart = getStartRes()
719 + Math.max(getViewportHeight(), getViewportWidth());
722 * don't page down beyond end of alignment, or if not all
723 * sequences fit in the visible height
725 if (newStart < getVisibleAlignmentWidth())
727 setStartRes(newStart);
732 setViewportStartAndHeight(endSeq, getViewportHeight());
736 public void setWrappedMode(boolean wrapped)
738 wrappedMode = wrapped;
741 public boolean isWrappedMode()
747 * Answers the vertical scroll position (0..) to set, given the visible column
748 * that is at top left.
752 * viewport width 40 columns (0-39, 40-79, 80-119...)
753 * column 0 returns scroll position 0
754 * columns 1-40 return scroll position 1
755 * columns 41-80 return scroll position 2
759 * @param topLeftColumn
763 public int getWrappedScrollPosition(final int topLeftColumn)
765 int w = getViewportWidth();
768 * visible whole widths
770 int scroll = topLeftColumn / w;
773 * add 1 for a part width if there is one
775 scroll += topLeftColumn % w > 0 ? 1 : 0;
781 * Answers the maximum wrapped vertical scroll value, given the column
782 * position (0..) to show at top left of the visible region.
784 * @param topLeftColumn
787 public int getWrappedMaxScroll(int topLeftColumn)
789 int scrollPosition = getWrappedScrollPosition(topLeftColumn);
792 * how many more widths could be drawn after this one?
794 int columnsRemaining = getVisibleAlignmentWidth() - topLeftColumn;
795 int width = getViewportWidth();
796 int widthsRemaining = columnsRemaining / width
797 + (columnsRemaining % width > 0 ? 1 : 0) - 1;
798 int maxScroll = scrollPosition + widthsRemaining;