Class: CTioga2::Graphics::Elements::Container

Inherits:
TiogaElement
  • Object
show all
Defined in:
lib/ctioga2/graphics/elements/containers.rb

Overview

A Container is a drawable object that contains several others, its #elements.

Direct Known Subclasses

RedirectingContainer, Subplot

Instance Attribute Summary collapse

Attributes inherited from TiogaElement

#clipped, #depth, #location, #parent

Instance Method Summary collapse

Methods inherited from TiogaElement

#inspect

Methods included from Log

context, debug, error, fatal, #format_exception, #identify, info, init_logger, log_to, logger, set_level, #spawn, warn

Constructor Details

#initialize(parent = nil, root = nil) ⇒ Container

Creates an empty new Container with the given parent.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/ctioga2/graphics/elements/containers.rb', line 63

def initialize(parent = nil, root = nil)
  super()
  @parent = parent
  
  # elements to be given to tioga
  @elements = []

  # By default the frame takes up all the space.
  @subframe = Types::MarginsBox.new(0, 0, 0, 0)

  @root_object = root

  @legend_storage = Legends::LegendStorage.new
  
  @legend_item_target = @legend_storage

  # By default, don't display legends.
  @legend_area = nil
end

Instance Attribute Details

#elementsObject

All drawable Element contained in this object. It may contain other Container subobjects.



30
31
32
# File 'lib/ctioga2/graphics/elements/containers.rb', line 30

def elements
  @elements
end

#gp_cacheObject

A general-purpose cache that objects may use.

It is a hash, and its contents are reset at the beginning of each invocation of #do.



56
57
58
# File 'lib/ctioga2/graphics/elements/containers.rb', line 56

def gp_cache
  @gp_cache
end

#legend_areaObject

The Legends::LegendArea dedicated to the display of the legend of this object and its children, or nil if the parent should handle the display.



42
43
44
# File 'lib/ctioga2/graphics/elements/containers.rb', line 42

def legend_area
  @legend_area
end

#legend_item_targetObject

The current legend container to which legend items are added. Defaults to the #legend_storage, but it can be changed



50
51
52
# File 'lib/ctioga2/graphics/elements/containers.rb', line 50

def legend_item_target
  @legend_item_target
end

#legend_storageObject

The Legends::LegendStorage that holds all the legends of the object



46
47
48
# File 'lib/ctioga2/graphics/elements/containers.rb', line 46

def legend_storage
  @legend_storage
end

#root_objectObject

A reference to the RootObject



37
38
39
# File 'lib/ctioga2/graphics/elements/containers.rb', line 37

def root_object
  @root_object
end

#subframeObject

The subframe position of this element with respect to its parent. It is a Types::Box object.



34
35
36
# File 'lib/ctioga2/graphics/elements/containers.rb', line 34

def subframe
  @subframe
end

Instance Method Details

#actual_subframe(t) ⇒ Object

Sometimes, the value of the subframe is nil and determined during the plot. This function is guaranteed to return the correct value. It takes a reference to a FigureMaker object.



97
98
99
# File 'lib/ctioga2/graphics/elements/containers.rb', line 97

def actual_subframe(t)
  return @subframe
end

#add_element(element) ⇒ Object

Adds an element



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/ctioga2/graphics/elements/containers.rb', line 102

def add_element(element)
  element.parent = self
  @elements << element
  
  # If the element has a curve_style, we add it as a
  # CurveLegend
  if element.respond_to?(:curve_style) and 
      element.curve_style.has_legend?
    add_legend_item(Legends::CurveLegend.new(element.curve_style))
  elsif element.is_a? Container
    add_legend_item(element)
  end

  # We call LocationStyle#finalize! if possible
  if(self.respond_to?(:style) and element.respond_to?(:location))
    element.location.finalize!(self.style)
  end
end

#add_legend_item(item) ⇒ Object

Adds a legend item to the current storage



123
124
125
# File 'lib/ctioga2/graphics/elements/containers.rb', line 123

def add_legend_item(item)
  @legend_item_target.add_item(item)
end

#do(t) ⇒ Object



83
84
85
86
87
# File 'lib/ctioga2/graphics/elements/containers.rb', line 83

def do(t)
  # reset the cache
  @gp_cache = {}
  super
end

#each_item(leaf_only = true, recursive = false, tl = true, &blk) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/ctioga2/graphics/elements/containers.rb', line 140

def each_item(leaf_only = true, recursive = false, tl = true, &blk)
  if (!recursive && !tl)
    return              # We're at the bottom level
  end
  for el in @elements
    if el.respond_to? :each_item
      if ! leaf_only
        blk.call(el)
      end
      el.each_item(leaf_only, recursive, false, &blk)
    else
      blk.call(el)
    end
  end
end

#enter_legend_subcontainer(sub) ⇒ Object

Adds a legend item to the current storage and make that item the next target for legend items.

If @a sub is nil, then switch back to the top



131
132
133
134
135
136
137
138
# File 'lib/ctioga2/graphics/elements/containers.rb', line 131

def enter_legend_subcontainer(sub)
  if sub
    add_legend_item(sub)
    @legend_item_target = sub
  else
    @legend_item_target = @legend_storage
  end
end

#sizeObject

Returns the number of child elements



90
91
92
# File 'lib/ctioga2/graphics/elements/containers.rb', line 90

def size
  return @elements.size
end