mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Allow pasting into the selection only
This commit is contained in:
parent
50b621397e
commit
830198d7b0
@ -68,8 +68,18 @@ class Command(QUndoCommand):
|
|||||||
canvas = self.canvas_ref()
|
canvas = self.canvas_ref()
|
||||||
canvas.set_image(self.after_image)
|
canvas.set_image(self.after_image)
|
||||||
|
|
||||||
|
def get_selection_rect(img, sr, target):
|
||||||
|
' Given selection rect return the corresponding rectangle in the underlying image as left, top, width, height '
|
||||||
|
left_border = (abs(sr.left() - target.left())/target.width()) * img.width()
|
||||||
|
top_border = (abs(sr.top() - target.top())/target.height()) * img.height()
|
||||||
|
right_border = (abs(target.right() - sr.right())/target.width()) * img.width()
|
||||||
|
bottom_border = (abs(target.bottom() - sr.bottom())/target.height()) * img.height()
|
||||||
|
return left_border, top_border, img.width() - left_border - right_border, img.height() - top_border - bottom_border
|
||||||
|
|
||||||
class Trim(Command):
|
class Trim(Command):
|
||||||
|
|
||||||
|
''' Remove the areas of the image outside the current selection. '''
|
||||||
|
|
||||||
def __init__(self, canvas):
|
def __init__(self, canvas):
|
||||||
Command.__init__(self, _('Trim image'), canvas)
|
Command.__init__(self, _('Trim image'), canvas)
|
||||||
|
|
||||||
@ -77,19 +87,26 @@ class Trim(Command):
|
|||||||
img = canvas.current_image
|
img = canvas.current_image
|
||||||
target = canvas.target
|
target = canvas.target
|
||||||
sr = canvas.selection_state.rect
|
sr = canvas.selection_state.rect
|
||||||
left_border = (abs(sr.left() - target.left())/target.width()) * img.width()
|
return img.copy(*get_selection_rect(img, sr, target))
|
||||||
top_border = (abs(sr.top() - target.top())/target.height()) * img.height()
|
|
||||||
right_border = (abs(target.right() - sr.right())/target.width()) * img.width()
|
|
||||||
bottom_border = (abs(target.bottom() - sr.bottom())/target.height()) * img.height()
|
|
||||||
return img.copy(left_border, top_border, img.width() - left_border - right_border, img.height() - top_border - bottom_border)
|
|
||||||
|
|
||||||
class Replace(Command):
|
class Replace(Command):
|
||||||
|
|
||||||
|
''' Replace the current image with another image. If there is a selection,
|
||||||
|
only the region of the selection is replaced. '''
|
||||||
|
|
||||||
def __init__(self, img, text, canvas):
|
def __init__(self, img, text, canvas):
|
||||||
self.after_image = img
|
self.after_image = img
|
||||||
Command.__init__(self, text, canvas)
|
Command.__init__(self, text, canvas)
|
||||||
|
|
||||||
def __call__(self, canvas):
|
def __call__(self, canvas):
|
||||||
|
if canvas.has_selection and canvas.selection_state.rect is not None:
|
||||||
|
pimg = self.after_image
|
||||||
|
img = self.after_image = QImage(canvas.current_image)
|
||||||
|
rect = QRectF(*get_selection_rect(img, canvas.selection_state.rect, canvas.target))
|
||||||
|
p = QPainter(img)
|
||||||
|
p.setRenderHint(p.SmoothPixmapTransform, True)
|
||||||
|
p.drawImage(rect, pimg, QRectF(pimg.rect()))
|
||||||
|
p.end()
|
||||||
return self.after_image
|
return self.after_image
|
||||||
|
|
||||||
def imageop(func):
|
def imageop(func):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user