mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-31 14:33:54 -04:00
Add copy and paste actions
This commit is contained in:
parent
c393278018
commit
5563970967
@ -58,7 +58,7 @@ class Command(QUndoCommand):
|
||||
raise ValueError('No image loaded')
|
||||
if i.isNull():
|
||||
raise ValueError('Cannot perform operations on invalid images')
|
||||
self.after_image = canvas.current_image = self(canvas)
|
||||
self.after_image = self(canvas)
|
||||
|
||||
def undo(self):
|
||||
canvas = self.canvas_ref()
|
||||
@ -83,6 +83,15 @@ class Trim(Command):
|
||||
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):
|
||||
|
||||
def __init__(self, img, text, canvas):
|
||||
self.after_image = img
|
||||
Command.__init__(self, text, canvas)
|
||||
|
||||
def __call__(self, canvas):
|
||||
return self.after_image
|
||||
|
||||
def imageop(func):
|
||||
@wraps(func)
|
||||
def ans(self, *args, **kwargs):
|
||||
@ -119,6 +128,8 @@ class Canvas(QWidget):
|
||||
self.undo_stack.setUndoLimit(10)
|
||||
|
||||
self.original_image_data = None
|
||||
self.is_valid = False
|
||||
self.original_image_format = None
|
||||
self.current_image = None
|
||||
self.current_scaled_pixmap = None
|
||||
self.last_canvas_size = None
|
||||
@ -130,6 +141,7 @@ class Canvas(QWidget):
|
||||
a.setIcon(QIcon(I('edit-redo.png')))
|
||||
|
||||
def load_image(self, data):
|
||||
self.is_valid = False
|
||||
try:
|
||||
fmt = identify_data(data)[-1].encode('ascii')
|
||||
except Exception:
|
||||
@ -156,6 +168,25 @@ class Canvas(QWidget):
|
||||
return self.original_image_data
|
||||
return pixmap_to_data(self.current_image, format=self.original_image_format or 'JPEG', quality=90)
|
||||
|
||||
def copy(self):
|
||||
if not self.is_valid:
|
||||
return
|
||||
clipboard = QApplication.clipboard()
|
||||
if not self.has_selection or self.selection_state.rect is None:
|
||||
clipboard.setImage(self.current_image)
|
||||
else:
|
||||
trim = Trim(self)
|
||||
clipboard.setImage(trim.after_image)
|
||||
trim.before_image = trim.after_image = None
|
||||
|
||||
def paste(self):
|
||||
clipboard = QApplication.clipboard()
|
||||
md = clipboard.mimeData()
|
||||
if md.hasImage():
|
||||
img = QImage(md.imageData())
|
||||
if not img.isNull():
|
||||
self.undo_stack.push(Replace(img, _('Paste image'), self))
|
||||
|
||||
def break_cycles(self):
|
||||
self.undo_stack.clear()
|
||||
self.original_image_data = self.current_image = self.current_scaled_pixmap = None
|
||||
|
Loading…
x
Reference in New Issue
Block a user