You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
3.1 KiB

  1. from PyQt5.QtWidgets import QScrollArea, QLabel, QVBoxLayout, QWidget, QSizePolicy
  2. from PyQt5.QtCore import Qt, QPoint, QSize
  3. from PyQt5.QtGui import QPixmap, QImage, QResizeEvent, QPainter
  4. import fitz
  5. class PDFLabel(QLabel):
  6. def __init__(self, parent):
  7. super().__init__(parent)
  8. self.parent = parent
  9. self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
  10. def paintEvent(self, event):
  11. self.adjustSize()
  12. if self.pixmap() is not None:
  13. painter = QPainter(self)
  14. # painter.setRenderHint(QPainter.Antialiasing)
  15. parentLayoutMargins = self.parent.scrollAreaLayout.getContentsMargins()
  16. parentMargins = self.parent.getContentsMargins()
  17. if self.parent.verticalScrollBar().isVisible():
  18. scrollBarWidth = self.parent.verticalScrollBar().sizeHint().width()
  19. else:
  20. scrollBarWidth = 0
  21. totalMargin = parentLayoutMargins[0] + parentLayoutMargins[2] + \
  22. parentMargins[0]*2 + scrollBarWidth
  23. idealWidth = self.parent.width() - totalMargin
  24. print(idealWidth)
  25. pixSize = self.pixmap().size()
  26. print(pixSize)
  27. print((self.parent.scrollAreaContents.size()))
  28. pixSize.scale(idealWidth, 1000000, Qt.KeepAspectRatio)
  29. scaledPix = self.pixmap().scaled(pixSize, Qt.KeepAspectRatio, Qt.SmoothTransformation)
  30. painter.drawPixmap(QPoint(), scaledPix)
  31. # self.setMaximumSize(pixSize)
  32. class PDFViewer(QScrollArea):
  33. def __init__(self, parent):
  34. super().__init__(parent)
  35. self.scrollAreaContents = QWidget()
  36. self.scrollAreaLayout = QVBoxLayout()
  37. self.setWidget(self.scrollAreaContents)
  38. self.setWidgetResizable(True)
  39. self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
  40. self.scrollAreaContents.setLayout(self.scrollAreaLayout)
  41. self.pixmapList = []
  42. def update_pdf(self, pdf):
  43. self.render(pdf)
  44. self.clear()
  45. self.show()
  46. def render(self, pdf):
  47. """
  48. Update the preview shown by rendering a new PDF and drawing it to the scroll area.
  49. """
  50. self.pixmapList = []
  51. pdfView = fitz.Document(stream=pdf, filetype='pdf')
  52. for page in pdfView:
  53. # needs to be high enough for big monitors. 300 should do it...
  54. self.pixmapList.append(page.get_pixmap(dpi=300, alpha=False))
  55. def clear(self):
  56. while self.scrollAreaLayout.count():
  57. item = self.scrollAreaLayout.takeAt(0)
  58. w = item.widget()
  59. if w:
  60. w.deleteLater()
  61. def show(self):
  62. for p in self.pixmapList:
  63. label = PDFLabel(parent=self)
  64. label.setAlignment(Qt.AlignHCenter)
  65. qtimg = QImage(p.samples, p.width, p.height, p.stride, QImage.Format_RGB888)
  66. label.setPixmap(QPixmap.fromImage(qtimg))
  67. self.scrollAreaLayout.addWidget(label)
  68. # self.scrollAreaLayout.addStretch(1)