forked from cedlemo/blingbling
-
Notifications
You must be signed in to change notification settings - Fork 0
/
progress_graph.lua
297 lines (256 loc) · 10.2 KB
/
progress_graph.lua
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
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
--@author cedlemo
local setmetatable = setmetatable
local ipairs = ipairs
local type = type
local math = math
local string = string
local helpers = require('blingbling.helpers')
local base = require("wibox.widget.base")
local color = require("gears.color")
local superproperties = require('blingbling.superproperties')
--- A progress graph widget.
--@module blingbling.progress_graph
local progressgraph = { mt = {} }
---Fill all the widget (width * height) with this color (default is none ).
--@usage mygraph:set_background_color(string) -->"#rrggbbaa"
--@name set_background_color
--@class function
--@param graph the graph
--@param color a string "#rrggbbaa" or "#rrggbb"
---Fill the graph area background with this color (default is none).
--@usage mygraph:set_graph_background_color(string) -->"#rrggbbaa"
--@name set_graph_background_color
--@class function
--@param graph the graph
--@param color a string "#rrggbbaa" or "#rrggbb"
---Set rounded corners for background and graph background.
--@usage mygraph:set_rounded_size(a) -> a in [0,1]
--@name set_rounded_size
--@class function
--@param graph the graph
--@param rounded_size float in [0,1]
---Define the top and bottom margin for the graph area.
--@usage mygraph:set_v_margin(integer)
--@name set_v_margin
--@class function
--@param graph the graph
--@param margin an integer for top and bottom margin
---Define the left and right margin for the graph area.
--@usage mygraph:set_h_margin(integer)
--@name set_h_margin
--@class function
--@param graph the graph
--@param margin an integer for left and right margin
---Define the graph color.
--@usage mygraph:set_graph_color(string) -->"#rrggbbaa"
--@name set_graph_color
--@class function
--@param graph the graph
--@param color a string "#rrggbbaa" or "#rrggbb"
---Define the graph outline.
--@usage mygraph:set_graph_line_color(string) -->"#rrggbbaa"
--@name set_graph_line_color
--@class function
--@param graph the graph
--@param color a string "#rrggbbaa" or "#rrggbb"
---Display text on the graph or not.
--@usage mygraph:set_show_text(boolean) --> true or false
--@name set_show_text
--@class function
--@param graph the graph
--@param boolean true or false (default is false)
---Define displayed text value format string
--@usage mygraph:set_value_format(string) --> "%2.f"
--@name set_value_format
--@class function
--@param graph the graph
--@param printf format string for display text
---Define the color of the text.
--@usage mygraph:set_text_color(string) -->"#rrggbbaa"
--@name set_text_color
--@class function
--@param graph the graph
--@param color a string "#rrggbbaa" or "#rrggbb" defaul is white
---Define the background color of the text.
--@usage mygraph:set_text_background_color(string) -->"#rrggbbaa"
--@name set_text_background_color
--@class function
--@param graph the graph
--@param color a string "#rrggbbaa" or "#rrggbb"
---Define the text font size.
--@usage mygraph:set_font_size(integer)
--@name set_font_size
--@class function
--@param graph the graph
--@param size the font size
---Define the template of the text to display.
--@usage mygraph:set_label(string)
--By default the text is : (value_send_to_the_widget *100) .. "%"
--static string: example set_label("CPU usage:") will display "CUP usage:" on the graph
--dynamic string: use $percent in the string example set_label("Load $percent %") will display "Load 10%"
--@name set_label
--@class function
--@param graph the graph
--@param text the text to display
---Define if the graph should increase/decrease horizontaly.
--@usage mygraph:set_horizontal(boolean) --> true or false
--@name set_horizontal
--@class function
--@param graph the graph
--@param boolean true or false (false by default)
local data = setmetatable({}, { __mode = "k" })
local properties = { "width", "height", "v_margin", "h_margin",
"background_color",
"graph_background_color","rounded_size",
"graph_color", "graph_line_color","show_text", "text_color",
"text_background_color" ,"label", "font_size","font","horizontal",
"value_format"}
function progressgraph.draw(p_graph, wibox, cr, width, height)
-- We want one pixel wide lines
cr:set_line_width(1)
-- Set the values we need
local value = data[p_graph].value
local props = helpers.load_properties(properties, data, p_graph, superproperties)
--Generate Background (background widget)
if data[p_graph].background_color then
helpers.draw_rounded_corners_rectangle( cr,
0,
0,
width,
height,
props.background_color,
props.rounded_size)
end
--draw a graph with graph_background_color
if props.horizontal == true then
helpers.draw_rounded_corners_horizontal_graph( cr,
props.h_margin,
props.v_margin,
width - props.h_margin,
height - props.v_margin,
props.graph_background_color,
props.graph_color,
props.rounded_size,
value,
props.graph_line_color)
else
helpers.draw_rounded_corners_vertical_graph( cr,
props.h_margin,
props.v_margin,
width - props.h_margin,
height - props.v_margin,
props.graph_background_color,
props.graph_color,
props.rounded_size,
value,
props.graph_line_color)
end
if props.show_text == true then
cr:set_font_size(props.font_size)
if type(props.font) == "string" then
cr:select_font_face(props.font,nil,nil)
elseif type(props.font) == "table" then
cr:select_font_face(props.font.family or "Sans", props.font.slang or
"normal", props.font.weight or "normal")
end
local value = string.format(props.value_format, data[p_graph].value * 100)
if data[p_graph].label then
text=string.gsub(data[p_graph].label,"$percent", value)
else
text=value .. "%"
end
--if vertical graph, text is at the middle of the width, if vertical bar text is at the middle of the height
if props.horizontal == nil or props.horizontal == false then
helpers.draw_text_and_background(cr,
text,
width/2,
height/2 ,
props.text_background_color,
props.text_color,
true,
true,
false,
false)
else
helpers.draw_text_and_background(cr,
text,
props.h_margin,
height/2 ,
props.text_background_color,
props.text_color,
false,
true,
false,
false)
end
end
end
function progressgraph.fit(p_graph, width, height)
return data[p_graph].width, data[p_graph].height
end
--- Set the p_graph value.
-- @param p_graph The progress bar.
-- @param value The progress bar value between 0 and 1.
function set_value(p_graph, value)
if not p_graph then return end
local value = value or 0
local max_value = data[p_graph].max_value or 1
data[p_graph].value = math.min(max_value, math.max(0, value))
p_graph:emit_signal("widget::updated")
return p_graph
end
--- Set the p_graph height.
-- @param height The height to set.
function progressgraph:set_height( height)
data[self].height = height
self:emit_signal("widget::updated")
return self
end
--- Set the p_graph width.
-- @param width The width to set.
function progressgraph:set_width( width)
data[self].width = width
self:emit_signal("widget::updated")
return self
end
-- Build properties function
for _, prop in ipairs(properties) do
if not progressgraph["set_" .. prop] then
progressgraph["set_" .. prop] = function(p_graph, value)
data[p_graph][prop] = value
p_graph:emit_signal("widget::updated")
return p_graph
end
end
end
--- Create a p_graph widget.
-- @param args Standard widget() arguments. You should add width and height
-- key to set p_graph geometry.
-- @return A p_graph widget.
function progressgraph.new(args)
local args = args or {}
args.width = args.width or 100
args.height = args.height or 20
if args.width < 5 or args.height < 5 then return end
local p_graph = base.make_widget()
data[p_graph] = {}
for _, v in ipairs(properties) do
data[p_graph][v] = args[v]
end
data[p_graph].value = 0
data[p_graph].max_value = 1
-- Set methods
for _, prop in ipairs(properties) do
p_graph["set_" .. prop] = progressgraph["set_" .. prop]
end
p_graph.set_value = set_value
p_graph.add_value = set_value
p_graph.draw = progressgraph.draw
p_graph.fit = progressgraph.fit
return p_graph
end
function progressgraph.mt:__call(...)
return progressgraph.new(...)
end
return setmetatable(progressgraph, progressgraph.mt)
-- vim: filetype=lua:expandtab:shiftwidth=2:tabstop=8:softtabstop=2:textwidth=80