Linux PyQt tray application to control OctoPrint instances
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.

AspectRatioPixmapLabel.py 1.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #!/usr/bin/env python3
  2. # OctoTray Linux Qt System Tray OctoPrint client
  3. #
  4. # AspectRatioPixmapLabel.py
  5. #
  6. # see also:
  7. # https://doc.qt.io/qt-5/qtwidgets-widgets-imageviewer-example.html
  8. # https://stackoverflow.com/a/22618496
  9. from PyQt5.QtWidgets import QLabel
  10. from PyQt5.QtGui import QPixmap
  11. from PyQt5.QtCore import QSize, Qt
  12. class AspectRatioPixmapLabel(QLabel):
  13. def __init__(self, *args, **kwargs):
  14. super(AspectRatioPixmapLabel, self).__init__(*args, **kwargs)
  15. self.setMinimumSize(1, 1)
  16. self.setScaledContents(False)
  17. self.pix = QPixmap(0, 0)
  18. def setPixmap(self, p):
  19. self.pix = p
  20. super(AspectRatioPixmapLabel, self).setPixmap(self.scaledPixmap())
  21. def heightForWidth(self, width):
  22. if self.pix.isNull():
  23. return self.height()
  24. else:
  25. return (self.pix.height() * width) / self.pix.width()
  26. def sizeHint(self):
  27. w = self.width()
  28. return QSize(int(w), int(self.heightForWidth(w)))
  29. def scaledPixmap(self):
  30. return self.pix.scaled(self.size(), Qt.KeepAspectRatio, Qt.SmoothTransformation)
  31. def resizeEvent(self, e):
  32. if not self.pix.isNull():
  33. super(AspectRatioPixmapLabel, self).setPixmap(self.scaledPixmap())