U
    td(                     @   s@   d Z ddlmZ ddlmZ ddlmZ dZG dd deZdS )	zGThis module provides implementation for a Cairo-specific matrix drawer.    )islice)pi)AbstractCairoDrawer)CairoMatrixDrawerc                       s(   e Zd ZdZ fddZdd Z  ZS )r   z)Default Cairo drawer object for matrices.c                    s   t  j|dd dS )zConstructs the vertex drawer and associates it to the given
        palette.

        @param context: the context on which we will draw
        N)bbox)super__init__)selfcontext	__class__ T/home/sam/Atlas/atlas_env/lib/python3.8/site-packages/igraph/drawing/cairo/matrix.pyr      s    zCairoMatrixDrawer.__init__c           &         s  | j }|j}|dd }| _|dd}|dkr:td|dkrJtdt|dd}t|dd}|d	d
}	|d}
|d|
}|d}|dt|	dkrtd|	dkrd}	|
dkr|dk	r|}
|
dk	rdd t|
|j	D }
t
|
|j	k r|
dg|j	t
|
   |dk	rfdd t||jD }t
||jk rf|dg|jt
|   |dkrtd}|dkr|}t|tr|t}|dk	rt||std|dk	r|j|jkrtd|j |
dk	s|dk	rV|j  dd }|
dk	r&t fdd|
D | }nd}|dk	rPt fdd|D | }nd}nd\}}d}t|j| }t|j| }||jd   }||jd  }|d!drt||t|| }}||jd   ||jd   }}|j|j| | d"  }|j|j| | d"  }||7 }||7 }|	dkrV| |  }}|}t
|d  t||  }nd#\}}|d$| k sz|d$| k r~d%}|dkr|| n
|d  |d%d%d% |
dk	r|| }}|
D ]N}||\}}}}}}||| | ||| d"   || ||7 }q|dk	r|  ||| |t d&  d'\}}|D ]J}||\}}}}}}||| ||| d"   || ||7 }qN|   || }}|	dkrd(d) } n|j!} |D ]}!|!D ]}"|"dkr||7 }q|	d
kr|"r|d%d%d% n|ddd n8|	dkrTt"|"| | }#|#dk rDd}#|j#||#  |$|||| |dkr|   |d*d*d* |%  n|   |%  ||7 }q|||  }}q|dk	r|| }}|d%d%d% |j&D ]}!t'd+rfd,d|!D }nfd-d|!D }|D ]R}"||"d$d. \}$}%||||% d"  |||$ d"   ||" ||7 }q|||  }}q|dkr|| |d%d%d% |$||||jd   ||jd   |%  dS )/a  Draws the given Matrix in a Cairo context.

        @param matrix: the igraph.Matrix to plot.

        It accepts the following keyword arguments:

          - C{bbox}:    the bounding box within which we will draw.
            Can be anything accepted by the constructor of L{BoundingBox}
            (i.e., a 2-tuple, a 4-tuple or a L{BoundingBox} object).

          - C{palette}: the palette that can be used to map integer color
            indices to colors when drawing vertices

          - C{style}: the style of the plot. C{boolean} is useful for plotting
            matrices with boolean (C{True}/C{False} or 0/1) values: C{False}
            will be shown with a white box and C{True} with a black box.
            C{palette} uses the given palette to represent numbers by colors,
            the minimum will be assigned to palette color index 0 and the maximum
            will be assigned to the length of the palette. C{None} draws transparent
            cell backgrounds only. The default style is C{boolean} (but it may
            change in the future). C{None} values in the matrix are treated
            specially in both cases: nothing is drawn in the cell corresponding
            to C{None}.

          - C{square}: whether the cells of the matrix should be square or not.
            Default is C{True}.

          - C{grid_width}: line width of the grid shown on the matrix. If zero or
            negative, the grid is turned off. The grid is also turned off if the size
            of a cell is less than three times the given line width. Default is C{1}.
            Fractional widths are also allowed.

          - C{border_width}: line width of the border drawn around the matrix.
            If zero or negative, the border is turned off. Default is C{1}.

          - C{row_names}: the names of the rows

          - C{col_names}: the names of the columns.

          - C{values}: values to be displayed in the cells. If C{None} or
            C{False}, no values are displayed. If C{True}, the values come
            from the matrix being plotted. If it is another matrix, the
            values of that matrix are shown in the cells. In this case,
            the shape of the value matrix must match the shape of the
            matrix being plotted.

          - C{value_format}: a format string or a callable that specifies how
            the values should be plotted. If it is a callable, it must be a
            function that expects a single value and returns a string.
            Example: C{"%#.2f"} for floating-point numbers with always exactly
            two digits after the decimal point. See the Python documentation of
            the C{%} operator for details on the format string. If the format
            string is not given, it defaults to the C{str} function.

        If only the row names or the column names are given and the matrix
        is square-shaped, the same names are used for both column and row
        names.
        r   Npalettez bbox is required for Cairo plotsz#palette is required for Cairo plots
grid_widthg      ?border_widthstyleboolean	row_names	col_namesvaluesvalue_format)r   r   noneNzinvalid styler   c                 S   s   g | ]}t |qS r   str.0namer   r   r   
<listcomp>k   s     z*CairoMatrixDrawer.draw.<locals>.<listcomp> c                 S   s   g | ]}t |qS r   r   r   r   r   r   r   o   s     FTz,values must be None, False, True or a matrixz"values must be a matrix of size %s    c                    s   g | ]} |d  qS r!   r   r   ster   r   r      s     r   c                    s   g | ]} |d  qS r"   r   r#   r%   r   r   r      s     )r   r      Zsquareg       @)r   r'                 )r)   r)   c                   S   s   d S )Nr   r   r   r   r   <lambda>       z(CairoMatrixDrawer.draw.<locals>.<lambda>g      ?__call__c                    s   g | ]} |qS r   r   r   itemr   r   r   r      s     c                    s   g | ]} | qS r   r   r.   r0   r   r   r      s        )(r
   r   popr   
ValueErrorfloatgetr   r   Z_nrowlenextendZ_ncol
isinstancelist	TypeErrorshapeZtext_extentsmaxwidthheightminlefttopZset_line_widthZset_source_rgbmove_toZ	show_textsave	translaterotater   restoreZfill_preserveintZset_source_rgbaZ	rectanglestrokedatahasattr)&r	   matrixkwdsr
   ZMatrixr   r   r   r   r   r   r   r   Zspace_widthZmax_row_name_widthZmax_col_name_widthtotal_widthZtotal_heightZdxZdyoxZoymimaZcolor_offsetZcolor_ratioxyheading_hZxafillrowr/   ZcidxthZtwr   )r&   r   r   draw   s   ;












 














$


$zCairoMatrixDrawer.draw)__name__
__module____qualname____doc__r   rY   __classcell__r   r   r   r   r      s   r   N)	r]   	itertoolsr   mathr   Zigraph.drawing.cairo.baser   __all__r   r   r   r   r   <module>   s
   