Class: CTioga2::Graphics::Elements::TiogaElement

Inherits:
Object
  • Object
show all
Includes:
Log
Defined in:
lib/ctioga2/graphics/elements/element.rb

Overview

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 ;-)...

Constant Summary collapse

StyleBaseOptions =
{
  'id' => CmdArg.new('text'),
  'class' => CmdArg.new('text-list')
}
@@style_classes =
{}
@@all_styles =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Log

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

Constructor Details

#initializeTiogaElement

Returns a new instance of TiogaElement.



160
161
162
163
164
165
166
# File 'lib/ctioga2/graphics/elements/element.rb', line 160

def initialize
  @clipped = true

  @depth = 50           # Hey, like xfig
  
  @gp_cache = {}
end

Instance Attribute Details

#clippedObject

Whether the object is clipped by default or not.



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

def clipped
  @clipped
end

#depthObject



239
240
241
# File 'lib/ctioga2/graphics/elements/element.rb', line 239

def depth
  @depth || 50
end

#hiddenObject

Wether or not the object is hidden



75
76
77
# File 'lib/ctioga2/graphics/elements/element.rb', line 75

def hidden
  @hidden
end

#locationObject

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



244
245
246
247
# File 'lib/ctioga2/graphics/elements/element.rb', line 244

def location
  @location ||= Styles::LocationStyle.new
  return @location
end

#object_classesObject

The classes (order matter)



68
69
70
# File 'lib/ctioga2/graphics/elements/element.rb', line 68

def object_classes
  @object_classes
end

#object_idObject (readonly)

The id



65
66
67
# File 'lib/ctioga2/graphics/elements/element.rb', line 65

def object_id
  @object_id
end

#object_parentObject

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



72
73
74
# File 'lib/ctioga2/graphics/elements/element.rb', line 72

def object_parent
  @object_parent
end

#parentObject

The parent Container.



49
50
51
# File 'lib/ctioga2/graphics/elements/element.rb', line 49

def parent
  @parent
end

Class Method Details

.all_stylesObject



150
151
152
# File 'lib/ctioga2/graphics/elements/element.rb', line 150

def self.all_styles
  return @style_classes
end

.base_styleObject



106
107
108
109
110
111
112
113
114
# 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) ⇒ Object



82
83
84
85
86
# 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) ⇒ Object



184
185
186
187
188
189
190
191
# 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) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
204
205
206
# 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) ⇒ Object



156
157
158
# File 'lib/ctioga2/graphics/elements/element.rb', line 156

def self.inherited(cls)
  # p cls
end

.register_object(obj) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# 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) ⇒ Object



95
96
97
98
99
100
101
102
103
104
# 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_classObject



116
117
118
119
120
121
122
123
# 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_nameObject



125
126
127
128
129
130
131
132
# 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_classesObject



91
92
93
# File 'lib/ctioga2/graphics/elements/element.rb', line 91

def self.styled_classes
  return @@all_styles
end

Instance Method Details

#check_styledObject



231
232
233
234
235
236
237
# 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

#do(f) ⇒ Object

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.



253
254
255
256
257
258
259
260
261
262
# 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_styleObject



220
221
222
223
# File 'lib/ctioga2/graphics/elements/element.rb', line 220

def get_style()
  check_styled()
  return Styles::StyleSheet.style_for(self)
end

#has_style?Boolean

Returns:

  • (Boolean)


142
143
144
145
146
147
148
# File 'lib/ctioga2/graphics/elements/element.rb', line 142

def has_style?
  if style_class
    return true
  else
    return false
  end
end

#inspect(prefix = "") ⇒ Object

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



266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# 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

#setup_style(obj_parent, opts) ⇒ Object



209
210
211
212
213
214
215
216
217
# 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_classObject



134
135
136
# File 'lib/ctioga2/graphics/elements/element.rb', line 134

def style_class
  return self.class.style_class
end

#style_nameObject



138
139
140
# File 'lib/ctioga2/graphics/elements/element.rb', line 138

def style_name
  return self.class.style_name
end

#update_style(style) ⇒ Object



225
226
227
228
229
# 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