
    cc3                         d dl mZmZmZ ddlmZ ddlmZ ddlm	Z	 g dZ
 G d d      Z G d	 d
e      Z G d de      Zy)    )Gtk	GdkPixbufGLib   )DrMessageDialog)!utilities_add_filechooser_filters)utilities_rgb_to_hexadecimal)jpegjpgjpepngtifficobmpc                   @    e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
d Zy	)
DrSavingManagerc                     || _         y )N)_window)selfwindows     8/usr/lib/python3/dist-packages/drawing/saving_manager.py__init__zDrSavingManager.__init__   s	    $,    c                    |s| j                         sy|rd}| j                  j                         }|j                         |s|r| j	                         }n|j
                  }|y|j                         }| j                  |      }|r|j                  j                         }	n|j                  }	|sd}
n| }
|dv }|r|sP	 | j                  j                  j                  d      }|dk(  r| j                  ||
      }| j                  |	||      }	|j-                  d       	 |	j/                  ||dgg        |sQ||_        |j1                          |j3                          |j5                          | j                  j7                          |j-                  d       |s9	 ||_        |j1                          |j5                          |j9                          yy# t        $ r}| j!                  dddd      cY d}~S d}~wt"        $ r
}Y d}~yd}~wt$        $ r:}t'        |       | j                  j)                  t+        d             Y d}~;d}~ww xY w# t$        $ rM}|j-                  d       t'        |       | j                  j)                  t+        d      |z         Y d}~yd}~ww xY w# t$        $ r<}t'        |       | j                  j)                  t+        d	      |z         Y d}~yd}~ww xY w)
zAll parameters are booleans. The value of `allow_alpha` reflects the
		user's intention (action they activated), not a technical capability.
		It returns a boolean (true if success).FTN)r   zreplace-alphaaskzFailed to replace transparencyzFailed to save %szFailed to reload %s)"_confirm_despite_ongoing_operationr   get_active_imageget_file_path_file_chooser_savegfileget_path_get_format	selection
get_pixbufmain_pixbuf	gsettings
get_string_ask_overwrite_alpha_replace_alphaWantAnotherFormatExceptionsave_current_imageWantToCancelException	Exceptionprintreveal_message_set_monitoringsavevconnect_gfile_monitoringremember_current_state	post_saveupdate_picture_titlereload_from_disk)r   	is_exportto_newselection_onlyallow_alphaimager    	file_pathfile_formatpixbufcan_save_assupports_alphareplacementes                 r   r+   z"DrSavingManager.save_current_image!   s    
 G G I
6
,,
'
'
)%
"f""$5;;5
]
nn)  ++OO&&(66 
 ; ;').	NE,,((33ODKe,,^[I[  e<F u	<<	;3 EK	""$	  "	OOLL%%' t		EK	""$	OO	 
c % =""5$t<<
 
 E	!HLL"B CDDE& 
 8<<q!45	AB
"  	!HLL"7 89 DE	sc   AG "A(I 7J, 	I G93I9I
I/II	J)AJ$$J),	K152K,,K1c                 (   | j                   j                         }|j                         ry|j                         }|t	        d      dz   }t	        d      }n$|j                  d      d   }|j                         }t        | j                         }|j                  t	        d      d	      }|j                  t	        d
      d      }|j                  t	        d      dd      }|j                  t	        d      |z         | j                   j                  j                          t        j                         j                  | j                   j                  j                        }t        j                   t        j"                  j$                  t        j"                  j$                        }	|	j'                  |       |j)                  |	       |j+                         }
|j-                          |
|k(  r| j                   j/                         S |
|k(  ryy)zReturn True if the image can be closed/overwritten (whether it's
		saved or not), or False otherwise (usually if the user clicked 'cancel',
		or if an error occurred).TNUntitled.pngzthis picture/Discarddestructive-actionCancelSavez&There are unsaved modifications to %s.valignhalignF)r   r   is_savedr   r0   splitget_filename_for_displayr   
set_action
add_stringminimapupdate_contentr   Imagenew_from_pixbufmini_pixbufFrameAlignCENTERadd
add_widgetrundestroyaction_save)r   r<   fnunsaved_file_namedisplay_namedialog
discard_id	cancel_idsave_idframeresults              r   confirm_save_modifsz#DrSavingManager.confirm_save_modifs   s    ,,
'
'
)%
^^
"Z}v-N#<xx}R(002<4<<(&  9/CD*(T2)ait4'Q?@<OP,,%%'
))+
%
%dll&:&:&F&F
G%
))399++CII4D4D
E%))EE::<&..w
,,
"
"
$$

r   c                    d}| j                   j                         j                         rt        d      }n3| j                   j	                         j                         rt        d      }|yt        | j                         }| j                   j                  d      j                         }|r%| j                   j	                         j                  }n)| j                   j                  d      j                         }|xs | }|j                  t        d      d|      }|rd}nd}d}|r|j                  t        d	      d
d      }n|r|j                  t        d      d
d      }|j                  t        d      |      }	|j                  |       |j                  t        d             | j                   j                  j                          t        j                         j!                  | j                   j                  j"                        }
t        j$                  t        j&                  j(                  t        j&                  j(                        }|j+                  |
       |j-                  |       |j/                         }|j1                          ||	k(  ry||k(  ry||k(  r9|r| j                   j3                          |r| j                   j5                          yy)zAsk to the user whether or not they want to apply the operation of
		the current tool (curve, shape, transform, selection) before saving.NzrA part of the image is selected: the pixels beneath the selection will be saved with a color you might not expect!z9Modifications from the current tool haven't been applied.Tapply_transformunselectrK   rJ   zApply & savezsuggested-actionzDeselect & savezSave anywayzDo you want to save anyway?rM   F)r   get_selection_toolselection_is_activer0   active_toolhas_ongoing_operationr   lookup_actionget_enabledapply_to_selectionrS   rT   rU   rV   r   rW   rX   rY   rZ   r[   r\   r]   r^   r_   r`   action_apply_transformaction_unselect)r   msgre   	can_applycan_unselectonly_2_itemsrg   
save_styleapply_idrh   r<   ri   rj   s                r   r   z2DrSavingManager._confirm_despite_ongoing_operation   sU    	#	\\$$&::<	
 G 
H3||!779 
F	G3[
4<<(& ll(():;GGI),,**,??<,,,,Z8DDF<  /<0,(T<@)$::(. 1.@$H8 "3 4.@$H8a.
;'CA345,,%%'
))+
%
%dll&:&:&F&F
G%
))399++CII4D4D
E%))EE::<&..w

LL'')LL  "

r   c           	      <   d}t         j                  j                  t        d      | j                  t         j
                  j                  t        d      t        d            }t        |       t        j                  t        j                        }|dk7  r|j                  |       t        t        d      dz         }|j                  |       |j                         }|t         j                  j                   k(  r|j#                         }|j%                          |S )zBOpens an "save" file chooser dialog, and return a GioFile or None.Nu   Save picture as…rL   rK   rE   rF   )r   FileChooserNativenewr0   r   FileChooserActionSAVEr   r   get_user_special_dirUSER_DIRECTORY_PICTURESset_current_folderstrset_current_namer_   ResponseTypeACCEPTget_filer`   )r   r    file_chooser
images_dirdefault_file_nameresponses         r   r   z"DrSavingManager._file_chooser_save   s    
%&&**1-A+B	s,,111V9akK,#L1(()E)EF*4"":.!J-&01 12(!!(((  "5	,r   c                 r    |j                  d      d   }|j                         }|dv rd}|S |t        vrd}|S )zfBuild a short string which will be recognized as a file format by the
		GdkPixbuf.Pixbuf.savev method..rH   )r
   r   r   r
   r   )rQ   lowerALL_SUPPORTED_FORMAT)r   r=   r>   s      r   r"   zDrSavingManager._get_format   sQ     $R(+!!#+**; 
 ..;	r   c                    t        | j                        }|j                  t        d      d      }|r|j                  t        d      d      }nd}|j                  t        d      dd      }|s|j	                  t        d             |r|j	                  t        d             n|j	                  t        d             t        j                  t
        j                  j                  	      }|j                  d
t        d             |j                  dt        d             |j                  dt        d             |j                  dt        d             |s|j                  dt        d             |j                  d
       |j                  |       |j                         }|j                         }	|j                          ||k(  r
t               ||k(  r
t!               |	S )zWarn the user about the replacement of the alpha channel for JPG or
		BMP files, but it may quickly annoy users to see a dialog so it's an
		option. Can be used on PNG files (if 'supports_alpha' is true).rK   Nu
   Save as…ReplaceTz4This file format doesn't support transparent colors.zCYou can save the image as a PNG file, or replace transparency with:zReplace transparency with:)rO   initialzDefault colorwhiteWhiteblackBlack
checkboard
CheckboardnothingNothing)r   r   rS   r0   rT   r   ComboBoxTextr[   r\   appendset_active_idr^   r_   get_active_idr`   r,   r*   )
r   rA   r@   re   rg   
save_as_id
replace_idalpha_comboboxrj   repls
             r   r(   z$DrSavingManager._ask_overwrite_alpha  s    4<<(&(T2)!!!L/48::  9tT:*		QMNO	Q F G H 
Q345##399+;+;<.
 	1_#567,7, ao6	 AiL1y) 	N#::<&		%	%	'$..y		  z	#	%%	+r   c           	      R   |dk(  r|S |j                         }|j                         }|dk(  rt        ddd      }t        ddd      }n|dk(  rs|j                         }t	        |j
                  dz        }	t	        |j                  dz        }
t	        |j                  dz        }t        |	|
|      }t        |	|
|      }n:|dk(  rt        ddd      }t        ddd      }nt        ddd      }t        ddd      }|j                  ||t        j                  j                  dd	||      S )
Nr   r      r   r   U      r      )	get_width
get_heightr	   get_initial_rgbaintredgreenbluecomposite_color_simpler   
InterpTypeTILES)r   r?   rB   r<   widthheightpcolor1pcolor2initial_rgbargbs               r   r)   zDrSavingManager._replace_alpha9  s2   I
=



%&G)#sC87)#sC87i((*<
<c!"1
<#$1
<s"#1 *!Q27)!Q27l")"b"57)#sC87)!Q27)!Q27		&	&uf))//a'
K Kr   N)__name__
__module____qualname____gtype_name__r   r+   rk   r   r   r"   r(   r)    r   r   r   r      s5    #]D D>@.	0dKr   r   c                        e Zd Z fdZ xZS )r,   c                 6    t         |   t        d             y )NrK   superr   r0   r   	__class__s    r   r   zWantToCancelException.__init__X  s    '1X;r   r   r   r   r   __classcell__r   s   @r   r,   r,   W  s       r   r,   c                        e Zd Z fdZ xZS )r*   c                 6    t         |   t        d             y )Nr   r   r   s    r   r   z#WantAnotherFormatException.__init__\  s    '1Y< r   r   r   s   @r   r*   r*   [  s    ! !r   r*   N)gi.repositoryr   r   r   message_dialogr   utilities_filesr   utilities_colorsr	   r   r   r-   r,   r*   r   r   r   <module>r      sC   $ / . + > :J wK wKx	 I  ! !r   