示例#1
0
文件: bug_714.py 项目: Hasimir/PySide
    def testReference(self):
        l = QLabel()
        p = QPixmap()
        l.setPixmap(p) # doesn't increment pixmap ref because this makes a copy
        self.assertEqual(sys.getrefcount(p), 2)

        p = l.pixmap() # this increment the reference because this is an internal pointer
        self.assertEqual(sys.getrefcount(p), 3)

        p2 = l.pixmap()
        self.assertEqual(p, p2)
示例#2
0
    def testReference(self):
        l = QLabel()
        p = QPixmap()
        l.setPixmap(
            p)  # doesn't increment pixmap ref because this makes a copy
        self.assertEqual(sys.getrefcount(p), 2)

        p = l.pixmap(
        )  # this increment the reference because this is an internal pointer
        self.assertEqual(sys.getrefcount(p), 3)

        p2 = l.pixmap()
        self.assertEqual(p, p2)
示例#3
0
class ImageViewer(QWidget):
    """A custom widget to display an image in Gui."""

    # Inspired by :
    # https://doc.qt.io/qt-6/qtwidgets-widgets-imageviewer-example.html
    # https://code.qt.io/cgit/pyside/pyside-setup.git/tree/examples/widgets/imageviewer

    def __init__(self, parent=None):
        """Initialize Widget."""
        super().__init__(parent)
        self.setLayout(QVBoxLayout())

        self.imglabel = QLabel()
        self.imglabel.setBackgroundRole(QPalette.Base)
        self.imglabel.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
        self.imglabel.setScaledContents(True)  # Resize pixmap along with label
        self.imglabel.setAlignment(Qt.AlignCenter)
        self.imglabel.setText("(No image yet)")

        self.namelabel = QLabel()

        self.scrollarea = QScrollArea()
        self.scrollarea.setWidget(self.imglabel)
        self.scrollarea.setWidgetResizable(False)
        self.scrollarea.setAlignment(Qt.AlignCenter)

        self.layout().addWidget(self.scrollarea)
        self.layout().addWidget(self.namelabel)

        self.scale_factor = 1.0
        self._initial_size = QSize(0, 0)
        self._img_path = ""

        self.setContextMenuPolicy(Qt.CustomContextMenu)
        # pylint: disable=no-member
        self.customContextMenuRequested.connect(self.show_context_menu)
        self.menu = QMenu()
        self.add_actions_to_menu(self.menu)

    def add_actions_to_menu(self, menu):
        """Add widget actions to a given menu.

        Args:
            menu -- the menu to add the actions (QMenu)
        """
        zoom_in_act = menu.addAction("Zoom In (25%)")
        zoom_in_act.triggered.connect(self.zoom_in)

        zoom_out_act = menu.addAction("Zoom Out (25%)")
        zoom_out_act.triggered.connect(self.zoom_out)

        zoom_normal_act = menu.addAction("Normal Size")
        zoom_normal_act.triggered.connect(self.normal_size)

        menu.addSeparator()

        copy_filename_act = menu.addAction("Copy Image to Clipboard")
        copy_filename_act.triggered.connect(self.copy_image)

        copy_filename_act = menu.addAction("Copy Filename to Clipboard")
        copy_filename_act.triggered.connect(self.copy_filename)

    def load_image(self, img_path):
        """Load an image in widget from a file.

        Args:
            img_path -- Path of image file to load (str)
        """
        img_path = str(img_path)
        pixmap = QPixmap(img_path)
        self.imglabel.setPixmap(pixmap)
        self.imglabel.resize(pixmap.size())
        self.namelabel.setText(f"File: {img_path}")
        self._img_path = img_path
        self._initial_size = pixmap.size()

    def resize_image(self, new_size=None):
        """Resize embedded image to target size.

        Args:
            new_size -- target size to resize image to (QSize)
        """
        if not new_size:
            new_size = self._initial_size
            self.scale_factor = 1.0
        new_size = QSize(new_size)
        self.imglabel.setMinimumSize(new_size)
        self.imglabel.setMaximumSize(new_size)

    def scale_image(self, factor):
        """Rescale embedded image applying a factor.

        Factor is applied relatively to current scale.

        Args:
            factor -- Factor to apply (float)
        """
        self.scale_factor *= float(factor)
        new_size = self.scale_factor * self._initial_size
        self.resize_image(new_size)

    @Slot()
    def zoom_in(self):
        """Zoom embedded image in (slot)."""
        self.scale_image(1.25)

    @Slot()
    def zoom_out(self):
        """Zoom embedded image out (slot)."""
        self.scale_image(0.8)

    @Slot()
    def normal_size(self):
        """Set embedded image scale to 1:1 (slot)."""
        self.resize_image()

    @Slot()
    def copy_filename(self):
        """Copy file name to clipboard (slot)."""
        QGuiApplication.clipboard().setText(self._img_path)

    @Slot()
    def copy_image(self):
        """Copy embedded image to clipboard (slot)."""
        QGuiApplication.clipboard().setImage(self.imglabel.pixmap().toImage())

    @Slot(QPoint)
    def show_context_menu(self, pos):
        """Show context menu."""
        self.menu.exec_(self.mapToGlobal(pos))