
    Jea                     ,   d dl Z d dlZd dlZd dlZd dlmZ d dlmZmZm	Z	 d dl
mZmZmZmZmZmZmZ d dlmZmZmZmZ d dlmZmZmZ d dlmZmZm 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, d dl-m.Z. dZ/dZ0 G d de      Z1 G d de1      Z2y)    N)to_qvariant)	QDateTimeQtSignal)QAbstractItemDelegate	QDateEditQDateTimeEditQItemDelegate	QLineEditQMessageBox
QTableView)
FakeObjectnumpypandasPIL)display_to_valueis_editable_typeis_known_type)_
is_pynsistrunning_in_mac_app)DEFAULT_SMALL_DELTA)get_font)is_binary_stringis_text_stringto_text_stringArrayEditorDataFrameEditor)
TextEditorg     j@g    SAc                        e Zd ZdZ e       Z e       Z e       ZddZd Z	d Z
d ZddZd Zd Zd	 Zd
 Zd Zd Zd Z fdZ xZS )CollectionsDelegatezCollectionsEditor Item Delegatec                 >    t        j                  | |       i | _        y N)r
   __init___editorsselfparents     ]/usr/lib/python3/dist-packages/spyder/plugins/variableexplorer/widgets/collectionsdelegate.pyr&   zCollectionsDelegate.__init__/   s    tV,    c                 b    |j                         r|j                         j                  |      S y r%   )isValidmodel	get_valuer)   indexs     r+   r0   zCollectionsDelegate.get_value3   s'    ==?;;=**511 r,   c                 f    |j                         r!|j                         j                  ||       y y r%   r.   r/   	set_valuer)   r2   values      r+   r5   zCollectionsDelegate.set_value7   &    ==?KKM##E51 r,   c                     |j                  |j                         d      j                         }|j                  |j                         d      j                         }|dv rt        |      t        kD  ryy
|dv sd|v sd|v rj	 |j                  d      j                  d	      D cg c]  }|st        |       }}t        j                  t        j                  |      }|t        kD  ry	 y
y
c c}w # t        $ r Y y
w xY w)a  
        Decide if showing a warning when the user is trying to view
        a big variable associated to a Tablemodel index.

        This avoids getting the variables' value to know its
        size and type, using instead those already computed by
        the TableModel.

        The problem is when a variable is too big, it can take a
        lot of time just to get its value.
              )listsettupledictT)	DataFrameSeriesArrayIndexz(),F)siblingrowdataintLARGE_COLLECTIONstripsplit	functoolsreduceoperatormulLARGE_ARRAY	Exception)r)   r2   val_typeval_sizesshapesizes          r+   show_warningz CollectionsDelegate.show_warning;   s     ==a0557==a0557778}//  11W5H8#)1)=)C)CC)HNAAQNN ''e<+% &
  O  s*   "C1 $C,,C,9/C1 ,C1 1	C=<C=c           
         |j                  |j                         d      j                         }| j                  j	                          |j                         dk  ry| j                  |      rt        j                  | j                         t        d      t        d      t        j                  t        j                  z        }|t        j                  k(  r| j                  j	                          y	 | j                  |      y	 |j1                         j3                  |      }
t5        t6        t8        f      xs( | j                         j:                  xs t=               }t5        t>        j@                        r| j                  j	                          yt5        tB        t8        t6        tD        f      rp|snddl#m$}  ||      }|jK                  |
| j                         jM                         |       | jO                  |tE        |j1                         ||
|             yt5        t>        jP                  t>        jR                  jT                  f      rt>        jP                  tV        urn|slddl,m-}  ||      }|j]                  |
|      s| j                  j	                          y| jO                  |tE        |j1                         ||
|             yt5        t^        j`                  j`                        rt>        jP                  tV        urt^        j`                  tV        ur|sddl,m-} t?        jb                        } ||      }|j]                  ||
|      s| j                  j	                          yfd}| jO                  |tE        |j1                         ||
||             yt5        td        jf                  td        jh                  td        jj                  f      rtd        jf                  tV        urm|skddl6m7}  ||      }|j]                  |
      s| j                  j	                          y| jO                  |tE        |j1                         ||
|             yt5        tp        jr                        r|s|r| j                  j	                          yt5        tp        jp                        rtu        |      }	 jw                          nt{        |      }|j}                  d       |j                  t        t                      | j                  j	                          |S t              rkt              d!kD  r]|s[t        d|      }|j]                        r<t        |
||"      }| jO                  |tE        |j1                         ||
|             yt              r|s|r| j                  j	                          yt        |      }|j                  t        t                      |j                  t        j                         | j                  j	                          |S dd#lJmK}  ||
||$      }| jO                  |tE        |j1                         ||
|             y# t        $ r.}| j                  j	                          t!        |      j#                  d      d   }|dv r|dk(  rd	}nd
}t        d      }	t%               r|	t        d      z  }	n|	t        d      z  }	t        j&                  | j                         t        d      |	j)                  ||             Y d}~yt%               s
t+               rt        d      }	nt        d      }	t        j&                  | j                         t        d      |	j)                  |             Y d}~yd}~wt,        $ rg}| j                  j	                          t        j&                  | j                         t        d      t        d      t/        |      z         Y d}~yd}~ww xY w# tx        $ r | j                  j	                          Y yw xY w)%Overriding method createEditorr:      NWarningBOpening this variable can be slow

Do you want to continue anyway?')r   r   r   arrayzdataframe, seriesztSpyder is unable to show the {val_type} or object you're trying to view because <tt>{module}</tt> is not installed. zKPlease consider using the full version of the Spyder MacOS application.<br>z;Please install this package in your Spyder environment.<br>Error)rR   modulezSpyder is unable to show the variable you're trying to view because the module <tt>{module}</tt> is not supported in the Spyder Lite application.<br>zSpyder is unable to show the variable you're trying to view because the module <tt>{module}</tt> is not found in your Spyder environment. Please install this package in this environment.<br>)r`   znSpyder was unable to retrieve the value of this variable from the console.<br><br>The error message was:<br>%sr   CollectionsEditorr*   iconreadonlyr/   editorkeyrf   r   titlerf   c                 Z    t         j                  j                  | j                        S N)moder   Image	fromarrayrn   arrr7   s    r+   <lambda>z2CollectionsDelegate.createEditor.<locals>.<lambda>       CII$7$7%**$7$M r,   r/   rh   ri   rf   convr   rk   Tfont_size_delta(   rf   r*   )ObjectExplorer)namer*   rf   )LrE   rF   rG   sig_editor_creation_startedemitcolumnrW   r   warningr*   r   YesNosig_editor_shownr0   ImportErrorstrrK   r   criticalformatr   rQ   r   r/   get_key
isinstancer>   r=   rf   r   npvoidr<   r?    spyder.widgets.collectionseditorrb   setup
windowIconcreate_dialogndarraymaMaskedArrayr   arrayeditorr   setup_and_checkr   rp   r^   pdr@   rC   rA   dataframeeditorr    datetimedater	   time
ValueErrorr   setCalendarPopupsetFontr   r   r   lenr!   r   r   setAlignmentr   	AlignLeft6spyder.plugins.variableexplorer.widgets.objectexplorerr}   )r)   r*   optionr2   object_explorerrR   answermsgr`   messageri   rf   rb   rh   r   rs   	conv_funcr    ter}   r7   s                       @r+   createEditorz CollectionsDelegate.createEditor\   s   ==a0557((--/<<>AU# ((q| 4 5+..0	2F
 '%%**,0	NN5)E} ` kkm##E*uucl3 0t{{}7M7M 0(// 	 eRWW%!!&&(c5$78J&f5FLL$++-*B*B*D"*  ,vt%++-03h(H IRUU->-> ?@

*,_0 /F))%sX)N%%**,vt%++-03h(H I		0

*,		+# 1((5/C /F))#S8)L%%**,MIvt%++-03h1:(< = rxx CDLL
2?8$F3F))%s);%%**,vt%++-03h(H Ix}}-o%%**,eX%6%67*5@F$


 'uV<F''-x8KLM%%**,E"s5zBD0B!!%(#E3-5fF""64ekkm7=39A,C D e$_%%**,"&1x8KLM##BLL1 %%**,&#!	#F
 vt%++-3903h(H I O  #	!!&&(X^^C(+F,,W$&H2H 2 3 &'q "E  F FG q "4  5 5G$$KKM1W:NNHVNDF %':< !? @G
   !C DG
 $$T[[]AgJ%,^^6^%BD 	!!&&(  qz   )-./ 	h & $--224#$s?   5Z `5 
`2B6^?A)^??`2A`--`25#aac                 .    | j                   t        |      <   |j                  j                  t        |      f fd	       |j                  j                  t        |      f fd	        j
                  j                          |j                          y )Nc                 &    j                  |       S r%   )editor_acceptedeidr)   s    r+   rt   z3CollectionsDelegate.create_dialog.<locals>.<lambda>      D,@,@,E r,   c                 &    j                  |       S r%   )editor_rejectedr   s    r+   rt   z3CollectionsDelegate.create_dialog.<locals>.<lambda>  r   r,   )r'   idacceptedconnectrejectedr   r   show)r)   rh   rG   s   `  r+   r   z!CollectionsDelegate.create_dialog  sj    $(bj! "6
E	G "6
E	G""$r,   c                 L   | j                   |   }|d   sU|d   j                  |d         }|d   j                         }|j                  dd       }| j	                  | ||             	 | j                   j                  |       | j                          y # t        $ r Y w xY w)Nrf   r/   ri   rh   rw   c                     | S r%    vs    r+   rt   z5CollectionsDelegate.editor_accepted.<locals>.<lambda>&      1 r,   )r'   get_index_from_keyr0   getr5   popKeyErrorfree_memoryr)   	editor_idrG   r2   r7   r   s         r+   r   z#CollectionsDelegate.editor_accepted!  s    }}Y'JM44T%[AEN,,.E5INN5)E"23	MMi( 	  		s   +B 	B#"B#c                 z    	 | j                   j                  |       | j                          y # t        $ r Y w xY wr%   )r'   r   r   r   )r)   r   s     r+   r   z#CollectionsDelegate.editor_rejected0  s;    	MMi( 	  		s   . 	::c                 X    	 | j                   j                          y# t        $ r Y yw xY w)z$Free memory after closing an editor.N)sig_free_memory_requestedr   RuntimeError)r)   s    r+   r   zCollectionsDelegate.free_memory9  s+    	**//1 		s    	))c                     | j                         }	 | j                  j                  |       | j                  j                  |t
        j                         y# t        $ r Y 6w xY w)z&Overriding method commitAndCloseEditorN)sender
commitDatar   AttributeErrorcloseEditorr   NoHint)r)   rh   s     r+   commitAndCloseEditorz(CollectionsDelegate.commitAndCloseEditor@  sW    	OO  ( 	f&;&B&BC  		s   A 	A$#A$c                    | j                  |      }t        |t              r@t        |      r	 t	        |d      }t        |      st        |      }|j                  |       yt        |t              r|j                  |       yt        |t              r8|j                  t        |j                         |j                                      yy# t
        $ r Y w xY w)zJ
        Overriding method setEditorData
        Model --> Editor
        utf8N)r0   r   r   r   r   rQ   r   reprsetTextr   setDater	   setDateTimer   r   r   )r)   rh   r2   r7   s       r+   setEditorDataz!CollectionsDelegate.setEditorDataK  s    
 u%fi(&*5&9E "%(UNN5!	*NN5!.yuzz|DE / ! s   C 	CCc           
         t        |d      rt        |j                         d      rt        |d      syt        |t              r9|j	                         }	 t        t        |      | j                  |      d      }n.t        |t              rR|j                         }t        j                  |j!                         |j#                         |j%                               }nt        |t&              r|j)                         }|j                         }|j+                         }t        j                  |j!                         |j#                         |j%                         |j-                         |j/                         |j1                         |j3                         dz        }nt5        d	      | j7                  ||       y# t        $ r?}t        j                  |t        d      t        d      t        |      z         Y d}~yd}~ww xY w)
zI
        Overriding method setModelData
        Editor --> Model
        sourceModelr5   NF)ignore_errorsz	Edit itemzA<b>Unable to assign data to item.</b><br><br>Error message:<br>%si  zUnsupported editor widget)hasattrr   r   r   textr   r   r0   rQ   r   r   r   r   r   r   r   yearmonthdayr	   dateTimer   hourminutesecondmsecr   r5   )	r)   rh   r/   r2   r7   r   qdate	qdatetimeqtimes	            r+   setModelDataz CollectionsDelegate.setModelData_  s   
 UM* 1 1 3[Auk2fi(KKME	(U);)-)>7<> 	*KKMEMM%**,uyy{KE.)INN$ENN$E%%ejjlEKKM599;&+jjlELLN&+llnejjl46GIE
 :;;ue$)  $$VQ{^%& (F &)+.s8&45 s   &F> >	H5HHc                 ~   |j                         j                         }t        |t              ry|j                         }|j	                         }|j                  |      }|j                  |      }|j                  |      }	|j                  |      }
|j                  |||	|
       yt        t        | 3  |||       y)ah  
        Overriding method updateEditorGeometry.

        This is necessary to set the correct position of the QLineEdit
        editor since option.rect doesn't have values -> QRect() and
        makes the editor to be invisible (i.e. it has 0 as x, y, width
        and height) when doing double click over a cell.
        See spyder-ide/spyder#9945
        N)r*   r   r   rF   r   rowViewportPositioncolumnViewportPositioncolumnWidth	rowHeightsetGeometrysuperr#   updateEditorGeometry)r)   rh   r   r2   
table_viewrF   r   y0x0widthheight	__class__s              r+   r   z(CollectionsDelegate.updateEditorGeometry  s     ]]_++-
j*-))+C\\^F//4B226:B**62E))#.Fr2uf5%tA'r,   r%   )F)__name__
__module____qualname____doc__r   r   r   r   r&   r0   r5   rW   r   r   r   r   r   r   r   r   r   __classcell__r   s   @r+   r#   r#   )   sj    ) &"((x22Bzx	DF(%%N' 'r,   r#   c                   H     e Zd ZdZd	dZd Zd Zd Zd Zd Z	 fdZ
 xZS )
ToggleColumnDelegatezToggleColumn Item Delegatec                 L    t         j                  | |       d | _        d | _        y r%   )r#   r&   current_indexold_objr(   s     r+   r&   zToggleColumnDelegate.__init__  s!    $$T62!r,   c                     | j                   rH| j                  ;| j                   }| j                  |j                         j                  |      _        yyy)z6Discart changes made to the current object in edition.N)r   r   r/   treeItemobjr1   s     r+   restore_objectz#ToggleColumnDelegate.restore_object  sD    $,,":&&E04EKKM""5)- #;r,   c                 z    |j                         r+|j                         j                  |      j                  }|S y)zGet object value in index.N)r.   r/   r  r  r6   s      r+   r0   zToggleColumnDelegate.get_value  s1    ==?KKM**5155EL r,   c                 f    |j                         r!|j                         j                  ||       y y r%   r4   r6   s      r+   r5   zToggleColumnDelegate.set_value  r8   r,   c           
      
   | j                  |      rkt        j                  | j                         t	        d      t	        d      t        j
                  t        j                  z        }|t        j                  k(  ry	 | j                  |      	 j                         | _	        y	 || _        |j!                         j#                  |      j$                  }t'        t(        t*        f      xs( | j                         j,                  xs t/               }t'        t0        t*        t(        t2        f      rnddlm}  ||      }	|	j9                  || j                         j;                         |	       | j=                  |	t3        |j!                         |	||
             yt'        t>        j@                  t>        jB                  jD                  f      ret>        j@                  tF        urOtI        |      }	|	jK                  ||      sy| j=                  |	t3        |j!                         |	||
             yt'        tL        jN                  jN                        rt>        j@                  tF        urtL        jN                  tF        urjt?        jP                        }
tI        |      }	|	jK                  |
||      syfd}| j=                  |	t3        |j!                         |	|||             yt'        tR        jT                  tR        jV                  tR        jX                  f      rdtR        jT                  tF        urNt[        |      }	|	jK                  |      sy| j=                  |	t3        |j!                         |	||
             yt'        t\        j^                        rj|ryt'        t\        j\                        rta        |      }	ntc        |      }	|	je                  d       |	jg                  ti        tj                     |	S tm              rito              dkD  r[tq        d|      }|jK                        r<tq        |||      }	| j=                  |	t3        |j!                         |	||
             yts              rO|rytu        |      }	|	jg                  ti        tj                     |	jw                  tx        jz                         |	S y# t        $ r t        j                        | _	        Y kw xY w# t        $ rM}t        j                  | j                         t	        d      t	        d      t        |      z         Y d}~yd}~ww xY w)rY   r[   r\   Nr_   zuSpyder was unable to retrieve the value of this variable from the console.<br><br>The error message was:<br><i>%s</i>r   ra   rc   rd   rg   rj   c                 Z    t         j                  j                  | j                        S rm   ro   rr   s    r+   rt   z3ToggleColumnDelegate.createEditor.<locals>.<lambda>  ru   r,   rv   rx   Try   r{   r|   )>rW   r   r   r*   r   r   r   r0   copyr   r   deepcopyrQ   r   r   r   r/   r   obj_namer   r>   r=   rf   r   r<   r?   r   rb   r   r   r   r   r   r   r   r   r   r   r   rp   r^   r   r@   rC   rA   r    r   r   r	   r   r   r   r   r   r   r   r!   r   r   r   r   r   )r)   r*   r   r2   r   r   ri   rf   rb   rh   rs   r   r   r7   s                @r+   r   z!ToggleColumnDelegate.createEditor  sa   U# ((q| 4 5+..0	2F
 '	NN5)E4$zz| }  #kkm##E*33uucl3 0t{{}7M7M 0(// 	 edC56J&f5FLL$++-*B*B*D"*  ,vt%++-03h(H IRUU->-> ?@

*, /F))%sX)Nvt%++-03h(H I		0

*,*1L((5/C /F))#S8)LMIvt%++-03h1:(< = rxx CDLL
2$F3F))%s);vt%++-03h(H Ix}}-eX%6%67*5@F&uV<F''-x8KLME"s5zBD0B!!%(#E3-5fF""64ekkm7=39A,C D e$"&1x8KLM##BLL1  A " 4#}}U34  	  qz   "0!456 	s=   ?T S+ &T +#TT TT 	U+AU&&U+c                 N   | j                   |   }|d   sV| j                  rJ| j                  }|d   j                         }|j                  dd       }| j	                  | ||             	 | j                   j                  |       | j                          y# t        $ r Y w xY w)z3Actions to execute when the editor has been closed.rf   rh   rw   c                     | S r%   r   r   s    r+   rt   z6ToggleColumnDelegate.editor_accepted.<locals>.<lambda>+  r   r,   N)r'   r   r0   r   r5   r   r   r   r   s         r+   r   z$ToggleColumnDelegate.editor_accepted%  s    }}Y'JD$6$6&&EN,,.E5INN5)E"23	MMi( 	  		s   ,B 	B$#B$c                 L    | j                          t        t        |   |       y)z+Actions to do when the editor was rejected.N)r  r   r   r   )r)   r   r   s     r+   r   z$ToggleColumnDelegate.editor_rejected5  s    "D9)Dr,   r%   )r   r   r   r   r&   r  r0   r5   r   r   r   r   r   s   @r+   r   r     s2    $
=2n` E Er,   r   )3r	  r   rL   rN   qtpy.compatr   qtpy.QtCorer   r   r   qtpy.QtWidgetsr   r   r	   r
   r   r   r    spyder_kernels.utils.lazymodulesr   r   r   r   r   r   spyder_kernels.utils.nsviewr   r   r   spyder.config.baser   r   r   spyder.config.fontsr   spyder.config.guir   spyder.py3compatr   r   r   3spyder.plugins.variableexplorer.widgets.arrayeditorr   7spyder.plugins.variableexplorer.widgets.dataframeeditorr    2spyder.plugins.variableexplorer.widgets.texteditorr!   rI   rP   r#   r   r   r,   r+   <module>r     s        $ - -O O O0 08 8 A @ 3 & M M K I  r'- r'jZE. ZEr,   