U
    td6                     @   s   d Z ddlZddlmZ ddlmZ ddlmZ ddlm	Z	 ddl
mZmZ ddlmZ dd	lmZ d
Ze	 ZG dd dZdS )at  
Drawing and plotting routines for IGraph.

IGraph has two plotting backends at the moment: Cairo and Matplotlib.

The Cairo backend is dependent on the C{pycairo} or C{cairocffi} libraries that
provide Python bindings to the popular U{Cairo library<http://www.cairographics.org>}.
This means that if you don't have U{pycairo<http://www.cairographics.org/pycairo>}
or U{cairocffi<http://cairocffi.readthedocs.io>} installed, you won't be able
to use the Cairo plotting backend. Whenever the documentation refers to the
C{pycairo} library, you can safely replace it with C{cairocffi} as the two are
API-compatible.

The Matplotlib backend uses the U{Matplotlib library<https://matplotlib.org>}.
You will need to install it from PyPI if you want to use the Matplotlib
plotting backend.

If you do not want to (or cannot) install any of the dependencies outlined
above, you can still save the graph to an SVG file and view it from
U{Mozilla Firefox<http://www.mozilla.org/firefox>} (free) or edit it in
U{Inkscape<http://www.inkscape.org>} (free), U{Skencil<http://www.skencil.org>}
(formerly known as Sketch, also free) or Adobe Illustrator.
    N)BytesIO)warn)Configuration)
find_cairo)Palettepalettes)BoundingBox)named_temporary_file)	CairoPlotc                   @   s   e Zd ZdZdddZdddZedd	 Zejd
d	 Zd ddZ	dd Z
d!ddZd"ddZdd Zedd Zedd Zedd Zedd ZdS )#r
   a  Class representing an arbitrary plot that uses the Cairo plotting
    backend.

    Objects that you can plot include graphs, matrices, palettes, clusterings,
    covers, and dendrograms.

    In Cairo, every plot has an associated surface object. The surface is an
    instance of C{cairo.Surface}, a member of the C{pycairo} library. The
    surface itself provides a unified API to various plotting targets like SVG
    files, X11 windows, PostScript files, PNG files and so on. C{igraph} does
    not usually know on which surface it is plotting at each point in time,
    since C{pycairo} takes care of the actual drawing. Everything that's
    supported by C{pycairo} should be supported by this class as well.

    Current Cairo surfaces include:

      - C{cairo.GlitzSurface} -- OpenGL accelerated surface for the X11
        Window System.

      - C{cairo.ImageSurface} -- memory buffer surface. Can be written to a
        C{PNG} image file.

      - C{cairo.PDFSurface} -- PDF document surface.

      - C{cairo.PSSurface} -- PostScript document surface.

      - C{cairo.SVGSurface} -- SVG (Scalable Vector Graphics) document surface.

      - C{cairo.Win32Surface} -- Microsoft Windows screen rendering.

      - C{cairo.XlibSurface} -- X11 Window System screen rendering.

    If you create a C{Plot} object with a string given as the target surface,
    the string will be treated as a filename, and its extension will decide
    which surface class will be used. Please note that not all surfaces might
    be available, depending on your C{pycairo} installation.

    A C{Plot} has an assigned default palette (see L{igraph.drawing.colors.Palette})
    which is used for plotting objects.

    A C{Plot} object also has a list of objects to be plotted with their
    respective bounding boxes, palettes and opacities. Palettes assigned to an
    object override the default palette of the plot. Objects can be added by the
    L{Plot.add} method and removed by the L{Plot.remove} method.
    Nc                 C   s  d| _ d| _|dkr"tdd| _n&t|ts6t|trBt|| _n|| _|dkr`t }|d }t|t	srt
| }|| _|dkrd| _ttjt| jjt| jj| _nt|tjr|| _n|| _ tj|\}}| }|dkrt|| jj| jj| _n|dks|dkr,t|| jj| jj| _n^|d	krZttjt| jjt| jj| _n0|d
kr~t|| jj| jj| _ntd| t| j| _g | _d| _|dkrd}|| _ dS )a  Creates a new plot.

        @param target: the target surface to write to. It can be one of the
          following types:

            - C{None} -- a Cairo surface will be created and the object will be
              plotted there.

            - C{cairo.Surface} -- the given Cairo surface will be used.

            - C{string} -- a file with the given name will be created and an
              appropriate Cairo surface will be attached to it.

        @param bbox: the bounding box of the surface. It is interpreted
          differently with different surfaces: PDF and PS surfaces will treat it
          as points (1 point = 1/72 inch). Image surfaces will treat it as
          pixels. SVG surfaces will treat it as an abstract unit, but it will
          mostly be interpreted as pixels when viewing the SVG file in Firefox.

        @param palette: the palette primarily used on the plot if the
          added objects do not specify a private palette. Must be either
          an L{igraph.drawing.colors.Palette} object or a string referring
          to a valid key of C{igraph.drawing.colors.palettes} (see module
          L{igraph.drawing.colors}) or C{None}. In the latter case, the default
          palette given by the configuration key C{plotting.palette} is used.

        @param background: the background color. If C{None}, the background
          will be transparent. You can use any color specification here that is
          understood by L{igraph.drawing.colors.color_name_to_rgba}.
        NFiX  zplotting.paletteTz.pdfz.psz.eps.pngz.svgz%image format not handled by Cairo: %swhite)!	_filename_need_tmpfiler   bbox
isinstancetuplelistr   instancer   r   _palettecairoImageSurfaceZFORMAT_ARGB32intwidthheight_surfaceZSurfaceospathsplitextlowerZ
PDFSurfaceZ	PSSurface
SVGSurface
ValueErrorContext_ctx_objects	_is_dirty
background)selftargetr   paletter%   config_ext r,   R/home/sam/Atlas/atlas_env/lib/python3.8/site-packages/igraph/drawing/cairo/plot.py__init__[   sp    &
 
 
    
 
 

  
zCairoPlot.__init__      ?c                 O   sd   |dk s|dkrt d|dkr&| j}|dk	r@t|ts@t|}| j||||||f |   dS )a?  Adds an object to the plot.

        Arguments not specified here are stored and passed to the object's
        plotting function when necessary. Since you are most likely interested
        in the arguments acceptable by graphs, see L{Graph.__plot__} for more
        details.

        @param obj: the object to be added
        @param bbox: the bounding box of the object. If C{None}, the object
          will fill the entire area of the plot.
        @param palette: the color palette used for drawing the object. If the
          object tries to get a color assigned to a positive integer, it
          will use this palette. If C{None}, defaults to the global palette
          of the plot.
        @param opacity: the opacity of the object being plotted, in the range
          0.0-1.0

        @see: Graph.__plot__
        g        r/   z#opacity must be between 0.0 and 1.0N)r    r   r   r   r#   append
mark_dirty)r&   objr   r(   opacityargskwdsr,   r,   r-   add   s    zCairoPlot.addc                 C   s   | j S )zbReturns the background color of the plot. C{None} means a
        transparent background.
        )_backgroundr&   r,   r,   r-   r%      s    zCairoPlot.backgroundc                 C   s"   |dkrd| _ n| j|| _ dS )a	  Sets the background color of the plot. C{None} means a
        transparent background. You can use any color specification here
        that is understood by the C{get} method of the current palette
        or by L{igraph.drawing.colors.color_name_to_rgb}.
        N)r7   r   get)r&   colorr,   r,   r-   r%      s       c                 C   sv   t t| jD ]b}| j| dd \}}||kr|dks@||kr|d8 }|dkrg | j||d < |    dS qdS )a  Removes an object from the plot.

        If the object has been added multiple times and no bounding box
        was specified, it removes the instance which occurs M{idx}th
        in the list of identical instances of the object.

        @param obj: the object to be removed
        @param bbox: optional bounding box specification for the object.
          If given, only objects with exactly this bounding box will be
          considered.
        @param idx: if multiple objects match the specification given by
          M{obj} and M{bbox}, only the M{idx}th occurrence will be removed.
        @return: C{True} if the object has been removed successfully,
          C{False} if the object was not on the plot at all or M{idx}
          was larger than the count of occurrences
        r      Nr;   TF)rangelenr#   r1   )r&   r2   r   idxiZcurrent_objZcurrent_bboxr,   r,   r-   remove   s    zCairoPlot.removec                 C   s
   d| _ dS )z+Marks the plot as dirty (should be redrawn)TN)r$   r8   r,   r,   r-   r1     s    zCairoPlot.mark_dirtyc           
      C   s   |p| j }| jdk	r@|j| j  |dd| jj| jj |  | jD ]\}}}}}}|dkrlt	|d| j
}t	|dd}	|	dkrtd|f  qF|dk r|  n|  |	d|f|||d| |dk r|  || qF|  qFd	| _dS )
zRedraws the plotNr   Z_default_paletteZ__plot__z%s does not support plottingr/   r   )r   r(   F)r"   r7   Zset_source_rgbaZ	rectangler   r   r   fillr#   getattrr   r   Z
push_groupsaveZpop_group_to_sourceZpaint_with_alpharestorer$   )
r&   contextctxr2   r   r(   r3   r4   r5   Zplotterr,   r,   r-   redraw  s(    



zCairoPlot.redrawc              	   C   s   | j r|   t| jtjr|dkrX| jrXtddd}| j| W 5 Q R  dS Q R X |p`| j	}|dkrrt
d| jt|S |dk	rtd | j  | j  dS )zSaves the plot.

        @param fname: the filename to save to. It is ignored if the surface
          of the plot is not an C{ImageSurface}.
        NZigraphr   )prefixsuffixz4no file name is known for the surface and none givenz8filename is ignored for surfaces other than ImageSurface)r$   rH   r   r   r   r   r   r	   Zwrite_to_pngr   r    strr   r"   	show_pagefinish)r&   fnamer,   r,   r-   rD   $  s    

zCairoPlot.savec                 C   sZ   t  }t|| jj| jj}t|}| | |  |	  |
 d}|ddifS )zReturns an SVG representation of this plot as a string.

        This method is used by IPython to display this plot inline.
        zutf-8isolatedT)r   r   r   r   r   r   r!   rH   rL   rM   getvaluedecode)r&   iosurfacerF   resultr,   r,   r-   
_repr_svg_@  s    

zCairoPlot._repr_svg_c                 C   s
   t | jS )zPReturns the bounding box of the Cairo surface as a
        L{BoundingBox} object)r   r   r8   r,   r,   r-   bounding_boxS  s    zCairoPlot.bounding_boxc                 C   s   | j jS )zJReturns the height of the Cairo surface on which the plot
        is drawn)r   r   r8   r,   r,   r-   r   Y  s    zCairoPlot.heightc                 C   s   | j S )z4Returns the Cairo surface on which the plot is drawn)r   r8   r,   r,   r-   rS   _  s    zCairoPlot.surfacec                 C   s   | j jS )zIReturns the width of the Cairo surface on which the plot
        is drawn)r   r   r8   r,   r,   r-   r   d  s    zCairoPlot.width)NNNN)NNr/   )Nr;   )N)N)__name__
__module____qualname____doc__r.   r6   propertyr%   setterrA   r1   rH   rD   rU   rV   r   rS   r   r,   r,   r,   r-   r
   ,   s0   0    
^








r
   )rZ   r   rR   r   warningsr   Zigraph.configurationr   Zigraph.drawing.cairo.utilsr   Zigraph.drawing.colorsr   r   Zigraph.drawing.utilsr   Zigraph.utilsr	   __all__r   r
   r,   r,   r,   r-   <module>   s   