Package ketUI.panel

Source Code of ketUI.panel.EquationDisplay

/*
* Copyright (C) 2011  Alasdair C. Hamilton
*
* This program 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.
*
* This program 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 this program.  If not, see <http://www.gnu.org/licenses/>
*/

package ketUI.panel;

import geom.Offset;
import java.util.Vector;

// import java.awt.*;  TODO: Expand
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Color;
import ket.*;
import ket.math.*;
import ket.display.*;
import ket.display.box.Box;
import ket.display.box.BoxText;
import geom.Position;

/**
* A single equation may be displayed on its own and its setup and painting is
* controlled by this class.
*/
public class EquationDisplay implements Display {
 
  final MathCollection mathCollection;
  /**
   * When the equation is drawn, record its box from which the arguments
   * associated with a mouse click may be determined.
   */
  Box equationBox;
  boolean isText;
  Box labelBox;


  public EquationDisplay(MathCollection mathCollection) {
    this.mathCollection = mathCollection;
  }

 
  @Override
  public Box findDeepestBox(Position p) {
    if (equationBox!=null) {
      return equationBox.findDeepestBox(p);
    } else {
      return null;
    }
  }

  @Override
  public Vector<Integer> findVisibleEquationIndices() {
    Vector<Integer> indices = new Vector<Integer>();
    if (equationBox==null) return indices;
    Argument a = equationBox.getArgument();
    if (a==null) return indices;
    Integer index = a.getEquationIndex();
    if (index==null) return indices;
    indices.add(index);
    return indices;
  }


  @Override
  public Argument findDeepestArgument(Position p) {
    if (equationBox!=null) {
      return equationBox.findDeepestArgument(p);
    } else {
      return null;
    }
  }

  @Override
  public Equation pointToEquation(Position p) {
    Argument argument = equationBox.getArgument();
    return (argument!=null) ? argument.getEquation() : null;
  }

  @Override
  public boolean generateBoxes(Graphics2D g2D, ColourScheme colourScheme, int fontSize, Offset panelRectangle) {
    // TODO: Handle multiple equation selection.
    Equation equation = mathCollection.getCursor().getEquation();
    equationBox = equation.toBox(colourScheme);

    labelBox = equation.getLabelBox(colourScheme);
    labelBox.setProperty(Box.RIGHT_ALIGN);
    labelBox.setProperty(Box.Y_CENTRE_ALIGN);

    isText = equation.isText();

    // -- new bit ---

    equationBox.setupInnerRectangle(fontSize);
    double minimumHeight = equationBox.getInnerRectangle().height;
    double shapeWidth = panelRectangle.width - KetPanel.BORDER_OFFSET.width;

    Offset equationRectangle = new Offset(shapeWidth, minimumHeight);

    labelBox.setup(fontSize, equationRectangle);

    Offset windowWithoutLabel = new Offset(shapeWidth-labelBox.getInnerRectangle().width, minimumHeight);
    equationBox.setupOuterRectangle(windowWithoutLabel);

    double panelHeightOfNextBox;
    if (isText) {
      panelHeightOfNextBox = KetPanel.TOP_BORDER_SIZE;
    } else {
      double centre = (panelRectangle.height-KetPanel.BORDER_OFFSET.height) / 2.0;
      double heightAboveCentre = equationBox.getOuterRectangle().height / 2.0;
      panelHeightOfNextBox = centre - heightAboveCentre;
    }
    actualEquationTopLeft = new Position(KetPanel.LEFT_BORDER_SIZE, panelHeightOfNextBox);
    return true;
  }


  Position actualEquationTopLeft;


  /**
   * Draw a single equation in the centre of the screen or text to the
   * top left.  In either case a label is also displayed.
   */
  @Override
  public void paint(Graphics2D g2D, ColourScheme colourScheme, int fontSize, Offset panelRectangle) {

    if (labelBox==null || equationBox==null) {
      // (Just in case) If ket panel is painted before boxes have been created in generate boxes.
      return;
    }


    labelBox.paint(g2D, actualEquationTopLeft, colourScheme);
    equationBox.paint(g2D, actualEquationTopLeft, colourScheme);
  }

  @Override
  public boolean isArgumentVisible(Argument argument) {
    return equationBox.containsArgument(argument);
  }

  @Override
  public boolean requiresRepaint() {
    return false;
  }

  @Override
  public void noteChange(Graphics2D g2D, ColourScheme colourScheme, Argument before, Equation afterEquation, int fontSize, Offset panelRectangle) {
    // Do nothing?
  }
}
TOP

Related Classes of ketUI.panel.EquationDisplay

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.