6501: Cannot crop via mouse cursor into Media reference editor

svn: r21743
This commit is contained in:
Benny Malengier 2013-03-25 20:04:33 +00:00
parent 47be9a4e02
commit cbb8d063df

View File

@ -135,7 +135,7 @@ class EditMediaRef(EditReference):
if not self.source.get_mime_type(): if not self.source.get_mime_type():
self.mimetext.set_text(_('Note')) self.mimetext.set_text(_('Note'))
def draw_preview(self): def draw_preview(self, onlysub=False):
""" """
Draw the two preview images. This method can be called on eg change of Draw the two preview images. This method can be called on eg change of
the path. the path.
@ -143,14 +143,16 @@ class EditMediaRef(EditReference):
mtype = self.source.get_mime_type() mtype = self.source.get_mime_type()
if mtype: if mtype:
fullpath = media_path_full(self.db, self.source.get_path()) fullpath = media_path_full(self.db, self.source.get_path())
pb = get_thumbnail_image(fullpath, mtype) if not onlysub:
self.pixmap.set_from_pixbuf(pb) pb = get_thumbnail_image(fullpath, mtype)
self.pixmap.set_from_pixbuf(pb)
subpix = get_thumbnail_image(fullpath, mtype, subpix = get_thumbnail_image(fullpath, mtype,
self.rectangle) self.rectangle)
self.subpixmap.set_from_pixbuf(subpix) self.subpixmap.set_from_pixbuf(subpix)
else: else:
pb = find_mime_type_pixbuf('text/plain') pb = find_mime_type_pixbuf('text/plain')
self.pixmap.set_from_pixbuf(pb) if not onlysub:
self.pixmap.set_from_pixbuf(pb)
self.subpixmap.set_from_pixbuf(pb) self.subpixmap.set_from_pixbuf(pb)
def _setup_fields(self): def _setup_fields(self):
@ -391,37 +393,7 @@ class EditMediaRef(EditReference):
""" """
Updates the thumbnail of the specified subsection Updates the thumbnail of the specified subsection
""" """
self.draw_preview(onlysub=True)
path = self.source.get_path()
if path is None:
self.subpixmap.hide()
else:
try:
fullpath = media_path_full(self.db, path)
pixbuf = GdkPixbuf.Pixbuf.new_from_file(fullpath)
width = pixbuf.get_width()
height = pixbuf.get_height()
upper_x = min(self.rectangle[0], self.rectangle[2])/100.
lower_x = max(self.rectangle[0], self.rectangle[2])/100.
upper_y = min(self.rectangle[1], self.rectangle[3])/100.
lower_y = max(self.rectangle[1], self.rectangle[3])/100.
sub_x = int(upper_x * width)
sub_y = int(upper_y * height)
sub_width = int((lower_x - upper_x) * width)
sub_height = int((lower_y - upper_y) * height)
if sub_width > 0 and sub_height > 0:
pixbuf = pixbuf.subpixbuf(sub_x, sub_y, sub_width, sub_height)
width = sub_width
height = sub_height
ratio = float(max(height, width))
scale = THUMBSCALE / ratio
x = int(scale * width)
y = int(scale * height)
pixbuf = pixbuf.scale_simple(x, y, GdkPixbuf.InterpType.BILINEAR)
self.subpixmap.set_from_pixbuf(pixbuf)
self.subpixmap.show()
except:
self.subpixmap.hide()
def build_menu_names(self, person): def build_menu_names(self, person):
""" """
@ -448,25 +420,20 @@ class EditMediaRef(EditReference):
""" """
self.button_press_coords = (event.x, event.y) self.button_press_coords = (event.x, event.y)
# prepare drawing of a feedback rectangle # prepare drawing of a feedback rectangle
self.rect_pixbuf = self.subpixmap.get_pixbuf() self.orig_subpixbuf = self.subpixmap.get_pixbuf()
w,h = self.rect_pixbuf.get_width(), self.rect_pixbuf.get_height() gtkimg_win = self.subpixmap.get_window()
self.rect_pixbuf_render = GdkPixbuf.Pixbuf(GdkPixbuf.Colorspace.RGB, False, 8, w, h) self.rect_pixbuf = Gdk.pixbuf_get_from_window(
cm = Gdk.colormap_get_system() gtkimg_win, 0, 0,
color = cm.alloc_color(Gdk.Color("blue")) gtkimg_win.get_width(),
self.rect_pixmap = Gdk.Pixmap(None, w, h, cm.get_visual().depth) gtkimg_win.get_height())
self.rect_pixmap.set_colormap(cm)
self.rect_gc = self.rect_pixmap.new_gc()
self.rect_gc.set_foreground(color)
def motion_notify_event_ref(self, widget, event): def motion_notify_event_ref(self, widget, event):
# get the image size and calculate the X and Y offsets # get the image size and calculate the X and Y offsets
# (image is centered *horizontally* when smaller than THUMBSCALE) # (image is centered *horizontally* when smaller than THUMBSCALE)
w, h = self.rect_pixbuf.get_width(), self.rect_pixbuf.get_height() w, h = self.orig_subpixbuf.get_width(), self.orig_subpixbuf.get_height()
offset_x = (THUMBSCALE - w) / 2 offset_x = (THUMBSCALE - w) / 2
offset_y = 0 offset_y = 0
self.rect_pixmap.draw_pixbuf(self.rect_gc, self.rect_pixbuf, 0, 0, 0, 0)
# get coordinates of the rectangle, so that x1 < x2 and y1 < y2 # get coordinates of the rectangle, so that x1 < x2 and y1 < y2
x1 = min(self.button_press_coords[0], event.x) x1 = min(self.button_press_coords[0], event.x)
x2 = max(self.button_press_coords[0], event.x) x2 = max(self.button_press_coords[0], event.x)
@ -478,13 +445,18 @@ class EditMediaRef(EditReference):
x1 = int(x1 - offset_x) x1 = int(x1 - offset_x)
y1 = int(y1 - offset_y) y1 = int(y1 - offset_y)
self.rect_pixmap.draw_rectangle(self.rect_gc, False, cr_pixbuf = self.subpixmap.get_window().cairo_create()
x1, y1, width, height) cr_pixbuf.reset_clip()
#reset the pixbuf
self.rect_pixbuf_render.get_from_drawable(self.rect_pixmap, Gdk.cairo_set_source_pixbuf(cr_pixbuf, self.rect_pixbuf, 0, 0)
Gdk.colormap_get_system(), cr_pixbuf.paint()
0,0,0,0, w, h) cr_pixbuf.set_source_rgba(0, 0, 1, 0.5) #blue transparant
self.subpixmap.set_from_pixbuf(self.rect_pixbuf_render) cr_pixbuf.move_to(x1,y1)
cr_pixbuf.line_to(x1+width, y1)
cr_pixbuf.line_to(x1+width, y1+height)
cr_pixbuf.line_to(x1, y1+height)
cr_pixbuf.close_path()
cr_pixbuf.fill()
def button_release_event_ref(self, widget, event): def button_release_event_ref(self, widget, event):
""" """
@ -501,9 +473,9 @@ class EditMediaRef(EditReference):
self.corner2_y_spinbutton.set_value(100) self.corner2_y_spinbutton.set_value(100)
else: else:
if (self.rect_pixbuf == None): if (self.orig_subpixbuf == None):
return return
self.subpixmap.set_from_pixbuf(self.rect_pixbuf) self.subpixmap.set_from_pixbuf(self.orig_subpixbuf)
# ensure the clicks happened at least 5 pixels away from each other # ensure the clicks happened at least 5 pixels away from each other
new_x1 = min(self.button_press_coords[0], event.x) new_x1 = min(self.button_press_coords[0], event.x)
@ -515,8 +487,8 @@ class EditMediaRef(EditReference):
# get the image size and calculate the X and Y offsets # get the image size and calculate the X and Y offsets
# (image is centered *horizontally* when smaller than THUMBSCALE) # (image is centered *horizontally* when smaller than THUMBSCALE)
w = self.rect_pixbuf.get_width() w = self.orig_subpixbuf.get_width()
h = self.rect_pixbuf.get_height() h = self.orig_subpixbuf.get_height()
x = (THUMBSCALE - w) / 2 x = (THUMBSCALE - w) / 2
y = 0 y = 0
@ -568,7 +540,9 @@ class EditMediaRef(EditReference):
self.corner2_y_spinbutton.set_value(new_y2) self.corner2_y_spinbutton.set_value(new_y2)
# Free the pixbuf as it is not needed anymore # Free the pixbuf as it is not needed anymore
self.orig_subpixbuf = None
self.rect_pixbuf = None self.rect_pixbuf = None
self.draw_preview()
def _update_addmedia(self, obj): def _update_addmedia(self, obj):
""" """