Ejemplo n.º 1
0
    def paintEvent(self, event):
        if len(self.main_element) > 0:
            renderer = self.renderer()
            bounds = renderer.boundsOnElement(self.main_element)
            svg_dx = bounds.width()
            svg_dy = bounds.height()
            svg_ratio = svg_dy / svg_dx

            widget_dx = self.width()
            widget_dy = self.height()
            widget_ratio = widget_dy / widget_dx

            if widget_ratio > svg_ratio:
                # widget is taller than svg item so pick taller view
                height = svg_dx * widget_ratio
                extra = (height - svg_dy) / 2
                margins = QMarginsF(0, extra, 0, extra)
            elif widget_ratio < svg_ratio:
                # widget is wider than svg item so pick wider view
                width = svg_dy / widget_ratio
                extra = (width - svg_dx) / 2
                margins = QMarginsF(extra, 0, extra, 0)
            else:
                margins = QMarginsF()

            bounds = bounds.marginsAdded(margins)

            renderer.setViewBox(bounds)

        QSvgWidget.paintEvent(self, event)