X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fgui%2FOverviewCanvas.java;h=7371eb5e0bf79a44aac37b9a081000d1a58df858;hb=2ff9268291ebf9d1d8d10c0108ea4f8b50c2885f;hp=7d42e5e038e9c25565d3e481b17beaba5d77356b;hpb=0a2de5e40c8a65c535374f965e0f568b2f41fc17;p=jalview.git diff --git a/src/jalview/gui/OverviewCanvas.java b/src/jalview/gui/OverviewCanvas.java index 7d42e5e..7371eb5 100644 --- a/src/jalview/gui/OverviewCanvas.java +++ b/src/jalview/gui/OverviewCanvas.java @@ -1,8 +1,29 @@ +/* + * 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 . + * The Jalview Authors are detailed in the 'AUTHORS' file. + */ package jalview.gui; import jalview.api.AlignViewportI; -import jalview.datamodel.SequenceI; -import jalview.renderer.AnnotationRenderer; +import jalview.bin.Cache; +import jalview.renderer.OverviewRenderer; +import jalview.renderer.OverviewResColourFinder; import jalview.viewmodel.OverviewDimensions; import java.awt.Color; @@ -16,15 +37,17 @@ public class OverviewCanvas extends JComponent { private static final Color TRANS_GREY = new Color(100, 100, 100, 25); - private BufferedImage miniMe; + // This is set true if the alignment view changes whilst + // the overview is being calculated + private volatile boolean restart = false; - private BufferedImage lastMiniMe = null; + private volatile boolean updaterunning = false; - public boolean updating = false; + private boolean dispose = false; - // This is set true if the user resizes whilst - // the overview is being calculated - public volatile boolean updateAgain = false; + private BufferedImage miniMe; + + private BufferedImage lastMiniMe = null; // Can set different properties in this seqCanvas than // main visible SeqCanvas @@ -32,91 +55,127 @@ public class OverviewCanvas extends JComponent private jalview.renderer.seqfeatures.FeatureRenderer fr; - private final AnnotationRenderer renderer = new AnnotationRenderer(); + private OverviewDimensions od; - OverviewDimensions od; + private OverviewRenderer or = null; - OverviewPanel op; + private AlignViewportI av; - AlignViewport av; + private OverviewResColourFinder cf; - AlignmentPanel ap; + private ProgressPanel progressPanel; public OverviewCanvas(OverviewDimensions overviewDims, - AlignViewportI alignvp, AlignmentPanel alignp, OverviewPanel overp) + AlignViewportI alignvp, ProgressPanel pp) { od = overviewDims; - av = alignp.av; - ap = alignp; - op = overp; + av = alignvp; + progressPanel = pp; sr = new SequenceRenderer(av); sr.renderGaps = false; - sr.forOverview = true; - fr = new FeatureRenderer(ap); + fr = new jalview.renderer.seqfeatures.FeatureRenderer(av); + + boolean useLegacy = Cache.getDefault(Preferences.USE_LEGACY_GAP, false); + Color gapCol = Cache.getDefaultColour(Preferences.GAP_COLOUR, + jalview.renderer.OverviewResColourFinder.OVERVIEW_DEFAULT_GAP); + Color hiddenCol = Cache.getDefaultColour(Preferences.HIDDEN_COLOUR, + jalview.renderer.OverviewResColourFinder.OVERVIEW_DEFAULT_HIDDEN); + cf = new OverviewResColourFinder(useLegacy, gapCol, hiddenCol); + + setSize(od.getWidth(), od.getHeight()); + } + + /** + * Update the overview dimensions object used by the canvas (e.g. if we change + * from showing hidden columns to hiding them or vice versa) + * + * @param overviewDims + */ + public void resetOviewDims(OverviewDimensions overviewDims) + { + od = overviewDims; + } + + /** + * Signals to drawing code that the associated alignment viewport has changed + * and a redraw will be required + */ + public boolean restartDraw() + { + synchronized (this) + { + if (updaterunning) + { + restart = true; + if (or != null) + { + or.setRedraw(true); + } + } + else + { + updaterunning = true; + } + return restart; + } } - public void draw(boolean showSequenceFeatures, boolean showAnnotation) + /** + * Draw the overview sequences + * + * @param showSequenceFeatures + * true if sequence features are to be shown + * @param showAnnotation + * true if the annotation is to be shown + * @param transferRenderer + * the renderer to transfer feature colouring from + */ + public void draw(boolean showSequenceFeatures, boolean showAnnotation, + FeatureRenderer transferRenderer) { miniMe = null; if (showSequenceFeatures) { - fr.transferSettings(ap.getSeqPanel().seqCanvas.getFeatureRenderer()); + fr.transferSettings(transferRenderer); } - // why do we need to set preferred size again? was set in - // updateOverviewImage setPreferredSize(new Dimension(od.getWidth(), od.getHeight())); - miniMe = new BufferedImage(od.getWidth(), od.getHeight(), - BufferedImage.TYPE_INT_RGB); + or = new OverviewRenderer(fr, od, av.getAlignment(), + av.getResidueShading(), cf); - Graphics mg = miniMe.getGraphics(); - mg.setColor(Color.orange); - mg.fillRect(0, 0, od.getWidth(), miniMe.getHeight()); + or.addPropertyChangeListener(progressPanel); - // calculate sampleCol and sampleRow - // alignment width is max number of residues/bases - // alignment height is number of sequences - int alwidth = av.getAlignment().getWidth(); - int alheight = av.getAlignment().getAbsoluteHeight(); + miniMe = or.draw(od.getRows(av.getAlignment()), + od.getColumns(av.getAlignment())); - // sampleCol or sampleRow is the width/height allocated to each residue - // in particular, sometimes we may need more than one row/col of the - // BufferedImage allocated - // sampleCol is how much of a residue to assign to each pixel - // sampleRow is how many sequences to assign to each pixel - float sampleCol = alwidth / (float) od.getWidth(); - float sampleRow = alheight / (float) od.getSequencesHeight(); - - buildImage(sampleRow, sampleCol); + Graphics mg = miniMe.getGraphics(); if (showAnnotation) { - renderer.updateFromAlignViewport(av); - for (int col = 0; col < od.getWidth() && !updateAgain; col++) - { - mg.translate(col, od.getSequencesHeight()); - renderer.drawGraph(mg, av.getAlignmentConservationAnnotation(), - av.getAlignmentConservationAnnotation().annotations, - (int) (sampleCol) + 1, od.getGraphHeight(), - (int) (col * sampleCol), (int) (col * sampleCol) + 1); - mg.translate(-col, -od.getSequencesHeight()); - - } + mg.translate(0, od.getSequencesHeight()); + or.drawGraph(mg, av.getAlignmentConservationAnnotation(), + av.getCharWidth(), od.getGraphHeight(), + od.getColumns(av.getAlignment())); + mg.translate(0, -od.getSequencesHeight()); } System.gc(); - updating = false; - - if (updateAgain) + or.removePropertyChangeListener(progressPanel); + or = null; + if (restart) { - updateAgain = false; - op.updateOverviewImage(); + restart = false; + if (!dispose) + { + draw(showSequenceFeatures, showAnnotation, transferRenderer); + } } else { + updaterunning = false; lastMiniMe = miniMe; } } @@ -124,7 +183,7 @@ public class OverviewCanvas extends JComponent @Override public void paintComponent(Graphics g) { - if (updating || updateAgain) + if (restart) { if (lastMiniMe == null) { @@ -140,104 +199,71 @@ public class OverviewCanvas extends JComponent } else if (lastMiniMe != null) { - g.drawImage(lastMiniMe, 0, 0, this); - if (lastMiniMe != miniMe) + // is this a resize? + if ((getWidth() > 0) && (getHeight() > 0) + && ((getWidth() != od.getWidth()) + || (getHeight() != od.getHeight()))) { - g.setColor(TRANS_GREY); - g.fillRect(0, 0, getWidth(), getHeight()); - } - } - - g.setColor(Color.red); - od.drawBox(g); - } - - /* - * Build the overview panel image - */ - private void buildImage(float sampleRow, float sampleCol) - { - int lastcol = -1; - int lastrow = -1; - int color = Color.white.getRGB(); - - SequenceI seq = null; - - final boolean hasHiddenCols = av.hasHiddenColumns(); - boolean hiddenRow = false; - // get hidden row and hidden column map once at beginning. - // clone featureRenderer settings to avoid race conditions... if state is - // updated just need to refresh again - for (int row = 0; row < od.getSequencesHeight() && !updateAgain; row++) - { - boolean doCopy = true; - int currentrow = (int) (row * sampleRow); - if (currentrow != lastrow) - { - doCopy = false; - - lastrow = currentrow; - - // get the sequence which would be at alignment index 'lastrow' if no - // rows were hidden, and determine whether it is hidden or not - hiddenRow = av.getAlignment().isHidden(lastrow); - seq = av.getAlignment().getSequenceAtAbsoluteIndex(lastrow); - } - - for (int col = 0; col < od.getWidth() && !updateAgain; col++) - { - if (doCopy) + // if there is annotation, scale the alignment and annotation separately + if (od.getGraphHeight() > 0) { - color = miniMe.getRGB(col, row - 1); + BufferedImage topImage = lastMiniMe.getSubimage(0, 0, + od.getWidth(), od.getSequencesHeight()); + BufferedImage bottomImage = lastMiniMe.getSubimage(0, + od.getSequencesHeight(), od.getWidth(), + od.getGraphHeight()); + + // must be done at this point as we rely on using old width/height + // above, and new width/height below + od.setWidth(getWidth()); + od.setHeight(getHeight()); + + // stick the images back together so lastMiniMe is consistent in the + // event of a repaint - BUT probably not thread safe + lastMiniMe = new BufferedImage(od.getWidth(), od.getHeight(), + BufferedImage.TYPE_INT_RGB); + Graphics lg = lastMiniMe.getGraphics(); + lg.drawImage(topImage, 0, 0, od.getWidth(), + od.getSequencesHeight(), null); + lg.drawImage(bottomImage, 0, od.getSequencesHeight(), + od.getWidth(), od.getGraphHeight(), this); + lg.dispose(); } - else if ((int) (col * sampleCol) != lastcol - || (int) (row * sampleRow) != lastrow) + else { - lastcol = (int) (col * sampleCol); - color = getColumnColourFromSequence(seq, hiddenRow, - hasHiddenCols, lastcol); + od.setWidth(getWidth()); + od.setHeight(getHeight()); } - // else we just use the color we already have , so don't need to set it - miniMe.setRGB(col, row, color); + // scale lastMiniMe to the new size + g.drawImage(lastMiniMe, 0, 0, getWidth(), getHeight(), this); + + // make sure the box is in the right place + od.setBoxPosition(av.getAlignment().getHiddenSequences(), + av.getAlignment().getHiddenColumns()); + } + else // not a resize + { + // fall back to normal behaviour + g.drawImage(lastMiniMe, 0, 0, getWidth(), getHeight(), this); } } + + // draw the box + g.setColor(Color.red); + od.drawBox(g); } - /* - * Find the colour of a sequence at a specified column position - */ - private int getColumnColourFromSequence(jalview.datamodel.SequenceI seq, - boolean hiddenRow, boolean hasHiddenCols, int lastcol) + public void dispose() { - int color; - - if (seq == null) - { - color = Color.white.getRGB(); - } - else if (seq.getLength() > lastcol) + dispose = true; + synchronized (this) { - color = sr.getResidueBoxColour(seq, lastcol).getRGB(); - - if (av.isShowSequenceFeatures()) + restart = true; + if (or != null) { - color = fr.findFeatureColour(color, seq, lastcol); + or.setRedraw(true); } } - else - { - color = Color.white.getRGB(); - } - - if (hiddenRow - || (hasHiddenCols && !av.getColumnSelection() - .isVisible(lastcol))) - { - color = new Color(color).darker().darker().getRGB(); - } - - return color; } - }