ó
ø`9Sc           @   sê   d  Z  d d l Td d l Td d l m Z d d l m Z d d d d g Z d	 e f d
 „  ƒ  YZ d e f d „  ƒ  YZ	 d e f d „  ƒ  YZ
 d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d S(   s<   Editing widgets with built-in error and constraint checking.iÿÿÿÿ(   t   *(   t   gui(   t   txtt   NumberEditWidgett   StringEditWidgett   ComboBoxEditWidgett   BoolEditWidgett
   EditWidgetc           B   s   e  Z d  Z e ƒ  Z e ƒ  Z d d „ Z d „  Z d „  Z	 d „  Z
 d „  Z d „  Z d „  Z d „  Z d	 „  Z d
 „  Z d „  Z d „  Z RS(   s  Base class defining interface and layout for an edit widget capable of
    displaying an error message and having a built-in mechanism for constraint
    checking.

    Signals:
      - 'valueChanged': the value being edited has been modified or became
        unavailable due to syntax or constraint checking errors.
      - 'returnPressed': Return or Enter key has been pressed by the user.

    Use 'setValue' to manually set the value to be displayed in the widget and
    'getValue' to retrieve the modified value edited by the user, unless some
    constraints are not met or a custom error message has been set on the
    widget.

    Redefine 'updateEditWidget' method to provide the functionlality to pass a
    value from the outside world to the actual edit widget. Use 'updateValue'
    for the inverse operation, calling this method from the editing slots to be
    connected in 'createEditWidget' whenever the value has been interactively
    modified by the user. Redefine 'getErrorMessage' to provide the
    functionality for constraint checking.

    You don't have to manually call the 'valueChanged' signal from any of the
    methods to be redefined. However, responsibility for implementation of the
    'returnPressed' signal lies completely on the derived class.c         C   sÐ   t  j |  | ƒ d  |  _ t |  _ t ƒ  } |  j ƒ  |  _ | j	 |  j ƒ t
 j d ƒ } t |  j j ƒ  ƒ j ƒ  } t ƒ  |  _ |  j j | j | ƒ ƒ | j	 |  j ƒ | j d d d d ƒ |  j | ƒ d  S(   Nt   warningi    (   t   QWidgett   __init__t   Nonet   valuet   Falset   hasCustomErrorMessaget   QHBoxLayoutt   createEditWidgett   editt	   addWidgetR   t   loadIcont   QFontMetricst   fontt   heightt   QLabelt
   errorLabelt	   setPixmapt   pixmapt   setContentsMarginst	   setLayout(   t   selft   parentt   layoutt	   errorIcont   errorIconHeight(    (    s   code\common\EditWidgets.pyR
   =   s    			c         C   s   |  j  S(   s0   Return the widget that actually edits the value.(   R   (   R   (    (    s   code\common\EditWidgets.pyt   getEditWidgetX   s    c         C   s   |  j  j ƒ  S(   sv   Check if the value being currently displayed by the widget is valid
        and meets any of the required constraints.(   R   t	   isVisible(   R   (    (    s   code\common\EditWidgets.pyt   hasValue]   s    c         C   s   |  j  ƒ  r |  j Sd Sd S(   sÄ   Return the current widget value converted to its genuine data type
        (e.g. 'int' for an integer editor) or 'None' if the current value is
        invalid or does not satisfy the constraints.N(   R$   R   R   (   R   (    (    s   code\common\EditWidgets.pyt   getValuec   s    c         C   s   |  j  | ƒ |  j | ƒ d S(   s  Manually set the value to be displayed by the widget.

        If the value is 'None' or does not meet the constraints, still modify
        the edit widget in a suitable way, but turn the error status on (so
        that an error icon is displayed and 'hasValue' returns 'False').N(   t   updateEditWidgett   updateValue(   R   R   (    (    s   code\common\EditWidgets.pyt   setValuem   s    c         C   s-   |  j  j t ƒ |  j  j | ƒ t |  _ d S(   s  Assign to the widget an error message that is not related to any of
        the built-in constraint checking failures.

        Custom error messages have greater priority than the ordinary ones. Use
        'clearCustomErrorMessage' to return to normal constraint checking.N(   R   t
   setVisiblet   Truet
   setToolTipR   (   R   t   message(    (    s   code\common\EditWidgets.pyt   setCustomErrorMessagew   s    c         C   s   t  |  _ |  j ƒ  d S(   s?   Clear the error message set by 'setCustomErrorMessage', if any.N(   R   R   t   updateErrorMessage(   R   (    (    s   code\common\EditWidgets.pyt   clearCustomErrorMessageƒ   s    	c         C   s   t  ƒ  S(   s  Create a widget capable of actually editing a value of the required
        type and connect the required slots to it, so that interactive
        modifications of the widget's content, as well as Enter key presses,
        could be tracked.

        Return the created edit widget.(   R	   (   R   (    (    s   code\common\EditWidgets.pyR   Š   s    c         C   s   d S(   s¼   Modify the widget content displayed to the user so that it matches
        the given 'value' of the genuine data type associated with the widget
        (e.g. 'int' for an integer editor).N(    (   R   R   (    (    s   code\common\EditWidgets.pyR&   ”   s    c         C   s   d S(   sÏ   Check if the given 'value' (of the genuine widget data type)
        violates any of the constraints defined by the widget and return an
        appropriate error string if it does. Otherwise, return 'None'.N(   R   (   R   R   (    (    s   code\common\EditWidgets.pyt   getErrorMessage›   s    c         C   s$   | |  _  |  j ƒ  |  j j ƒ  d S(   sJ  Assign a value (of the genuine widget data type) to the widget and
        check it against the constraints (by means of a 'getErrorMessage'
        call), but don't modify the widget content displayed to the user.

        This function has to be called in response to interactive modifications
        of the value being edited.N(   R   R.   t   valueChangedt   emit(   R   R   (    (    s   code\common\EditWidgets.pyR'   £   s    	
c         C   sr   |  j  r d S|  j |  j ƒ } | d k	 rN |  j j t ƒ |  j j | ƒ n  |  j j t ƒ |  j j d ƒ d S(   sm   Show the error label if 'self.value' does not satisfy some of the
        constraints, and hide it otherwise.Nt    (	   R   R0   R   R   R   R)   R*   R+   R   (   R   t   errorMessage(    (    s   code\common\EditWidgets.pyR.   °   s    	N(   t   __name__t
   __module__t   __doc__t
   pyqtSignalR1   t   returnPressedR   R
   R"   R$   R%   R(   R-   R/   R   R&   R0   R'   R.   (    (    (    s   code\common\EditWidgets.pyR      s   					
	
			
			c           B   sV   e  Z d  Z d d e e e d d „ Z d „  Z d „  Z d „  Z	 d „  Z
 d „  Z RS(   sÌ  Floating point or integer value editor with built-in error and
    constraint checking.

    Attributes:
      - 'minValue', 'maxValue': constraints defining lower and upper boundaries
        for the number being edited.
      - 'isMinStrict', 'isMaxStrict': 'True' if the respective boundary values
        themselves are not acceptable.
      - 'isFloatingPoint': 'True' if the value being edited is a floating point
        and 'False' if it is an integer.c         C   sÎ   t  j |  | ƒ | r[ | d  k s7 t | t ƒ s7 t ‚ | d  k s t | t ƒ s t ‚ nB | d  k s| t | t ƒ s| t ‚ | d  k s t | t ƒ s t ‚ | |  _ | |  _ | |  _	 | |  _
 | |  _ d  S(   N(   R   R
   R   t
   isinstancet   floatt   AssertionErrort   intt   minValuet   maxValuet   isMinStrictt   isMaxStrictt   isFloatingPoint(   R   R>   R?   R@   RA   RB   R   (    (    s   code\common\EditWidgets.pyR
   Ï   s    !$!!				c         C   s3   t  ƒ  } | j j |  j ƒ | j j |  j ƒ | S(   N(   t	   QLineEditt
   textEditedt   connectt   onTextEditedR9   t   onReturnPressed(   R   R   (    (    s   code\common\EditWidgets.pyR   ã   s    	c         C   s9   | d  k r |  j j d ƒ n |  j j t | ƒ ƒ d  S(   NR3   (   R   R   t   setTextt   str(   R   R   (    (    s   code\common\EditWidgets.pyR&   ë   s    c         C   s  | d  k r  |  j r d Sd Sn  |  j rA t | t ƒ sV t ‚ n t | t ƒ sV t ‚ |  j d  k	 rˆ |  j rˆ | |  j k rˆ d |  j S|  j d  k	 r± | |  j k  r± d |  j S|  j d  k	 rã |  j	 rã | |  j k rã d |  j S|  j d  k	 r| |  j k rd |  j Sd  S(   Ns6   The value entered is not a valid floating point numbers#   The value entered is not an integers"   This value must be greater than %gs.   This value must be greater than or equal to %gs   This value must be less than %gs+   This value must be less than or equal to %g(
   R   RB   R:   R;   R<   R=   R>   R@   R?   RA   (   R   R   (    (    s   code\common\EditWidgets.pyR0   ô   s(    		c         C   s;   |  j  r t j | ƒ } n t j | ƒ } |  j | ƒ d  S(   N(   RB   R   t   stringToFloatt   stringToIntR'   (   R   t   textR   (    (    s   code\common\EditWidgets.pyRF     s    	c         C   s   |  j  j ƒ  d  S(   N(   R9   R2   (   R   (    (    s   code\common\EditWidgets.pyRG      s    N(   R5   R6   R7   R   R   R*   R
   R   R&   R0   RF   RG   (    (    (    s   code\common\EditWidgets.pyR   Â   s   
					"	
c           B   sM   e  Z d  Z d d d d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z	 RS(   sê   ASCII string editor with built-in error and constraint checking.

    Attributes:
      - 'minLength', 'maxLength': minimal and maximal character counts for the
        string being edited or 'None' if string length is not restricted.c         C   sŒ   t  j |  | ƒ | d  k s. | d k s. t ‚ | d  k sL | d k sL t ‚ | d  k sv | d  k sv | | k sv t ‚ | |  _ | |  _ d  S(   Ni    (   R   R
   R   R<   t	   minLengtht	   maxLength(   R   RM   RN   R   (    (    s   code\common\EditWidgets.pyR
   ,  s    *	c         C   s3   t  ƒ  } | j j |  j ƒ | j j |  j ƒ | S(   N(   RC   RD   RE   RF   R9   RG   (   R   R   (    (    s   code\common\EditWidgets.pyR   9  s    	c         C   sH   | d  k r |  j j d ƒ n% t | t ƒ s4 t ‚ |  j j | ƒ d  S(   NR3   (   R   R   RH   R:   RI   R<   (   R   R   (    (    s   code\common\EditWidgets.pyR&   A  s    c         C   s   | d  k r d St | t ƒ s% t ‚ |  j d  k	 rj t | ƒ |  j k  rj |  j d k r\ d Sd |  j Sn  |  j d  k	 r™ t | ƒ |  j k r™ d |  j Sd  S(   Ns-   The value entered is not a valid ASCII stringi   s%   This value must be a non-empty strings.   This string may contain at least %d characterss-   This string may contain at most %d characters(   R   R:   RI   R<   RM   t   lenRN   (   R   R   (    (    s   code\common\EditWidgets.pyR0   J  s    $$c         C   s;   y t  | ƒ } Wn t k
 r) d  } n X|  j | ƒ d  S(   N(   RI   t
   ValueErrorR   R'   (   R   RL   R   (    (    s   code\common\EditWidgets.pyRF   b  s
    
c         C   s   |  j  j ƒ  d  S(   N(   R9   R2   (   R   (    (    s   code\common\EditWidgets.pyRG   l  s    N(
   R5   R6   R7   R   R
   R   R&   R0   RF   RG   (    (    (    s   code\common\EditWidgets.pyR   $  s   					
t   ReturnSignallingComboBoxc           B   s    e  Z d  Z e ƒ  Z d „  Z RS(   sŠ   A 'QComboBox' capable of detecting Enter key presses (passing these
    events to the outer world by means of the 'returnPressed' signal).c         C   sB   t  j |  | ƒ | j ƒ  t j t j f k r> |  j j ƒ  n  d  S(   N(   t	   QComboBoxt   keyPressEventt   keyt   Qtt
   Key_Returnt	   Key_EnterR9   R2   (   R   t   event(    (    s   code\common\EditWidgets.pyRS   x  s    (   R5   R6   R7   R8   R9   RS   (    (    (    s   code\common\EditWidgets.pyRQ   p  s   	c           B   sA   e  Z d  Z d d „ Z d „  Z d „  Z e d ƒ d „  ƒ Z RS(   s­   An 'EditWidget' capable of selecting an item from the given fixed list
    of strings.

    Attributes:
      - 'admissibleValues': list of strings to select the value from.c         C   sh   t  j |  d d  ƒ| |  _ xE |  j D]: } t | t ƒ sP t | t ƒ sP t ‚ |  j j	 | ƒ q& Wd  S(   NR   (
   R   R
   R   t   admissibleValuesR:   RI   t   unicodeR<   R   t   addItem(   R   RY   R   R   (    (    s   code\common\EditWidgets.pyR
   ‰  s
    	$c         C   s3   t  ƒ  } | j j |  j ƒ | j j |  j ƒ | S(   N(   RQ   t   currentIndexChangedRE   t   onIndexChangedR9   (   R   R   (    (    s   code\common\EditWidgets.pyR   –  s    	c         C   s5   | |  j  k s t ‚ |  j j |  j j | ƒ ƒ d  S(   N(   RY   R<   R   t   setCurrentIndext   findText(   R   R   (    (    s   code\common\EditWidgets.pyR&   Ÿ  s    R=   c         C   s9   |  j  | } | |  j j ƒ  k s( t ‚ |  j | ƒ d  S(   N(   RY   R   t   currentTextR<   R'   (   R   t   indexR   (    (    s   code\common\EditWidgets.pyR]   ¥  s    N(	   R5   R6   R7   R   R
   R   R&   t   pyqtSlotR]   (    (    (    s   code\common\EditWidgets.pyR     s
   			t   ReturnSignallingCheckBoxc           B   s    e  Z d  Z e ƒ  Z d „  Z RS(   sŠ   A 'QCheckBox' capable of detecting Enter key presses (passing these
    events to the outer world by means of the 'returnPressed' signal).c         C   sB   t  j |  | ƒ | j ƒ  t j t j f k r> |  j j ƒ  n  d  S(   N(   t	   QCheckBoxRS   RT   RU   RV   RW   R9   R2   (   R   RX   (    (    s   code\common\EditWidgets.pyRS   µ  s    (   R5   R6   R7   R8   R9   RS   (    (    (    s   code\common\EditWidgets.pyRc   ­  s   	c           B   sA   e  Z d  Z d d „ Z d „  Z d „  Z e d ƒ d „  ƒ Z RS(   s)   An 'EditWidget' for the 'bool' data type.c         C   s'   t  j |  d d ƒ|  j j | ƒ d S(   sl   Parameters:

          - 'label': text to be displayed along the checkbox implementing the
            edit.R   N(   R   R
   R   R   RH   (   R   t   labelR   (    (    s   code\common\EditWidgets.pyR
   Â  s    c         C   s3   t  ƒ  } | j j |  j ƒ | j j |  j ƒ | S(   N(   Rc   t   stateChangedRE   t   onStateChangedR9   (   R   R   (    (    s   code\common\EditWidgets.pyR   Î  s    	c         C   s&   |  j  j | r t j n t j ƒ d  S(   N(   R   t   setCheckStateRU   t   Checkedt	   Unchecked(   R   R   (    (    s   code\common\EditWidgets.pyR&   ×  s    R=   c         C   sD   | t  j k r t } n | t  j k s- t ‚ t } |  j | ƒ d  S(   N(   RU   Rj   R   Ri   R<   R*   R'   (   R   t   stateR   (    (    s   code\common\EditWidgets.pyRg   Ü  s
    	N(	   R5   R6   R7   R   R
   R   R&   Rb   Rg   (    (    (    s   code\common\EditWidgets.pyR   ¾  s
   			N(   R7   t   PyQt4.QtCoret   PyQt4.QtGuit   common.utilsR   R   t   __all__R	   R   R   R   RR   RQ   R   Rd   Rc   R   (    (    (    s   code\common\EditWidgets.pyt   <module>   s   

		¤bL,