summaryrefslogtreecommitdiffstats
path: root/qtruby/rubylib/examples/qt-examples/chart/element.rb
blob: bc6c0d518403f439e010ef773382cdacd95a5b8d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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
153
154
155
156
157
158
159
160
161
class Element

    FIELD_SEP = ':'
    PROPOINT_SEP = ';'
    XY_SEP = ','
    
    EPSILON = 0.0000001
    
    INVALID = -1
    NO_PROPORTION = -1
    MAX_PROPOINTS = 3  # One proportional point per chart type
    
    attr_accessor :value, :valueColor, :valuePattern, :label, :labelColor

    def initialize( value = INVALID, valueColor = TQt::gray,
            valuePattern = TQt::SolidPattern,
            label = nil,
            labelColor = TQt::black )
        init( value, valueColor, valuePattern, label, labelColor )
        @propoints = []
        for i in 0...MAX_PROPOINTS * 2
            @propoints[i] = NO_PROPORTION
        end
    end
    
    def isValid() return @value > EPSILON end
    
        
    def init( value, valueColor, valuePattern,
                        label, labelColor )
        @value = value
        @valueColor = valueColor
        if  TQt::SolidPattern >= valuePattern || TQt::DiagCrossPattern <= valuePattern
            valuePattern = TQt::SolidPattern
        end
        @valuePattern = valuePattern
        @label = label
        @labelColor = labelColor
    end
    
    def set( value = INVALID, valueColor = TQt::gray,
            valuePattern = TQt::SolidPattern,
            label = nil,
            labelColor = TQt::black )
        init( value, valueColor, valuePattern, label, labelColor )
    end
    
    def setValuePattern( valuePattern )
        if  valuePattern < TQt::SolidPattern.to_i || valuePattern > TQt::DiagCrossPattern.to_i
            valuePattern = TQt::SolidPattern
        end
        @valuePattern = valuePattern
    end
    
    
    def proX( index )
        return @propoints[2 * index]
    end
    
    
    def proY( index )
        return @propoints[(2 * index) + 1]
    end
    
    
    def setProX( index, value )
        @propoints[2 * index] = value
    end
    
    
    def setProY( index, value )
        @propoints[(2 * index) + 1] = value
    end
    
end

class TQt::TextStream

    alias op_write <<
	
    def <<( item )
		if !item.kind_of? Element
			return op_write(item)
		end
		element = item
        self << element.value() << Element::FIELD_SEP <<
        element.valueColor().name() << Element::FIELD_SEP << 
        element.valuePattern().to_i << Element::FIELD_SEP <<
        element.labelColor().name() << Element::FIELD_SEP
    
        for i in 0...Element::MAX_PROPOINTS
            self << element.proX( i ) << Element::XY_SEP << element.proY( i )
            self << ( i == Element::MAX_PROPOINTS - 1 ? Element::FIELD_SEP : Element::PROPOINT_SEP )
        end
    
        self << element.label() << "\n"
    
        return self
    end
    
    alias op_read >>
    
	def >>( item )
		if !item.kind_of? Element
			return op_read(item)
		end
		
		element = item
        data = readLine()
        element.value = Element::INVALID
    
        errors = 0
    
        fields = data.split( Element::FIELD_SEP )
        if  fields.length() >= 4 
            value = fields[0].to_f
            if  value.nil?
                errors += 1
            end
            valueColor = TQt::Color.new( fields[1] )
            if  !valueColor.isValid()
                errors += 1
            end
            valuePattern = fields[2].to_i
            if  valuePattern.nil?
                errors += 1
            end
            labelColor = TQt::Color.new( fields[3] )
            if  !labelColor.isValid()
                errors += 1
            end
            propoints = fields[4].split( Element::PROPOINT_SEP )
            label = data.split(Element::FIELD_SEP)[5]
            if errors == 0
                element.set( value, valueColor, valuePattern, label, labelColor )
                i = 0
                propoints.each do |point|
                errors = 0
                    xy = point.split( Element::XY_SEP )
                    x = xy[0].to_f
                    if  x.nil? || x <= 0.0 || x >= 1.0
                        errors += 1
                    end
                    y = xy[1].to_f
                    if  y.nil? || y <= 0.0 || y >= 1.0
                        errors += 1
                    end
                    if  errors > 0
                        x = y = Element::NO_PROPORTION
                    end
                    element.setProX( i, x )
                    element.setProY( i, y )
                    i += 1
                end
            end
        end
    
        return self
    end

end