pdf_preview.py

2021/10/06 categories:Code| tags:Code|python|

記事に戻る

# -*- coding: utf-8 -*-
from PyQt6 import QtCore, QtGui, QtWidgets

class PdfPreview(QtWidgets.QWidget):
    next = QtCore.pyqtSignal()
    prev = QtCore.pyqtSignal()
    def __init__(self) -> None:
        super().__init__()
        self.resize(800, 600)
        self.setWindowTitle('PDFプレビュー')

        self.view = GraphicsView()
        self.prev_page_button = QtWidgets.QPushButton('前のページへ')
        self.next_page_button = QtWidgets.QPushButton('次のページへ')
        self.page = QtWidgets.QLabel('ページ 0/0')
        
        w0 = QtWidgets.QWidget()
        w0.setLayout( QtWidgets.QHBoxLayout() )
        w0.layout().addItem(QtWidgets.QSpacerItem(20, 20, QtWidgets.QSizePolicy.Policy.Expanding, QtWidgets.QSizePolicy.Policy.Minimum))
        w0.layout().addWidget(self.prev_page_button)
        w0.layout().addWidget(self.next_page_button)
        w0.layout().addWidget(self.page)
        w0.layout().addItem(QtWidgets.QSpacerItem(20, 20, QtWidgets.QSizePolicy.Policy.Expanding, QtWidgets.QSizePolicy.Policy.Minimum))
        w0.layout().setContentsMargins(0, 0, 0, 0)

        self.setLayout(QtWidgets.QVBoxLayout())
        self.layout().addWidget(w0)
        self.layout().addWidget(self.view)
        self.layout().setContentsMargins(0, 0, 0, 0)

        self.prev_page_button.clicked.connect(lambda : self.prev.emit())
        self.next_page_button.clicked.connect(lambda : self.next.emit())
        
    def set_pixmap(self, pixmap: QtGui.QPixmap):
        self.view.scene.clear()
        self.view.scene.addPixmap(pixmap)
        self.view.scene.setSceneRect(0.0, 0.0, float(pixmap.width()), float(pixmap.height()))

    def set_page_text(self, page_number, page_count):
        self.page.setText('ページ {}/{}'.format(page_number, page_count))

class GraphicsView(QtWidgets.QGraphicsView):
    def __init__(self, *argv, **keywords):
        super(GraphicsView, self).__init__(*argv, **keywords)
        self._numScheduledScalings = 0
        self.scene = QtWidgets.QGraphicsScene()
        self.setScene(self.scene)
        self.setDragMode(QtWidgets.QGraphicsView.DragMode.ScrollHandDrag)
        self.setBackgroundBrush(QtGui.QBrush(QtGui.QColor(200, 200, 200)))

    def wheelEvent(self, event):
        numDegrees = event.angleDelta().y() / 8
        numSteps = numDegrees / 15
        self._numScheduledScalings += numSteps
        if self._numScheduledScalings * numSteps < 0:
            self._numScheduledScalings = numSteps
        anim = QtCore.QTimeLine(350, self)
        anim.setUpdateInterval(20)
        anim.valueChanged.connect(self.scalingTime)
        anim.finished.connect(self.animFinished)
        anim.start()

    def scalingTime(self, x):
        factor = 1.0 + float(self._numScheduledScalings) / 300.0
        self.scale(factor, factor)

    def animFinished(self):
        if self._numScheduledScalings > 0:
            self._numScheduledScalings -= 1
        else:
            self._numScheduledScalings += 1

if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    window = PdfPreview()
    window.show()
    app.exec()

Share post

Related Posts

Comments

comments powered by Disqus