Skip to content

Commit

Permalink
Update annotation edit flow to fail gracefully on empty docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Cimbali committed Dec 12, 2023
1 parent aaabbb6 commit ad106c3
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions pympress/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -591,16 +591,17 @@ def get_annotations(self):
return self.annotations


def new_annotation(self, pos, rect=None):
def new_annotation(self, pos, rect=None, value=''):
""" Add an annotation to this page
Args:
pos (`int`): The position in the list of annotations in which to insert this annotation
rect (:class:`~Poppler.Rectangle`): A rectangle for the position of this annotation
Returns:
:class:`~Poppler.Annot`: A new annotation on this page
value (`str`): The contents of the annotation
"""
if self.parent.doc is None:
return

if pos < 0:
pos = 0
if pos > len(self.annotations):
Expand All @@ -615,25 +616,30 @@ def new_annotation(self, pos, rect=None):

new_annot = Poppler.AnnotText.new(self.parent.doc, rect)
new_annot.set_icon(Poppler.ANNOT_TEXT_ICON_NOTE)
new_annot.set_contents(value)
self.annotations.insert(pos, new_annot)
self.parent.made_changes()
return new_annot


def set_annotation(self, pos, value):
""" Add an annotation to this page
""" Update an annotation on this page
Args:
pos (`int`): The number of the annotation
value (`str`): The new contents of the annotation
"""
rect = self.annotations[pos].get_rectangle()
self.remove_annotation(pos)
self.new_annotation(pos, rect).set_contents(value)
try:
rect = self.annotations[pos].get_rectangle()
except IndexError:
# Often because no document is loaded
logger.error(_("Pympress can not edit PDF annotation {}").format(pos))
else:
self.remove_annotation(pos)
self.new_annotation(pos, rect, value)


def remove_annotation(self, pos):
""" Add an annotation to this page
""" Remove an annotation from this page
Args:
pos (`int`): The number of the annotation
Expand Down Expand Up @@ -1237,8 +1243,8 @@ class EmptyPage(Page):
Also, it has no "rendering" capability, and is made harmless by overriding its render function.
"""

def __init__(self):
super(EmptyPage, self).__init__(None, -1, None)
def __init__(self, parent):
super(EmptyPage, self).__init__(None, -1, parent)
self.page_label = None
# by default, anything that will have a 1.3 asapect ratio
self.pw, self.ph = 1.3, 1.0
Expand Down Expand Up @@ -1270,7 +1276,7 @@ class EmptyDocument(Document):
"""
def __init__(self):
super(EmptyDocument, self).__init__(None, None, None)
self.pages_cache[-1] = EmptyPage()
self.pages_cache[-1] = EmptyPage(self)


def page(self, number):
Expand Down

0 comments on commit ad106c3

Please sign in to comment.