ó
±éRc        	   @   s,  d  Z  d d l Z d d l Z d d l Z d d l Z d d l j Z d d l j	 Z	 d d d „ Z d d d „ Z d „  Z d e j f d „  ƒ  YZ d d e d	 „ Z d
 e j f d „  ƒ  YZ d e j f d „  ƒ  YZ d d d e d „ Z e d k r(d d l Z d d l Z d d l Z d d l j	 Z	 d Z e j j ƒ  Z g  e e ƒ D] Z e e j d e ƒ ^ qNZ  g  e  D] Z! e! j" d ƒ ^ qwZ# e j$ j% e ƒ Z& e j$ j' e ƒ d Z( e& d k Z) e e ƒ Z* e j+ j, e* e  e# e& e( e) g d d ƒZ- e	 j. e- ƒ Z/ e	 j0 d ƒ e/ d <e	 j1 d d ƒ e/ d <e	 j2 d d ƒ e/ d <e3 d d d d  d! d" d# d$ g ƒ Z4 d% g Z5 e e- d& e/ d' e4 ƒZ6 e e6 d( e5 ƒZ7 d) „  Z8 e6 j9 j: d* e8 ƒ e j; ƒ  Z< e< j= d+ ƒ e< j> e7 ƒ e< j? ƒ  e- j ƒ  Z@ e e@ ƒ \ ZA ZB ZC eC j= d, ƒ e jD ƒ  n  d S(-   s¸  

Some gtk specific tools and widgets

   * rec2gtk          : put record array in GTK treeview - requires gtk

Example usage

    import matplotlib.mlab as mlab
    import mpl_toolkits.gtktools as gtktools

    r = mlab.csv2rec('somefile.csv', checkrows=0)

    formatd = dict(
        weight = mlab.FormatFloat(2),
        change = mlab.FormatPercent(2),
        cost   = mlab.FormatThousands(2),
        )


    exceltools.rec2excel(r, 'test.xls', formatd=formatd)
    mlab.rec2csv(r, 'test.csv', formatd=formatd)


    import gtk
    scroll = gtktools.rec2gtk(r, formatd=formatd)
    win = gtk.Window()
    win.set_size_request(600,800)
    win.add(scroll)
    win.show_all()
    gtk.main()

iÿÿÿÿNc      	   C   s‘   t  j d d d t  j d t  j d |  ƒ } | d k	 rF | j | ƒ n  | d k	 rb | j | ƒ n | j d ƒ | j ƒ  | j ƒ  | j	 ƒ  d S(   sp   
    create an error message dialog with string msg.  Optionally set
    the parent widget and dialog title
    t   parentt   typet   buttonst   message_formats   Error!N(
   t   gtkt   MessageDialogt   Nonet   MESSAGE_ERRORt
   BUTTONS_OKt   set_transient_fort	   set_titlet   showt   runt   destroy(   t   msgR    t   titlet   dialog(    (    sO   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/mpl_toolkits/gtktools.pyt   error_message)   s    				


c      	   C   s„   t  j d d d t  j d t  j d |  ƒ } | d k	 rF | j | ƒ n  | d k	 rb | j | ƒ n  | j ƒ  | j ƒ  | j	 ƒ  d S(   sp   
    create a simple message dialog with string msg.  Optionally set
    the parent widget and dialog title
    R    R   R   R   N(
   R   R   R   t   MESSAGE_INFOR   R	   R
   R   R   R   (   R   R    R   R   (    (    sO   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/mpl_toolkits/gtktools.pyt   simple_message?   s    				


c            s›   |  d k r d St j |  ƒ }  d |  _ d |  _ ‡  f d †  } t |  t j ƒ sd t |  t j ƒ ry | |  _ d |  _ n t |  t j ƒ r— d |  _ n  |  S(   sr   
    copy the format, perform any overrides, and attach an gtk style attrs


    xalign = 0.
    cell = None

    g        c            so   | j  | ˆ  ƒ } y t | ƒ } Wn | j d d ƒ n0 X| d k  r[ | j d d ƒ n | j d d ƒ d  S(   Nt
   foregroundt   blacki    t   red(   t	   get_valuet   floatt   set_property(   t   columnt   cellt   modelt   thisitert   val(   t   colnum(    sO   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/mpl_toolkits/gtktools.pyt   negative_red_cella   s      g      ð?N(	   R   t   copyt   xalignR   t
   isinstancet   mlabt   FormatFloatt	   FormatIntt
   FormatDate(   t   formatR   R    (    (   R   sO   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/mpl_toolkits/gtktools.pyt   gtkformat_factoryS   s    	 		$	t   SortedStringsScrolledWindowc           B   sk   e  Z d  Z d d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d „  Z d	 „  Z d
 „  Z RS(   s‹   
    A simple treeview/liststore assuming all columns are strings.
    Supports ascending/descending sort by clicking on column header
    c            s$  t  j j |  ƒ ˆ  |  _ d	 |  _ |  j t  j ƒ |  j t  j	 t  j	 ƒ t
 j g t ˆ  ƒ } t  j | Œ  ‰ |  _ t  j |  j ƒ } | j ƒ  | j ƒ  j t  j ƒ | j t ƒ d d
 ‡  ‡ f d †  ƒ  Y} | d	 k rç t ƒ  } n  | j ƒ  } xø t ˆ  ƒ D]ê \ } } t  j ƒ  } | | k r4t ƒ  | | <n  | | }	 t  j | | d | ƒ}
 | j d |	 j ƒ | j d t ƒ | j d |  j | ƒ |
 j d | |  | ƒ ƒ |
 j d t ƒ |	 j  d	 k	 rÝ|
 j! | |	 j  ƒ n  | j" |
 ƒ q W| |  _# |
 |  _$ |  j% | ƒ | |  _& |  j' ƒ  d	 S(   s¯   
        xalignd if not None, is a dict mapping col header to xalignent (default 1)

        formatterd if not None, is a dict mapping col header to a ColumnFormatter
        t   Clickedc              s#   e  Z d  „  Z ‡  ‡ f d †  Z RS(   c         S   s   | |  _  | |  _ d |  _ d  S(   Ni    (   R    t   it   num(   t   selfR    R,   (    (    sO   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/mpl_toolkits/gtktools.pyt   __init__•   s    		c            s¹  g  } g  } x– t  |  j j ƒ D]‚ \ } } ˆ j | |  j ƒ } y t | j ƒ  j d ƒ ƒ } Wn t k
 rr n Xt	 j
 | ƒ rŽ t j } n  | j | | f ƒ q W| j ƒ  |  j d sÉ | j ƒ  n  t | Œ  \ } } | j | ƒ |  j j j | ƒ g  }	 x% | D] }
 |	 j |  j j |
 ƒ qW|	 |  j _ xm t  |  j j ƒ D]Y \ }
 } t g  t t ˆ  ƒ ƒ D] } |  j j j | | ƒ ^ qkƒ } | |  j j |
 <qIW|  j d 7_ d  S(   Nt   %i   i   (   t	   enumerateR    t   itersR   R,   R   t   stript   rstript
   ValueErrorR$   t
   safe_isnant   npyt   inft   appendt   sortR-   t   reverset   zipt   extendR   t   reordert   tuplet   ranget   lent   rownumd(   R.   R   t   indt   dsut   rownumR   R   t   valst   otherindt   newitersR,   t   jt   key(   t
   colheadersR   (    sO   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/mpl_toolkits/gtktools.pyt   __call__š   s2       
 =(   t   __name__t
   __module__R/   RL   (    (   RK   R   (    sO   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/mpl_toolkits/gtktools.pyR+   ”   s   	t   textR"   t   editablet   editedt   clickedt	   clickableN(    ((   R   t   ScrolledWindowR/   RK   R   t   seqt   set_shadow_typet   SHADOW_ETCHED_INt
   set_policyt   POLICY_AUTOMATICt   gobjectt   TYPE_STRINGRA   t	   ListStoreR   t   TreeViewR   t   get_selectiont   set_modet   SELECTION_MULTIPLEt   set_rules_hintt   Truet   dictR!   R1   t   CellRendererTextt   ColumnFormattert   TreeViewColumnR   R"   t   connectt   position_editedR   t   set_cell_data_funct   append_columnt
   formatterdt   lastcolt   addt   treeviewt   clear(   R.   RK   Rk   t   typesRn   R+   R,   t   headert   renderert	   formatterR   (    (   RK   R   sO   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/mpl_toolkits/gtktools.pyR/   {   sF    		

!
			c         C   s   | |  j  | | <d  S(   N(   R   (   R.   Rr   t   patht   newtextt   position(    (    sO   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/mpl_toolkits/gtktools.pyRh   Ô   s    c         C   s>   t  ƒ  |  _ g  |  _ t  ƒ  |  _ |  j j ƒ  t  ƒ  |  _ d  S(   N(   Rc   t   iterdR2   RB   R   Ro   t   datad(   R.   (    (    sO   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/mpl_toolkits/gtktools.pyRo   Ø   s
    	c         C   s\   g  } xO t  | ƒ D]A \ } } |  j j |  j | ƒ } | j | | j | ƒ g ƒ q W| S(   N(   R1   Rk   t   getRK   R=   t   tostr(   R.   t   rowRU   R,   R   Rs   (    (    sO   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/mpl_toolkits/gtktools.pyt   flatà   s
     c         G   sÍ   t  g  |  j j ƒ  D] \ } } | | f ^ q ƒ } xa |  j ƒ  D]S } t | ƒ } |  j | } |  j j | ƒ |  j | =|  j | =|  j j | ƒ qA Wx. t	 |  j ƒ D] \ } } | | |  j
 | <q¨ Wd  S(   N(   Rc   Rw   t   valuest   get_selectedR?   R   t   removeRx   R2   R1   RB   (   R.   t   unusedRJ   R   t   keydR{   R,   (    (    sO   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/mpl_toolkits/gtktools.pyt   __delete_selectedç   s    4

c         C   s    t  | ƒ } |  j | } |  j j | ƒ |  j | =|  j | =| |  j t |  j ƒ <|  j j | ƒ x6 |  j j ƒ  D]% \ } } | | k rs |  j | =qs qs Wd  S(   N(	   R?   Rw   R   R   Rx   RB   RA   R2   t   items(   R.   R{   RJ   R   RE   t   thiskey(    (    sO   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/mpl_toolkits/gtktools.pyt
   delete_rowø   s    

 c         C   s{   |  j  j ƒ  } |  j  j | |  j | ƒ Œ t | ƒ } | |  j | <| |  j | <| |  j t |  j	 ƒ <|  j	 j | ƒ d  S(   N(
   R   R9   t   setR|   R?   Rx   Rw   RB   RA   R2   (   R.   R{   R   RJ   (    (    sO   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/mpl_toolkits/gtktools.pyt   add_row  s    c         C   s�   |  j  | } |  j | } t | ƒ } | |  j  | <|  j | =|  j | =| |  j | <| |  j | <|  j j | |  j | ƒ Œ d  S(   N(   RB   Rw   R?   Rx   R   R†   R|   (   R.   RE   t   newrowRJ   R   t   newkey(    (    sO   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/mpl_toolkits/gtktools.pyt
   update_row  s    

c         C   s   |  j  | } |  j | S(   N(   RB   Rx   (   R.   RE   RJ   (    (    sO   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/mpl_toolkits/gtktools.pyt   get_row  s    c         C   s,   g  } d „  } |  j  j ƒ  j | | ƒ | S(   Nc         S   s   | j  |  j | d ƒ ƒ d  S(   Ni    (   R9   R   (   R   Rt   t   itert   selected(    (    sO   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/mpl_toolkits/gtktools.pyt   foreach#  s    (   Rn   R^   t   selected_foreach(   R.   R�   RŽ   (    (    sO   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/mpl_toolkits/gtktools.pyR~   !  s    	N(   RM   RN   t   __doc__R   R/   Rh   Ro   R|   t-   _SortedStringsScrolledWindow__delete_selectedR…   R‡   RŠ   R‹   R~   (    (    (    sO   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/mpl_toolkits/gtktools.pyR*   u   s   Y									i    c         C   s@  | d k r t ƒ  } n  g  } x‚ t |  j j ƒ D]n \ } } |  j | } | j | ƒ } | d k r† t j j | j t j	 ƒ  ƒ } n  t
 | | ƒ } | | | <q1 W|  j j }	 t |	 | ƒ }
 t j t |  j j ƒ ƒ } x |  D] } |
 j | ƒ qà W| r<t j ƒ  } | j d d ƒ | j |
 ƒ | j ƒ  | |
 _ n  |
 S(   sÉ  
    formatd is a dictionary mapping dtype name -> mlab.Format instances

    This function creates a SortedStringsScrolledWindow (derived
    from gtk.ScrolledWindow) and returns it.  if autowin is True,
    a gtk.Window is created, attached to the
    SortedStringsScrolledWindow instance, shown and returned.  If
    autowin=False, the caller is responsible for adding the
    SortedStringsScrolledWindow instance to a gtk widget and
    showing it.
    i   iX  N(   R   Rc   R1   t   dtypet   namesRy   R$   t   defaultformatdR   t	   FormatObjR)   R*   R7   t   arangeRA   R‡   R   t   Windowt   set_default_sizeRm   t   show_allt   win(   t   rt   formatdRE   t   autowint   formatsR,   t   namet   dtR(   RK   t   scrollRC   R{   Rš   (    (    sO   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/mpl_toolkits/gtktools.pyt   rec2gtk+  s,    !
t   RecListStorec           B   s/   e  Z d  Z d d d „ Z d „  Z d „  Z RS(   s}  
    A liststore as a model of an editable record array.

    attributes:

     * r - the record array with the edited values

     * formatd - the list of mlab.FormatObj instances, with gtk attachments

     * stringd - a dict mapping dtype names to a list of valid strings for the combo drop downs

     * callbacks - a matplotlib.cbook.CallbackRegistry.  Connect to the cell_changed with

        def mycallback(liststore, rownum, colname, oldval, newval):
           print 'verify: old=%s, new=%s, rec=%s'%(oldval, newval, liststore.r[rownum][colname])

        cid = liststore.callbacks.connect('cell_changed', mycallback)

        c         C   sÈ  | d k r t ƒ  } n  | d k r6 t j | ƒ } n  | |  _ t j d g ƒ |  _ | |  _ | j	 j
 |  _ g  t |  j ƒ D]- \ } } t | j | t j ƒ  ƒ | ƒ ^ q| |  _ t t |  j |  j ƒ ƒ |  _ } g  } xF |  j D]; } t | t j ƒ r| j t j ƒ qä | j t j ƒ qä Wt ƒ  |  _ t | ƒ r	| j t j g t | ƒ ƒ | j ƒ  } | j ƒ  t | j	 j
 ƒ }	 x† t | ƒ D]u \ }
 } | |	 k s«t  ‚ t! j" t j ƒ } x" | | D] } | j | g ƒ qÈW| t |  j ƒ |
 f |  j | <q�Wn  t! j" j# |  | Œ x¥ | D]� } g  } xX t |  j | ƒ D]D \ } } t | t j ƒ rp| j | ƒ qB| j | j$ | ƒ ƒ qBWt | ƒ r³| j d g t | ƒ ƒ n  |  j | ƒ q#Wd S(   sò   
        r is a numpy record array

        formatd is a dict mapping dtype name to mlab.FormatObj instances

        stringd, if not None, is a dict mapping dtype names to a list of
        valid strings for a combo drop down editor
        t   cell_changedi    N(%   R   Rc   R$   t   get_formatdt   stringdt   cbookt   CallbackRegistryt	   callbacksR›   R’   R“   t   headersR1   R)   Ry   R•   Rž   R<   Rœ   R#   t
   FormatBoolR9   RZ   t   TYPE_BOOLEANR[   t   combodRA   R=   t   TYPE_INTt   keysR:   R†   t   AssertionErrorR   R\   R/   Rz   (   R.   R›   Rœ   R¦   R,   RŸ   Rp   R(   R¯   t   validt   ikeyRJ   t
   combostoret   sR{   RF   Rs   R   (    (    sO   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/mpl_toolkits/gtktools.pyR/   o  sL    
		C"
'c         C   sÙ   t  | ƒ } |  j | } t  | ƒ } |  j | } |  j | | } y | j | ƒ }	 Wn5 t k
 r� t j d | ƒ }
 t |
 d d ƒd  SX|	 |  j | | <| j	 |	 ƒ |  | | <|  j
 j d |  | | | |	 ƒ d  S(   Ns   Error converting "%s"R   t   ErrorR¤   (   t   intRž   Rª   R›   t   fromstrR5   R§   t   exception_to_strR   Rz   R©   t   process(   R.   Rr   Rt   Ru   Rv   R(   RE   t   colnamet   oldvalt   newvalR   (    (    sO   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/mpl_toolkits/gtktools.pyRh   °  s     c   	      C   s’   t  | ƒ } |  j | } | j ƒ  } t  | ƒ } |  j | } |  j | | } | |  j | | <| |  | | <|  j j d |  | | | | ƒ d  S(   NR¤   (   R¶   Rž   t
   get_activeRª   R›   R©   R¹   (	   R.   t   cellrendererRt   Rv   R(   R¼   RE   Rº   R»   (    (    sO   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/mpl_toolkits/gtktools.pyt   position_toggledÄ  s    N(   RM   RN   R�   R   R/   Rh   R¿   (    (    (    sO   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/mpl_toolkits/gtktools.pyR£   [  s   A	t   RecTreeViewc           B   s,   e  Z d  Z d d „ Z d „  Z d „  Z RS(   s8   
    An editable tree view widget for record arrays
    c         C   sÇ  | |  _  t j j |  | ƒ t | j j ƒ  ƒ } | d k rF g  } n  t | ƒ } x/t | j	 ƒ D]\ } } | j
 | } | j | ƒ } | t j k rÝ t j ƒ  } | | k r¤| j d | j | ƒ | j d t ƒ q¤nÇ | | k rAt j ƒ  } | j d | j | ƒ | j | \ }	 }
 | j d |	 ƒ | j d t ƒ nc t j ƒ  } | | k r‚| j d | j | ƒ | j d t ƒ n  | d k	 r¤| j d | j ƒ n  t j | ƒ } |  j | ƒ | j | t ƒ | t j k r*| j | d | ƒ | | k rO| j | \ }	 }
 | j | d |
 ƒ qOn% | t j k rO| j | d	 | ƒ n  | d k	 rb | j d k	 rb | j | | j ƒ qb qb W|  j d
 |  j ƒ |  j  ƒ  j! t j" ƒ |  j  ƒ  j# |  j$ ƒ d S(   s�   
        build a gtk.TreeView to edit a RecListStore

        constant, if not None, is a list of dtype names which are not editable
        t   toggledt   activatableRQ   R   RP   R"   RO   s   text-columnt   actives   button-release-eventN(%   t   recliststoreR   R]   R/   R†   R¦   R¯   R   R1   Rª   Rœ   t   get_column_typeRZ   R¬   t   CellRendererToggleRg   R¿   R   Rb   t   CellRendererComboRh   R­   Rd   R"   Rf   Rj   t
   pack_startR[   t   add_attributeR   Ri   t   on_selection_changedR^   R_   t   SELECTION_BROWSEt   set_select_functiont	   on_select(   R.   RÄ   t   constantt   combostringsR,   Rq   Rs   t   coltypeRr   R³   t   listindt   tvcol(    (    sO   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/mpl_toolkits/gtktools.pyR/   Û  sR    		c         G   s   t  S(   N(   t   False(   R.   t   args(    (    sO   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/mpl_toolkits/gtktools.pyRÍ   #  s    c         G   sT   |  j  ƒ  \ } } | j ƒ  d } t | t j ƒ rP |  j | | | d t ƒn  d  S(   Ni    t   start_editing(   t
   get_cursort   get_cell_renderersR#   R   Rd   t   set_cursor_on_cellRb   (   R.   RÔ   Rt   t   colt   ren(    (    sO   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/mpl_toolkits/gtktools.pyRÊ   &  s    N(   RM   RN   R�   R   R/   RÍ   RÊ   (    (    (    sO   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/mpl_toolkits/gtktools.pyRÀ   ×  s   H	c         C   sn   t  |  d | d | ƒ} t | d | ƒ} | r` t j ƒ  } | j | ƒ | j ƒ  | | | f S| | f Sd S(   s  
    create a RecListStore and RecTreeView and return them.

    If autowin is True, create a gtk.Window, insert the treeview into
    it, and return it (return value will be (liststore, treeview, win)

    See RecListStore and RecTreeView for a description of the keyword args
    Rœ   R¦   RÎ   N(   R£   RÀ   R   R—   Rm   R™   (   R›   Rœ   R¦   RÎ   R�   t	   liststoreRn   Rš   (    (    sO   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/mpl_toolkits/gtktools.pyt   edit_recarray,  s    

t   __main__i
   t   dayss   %ag    ÐcAR“   s&   clientid,date,weekdays,gains,prices,ups   %Y-%m-%dt   datet	   precisioni   t   pricesi   t   gaint   weekdayst   Sunt   Mont   Tuet   Wedt   Thut   Frit   Satt   clientidRœ   R¦   RÎ   c         C   s!   d | | |  j  | | f GHd  S(   Ns   verify: old=%s, new=%s, rec=%s(   R›   (   RÛ   RE   Rº   R»   R¼   (    (    sO   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/mpl_toolkits/gtktools.pyt
   mycallbackd  s    R¤   s   with full customizations   with all defaults(E   R�   R!   R   RZ   t   numpyR7   t   matplotlib.cbookR§   t   matplotlib.mlabR$   R   R   R   R)   RT   R*   Rb   R¢   R\   R£   R]   RÀ   RÜ   RM   t   datetimet   npt   NRß   t   todayR@   R,   t	   timedeltat   datest   dt   strftimeRã   t   randomt   randnt   gainst   randRá   t   upRë   t   rect
   fromarraysR›   R¥   Rœ   R'   t   FormatMillionst   FormatPercentRc   R¦   RÎ   RÛ   Rn   Rì   R©   Rg   R—   Rš   R
   Rm   R™   t   r2t   lst   tvt   win2t   main(    (    (    sO   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/mpl_toolkits/gtktools.pyt   <module>!   s\   	"¶0|U/"!	$		
