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

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

Overview

This is a style sheet, is a storage place for style objects. It has two related functions:

  • first, store the user-specified preferences

  • second, provide the appropriate default style for any given object, most probably at construction time (although that may get hard some times)

The style are cascading and scoped. A scope should begin in each plot.

Cascades happen in two ways:

  • more specific styles inherit from less specific (axis -> yaxis -> left)

  • children style inherit from parent style

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(par = nil) ⇒ StyleSheet

Returns a new instance of StyleSheet.



53
54
55
56
# File 'lib/ctioga2/graphics/styles/sheet.rb', line 53

def initialize(par = nil)
  @parent = par
  @own_styles = {}
end

Instance Attribute Details

#own_stylesObject

The styles, in form of a style class -> style name -> style object nested hash

The style object is actually a hash ready to be fed to the BasicStyle#set_from_hash



51
52
53
# File 'lib/ctioga2/graphics/styles/sheet.rb', line 51

def own_styles
  @own_styles
end

#parentObject

The parent of the style sheet, or nil if this is the top one.



44
45
46
# File 'lib/ctioga2/graphics/styles/sheet.rb', line 44

def parent
  @parent
end

Class Method Details

.current_sheetObject



152
153
154
# File 'lib/ctioga2/graphics/styles/sheet.rb', line 152

def self.current_sheet()
  return @sheet
end

.enter_scopeObject



140
141
142
# File 'lib/ctioga2/graphics/styles/sheet.rb', line 140

def self.enter_scope()
  @sheet = StyleSheet.new(@sheet)
end

.get_parent(cls, style) ⇒ Object

Returns the parent style for the style (or nil should the style have no parent)

All styles (but base) derive from the corresponding “base” style.



75
76
77
78
79
80
81
82
# File 'lib/ctioga2/graphics/styles/sheet.rb', line 75

def self.get_parent(cls, style)
  @style_parent[cls] ||= {}
  stl = @style_parent[cls][style]
  if (! stl) and (! (style == 'base'))
    return 'base'
  end
  return stl
end

.leave_scopeObject



144
145
146
147
148
149
150
# File 'lib/ctioga2/graphics/styles/sheet.rb', line 144

def self.leave_scope()
  if @sheet.parent
    @sheet = @sheet.parent
  else
    warn { "Trying to leave top-level stylesheet scope" }
  end
end

.set_parent(cls, style, parent) ⇒ Object

Sets the parent for the given style



65
66
67
68
# File 'lib/ctioga2/graphics/styles/sheet.rb', line 65

def self.set_parent(cls, style, parent)
  @style_parent[cls] ||= {}
  @style_parent[cls][style] = parent
end

.style_for(cls, name, *args) ⇒ Object

Returns a suitable style object for the given style name, or crashes if the name isn’t known.

Additional arguments are passed to the constructor



134
135
136
137
138
# File 'lib/ctioga2/graphics/styles/sheet.rb', line 134

def self.style_for(cls, name, *args)
  a = cls.new(*args)
  a.set_from_hash(@sheet.get_style_hash_for(cls, name))
  return a
end

.update_style(cls, what, values) ⇒ Object

Updates the style sheet concerning the what of class cls with the given values



159
160
161
162
163
# File 'lib/ctioga2/graphics/styles/sheet.rb', line 159

def self.update_style(cls, what, values)
  StyleSheet.current_sheet.own_styles[cls] ||= {}
  StyleSheet.current_sheet.own_styles[cls][what] ||= {}
  StyleSheet.current_sheet.own_styles[cls][what].merge!(values)
end

Instance Method Details

#get_style_hash_for(cls, name) ⇒ Object

The style for the given name, including all cascading



115
116
117
118
119
120
121
122
123
# File 'lib/ctioga2/graphics/styles/sheet.rb', line 115

def get_style_hash_for(cls, name)
  ps = {}
  if @parent
    ps = @parent.get_style_hash_for(cls, name);
  end
  style = own_style_hash_for(cls, name)
  style.merge!(ps) { |key, v1, v2| v1 }
  return style
end

#own_style_hash_for(cls, name) ⇒ Object

This returns the style we have in this object for the given name. Inner cascading should take place (ie object hierarchy, but not scope hierarchy).

This returns a hash that can be modified.



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/ctioga2/graphics/styles/sheet.rb', line 98

def own_style_hash_for(cls, name)
  p = self.class.get_parent(cls, name)
  base = {}
  if p
    base = own_style_hash_for(cls, p)
  end
  @own_styles[cls] ||= {}
  style = @own_styles[cls][name]
  if ! style
    return base
  end
  style = style.dup
  style.merge!(base) { |key, v1, v2| v1 }
  return style
end