
    cc6+                     `    d dl Z d dlmZmZmZ  G d de      Z G d de      Z G d d      Zy)	    N)GtkGdk	GdkPixbufc                        e Zd Z fdZ xZS )NoSelectionPixbufExceptionc                 6    t         |   t        d             y )NzThe selection pixbuf is empty.super__init___selfargs	__class__s     ;/usr/lib/python3/dist-packages/drawing/selection_manager.pyr   z#NoSelectionPixbufException.__init__   s    '1567    __name__
__module____qualname__r   __classcell__r   s   @r   r   r      s    8 8r   r   c                        e Zd Z fdZ xZS )NoSelectionPathExceptionc                 6    t         |   t        d             y )NzThe selection path is empty.r	   r   s     r   r   z!NoSelectionPathException.__init__   s    '1345r   r   r   s   @r   r   r      s    6 6r   r   c                       e Zd Zd Zd Zd ZddZd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zy)DrSelectionManagerc                    || _         | j                          | j                          t        j                  j                  d      }|j                  d      }t        j                  j                  | j                   |      | _	        |j                  d      }t        j                  j                  | j                   |      | _
        | j                  dd       y )Nz5/com/github/maoschanz/drawing/ui/selection-manager.uizinactive-selection-menuzactive-selection-menug      ?)imageinit_pixbufreset_future_datar   Buildernew_from_resource
get_objectPopovernew_from_modelmenu_if_inactivemenu_if_activeset_popovers_position)r   r   buildermenu_rmenu_ls        r   r   zDrSelectionManager.__init__#   s    $*KK))IK'78&++44TZZH$56&224::vF$S#&r   c                     t         j                  j                  t         j                  j                  dddd      | _        | j                  ddd       d | _        d| _        y )NT      r   F)	r   Pixbufnew
ColorspaceRGBselection_pixbuf
set_coordsselection_path	is_activer   s    r   r    zDrSelectionManager.init_pixbuf0   sR    #**..y/C/C/G/G<@!QK$//$1$$.r   Nc           	      F   |
t               || _        d| _        | j                  j                  }t        j                  |dd      }|j                  | j                  j                  | j                  j                         t        j                  |      }|j                  t        j                  j                         |j                          |j                  | j                         |j!                          |j                  t        j                  j"                         |j%                         }|j'                         }|j)                         \  }}	}
}t+        |
|      }
t+        ||      }t-        t/        |d            }t-        t/        |	d            }	| j0                  dk  r| j0                  }| j2                  dk  r| j2                  }	t-        |
|z
        }t-        ||	z
        }|dkD  r|dkD  rt        j4                  |t-        |      t-        |	      ||      }|d|I|d   dkD  rA|j7                  dt-        |d   dz        t-        |d   dz        t-        |d   dz              }|| _        n| j;                  d       | j                  j=                          y)	zwCreate a selection_pixbuf from a minimal part of the main surface by
		erasing everything outside of the provided path.NTr                 r/      )r   r6   r7   r   main_pixbufr    cairo_surface_create_from_pixbufset_device_scaleSCALE_FACTORcairoContextset_operatorOperatorDEST_INnew_pathappend_pathfill_preserveOVER	get_width
get_heightpath_extentsminintmaxselection_xselection_ypixbuf_get_from_surface	add_alphar4   resetupdate_actions_state)r   rG   rgbar>   surfacecairo_context
main_widthmain_heightxminyminxmaxymaxselection_widthselection_heightpixbufs                  r   load_from_pathz!DrSelectionManager.load_from_path8   sG    	!	## $$.

&&+ 00aF'	4::22DJJ4K4KL--(-U^^334D//0U^^001 $$&*&&(+(557$dD	T:	$	T;	$	c$n	$	c$n	$	


4	


4 t$/%q-1''TCI%46FH6DGcMtSa3%7%(a3%7%(a3%79V #D ::d**!!#r   c                 B    || _         || _        |r|| _        || _        y y N)rQ   rR   temp_xtemp_y)r   temp_tooxys       r   r5   zDrSelectionManager.set_coordsl   s(    $$4;4; r   c                 2    || _         | j                          y re   )r4   _create_path_from_pixbuf)r   rb   s     r   
set_pixbufzDrSelectionManager.set_pixbufs   s     $!r   c                     | j                   S re   )r4   r8   s    r   
get_pixbufzDrSelectionManager.get_pixbufx   s    			r   c                     d | _         d | _        | j                  ddd       d| _        |r5| j                  j                          | j                  j                          y y )NTr   F)r4   r6   r5   r7   r   rV   update)r   update_images     r   rU   zDrSelectionManager.reset{   sQ    $$//$1$.::""$:: r   c                 ^   | j                   
t               || j                  j                  z
  | j                  z   | j
                  z
  }|| j                  j                  z
  | j                  z   | j                  z
  }| j                  ||      }|j                          |j                         S re   )r6   r   r   scroll_xrQ   rf   scroll_yrR   rg   _get_context_with_path
close_path	copy_path)r   tool_dxtool_dydelta_xdelta_yrY   s         r   get_path_with_scrollz'DrSelectionManager.get_path_with_scroll   s    	 	!	##djj)))D,<,<<t{{J'djj)))D,<,<<t{{J'--gw?-		 	 	""r   c                    | j                   
t               |rM| j                  | j                  j                  z
  |z   }| j
                  | j                  j                  z
  |z   }n| j                  |z   }| j
                  |z   }t        j                  || j                   ||       | j                  j                         r-|j                         j                  t        j                         |j                          y re   )r4   r   rQ   r   rt   rR   ru   r   cairo_set_source_pixbufis_zoomed_surface_sharp
get_source
set_filterrB   FILTER_NEARESTpaint)r   rY   with_scrollry   rz   ri   rj   s          r   show_selection_on_surfacez,DrSelectionManager.show_selection_on_surface   s    	"	#	%%$**---71$**---71'!1'!1mT-B-BAqI	ZZ'')(()=)=>r   c                     | j                   j                         }| j                   j                         }| j                  |dz  z   | j                  |dz  z   fS )z1Return the coords of the center of the selection.r=   )r4   rK   rL   rQ   rR   )r   whs      r   get_center_coordsz$DrSelectionManager.get_center_coords   sS    
%%'!
&&(!			AE	!4#3#3a!e#;	;;r   c                    | j                   sy| j                  | j                  j                  | j                  j                        }| j                         }|j                          |j                  |       |j                  ||      S )zReturns a boolean if the point whose coordinates are "(tested_x,
		tested_y)" is in the path defining the selection. If such path doesn't
		exist, it returns None.T)	r7   r}   r   rt   ru   _get_contextrG   rH   in_fill)r   tested_xtested_yscrolled_pathrY   s        r   point_is_in_selectionz(DrSelectionManager.point_is_in_selection   so     

++DJJ,?,?ATATU-##%-M*			x	22r   c                    | j                   
t               | j                  | _        | j                  | _        d| _        | j                         }|j                  | j                  | j                         |j                  | j                   j                         d       |j                  d| j                   j                                |j                  d| j                   j                         z  d       |j                          |j                         | _        | j                          | j                   j#                          y)zThis method creates a rectangle selection from the currently set
		selection pixbuf.
		It can be the result of an editing operation (crop, scale, etc.), or it
		can be an imported picture (from a file or from the clipboard).NTr   )r4   r   rQ   rf   rR   rg   r7   r   move_torel_line_torK   rL   rw   rx   r6   hide_popoversr   rV   )r   rY   s     r   rl   z+DrSelectionManager._create_path_from_pixbuf   s    
 
"	#	%%  $+  $+$.##%-(($*:*:;D11;;=qAAt44??ABB!6!6!@!@!BBAF%//1$**!!#r   c                 T    t        j                  | j                  j                        S re   )rB   rC   r   rX   r8   s    r   r   zDrSelectionManager._get_context   s    	tzz))	**r   c                     | j                         }| j                  D ]E  }|d   dk7  s|d   d   |z   }|d   d   |z   }|j                  t        |      t        |             G |S )Nr/    r   )r   r6   line_torO   )r   r{   r|   rY   ptsri   rj   s          r   rv   z)DrSelectionManager._get_context_with_path   st    ##%-   *c	!flAq	GAAq	GA#a&#a&)	*
 
r   c                     t        j                         }t        |      |_        t        |      |_        d|_        d|_        | j                  j                  |       | j                  j                  |       y)zpSet the coords where the popover should be opened. These coords are
		relative TO THE WIDGET, not to the pixbuf.r/   N)
r   	RectanglerO   ri   rj   heightwidthr'   set_pointing_tor(   )r   ri   rj   	rectangles       r   r)   z(DrSelectionManager.set_popovers_position   s\     mmo)A)+A)+))/''	2%%i0r   c                 l    | j                   j                          | j                  j                          y re   )r(   popdownr'   r8   s    r   r   z DrSelectionManager.hide_popovers   s&    !r   c                     | j                   r| j                  j                          y| j                  j                          y)z7Open the adequate popover at the previously set coords.N)r7   r(   popupr'   r8   s    r   show_popoverzDrSelectionManager.show_popover   s-    	^^ r   c                 .    d| _         d| _        d | _        y )Nr   )	_future_x	_future_y_future_pathr8   s    r   r!   z$DrSelectionManager.reset_future_data   s    $.$.$r   c                 D    t        |      | _        t        |      | _        y re   )rO   r   r   )r   ri   rj   s      r   set_future_coordsz$DrSelectionManager.set_future_coords   s    q6$.q6$.r   c                 2    | j                   | j                  fS re   )r   r   r8   s    r   get_future_coordsz$DrSelectionManager.get_future_coords   s    		''r   c                 x   || _         |sy | j                  j                  j                         }| j                  j                  j	                         }||}}| j                   D ]/  }|d   dk7  st        |d   d   |      }t        |d   d   |      }1 | j                  t        |d      t        |d             y )Nr/   r   r   r:   )r   r   r>   rK   rL   rN   r   rP   )r   pathresync_coordsrZ   r[   r\   r]   r   s           r   set_future_pathz"DrSelectionManager.set_future_path   s    $		 zz%%//1*

&&113+;$  c	!fls1vay$Ds1vay$D  T3T38r   c                     | j                   S re   )r   r8   s    r   get_future_pathz"DrSelectionManager.get_future_path	  s    			r   c                     | j                  |       | j                  |z   }| j                  |z   }| j                  d||       | j	                  | j
                  |z   | j                  |z          y )NF)rm   rQ   rR   r5   r   r   r   )r   
new_pixbufdxdyri   rj   s         r   update_from_transform_toolz-DrSelectionManager.update_from_transform_tool  s`    //*
!
!//%A",dnnr.ABr   c                    t        d| j                  | j                         t        d| j                  | j                         t        d| j
                  j                  | j
                  j                         t        d| j
                  j                         t        d       d| j
                  j                  z
  | j                  z   | j                  z
  }d| j
                  j                  z
  | j                  z   | j                  z
  }| j                  D ].  }|d   dk7  s|d   d   |z   }|d   d   |z   }t        d	||       0 t        d
| j                         t        d| j                  | j                         t        d       y)z_Development only: is linked to the "troubleshoot selection" item in
		the short hamburger menu.zselection coordsztemp coordszimage.scroll coordszimage.zoom_levelz(selection_path with scroll & temp deltasr   r/   r   	zfuture pathzfuture coordsz?---------------------------------------------------------------N)printrQ   rR   rf   rg   r   rt   ru   
zoom_levelr6   r   r   r   )r   r   r{   r|   r   ri   rj   s          r   print_valueszDrSelectionManager.print_values  s?    D,,d.>.>?t{{DKK0tzz22DJJ4G4GHDJJ11223

###d&6&66D'

###d&6&66D'   c	!flAq	GAAq	GA	$1	 t(()8IJr   re   )r   r   r   __gtype_name__r   r    rc   r5   rm   ro   rU   r}   r   r   r   rl   r   rv   r)   r   r   r!   r   r   r   r   r   r   r   r   r   r   r       s    &'2$h"
#<
3$(+	1"!
(9 CKr   r   )	rB   gi.repositoryr   r   r   	Exceptionr   r   r   r   r   r   <module>r      s6   $  - -8 8
6y 6IK IKr   