Repository: matplotlib/matplotlib
File: lib/matplotlib/backends/backend_qt.py (Python)

Code before:
            svg_content = svg_content.replace(b'fill:black;', b'fill:white;')
            svg_content = svg_content.replace(b'stroke:black;', b'stroke:white;')

        try:
            from PyQt5.QtSvg import QSvgRenderer
        except Exception:
            from PySide2.QtSvg import QSvgRenderer

        renderer = QSvgRenderer(QtCore.QByteArray(svg_content))
        if not renderer.isValid():
            raise Exception("Invalid SVG content")

        icon_size = self.iconSize()
        if not icon_size.isValid() or icon_size.width() <= 0 or icon_size.height() <= 0:
            if hasattr(self, 'style'):
                style = self.style()
            else:
                style = QtWidgets.QApplication.style()
            pm_metric = QtWidgets.QStyle.PixelMetric.PM_ToolBarIconSize
            sz = style.pixelMetric(pm_metric)
            icon_size = QtCore.QSize(sz, sz)

        w = max(1, icon_size.width())
        h = max(1, icon_size.height())

        ratio = self.devicePixelRatioF() or 1
        pm = QtGui.QPixmap(int(w * ratio), int(h * ratio))
        pm.setDevicePixelRatio(ratio)
        pm.fill(QtCore.Qt.GlobalColor.transparent)

        painter = QtGui.QPainter()
        try:
            painter.begin(pm)
            renderer.render(painter, QtCore.QRectF(0, 0, w, h))
        finally:
            if painter.isActive():
                painter.end()
        return QtGui.QIcon(pm)

    def edit_parameters(self):
        axes = self.canvas.figure.get_axes()
        if not axes:
            QtWidgets.QMessageBox.warning(
                self.canvas.parent(), "Error", "There are no Axes to edit.")
            return
        elif len(axes) == 1:
            ax, = axes
        else:
            titles = [
                ax.get_label() or
                ax.get_title() or

Review comment (suggestion):
given that this is depnedent of the dpi ratio we need to update this when it changes like we do with the rendered images.

This handles both the case where the user moves the figure from a low to high DPI screen and details about wayland where the first ratio you are given is always an integer and the true fractional ratio is set via an update after the window is shown.

Code after:
        scaled_pixmap.setDevicePixelRatio(dpr)

        if self._is_dark_mode():
            # On some platforms (e.g., macOS with Qt5 in dark mode), this may
            # incorrectly return a black color instead of a light one.
            # See issue #27590 for details.
            icon_color = self.toolbar.palette().color(self.toolbar.foregroundRole())
            mask = scaled_pixmap.createMaskFromColor(
                QtGui.QColor('black'),
                QtCore.Qt.MaskMode.MaskOutColor)
            scaled_pixmap.fill(icon_color)
            scaled_pixmap.setMask(mask)

        return scaled_pixmap


class NavigationToolbar2QT(NavigationToolbar2, QtWidgets.QToolBar):
    toolitems = [*NavigationToolbar2.toolitems]
    toolitems.insert(
        # Add 'customize' action after 'subplots'
        [name for name, *_ in toolitems].index("Subplots") + 1,
        ("Customize", "Edit axis, curve and image parameters",
         "qt4_editor_options", "edit_parameters"))

    def __init__(self, canvas, parent=None, coordinates=True):
        """coordinates: should we show the coordinates on the right?"""
        QtWidgets.QToolBar.__init__(self, parent)
        self.setAllowedAreas(QtCore.Qt.ToolBarArea(
            _to_int(QtCore.Qt.ToolBarArea.TopToolBarArea) |
            _to_int(QtCore.Qt.ToolBarArea.BottomToolBarArea)))
        self.coordinates = coordinates
        self._actions = {}  # mapping of toolitem method names to QActions.
        self._subplot_dialog = None

        for text, tooltip_text, image_file, callback in self.toolitems:
            if text is None:
                self.addSeparator()
            else:
                slot = getattr(self, callback)
                # https://bugreports.qt.io/browse/PYSIDE-2512
                slot = functools.wraps(slot)(functools.partial(slot))
                slot = QtCore.Slot()(slot)

                a = self.addAction(self._icon(image_file + '.png'),
                                   text, slot)
                self._actions[callback] = a
                if callback in ['zoom', 'pan']:
                    a.setCheckable(True)
                if tooltip_text is not None:
                    a.setToolTip(tooltip_text)