U
    Pe                     @   sp   d Z dZddlmZ ddlmZ ddlmZmZm	Z	 ddl
mZ ddlmZ G dd	 d	eZG d
d deZdS )a  
Spinner
=======

.. versionadded:: 1.4.0

.. image:: images/spinner.jpg
    :align: right

Spinner is a widget that provides a quick way to select one value from a set.
In the default state, a spinner shows its currently selected value.
Touching the spinner displays a dropdown menu with all the other available
values from which the user can select a new one.

Example::

    from kivy.base import runTouchApp
    from kivy.uix.spinner import Spinner

    spinner = Spinner(
        # default value shown
        text='Home',
        # available values
        values=('Home', 'Work', 'Other', 'Custom'),
        # just for positioning in our example
        size_hint=(None, None),
        size=(100, 44),
        pos_hint={'center_x': .5, 'center_y': .5})

    def show_selected_value(spinner, text):
        print('The spinner', spinner, 'has text', text)

    spinner.bind(text=show_selected_value)

    runTouchApp(spinner)


Kv Example::

    FloatLayout:
        Spinner:
            size_hint: None, None
            size: 100, 44
            pos_hint: {'center': (.5, .5)}
            text: 'Home'
            values: 'Home', 'Work', 'Other', 'Custom'
            on_text:
                print("The spinner {} has text {}".format(self, self.text))
)SpinnerSpinnerOption    )string_types)Factory)ListPropertyObjectPropertyBooleanProperty)Button)DropDownc                   @   s   e Zd ZdZdS )r   zSpecial button used in the :class:`Spinner` dropdown list. By default,
    this is just a :class:`~kivy.uix.button.Button` with a size_hint_y of None
    and a height of :meth:`48dp <kivy.metrics.dp>`.
    N)__name__
__module____qualname____doc__ r   r   4/tmp/pip-unpacked-wheel-xzebddm3/kivy/uix/spinner.pyr   <   s   r   c                       s   e Zd ZdZe ZedZee	Z
eeZedZedZ f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   zBSpinner class, see module documentation for more information.
    Fc                    sr   d | _ tt| jf | | j}| j}|d| j |d| |d| |d| j |d| j |d| j |  d S )N
on_releasedropdown_cls
option_clsvaluessizetext_autoupdate)		_dropdownsuperr   __init__fbind_build_dropdown_toggle_dropdown_update_dropdown_update_dropdown_size)selfkwargsr   Zbuild_dropdown	__class__r   r   r      s    

zSpinner.__init__c                 G   s   | j r6| j j| jd | j j| jd | j   d | _ | j}t|trPt	|}| | _ | j j
| jd | j j
| jd |   d S )N)Z	on_select)Z
on_dismiss)r   Zunbind_on_dropdown_select_close_dropdowndismissr   
isinstancer   r   getbindr   )r   largsclsr   r   r   r      s    


zSpinner._build_dropdownc                 G   sJ   | j s
d S | j}|sd S |j}|s&d S | j}|jd d  D ]
}||_q:d S N)sync_heightr   	containerheightchildren)r   r)   dpr-   hitemr   r   r   r      s    zSpinner._update_dropdown_sizec                    s   | j  | j}| j}| j}t|tr,t|}   |D ]@}||d}| j	rR| j
n|j
|_
|j fddd  | q8|r|r| jr| j|kr|d | _nd| _d S )N)textc                    s     | jS r+   )selectr3   )optionr0   r   r   <lambda>       z*Spinner._update_dropdown.<locals>.<lambda>)r   r    )r   r   r   r   r&   r   r   r'   Zclear_widgetsr,   r.   r(   Z
add_widgetr3   )r   r)   r*   r   r   valuer2   r   r6   r   r      s"    


zSpinner._update_dropdownc                 G   s   | j r| j | _d S r+   )r   is_openr   r)   r   r   r   r      s    zSpinner._toggle_dropdownc                 G   s
   d| _ d S NF)r;   r<   r   r   r   r$      s    zSpinner._close_dropdownc                 G   s   || _ d| _d S r=   )r3   r;   )r   instancedatar)   r   r   r   r#      s    zSpinner._on_dropdown_selectc                 C   s(   |r| j |  n| j jr$| j   d S r+   )r   openZ	attach_tor%   )r   r>   r:   r   r   r   
on_is_open   s    zSpinner.on_is_open)r   r   r   r   r   r   r   r   r   r   r   r
   r   r;   r,   r   r   r   r   r   r$   r#   rA   __classcell__r   r   r!   r   r   D   s   	r   N)r   __all__Zkivy.compatr   Zkivy.factoryr   Zkivy.propertiesr   r   r   Zkivy.uix.buttonr	   Zkivy.uix.dropdownr
   r   r   r   r   r   r   <module>   s   2