from pycs.ui.AnnotatedImageView import AnnotatedImageView class AnnotatedInteractiveImageView(AnnotatedImageView): def __init__(self, parent, click_handler=None): super().__init__(parent) self.__click_handler = click_handler def mousePressEvent(self, event): """ handle click event, get point and emit to parent node :param event: :return: """ # do not execute if no click handler is given if self.__click_handler is None: return # get clicked point pos = event.pos() point_x, point_y = pos.x() / self.width(), pos.y() / self.height() # get image and box dimensions image_width, image_height = self._image.width(), self._image.height() box_width, box_height = self.width(), self.height() maximum_width, maximum_height = max(image_width, box_width), max(image_height, box_height) # determine orientation # horizontal if image_width / image_height >= box_width / box_height: # calculate top margin image_height, box_height = maximum_width / image_width * image_height, maximum_width / box_width * box_height top = (box_height - image_height) / 2 # click is inside the image if top <= point_y * box_height <= box_height - top: off = top / box_height rat = 1 - 2 * off self.__click_handler(point_x, (point_y - off) / rat, event) # vertical else: # calculate left margin image_width, box_width = maximum_height / image_height * image_width, maximum_height / box_height * box_width left = (box_width - image_width) / 2 if left <= point_x * box_width <= box_width - left: off = left / box_width rat = 1 - 2 * off self.__click_handler((point_x - off) / rat, point_y, event)