graph: Port to oocairo

Signed-off-by: Uli Schlachter <psychon@znc.in>
This commit is contained in:
Uli Schlachter 2010-09-29 18:57:52 +02:00
parent d74198b7db
commit 6375ce4175
1 changed files with 39 additions and 30 deletions

View File

@ -9,9 +9,10 @@ local ipairs = ipairs
local math = math local math = math
local table = table local table = table
local type = type local type = type
local capi = { image = image, local capi = { oocairo = oocairo,
widget = widget } widget = widget }
local layout = require("awful.widget.layout") local layout = require("awful.widget.layout")
local color = require("gears.color")
--- A graph widget. --- A graph widget.
module("awful.widget.graph") module("awful.widget.graph")
@ -84,7 +85,8 @@ local properties = { "width", "height", "border_color", "stack",
local function update(graph) local function update(graph)
-- Create new empty image -- Create new empty image
local img = capi.image.argb32(data[graph].width, data[graph].height, nil) local img = capi.oocairo.image_surface_create("argb32", data[graph].width, data[graph].height)
local cr = capi.oocairo.context_create(img)
local max_value = data[graph].max_value local max_value = data[graph].max_value
local values = data[graph].values local values = data[graph].values
@ -93,6 +95,8 @@ local function update(graph)
border_width = 1 border_width = 1
end end
cr:set_line_width(1)
-- Draw a stacked graph -- Draw a stacked graph
if data[graph].stack then if data[graph].stack then
@ -107,33 +111,34 @@ local function update(graph)
end end
-- Draw the background first -- Draw the background first
img:draw_rectangle(border_width, border_width, cr:rectangle(border_width, border_width,
data[graph].width - (2 * border_width), data[graph].width - (2 * border_width),
data[graph].height, data[graph].height)
true, data[graph].background_color or "#000000aa") cr:set_source(color(data[graph].background_color or "#000000aa"))
cr:fill()
for i = 0, data[graph].width - (2 * border_width) do for i = 0, data[graph].width - (2 * border_width) do
local rel_i = 0 local rel_i = 0
local rel_x = data[graph].width - border_width - i - 1 local rel_x = data[graph].width - border_width - i - 0.5
if data[graph].stack_colors then if data[graph].stack_colors then
for idx, color in ipairs(data[graph].stack_colors) do for idx, col in ipairs(data[graph].stack_colors) do
local stack_values = values[idx] local stack_values = values[idx]
if stack_values and i < #stack_values then if stack_values and i < #stack_values then
local value = stack_values[#stack_values - i] + rel_i local value = stack_values[#stack_values - i] + rel_i
img:draw_line(rel_x, border_width - 1 + cr:move_to(rel_x, border_width - 0.5 +
math.ceil((data[graph].height - 2 * border_width) * (1 - (rel_i / max_value))), (data[graph].height - 2 * border_width) * (1 - (rel_i / max_value)))
rel_x, border_width - 1 + cr:line_to(rel_x, border_width - 0.5 +
math.ceil((data[graph].height - 2 * border_width) * (1 - (value / max_value))), (data[graph].height - 2 * border_width) * (1 - (value / max_value)))
color or "#ff0000") cr:set_source(color(col or "#ff0000"))
cr:stroke()
rel_i = value rel_i = value
end end
end end
end end
end end
else else
if data[graph].scale then if data[graph].scale then
for _, v in ipairs(values) do for _, v in ipairs(values) do
if v > max_value then if v > max_value then
@ -150,10 +155,11 @@ local function update(graph)
data[graph].gradient_colors, data[graph].gradient_colors,
data[graph].gradient_angle or 270) data[graph].gradient_angle or 270)
else else
img:draw_rectangle(border_width, border_width, cr:rectangle(border_width, border_width,
data[graph].width - (2 * border_width), data[graph].width - (2 * border_width),
data[graph].height - (2 * border_width), data[graph].height - (2 * border_width))
true, data[graph].color or "#ff0000") cr:set_source(color(data[graph].color or "#ff0000"))
cr:fill()
end end
-- Draw the background on no value -- Draw the background on no value
@ -163,31 +169,34 @@ local function update(graph)
local value = values[#values - i] local value = values[#values - i]
if value >= 0 then if value >= 0 then
value = value / max_value value = value / max_value
img:draw_line(data[graph].width - border_width - i - 1, cr:move_to(data[graph].width - border_width - i - 0.5,
border_width - 1 + border_width - 0.5 +
math.ceil((data[graph].height - 2 * border_width) * (1 - value)), (data[graph].height - 2 * border_width) * (1 - value))
data[graph].width - border_width - i - 1, cr:line_to(data[graph].width - border_width - i - 0.5,
border_width - 1, border_width - 1)
data[graph].background_color or "#000000aa")
end end
end end
cr:set_source(color(data[graph].background_color or "#000000aa"))
cr:stroke()
end end
-- If we didn't draw values in full length, draw a square -- If we didn't draw values in full length, draw a square
-- over the last, left, part to reset everything to 0 -- over the last, left, part to reset everything to 0
if #values < data[graph].width - (2 * border_width) then if #values < data[graph].width - (2 * border_width) then
img:draw_rectangle(border_width, border_width, cr:rectangle(border_width, border_width,
data[graph].width - (2 * border_width) - #values, data[graph].width - (2 * border_width) - #values,
data[graph].height - (2 * border_width), data[graph].height - (2 * border_width))
true, data[graph].background_color or "#000000aa") cr:set_source(color(data[graph].background_color or "#000000aa"))
cr:fill()
end end
end end
-- Draw the border last so that it overlaps already drawn values -- Draw the border last so that it overlaps already drawn values
if data[graph].border_color then if data[graph].border_color then
-- Draw the border -- Draw the border
img:draw_rectangle(0, 0, data[graph].width, data[graph].height, cr:rectangle(0.5, 0.5, data[graph].width - 0.5, data[graph].height - 0.5)
false, data[graph].border_color or "white") cr:set_source(color(data[graph].background_color or "#ffffff"))
cr:stroke()
end end
-- Update the image -- Update the image