Class: CTioga2::Graphics::Styles::StyleSheet

Inherits:
Object
  • Object
show all
Defined in:
lib/ctioga2/graphics/styles/stylesheet.rb

Overview

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

Defined Under Namespace

Classes: Bucket, XPath, XPathElement

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStyleSheet

Returns a new instance of StyleSheet.



257
258
259
260
# File 'lib/ctioga2/graphics/styles/stylesheet.rb', line 257

def initialize()
  @buckets = []
  @buckets_by_xpath = {}
end

Instance Attribute Details

#bucketsObject

The list of buckets



251
252
253
# File 'lib/ctioga2/graphics/styles/stylesheet.rb', line 251

def buckets
  @buckets
end

#buckets_by_xpathObject

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



255
256
257
# File 'lib/ctioga2/graphics/styles/stylesheet.rb', line 255

def buckets_by_xpath
  @buckets_by_xpath
end

Class Method Details

.style_for(obj) ⇒ Object



313
314
315
# 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) ⇒ Object



309
310
311
# 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_sheetObject



304
305
306
307
# File 'lib/ctioga2/graphics/styles/stylesheet.rb', line 304

def self.style_sheet
  @style_sheet ||= StyleSheet.new
  @style_sheet
end

Instance Method Details

#set_style(xpath, style) ⇒ Object



262
263
264
265
266
267
# 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) ⇒ Object



300
301
302
# 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) ⇒ Object



282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# 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) ⇒ Object



317
318
# File 'lib/ctioga2/graphics/styles/stylesheet.rb', line 317

def update_from_file(file)
end

#update_from_string(str) ⇒ Object



320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# 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) ⇒ Object

TODO:

Maybe update and set should just add a new bucket at

the end, so that it overrides the previous ones anyway ?



271
272
273
274
275
276
277
278
279
280
# 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