Class: CTioga::GridLayout

Inherits:
SimpleLayout show all
Defined in:
lib/CTioga/layout.rb

Overview

A layout that will handle objects aligned on a grid. All objects belonging to one row of the grid will share the same vertical padding. All the ones belonging to a column will share the same horizontal padding. All layouts added to this one should be a GridItemLayout or a descendant, or at least something behaving correctly in that respect.

Instance Attribute Summary

Attributes inherited from SimpleLayout

#bounding_box

Attributes inherited from Layout

#child_layouts, #object, #parent

Instance Method Summary collapse

Methods inherited from SimpleLayout

#extension, #legend_specs

Methods inherited from Layout

#add_child, #compute_padding, #convert_layout, #extension, #legend_extension, #legend_only_specs, #legend_specs, #update_extension_with_object, #update_extensions

Methods included from Log

#identify, #init_logger, #logger, #logger_options, #spawn

Constructor Details

#initialize(*a) ⇒ GridLayout

Returns a new instance of GridLayout.



403
404
405
406
407
408
# File 'lib/CTioga/layout.rb', line 403

def initialize(*a)
  super
  # We don't want the children's extensions messing
  # with this layout's extension
  @ignore_children_extensions = true
end

Instance Method Details

#child(row, col) ⇒ Object

Returns the child at position (row, col)



446
447
448
# File 'lib/CTioga/layout.rb', line 446

def child(row, col)
  return @child_layouts[col * rows + row ]
end

#child_extension(child, t) ⇒ Object

Returns the extension for the given child.



535
536
537
538
539
540
541
542
543
544
# File 'lib/CTioga/layout.rb', line 535

def child_extension(child,t)
  compute_extensions(t) unless @columns_extensions
  row,col = child_position(child)
  a = @rows_extensions[row].dup
  # We override the horizontal extensions with the
  # @columns_extensions
  a[0] = @columns_extensions[col][0]
  a[1] = @columns_extensions[col][1]
  return a
end

#child_frame(t, row, col = nil) ⇒ Object

Returns the frame allocated for one item of the layout.



547
548
549
550
551
552
553
554
555
556
557
# File 'lib/CTioga/layout.rb', line 547

def child_frame(t, row,col = nil)
  if row.is_a? Layout       # Row is a child, actually...
    return child_frame(t, *child_position(row))
  end

  compute_positions(t) unless @rows_positions
  a = @rows_positions[row].dup
  a[0] = @columns_positions[col][0]
  a[1] = @columns_positions[col][1]
  return a
end

#child_position(child) ⇒ Object

Returns the position of the given child layout in terms of [row, col]. The layouts are added first in columns



440
441
442
443
# File 'lib/CTioga/layout.rb', line 440

def child_position(child)
  i = @child_layouts.index(child)
  return [i % rows, i / rows]
end

#columnsObject

Returns the number of rows



433
434
435
436
# File 'lib/CTioga/layout.rb', line 433

def columns
  return @columns if @columns
  return (@child_layouts.size.to_f / @rows).ceil
end

#columns=(nb) ⇒ Object

Sets the number of columns. The number of rows will be computed automatically.



427
428
429
430
# File 'lib/CTioga/layout.rb', line 427

def columns=(nb)
  @columns = nb
  @rows = false
end

#compute_extensions(t) ⇒ Object

Computes the various extensions for each columns and row



451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
# File 'lib/CTioga/layout.rb', line 451

def compute_extensions(t)
  @rows_extensions = []
  rows.times do |i|
    a = [0,0,0,0]
    columns.times do |j|
      update_extensions(a, child(i,j).extension(t)) if child(i,j)
    end
    @rows_extensions[i] = a
  end
  debug "Grid layout rows #{@rows_extensions.inspect}"
  @columns_extensions = []
  columns.times do |j|
    a = [0,0,0,0]
    columns.times do |i|
      update_extensions(a, child(i,j).extension(t)) if child(i,j)
    end
    @columns_extensions[j] = a
  end
  debug "Grid layout columns #{@columns_extensions.inspect}"
end

#compute_positions(t) ⇒ Object

Computes the positions of the various columns/rows, so that all the objects in the same line/column share the same size in both directions – that is, all plots are identical.



475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
# File 'lib/CTioga/layout.rb', line 475

def compute_positions(t)
  compute_extensions(t) unless @rows_extensions
  frame = @object.outer_frame(t)
  @rows_positions = []
  vert_ext = 0
  vert_top = []
  vert_bottom = []
  for f in @rows_extensions
    a = Dimension.absolute_to_relative(f,frame)
    vert_top << a[2]
    vert_bottom << a[3]
    vert_ext += a[2] + a[3]
  end

  # Now, vert_top and vert_bottom contain the necessary
  # spacing above and below each element, in relative size.
  height = (1.0 - vert_ext)/rows
  debug "Grid layout height #{height}"
  current = 0
  rows.times do |i|
    new = current + vert_top[i] + height + vert_bottom[i]
    @rows_positions << [0,0, # ignored
                        current, 1 - new]
    current  = new
  end

  debug "Grid layout rows positions #{@rows_positions.inspect}"

  # Now, rows_positions contains the vertical position of
  # the given row.

  # We can now do the same for the horizontal positions:
  @columns_positions = []
  horiz_ext = 0
  horiz_left = []
  horiz_right = []
  for f in @columns_extensions
    a = Dimension.absolute_to_relative(f,frame)
    horiz_left << a[0]
    horiz_right << a[1]
    horiz_ext += a[0] + a[1]
  end
  debug "Grid horiz left #{ horiz_left.inspect }"
  debug "Grid horiz right #{horiz_right.inspect}"

  # Now, horiz_left and horiz_right contain the necessary
  # spacing above and below each element, in relative size.
  width = (1.0 - horiz_ext)/columns
  debug "Grid layout width #{width}"
  current = 0
  columns.times do |i|
    new = current + horiz_left[i] + width + horiz_right[i]
    @columns_positions << [current, 1 - new, 0, 0]
    current =  new
  end
  debug "Grid layout columns positions #{@columns_positions.inspect}"
  
end

#rowsObject

Returns the number of rows



420
421
422
423
# File 'lib/CTioga/layout.rb', line 420

def rows
  return @rows if @rows
  return (@child_layouts.size.to_f / @columns).ceil
end

#rows=(nb) ⇒ Object

Sets the number of rows. The number of columns will be computed automatically.



414
415
416
417
# File 'lib/CTioga/layout.rb', line 414

def rows=(nb)
  @rows = nb
  @columns = false
end