Source code for models.model_zoom_area

"""
The model_zoom_area module manages the zoom area selection within the MoilApp.
"""

from PyQt6.QtCore import QRect


[docs] class ModelZoomArea: """ Class responsible for managing the zoom area selection. Attributes: _model (object): The main model instance of the application. _state_rubberband (bool): State of the rubberband. _size_zoom (bool): Size of the zoom area. _size_rubberband (QRect): Size of the rubberband. _point_rectangle (list): List to store points defining the zoom area rectangle. Methods: point_rectangle: Getter method for the points defining the zoom area rectangle. state_rubberband: Getter and setter methods for the state of the rubberband. size_rubberband: Getter and setter methods for the size of the rubberband. size_zoom: Getter and setter methods for the size of the zoom area. crop_image(image_ori): Crop the image based on the zoom area selection. """ def __init__(self, model): self._model = model self._state_rubberband = False self._size_zoom = False self._size_rubberband = None self._point_rectangle = [None, None] @property def point_rectangle(self): """ Getter for the points defining the zoom area rectangle. """ try: return self._point_rectangle except Exception as e: self._model.activity_logger.error(f"ModelZoomArea.point_rectangle(): Error: {str(e)}") @property def state_rubberband(self): """ Getter for the state of the rubberband. """ try: return self._state_rubberband except Exception as e: self._model.activity_logger.error(f"ModelZoomArea.state_rubberband(): Error: {str(e)}") @state_rubberband.setter def state_rubberband(self, state): """ Setter for the state of the rubberband. """ try: self._state_rubberband = state except Exception as e: self._model.activity_logger.error(f"ModelZoomArea.state_rubberband(): Error: {str(e)}") @property def size_rubberband(self): """ Getter for the size of the rubberband. """ try: return self._size_rubberband except Exception as e: self._model.activity_logger.error(f"ModelZoomArea.size_rubberband(): Error: {str(e)}") @size_rubberband.setter def size_rubberband(self, value): """ Setter for the size of the rubberband. """ try: self._size_rubberband = value except Exception as e: self._model.activity_logger.error(f"ModelZoomArea.size_rubberband(): Error: {str(e)}") @property def size_zoom(self): """ Getter for the size of the zoom area. """ try: return self._size_zoom except Exception as e: self._model.activity_logger.error(f"ModelZoomArea.size_zoom(): Error: {str(e)}") @size_zoom.setter def size_zoom(self, value): """ Setter for the size of the zoom area. """ try: self._size_zoom = value except Exception as e: self._model.activity_logger.error(f"ModelZoomArea.size_zoom(): Error: {str(e)}")
[docs] def crop_image(self, image_ori): """ Crop the image based on the zoom area selection. This method crops the input image based on the zoom area selection using the rubberband's size and position. Arg: image_ori: The original image to be cropped. Return: The cropped image. """ try: image = self._model.convert_cv2_to_q_image(image_ori) height = self._model.calculate_height(image_ori, self.size_zoom) ratio_x = self._model.main_config["Image_original"]["width"] / self.size_zoom ratio_y = self._model.main_config["Image_original"]["height"] / height x = int(self.size_rubberband.x() * ratio_x) y = int(self.size_rubberband.y() * ratio_y) width = int(self.size_rubberband.width() * ratio_x) height = int(self.size_rubberband.height() * ratio_y) rect = QRect(x, y, width, height) cropped_image = image.copy(rect) self._point_rectangle[0] = (round(x), round(y)) self._point_rectangle[1] = (round(x + width), round(y + height)) return self._model.convert_q_image_to_mat(cropped_image) except Exception as e: self._model.activity_logger.error(f"ModelZoomArea.crop_image(): Error: {str(e)}")