Source

lqpl / GUI / src / panels / quantum_stack / quantum_stack_painter.rb

LINE_LABEL_FONT_SIZE = 8.0
PLACEMENTS={-1 => :left, 0 => :right, 1 => :right}

class QuantumStackPainter
  include Painter
  include Drawing

  attr :model_element

  def initialize(model_element)
    self.model_element=model_element
  end

  def model_element=(model)
    @model_element = model

    @descriptor_painter = DescriptorPainterFactory.make_painter(model.descriptor)
    @sstack_painters = @model_element.substacks.collect {|s| QuantumStackPainter.new(s)}

  end

  # painter interface
  alias :setModelElement :model_element=

  def imageOfModel()
    bistart = BufferedImage.new(10,10,BufferedImage::TYPE_4BYTE_ABGR)
    gstart = bistart.create_graphics
    image_size = model_paint_size(gstart)
    bifull = BufferedImage.new(image_size.required_width+40,image_size.height+40,BufferedImage::TYPE_4BYTE_ABGR)
    g = bifull.create_graphics
    paint_model(g)
    ImageIcon.new(bifull)
  end

  alias :image_of_model :imageOfModel

  def paintModel(g)
    g.set_rendering_hint(RenderingHints::KEY_ANTIALIASING, RenderingHints::VALUE_ANTIALIAS_ON)

    d = model_paint_size(g);
    paint_model_at_point(g,Point.new(d.left_required_width+20.0,20.0))
  end

  alias :paint_model :paintModel

  def paintModelAtPoint(g,center)
    if model_element.bottom?
      draw_text_centered_at_point(g,"...", center)
    else
      paint_substacks(center,g)
      @descriptor_painter.paint_model_at_point(g,center)

    end
  end

  alias :paint_model_at_point :paintModelAtPoint

  # end of painter interface

  def bottom_element_size(g)
    dim= get_string_size(g,"...")
    @preferred_size = CanvasSize.new_with_measures(dim.width * 0.5, dim.width * 0.5, dim.height)
    return @preferred_size
  end
  
  def model_paint_size(g)
    return @preferred_size if @preferred_size
    return bottom_element_size g if model_element.bottom?
    
    
    @preferred_size = CanvasSize.new_from_subtree (@sstack_painters.collect { |painter| painter.model_paint_size(g)})
    
    @preferred_size.max_of_each_dimension!(@descriptor_painter.model_paint_size(g)) if @descriptor_painter
 
    @preferred_size
  end

  def substack_label(index)
    d = @model_element.descriptor.substack_labels
    return d[index].to_s if d
    "Nil for model descriptor"
    #"#{@model_element.descriptor.substack_labels[index]}"
  end
  
  def substack_label_placement(index)
    PLACEMENTS[index <=> (@model_element.substacks.length - 1) * 0.5]
  end
  
  def paint_substacks(top_point,g)
    offsets = CanvasSize::compute_offsets(sub_stack_sizes(g))
    Range.new(0,@sstack_painters.size-1).each do |i|
      paint_at_point = top_point.copy_with_x_and_y_offset(offsets[i], CanvasSize::vertical_node_separation)
      paint_substack(g,i,top_point,paint_at_point)
    end
  end
  
  def paint_substack(g,index,top_point,paint_point)
    draw_black_line(g,top_point, paint_point)
    draw_sized_text(g,LINE_LABEL_FONT_SIZE,substack_label(index),top_point, paint_point,substack_label_placement(index))
    @sstack_painters[index].paint_model_at_point(g,paint_point)

  end

  def sub_stack_sizes(g)
    @sstack_painters.collect {|sstack_painter| sstack_painter.model_paint_size(g)}
  end

 

end
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.