Class: RubyCurses::ModStack::BaseStack

Inherits:
Object
  • Object
show all
Defined in:
lib/canis/core/util/basestack.rb

Overview

Base class for stacks and flows. Will manage determining row col and width height of objects Stacks place objects one below another. Flows place objects to the right of the previous. Orientation can be reversed.

Direct Known Subclasses

Flow, Stack

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}, components = []) ⇒ BaseStack

Returns a new instance of BaseStack.



23
24
25
26
27
28
29
30
# File 'lib/canis/core/util/basestack.rb', line 23

def initialize config={}, components=[]
  @config = config
  config.each do |k, v|
    instance_variable_set "@#{k}", v
  end
  @components = components
  @calc_needed = true
end

Instance Attribute Details

#componentsObject

Returns the value of attribute components.



20
21
22
# File 'lib/canis/core/util/basestack.rb', line 20

def components
  @components
end

#configObject (readonly)

Returns the value of attribute config.



21
22
23
# File 'lib/canis/core/util/basestack.rb', line 21

def config
  @config
end

#formObject

Returns the value of attribute form.



22
23
24
# File 'lib/canis/core/util/basestack.rb', line 22

def form
  @form
end

Instance Method Details

#check_coords(e) ⇒ Object

Traverses the comopnent tree and calculates weightages for all components based on what has been specified by user



175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/canis/core/util/basestack.rb', line 175

def check_coords e # stack
  r = e.row
  c = e.col
  if r >= row + height
    $log.warn "XXX: WARN e.class is out of bounds row #{r} "
    e.visible = false
  end
  if c >= col + width
    $log.warn "XXX: WARN e.class is out of bounds col #{c} "
    e.visible = false
  end
end

#decrease(c = @current_component) ⇒ Object



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/canis/core/util/basestack.rb', line 219

def decrease c=@current_component
  p = self #c.parent_component
  ci = p.components.index(c)
  ni = ci + 1
  if p.components[ni].nil?
    ni = nil
  end
  case p
  when Flow
    # increase width of current and reduce from neighbor
    if ni
      n = p.components[ni]
      $log.debug "XXX: INC fl current #{ci}, total#{p.components.count}, next #{n} "

      c.width -= 1
      n.width += 1
      n.col   -= 1
    end

  when Stack
    if ni
      n = p.components[ni]
      $log.debug "XXX: INC fl current #{ci}, total#{p.components.count}, next #{n} "

      c.height -= 1
      n.height += 1
      n.row   -= 1
    end
    $log.debug "XXX: INC st current #{ci}, total#{p.components.count} "
  end

end

#focusableObject



59
# File 'lib/canis/core/util/basestack.rb', line 59

def focusable; false; end

#increase(c = @current_component) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/canis/core/util/basestack.rb', line 187

def increase c=@current_component
  p = self #c.parent_component
  ci = p.components.index(c)
  ni = ci + 1
  if p.components[ni].nil?
    ni = nil
  end
  case p
  when Flow
    # increase width of current and reduce from neighbor
    if ni
      n = p.components[ni]
      $log.debug "XXX: INC fl current #{ci}, total#{p.components.count}, next #{n} "

      c.width += 1
      n.width -= 1
      n.col   += 1
    end

  when Stack
    if ni
      n = p.components[ni]
      $log.debug "XXX: INC fl current #{ci}, total#{p.components.count}, next #{n} "

      c.height += 1
      n.height -= 1
      n.row   += 1
    end
    $log.debug "XXX: INC st current #{ci}, total#{p.components.count} "
  end

end

#override_graphic(gr) ⇒ Object



56
57
58
# File 'lib/canis/core/util/basestack.rb', line 56

def override_graphic gr
  @graphic = gr
end

#recalcObject

Calculates row col and width height for each subc-omponent based on coords of Container This is to be called only when the container has got its coordinates (i.e Containers repaint). This should be in this objects repaint.



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
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/canis/core/util/basestack.rb', line 64

def recalc
  @calc_needed = false
  comp = self
  if comp.is_a? BaseStack
    check_coords comp
    @margin_left   ||= 0
    @margin_right  ||= 0
    @margin_top    ||= 0
    @margin_bottom ||= 0
    if comp.is_a? Stack
      r   = row + @margin_top
      rem = 0
      ht = height - (@margin_top + @margin_bottom)
      if @orientation == :bottom 
        mult = -1
        comps = @components.reverse
        r = row + height - @margin_bottom

      else
        mult = 1
        comps = @components
   
      end
      comps.each { |e| 
        # should only happen if expandable FIXME
        e.margin_top ||= 0
        e.margin_bottom ||= 0
        e.height = 0.01 * e.weight * (ht - (e.margin_top + e.margin_bottom)) 
        hround = e.height.floor
        rem += e.height - hround
        e.height = hround #- (@margin_top + @margin_bottom)
        # rounding creates a problem, since 0.5 gets rounded up and we can exceed bound
        # So i floor, and maintain the lost space, and add it back when it exceeds 1
        # This way the last components gets stretched to meet the end, which is required
        # when the height of the stack is odd and there's a left-over row
        if rem >= 1
          e.height += 1
          rem = 0
        end
        # Item level margins have not been accounted for when calculating weightages, and
        # should not be used on the weightage axis
        r += e.margin_top
        if @orientation == :bottom
          r += e.height * mult
          e.row = r 
        else
          e.row = r 
          r += e.height + 0
        end
        e.margin_left ||= 0
        e.margin_right ||= 0
        e.width = width - (@margin_left + @margin_right + e.margin_left + e.margin_right)
        e.col = col + @margin_left + e.margin_left # ??? XXX
        #$log.debug "XXX: recalc stack #{e.widget.class} r:#{e.row} c:#{e.col} h:#{e.height} = we:#{e.weight} * h:#{height} "
        #e.col_offset = col_offset # ??? XXX
        check_coords e
        e.repaint_all(true)
        e.recalc if e.is_a? BaseStack
      }
    elsif comp.is_a? Flow
      c = col + @margin_left #+ col_offset
      rem = 0
      wd = width - (@margin_left + @margin_right)
      # right_to_left orientation
      if @orientation == :right
        mult = -1
        comps = @components.reverse
        c = col + width - @margin_right
        $log.debug "XXX:  ORIENT1f recalc #{@orientation} "
      else
        mult = 1
        comps = @components
        $log.debug "XXX:  ORIENT2f recalc #{@orientation} "
      end
      comps.each { |e| 
        e.width = e.weight * wd  * 0.01
        wround = e.width.floor
        rem += e.width - wround
        e.width = wround
        # see comment in prev block regarding remaininder
        if rem >= 1
          e.width += 1
          rem = 0
        end
        e.height = height - (@margin_top + @margin_bottom) #* weight * 0.01
        #e.height = e.height.round
        if @orientation == :right
          c += e.width * mult # mult 1 or -1
          e.col = c
        else
          e.col = c
          c += e.width * mult # mult 1 or -1
        end
        e.row = row + @margin_top
        check_coords e
        $log.debug "XXX: recalc flow #{e.widget.class} r:#{e.row} c:#{e.col} h:#{e.height} = we:#{e.weight} * w:#{width} "
        e.repaint_all(true)   # why not happening when we change row, hieght etc
        e.recalc if e.is_a? BaseStack
      }
    end
  else
    alert "in else recalc DOES NOT COME HERE "
    comp.col    = comp.parent.col
    comp.row    = comp.parent.row
    comp.height = comp.parent.height
    comp.width  = comp.parent.width
    $log.debug "XXX: recalc else #{comp.class} r #{comp.row} c #{comp.col} . h #{comp} height w #{comp.width} "
  end
end

#repaintObject

alias :parent= :parent_component



47
48
49
50
51
52
# File 'lib/canis/core/util/basestack.rb', line 47

def repaint # stack
  $log.debug "XXX: stack repaint recalc #{@calc_needed} "
  @components.each { |e| e.form = @form unless e.form } #unless @calc_needed
  recalc if @calc_needed
  @components.each { |e| e.repaint }
end

#repaint_all(x) ⇒ Object



53
54
55
# File 'lib/canis/core/util/basestack.rb', line 53

def repaint_all x
  @calc_needed = true
end

#to_sObject



251
252
253
# File 'lib/canis/core/util/basestack.rb', line 251

def to_s
  @components
end