~ubuntu-branches/ubuntu/oneiric/ctioga2/oneiric

« back to all changes in this revision

Viewing changes to lib/ctioga2/graphics/elements/containers.rb

  • Committer: Bazaar Package Importer
  • Author(s): Vincent Fourmond
  • Date: 2011-01-24 21:36:06 UTC
  • Revision ID: james.westby@ubuntu.com-20110124213606-9ettx0ugl83z0bzp
Tags: upstream-0.1
ImportĀ upstreamĀ versionĀ 0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# containers.rb: drawables that contains other drawables
 
2
# copyright (c) 2006, 2007, 2008, 2009 by Vincent Fourmond
 
3
  
 
4
# This program is free software; you can redistribute it and/or modify
 
5
# it under the terms of the GNU General Public License as published by
 
6
# the Free Software Foundation; either version 2 of the License, or
 
7
# (at your option) any later version.
 
8
  
 
9
# This program is distributed in the hope that it will be useful,
 
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
# GNU General Public License for more details (in the COPYING file).
 
13
 
 
14
 
 
15
require 'ctioga2/utils'
 
16
require 'ctioga2/log'
 
17
 
 
18
module CTioga2
 
19
 
 
20
  Version::register_svn_info('$Revision: 155 $', '$Date: 2010-06-21 21:41:32 +0200 (Mon, 21 Jun 2010) $')
 
21
 
 
22
  module Graphics
 
23
 
 
24
    module Elements
 
25
      
 
26
      # A Container is a drawable object that contains several others, its
 
27
      # #elements.
 
28
      class Container < TiogaElement
 
29
 
 
30
        # All drawable Element contained in this object. It may
 
31
        # contain other Container subobjects.
 
32
        attr_accessor :elements
 
33
 
 
34
        # The subframe position of this element with respect to its
 
35
        # parent. It is a Types::Box object.
 
36
        attr_accessor :subframe
 
37
 
 
38
        # A reference to the RootObject
 
39
        attr_accessor :root_object
 
40
 
 
41
        # The Legends::LegendArea dedicated to the display of the
 
42
        # legend of this object and its children, or _nil_ if the
 
43
        # parent should handle the display.
 
44
        attr_accessor :legend_area
 
45
 
 
46
        # The Legends::LegendStorage that holds all the legends of the
 
47
        # object
 
48
        attr_accessor :legend_storage
 
49
 
 
50
        # Creates an empty new Container with the given _parent_.
 
51
        def initialize(parent = nil, root = nil)
 
52
          @parent = parent
 
53
          
 
54
          # elements to be given to tioga
 
55
          @elements = []
 
56
 
 
57
          # By default the frame takes up all the space.
 
58
          @subframe = Types::MarginsBox.new(0, 0, 0, 0)
 
59
 
 
60
          @root_object = root
 
61
 
 
62
          @legend_storage = Legends::LegendStorage.new
 
63
 
 
64
          # By default, don't display legends.
 
65
          @legend_area = nil
 
66
        end
 
67
 
 
68
        # Returns the number of child elements
 
69
        def size
 
70
          return @elements.size
 
71
        end
 
72
 
 
73
        # Sometimes, the value of the subframe is _nil_ and determined
 
74
        # during the plot. This function is guaranteed to return the
 
75
        # correct value. It takes a reference to a FigureMaker object.
 
76
        def actual_subframe(t)
 
77
          return @subframe
 
78
        end
 
79
 
 
80
        # Adds an element
 
81
        def add_element(element)
 
82
          element.parent = self
 
83
          @elements << element
 
84
          
 
85
          # If the element has a curve_style, we add it as a
 
86
          # CurveLegend
 
87
          if element.respond_to?(:curve_style) and 
 
88
              element.curve_style.has_legend?
 
89
            add_legend_item(Legends::CurveLegend.new(element.curve_style))
 
90
          elsif element.is_a? Container
 
91
            add_legend_item(element)
 
92
          end
 
93
 
 
94
          # We call LocationStyle#finalize! if possible
 
95
          if(self.respond_to?(:style) and element.respond_to?(:location))
 
96
            element.location.finalize!(self.style)
 
97
          end
 
98
        end
 
99
 
 
100
 
 
101
        # Adds a legend item to the #associated_legend_storage
 
102
        def add_legend_item(item)
 
103
          @legend_storage.add_item(item)
 
104
        end
 
105
 
 
106
        # \todo provide coordinate conversion facilities...
 
107
 
 
108
        protected 
 
109
 
 
110
        # Creates the appropriate subfigure and draws all its elements
 
111
        # within.
 
112
        def real_do(t)
 
113
          t.subfigure(@subframe.to_frame_margins(t)) do 
 
114
            for el in @elements
 
115
              el.do(t)
 
116
            end
 
117
          end
 
118
        end
 
119
 
 
120
      end
 
121
    end
 
122
  end
 
123
end