Class: CTioga2::Graphics::Styles::CurveStyleFactory

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

Overview

This object is in charge of the generation of the CurveStyle object for the next curve to be drawn.

Defined Under Namespace

Classes: CurveStyleFactoryParameter

Constant Summary collapse

AutoRE =

Switch some parameter back to automatic

/auto/i
DisableRE =

Sets some parameter to false.

/no(ne)?|off/i
LinkRE =

If that matches, we use the value as a link to other values.

/(?:=|->)(\S+)/
CurveStyleGroup =

The CmdGroup for stylistic information about curves.

CmdGroup.new('curve-style', "Curves styles", 
"Set stylistic details of curves or other object drawn from data", 1)
PlotCommandOptions =

A constant suitable for use as the optional arguments of the plot command.

plot_optional_arguments

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Log

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

Constructor Details

#initializeCurveStyleFactory

Creates a new CurveStyleFactory.



241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/ctioga2/graphics/styles/factory.rb', line 241

def initialize
  # Overrides as in the first ctioga
  @override_parameters = {
    'line_style' => LineStyles::Solid,
    'marker_marker' => false,
    'marker_scale' => 0.5,
    'fill_color' => '=color'.to_sym,
    'error_bar_color' => '=marker_color'.to_sym
  }
  @parameters_carrays = {}
  for target, param in self.class.parameters
    set = param.default_set
    if set
      @parameters_carrays[target] = CircularArray.new(set)
    end
  end
end

Instance Attribute Details

#override_parametersObject

A hash containing values that override default ones derived from the CircularArray objects.



234
235
236
# File 'lib/ctioga2/graphics/styles/factory.rb', line 234

def override_parameters
  @override_parameters
end

#parameter_carraysObject

A hash of CircularArray objects.



237
238
239
# File 'lib/ctioga2/graphics/styles/factory.rb', line 237

def parameter_carrays
  @parameter_carrays
end

Class Method Details

.create_commandsObject

Creates two commands for each parameter of the object:

  • a command to set the override

  • a command to choose the sets.



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/ctioga2/graphics/styles/factory.rb', line 177

def self.create_commands
  parameters.each do |target, param|
    next if param.disable_commands
    override_cmd = 
      Cmd.new("#{param.name}",
              param.short_option,
              "--#{param.name}", 
              [
               CmdArg.new("#{param.type.name}-or-auto") 
              ], {},
              "Sets the #{param.description} for subsequent curves",
              "Sets the #{param.description} for subsequent curves, until cancelled with @auto@ as argument.", CurveStyleGroup) do |plotmaker, value|
      plotmaker.curve_generator.style_factory.
        set_parameter_override(target, value)
    end

    if param.sets
      next if param.disable_commands
      set_cmd = 
        Cmd.new("#{param.name}-set",
                nil,
                "--#{param.name}-set", 
                [
                 CmdArg.new("#{param.type.name}-set")
                ], {},
                "Chooses a set for the #{param.description} of subsequent curves",
                "Chooses a set for the #{param.description} of subsequent curves. Also sets {command: #{param.name}} to @auto@, so that the set takes effect immediately", 
                CurveStyleGroup) do |plotmaker, value|
        plotmaker.curve_generator.style_factory.
          set_parameter_set(target, value)
        plotmaker.curve_generator.style_factory.
          set_parameter_override(target, 'auto')
      end
    end
  end
end

.define_parameter(target, name, sets, description, short_option = nil, disable_cmds = false) ⇒ Object

Creates a new parameter for the style factory.



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/ctioga2/graphics/styles/factory.rb', line 106

def self.define_parameter(target, name, sets, description, 
                          short_option = nil, disable_cmds = false)
  # We define two new types:
  # - first, the color-or-auto type:
  base_type = CurveStyle.attribute_type(target)

  if ! Commands::Interpreter.type("#{base_type.name}-or-auto")
    mb_type = base_type.type.dup
    mb_type.re_shortcuts = (mb_type.re_shortcuts ? 
                                mb_type.re_shortcuts.dup : {}) 
    
    mb_type.re_shortcuts[AutoRE] = 'auto'
    mb_type.re_shortcuts[DisableRE] = false

    # Add passthrough for expressions such as =color...
    mb_type.passthrough = LinkRE

    # Now, register a type for the type or automatic.
    CmdType.new("#{base_type.name}-or-auto", mb_type,
                "Same thing as {type:#{base_type.name}}, or @auto@ to let the style factory handle automatically.")

  end

  if sets and ! Commands::Interpreter.type("#{base_type.name}-set")
    # Now, register a type for the type or automatic.
    CmdType.new("#{base_type.name}-set",{
                  :type => :set,
                  :subtype => base_type.type,
                  :shortcuts => sets
                } ,
                "Sets of {type: #{base_type.name}}")
  end
  param = 
    CurveStyleFactoryParameter.new(name, base_type, sets, 
                                   description, short_option, 
                                   disable_cmds)
  @parameters ||= {}
  @parameters[target] = param

  @name_to_target ||= {}
  @name_to_target[name] = target
end

.name_to_targetObject

Returns the Hash containing the class parameters.



163
164
165
# File 'lib/ctioga2/graphics/styles/factory.rb', line 163

def self.name_to_target
  return @name_to_target
end

.parametersObject

Returns the Hash containing the class parameters.



157
158
159
# File 'lib/ctioga2/graphics/styles/factory.rb', line 157

def self.parameters
  return @parameters || {}
end

.plot_optional_argumentsObject

This function returns a hash suitable for use with the plot command as optional arguments, that will end up as the one_time hash in #next.



217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/ctioga2/graphics/styles/factory.rb', line 217

def self.plot_optional_arguments
  args = {}
  for option_name, param in @parameters
    args[param.name] = 
      CmdArg.new(param.type)
  end

  # Here, we add the support for a /legend= option
  args['legend'] = CmdArg.new('text')
  @name_to_target['legend'] = 'legend'

  return args
end

.simple_parameter(target, text, sets = nil) ⇒ Object

A simple parameter is something whose target defines all, ie only the name and a documentation text is necessary.



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

def self.simple_parameter(target, text, sets = nil)
  name = target.gsub(/_/, '-')
  define_parameter(target, name, sets, text, nil)
end

Instance Method Details

#marker_color_mapObject

TODO:

For xy-parametric, there should be a way to specify

to which z value the maps apply (ie lines = y2, marker = y3…). Although for readability, it is probably better to avoid that…



379
# File 'lib/ctioga2/graphics/styles/factory.rb', line 379

simple_parameter 'marker_color_map', 'color map for markers'

#next(one_time = {}) ⇒ Object

Gets the style for the next curve. The one_time hash contains values ‘parameter name’ (name, and not target) => value that are used for this time only.



262
263
264
265
266
267
268
269
270
# File 'lib/ctioga2/graphics/styles/factory.rb', line 262

def next(one_time = {})
  base = {}
  for target, array in @parameters_carrays
    base[target] = array.next
  end
  base.merge!(@override_parameters)
  base.merge!(hash_name_to_target(one_time))
  return CurveStyle.from_hash(resolve_links(base))
end

#set_parameter_override(target, value) ⇒ Object

Sets the override for the given parameter. This corresponds to fixing manually the corresponding element until the override is removed, by a call with a value that matches AutoRE.

The value should ideally be a String that is further converted to the appropriate type. Non-string objects will be left untouched.



282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/ctioga2/graphics/styles/factory.rb', line 282

def set_parameter_override(target, value)
  param = get_parameter(target)
  # Perform automatic type conversion only on strings.
  if value.is_a? String 
    if value =~ AutoRE
      @override_parameters.delete(target)
      return
    elsif value =~ LinkRE
      t = $1
      convert = self.class.name_to_target
      if convert.key?(t)
        value = "=#{convert[t]}".to_sym
      else
        warn { "No known key: #{t}, treating as auto" }
        @override_parameters.delete(target)
        return
      end

    elsif value =~ DisableRE
      value = false
    else
      value = param.type.string_to_type(value)
    end
  end

  @override_parameters[target] = value
end

#set_parameter_set(target, value) ⇒ Object

Sets the CircularArray set corresponding to the named



311
312
313
314
315
316
317
318
# File 'lib/ctioga2/graphics/styles/factory.rb', line 311

def set_parameter_set(target, value)
  param = get_parameter(target)
  # Perform automatic type conversion only on strings.
  if value.is_a? String 
    value = param.sets_type.string_to_type(value)
  end
  @parameters_carrays[target].set = value
end