Python gi.repository.Gdk 模块,cairo_set_source_pixbuf() 实例源码
我们从Python开源项目中,提取了以下13个代码示例,用于说明如何使用gi.repository.Gdk.cairo_set_source_pixbuf()。
def do_render(self, cr, widget, bg_area, cell_area, flags):
pixbuf = GdkPixbuf.Pixbuf.new(Colorspace.RGB, True, 8, cell_area.width, cell_area.height)
Gdk.cairo_set_source_pixbuf(cr, pixbuf, cell_area.x, cell_area.y)
## Draw a filled square
cr.set_source_rgb(*self.rgb_triplet)
cr.rectangle(cell_area.x+1, cell_area.y+1, cell_area.width-2, cell_area.height-2)
cr.fill()
## Outline it black
cr.set_source_rgb(0, .8, 0)
cr.rectangle(cell_area.x+1, cell_area.y+1, cell_area.width-2, cell_area.height-2)
cr.stroke()
## Draw a blue line
#cr.set_source_rgb(0, 0, .8)
#cr.move_to(cell_area.x+5, cell_area.y+5)
#cr.line_to(cell_area.x+15, cell_area.y+15)
#cr.stroke()
## Semitransparent overwrite of whole image
#cr.set_source_rgb(0,0,0)
#cr.paint_with_alpha(self.alpha)
def do_render(self, cr, widget, bg_area, cell_area, flags):
pixbuf = GdkPixbuf.Pixbuf.new(Colorspace.RGB, True, 8, cell_area.width, cell_area.height)
Gdk.cairo_set_source_pixbuf(cr, pixbuf, cell_area.x, cell_area.y)
## Draw a filled square
cr.set_source_rgb(rgb_triplet)
cr.rectangle(cell_area.x+1, cell_area.y+1, cell_area.width-2, cell_area.height-2)
cr.fill()
## Outline it black
cr.set_source_rgb(0, .8, 0)
cr.rectangle(cell_area.x+1, cell_area.y+1, cell_area.width-2, cell_area.height-2)
cr.stroke()
## Draw blue line
#cr.set_source_rgb(0, 0, .8)
#cr.move_to(cell_area.x+5, cell_area.y+5)
#cr.line_to(cell_area.x+15, cell_area.y+15)
#cr.stroke()
## Semitransparent overwrite of whole image
cr.set_source_rgb(0,0,0)
cr.paint_with_alpha(self.alpha)
def drawn(self, widget, cr):
if self._draw_image is None:
return
with self._size_lock:
img = self._draw_image
if self._show_orientation:
img = img.copy()
self._orient_scaled.composite(
img, 0, 0, img.props.width, img.props.height, 0, 0, 1, 1,
GdkPixbuf.InterpType.NEAREST, 215)
img = img.rotate_simple(self._rotation)
rect = self.get_allocation()
Gdk.cairo_set_source_pixbuf(cr, img,
(rect.width - img.props.width) // 2,
(rect.height - img.props.height) // 2)
cr.paint()
self._draw_pending.clear()
def get_surface_from_pixbuf(pixbuf):
surface = cairo.ImageSurface(
cairo.FORMAT_ARGB32, pixbuf.get_width(), pixbuf.get_height())
micairo = cairo.Context(surface)
micairo.save()
Gdk.cairo_set_source_pixbuf(micairo, pixbuf, 0, 0)
micairo.paint()
micairo.restore()
return surface
def get_surface_from_file(filename):
if os.path.exists(filename):
pixbuf = GdkPixbuf.Pixbuf.new_from_file(filename)
if pixbuf:
surface = cairo.ImageSurface(
cairo.FORMAT_ARGB32, pixbuf.get_width(), pixbuf.get_height())
context = cairo.Context(surface)
Gdk.cairo_set_source_pixbuf(context, pixbuf, 0, 0)
context.paint()
return surface
return None
def draw(self, cr, highlight=False):
pixbuf = GdkPixbuf.Pixbuf.new_from_file(self.path)
sx = float(self.w)/float(pixbuf.get_width())
sy = float(self.h)/float(pixbuf.get_height())
cr.save()
cr.translate(self.x0, self.y0 - self.h)
cr.scale(sx, sy)
Gdk.cairo_set_source_pixbuf(cr, pixbuf, 0, 0)
cr.paint()
cr.restore()
def do_draw(self, cr):
a = self.get_allocation()
#state = self.get_state_flags()
context = self.get_style_context()
ds_h = self._dropshadow.get_height()
y = (a.height - ds_h) / 2
Gdk.cairo_set_source_pixbuf(cr, self._dropshadow, 0, y)
cr.paint()
# layout circle
x = self._margin
y = (a.height - ds_h) / 2 + self._margin
w = a.width - 2 * self._margin
h = a.width - 2 * self._margin
cr.new_path()
r = min(w, h) * 0.5
x += int((w - 2 * r) / 2)
y += int((h - 2 * r) / 2)
from math import pi
cr.arc(r + x, r + y, r, 0, 2 * pi)
cr.close_path()
if self.is_active:
color = context.get_background_color(Gtk.StateFlags.SELECTED)
else:
color = context.get_background_color(Gtk.StateFlags.INSENSITIVE)
Gdk.cairo_set_source_rgba(cr, color)
cr.fill()
for child in self:
self.propagate_draw(child, cr)
def do_draw(self, cr):
cr.save()
A = self.get_allocation()
if self._pressed:
cr.translate(1, 1)
if self.has_focus():
Gtk.render_focus(self.get_style_context(),
cr,
3, 3,
A.width - 6, A.height - 6)
for child in self:
self.propagate_draw(child, cr)
if self.is_installed:
# paint installed tick overlay
if self.get_direction() != Gtk.TextDirection.RTL:
x = y = 36
else:
x = A.width - 56
y = 36
Gdk.cairo_set_source_pixbuf(cr, self._overlay, x, y)
cr.paint()
cr.restore()
def _render_icon(self, cr, app, cell_area, xpad, ypad, is_rtl):
# calc offsets so icon is nicely centered
self.icon = self.model.get_icon(app)
self.icon_x_offset = xpad + cell_area.x
self.icon_y_offset = ypad + cell_area.y
xo = (self.pixbuf_width - self.icon.get_width()) / 2
if not is_rtl:
x = cell_area.x + xo + xpad
else:
x = cell_area.x + cell_area.width + xo - self.pixbuf_width - xpad
y = cell_area.y + ypad
# draw appicon pixbuf
Gdk.cairo_set_source_pixbuf(cr, self.icon, x, y)
cr.paint()
# draw overlay if application is installed
if self.model.is_installed(app):
if not is_rtl:
x += (self.pixbuf_width - self.OVERLAY_SIZE + self.OVERLAY_XO)
else:
x -= self.OVERLAY_XO
y += (self.pixbuf_width - self.OVERLAY_SIZE + self.OVERLAY_YO)
Gdk.cairo_set_source_pixbuf(cr, self._installed, x, y)
cr.paint()
def do_draw(self, cr):
a = self.get_allocation()
#state = self.get_state_flags()
context = self.get_style_context()
ds_h = self._dropshadow.get_height()
y = (a.height - ds_h) / 2
Gdk.cairo_set_source_pixbuf(cr, self._dropshadow, 0, y)
cr.paint()
# layout circle
x = self._margin
y = (a.height - ds_h) / 2 + self._margin
w = a.width - 2 * self._margin
h = a.width - 2 * self._margin
cr.new_path()
r = min(w, h) * 0.5
x += int((w - 2 * r) / 2)
y += int((h - 2 * r) / 2)
from math import pi
cr.arc(r + x, r + y, r, 0, 2 * pi)
cr.close_path()
if self.is_active:
color = context.get_background_color(Gtk.StateFlags.SELECTED)
else:
color = context.get_background_color(Gtk.StateFlags.INSENSITIVE)
Gdk.cairo_set_source_rgba(cr, color)
cr.fill()
for child in self:
self.propagate_draw(child, cr)
def do_draw(self, cr):
cr.save()
A = self.get_allocation()
if self._pressed:
cr.translate(1, 1)
if self.has_focus():
Gtk.render_focus(self.get_style_context(),
cr,
3, 3,
A.width - 6, A.height - 6)
for child in self:
self.propagate_draw(child, cr)
if self.is_installed:
# paint installed tick overlay
if self.get_direction() != Gtk.TextDirection.RTL:
x = y = 36
else:
x = A.width - 56
y = 36
Gdk.cairo_set_source_pixbuf(cr, self._overlay, x, y)
cr.paint()
cr.restore()
def draw(self, cr, layout, width, dpi_x, dpi_y):
from gi.repository import Gtk, Gdk
img_width = self._width * dpi_x / 2.54
img_height = self._height * dpi_y / 2.54
if self._style == 'right':
l_margin = width - img_width
elif self._style == 'center':
l_margin = (width - img_width) / 2.0
else:
l_margin = 0
# load the image and get its extents
pixbuf = resize_to_buffer(self._filename, [img_width, img_height],
self._crop)
pixbuf_width = pixbuf.get_width()
pixbuf_height = pixbuf.get_height()
# calculate the scale to fit image into the set extents
scale = min(img_width / pixbuf_width, img_height / pixbuf_height)
# draw the image
cr.save()
cr.translate(l_margin, 0)
cr.scale(scale, scale)
Gdk.cairo_set_source_pixbuf(cr, pixbuf,
(img_width / scale - pixbuf_width) / 2,
(img_height / scale - pixbuf_height) / 2)
cr.rectangle(0 , 0, img_width / scale, img_height / scale)
##gcr.set_source_pixbuf(pixbuf,
##(img_width - pixbuf_width) / 2,
##(img_height - pixbuf_height) / 2)
##cr.rectangle(0 , 0, img_width, img_height)
##cr.scale(scale, scale)
cr.fill()
cr.restore()
if DEBUG:
cr.set_line_width(0.1)
cr.set_source_rgb(1.0, 0, 0)
cr.rectangle(l_margin, 0, img_width, img_height)
cr.stroke()
return (img_height)
def draw_overlay(self, context, width, height):
if self.scroll_enable and self.message:
context = cairo_context_from_gi(context)
#print str(width) + " x " + str(height)
#context.scale(width, height)
#context.scale(width / 100, height / 100)
#context.scale(100, 100)
#context.set_source_rgb(1, 0, 0)
#context.paint_with_alpha(1)
#context.select_font_face("Helvetica")
#context.set_font_face(None)
#context.set_font_size(0.05)
#context.move_to(0.1, 0.1)
#context.show_text("Hello World")
#context.rectangle(0, height * 0.60, width, 30)
#context.rectangle(0, 0.60, 1, 0.1)
context.set_source_rgb(1, 0, 0)
context.rectangle(0, 0.55 * height, width, 0.15 * height)
context.fill()
#context.scale(1.0 / width, 1.0 / height)
#context.translate(0, height * 0.60)
layout = PangoCairo.create_layout(context)
#font = Pango.FontDescription("Arial " + str(0.090 * height))
#font.set_family("Sans")
#font.set_size(0.090 * height)
#font.set_size(25)
#font.set_stretch(Pango.Stretch.ULTRA_CONDENSED)
font = Pango.font_description_from_string("Sans Condensed " + str(0.090 * height))
layout.set_font_description(font)
layout.set_text(self.message, -1)
(layout_width, layout_height) = layout.get_pixel_size()
self.scroll_wrap = 1.0 + (float(layout_width) / float(width))
pos = (self.scroll_pos * width) - layout_width
context.set_source_rgb(1, 1, 1)
context.translate(pos, 0.55 * height)
PangoCairo.update_layout(context, layout)
PangoCairo.show_layout(context, layout)
#context.set_line_width(0.1)
#context.move_to(0, 0)
#context.line_to(1, 0)
#context.stroke()
#pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size("/home/trans/Downloads/kitty.jpg", width, height)
#Gdk.cairo_set_source_pixbuf(context, pixbuf, 0, 0)
#context.stroke()