U
    Pe                     @   sb   d Z ddlmZmZmZ ddlmZ ddlmZ dZ	dd Z
G dd	 d	eZG d
d deeZdS )a  
RecycleView Data Model
======================

.. versionadded:: 1.10.0

The data model part of the RecycleView model-view-controller pattern.

It defines the models (classes) that store the data associated with a
:class:`~kivy.uix.recycleview.RecycleViewBehavior`. Each model (class)
determines how the data is stored and emits requests to the controller
(:class:`~kivy.uix.recycleview.RecycleViewBehavior`) when the data is
modified.
    )ListPropertyObservableDictObjectProperty)EventDispatcher)partial)RecycleDataModelBehaviorRecycleDataModelc                 C   s  t | tst| | d S || }| j| j| j  }}}||krHtddS |d k	r|dkr||ksdt|dk rztd|| }t||}|dk rtd|| }t||}t|||S |dk r|| }|dk r|| }|dk s|dk s||ks||ks||krd S t||S )N   r   )
isinstanceslicestartstopstepAssertionErrormaxmin)vallast_lennew_lenZdiffr   r   r    r   B/tmp/pip-unpacked-wheel-xzebddm3/kivy/uix/recycleview/datamodel.pyrecondition_slice_assign   s0    



 r   c                   @   s8   e Zd ZdZdZedddZdd Zdd	 Zd
d Z	dS )r   a  :class:`RecycleDataModelBehavior` is the base class for the models
    that describes and provides the data for the
    :class:`~kivy.uix.recycleview.RecycleViewBehavior`.

    :Events:
        `on_data_changed`:
            Fired when the data changes. The event may dispatch
            keyword arguments specific to each implementation of the data
            model.
            When dispatched, the event and keyword arguments are forwarded to
            :meth:`~kivy.uix.recycleview.RecycleViewBehavior.refresh_from_data`.
    )on_data_changedNT)Z	allownonec                 C   s   || _ |r| d|j dS )znAssociates a
        :class:`~kivy.uix.recycleview.RecycleViewBehavior` with
        this data model.
        r   N)recycleviewfbindrefresh_from_dataselfrvr   r   r   attach_recycleviewQ   s    z+RecycleDataModelBehavior.attach_recycleviewc                 C   s"   | j }|r| d|j d| _ dS )zxRemoves the
        :class:`~kivy.uix.recycleview.RecycleViewBehavior`
        associated with this data model.
        r   N)r   funbindr   r   r   r   r   detach_recycleviewZ   s    z+RecycleDataModelBehavior.detach_recycleviewc                 O   s   d S Nr   )r   largskwargsr   r   r   r   d   s    z(RecycleDataModelBehavior.on_data_changed)
__name__
__module____qualname____doc__Z
__events__r   r   r   r!   r   r   r   r   r   r   :   s   	
r   c                       s`   e Zd ZdZeg ZdZ fddZdd Ze	dd Z
 fd	d
Z fddZdd Z  ZS )r   ao  An implementation of :class:`RecycleDataModelBehavior` that keeps the
    data in a indexable list. See :attr:`data`.

    When data changes this class currently dispatches `on_data_changed`  with
    one of the following additional keyword arguments.

    `none`: no keyword argument
        With no additional argument it means a generic data change.
    `removed`: a slice or integer
        The value is a slice or integer indicating the indices removed.
    `appended`: a slice
        The slice in :attr:`data` indicating the first and last new items
        (i.e. the slice pointing to the new items added at the end).
    `inserted`: a integer
        The index in :attr:`data` where a new data item was inserted.
    `modified`: a slice
        The slice with the indices where the data has been modified.
        This currently does not allow changing of size etc.
    r   c                    s$   |  d| j tt| jf | d S Ndata)r   _on_data_callbacksuperr   __init__)r   r$   	__class__r   r   r-      s    zRecycleDataModel.__init__c                 C   s
   | j | S r"   )r*   )r   indexr   r   r   __getitem__   s    zRecycleDataModel.__getitem__c                 C   s   t t| jj| S )zA dictionary instance, which when modified will trigger a `data` and
        consequently an `on_data_changed` dispatch.
        )r   r   r/   r*   )r   r   r   r   observable_dict   s    z RecycleDataModel.observable_dictc                    s(   t t| | |r$| d|jd d S r)   )r,   r   r   r   _dispatch_prop_on_sourcer   r.   r   r   r      s    z#RecycleDataModel.attach_recycleviewc                    s,   | j }|r| d|jd tt|   d S r)   )r   r    r3   r,   r   r!   r   r.   r   r   r!      s    z#RecycleDataModel.detach_recycleviewc                 C   s  | j }t| j }| _ |j\}}|dkrZt|||}|d k	rL| jd|d n
| d n4|dkrt| jd|d n|dkrtt| ||}|d k	r| jd|d n
| d n|dkr| jdt| d n|dks|d	kr| jdt||d
 n|dkr| jdt||d
 n||dkr,| jd|d nb|dkrd|rP| jd|d d n| jd|d d n*|dkr| jdt||d
 n
| d d S )N__setitem__r   )modified__delitem__)removedZ__setslice__Z__delslice____iadd____imul__)Zappendedappendinsert)Zinsertedpopr   r	   extend)	_last_lenlenr*   Zlast_opr   dispatchr   )r   instancevaluer   r   opr   r   r   r   r+      s<    




z"RecycleDataModel._on_data_callback)r%   r&   r'   r(   r   r*   r>   r-   r1   propertyr2   r   r!   r+   __classcell__r   r   r.   r   r   h   s   
r   N)r(   Zkivy.propertiesr   r   r   Z
kivy.eventr   	functoolsr   __all__r   objectr   r   r   r   r   r   <module>   s   #.