def paintEvent(self, event): """Overrides :meth:`QtWidgets.QWidget.paintEvent`.""" option = QtWidgets.QStyleOption() option.initFrom(self) h = option.rect.height() w = option.rect.width() if self._shape == Shapes.Triangle or self._shape == Shapes.Rounded: aspect = self._triangle_factor if self._shape == Shapes.Triangle else self._rounded_factor ah = w / aspect aw = w if ah > h: ah = h aw = h * aspect x = abs(aw - w) / 2.0 y = abs(ah - h) / 2.0 bounds = QtCore.QRectF(x, y, aw, ah) else: size = min(w, h) x = abs(size - w) / 2.0 y = abs(size - h) / 2.0 bounds = QtCore.QRectF(x, y, size, size) painter = QtGui.QPainter(self) painter.setRenderHint(QtGui.QPainter.Antialiasing, True) if self.isEnabled(): color = self._on_color if self._is_on else self._off_color else: color = QtGui.QColor('#BDBDBD') lighter = QtGui.QColor(color).lighter(150) # 150 -> 50% brighter color_str = 'rgb(%d,%d,%d)' % (color.red(), color.green(), color.blue()) lighter_str = 'rgb(%d,%d,%d)' % (lighter.red(), lighter.green(), lighter.blue()) svg = (SVG_MAP[self._shape] % (color_str, lighter_str)).encode('utf8') self._renderer.load(QtCore.QByteArray(svg)) self._renderer.render(painter, bounds)
def select_zoom(self, event): """Called when the user wants to zoom in.""" event.accept() if self.canvas.image is None: return if event.button() & (QtCore.Qt.LeftButton | QtCore.Qt.MidButton): if event.isFinish(): self.view_box.rbScaleBox.hide() # get the coordinates of the highlighted region rect = QtCore.QRectF(pg.Point(event.buttonDownPos(event.button())), pg.Point(event.pos())) rect = self.view_box.childGroup.mapRectFromParent(rect).normalized() # get the size of the image on the canvas # DO NOT use self.canvas.width(), self.canvas.height() height, width = self.original_image.shape[:2] if self.zoom_history: width = int(self.zoom_history[-1][2] * width) height = int(self.zoom_history[-1][3] * height) angle = self.rotate_slider.value() values = rotate_zoom(angle, rect, width, height) if values is None: return # zoom outside of the image x, y, w, h, width, height = values # convert to the coordinates of the original (un-zoomed) image if self.zoom_history: orig_height, orig_width = self.original_image.shape[:2] x2, y2, w2, h2 = self.zoom_history[-1] x = x * width / orig_width + x2 y = y * height / orig_height + y2 w *= w2 h *= h2 self.append_zoom(x, y, w, h) else: self.view_box.updateScaleBox(event.buttonDownPos(), event.pos())