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

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

Constructor Details

#initializeCurveStyleFactory

Creates a new CurveStyleFactory.



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

def initialize
  # Overrides as in the first ctioga
  @override_parameters = {
    'line_style' => LineStyles::Solid,
    'marker_marker' => false,
    'marker_scale' => 0.5 
  }
  @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.



232
233
234
# File 'lib/ctioga2/graphics/styles/factory.rb', line 232

def override_parameters
  @override_parameters
end

#parameter_carraysObject

A hash of CircularArray objects.



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

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.



175
176
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
# File 'lib/ctioga2/graphics/styles/factory.rb', line 175

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, type, sets, description, short_option = nil, disable_cmds = false) ⇒ Object

Creates a new parameter for the style factory.

todo add a way to add some more text to the description; possibly a self.describe_parameter function ?



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
148
149
150
151
152
# File 'lib/ctioga2/graphics/styles/factory.rb', line 111

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

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



161
162
163
# File 'lib/ctioga2/graphics/styles/factory.rb', line 161

def self.name_to_target
  return @name_to_target
end

.parametersObject

Returns the Hash containing the class parameters.



155
156
157
# File 'lib/ctioga2/graphics/styles/factory.rb', line 155

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.



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

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

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
380
# File 'lib/ctioga2/graphics/styles/factory.rb', line 379

define_parameter 'marker_color_map', 'marker-color-map', 'colormap',
nil, "Marker color map", nil

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



258
259
260
261
262
263
264
265
266
# File 'lib/ctioga2/graphics/styles/factory.rb', line 258

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.



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

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



307
308
309
310
311
312
313
314
# File 'lib/ctioga2/graphics/styles/factory.rb', line 307

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