Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

setStyle method on HighlightAnnotation #10

Open
divico opened this issue Apr 3, 2015 · 6 comments
Open

setStyle method on HighlightAnnotation #10

divico opened this issue Apr 3, 2015 · 6 comments

Comments

@divico
Copy link
Contributor

divico commented Apr 3, 2015

python -V
Python 3.4.3       

popplerqt4.version()
(0, 24, 0)

popplerqt4.poppler_version()
(0, 30, 0)

Hi all

I cannot figure out how to change the color of an existing HighlightAnnotation. I have a PDF document with existing HighlightAnnotations that I can retrieve for a given page with:

 In [1]: doc = popplerqt4.Poppler.Document.load('doc.pdf')
 In [2]: annotations = doc.page(0).annotations()

which gives me two annotations of type Highlight.

 In [3]: annotations
 Out[3]: 
 [<popplerqt4.HighlightAnnotation at 0x7f4cbc170948>,
  <popplerqt4.HighlightAnnotation at 0x7f4cbc170558>]

Now I can change the author or change the quads with respectively the setAuthor() and setHighlightQuads()methods. This works perfectly and it has the expected behavior when the document is saved (with pdfConverter()).

My question is, given annotations, what is the proper way to change the color of the highlight ? I've seen in a couple of c++ examples that they use annot->style().setColor( RED ); for instance. How can I access the style property of my annotations with python ? In my example I have only access to the attributes below. Thank you in advance for any hints !

In [4]: annot = annotations[0]

In [4]: annot.
annot.ACaret               annot.Dashed               annot.NoEffect             annot.creationDate
annot.AFileAttachment      annot.Delete               annot.None                 annot.flags
annot.AGeom                annot.DenyDelete           annot.Quad                 annot.highlightQuads
annot.AHighlight           annot.DenyPrint            annot.Rejected             annot.highlightType
annot.AInk                 annot.DenyWrite            annot.Reply                annot.modificationDate
annot.ALine                annot.External             annot.RevScope             annot.setAuthor
annot.ALink                annot.FixedRotation        annot.RevType              annot.setBoundary
annot.AMovie               annot.FixedSize            annot.Solid                annot.setContents
annot.ASound               annot.Flag                 annot.Squiggly             annot.setCreationDate
annot.AStamp               annot.Group                annot.StrikeOut            annot.setFlags
annot.AText                annot.Hidden               annot.SubType              annot.setHighlightQuads
annot.A_BASE               annot.Highlight            annot.ToggleHidingOnMouse  annot.setHighlightType
annot.Accepted             annot.HighlightType        annot.Underline            annot.setModificationDate
annot.Beveled              annot.Inset                annot.Unmarked             annot.setUniqueName
annot.Cancelled            annot.LineEffect           annot.author               annot.store
annot.Cloudy               annot.LineStyle            annot.boundary             annot.subType
annot.Completed            annot.Marked               annot.contents             annot.uniqueName
@wbsoft
Copy link
Collaborator

wbsoft commented Apr 5, 2015

The Style changed from a struct to a class, I have been trying to write the correct sip specification in poppler-annotation.sip but I didn't succeed yet...

@divico
Copy link
Contributor Author

divico commented Apr 5, 2015

Thank you for the quick reply!

I finally managed to change the color. Unfortunately, I've never used sip so I cannot really help to fix poppler-annotation.sip properly. Here is what I did to finally change the color, maybe it can give you some hints for a proper fix...

I've taken the code of the Style class of python-poppler-qt5 and inserted it in poppler-annotation.sip of python-poppler-qt4.

Given annotations

In [4]: annotations
Out[4]: 
[<popplerqt4.HighlightAnnotation at 0x7f7cd81608b8>,
 <popplerqt4.HighlightAnnotation at 0x7f7cd8160948>,
 <popplerqt4.HighlightAnnotation at 0x7f7cd81609d8>,
 <popplerqt4.HighlightAnnotation at 0x7f7cd8160a68>]

I was then able to read all attributes with style().. For instance the color:

 In [5]: annotations[0].style().color().red()
 Out[5]: 0

However changing the value of the attributes with style(). did not work. For instance the color:

In [6]: import PyQt4

In [7]: annotations[0].style().setColor(PyQt4.QtGui.QColor(255,0,0))

In [8]: annotations[0].style().color().red()
Out[8]: 0

But setting a new style with an other color worked !

In [9]: style = annotations[0].style()

In [10]: style.setColor(PyQt4.QtGui.QColor(255,0,0))

In [11]: annotations[0].setStyle(style)

In [12]: annotations[0].style().color().red()
Out[12]: 255

According to the poppler-qt4 documentation one should be able to change the color with setColor(). I really don't have the skills to find why this is not working, but I would be happy to help you further with tests if this can help-

Note that for other reasons I updated to:

 popplerqt4.poppler_version()
 (0, 31, 0)

@wbsoft
Copy link
Collaborator

wbsoft commented Apr 5, 2015

Is in C++ the equivalent of annotations[0].style().setColor(PyQt4.QtGui.QColor(255,0,0)) working? In Qt it is often so that some objects are created on the fly (such as QPalette, which has the same behaviour) and that one has to get an object (getStyle), modify some attributes, but need to write it back using a method like setStyle to really have it take effect.

@wbsoft
Copy link
Collaborator

wbsoft commented Apr 5, 2015

I seems I forgot that I already took some efforts in poppler-qt5 and to merge those in poppler-qt4 as well. I'm very busy right now, but hopefully in the coming weeks I can release a python-poppler-qt4 which is on par with python-poppler-qt5...

@divico
Copy link
Contributor Author

divico commented Apr 5, 2015

I haven't tested it in c++ but it seems that it is how they do it in Okular

http://code.openhub.net/file?fid=LbKk_wSlBp_sOb7CuEHiDuEx4QU&cid=rh68-VIVNW8&s=setColor&fp=303105&projSelected=true&filterChecked=true&mp=1&ml=1&me=1&md=1#L65
line 65 for instance

@wbsoft
Copy link
Collaborator

wbsoft commented Apr 6, 2015

Thanks for investigating!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants