Class: LCDProc::Widgets::Graph

Inherits:
Object
  • Object
show all
Includes:
LCDProc::Widget
Defined in:
lib/lcdproc/widgets/graph.rb

Constant Summary collapse

@@widget_count =
0

Instance Attribute Summary collapse

Attributes included from LCDProc::Widget

#id, #screen, #type, #visible

Instance Method Summary collapse

Methods included from LCDProc::Widget

add_support, #attach_to, #detach, new, supported_types

Constructor Details

#initialize(id = "GraphWidget_" + @@widget_count.to_s, col = 1, row = 1, length = 21 - col) ⇒ Graph

Creates a new Graph widget which can then be displayed anywhere on the screen.

  • id - A unique string which identifies this widget. Defaults to “GraphWidget_” + a sequence number.

  • col - The beginning column in which you wish to display the graph on screen. Defaults to 1.

  • row - The beginning row in which you wish to display the text on screen. Defaults to 1.

  • length - The length of the graph (how many vertical bars to show). Defaults to 21 - x for a 20x4 screen.

Example:

w = Graph.new

or

w = Graph.new( 'Test', 'This is a graph', 1, 5, 10 )

NOTE: The length of the graph is not changeable after it has been created



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/lcdproc/widgets/graph.rb', line 59

def initialize( id = "GraphWidget_" + @@widget_count.to_s, col = 1, row = 1, length = 21 - col)
  if id.nil?
    id = "GraphWidget_#{@@widget_count}"
  end
  
  @id = id
  @type = :graph
  @screen = nil
  @visible = false
  
  @x = col
  @y = row
  @length = length
  
  @bars = []
  @length.times{ |i| @bars << VBar.new( "#{self.id}_Bar_#{i}", 1, @x + i, @y ) }
  
  @@widget_count += 1
end

Instance Attribute Details

#barsObject

Returns the value of attribute bars.



35
36
37
# File 'lib/lcdproc/widgets/graph.rb', line 35

def bars
  @bars
end

#lengthObject (readonly)

Note that the lenght is not changeable after you have added a graph.



38
39
40
# File 'lib/lcdproc/widgets/graph.rb', line 38

def length
  @length
end

#xObject

Returns the value of attribute x.



35
36
37
# File 'lib/lcdproc/widgets/graph.rb', line 35

def x
  @x
end

#yObject

Returns the value of attribute y.



35
36
37
# File 'lib/lcdproc/widgets/graph.rb', line 35

def y
  @y
end

Instance Method Details

#hideObject

Actually sends the command to the LCDd server to remove the widget from the screen



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
# File 'lib/lcdproc/widgets/graph.rb', line 81

def hide
  
  # Only if we are visible, attempt to hide ourselves
  if @visible
    passed = true
    
    @bars.each do |bar|
      
      # If at any point we fail, bail out and return false
      if not bar.hide or not bar.detach
        passed = false
        break
      end
      
    end
    
    if passed
      @visible = false
      @screen.client.add_message( "Widget '#{@id}' is now hidden" )
      return true
    else
      @visible = true
      @screen.client.add_message( "Error: Widget '#{@id}' could NOT be hidden (#{response.message})" )
      return false
    end
  else
    return true
  end
  
end

#showObject

Actually sends the command to the LCDd server to add the widget to the screen



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
# File 'lib/lcdproc/widgets/graph.rb', line 114

def show
  
  # If we aren't already visible, attempt to make oursevles visible
  if not @visible
    commands = []
    passed = true
    
    @bars.each do |bar|
      
      # If at any point we fail, bail out and return false
      if not bar.attach_to( @screen ) or not bar.show( false )
        passed = false
        break
      end
      
    end
    
    @bars.each do |bar|
      commands << "widget_add #{@screen.id} #{bar.id} #{bar.type.to_s}"
    end
    
    response = @screen.client.send_command( Command.new( commands ) )
    
    if response.successful? and passed
      @visible = true
      @screen.client.add_message( "Widget '#{@id}' is now visible" )
      
      self.update
      
      return true
    else
      @visible = false
      @screen.client.add_message( "Error: Widget '#{@id}' could NOT be displayed (#{response.message})" )
      return false
    end
  else
    return true
  end
  
end

#updateObject

Sends to command to the LCDd server to update the widget on screen



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/lcdproc/widgets/graph.rb', line 157

def update
  
  if @screen
    commands = []
    
    @bars.each do |bar|
      commands << "widget_set #{@screen.id} #{bar.id} #{bar.x} #{bar.y} #{bar.height}"
    end
    
    response = @screen.client.send_command( Command.new( commands ) )
    
    if response.successful?
      @screen.client.add_message( "Widget '#{@id}' was successfully updated" )
      return true
    else
      @screen.client.add_message( "Error: Widget '#{@id}' was NOT successfully updated (#{response.message})" )
      return true
    end
  else
    @screen.client.add_message( "Error: Cannot update Widget '#{@id}' until it is attached to a screen" )
    return false
  end
end