o
    tf<                     @   s   d Z ddlZddlmZ ddlmZmZmZmZm	Z	m
Z
 ddlmZ ddlmZmZ ddlmZmZ dd	lmZ dd
lmZ ddlmZ ddiZeeG dd deZeeG dd deeZeeG dd deeZeeG dd deeZdS )z7Implement common widgets layouts as reusable components    N)defaultdict)InstanceBoolUnicodeCUnicodeCaselessStrEnumTuple)Integer)	HasTraits
TraitError)observevalidate   )Widget)GridBox)	doc_substZstyle_paramsa  

    grid_gap : str
        CSS attribute used to set the gap between the grid cells

    justify_content : str, in ['flex-start', 'flex-end', 'center', 'space-between', 'space-around']
        CSS attribute used to align widgets vertically

    align_items : str, in ['top', 'bottom', 'center', 'flex-start', 'flex-end', 'baseline', 'stretch']
        CSS attribute used to align widgets horizontally

    width : str
    height : str
        width and heightc                       s   e Zd ZdZeddddZeg ddddZeg ddd	dZeddd
dZ	eddd
dZ
 fddZdd Zdd Zdd Z  ZS )LayoutPropertiesaK  Mixin class for layout templates

    This class handles mainly style attributes (height, grid_gap etc.)

    Parameters
    ----------

    {style_params}


    Note
    ----

    This class is only meant to be used in inheritance as mixin with other
    classes. It will not work, unless `self.layout` attribute is defined.

    NTzThe grid-gap CSS attribute.)
allow_nonehelp)
flex-startflex-endcenterzspace-betweenzspace-aroundz"The justify-content CSS attribute.)topbottomr   r   r   ZbaselineZstretchzThe align-items CSS attribute.zThe width CSS attribute.c                    s@   t  jdi | tt| _ddd| jd< |   |   d S )Nr   r   )r   r   align_items )super__init__r   dict_property_rewrite_copy_layout_props_set_observersselfkwargs	__class__r   l/var/www/html/software/conda/envs/catlas/lib/python3.10/site-packages/ipywidgets/widgets/widget_templates.pyr   O   s   
zLayoutProperties.__init__c                 C   s6   |d |d }}| j | ||}t| j|| dS )z=delegate the trait types to their counterparts in self.layoutnewnameN)r   getsetattrlayout)r#   changevaluer)   r   r   r'   _delegate_to_layoutW   s   z$LayoutProperties._delegate_to_layoutc                 C   s   t  }| | j| dS )z<set observers on all layout properties defined in this classN)r   class_trait_namesr   r/   )r#   _propsr   r   r'   r!   ]   s   zLayoutProperties._set_observersc                 C   sD   t  }|D ]}t| |}|r| j| ||}t| j|| qd S N)r   r0   getattrr   r*   r+   r,   )r#   r1   propr.   r   r   r'   r    b   s   
z#LayoutProperties._copy_layout_props)__name__
__module____qualname____doc__r   Zgrid_gapr   Zjustify_contentr   widthheightr   r/   r!   r    __classcell__r   r   r%   r'   r   "   s<    r   c                	       s   e Zd ZdZeeddZeeddZeeddZeeddZ	eeddZ
ee e e g ddZee e e g ddZeddZ fddZed	d
 Zdd Zdd Zedddddddddd Z  ZS )	AppLayouta"   Define an application like layout of widgets.

    Parameters
    ----------

    header: instance of Widget
    left_sidebar: instance of Widget
    center: instance of Widget
    right_sidebar: instance of Widget
    footer: instance of Widget
        widgets to fill the positions in the layout

    merge: bool
        flag to say whether the empty positions should be automatically merged

    pane_widths: list of numbers/strings
        the fraction of the total layout width each of the central panes should occupy
        (left_sidebar,
        center, right_sidebar)

    pane_heights: list of numbers/strings
        the fraction of the width the vertical space that the panes should occupy
         (left_sidebar, center, right_sidebar)

    {style_params}

    Examples
    --------

    Tr   )1frZ2frr>   default_value)r>   Z3frr>   c                       t  jdi | |   d S Nr   r   r   _update_layoutr"   r%   r   r'   r         zAppLayout.__init__c                 C   s2   t d| r| S t d| r| d S td| )Nz\d+\.?\d*(px|fr|%)$z
\d+\.?\d*$frzqthe pane sizes must be in one of the following formats: '10px', '10fr', 10 (will be converted to '10fr').Got '{}')rematch	TypeErrorformat)sizer   r   r'   _size_to_css   s   zAppLayout._size_to_cssc                 C   s   t t| j|S r2   )listmaprL   )r#   Z	size_listr   r   r'   _convert_sizes   s   zAppLayout._convert_sizesc           
      C   s  g dg dg dg}|  | j}|  | j}| j| j| j| j| jd}dd | D }|s1d S | D ]\}}||j	_
q5| jrt|dkr^t| d }|||g|||g|||gg}n`| jd u rn|D ]}|d= qe|d= | jd u r}|d d |d d< | jd u r|d d	 |d d
< | jd u r| jd u r| jd u rdgdgg}dg}ddg}| jd u r|d= |d= | jd u r|d
= |d
= ddd |D }	d|| j	_d|| j	_|	| j	_t| | _d S )N)headerrP   rP   )left-sidebarr   right-sidebar)footerrS   rS   )rP   rS   rQ   rR   r   c                 S      i | ]\}}|d ur||qS r2   r   .0positionchildr   r   r'   
<dictcomp>   
    z,AppLayout._update_layout.<locals>.<dictcomp>r   r   rP   rS   r>   
c                 s        | ]}d  d|V  qdS z"{}" NrJ   joinrV   liner   r   r'   	<genexpr>       z+AppLayout._update_layout.<locals>.<genexpr>r`   )rO   pane_widthspane_heightsrP   rS   left_sidebarright_sidebarr   itemsr,   	grid_areamergelenrM   keysrb   grid_template_columnsgrid_template_rowsgrid_template_areastuplevalueschildren)
r#   rr   rp   rq   all_childrenru   rW   rX   rowgrid_template_areas_cssr   r   r'   rD      sj   









zAppLayout._update_layoutrS   rP   r   ri   rj   rm   rg   rh   c                 C      |    d S r2   rD   r#   r-   r   r   r'   _child_changed   s   zAppLayout._child_changed)r5   r6   r7   r8   r   r   rP   rS   ri   rj   r   r   r   rg   rh   r   rm   r   staticmethodrL   rO   rD   r   r|   r;   r   r   r%   r'   r<   l   s.     


Fr<   c                       s`   e Zd ZdZe Ze Zd fdd	Zedddd Z	d	d
 Z
dd Zdd Zdd Z  ZS )GridspecLayouta   Define a N by M grid layout

    Parameters
    ----------

    n_rows : int
        number of rows in the grid

    n_columns : int
        number of columns in the grid

    {style_params}

    Examples
    --------

    >>> from ipywidgets import GridspecLayout, Button, Layout
    >>> layout = GridspecLayout(n_rows=4, n_columns=2, height='200px')
    >>> layout[:3, 0] = Button(layout=Layout(height='auto', width='auto'))
    >>> layout[1:, 1] = Button(layout=Layout(height='auto', width='auto'))
    >>> layout[-1, 0] = Button(layout=Layout(height='auto', width='auto'))
    >>> layout[0, 1] = Button(layout=Layout(height='auto', width='auto'))
    >>> layout
    Nc                    sd   t  jdi | | _| _ fddt jD  _d jf  _d jf  _i  _d _	d S )Nc                    s   g | ]}d g j  qS ).)	n_columns)rV   ir#   r   r'   
<listcomp>  s    z+GridspecLayout.__init__.<locals>.<listcomp>zrepeat(%d, 1fr)r   r   )
r   r   n_rowsr   range_grid_template_areas_grid_template_rows_grid_template_columns	_children	_id_count)r#   r   r   r$   r%   r   r'   r     s   
zGridspecLayout.__init__r   r   c                 C   s   |d dkr
|d S t d)Nr.   r   z-n_rows and n_columns must be positive integer)r   )r#   Zproposalr   r   r'   _validate_integer#  s   z GridspecLayout._validate_integerc                 C   sn   t |tr|| j\}}}t|||}n|g}t |tr0|| j\}}}t|||}||fS |g}||fS )zDconvert a two-dimensional slice to a list of rows and column indices)
isinstancesliceindicesr   r   r   )r#   rw   columnstartstopZstriderowscolumnsr   r   r'   _get_indices_from_slice)  s   

z&GridspecLayout._get_indices_from_slicec           	      C   s   |\}}|  j d7  _ d| j  }||j_| ||\}}|D ]"}|D ]}| j| | }|dkr8|| jv r8| j|= || j| |< q"q|| j|< |   d S )Nr   z
widget%03dr   )r   r,   rl   r   r   r   rD   )	r#   keyr.   rw   r   obj_idr   r   current_valuer   r   r'   __setitem__:  s   

zGridspecLayout.__setitem__c                 C   sV   | j | \}}d }|D ]}|D ]}| j| | }|p|}||kr$tdqq| j| S )NzTThe slice spans several widgets, but only a single widget can be retrieved at a time)r   r   rI   r   )r#   r   r   r   r   rw   r   Z
new_obj_idr   r   r'   __getitem__L  s   
zGridspecLayout.__getitem__c                 C   sF   d dd | jD }| j| j_| j| j_|| j_t| j	
 | _d S )Nr]   c                 s   r^   r_   ra   rc   r   r   r'   re   ]  rf   z0GridspecLayout._update_layout.<locals>.<genexpr>)rb   r   r   r,   rp   r   rq   rr   rs   r   rt   ru   )r#   rx   r   r   r'   rD   [  s   


zGridspecLayout._update_layout)NN)r5   r6   r7   r8   r	   r   r   r   r   r   r   r   r   rD   r;   r   r   r%   r'   r~      s    
r~   c                       sz   e Zd ZdZeeddZeeddZeeddZeeddZ	e
ddZ fddZdd Zed	d
ddddd Z  ZS )TwoByTwoLayouta   Define a layout with 2x2 regular grid.

    Parameters
    ----------

    top_left: instance of Widget
    top_right: instance of Widget
    bottom_left: instance of Widget
    bottom_right: instance of Widget
        widgets to fill the positions in the layout

    merge: bool
        flag to say whether the empty positions should be automatically merged

    {style_params}

    Examples
    --------

    >>> from ipywidgets import TwoByTwoLayout, Button
    >>> TwoByTwoLayout(top_left=Button(description="Top left"),
    ...                top_right=Button(description="Top right"),
    ...                bottom_left=Button(description="Bottom left"),
    ...                bottom_right=Button(description="Bottom right"))

    Tr=   r?   c                    rA   rB   rC   r"   r%   r   r'   r     rE   zTwoByTwoLayout.__init__c                 C   s  ddgddgg}| j | j| j| jd}dd | D }|s d S | D ]\}}||j_q$| jrt|dkrGt	|
 d	 }||g||gg}n\d
dg}t|D ]S\}}|d| |d| }	}
|d d }|	d u r|
d u r|d	 | |d	 |< |d | |d |< qO|	d u r|d | |d	 |< qO|
d u r|d	 | |d |< qOddd |D }d| j_d| j_|| j_t| | _d S )Ntop-left	top-rightbottom-leftbottom-right)r   r   r   r   c                 S   rT   r2   r   rU   r   r   r'   rY     rZ   z1TwoByTwoLayout._update_layout.<locals>.<dictcomp>r   r   leftrightztop-zbottom-   r]   c                 s   r^   r_   ra   rc   r   r   r'   re     rf   z0TwoByTwoLayout._update_layout.<locals>.<genexpr>z1fr 1fr)top_left	top_rightbottom_leftbottom_rightrk   r,   rl   rm   rn   rM   ro   	enumerater*   rb   rp   rq   rr   rs   rt   ru   )r#   rr   rv   ru   rW   rX   r   r   r   r   r   Zi_neighbourrx   r   r   r'   rD     sP   

zTwoByTwoLayout._update_layoutr   r   r   r   rm   c                 C   ry   r2   rz   r{   r   r   r'   r|     s   zTwoByTwoLayout._child_changed)r5   r6   r7   r8   r   r   r   r   r   r   r   rm   r   rD   r   r|   r;   r   r   r%   r'   r   f  s    
4r   )r8   rG   collectionsr   Z	traitletsr   r   r   r   r   r   r	   r
   r   r   r   Zwidgetr   Z
widget_boxr   Zdocutilsr   Z_doc_snippetsr   r<   r~   r   r   r   r   r'   <module>   s*     I k