class CTioga2::Graphics::Elements::TiogaElement

The base class for every single object that is drawn on Tioga's output. Each object can have a style attached to it, and obtained from the StyleSheet class.

For styling-related purposes, all subclasses of this class have the following characteristics:

* a style name (a "type selector")
* a style class (the corresponding underlying class)

All instances of this classes have several properties:

* a (unique) id
* a list of classes (specified as comma-separated stuff)

Ideas for how the style should be used:

- first create the object
- then, soon afterwards, use #setup_style to give it a
  workable position in the style list
- then, use get_style to get the style ;-)...

Constants

StyleBaseOptions

Attributes

clipped[RW]

Whether the object is clipped by default or not.

depth[W]

Depth

hidden[RW]

Wether or not the object is hidden

location[W]

Details pertaining to the location of the object, as a LocationStyle object

object_classes[RW]

The classes (order matter)

object_id[R]

The id

object_parent[RW]

The parent (in the style point of view, which may be different from the rest)

parent[RW]

The parent Container.

Public Class Methods

all_styles() click to toggle source
# File lib/ctioga2/graphics/elements/element.rb, line 150
def self.all_styles
  return @style_classes
end
base_style() click to toggle source
# File lib/ctioga2/graphics/elements/element.rb, line 106
def self.base_style
  if @style_name
    return self
  elsif self == TiogaElement
    return nil
  else
    return self.superclass.base_style
  end
end
define_style(name, cls = nil) click to toggle source
# File lib/ctioga2/graphics/elements/element.rb, line 82
def self.define_style(name, cls = nil)
  @style_name = name
  @style_class = cls
  register_style(name, cls)
end
find_object(obj_id) click to toggle source
# File lib/ctioga2/graphics/elements/element.rb, line 184
def self.find_object(obj_id)
  @registered_objects ||= {}
  if @registered_objects.key? obj_id
    return @registered_objects[obj_id]
  else
    raise "No such object: '#{obj_id}'"
  end
end
find_objects(id_list) click to toggle source
# File lib/ctioga2/graphics/elements/element.rb, line 193
def self.find_objects(id_list)
  # First split on commas:
  ids = id_list.split(/\s*,\s*/)
  objs = []
  @objects_by_class ||= {}
  for oi in ids
    if oi =~ /^\.(.*)/
      objs += (@objects_by_class[$1] || [])
    elsif oi =~ /^\#?(.*)/
      objs << self.find_object($1)
    end
  end
  return objs
end
inherited(cls) click to toggle source
# File lib/ctioga2/graphics/elements/element.rb, line 156
def self.inherited(cls)
  # p cls
end
new() click to toggle source
# File lib/ctioga2/graphics/elements/element.rb, line 160
def initialize
  @clipped = true

  @depth = 50           # Hey, like xfig
  
  @gp_cache = {}
end
register_object(obj) click to toggle source
# File lib/ctioga2/graphics/elements/element.rb, line 168
def self.register_object(obj)
  @registered_objects ||= {}
  @objects_by_class ||= {}
  if i = obj.object_id
    if @registered_objects.key? i
      warn { "Second object with ID #{i}, ignoring the name" }
    else
      @registered_objects[i] = obj
    end
  end
  for cls in (obj.object_classes || [])
    @objects_by_class[cls] ||= []
    @objects_by_class[cls] << obj
  end
end
register_style(name, cls) click to toggle source
# File lib/ctioga2/graphics/elements/element.rb, line 95
def self.register_style(name, cls)
  @@all_styles[name] = self
  if @@style_classes.key? name
    if @@style_classes[name] != cls
      raise "Trying to register different classes under the same name"
    end
  else
    @@style_classes[name] = cls
  end
end
style_class() click to toggle source
# File lib/ctioga2/graphics/elements/element.rb, line 116
def self.style_class
  if @style_name
    return @style_class
  else
    bs = base_style
    return (bs ? bs.style_class : nil)
  end
end
style_name() click to toggle source
# File lib/ctioga2/graphics/elements/element.rb, line 125
def self.style_name
  if @style_name
    return @style_name
  else
    bs = base_style
    return (bs ? bs.style_name : nil)
  end
end
styled_classes() click to toggle source
# File lib/ctioga2/graphics/elements/element.rb, line 91
def self.styled_classes
  return @@all_styles
end

Public Instance Methods

check_styled() click to toggle source
# File lib/ctioga2/graphics/elements/element.rb, line 231
def check_styled()
  if ! self.style_class
    raise "Object has no attached style class !"
  elsif ! @style_is_setup
    raise "Should have setup style before !"
  end
end
depth() click to toggle source
# File lib/ctioga2/graphics/elements/element.rb, line 239
def depth
  @depth || 50
end
do(f) click to toggle source

This function must be called with a FigureMaker object to draw the contents of the TiogaElement onto it. It calls real_do, which should be redefined by the children. You can redefine do too if you need another debugging output.

# File lib/ctioga2/graphics/elements/element.rb, line 253
def do(f)
  if @hidden
    debug { "not plotting hidden #{self.to_yaml}" }
    return 
  else
    debug { "plotting #{self.to_yaml}" }
  end
  @gp_cache = {}
  real_do(f)
end
get_style() click to toggle source
# File lib/ctioga2/graphics/elements/element.rb, line 220
def get_style()
  check_styled()
  return Styles::StyleSheet.style_for(self)
end
has_style?() click to toggle source
# File lib/ctioga2/graphics/elements/element.rb, line 142
def has_style?
  if style_class
    return true
  else
    return false
  end
end
inspect(prefix="") click to toggle source

We plot everything but parent. If a prefix is given, it is prepended to all lines but the first (for indentation)

# File lib/ctioga2/graphics/elements/element.rb, line 266
def inspect(prefix="")
  ret = "#<#{self.class.name}:\n"
  for i in instance_variables
    next if i == "@parent"
    var = instance_variable_get(i)
    ret += "#{prefix}  - #{i} -> "
    if var.is_a? TiogaElement
      ret += "#{var.inspect("#{prefix}  ")}\n"
    else
      ret += "#{var.inspect}\n"
    end
  end
  ret += "#{prefix}>"
  return ret
end
location() click to toggle source

Makes sure there is a location when one asks for it.

# File lib/ctioga2/graphics/elements/element.rb, line 244
def location
  @location ||= Styles::LocationStyle.new
  return @location
end
setup_style(obj_parent, opts) click to toggle source
# File lib/ctioga2/graphics/elements/element.rb, line 209
def setup_style(obj_parent, opts) 
  @cached_options = opts
  @object_id = opts["id"] || nil
  @object_classes = opts.key?("class") ? [opts["class"]].flatten : []
  @object_parent = obj_parent

  TiogaElement.register_object(self)
  @style_is_setup = true
end
style_class() click to toggle source
# File lib/ctioga2/graphics/elements/element.rb, line 134
def style_class
  return self.class.style_class
end
style_name() click to toggle source
# File lib/ctioga2/graphics/elements/element.rb, line 138
def style_name
  return self.class.style_name
end
update_style(style) click to toggle source
# File lib/ctioga2/graphics/elements/element.rb, line 225
def update_style(style)
  check_styled()
  stl = Styles::StyleSheet.style_hash_for(self)
  style.set_from_hash(stl)
end

Protected Instance Methods

real_do(t) click to toggle source
# File lib/ctioga2/graphics/elements/element.rb, line 284
def real_do(t)
  raise "Should be reimplemented by children"
end