class svg_size(QDialog):
    def showEvent(self, event):
        #self.grview.fitInView(self.scene.sceneRect(), Qt.KeepAspectRatio)
        pass

    def standard_size(self):
        # size in sqares times squaresize, minus distance from square borders
        w = self.ui.spin_width.value() * self.squaresize - 10
        h = self.ui.spin_height.value() * self.squaresize - 10

        standard_size = QRectF(0, 0, w, h)
        start_pos = QPointF(self.startpos + 5, self.startpos + 5)
        self.start_bounding_rect = QRectF(self.startpos + 5, self.startpos + 5,
                                          w, h)
        self.accept_rect(standard_size, start_pos)
        self.user_rect.setRect(self.start_bounding_rect)

    def __init__(self,
                 parent=None,
                 original_path=None,
                 icon_path=None,
                 xmax=1100,
                 ymax=1100,
                 squaresize=100):

        super(svg_size, self).__init__(parent)

        # boilerplate initialization
        self.xmax = xmax
        self.ymax = ymax
        self.squaresize = squaresize
        self.ui = size_gui()
        self.ui.setupUi(self)

        self.startpos = 5 * self.squaresize

        self.icon_path = Path(icon_path) if icon_path else None
        self.original_path = Path(original_path) if original_path else None
        self.outfilepath = None

        # set up grahpicsView
        self.grview = MyQGraphicsView(self)
        self.grview.setGeometry(QRect(0, 0, 571, 571))
        self.grview.setContextMenuPolicy(Qt.NoContextMenu)
        self.grview.setFrameShadow(QFrame.Plain)
        self.grview.setSizeAdjustPolicy(QAbstractScrollArea.AdjustIgnored)
        self.grview.setObjectName("graphicsView")

        #self.ui.graphicsView.wheelEvent = my_mouse_wheel_event
        self.scene = myGraphicsScene(
        )  # use my own scene class with the added signal
        self.scene.setSceneRect(0, 0, self.xmax, self.ymax)

        # Paint the grid lines every 100 "svg_points"
        for i in range(0, self.xmax + 1, self.squaresize):
            self.line(i, hori=False)
        for i in range(0, self.ymax + 1, self.squaresize):
            self.line(i, hori=True)

        # start rect and svg with some standard numbers
        self.svg = QGraphicsSvgItem(str(icon_path))
        self.user_rect = GraphicsRectItem(
            QRectF(self.startpos + 5, self.startpos + 5, self.squaresize - 10,
                   self.squaresize - 10))

        # add svg and rect to scene
        self.scene.addItem(self.svg)
        self.scene.addItem(self.user_rect)

        # set sizes according to spinboxes
        start_bounding_rect = self.standard_size()

        # connect interactive elements
        self.ui.spin_height.valueChanged.connect(self.standard_size)
        self.ui.spin_width.valueChanged.connect(self.standard_size)
        self.scene.rect_changed.connect(self.accept_rect)

        # put the graphics on the screen and show
        self.grview.setScene(self.scene)
        #self.grview.fitInView(self.scene.sceneRect(), Qt.KeepAspectRatio)
        self.grview.show()

    def retranslateUI(self):
        _translate = QtCore.QCoreApplication.translate
        self.graphicsView.setToolTip(
            _translate(
                "dialog_symbol_sizing",
                "After width and height are entered, do the last adjustments in this window"
            ))

    def line(self, coord, hori=True):
        if hori:
            self.scene.addLine(0, coord, self.xmax, coord, QPen())
        else:
            self.scene.addLine(coord, 0, coord, self.ymax, QPen())

    def accept_rect(self, rect, pos):
        ''' takes the new coordinates from the moved rectangle and applies them to the svg graphic'''
        br = self.svg.boundingRect()
        relative = min(br.width(), br.height())
        rect_min = min(rect.width(), rect.height())
        scale = rect_min / relative
        self.svg.setScale(scale)
        self.svg_scale = scale

        pos_x = pos.x() + rect.x()
        pos_y = pos.y() + rect.y()
        self.svg.setX(pos_x)
        self.svg.setY(pos_y)
        self.svg_pos = (pos_x % self.squaresize, pos_y % self.squaresize)