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

« back to all changes in this revision

Viewing changes to lib/ctioga2/graphics/elements/region.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
# region.rb: draw curves-delimited fills
 
2
# copyright (c) 2010 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: 139 $', '$Date: 2010-01-22 00:12:17 +0100 (Fri, 22 Jan 2010) $')
 
21
 
 
22
  module Graphics
 
23
 
 
24
    module Elements
 
25
 
 
26
      # A Region is an object that draws filled regions among its
 
27
      # "elements". It is a fake container in the sense that all the
 
28
      # elements are actually forwarded to the parent.
 
29
      class Region < Container
 
30
 
 
31
        undef :elements
 
32
        undef :subframe
 
33
 
 
34
        # The curves which delimit the region
 
35
        attr_accessor :curves
 
36
 
 
37
        # The fill style
 
38
        attr_accessor :fill_style
 
39
 
 
40
        # The fill style for reversed polarity
 
41
        attr_accessor :reversed_fill_style
 
42
 
 
43
        # Creates a new empty region
 
44
        def initialize(parent = nil, root = nil)
 
45
          @parent = parent
 
46
          
 
47
          # elements to be given to tioga
 
48
          @curves = []
 
49
 
 
50
          @root_object = root
 
51
 
 
52
          @legend_area = nil
 
53
 
 
54
          @fill_style = Styles::FillStyle.new
 
55
          @fill_style.color = [0.7,0.7,0.7]
 
56
 
 
57
          # For reversed polarity
 
58
          @reversed_fill_style = Styles::FillStyle.new
 
59
        end
 
60
 
 
61
        # Adds an element. Actually forwards it to the parent.
 
62
        def add_element(element)
 
63
          parent.add_element(element)
 
64
          if element.respond_to?(:curve_style)  &&
 
65
              element.curve_style.region_position
 
66
            @curves << element
 
67
          end
 
68
        end
 
69
 
 
70
        # Sets the various things from hash.
 
71
        def set_from_hash(hash)
 
72
          @fill_style.set_from_hash(hash)
 
73
          # Reversed isn't what I want...
 
74
          @reversed_fill_style.set_from_hash(hash, 'reversed_%s')
 
75
        end
 
76
 
 
77
        # Redirects to the parent's style
 
78
        def style(*a)
 
79
          return parent.style(*a)
 
80
        end
 
81
 
 
82
        protected 
 
83
 
 
84
        # Creates the appropriate subfigure and draws all its elements
 
85
        # within.
 
86
        #
 
87
        # \todo: attempt to work fine while mixing curves with
 
88
        # different axes. That won't be easy.
 
89
        #
 
90
        # \todo: enable to do positive and negative. The only thing to
 
91
        # do is to swap above for below and call again.
 
92
        def real_do(t)
 
93
          # This function will be called with the proper figure
 
94
          # coordinates.
 
95
          
 
96
          if @fill_style.color
 
97
            t.context do
 
98
              @fill_style.setup_fill(t)
 
99
              prepare_path(t)
 
100
              @fill_style.do_fill(t)
 
101
            end
 
102
          end
 
103
 
 
104
          if @reversed_fill_style.color
 
105
            t.context do
 
106
              @reversed_fill_style.setup_fill(t)
 
107
              prepare_path(t, :reversed)
 
108
              @reversed_fill_style.do_fill(t)
 
109
            end
 
110
          end
 
111
          
 
112
        end
 
113
 
 
114
        # Prepares the path that will be filled, according to the
 
115
        # given polarity.
 
116
        def prepare_path(t, polarity = :normal)
 
117
          # We clip the path for the given
 
118
          case polarity
 
119
          when :normal
 
120
            conversion = {
 
121
              :above => :bottom,
 
122
              :below => :top
 
123
            }
 
124
          when :reversed
 
125
            conversion = {
 
126
              :above => :top,
 
127
              :below => :bottom
 
128
            }
 
129
          end
 
130
          # We clip for the first ones...
 
131
          for c in @curves[0..-2]
 
132
            c.make_closed_path(t, conversion[c.curve_style.region_position])
 
133
            t.clip
 
134
          end
 
135
          # We don't clip on the last one !
 
136
          c = @curves.last
 
137
          c.make_closed_path(t, conversion[c.curve_style.region_position])
 
138
        end
 
139
 
 
140
      end
 
141
    end
 
142
  end
 
143
end