@@ -114,8 +114,10 @@ def __init__(self, parent:Optional[QWidget]=None, viewport:bool=True) -> None:
114114 self .fps : int = 5
115115 self .scaleBar : bool = False
116116 self .crop : Optional [QRect ] = None
117+ self .startCrop : bool = False
117118 self .scale : List [int ] = []
118119 self .rubberBand : "ResizableRubberBand|None" = None
120+ self .zoomRubberBand : "Optional[ResizableRubberBand]" = None
119121 self ._grid_color : "QColor|None" = None
120122
121123 self .url : str = 'http://localhost:9998/jpg/image.jpg'
@@ -229,26 +231,34 @@ def paintEvent(self, a0: QPaintEvent) -> None:
229231
230232 def mousePressEvent (self , a0 : QMouseEvent ):
231233 if a0 .buttons () == Qt .LeftButton :
232- pos = a0 .pos ()
233- #self.roiClicked.emit(pos.x(), pos.y())
234- #self.clicks.append(pos)
235- self .temp_start = pos
236- #self.end = pos
237- #self.update()
238- if not self .rubberBand and not self .viewport :
234+ self .temp_start = a0 .pos ()
235+ if not self .rubberBand and not self .viewport and not self .startCrop :
239236 self .rubberBand = ResizableRubberBand (self )
240237 self .rubberBand .box_modified .connect (self .update_grid )
241238 self .rubberBand .setGeometry (QRect (self .start , QSize ()))
242239 self .rubberBand .show ()
240+ elif not self .zoomRubberBand and not self .viewport and self .startCrop :
241+ self .zoomRubberBand = ResizableRubberBand (self )
243242 else :
244243 self .clicked_url .emit (self .url )
245244
246245 def mouseMoveEvent (self , a0 : QMouseEvent ):
247- if self .rubberBand and a0 .buttons () == Qt .LeftButton :
246+ if self .rubberBand and not self . startCrop and a0 .buttons () == Qt .LeftButton :
248247 if self .rubberBand .isVisible ():
249248 self .rubberBand .setGeometry (QRect (self .start , a0 .pos ()).normalized ())
250249 self .start = self .temp_start
251250 self .end = a0 .pos ()
251+ if self .startCrop and a0 .buttons () == Qt .LeftButton :
252+ #if self.zoomRubberBand.isVisible():
253+ self .zoomRubberBand .show ()
254+ self .zoomRubberBand .setGeometry (QRect (self .temp_start , a0 .pos ()).normalized ())
255+
256+ def mouseReleaseEvent (self , a0 : QMouseEvent ) -> None :
257+ if self .startCrop :
258+ self ._crop_image ()
259+ self .startCrop = False
260+ self .unsetCursor ()
261+ return super ().mouseReleaseEvent (a0 )
252262
253263 def contextMenuEvent (self , a0 : QContextMenuEvent ) -> None :
254264 super ().contextMenuEvent (a0 )
@@ -261,7 +271,7 @@ def contextMenuEvent(self, a0: QContextMenuEvent) -> None:
261271
262272 if self .rubberBand :
263273 hide_show_action .triggered .connect (self .rubberBand .toggle_selector )
264- crop_action .triggered .connect (self ._crop_image )
274+ crop_action .triggered .connect (self ._start_crop )
265275 reset_crop_action .triggered .connect (self ._reset_crop )
266276 hide_show_grid_action .triggered .connect (self ._toggle_grid )
267277 select_grid_color_action .triggered .connect (self ._select_grid_color )
@@ -272,6 +282,10 @@ def contextMenuEvent(self, a0: QContextMenuEvent) -> None:
272282 reset_crop_action ])
273283 self .menu .move (a0 .globalPos ())
274284 self .menu .show ()
285+
286+ def _start_crop (self ) -> None :
287+ self .startCrop = True
288+ self .setCursor (Qt .CrossCursor )
275289
276290 def _select_grid_color (self ) -> None :
277291 self ._grid_color = QColorDialog .getColor ()
@@ -283,17 +297,17 @@ def _reset_crop(self) -> None:
283297 self .crop = None
284298
285299 def _crop_image (self ) -> None :
286- if self .rubberBand :
300+ if self .zoomRubberBand :
287301 width_scaling_factor = self .org_image_wd / self .image .width ()
288302 ht_scaling_factor = self .org_image_ht / self .image .height ()
289303
290- rect_x , rect_y , rect_width , rect_ht = self .rubberBand .geometry ().getRect ()
304+ rect_x , rect_y , rect_width , rect_ht = self .zoomRubberBand .geometry ().getRect ()
291305 x = int (rect_x * width_scaling_factor )
292306 y = int (rect_y * ht_scaling_factor )
293307 wd = int (rect_width * width_scaling_factor )
294308 ht = int (rect_ht * ht_scaling_factor )
295309 self .crop = QRect (x ,y ,wd ,ht )
296- self .rubberBand .hide ()
310+ self .zoomRubberBand .hide ()
297311
298312 def update_grid (self , start : QPoint , end : QPoint ) -> None :
299313 self .start = start
0 commit comments