U
    PeTV                     @   s   d Z dZddlmZ ddlmZ ddlmZmZm	Z	m
Z
mZmZmZmZmZ ddlmZ ddlmZmZmZmZ ddlmZ d	d
 Zdd ZG dd deZG dd deZdS )a  
Grid Layout
===========

.. only:: html

    .. image:: images/gridlayout.gif
        :align: right

.. only:: latex

    .. image:: images/gridlayout.png
        :align: right

.. versionadded:: 1.0.4

The :class:`GridLayout` arranges children in a matrix. It takes the available
space and divides it into columns and rows, then adds widgets to the resulting
"cells".

.. versionchanged:: 1.0.7
    The implementation has changed to use the widget size_hint for calculating
    column/row sizes. `uniform_width` and `uniform_height` have been removed
    and other properties have added to give you more control.

Background
----------

Unlike many other toolkits, you cannot explicitly place a widget in a specific
column/row. Each child is automatically assigned a position determined by the
layout configuration and the child's index in the children list.

A GridLayout must always have at least one input constraint:
:attr:`GridLayout.cols` or :attr:`GridLayout.rows`. If you do not specify cols
or rows, the Layout will throw an exception.

Column Width and Row Height
---------------------------

The column width/row height are determined in 3 steps:

    - The initial size is given by the :attr:`col_default_width` and
      :attr:`row_default_height` properties. To customize the size of a single
      column or row, use :attr:`cols_minimum` or :attr:`rows_minimum`.
    - The `size_hint_x`/`size_hint_y` of the children are taken into account.
      If no widgets have a size hint, the maximum size is used for all
      children.
    - You can force the default size by setting the :attr:`col_force_default`
      or :attr:`row_force_default` property. This will force the layout to
      ignore the `width` and `size_hint` properties of children and use the
      default size.

Using a GridLayout
------------------

In the example below, all widgets will have an equal size. By default, the
`size_hint` is (1, 1), so a Widget will take the full size of the parent::

    layout = GridLayout(cols=2)
    layout.add_widget(Button(text='Hello 1'))
    layout.add_widget(Button(text='World 1'))
    layout.add_widget(Button(text='Hello 2'))
    layout.add_widget(Button(text='World 2'))

.. image:: images/gridlayout_1.jpg

Now, let's fix the size of Hello buttons to 100px instead of using
size_hint_x=1::

    layout = GridLayout(cols=2)
    layout.add_widget(Button(text='Hello 1', size_hint_x=None, width=100))
    layout.add_widget(Button(text='World 1'))
    layout.add_widget(Button(text='Hello 2', size_hint_x=None, width=100))
    layout.add_widget(Button(text='World 2'))

.. image:: images/gridlayout_2.jpg

Next, let's fix the row height to a specific size::

    layout = GridLayout(cols=2, row_force_default=True, row_default_height=40)
    layout.add_widget(Button(text='Hello 1', size_hint_x=None, width=100))
    layout.add_widget(Button(text='World 1'))
    layout.add_widget(Button(text='Hello 2', size_hint_x=None, width=100))
    layout.add_widget(Button(text='World 2'))

.. image:: images/gridlayout_3.jpg

)
GridLayoutGridLayoutException    )Logger)Layout)	NumericPropertyBooleanPropertyDictPropertyBoundedNumericPropertyReferenceListPropertyVariableListPropertyObjectPropertyStringPropertyOptionProperty)ceil)
accumulateproductchainislice)subc                  G   s   dd | D } t | S )Nc                 S   s   g | ]}|d k	r|qS N .0xr   r   7/tmp/pip-unpacked-wheel-xzebddm3/kivy/uix/gridlayout.py
<listcomp>h   s      znmax.<locals>.<listcomp>)maxargsr   r   r   nmaxf   s    r   c                  G   s   dd | D } t | S )Nc                 S   s   g | ]}|d k	r|qS r   r   r   r   r   r   r   n   s      znmin.<locals>.<listcomp>)minr   r   r   r   nminl   s    r!   c                   @   s   e Zd ZdZdS )r   z@Exception for errors if the grid layout manipulation fails.
    N)__name__
__module____qualname____doc__r   r   r   r   r   r   s   r   c                       s  e Zd ZdZeddgddZeddddgZeddddZeddddZ	e
dZe
dZedZedZei Zei Ze
dZe
dZeeeZed	d
dZ fddZdd Zdd Zedd Zedd Zedd Zdd Z dd Z!dd Z"dd Z#d d! Z$d"d# Z%d$d% Z&  Z'S )&r   zFGrid layout class. See module documentation for more information.
    r      )lengthNT)r    Z	allownoneFlr-tb)r(   ztb-lrzrl-tbztb-rlzlr-btzbt-lrzrl-btzbt-rl)optionsc                    s   d  | _ | _tt| jf | | j}| j}|d| |d| |d| |d| |d| |d| |d| |d| |d	| |d
| |d| |d| |d| d S )Ncol_default_widthrow_default_heightcol_force_defaultrow_force_defaultcolsrowsparentspacingpaddingchildrensizeposorientation)_cols_rowssuperr   __init__fbindZ_trigger_layout)selfkwargsr;   update	__class__r   r   r:     s"    











zGridLayout.__init__c                 C   s    | j r| jr| j| j  S d S d S r   r.   r/   r<   r   r   r   get_max_widgets(  s    zGridLayout.get_max_widgetsc                 C   s$   |   }|r t||kr tdd S )Nz4Too many children in GridLayout. Increase rows/cols!)rC   lenr   )r<   instancevalueZsmaxr   r   r   on_children.  s
    zGridLayout.on_childrenc                 C   s   | j d dkS )Nr   lrr6   rB   r   r   r   _fills_row_first6  s    zGridLayout._fills_row_firstc                 C   s
   d| j kS )NrH   rI   rB   r   r   r   _fills_from_left_to_right:  s    z$GridLayout._fills_from_left_to_rightc                 C   s
   d| j kS )NtbrI   rB   r   r   r   _fills_from_top_to_bottom>  s    z$GridLayout._fills_from_top_to_bottomc                    sp  | j }| j}|s&|s&td|   d S |d krDtt|t| }n|d kr`tt|t| }td|}td|}d| _d| _	d| _
d| _| jg|  | _ d g| | _d g| | _d g| | _| jg|  | _d g| | _d g| | _d g| | _ fdd| j D }|D ]\}}t| |  |< qfdd| j D }|D ]\}}t|| |< qNdS )	Nz5%r have no cols or rows set, layout is not triggered.   F        c                 3   s"   | ]}|d  t  k r|V  qdS r   NrD   r   i)r.   r   r   	<genexpr>e  s      z3GridLayout._init_rows_cols_sizes.<locals>.<genexpr>c                 3   s"   | ]}|d  t  k r|V  qdS rP   rQ   rR   )r/   r   r   rT   i  s      T)r.   r/   r   warningintr   floatr   _has_hint_bound_x_has_hint_bound_y_cols_min_size_none_rows_min_size_noner*   r7   _cols_sh_cols_sh_min_cols_sh_maxr+   r8   _rows_sh_rows_sh_min_rows_sh_maxcols_minimumitemsrows_minimum)r<   countZcurrent_colsZcurrent_rowsrc   indexrF   r   rA   r   _init_rows_cols_sizesB  s@    

z GridLayout._init_rows_cols_sizesc                 C   s  | j | j }}| j| j }}| j| j }}| j| j }}d }	}
| t	|t	|}t
t| j|D ]\}\}}|j|j \}}\}}|j\}}|j\}}|d krt|| |||< nPt|| |||< |d k	rd}
t|| |||< |d k	rd}
t|| |||< |d kr&t|| |||< qdt|| |||< |d k	rXd}	t|| |||< |d k	rdd}	t|| |||< qd|
| _|	| _d S )NFT)r7   r8   r\   r_   r]   r`   r^   ra   _create_idx_iterrD   zipreversedr3   	size_hintr4   size_hint_minsize_hint_maxr   r!   rX   rY   )r<   r.   r/   cols_shrows_shcols_sh_minrows_sh_mincols_sh_maxrows_sh_maxZhas_bound_yZhas_bound_xZidx_iterchildcolrowshwshhwhshw_minshh_minshw_maxshh_maxr   r   r   _fill_rows_cols_sizesn  s<    




z GridLayout._fill_rows_cols_sizesc                 C   s  | j \}}}}| j\}}| j| j }}|| |t|d   }	t||	 | _| jr| j}
| j	}t
t||
|D ]Z\}\}}}|d k	r|	t||7 }	td|| |
|< n|	|7 }	|d k	rntd|| ||< qnn| j}	|| |t|d   }t|| | _| jr| j}| j}t
t|||D ]`\}\}}}|d k	rV|t||7 }td|| ||< n||7 }|d k	rtd|| ||< qn| j}|	|f| _d S )NrN   rO   )r2   r1   r7   r8   rD   sumrZ   rX   r]   r^   	enumerateri   r   r[   rY   r`   ra   minimum_size)r<   ltrb	spacing_x	spacing_yr.   r/   widthrp   rr   rS   cZsh_minZsh_maxheightrq   rs   r   r   r   _update_minimum_size  sD    




zGridLayout._update_minimum_sizec                 C   s  | j }| j}| jrH| jgt| j }| j D ]\}}|||< q.|| _n| j}| j}| j	}t
tdd |D }td|| j }	|	dkr| jr| ||	tdd |D || j| t|D ]&\}}
|
sq||  |	|
 | 7  < q| jr"| jgt| j }| j D ]\}}|||< q|| _n| j}| j}| j}t
tdd |D }td|| j }|dkr| jr| ||tdd |D || j| t|D ],\}}|sq||  || | 7  < qd S )Nc                 s   s   | ]}|d k	r|V  qd S r   r   r   r   r   r   rT     s      z7GridLayout._finalize_rows_cols_sizes.<locals>.<genexpr>rO   g&.>c                 s   s   | ]}|d k	r|V  qd S r   r   )r   r   r   r   r   rT     s      c                 s   s   | ]}|d k	r|V  qd S r   r   r   r   r   r   rT     s      c                 s   s   | ]}|d k	r|V  qd S r   r   )r   r   r   r   r   rT     s      )r   r   r,   r*   rD   r7   rb   rc   r\   r]   rW   r   r   rZ   rX   Zlayout_hint_with_boundsr^   r   r-   r+   r8   rd   r_   r`   r[   rY   ra   )r<   ZselfwZselfhr.   rf   rF   rn   rp   Zcols_weightZ	stretch_wZcol_stretchr/   ro   rq   Zrows_weightZ	stretch_hZrow_stretchr   r   r   _finalize_rows_cols_sizes  sd    
   
   z$GridLayout._finalize_rows_cols_sizesc                 #   s  | j }| j}| j\ | j}| jrZtt| j|d  f fddt|t	|d D }nFtt| j
|d  |d  f fddtt|dd D t}t|}| j}| jrtt| j|d  |d  ffddt|dd D t}nBtt| j|d	  ffd
dtt|t	|d D }t|}| jrzttt|t||t||D ]$\}\}	}
\}}||
|	||fV  qRnFttt|t||t||D ]$\}\}
}	\}}||
|	||fV  qd S )Nr   c                 3   s   | ]}|  V  qd S r   r   r   	col_widthr   r   r   rT     s   z-GridLayout._iterate_layout.<locals>.<genexpr>rN   r&   c                 3   s   | ]}|  V  qd S r   r   r   r   r   r   rT     s   c                 3   s   | ]}|  V  qd S r   r   r   
row_heightr   r   r   rT   "  s      c                 3   s   | ]}|  V  qd S r   r   r   r   r   r   rT   *  s   )r6   r2   r1   r7   rK   r   r   r   r   rD   rightrj   r   r8   rM   topyrJ   ri   ranger   )r<   re   r6   r2   r.   Zx_iterr/   Zy_iterrS   r   r   r   r   r   )r   r   r   _iterate_layout  sd    







zGridLayout._iterate_layoutc                 G   sd  | j }|r| t|s<| j\}}}}|| || f| _d S |   |   |   | t|D ]\}}}	}
}|| }||	f|_	|j
\}}|j\}}|j\}}|d k	r|d k	rtt|
||}
qt|
|}
n|d k	rt|
|}
|d k	r|d k	rtt|||}n
t||}n|d k	r&t||}|d krB|d k	r^||_qb|d krT|
|_qb|
|f|_qbd S r   )r3   rg   rD   r2   r   r   r   r   r   r5   rk   rl   rm   r   r    r   r   r4   )r<   largsr3   r   r   r   r   rS   r   r   ry   rz   r   rw   rx   r{   r|   r}   r~   r   r   r   	do_layout>  s@    











zGridLayout.do_layoutc                 C   sd   | j rt|nt|d dd}| jr,t|nt|d dd}| jrVdd t||D S t||S d S )NrN   r   c                 s   s   | ]\}}||fV  qd S r   r   )r   Z	row_indexZ	col_indexr   r   r   rT   q  s   z.GridLayout._create_idx_iter.<locals>.<genexpr>)rK   r   rM   rJ   r   )r<   Zn_colsZn_rowsZcol_indicesZrow_indicesr   r   r   rh   j  s    zGridLayout._create_idx_iter)(r"   r#   r$   r%   r   r1   r2   r	   r.   r/   r   r*   r+   r   r,   r-   r   rb   rd   Zminimum_widthZminimum_heightr
   r   r   r6   r:   rC   rG   propertyrJ   rK   rM   rg   r   r   r   r   r   rh   __classcell__r   r   r?   r   r   x   s>   			



		



,'5<8,r   N)r%   __all__Zkivy.loggerr   Zkivy.uix.layoutr   Zkivy.propertiesr   r   r   r	   r
   r   r   r   r   mathr   	itertoolsr   r   r   r   operatorr   r   r!   	Exceptionr   r   r   r   r   r   <module>   s   Y,