o
    e                     @   sX   d Z ddlZddlmZmZmZmZmZmZm	Z	 ddl
Z
ddlmZ G dd deZdS )z!Main frame with grouped controls.    N)TkFrameLabelEntryButton
filedialog
messagebox)	Converterc                       sD   e Zd Zddef fddZdd Zdd Zd	d
 Zdd Z  Z	S )	MainFrameNparentc                    sF   t  | || _| jdd | jddd |   t | _d| _dS )zVMain frame with grouped controls. This should be attached to a parent ``Tk`` instance.grey)bgd   )ipadxipadyN)	super__init__r   configgridsetup_uiset	pdf_pathsdocx_folder)selfr   	__class__ FD:\Projects\ConvertPro\env\Lib\site-packages\pdf2docx/gui/MainFrame.pyr   
   s   
zMainFrame.__init__c              	   C   sN  t | ddddd| _| jjddddd	d
 t| ddd| _| jjdddddd t| ddd| _| jjddddddd t| dddd| jd| _| jjdddd t| dddd| j	d| _
| j
jdddd t| ddddd| jd| _| jjdddddd t | dd d!| _| jjddddd" t| d#d dd$d% d&| _| jjdddd' d(S ))zLayout of the user interface.zPDF to Docxz	Impact 40whitez#1E90FF)textfontr   fgr      x   2   )rowcolumn
columnspanpadxpady   7   )borderwidth   w   
   )r%   r&   stickyr   r(   K   )r%   r&   r2   r   r(   r)   zSelect PDF filesblack   )r   r!   r   r,   command)r%   r&   r2   zSelect new files folderZConvertz	impact 20)r   r   r!   r    r,   r6   )r%   r&   r'   r   r   zN*This converter can only convert text based pdf 
 cant convert image based pdfr   )r   r   )r%   r&   r'   r)   zhow to check my file?c                   S   s
   t dS )Nzkhttps://support.policystat.com/hc/en-us/articles/207993346-How-can-I-tell-if-my-PDFs-are-text-based-or-not-)
webbrowserZopen_newr   r   r   r   <lambda>G   s   
 z$MainFrame.setup_ui.<locals>.<lambda>)r   r   r!   r6   )r%   r&   r'   N)r   Zprogram_use_labelr   r   file_path_pdf_entryfile_path_docx_entryr   _callback_pdf_file_locationZselect_pdf_file_callback_docx_folder_locationZselect_new_file_folder_callback_convertZconverter_buttoncheck_labelZfile_check_buttonr   r   r   r   r      sL   zMainFrame.setup_uic                 C   sb   t jdgd}t | _|D ]}| j| qddd | jD }| jdd | jd| dS )	zTOpens file explorer and let you select choose the pdf file that you want to convert.)zPDF filez*.pdf)	filetypes;c                 S   s    g | ]}d t j| d qS )")ospathbasename.0rD   r   r   r   
<listcomp>T   s     z9MainFrame._callback_pdf_file_location.<locals>.<listcomp>r   endN)	r   Zaskopenfilenamesr   r   addjoinr9   deleteinsert)r   
file_pathsrD   namesr   r   r   r;   L   s   z%MainFrame._callback_pdf_file_locationc                 C   s,   t  | _| jdd | jd| j dS )zgOpens file explorer and let you choose the folder, that all the converted file or files going to saved.r   rI   N)r   Zaskdirectoryr   r:   rL   rM   r?   r   r   r   r<   Y   s   
z(MainFrame._callback_docx_folder_locationc                 C   sf  | j s| jstjddd dS | j stjddd dS | js'tjddd dS g }| j D ] }tj|}tj|\}}tj| j| d	}|	| q,t
d
d |D r_tjddds_dS d\}}t| j |D ]9\}}t|}	z,z|	| W n ty }
 zt|
 |d7 }W Y d}
~
n	d}
~
ww |d7 }W |	  qi|	  w tjdd| d| dd dS )z(Starts the convert of the file or files.z Neither files or folder selectedzQSelect PDF file or files for convert and Select a folder for the converted files!)titlemessageNzNot files for convert selectedz)Select PDF file or PDF files for convert!zNot files folder selectedz(Select a folder for the converted files!z.docxc                 S   s   g | ]}t j|qS r   )rC   rD   existsrF   r   r   r   rH   ~   s    z/MainFrame._callback_convert.<locals>.<listcomp>zExisted target filezkDocx files with same target name are found under selected folder. Do you want to continue and replace them?)r   r   r.   zConvert Done!zSuccessful (z), Failed (z).)r   r   r   showwarningrC   rD   rE   splitextrK   appendanyZaskokcancelzipr	   convert	ExceptionprintcloseZshowinfo)r   Z
docx_pathsZpdf_path	base_namenameextZ	docx_pathZnum_succZnum_failZcver   r   r   r=   `   s\   

zMainFrame._callback_convert)N)
__name__
__module____qualname__r   r   r   r;   r<   r=   __classcell__r   r   r   r   r
   	   s    5r
   )__doc__rC   tkinterr   r   r   r   r   r   r   r7   Zpdf2docxr	   r
   r   r   r   r   <module>   s    $