Module: Wx::SF::AutoLayout

Defined in:
lib/wx/shapes/auto_layout.rb

Overview

Module implements automatic diagram layout. The module allows to automatically layout shapes included in diagram manager/shape canvas/list of shapes by using several pre-defined layouting algorithms. The module should be used as it is.

Class Method Summary collapse

Class Method Details

.each_layout_algorithm(&block) ⇒ Object



290
291
292
293
294
295
296
# File 'lib/wx/shapes/auto_layout.rb', line 290

def each_layout_algorithm(&block)
  if block
    layout_algorithms_table.each_value { |klass| block.call(klass.new) }
  else
    ::Enumerator.new { |y| layout_algorithms_table.each_value { |klass| y << klass.new } }
  end
end

.get_layout_algorithm(name) ⇒ Object



282
283
284
# File 'lib/wx/shapes/auto_layout.rb', line 282

def get_layout_algorithm(name)
  layout_algorithms_table.has_key?(name.to_s) ? layout_algorithms_table[name.to_s].new : nil
end

.layout(shapes, algname) ⇒ Boolean .layout(diagram, algname) ⇒ Boolean .layout(canvas, algname) ⇒ Boolean

Overloads:

  • .layout(shapes, algname) ⇒ Boolean

    Layout shapes included in given list.

    Parameters:

    • shapes (Array<Shape>)

      List of shapes

    • algname (String)

      Algorithm name

    Returns:

    • (Boolean)

      true if layout algorithm was found and executed, false otherwise

  • .layout(diagram, algname) ⇒ Boolean

    Layout shapes included in given diagram.

    Parameters:

    • diagram (Diagram)

      Reference to diagram

    • algname (String)

      Algorithm name

    Returns:

    • (Boolean)

      true if layout algorithm was found and executed, false otherwise

  • .layout(canvas, algname) ⇒ Boolean

    Layout shapes included in given shape canvas.

    Parameters:

    • canvas (ShapeCanvas)

      Reference to shape canvas

    • algname (String)

      Algorithm name

    Returns:

    • (Boolean)

      true if layout algorithm was found and executed, false otherwise



313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# File 'lib/wx/shapes/auto_layout.rb', line 313

def layout(shapes, algname)
  if shapes.is_a?(::Array)
    alg = get_layout_algorithm(algname)
    if alg
      shapes.first.get_diagram.set_modified unless shapes.empty? || shapes.first.get_diagram.nil?
      alg.do_layout(shapes)
      return true
    end
  elsif shapes.is_a?(Diagram) || shapes.is_a?(ShapeCanvas)
    alg = get_layout_algorithm(algname)
    if alg
      diagram = shapes.is_a?(Diagram) ? shapes : shapes.get_diagram
      # layout all top level shapes excluding the line shapes
      alg.do_layout(diagram.get_top_shapes.reject { |shp| shp.is_a?(LineShape) })
      diagram.move_shapes_from_negatives
      diagram.set_modified
      update_canvas(diagram.get_shape_canvas) if diagram.get_shape_canvas
      return true
    end
  else
    ::Kernel.raise ArgumentError, 'Expected array, diagram or canvas'
  end
  false
end

.layout_algorithmsObject



286
287
288
# File 'lib/wx/shapes/auto_layout.rb', line 286

def layout_algorithms
  layout_algorithms_table.keys
end

.layout_algorithms_tableObject



273
274
275
# File 'lib/wx/shapes/auto_layout.rb', line 273

def layout_algorithms_table
  @layout_algorithms ||= {}
end

.register_layout_algorithm(name, klass) ⇒ Object



278
279
280
# File 'lib/wx/shapes/auto_layout.rb', line 278

def register_layout_algorithm(name, klass)
  layout_algorithms_table[name.to_s] = klass
end

.update_canvas(canvas) ⇒ Object (protected)

Update given shape canvas.

Parameters:



342
343
344
345
346
347
# File 'lib/wx/shapes/auto_layout.rb', line 342

def update_canvas(canvas)
  canvas.center_shapes
  canvas.update_virtual_size
  canvas.update_multiedit_size
  canvas.refresh(false)
end