class CTioga2::Graphics::Styles::StyleSheet

A StyleSheet is a simple implementation of CSS-like facilities in ctioga2. As in CSS, it recognizes id and classes, and type.

Attributes

buckets[RW]

The list of buckets

buckets_by_xpath[RW]

A hash “full xpath” -> bucket name, so that one can update a bucket instead of just adding to it.

Public Class Methods

new() click to toggle source
# File lib/ctioga2/graphics/styles/stylesheet.rb, line 257
def initialize()
  @buckets = []
  @buckets_by_xpath = {}
end
style_for(obj) click to toggle source
# File lib/ctioga2/graphics/styles/stylesheet.rb, line 313
def self.style_for(obj)
  return self.style_sheet.style_for(obj)
end
style_hash_for(obj) click to toggle source
# File lib/ctioga2/graphics/styles/stylesheet.rb, line 309
def self.style_hash_for(obj)
  return self.style_sheet.style_hash_for(obj)
end
style_sheet() click to toggle source
# File lib/ctioga2/graphics/styles/stylesheet.rb, line 304
def self.style_sheet
  @style_sheet ||= StyleSheet.new
  @style_sheet
end

Public Instance Methods

set_style(xpath, style) click to toggle source
# File lib/ctioga2/graphics/styles/stylesheet.rb, line 262
def set_style(xpath, style)
  for f in xpath.split(/\s*,\s*/) 
    bkt = get_bucket(f)
    bkt.style = style
  end
end
style_for(obj) click to toggle source
# File lib/ctioga2/graphics/styles/stylesheet.rb, line 300
def style_for(obj)
  return obj.style_class.from_hash(style_hash_for(obj))
end
style_hash_for(obj) click to toggle source
# File lib/ctioga2/graphics/styles/stylesheet.rb, line 282
def style_hash_for(obj)
  stl = {}
  # p [:cls, obj.class, obj.object_id, obj.object_classes]
  for bkt in @buckets
    # p [bkt.xpath, bkt.matches?(obj), bkt.style]
    if bkt.matches?(obj)
      stl.merge!(bkt.normalized_style)
    end
  end

  # p [:s, stl]
  cls = obj.style_class
  # p cls.options_hash.keys
  rv = cls.convert_string_hash(stl)
  # p [:t, rv]
  return rv
end
update_from_file(file) click to toggle source
# File lib/ctioga2/graphics/styles/stylesheet.rb, line 317
def update_from_file(file)
end
update_from_string(str) click to toggle source
# File lib/ctioga2/graphics/styles/stylesheet.rb, line 320
def update_from_string(str)
  # First, strip all comments from the string
  str = str.gsub(/\/\*.*?\*\//m, '')

  str.gsub(/^\s*((?:[.#]?[,\w-]+\s*>?\s*)+)\s*\{([^}]+)\}/m) do |x|
    xpath = $1
    smts = $2.split(/\s*;\s*/)
    
    stl = {}
    for s in smts
      if s =~ /\s*([\w-]+)\s*:\s*(.*)/m
        stl[$1] = $2
      else
        error { "Style not understood: #{s}" }
      end
    end
    update_style(xpath, stl)
  end
  # p self
end
update_style(xpath, style, default_type = nil) click to toggle source

@todo Maybe update and set should just add a new bucket at the end, so that it overrides the previous ones anyway ?

# File lib/ctioga2/graphics/styles/stylesheet.rb, line 271
def update_style(xpath, style, default_type = nil)
  for f in xpath.split(/\s*,\s*/)
    xp = XPath.from_text(f)
    if default_type
      xp.elements.first.obj_type ||= default_type
    end
    bkt = get_bucket(xp)
    bkt.style.merge!(style)
  end
end

Protected Instance Methods

get_bucket(xp) click to toggle source
# File lib/ctioga2/graphics/styles/stylesheet.rb, line 343
def get_bucket(xp)
  xpath = xp.to_s
  if ! @buckets_by_xpath.key? xpath
    @buckets << Bucket.new(xpath)
    @buckets_by_xpath[xpath] = @buckets.last
  end
  return @buckets_by_xpath[xpath]
end