Dup Ver Goto 📝

DrawPixmap2_ScrollingPages

PT2/lang/python/qt/pyside6/examples does not exist
To
92 lines, 270 words, 2575 chars Page 'DrawPixmap2_ScrollingPages' does not exist.

This is a simple example I wrote. Input images are produced from PDF's by ImageMagick.

from PySide6.QtCore import *
from PySide6.QtGui import *
from PySide6.QtWidgets import *
from glob import glob
import os
import sys
app = QApplication([])

i = 0
pngs = []
while os.path.exists(fn:=f"{i}.png"):
  pngs.append(fn)
  i += 1

pixmaps = [ QPixmap(png) for png in pngs ]
idx = 0

class Canvas(QWidget):
  def __init__(self,*xs,pixmaps=pixmaps,**kw):
    super().__init__(*xs,**kw)
    self.resize(800,600)
    self.offset = 0
    self.skip_size = 100
    self.update_pixmaps(pixmaps)
  def update_pixmaps(self,pixmaps):
    self.pixmaps = pixmaps
    if len(self.pixmaps) == 0:
      print(f"No pixmaps")
      return
    self.pwidth = pixmaps[0].width()
    self.pheight = pixmaps[0].height()
    self.total_height = self.pheight * len(pixmaps)
  def keyPressEvent(self,e):
    print(e.key())
    if e.key() == Qt.Key_C:
      return self.setOffset(self.offset + self.skip_size)
    elif e.key() == Qt.Key_Q:
      app.quit()

  def setOffset(self,offset):
    self.offset = offset % self.total_height
    self.update()
  def paintEvent(self,e):
    # offset is based on screen pixels
    # assume pages have the same height
    rect = self.rect()
    width = rect.width()
    height = rect.height()
    square = min(width,height)
    x0 = (width-square)//2
    offset = self.offset
    swidth = self.pwidth
    sheight = self.pheight
    sdratio = swidth/square
    soffset = int(offset * swidth / square)
    soffset_in_page = soffset % self.pheight
    page = (soffset // self.pheight) % len(self.pixmaps)
    page2 = (page + 1) % len(self.pixmaps)
    pixmap1 = self.pixmaps[page]
    pixmap2 = self.pixmaps[page2]

    if soffset_in_page + swidth > sheight:
      sheight2 = (soffset_in_page + swidth) - sheight
      sheight1 = swidth - sheight2
      srect1 = QRect(0,soffset_in_page,swidth,sheight1)
      srect2 = QRect(0,0,swidth,sheight2)
      trect1 = QRect(x0,0,square,sheight1/sdratio)
      trect2 = QRect(x0,sheight1/sdratio,square,square-trect1.height())
    else:
      srem = 0
      srect1 = QRect(0,soffset_in_page,swidth,swidth)
      srect2 = None
      trect1 = QRect(x0,0,square,square)
      trect2 = None

    with QPainter(self) as qp:
      qp.fillRect(rect,Qt.red)
      if swidth == 0:
        print(f"Empty image 1235")
        return
      qp.drawPixmap(trect1,pixmap1,srect1)
      if srect2 is not None:
        qp.drawPixmap(trect2,pixmap2,srect2)

canvas = Canvas()
canvas.show()

exit(app.exec())