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, counts, debug, error, fatal, #format_exception, #identify, info, init_logger, log_to, logger, set_level, #spawn, warn

Constructor Details

#initializeCurveStyleFactory

Creates a new CurveStyleFactory.



254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/ctioga2/graphics/styles/factory.rb', line 254

def initialize
  # Overrides as in the first ctioga
  @override_parameters = {
    'line_style' => LineStyles::Solid,
    '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
    # There should be a way to do that !
    set = param.default_set
    if set
      @parameters_carrays[target] = CircularArray.new(set)
    end
  end

  @next_style = nil
end

Instance Attribute Details

#override_parametersObject

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



247
248
249
# File 'lib/ctioga2/graphics/styles/factory.rb', line 247

def override_parameters
  @override_parameters
end

#parameter_carraysObject

A hash of CircularArray objects.



250
251
252
# File 'lib/ctioga2/graphics/styles/factory.rb', line 250

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.



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/ctioga2/graphics/styles/factory.rb', line 190

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.



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
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/ctioga2/graphics/styles/factory.rb', line 119

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.



176
177
178
# File 'lib/ctioga2/graphics/styles/factory.rb', line 176

def self.name_to_target
  return @name_to_target
end

.parametersObject

Returns the Hash containing the class parameters.



170
171
172
# File 'lib/ctioga2/graphics/styles/factory.rb', line 170

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.



230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/ctioga2/graphics/styles/factory.rb', line 230

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, short = nil) ⇒ Object

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



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

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

Instance Method Details

#hash_name_to_target(h) ⇒ Object

Converts the one-time parameters, which is a hash whose keys are the names of the parameters to targets.



437
438
439
440
441
442
443
444
445
446
447
448
# File 'lib/ctioga2/graphics/styles/factory.rb', line 437

def hash_name_to_target(h)
  retval = {}
  convert = self.class.name_to_target
  for k,v in h
    if convert.key? k 
      retval[convert[k]] = v
    else
      warn { "Unkown key for hash_name_to_target: #{k}" }
    end
  end
  return retval
end

#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.



284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/ctioga2/graphics/styles/factory.rb', line 284

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

#set_next_style(stl) ⇒ Object

Sets the style to be returned from the next call to #next (not counting the effect of the options passed)



277
278
279
# File 'lib/ctioga2/graphics/styles/factory.rb', line 277

def set_next_style(stl)
  @next_style = stl
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.



309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# File 'lib/ctioga2/graphics/styles/factory.rb', line 309

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



338
339
340
341
342
343
344
345
# File 'lib/ctioga2/graphics/styles/factory.rb', line 338

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