B
    Zeo[¬	  ã               @   sD   d Z ddlmZmZmZ ddl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)Úunicode_typeNc             C   s‚   t | tƒr|  d¡} t |d¡}| tjj¡ | d¡ | d¡ | 	¡ r~| 
¡ d }t|dƒ}z| | ¡ W d| ¡  X |S 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.
    zutf-8zSave SVG DocumentZsvgzSVG document (*.svg)r   ÚwbN)Ú
isinstancer   Úencoder   ZQFileDialogZsetAcceptModeZ
AcceptSaveZsetDefaultSuffixZsetNameFilterZexec_ZselectedFilesÚopenÚwriteÚclose)ÚstringÚparentZdialogÚfilenameÚf© r   ú,lib/python3.7/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.
    zutf-8zimage/svg+xmlN)
r   r   r   r   Z	QMimeDataZsetDatar   ZQApplicationZ	clipboardZsetMimeData)r   Z	mime_datar   r   r   Úsvg_to_clipboard+   s
    

r   c             C   sv   t | tƒr|  d¡} t t | ¡¡}| ¡ s4tdƒ‚|dkrD| 	¡ }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.
    zutf-8zInvalid SVG data.Nr   )r   r   r   r   ZQSvgRendererr   Z
QByteArrayZisValidÚ
ValueErrorZdefaultSizer   ZQImageZFormat_ARGB32ZfillZQPainterZrender)r   ÚsizeZrendererZimageZpainterr   r   r   Úsvg_to_image:   s    




r   )N)N)
Ú__doc__Zqtconsole.qtr   r   r   Zipython_genutils.py3compatr   r   r   r   r   r   r   r   Ú<module>   s
   
!