Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGonzalo Odiard <godiard@gmail.com>2012-07-25 18:23:55 (GMT)
committer Gonzalo Odiard <godiard@gmail.com>2012-07-25 18:23:55 (GMT)
commit0c80f6be4f91fd7edc9ff215fe821485f1cf9e8f (patch)
tree535b249701d763924b12ca606d934924f8c45173
parent0c8e5ae2fd87fb7e37df2eeb02057ddc2716afa2 (diff)
Resize of selections use cairo
Singled-off-by: Gonzalo Odiard <gonzalo@laptop.org>
-rw-r--r--Area.py50
-rw-r--r--Desenho.py68
2 files changed, 50 insertions, 68 deletions
diff --git a/Area.py b/Area.py
index 5534c43..c9503f0 100644
--- a/Area.py
+++ b/Area.py
@@ -177,7 +177,8 @@ class Area(gtk.DrawingArea):
self.font_description.set_family('Sans')
self.font_description.set_size(12)
- self.set_selection_bounds(0, 0, 0, 0)
+ # selection properties
+ self.clear_selection()
self.pending_clean_selection_background = False
# List of pixbuf for the Undo function:
@@ -242,6 +243,7 @@ class Area(gtk.DrawingArea):
if not self.is_selected():
return
x, y, width, height = self.get_selection_bounds()
+
ctx.save()
ctx.set_line_width(1)
ctx.set_source_rgba(1., 1., 1., 1.)
@@ -1166,20 +1168,29 @@ class Area(gtk.DrawingArea):
def clear_selection(self):
self.set_selection_bounds(0, 0, 0, 0)
+ self._selection_horizontal_scale = 1.0
+ self._selection_vertical_scale = 1.0
- def set_selection_bounds(self, x1, y1, width, height):
+ def set_selection_bounds(self, x, y, width, height):
"""
Set selection bounds
- @param x1, y1, width, height - the rectangle to define the area
+ @param x, y, width, height - the rectangle to define the area
"""
- self._selection_bounds = (x1, y1, width, height)
+ self._selection_bounds = (x, y, width, height)
+
+ def set_selection_start(self, x, y):
+ self._selection_bounds = (x, y, self._selection_bounds[2],
+ self._selection_bounds[3])
def get_selection_bounds(self):
"""
@return x1, y1, width, height - the rectangle to define the area
"""
- return self._selection_bounds[0], self._selection_bounds[1], \
- self._selection_bounds[2], self._selection_bounds[3]
+ x, y = self._selection_bounds[0], self._selection_bounds[1]
+ width, height = self._selection_bounds[2], self._selection_bounds[3]
+ width = width * self._selection_horizontal_scale
+ height = height * self._selection_vertical_scale
+ return (x, y, int(width), int(height))
def create_selection_surface(self, clear_background=True):
x, y, width, height = self.get_selection_bounds()
@@ -1190,10 +1201,37 @@ class Area(gtk.DrawingArea):
selection_ctx.translate(-x, -y)
selection_ctx.set_source_surface(self.drawing_canvas)
selection_ctx.paint()
+ self.selection_resized_surface = None
if clear_background:
self.pending_clean_selection_background = True
+ def resize_selection_surface(self, horizontal_scale, vertical_scale):
+ x, y = self._selection_bounds[0], self._selection_bounds[1]
+ new_width = int(self .selection_surface.get_width() * horizontal_scale)
+ new_height = int(self.selection_surface.get_height() * vertical_scale)
+
+ # create a surface with the selection scaled to the new size
+ self.selection_resized_surface = cairo.ImageSurface(
+ cairo.FORMAT_ARGB32, new_width, new_height)
+ temp_ctx = cairo.Context(self.selection_resized_surface)
+ temp_ctx.scale(horizontal_scale, vertical_scale)
+ temp_ctx.set_source_surface(self.selection_surface)
+ temp_ctx.paint()
+
+ # draw over temp canvas
+ self.temp_ctx.save()
+ self.temp_ctx.translate(x, y)
+ self.temp_ctx.set_source_surface(self.selection_resized_surface)
+ self.temp_ctx.rectangle(0, 0, new_width, new_height)
+ self.temp_ctx.paint()
+ self.temp_ctx.restore()
+
+ self._selection_horizontal_scale = horizontal_scale
+ self._selection_vertical_scale = vertical_scale
+
def get_selection(self):
+ if self.selection_resized_surface is not None:
+ return self.selection_resized_surface
if self.selection_surface is not None:
return self.selection_surface
else:
diff --git a/Desenho.py b/Desenho.py
index c67c532..cdcc674 100644
--- a/Desenho.py
+++ b/Desenho.py
@@ -635,7 +635,7 @@ class Desenho:
widget.temp_ctx.set_source_surface(selection_surface)
widget.temp_ctx.paint()
widget.temp_ctx.restore()
- widget.set_selection_bounds(new_x, new_y, width, height)
+ widget.set_selection_start(new_x, new_y)
widget.queue_draw()
@@ -647,75 +647,19 @@ class Desenho:
@param height_percent -- Percent of y scale
"""
- width, height = widget.window.get_size()
- widget.desenha = True
-
- #Create the pixbuf for future resizes
- try:
- self.pixbuf_resize
- except:
- size = widget.pixmap_sel.get_size()
- self.pixbuf_resize = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False,
- 8, size[0], size[1])
- self.pixbuf_resize.get_from_drawable(widget.pixmap_sel,
- gtk.gdk.colormap_get_system(), 0, 0, 0, 0, size[0], size[1])
-
- w = self.pixbuf_resize.get_width()
- h = self.pixbuf_resize.get_height()
- wr = int(w * width_percent)
- hr = int(h * height_percent)
-
- self._draw_selection(widget, wr, hr, is_preview=True)
-
# Add a timer for resize or update it if there is one already:
if self._resize_timer is not None:
gobject.source_remove(self._resize_timer)
self._resize_timer = gobject.timeout_add(RESIZE_DELAY,
- self._do_resize, widget, wr, hr)
+ self._do_resize, widget, width_percent, height_percent)
- def _do_resize(self, widget, wr, hr):
+ def _do_resize(self, widget, width_percent, height_percent):
"""Do the resize calculation.
-
"""
- resized = self.pixbuf_resize.scale_simple(wr, hr, gtk.gdk.INTERP_HYPER)
-
- #Copy the resized picture to pixmap_sel
- try:
- del (widget.pixmap_sel)
- except:
- pass
- widget.pixmap_sel = gtk.gdk.Pixmap(widget.window, wr, hr, -1)
- widget.pixmap_sel.draw_pixbuf(widget.get_style().white_gc, resized,
- 0, 0, 0, 0, wr, hr)
-
- #Draw the new pixmap_sel
- self._draw_selection(widget, wr, hr)
-
- # Clean the timer:
- self.resize_timer = None
- return False
-
- def _draw_selection(self, widget, wr, hr, is_preview=False):
- gc.collect()
- width, height = widget.window.get_size()
-
- widget.pixmap_temp.draw_drawable(widget.gc, widget.pixmap,
- 0, 0, 0, 0, width, height)
- if is_preview:
- widget.pixmap_temp.draw_drawable(widget.gc, widget.pixmap_sel,
- 0, 0, widget.orig_x, widget.orig_y, -1, -1)
- else:
- widget.pixmap_temp.draw_drawable(widget.gc, widget.pixmap_sel,
- 0, 0, widget.orig_x, widget.orig_y, wr, hr)
-
- widget.set_selection_bounds(widget.orig_x, widget.orig_y, wr, hr)
-# widget.pixmap_temp.draw_rectangle(widget.gc_selection, False,
-# widget.orig_x, widget.orig_y, wr, hr)
-# widget.pixmap_temp.draw_rectangle(widget.gc_selection1, False,
-# widget.orig_x - 1, widget.orig_y - 1, wr + 2, hr + 2)
-
+ widget.desenha = True
+ widget.resize_selection_surface(float(width_percent),
+ float(height_percent))
widget.queue_draw()
- gc.collect()
def freeform(self, widget, coords, temp, fill, param=None):
"""Draw polygon.