o
    iR\                     @   s"  d Z ddlZddlZddlZddlZeeZ	dd Z
G dd dZd7dd	Zd7d
dZd7ddZd7ddZd8ddZd8ddZd9ddZd9ddZd:ddZd;ddZd;ddZd;d d!Zd;d"d#Zd;d$d%Zd;d&d'Zd;d(d)Ze Zd*d+ Zd,d- Zd.d/ Z d0d1 Z!d2d3 Z"d<d5d6Z#dS )=aX  

Conventions:

"constrain_x" means to constrain the variable with either
another kiwisolver variable, or a float.  i.e. `constrain_width(0.2)`
will set a constraint that the width has to be 0.2 and this constraint is
permanent - i.e. it will not be removed if it becomes obsolete.

"edit_x" means to set x to a value (just a float), and that this value can
change.  So `edit_width(0.2)` will set width to be 0.2, but `edit_width(0.3)`
will allow it to change to 0.3 later.  Note that these values are still just
"suggestions" in `kiwisolver` parlance, and could be over-ridden by
other constrains.

    Nc                 C   sZ   | j r| j }|S | j}|rt|dr| }|S td ddlm} || }| }|S )Nget_rendererz1constrained_layout : falling back to Agg rendererr   )FigureCanvasAgg)_cachedRenderercanvashasattrr   _loginfomatplotlib.backends.backend_aggr   )figrendererr   r    r   P/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/matplotlib/_layoutbox.pyr      s   	
r   c                   @   s^  e Zd ZdZ				dPddZd	d
 Zdd Zdd Zdd Zdd Z	dd Z
dd Zdd ZdQddZdQddZdQddZd d! ZdQd"d#Zd$d% ZdQd&d'Zd(d) ZdQd*d+Zd,d- Zd.d/ Zd0d1 ZdQd2d3ZdQd4d5ZdQd6d7ZdQd8d9ZdQd:d;ZdQd<d=ZdQd>d?ZdQd@dAZ dQdBdCZ!dQdDdEZ"dFdG Z#dHdI Z$dJdK Z%	dRdLdMZ&dNdO Z'dS )S	LayoutBoxzD
    Basic rectangle representation using kiwi solver variables
    N Fr   r      r   c                 C   s  t j}|| _|| _| jd }|d u rt  | _d| _n|j| _d | _||  || _|| _	|	| _
||d | _||d | _||d | _||d | _||d | _||d | _||d	 | _||d
 | _||d | _||d | _||d | _||d | _||d | _||d | _||d | _||d | _||d | _||d | _||d | _||d | _|\}}|\}}|| _|| _ | !  g | _"d | _#| j	r| $  |
| _%|| _&d S )N_r   topbottomleftrightwidthheighth_centerv_center	min_width
min_height
pref_widthpref_heightleft_marginright_marginbottom_margin
top_marginleft_margin_minright_margin_minbottom_margin_mintop_margin_min)'kiwiVariableparentnameSolversolverconstrained_layout_called	add_childartistpossubplotr   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   tightheight
tightwidthadd_constraintschildrensubplotspecconstrain_marginsh_padw_pad)selfr*   r+   r4   r3   r0   
lower_leftupper_rightr1   r2   r9   r:   r)   snr   r   r   r   r   r   r   __init__3   sX   



zLayoutBox.__init__c                 C   s  | j }|| js|| jd || jd | j| j| jj k}| j |dB  | j| jk}| j |dB  || j	sK|| j	d || j	d | j
| jj| j k}| j |dB  | j
| j	k}| j |dB  || js|| jd || jd | j| j| jj k}| j |dB  | j| jk}| j |dB  || js|| jd || jd | j| jj| j k}| j |dB  | j| jk}| j |dB  dS )a  
        Only do this for pos.  This sets a variable distance
        margin between the position of the axes and the outer edge of
        the axes.

        Margins are variable because they change with the figure size.

        Margin minimums are set to make room for axes decorations.  However,
        the margins can be larger if we are mathicng the position size to
        other axes.
        strongg-C6?requiredN)r-   hasEditVariabler$   addEditVariablesuggestValuer    r   r*   addConstraintr%   r!   r   r&   r"   r   r'   r#   r   )r;   solcr   r   r   r8   q   s:   zLayoutBox.constrain_marginsc                 C   s   |  j |g7  _ d S N)r6   r;   childr   r   r   r/         zLayoutBox.add_childc                 C   s2   z	| j | W d S  ty   td Y d S w )Nz3Tried to remove child that doesn't belong to parent)r6   remove
ValueErrorr   r   rI   r   r   r   remove_child   s
   zLayoutBox.remove_childc                 C   sN   | j }| j| jfD ]}||d ||d q	|   | jr%|   d S d S )Ng    eA        )r-   r   r   rC   rD   hard_constraintsr*   parent_constrain)r;   rF   ir   r   r   r5      s   zLayoutBox.add_constraintsc                 C   sP   | j }| j|jk| j|jk| j|jk| j|jkg}|D ]
}| j|dB  qd S )NrA   )r*   r   r   r   r   r-   rE   )r;   r*   hcrG   r   r   r   rQ      s   



zLayoutBox.parent_constrainc                 C   s~   | j | j| j k| j| j| j k| j| j| j d k| j| j| j d k| j | jk| j| j	kg}|D ]
}| j
|dB  q2d S )Ng      ?rA   )r   r   r   r   r   r   r   r   r   r   r-   rE   )r;   rS   rG   r   r   r   rP      s   

zLayoutBox.hard_constraintsc                 C   s   | j }| jr	d}nd}| j|k}|D ]	}||dB  q| jr"d}nd}| j|k}|D ]	}||dB  q+| j|k| j|kg}|D ]	}||dB  qAd S )NrO   g      4@rA   iI )r-   r4   r   rE   r3   r   r   r   )r;   rF   suggestrG   rR   r   r   r   soft_constraints   s&   

zLayoutBox.soft_constraintsc                 C   s   || _ |   dS )z/Replace the parent of this with the new parent.N)r*   rQ   )r;   r*   r   r   r   
set_parent   s   zLayoutBox.set_parentr@   c                 C   sB   | j |k| j|k| j|k| j|kg}|D ]
}| j||B  qd S rH   r   r   r   r   r-   rE   )r;   r   r   r   r   strengthrS   rG   r   r   r   constrain_geometry   s   zLayoutBox.constrain_geometryc                 C   sJ   | j |j k| j|jk| j|jk| j|jkg}|D ]
}| j||B  qdS )zJ
        Make the layoutbox have same position as other layoutbox
        NrW   )r;   otherrX   rS   rG   r   r   r   constrain_same   s   



zLayoutBox.constrain_samec                 C   &   | j | jj | k}| j||B  d S rH   )r   r*   r-   rE   r;   marginrX   rG   r   r   r   constrain_left_margin      zLayoutBox.constrain_left_marginc                 C      | j | j| d S rH   )r-   rD   r$   r;   r^   r   r   r   edit_left_margin_min   rK   zLayoutBox.edit_left_margin_minc                 C   &   | j | jj | k}| j||B  d S rH   )r   r*   r-   rE   r]   r   r   r   constrain_right_margin   r`   z LayoutBox.constrain_right_marginc                 C   ra   rH   )r-   rD   r%   rb   r   r   r   edit_right_margin_min  rK   zLayoutBox.edit_right_margin_minc                 C   r\   rH   )r   r*   r-   rE   r]   r   r   r   constrain_bottom_margin  r`   z!LayoutBox.constrain_bottom_marginc                 C   ra   rH   )r-   rD   r&   rb   r   r   r   edit_bottom_margin_min  rK   z LayoutBox.edit_bottom_margin_minc                 C   rd   rH   )r   r*   r-   rE   r]   r   r   r   constrain_top_margin  r`   zLayoutBox.constrain_top_marginc                 C   ra   rH   )r-   rD   r'   rb   r   r   r   edit_top_margin_min  rK   zLayoutBox.edit_top_margin_minc                 C   s$   | j  | j | j | j fS rH   )r   valuer   r   r   r;   r   r   r   get_rect  s   zLayoutBox.get_rectc                 C   s   | j   dS )zp
        Update *all* the variables that are part of the solver this LayoutBox
        is created with.
        N)r-   updateVariablesrl   r   r   r   update_variables  s   zLayoutBox.update_variablesc                 C   s<   | j }| jfD ]}||s||| q|| j| dS )z
        Set the height of the layout box.

        This is done as an editable variable so that the value can change
        due to resizing.
        N)r-   r   rB   rC   rD   )r;   r   rX   rF   rR   r   r   r   edit_height   s   
zLayoutBox.edit_heightc                 C      | j |k}| j||B  dS )zr
        Constrain the height of the layout box.  height is
        either a float or a layoutbox.height.
        Nr   r-   rE   r;   r   rX   rG   r   r   r   constrain_height-     
zLayoutBox.constrain_heightc                 C      | j |k}| j||B  d S rH   rr   rs   r   r   r   constrain_height_min5     
zLayoutBox.constrain_height_minc                 C   s<   | j }| jfD ]}||s||| q|| j| d S rH   )r-   r   rB   rC   rD   )r;   r   rX   rF   rR   r   r   r   
edit_width9  s   
zLayoutBox.edit_widthc                 C   rq   )zq
        Constrain the width of the layout box.  *width* is
        either a float or a layoutbox.width.
        Nr   r-   rE   r;   r   rX   rG   r   r   r   constrain_width@  ru   zLayoutBox.constrain_widthc                 C   rv   rH   rz   r{   r   r   r   constrain_width_minH  rx   zLayoutBox.constrain_width_minc                 C      | j |k}| j||B  d S rH   )r   r-   rE   )r;   r   rX   rG   r   r   r   constrain_leftL  rx   zLayoutBox.constrain_leftc                 C   r~   rH   )r   r-   rE   )r;   r   rX   rG   r   r   r   constrain_bottomP  rx   zLayoutBox.constrain_bottomc                 C   r~   rH   )r   r-   rE   )r;   r   rX   rG   r   r   r   constrain_rightT  rx   zLayoutBox.constrain_rightc                 C   r~   rH   )r   r-   rE   )r;   r   rX   rG   r   r   r   constrain_topX  rx   zLayoutBox.constrain_topc                 C       | j dd }|dd dkS )zV
        Helper to check if this layoutbox is the layoutbox of a subplotspec.
        .N   ssr+   splitr;   r+   r   r   r   _is_subplotspec_layoutbox\     z#LayoutBox._is_subplotspec_layoutboxc                 C   r   )zS
        Helper to check if this layoutbox is the layoutbox of a gridspec.
        r   r   N   gridspecr   r   r   r   r   _is_gridspec_layoutboxc  r   z LayoutBox._is_gridspec_layoutboxc                 C   s.   | j r| g}ng }| jD ]}|| 7 }q|S )z
        Find children of this layout box that are subplots.  We want to line
        poss up, and this is an easy way to find them all.
        )r2   r6   find_child_subplots)r;   subplotsrJ   r   r   r   r   j  s   
zLayoutBox.find_child_subplotsc           (         s  t | |||d}| }| \}}| j}	dd}
d}d|
 }| }d}d}||||d    }|| }|jdurQ|| t|jfdd|jD }n|g| }dg|g|d   }tt||gj	 ||||d    }|| }|j
dur|| t|j
fd	d|j
D }n|g| }dg|g|d   }tt||gj	 fd
dt|D } fddt|D }fddt|D }fddt|D }t|j|\}}t|j|\}}t|| || } t|| || }!t|| || }"t|| || }#|#|" }$|!|  }%| j}	| j|	j|	j|"  k| j|	j|	j|   k| j|	j|$ k| j|	j|% kg}&|&D ]}'| j|'dB  qB|S )a  
        Make a layout box from a subplotspec. The layout box is
        constrained to be a fraction of the width/height of the parent,
        and be a fraction of the parent width/height from the left/bottom
        of the parent.  Therefore the parent can move around and the
        layout for the subplot spec should move with it.

        The parent is *usually* the gridspec that made the subplotspec.??
        )r*   r+   r0   r1   rO         ?r   Nc                       g | ]} |  qS r   r   .0r)	netHeighttrr   r   
<listcomp>      z5LayoutBox.layout_from_subplotspec.<locals>.<listcomp>r   c                    r   r   r   r   )netWidthr   r   r   r     r   c                    s   g | ]
} d |   qS r   r   r   rowNumcellHsr   r   r   r         c                    s    g | ]} d | d   qS r   r   r   r   r   r   r   r          c                    s   g | ]
} d |   qS r   r   r   colNumcellWsr   r   r   r     r   c                    s    g | ]} d | d   qS r   r   r   r   r   r   r     r   rA   )r   get_gridspecget_geometryr*   _row_height_ratiossumnpcumsumcolumn_stackflat_col_width_ratiosrangedivmodnum1num2minmaxr   r   r   r   r-   rE   )(r;   subspecr+   r0   r1   lbgsnrowsncolsr*   r   r   totWidth	totHeighthspacewspacecellHsepHcellHeights
sepHeightscellWsepW
cellWidths	sepWidthsfigTops
figBottomsfigLefts	figRightsrowNum1colNum1rowNum2colNum2	figBottomfigTopfigLeftfigRightr   r   csrG   r   )r   r   r   r   r   r   r   r   layout_from_subplotspecw  sf   





z!LayoutBox.layout_from_subplotspecc                 C   sH   d| j dd| j dd| j dd| j dd| j ddS )	NzLayoutBox: 25sz	, (left: z1.3fz) (bot: z
) (right: z) (top: ))r+   r   rk   r   r   r   rl   r   r   r   __repr__  s   



zLayoutBox.__repr__)Nr   FFNr   r   FFNNr@   )r   NF)(__name__
__module____qualname____doc__r?   r8   r/   rN   r5   rQ   rP   rU   rV   rY   r[   r_   rc   re   rf   rg   rh   ri   rj   rm   ro   rp   rt   rw   ry   r|   r}   r   r   r   r   r   r   r   r   r   r   r   r   r   r   .   sV    
>0	


	














Sr   r@   c                 C   sH   t dt| D ]}| |d  j| | | jk}| | j||B  qdS )zb
    Stack LayoutBox instances from left to right.
    *padding* is in figure-relative units.
    r   Nr   lenr   r   r-   rE   boxespaddingrX   rR   rG   r   r   r   hstack  s   r   c                 C   sH   t dt| D ]}| |d  j| | | jk}| | j||B  qdS )z-Stack LayoutBox instances from left to right.r   Nr   r   r   r   r   hpack     r   c                 C   H   t dt| D ]}| |d  j| | | jk}| | j||B  qdS -Stack LayoutBox instances from top to bottom.r   Nr   r   r   r   r-   rE   r   r   r   r   vstack  r   r   c                 C   r   r   r   r   r   r   r   vpack  r   r   mediumc                 C   n   |du rt t| }tdt| D ]"}| |d  j| | j||d   ||  k}| | j||B  qdS r   Nr   )r   onesr   r   r   r-   rE   )r   height_ratiosrX   rR   rG   r   r   r   match_heights     r   c                 C   r   r   )r   r   r   r   r   r-   rE   )r   width_ratiosrX   rR   rG   r   r   r   match_widths  r   r   c                 C      t | |d t| |d d S )Nr   )r   )r   r   )r   r   r   r   r   r   vstackeq     r   c                 C   r   )Nr   )r   )r   r   )r   r   r   r   r   r   hstackeq  r   r   c                 C   sF   g }| dd  D ]}t | d |t ||k}| d j||B  qd S )Nr   r   )getattrr-   rE   )r   attrrX   consboxr   r   r   align  s
   r   r   c                 C   v   | d }|}t |D ]}|j}q
| dd  D ]"}|}t |D ]}|j}q|j|j |j|j k}|j|dB  qd S Nr   r   r@   )r   r*   r   r-   rE   r   levelsbox0top0nr   topbrG   r   r   r   match_top_margins     r  c                 C   r   r   )r   r*   r   r-   rE   r  r   r   r   match_bottom_margins*  r  r	  c                 C   r   r   )r   r*   r   r-   rE   r  r   r   r   match_left_margins7  r  r
  c                 C   r   r   )r   r*   r   r-   rE   r  r   r   r   match_right_marginsD  r  r  c                 C      t | |d t| |d d S N)r  )r
  r  r   r  r   r   r   match_width_marginsQ  r   r  c                 C   r  r  )r  r	  r  r   r   r   match_height_marginsV  r   r  c                 C   r  r  )r  r  r  r   r   r   match_margins[  r   r  c                   C   s   dt t S )z5Generate a short sequential id for layoutbox objects.z%06d)next_layoutboxobjnumr   r   r   r   seq_idc  s   r  c                 C   s    t |  | jD ]}t| qdS )z$Print the children of the layoutbox.N)printr6   print_childrenr   rJ   r   r   r   r  h  s   

r  c                 C   s:   | dur| j du r| j  t|  dS t| j  dS dS )zT
    Make all elements in this tree None, signalling not to do any more layout.
    N)r*   r-   resetnonechildrennonetreer   r   r   r   r  o  s   

r  c                 C   s$   | j D ]}t| qd | j_d } d S rH   )r6   r  r0   
_layoutboxr  r   r   r   r  |  s   

r  c                 C   s<   | j du rtd td t|  td dS t| j  dS )zPrint the tree of layoutboxes.NzLayoutBox Tree
z==============

)r*   r  r  
print_treer  r   r   r   r    s   
r  Tc                 C   sp  ddl }ddlm} t| |jjr-| g d}|g d |d | 	| j
  n| }ddlm} |jd  d }|rEtd| |jD ]m}	|rPt|	 ||j|	j |	j f|	j |	j d	d
|| d |dkr|	jdd }
|d dkr|j|	j |	j |
d| || d n|j|	j |	j |
ddd| || d t||	|d |d qHdS )z(Simple plotting to show where boxes are.r   N)rO   rO   r   r   )r   r   r   gffffff?g333333?zaxes.prop_cyclecolorzLevel:noneg?)fcalphaecr   r   r      )sizer  r   r   )havar%  r  r   )levelprintit)
matplotlibmatplotlib.pyplotpyplot
isinstancefigureFigureadd_axesset_facecolor	set_alphadrawr   r   matplotlib.patchespatchesrcParamsby_keyr  r6   	add_patch	Rectangler   rk   r   r   r   r+   r   textr   r   plot_children)r
   r   r(  r)  r*  pltaxr5  colorsrJ   r+   r   r   r   r;    sJ   




r;  )r   r@   )Nr   )r   Nr   )r   )r   T)$r   	itertools
kiwisolverr(   loggingnumpyr   	getLoggerr   r   r   r   r   r   r   r   r   r   r   r   r   r  r	  r
  r  r  r  r  countr  r  r  r  r  r  r;  r   r   r   r   <module>   sD    
   
(














