ó
_y]c           @   sW   d  Z  d d l m Z m Z m Z d d l m Z d d  Z d   Z	 d d  Z
 d S(   sA    Defines utility functions for working with SVG documents in Qt.
i’’’’(   t   QtCoret   QtGuit   QtSvg(   t   unicode_typec         C   s²   t  |  t  r! |  j d  }  n  t j | d  } | j t j j  | j d  | j d  | j	   r® | j
   d } t | d  } z | j |   Wd | j   X| Sd S(   sj   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.
    s   utf-8s   Save SVG Documentt   svgs   SVG document (*.svg)i    t   wbN(   t
   isinstanceR   t   encodeR   t   QFileDialogt   setAcceptModet
   AcceptSavet   setDefaultSuffixt   setNameFiltert   exec_t   selectedFilest   opent   writet   closet   None(   t   stringt   parentt   dialogt   filenamet   f(    (    s,   lib/python2.7/site-packages/qtconsole/svg.pyt   save_svg
   s    c         C   sW   t  |  t  r! |  j d  }  n  t j   } | j d |   t j j   j	 |  d S(   s    Copy a SVG document to the clipboard.

    Parameters
    ----------
    string : basestring
        A Python string containing a SVG document.
    s   utf-8s   image/svg+xmlN(
   R   R   R   R    t	   QMimeDatat   setDataR   t   QApplicationt	   clipboardt   setMimeData(   R   t	   mime_data(    (    s,   lib/python2.7/site-packages/qtconsole/svg.pyt   svg_to_clipboard+   s
    c         C   s“   t  |  t  r! |  j d  }  n  t j t j |    } | j   sT t d   n  | d k ro | j
   } n  t j | t j j  } | j d  t j |  } | j |  | S(   sĄ   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.
    s   utf-8s   Invalid SVG data.i    N(   R   R   R   R   t   QSvgRendererR    t
   QByteArrayt   isValidt
   ValueErrorR   t   defaultSizeR   t   QImaget   Format_ARGB32t   fillt   QPaintert   render(   R   t   sizet   renderert   imaget   painter(    (    s,   lib/python2.7/site-packages/qtconsole/svg.pyt   svg_to_image:   s    N(   t   __doc__t   qtconsole.qtR    R   R   t   ipython_genutils.py3compatR   R   R   R   R.   (    (    (    s,   lib/python2.7/site-packages/qtconsole/svg.pyt   <module>   s
   !	