o
    º¼tfZ	  ã                   @   s<   d Z ddlmZmZmZmZ d
dd„Zdd„ Zd
dd	„ZdS )zA Defines utility functions for working with SVG documents in Qt.
é    )ÚQtCoreÚQtGuiÚQtSvgÚ	QtWidgetsNc                 C   sˆ   t | tƒr
|  d¡} t |d¡}| tjj¡ | d¡ | d¡ | 	¡ rB| 
¡ d }t|dƒ}z| | ¡ W | ¡  |S | ¡  w dS )aj   Prompts the user to save an SVG document to disk.

    Parameters
    ----------
    string : basestring
        A Python string containing a SVG document.

    parent : QWidget, optional
        The parent to use for the file dialog.

    Returns
    -------
    The name of the file to which the document was saved, or None if the save
    was cancelled.
    úutf-8zSave SVG DocumentÚsvgzSVG document (*.svg)r   ÚwbN)Ú
isinstanceÚstrÚencoder   ZQFileDialogZsetAcceptModeZ
AcceptSaveZsetDefaultSuffixZsetNameFilterÚexec_ZselectedFilesÚopenÚwriteÚclose)ÚstringÚparentZdialogÚfilenameÚf© r   úV/var/www/html/software/conda/envs/catlas/lib/python3.10/site-packages/qtconsole/svg.pyÚsave_svg   s   





ÿr   c                 C   s<   t | tƒr
|  d¡} t ¡ }| d| ¡ tj ¡  	|¡ dS )z• Copy a SVG document to the clipboard.

    Parameters
    ----------
    string : basestring
        A Python string containing a SVG document.
    r   zimage/svg+xmlN)
r	   r
   r   r   Z	QMimeDataZsetDatar   ZQApplicationÚ	clipboardZsetMimeData)r   Z	mime_datar   r   r   Úsvg_to_clipboard)   s
   

r   c                 C   sv   t | tƒr
|  d¡} t t | ¡¡}| ¡ stdƒ‚|du r"| 	¡ }t
 |t
jj¡}| d¡ t
 |¡}| |¡ |S )aÀ   Convert a SVG document to a QImage.

    Parameters
    ----------
    string : basestring
        A Python string containing a SVG document.

    size : QSize, optional
        The size of the image that is produced. If not specified, the SVG
        document's default size is used.
    
    Raises
    ------
    ValueError
        If an invalid SVG string is provided.

    Returns
    -------
    A QImage of format QImage.Format_ARGB32.
    r   zInvalid SVG data.Nr   )r	   r
   r   r   ZQSvgRendererr   Z
QByteArrayZisValidÚ
ValueErrorZdefaultSizer   ZQImageZFormat_ARGB32ÚfillZQPainterÚrender)r   ÚsizeÚrendererÚimageZpainterr   r   r   Úsvg_to_image8   s   




r   )N)	Ú__doc__Zqtpyr   r   r   r   r   r   r   r   r   r   r   Ú<module>   s
    
!