3
ƽhR\                 @   s"  d Z ddlZddlZddlZddlZejeZ	dd Z
G dd dZd7dd	Zd8d
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dAddZdBd d!ZdCd"d#ZdDd$d%ZdEd&d'ZdFd(d)Zej Zd*d+ Zd,d- Zd.d/ Z d0d1 Z!d2d3 Z"dGd5d6Z#dS )HaX  

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   sV   | j r| j }nD| j}|r,t|dr,|j }n&tjd ddlm} || }|j }|S )Nget_rendererz1constrained_layout : falling back to Agg rendererr   )FigureCanvasAgg)Z_cachedRenderercanvashasattrr   _loginfoZmatplotlib.backends.backend_aggr   )figZrendererr   r    r	   ;/tmp/pip-build-7iwl8md4/matplotlib/matplotlib/_layoutbox.pyr      s    

r   c               @   sT  e Zd ZdZdRd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dSddZdTddZdUddZd d! ZdVd"d#Zd$d% ZdWd&d'Zd(d) ZdXd*d+Zd,d- Zd.d/ Zd0d1 ZdYd2d3ZdZd4d5Zd[d6d7Zd\d8d9Zd]d:d;Zd^d<d=Zd_d>d?Zd`d@dAZ dadBdCZ!dbdDdEZ"dFdG Z#dHdI Z$dJdK Z%dcdLdMZ&dNdO Z'dS )d	LayoutBoxzD
    Basic rectangle representation using kiwi solver variables
    N Fr      c             C   s  t j}|| _|| _| jd }|d kr6t j | _d| _n|j| _d | _|j|  || _|| _	|	| _
||d | _||d | _||d | _||d | _||d | _||d | _||d	 | _||d
 | _||d | _||d | _||d | _||d | _||d | _||d | _||d | _||d | _||d | _||d | _||d | _||d | _|\}}|\}}|| _|| _ | j!  g | _"d | _#| j	r| j$  |
| _%|| _&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nameZSolversolverZ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childrenZsubplotspecconstrain_marginsh_padw_pad)selfr%   r&   r-   r,   r)   Z
lower_leftZupper_rightr*   r+   r1   r2   r$   Zsnr   r   r   r   r	   r	   r
   __init__3   sX    


zLayoutBox.__init__c             C   s  | j }|j| js.|j| jd |j| jd | j| j| jj k}| j j|dB  | j| jk}| j j|dB  |j| j	s|j| j	d |j| j	d | j
| jj| j k}| j j|dB  | j
| j	k}| j j|dB  |j| js |j| jd |j| jd | j| j| jj k}| j j|dB  | j| jk}| j j|dB  |j| jsj|j| jd |j| jd | j| jj| j k}| j j|dB  | j| jk}| j 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   )r3   solcr	   r	   r
   r0   q   s:    zLayoutBox.constrain_marginsc             C   s   |  j |g7  _ d S )N)r/   )r3   childr	   r	   r
   r(      s    zLayoutBox.add_childc             C   s4   y| j j| W n tk
r.   tjd Y nX d S )Nz3Tried to remove child that doesn't belong to parent)r/   remove
ValueErrorr   r   )r3   r=   r	   r	   r
   remove_child   s    zLayoutBox.remove_childc             C   sN   | j }x,| j| jgD ]}|j|d |j|d qW | j  | jrJ| j  d S )Ng    eAg        )r'   r   r   r8   r9   hard_constraintsr%   parent_constrain)r3   r;   ir	   r	   r
   r.      s    zLayoutBox.add_constraintsc             C   sT   | j }| j|jk| j|jk| j|jk| j|jkg}x|D ]}| jj|dB  q8W d S )Nr6   )r%   r   r   r   r   r'   r:   )r3   r%   hcr<   r	   r	   r
   rB      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}x|D ]}| j
j|dB  qfW d S )Ng      ?r6   )r   r   r   r   r   r   r   r   r   r   r'   r:   )r3   rD   r<   r	   r	   r
   rA      s    

zLayoutBox.hard_constraintsc             C   s   | j }| jrd}nd}| j|k}x|D ]}|j|dB  q&W | jrHd}nd}| j|k}x|D ]}|j|dB  q\W | j|k| j|kg}x|D ]}|j|dB  qW d S )Ng        g      4@r6   iI )r'   r-   r   r:   r,   r   r   r   )r3   r;   Zsuggestr<   rC   r	   r	   r
   soft_constraints   s"    




zLayoutBox.soft_constraintsc             C   s   || _ | j  dS )z/Replace the parent of this with the new parent.N)r%   rB   )r3   r%   r	   r	   r
   
set_parent   s    zLayoutBox.set_parentr5   c             C   sF   | j |k| j|k| j|k| j|kg}x|D ]}| jj||B  q*W d S )N)r   r   r   r   r'   r:   )r3   r   r   r   r   strengthrD   r<   r	   r	   r
   constrain_geometry   s    
zLayoutBox.constrain_geometryc             C   sN   | j |j k| j|jk| j|jk| j|jkg}x|D ]}| jj||B  q2W dS )zJ
        Make the layoutbox have same position as other layoutbox
        N)r   r   r   r   r'   r:   )r3   otherrG   rD   r<   r	   r	   r
   constrain_same   s    



zLayoutBox.constrain_samec             C   s&   | j | jj | k}| jj||B  d S )N)r   r%   r'   r:   )r3   marginrG   r<   r	   r	   r
   constrain_left_margin   s    zLayoutBox.constrain_left_marginc             C   s   | j j| j| d S )N)r'   r9   r   )r3   rK   r	   r	   r
   edit_left_margin_min   s    zLayoutBox.edit_left_margin_minc             C   s&   | j | jj | k}| jj||B  d S )N)r   r%   r'   r:   )r3   rK   rG   r<   r	   r	   r
   constrain_right_margin   s    z LayoutBox.constrain_right_marginc             C   s   | j j| j| d S )N)r'   r9   r    )r3   rK   r	   r	   r
   edit_right_margin_min  s    zLayoutBox.edit_right_margin_minc             C   s&   | j | jj | k}| jj||B  d S )N)r   r%   r'   r:   )r3   rK   rG   r<   r	   r	   r
   constrain_bottom_margin  s    z!LayoutBox.constrain_bottom_marginc             C   s   | j j| j| d S )N)r'   r9   r!   )r3   rK   r	   r	   r
   edit_bottom_margin_min  s    z LayoutBox.edit_bottom_margin_minc             C   s&   | j | jj | k}| jj||B  d S )N)r   r%   r'   r:   )r3   rK   rG   r<   r	   r	   r
   constrain_top_margin  s    zLayoutBox.constrain_top_marginc             C   s   | j j| j| d S )N)r'   r9   r"   )r3   rK   r	   r	   r
   edit_top_margin_min  s    zLayoutBox.edit_top_margin_minc             C   s$   | j j | jj | jj | jj fS )N)r   valuer   r   r   )r3   r	   r	   r
   get_rect  s    zLayoutBox.get_rectc             C   s   | j j  dS )zp
        Update *all* the variables that are part of the solver this LayoutBox
        is created with.
        N)r'   ZupdateVariables)r3   r	   r	   r
   update_variables  s    zLayoutBox.update_variablesc             C   s@   | j }x&| jgD ]}|j|s|j|| qW |j| 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   r7   r8   r9   )r3   r   rG   r;   rC   r	   r	   r
   edit_height   s
    
zLayoutBox.edit_heightc             C   s   | j |k}| jj||B  dS )zr
        Constrain the height of the layout box.  height is
        either a float or a layoutbox.height.
        N)r   r'   r:   )r3   r   rG   r<   r	   r	   r
   constrain_height-  s    
zLayoutBox.constrain_heightc             C   s   | j |k}| jj||B  d S )N)r   r'   r:   )r3   r   rG   r<   r	   r	   r
   constrain_height_min5  s    
zLayoutBox.constrain_height_minc             C   s@   | j }x&| jgD ]}|j|s|j|| qW |j| j| d S )N)r'   r   r7   r8   r9   )r3   r   rG   r;   rC   r	   r	   r
   
edit_width9  s
    
zLayoutBox.edit_widthc             C   s   | j |k}| jj||B  dS )zq
        Constrain the width of the layout box.  *width* is
        either a float or a layoutbox.width.
        N)r   r'   r:   )r3   r   rG   r<   r	   r	   r
   constrain_width@  s    
zLayoutBox.constrain_widthc             C   s   | j |k}| jj||B  d S )N)r   r'   r:   )r3   r   rG   r<   r	   r	   r
   constrain_width_minH  s    
zLayoutBox.constrain_width_minc             C   s   | j |k}| jj||B  d S )N)r   r'   r:   )r3   r   rG   r<   r	   r	   r
   constrain_leftL  s    
zLayoutBox.constrain_leftc             C   s   | j |k}| jj||B  d S )N)r   r'   r:   )r3   r   rG   r<   r	   r	   r
   constrain_bottomP  s    
zLayoutBox.constrain_bottomc             C   s   | j |k}| jj||B  d S )N)r   r'   r:   )r3   r   rG   r<   r	   r	   r
   constrain_rightT  s    
zLayoutBox.constrain_rightc             C   s   | j |k}| jj||B  d S )N)r   r'   r:   )r3   r   rG   r<   r	   r	   r
   constrain_topX  s    
zLayoutBox.constrain_topc             C   s    | j jdd }|dd dkS )zV
        Helper to check if this layoutbox is the layoutbox of a subplotspec.
        .r   N   ss)r&   split)r3   r&   r	   r	   r
   _is_subplotspec_layoutbox\  s    z#LayoutBox._is_subplotspec_layoutboxc             C   s    | j jdd }|dd dkS )zS
        Helper to check if this layoutbox is the layoutbox of a gridspec.
        ra   r   N   Zgridspecrd   )r&   re   )r3   r&   r	   r	   r
   _is_gridspec_layoutboxc  s    z LayoutBox._is_gridspec_layoutboxc             C   s2   | j r| g}ng }x| jD ]}||j 7 }qW |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.
        )r+   r/   find_child_subplots)r3   Zsubplotsr=   r	   r	   r
   ri   j  s    zLayoutBox.find_child_subplotsc       (         s  t | |||d}|j }|j \}}| j}	dd}
d}d|
 }| }d}d}||||d    }|| }|jdk	r|| t|jfdd|jD }n
|g| }dg|g|d   }tjtj||gj	 ||||d    }|| }|j
dk	r(|| t|j
fd	d|j
D }n
|g| }dg|g|d   }tjtj||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}&x|&D ]}'| jj|'dB  qW |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&   r)   r*   g        g      ?r   Nc                s   g | ]} |  qS r	   r	   ).0r)	netHeighttrr	   r
   
<listcomp>  s    z5LayoutBox.layout_from_subplotspec.<locals>.<listcomp>r   c                s   g | ]} |  qS r	   r	   )rj   rk   )netWidthrm   r	   r
   rn     s    c                s   g | ]} d |   qS )rb   r	   )rj   rowNum)cellHsr   r	   r
   rn     s    c                s    g | ]} d | d   qS )rb   r   r	   )rj   rp   )rq   r   r	   r
   rn     s    c                s   g | ]} d |   qS )rb   r	   )rj   colNum)cellWsr   r	   r
   rn     s    c                s    g | ]} d | d   qS )rb   r   r	   )rj   rr   )rs   r   r	   r
   rn     s    r6   )r   Zget_gridspecZget_geometryr%   Z_row_height_ratiossumnpZcumsumZcolumn_stackZflatZ_col_width_ratiosrangedivmodZnum1Znum2minmaxr   r   r   r   r'   r:   )(r3   Zsubspecr&   r)   r*   lbZgsZnrowsZncolsr%   r   r   ZtotWidthZ	totHeightZhspaceZwspaceZcellHZsepHZcellHeightsZ
sepHeightsZcellWZsepWZ
cellWidthsZ	sepWidthsZfigTopsZ
figBottomsZfigLeftsZ	figRightsZrowNum1ZcolNum1ZrowNum2ZcolNum2Z	figBottomZfigTopZfigLeftZfigRightr   r   csr<   r	   )rq   rs   r   rl   ro   r   rm   r
   layout_from_subplotspecw  sd    





z!LayoutBox.layout_from_subplotspecc             C   sH   d| j dd| jj dd| jj dd| jj dd| jj ddS )	NzLayoutBox: Z25sz	, (left: z1.3fz) (bot: z
) (right: z) (top: ))r&   r   rT   r   r   r   )r3   r	   r	   r
   __repr__  s    zLayoutBox.__repr__r   r   r   r   )Nr   FFNr   r   FFNN)r5   )r5   )r5   )r5   )r5   )r5   )r5   )r5   )r5   )r5   )r5   )r5   )r5   )r5   )r5   )r5   )r   NF)(__name__
__module____qualname____doc__r4   r0   r(   r@   r.   rB   rA   rE   rF   rH   rJ   rL   rM   rN   rO   rP   rQ   rR   rS   rU   rV   rW   rX   rY   rZ   r[   r\   r]   r^   r_   r`   rf   rh   ri   r|   r~   r	   r	   r	   r
   r   .   sP      
;0	

	















Rr   r5   c             C   sL   xFt dt| D ]4}| |d  j| | | jk}| | jj||B  qW dS )zb
    Stack LayoutBox instances from left to right.
    *padding* is in figure-relative units.
    r   N)rv   lenr   r   r'   r:   )boxespaddingrG   rC   r<   r	   r	   r
   hstack  s    r   c             C   sL   xFt dt| D ]4}| |d  j| | | jk}| | jj||B  qW dS )z-Stack LayoutBox instances from left to right.r   N)rv   r   r   r   r'   r:   )r   r   rG   rC   r<   r	   r	   r
   hpack  s    r   c             C   sL   xFt dt| D ]4}| |d  j| | | jk}| | jj||B  qW dS )z-Stack LayoutBox instances from top to bottom.r   N)rv   r   r   r   r'   r:   )r   r   rG   rC   r<   r	   r	   r
   vstack  s    r   c             C   sL   xFt dt| D ]4}| |d  j| | | jk}| | jj||B  qW dS )z-Stack LayoutBox instances from top to bottom.r   N)rv   r   r   r   r'   r:   )r   r   rG   rC   r<   r	   r	   r
   vpack  s    r   mediumc             C   sr   |dkrt jt| }xVtdt| D ]D}| |d  j| | j||d   ||  k}| | jj||B  q&W dS )z-Stack LayoutBox instances from top to bottom.Nr   )ru   onesr   rv   r   r'   r:   )r   height_ratiosrG   rC   r<   r	   r	   r
   match_heights  s     r   c             C   sr   |dkrt jt| }xVtdt| D ]D}| |d  j| | j||d   ||  k}| | jj||B  q&W dS )z-Stack LayoutBox instances from top to bottom.Nr   )ru   r   r   rv   r   r'   r:   )r   width_ratiosrG   rC   r<   r	   r	   r
   match_widths  s     r   c             C   s   t | |d t| |d d S )N)r   )r   )r   r   )r   r   r   r	   r	   r
   vstackeq  s    r   c             C   s   t | |d t| |d d S )N)r   )r   )r   r   )r   r   r   r	   r	   r
   hstackeq  s    r   c             C   sJ   g }x@| dd  D ]0}t | d |t ||k}| d jj||B  qW d S )Nr   r   )getattrr'   r:   )r   attrrG   Zconsboxr	   r	   r
   align  s    r   r   c             C   s   | d }|}xt |D ]
}|j}qW xX| dd  D ]H}|}xt |D ]
}|j}qDW |j|j |j|j k}|jj|dB  q2W d S )Nr   r   r5   )rv   r%   r   r'   r:   )r   levelsbox0top0nr   topbr<   r	   r	   r
   match_top_margins  s    

r   c             C   s   | d }|}xt |D ]
}|j}qW xX| dd  D ]H}|}xt |D ]
}|j}qDW |j|j |j|j k}|jj|dB  q2W d S )Nr   r   r5   )rv   r%   r   r'   r:   )r   r   r   r   r   r   r   r<   r	   r	   r
   match_bottom_margins*  s    

r   c             C   s   | d }|}xt |D ]
}|j}qW xX| dd  D ]H}|}xt |D ]
}|j}qDW |j|j |j|j k}|jj|dB  q2W d S )Nr   r   r5   )rv   r%   r   r'   r:   )r   r   r   r   r   r   r   r<   r	   r	   r
   match_left_margins7  s    

r   c             C   s   | d }|}xt |D ]
}|j}qW xX| dd  D ]H}|}xt |D ]
}|j}qDW |j|j |j|j k}|jj|dB  q2W d S )Nr   r   r5   )rv   r%   r   r'   r:   )r   r   r   r   r   r   r   r<   r	   r	   r
   match_right_marginsD  s    

r   c             C   s   t | |d t| |d d S )N)r   )r   r   )r   r   r	   r	   r
   match_width_marginsQ  s    r   c             C   s   t | |d t| |d d S )N)r   )r   r   )r   r   r	   r	   r
   match_height_marginsV  s    r   c             C   s   t | |d t| |d d S )N)r   )r   r   )r   r   r	   r	   r
   match_margins[  s    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 |  x| jD ]}t| qW dS )z$Print the children of the layoutbox.N)printr/   print_children)rz   r=   r	   r	   r
   r   h  s    r   c             C   s4   | dk	r0| j dkr&| jj  t|  n
t| j  dS )zT
    Make all elements in this tree None, signalling not to do any more layout.
    N)r%   r'   resetnonechildrennonetree)rz   r	   r	   r
   r   o  s
    


r   c             C   s(   x| j D ]}t| qW d | j_d } d S )N)r/   r   r)   Z
_layoutbox)rz   r=   r	   r	   r
   r   |  s    r   c             C   s:   | j dkr,td td t|  td n
t| j  dS )zPrint the tree of layoutboxes.NzLayoutBox Tree
z==============

)r%   r   r   
print_tree)rz   r	   r	   r
   r     s    

r   Tc          
   C   s|  ddl }ddlj}t| |jjr`| jddddg}|jddddg |jd | j	| j
j  n| }ddlj}|jd j d }|rtd	| x|jD ]}	|rt|	 |j|j|	jj |	jj f|	jj |	jj d
d|| d |dkr`|	jjdd }
|d dkr4|j|	jj |	jj |
d| || d n,|j|	jj |	jj |
ddd| || d t||	|d |d qW dS )z(Simple plotting to show where boxes are.r   Ng        g      ?gffffff?g333333?zaxes.prop_cyclecolorzLevel:noneg?)ZfcalphaZecra   r   rb      )sizer   r   r   )Zhavar   r   )levelprintitrd   )
matplotlibZmatplotlib.pyplotZpyplot
isinstancefigureZFigureZadd_axesZset_facecolorZ	set_alphaZdrawr   r   Zmatplotlib.patchespatchesZrcParamsby_keyr   r/   Z	add_patchZ	Rectangler   rT   r   r   r   r&   re   textr   r   plot_children)r   r   r   r   r   ZpltZaxr   colorsr=   r&   r	   r	   r
   r     s@    





r   )r   r5   )r   r5   )r   r5   )r   r5   )Nr   )Nr   )r   N)r   N)r5   )r   )r   )r   )r   )r   )r   )r   )r   T)$r   	itertoolsZ
kiwisolverr#   loggingnumpyru   	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>   sB   
   (















