a
    ø£Beï  ã                   @   s�   d Z ddlZz@ddlmZ ddlmZmZ ddlmZm	Z	 ddl
mZ dZW n eyb   dZY n0 erŒdZe ¡ s|eg ƒZG d	d
„ d
e	ƒZdS )úA qt screenshot exporter.é    N)ÚQtCore)ÚQPageLayoutÚ	QPageSize)ÚQWebEngineSettingsÚQWebEngineView)ÚQApplicationTFc                       sH   e Zd ZdZ‡ fdd„Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	‡  Z
S )ÚQtScreenshotr   c                    s   t ƒ  ¡  t| _dS )zInitialize the exporter.N)ÚsuperÚ__init__ÚAPPÚapp©Úself©Ú	__class__© úd/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/nbconvert/exporters/qt_screenshot.pyr      s    
zQtScreenshot.__init__c                    sÀ   |ˆ _ |ˆ _ˆ  t |¡¡ ˆ j ˆ j¡ ˆ  tj	j
¡ ˆ  ¡  ¡  tjd¡ dˆ _| d¡r„ˆ jˆ _‡ fdd„}ˆ  ¡ j |¡ n&| d¡r˜ˆ jˆ _nd|› �}t|ƒ‚ˆ  ¡  ˆ j ¡  dS )	zCapture the screenshot.Fó    z.pdfc                     s   ˆ j  ¡  ˆ  ¡  dS )zCleanup the app.N)r   ÚquitÚget_data)Úargsr   r   r   Úcleanup,   s    
z%QtScreenshot.capture.<locals>.cleanupz.pngz%Export file extension not supported: N)Úoutput_fileÚpaginateÚloadr   ZQUrlZloadFinishedÚconnectÚ	on_loadedZsetAttributeZQtZWA_DontShowOnScreenÚpageÚsettingsr   ZShowScrollBarsÚdataÚendswithÚ
export_pdfÚexportZpdfPrintingFinishedÚ
export_pngÚRuntimeErrorÚshowr   Úexec)r   Úurlr   r   r   Úmsgr   r   r   Úcapture   s"    



zQtScreenshot.capturec                 C   s2   |   ¡  ¡  ¡ | _|  | j¡ tj d| j¡ dS )zHandle app load.iè  N)	r   ZcontentsSizeZtoSizeÚsizeÚresizer   ZQTimerZ
singleShotr#   r   r   r   r   r   :   s    zQtScreenshot.on_loadedc                 C   sz   | j r$ttjƒ}t|tjt ¡ ƒ}n>d}tt | j 	¡ | | j 
¡ | ¡tjƒ}t|tjt ¡ ƒ}|  ¡ j| j|d� dS )zExport to pdf.g      è?)Z
pageLayoutN)r   r   ZA4r   ZPortraitr   Z	QMarginsFZQSizeFr+   ÚwidthÚheightZPointr   Z
printToPdfr   )r   Z	page_sizeZpage_layoutÚfactorr   r   r   r"   A   s    
þzQtScreenshot.export_pdfc                 C   s(   |   ¡  | jd¡ | j ¡  |  ¡  dS )zExport to png.ZPNGN)ZgrabÚsaver   r   r   r   r   r   r   r   r$   P   s    
zQtScreenshot.export_pngc                 C   sT   t j | j¡rPt| jdƒ�}| ¡ | _W d  ƒ n1 s:0    Y  t  | j¡ dS )zGet output data.ÚrbN)ÚosÚpathÚexistsr   ÚopenÚreadr    Úunlink)r   Úfr   r   r   r   V   s    (zQtScreenshot.get_data)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r*   r   r"   r$   r   Ú__classcell__r   r   r   r   r	      s   r	   )r<   r2   ZPyQt5r   ZPyQt5.QtGuir   r   ZPyQt5.QtWebEngineWidgetsr   r   ZPyQt5.QtWidgetsr   ZQT_INSTALLEDÚModuleNotFoundErrorr   Úinstancer	   r   r   r   r   Ú<module>   s   
