Class: CDK::HISTOGRAM

Inherits:
CDKOBJS show all
Defined in:
lib/cdk/components/histogram.rb

Instance Attribute Summary

Attributes included from HasTitle

#title_attrib

Attributes included from HasScreen

#is_visible, #screen, #screen_index

Attributes included from ExitConditions

#exit_type

Attributes included from Bindings

#binding_list

Attributes included from Focusable

#accepts_focus, #has_focus

Attributes included from Borders

#BXAttr, #HZChar, #LLChar, #LRChar, #ULChar, #URChar, #VTChar, #border_size, #box

Instance Method Summary collapse

Methods inherited from CDKOBJS

#setBackgroundColor, #timeout, #validCDKObject, #validObjType

Methods included from WindowHooks

#refreshData, #saveData

Methods included from WindowInput

#getc, #getch, #inject, #setPostProcess, #setPreProcess

Methods included from HasTitle

#cleanTitle, #drawTitle, #init_title, #setTitle

Methods included from HasScreen

#SCREEN_XPOS, #SCREEN_YPOS, #init_screen, #wrefresh

Methods included from ExitConditions

#init_exit_conditions, #resetExitType, #setExitType

Methods included from Bindings

#bind, #bindableObject, #checkBind, #cleanBindings, #init_bindings, #isBind, #unbind

Methods included from Focusable

#focus, #init_focus, #unfocus

Methods included from Borders

#getBox, #init_borders, #setBXattr, #setBox, #setHZchar, #setLLchar, #setLRchar, #setULchar, #setURchar, #setVTchar

Methods included from Movement

#move, #move_specific, #position

Methods included from Converters

#char2Chtype, #charOf, #chtype2Char, #chtype2String, #decode_attribute, #encode_attribute

Methods included from Justifications

#justify_string

Methods included from Alignments

#alignxy

Constructor Details

#initialize(cdkscreen, xplace, yplace, height, width, orient, title, box, shadow) ⇒ HISTOGRAM

Returns a new instance of HISTOGRAM.



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
# File 'lib/cdk/components/histogram.rb', line 5

def initialize(cdkscreen, xplace, yplace, height, width, orient,
    title, box, shadow)
  super()
  parent_width = cdkscreen.window.getmaxx
  parent_height = cdkscreen.window.getmaxy

  self.setBox(box)

  box_height = CDK.setWidgetDimension(parent_height, height, 2)
  old_height = box_height

  box_width = CDK.setWidgetDimension(parent_width, width, 0)
  old_width = box_width

  box_width = self.setTitle(title, -(box_width + 1))

  # Increment the height by number of lines in in the title
  box_height += @title_lines

  # Make sure we didn't extend beyond the dimensions of the window.
  box_width = if box_width > parent_width
              then old_width
              else box_width
              end
  box_height = if box_height > parent_height
              then old_height
              else box_height
              end

  # Rejustify the x and y positions if we need to.
  xtmp = [xplace]
  ytmp = [yplace]
  alignxy(cdkscreen.window, xtmp, ytmp, box_width, box_height)
  xpos = xtmp[0]
  ypos = ytmp[0]

  # Set up the histogram data
  @screen = cdkscreen
  @parent = cdkscreen.window
  @win = Ncurses::WINDOW.new(box_height, box_width, ypos, xpos)
  @shadow_win = nil
  @box_width = box_width
  @box_height = box_height
  @field_width = box_width - 2 * @border_size
  @field_height = box_height - @title_lines - 2 * @border_size
  @orient = orient
  @shadow = shadow

  # Is the window nil
  if @win.nil?
    self.destroy
    return nil
  end

  @win.keypad(true)

  # Set up some default values.
  @filler = '#'.ord | Ncurses::A_REVERSE
  @stats_attr = Ncurses::A_NORMAL
  @stats_pos = CDK::TOP
  @view_type = :REAL
  @high = 0
  @low = 0
  @value = 0
  @lowx = 0
  @lowy = 0
  @highx = 0
  @highy = 0
  @curx = 0
  @cury = 0
  @low_string = ''
  @high_string = ''
  @cur_string = ''

  # Do we want a shadow?
  if shadow
    @shadow_win = Ncurses::WINDOW.new(box_height, box_width,
        ypos + 1, xpos + 1)
  end

  cdkscreen.register(:HISTOGRAM, self)
end

Instance Method Details

#activate(actions) ⇒ Object

This was added for the builder



89
90
91
# File 'lib/cdk/components/histogram.rb', line 89

def activate(actions)
  self.draw(@box)
end

#destroyObject

Destroy the widget.



384
385
386
387
388
389
390
391
392
393
394
395
396
# File 'lib/cdk/components/histogram.rb', line 384

def destroy
  self.cleanTitle

  # Clean up the windows.
  CDK.deleteCursesWindow(@shadow_win)
  CDK.deleteCursesWindow(@win)

  # Clean the key bindings.
  self.cleanBindings(:HISTOGRAM)

  # Unregister this object.
  CDK::SCREEN.unregister(:HISTOGRAM, self)
end

#draw(box) ⇒ Object

Draw the widget.



319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
# File 'lib/cdk/components/histogram.rb', line 319

def draw(box)
  battr = 0
  bchar = 0
  fattr = @filler & Ncurses::A_ATTRIBUTES
  hist_x = @title_lines + 1
  hist_y = @bar_size

  @win.werase

  # Box the widget if asked.
  if box
    Draw.drawObjBox(@win, self)
  end

  # Do we have a shadow to draw?
  if !(@shadow.nil?)
    Draw.drawShadow(@shadow_win)
  end

  self.drawTitle(@win)

  # If the user asked for labels, draw them in.
  if @view_type != :NONE
    # Draw in the low label.
    if @low_string.size > 0
      Draw.writeCharAttrib(@win, @lowx, @lowy, @low_string,
          @stats_attr, @orient, 0, @low_string.size)
    end

    # Draw in the current value label.
    if @cur_string.size > 0
      Draw.writeCharAttrib(@win, @curx, @cury, @cur_string,
          @stats_attr, @orient, 0, @cur_string.size)
    end

    # Draw in the high label.
    if @high_string.size > 0
      Draw.writeCharAttrib(@win, @highx, @highy, @high_string,
          @stats_attr, @orient, 0, @high_string.size)
    end
  end

  if @orient == CDK::VERTICAL
    hist_x = @box_height - @bar_size - 1
    hist_y = @field_width
  end

  # Draw the histogram bar.
  (hist_x...@box_height - 1).to_a.each do |x|
    (1..hist_y).each do |y|
      battr = @win.mvwinch(x, y)
      
      if battr == ' '.ord
        @win.mvwaddch(x, y, @filler)
      else
        @win.mvwaddch(x, y, battr | fattr)
      end
    end
  end

  # Refresh the window
  wrefresh
end

#eraseObject

Erase the widget from the screen.



399
400
401
402
403
404
# File 'lib/cdk/components/histogram.rb', line 399

def erase
  if self.validCDKObject
    CDK.eraseCursesWindow(@win)
    CDK.eraseCursesWindow(@shadow_win)
  end
end

#getFillerCharObject



304
305
306
# File 'lib/cdk/components/histogram.rb', line 304

def getFillerChar
  return @filler
end

#getHighValueObject



268
269
270
# File 'lib/cdk/components/histogram.rb', line 268

def getHighValue
  return @high
end

#getLowValueObject



264
265
266
# File 'lib/cdk/components/histogram.rb', line 264

def getLowValue
  return @low
end

#getStatsAttrObject



295
296
297
# File 'lib/cdk/components/histogram.rb', line 295

def getStatsAttr
  return @stats_attr
end

#getStatsPosObject



286
287
288
# File 'lib/cdk/components/histogram.rb', line 286

def getStatsPos
  return @stats_pos
end

#getValueObject



260
261
262
# File 'lib/cdk/components/histogram.rb', line 260

def getValue
  return @value
end

#getViewTypeObject



277
278
279
# File 'lib/cdk/components/histogram.rb', line 277

def getViewType
  return @view_type
end

#object_typeObject



406
407
408
# File 'lib/cdk/components/histogram.rb', line 406

def object_type
  :HISTOGRAM
end

#set(view_type, stats_pos, stats_attr, low, high, value, filler, box) ⇒ Object

Set various widget attributes



94
95
96
97
98
99
100
101
# File 'lib/cdk/components/histogram.rb', line 94

def set(view_type, stats_pos, stats_attr, low, high, value, filler, box)
  self.setDisplayType(view_type)
  self.setStatsPos(stats_pos)
  self.setValue(low, high, value)
  self.setFillerChar(filler)
  self.setStatsAttr(stats_attr)
  self.setBox(box)
end

#setBKattr(attrib) ⇒ Object

Set the background attribute of the widget.



309
310
311
# File 'lib/cdk/components/histogram.rb', line 309

def setBKattr(attrib)
  @win.wbkgd(attrib)
end

#setDisplayType(view_type) ⇒ Object

Set the histogram display type



273
274
275
# File 'lib/cdk/components/histogram.rb', line 273

def setDisplayType(view_type)
  @view_type = view_type
end

#setFillerChar(character) ⇒ Object

Set the character to use when drawing the widget.



300
301
302
# File 'lib/cdk/components/histogram.rb', line 300

def setFillerChar(character)
  @filler = character
end

#setStatsAttr(stats_attr) ⇒ Object

Set the attribute of the statistics.



291
292
293
# File 'lib/cdk/components/histogram.rb', line 291

def setStatsAttr(stats_attr)
  @stats_attr = stats_attr
end

#setStatsPos(stats_pos) ⇒ Object

Set the position of the statistics information.



282
283
284
# File 'lib/cdk/components/histogram.rb', line 282

def setStatsPos(stats_pos)
  @stats_pos = stats_pos
end

#setValue(low, high, value) ⇒ Object

Set the values for the widget.



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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
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
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
251
252
253
254
255
256
257
258
# File 'lib/cdk/components/histogram.rb', line 104

def setValue(low, high, value)
  # We should error check the information we have.
  @low = if low <= high then low else 0 end
  @high = if low <= high then high else 0 end
  @value = if low <= value && value <= high then value else 0 end
  # Determine the percentage of the given value.
  @percent = if @high == 0 then 0 else 1.0 * @value / @high end

  # Determine the size of the histogram bar.
  if @orient == CDK::VERTICAL
    @bar_size = @percent * @field_height
  else
    @bar_size = @percent * @field_width
  end

  # We have a number of variables which determine the personality of the
  # histogram.  We have to go through each one methodically, and set them
  # correctly.  This section does this.
  if @view_type != :NONE
    if @orient == CDK::VERTICAL
      if @stats_pos == CDK::LEFT || @stats_pos == CDK::BOTTOM
        # Set the low label attributes.
        @low_string = @low.to_s
        @lowx = 1
        @lowy = @box_height - @low_string.size - 1

        # Set the high label attributes
        @high_string = @high.to_s
        @highx = 1
        @highy = @title_lines + 1

        string = ''
        # Set the current value attributes.
        string = if @view_type == :PERCENT
                 then "%3.1f%%" % [1.0 * @percent * 100]
                 elsif @view_type == :FRACTION
                     string = "%d/%d" % [@value, @high]
                 else string = @value.to_s
                 end
        @cur_string = string
        @curx = 1
        @cury = (@field_height - string.size) / 2 + @title_lines + 1
      elsif @stats_pos == CDK::CENTER
        # Set the lower label attributes
        @low_string = @low.to_s
        @lowx = @field_width / 2 + 1
        @lowy = @box_height - @low_string.size - 1

        # Set the high label attributes
        @high_string = @high.to_s
        @highx = @field_width / 2 + 1
        @highy = @title_lines + 1

        # Set the stats label attributes
        string = if @view_type == :PERCENT
                 then "%3.2f%%" % [1.0 * @percent * 100]
                 elsif @view_type == :FRACTIOn
                     "%d/%d" % [@value, @high]
                 else @value.to_s
                 end

        @cur_string = string
        @curx = @field_width / 2 + 1
        @cury = (@field_height - string.size) / 2 + @title_lines + 1
      elsif @stats_pos == CDK::RIGHT || @stats_pos == CDK::TOP
        # Set the low label attributes.
        @low_string = @low.to_s
        @lowx = @field_width
        @lowy = @box_height - @low_string.size - 1

        # Set the high label attributes.
        @high_string = @high.to_s
        @highx = @field_width
        @highy = @title_lines + 1

        # Set the stats label attributes.
        string = if @view_type == :PERCENT
                 then "%3.2f%%" % [1.0 * @percent * 100]
                 elsif @view_type == :FRACTION
                     "%d/%d" % [@value, @high]
                 else @value.to_s
                 end
        @cur_string = string
        @curx = @field_width
        @cury = (@field_height - string.size) / 2 + @title_lines + 1
      end
    else
      # Alignment is HORIZONTAL
      if @stats_pos == CDK::TOP || @stats_pos == CDK::RIGHT
        # Set the low label attributes.
        @low_string = @low.to_s
        @lowx = 1
        @lowy = @title_lines + 1

        # Set the high label attributes.
        @high_string = @high.to_s
        @highx = @box_width - @high_string.size - 1
        @highy = @title_lines + 1

        # Set the stats label attributes.
        string = if @view_type == :PERCENT
                 then "%3.1f%%" % [1.0 * @percent * 100]
                 elsif @view_type == :FRACTION
                     "%d/%d" % [@value, @high]
                 else @value.to_s
                 end
        @cur_string = string
        @curx = (@field_width - @cur_string.size) / 2 + 1
        @cury = @title_lines + 1
      elsif @stats_pos == CDK::CENTER
        # Set the low label attributes.
        @low_string = @low.to_s
        @lowx = 1
        @lowy = (@field_height / 2) + @title_lines + 1

        # Set the high label attributes.
        @high_string = @high.to_s
        @highx = @box_width - @high_string.size - 1
        @highy = @field_height / 2 + @title_lines + 1

        # Set the stats label attributes.
        string = if @view_type == :PERCENT
                 then "%3.1f%%" % [1.0 * @percent * 100]
                 elsif @view_type == :FRACTION
                     "%d/%d" % [@value, @high]
                 else @value.to_s
                 end
        @cur_string = string
        @curx = (@field_width - @cur_string.size) / 2 + 1
        @cury = @field_height / 2 + @title_lines + 1
      elsif @stats_pos == CDK::BOTTOM || @stats_pos == CDK::LEFT
        # Set the low label attributes.
        @low_string = @low.to_s
        @lowx = 1
        @lowy = @box_height -2 * @border_size

        # Set the high label attributes.
        @high_string = @high.to_s
        @highx = @box_width - @high_string.size - 1
        @highy = @box_height - 2 * @border_size

        # Set the stats label attributes.
        string = if @view_type == :PERCENT
                 then "%3.1f%%" % [1.0 * @percent * 100]
                 elsif @view_type == :FRACTION
                     "%d/%d" % [@value, @high]
                 else @value.to_s
                 end
        @cur_string = string
        @curx = (@field_width - @cur_string.size) / 2 + 1
        @cury = @box_height - 2 * @border_size
      end
    end
  end
end