Пример #1
0
 def __init__(self,parent=None):
     QtGui.QWidget.__init__(self,parent)
     self.ui=Ui_Form()
     self.ui.setupUi(self)
     self.pdfd = None
Пример #2
0
class PDFWidget(QtGui.QWidget):
    def __init__(self,parent=None):
        QtGui.QWidget.__init__(self,parent)
        self.ui=Ui_Form()
        self.ui.setupUi(self)
        self.pdfd = None

    def loadDocument(self,data):
        self.document = QtPoppler.Poppler.Document.loadFromData(data)
        self.emit(QtCore.SIGNAL('pageCount'),self.document.numPages())
        self.document.setRenderHint(QtPoppler.Poppler.Document.Antialiasing and QtPoppler.Poppler.Document.TextAntialiasing)

        # When rerendering, keep state as much as possible in
        # the viewer

        if self.pdfd:
            res = self.pdfd.res
            xpos = self.ui.scroll.horizontalScrollBar().value()
            ypos = self.ui.scroll.verticalScrollBar().value()
            currentPage = self.pdfd.currentPage
        else:
            res=72.
            xpos=0
            ypos=0
            currentPage = 0

        self.pdfd=PDFDisplay(self.document)
        self.connect(self.pdfd,QtCore.SIGNAL('pageChanged'),
                     self,QtCore.SIGNAL('pageChanged'))
        self.pdfd.currentPage = currentPage
        self.checkActions()
        self.pdfd.res = res
        self.ui.scroll.setWidget(self.pdfd)
        self.ui.scroll.horizontalScrollBar().setValue(xpos)
        self.ui.scroll.verticalScrollBar().setValue(ypos)

    def checkActions(self):
        if not self.pdfd or \
               self.pdfd.currentPage == self.document.numPages():
            self.ui.next.setEnabled(False)
        else:
            self.ui.next.setEnabled(True)
        if not self.pdfd or \
                self.pdfd.currentPage == 1:
            self.ui.previous.setEnabled(False)
        else:
            self.ui.previous.setEnabled(True)

    def gotoPosition(self, page, top, left, bottom):
        """The position is defined in terms of poppler's linkdestinations,
        top is in the range 0-1, page is one-based."""

        if not self.pdfd:
            return

        self.gotoPage(page)

        # Draw a mark to see if we are calculating correctly
        pixmap=QtGui.QPixmap(self.pdfd.pdfImage)
        p=QtGui.QPainter(pixmap)
        c=QtGui.QColor(QtCore.Qt.yellow).lighter(160)
        c.setAlpha(150)
        p.setBrush(c)
        p.setPen(c)
        # FIXME, move the highlighting outside
        y1=self.pdfd.pdfImage.height()*top
        y2=self.pdfd.pdfImage.height()*(bottom-top)
        w=self.pdfd.pdfImage.width()
        p.drawRect(0,y1,w,y2)
        self.pdfd.setPixmap(pixmap)
        p.end()



    def gotoPage(self,n):
        if self.pdfd:
            self.pdfd.currentPage = n
        self.checkActions()

    def on_next_triggered(self, b=None):
        if b is None: return
        self.pdfd.nextPage()
        self.checkActions()

    def on_previous_triggered(self, b=None):
        if b is None: return
        self.pdfd.prevPage()
        self.checkActions()

    def on_zoomin_triggered(self, b=None):
        if b is None: return
        self.pdfd.zoomin()

    def on_zoomout_triggered(self, b=None):
        if b is None: return
        self.pdfd.zoomout()