
    G@da                     :   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          Z2dS )    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 DelegateNc                 >    t          j        | |           i | _        d S N)r
   __init___editorsselfparents     [lib/python3.11/site-packages/spyder/plugins/variableexplorer/widgets/collectionsdelegate.pyr&   zCollectionsDelegate.__init__/   s     tV,,,    c                 |    |                                 r'|                                                    |          S d S r%   )isValidmodel	get_valuer)   indexs     r+   r0   zCollectionsDelegate.get_value3   s8    ==?? 	2;;==**5111	2 	2r,   c                     |                                 r*|                                                    ||           d S d S r%   r.   r/   	set_valuer)   r2   values      r+   r5   zCollectionsDelegate.set_value7   @    ==?? 	2KKMM##E511111	2 	2r,   c                 $   |                     |                                d                                          }|                     |                                d                                          }|dv rt          |          t          k    rdS n||dv sd|v sd|v rp	 d |                    d	                              d
          D             }t          j        t          j
        |          }|t          k    rdS n# t          $ r Y nw xY wdS )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Indexc                 0    g | ]}|t          |          S  )int).0ss     r+   
<listcomp>z4CollectionsDelegate.show_warning.<locals>.<listcomp>S   s#    NNNAANQNNNr,   z(),F)siblingrowdatarF   LARGE_COLLECTIONstripsplit	functoolsreduceoperatormulLARGE_ARRAY	Exception)r)   r2   val_typeval_sizeshapesizes         r+   show_warningz CollectionsDelegate.show_warning;   s    ==a005577==a0055777778}}///t 0111W5H5H8##NN)=)=)C)CC)H)HNNN 'e<<+%%4 &    us   !AD   
DDFc           
         |                     |                                d                                          }| j                                         |                                dk     rdS |                     |          rt          j        | 	                                t          d          t          d          t          j        t          j        z            }|t          j        k    r| j                                         dS 	 |                     |          dS n# t          $ rq}| j                                         t!          |                              d          d         }|dv r|dk    rd	}nd
}t          d          }	t%                      r|	t          d          z  }	n|	t          d          z  }	t          j        | 	                                t          d          |	                    ||                     Y d}~dS t%                      st+                      rt          d          }	nt          d          }	t          j        | 	                                t          d          |	                    |                     Y d}~dS d}~wt,          $ rv}| j                                         t          j        | 	                                t          d          t          d          t/          |          z             Y d}~dS d}~ww xY w|                                                    |          }
t5          t6          t8          f          p(| 	                                j        pt=                     }t5          t>          j                   r| j                                         dS t5          tB          t8          t6          tD          f          r|sddl#m$}  ||          }|%                    |
| 	                                &                                |           | '                    |tE          |                                ||
|                     dS t5          t>          j(        t>          j)        j*        f          rt>          j(        tV          ur|sddl,m-}  ||          }|.                    |
|          s| j                                         dS | '                    |tE          |                                ||
|                     dS t5          t^          j0        j0                  rt>          j(        tV          urt^          j0        tV          ur|sddl,m-} t?          j1                  } ||          }|.                    ||
|          s| j                                         dS fd}| '                    |tE          |                                ||
||                     dS t5          td          j3        td          j4        td          j5        f          rtd          j3        tV          ur|sddl6m7}  ||          }|.                    |
          s| j                                         dS | '                    |tE          |                                ||
|                     dS t5          tp          j9                  r|s|r| j                                         dS t5          tp          j8                  rQtu          |          }	 ;                                 n;# tx          $ r | j                                         Y dS w xY wt{          |          }|>                    d           |?                    t          t                                | j                                         |S t                    rt                    d!k    rv|stt          d|          }|.                              rLt          |
||"          }| '                    |tE          |                                ||
|                     dS t                    r|s|r| j                                         dS t          |          }|?                    t          t                                |G                    t          jI                   | j                                         |S dd#lJmK}  ||
||$          }| '                    |tE          |                                ||
|                     dS )%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)rW   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>)rd   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keyrj   r   titlerj   c                 P    t           j                            | j                  S N)moder   Image	fromarrayrr   arrr7   s    r+   <lambda>z2CollectionsDelegate.createEditor.<locals>.<lambda>       CI$7$7%*$7$M$M r,   r/   rl   rm   rj   convr   ro   Tfont_size_delta(   rj   r*   )ObjectExplorer)namer*   rj   )LrK   rL   rM   sig_editor_creation_startedemitcolumnr[   r   warningr*   r   YesNosig_editor_shownr0   ImportErrorstrrP   r   criticalformatr   rV   r   r/   get_key
isinstancer>   r=   rj   r   npvoidr<   r?    spyder.widgets.collectionseditorrf   setup
windowIconcreate_dialogndarraymaMaskedArrayr   arrayeditorr   setup_and_checkr   rt   rb   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_explorerrW   answermsgrd   messagerm   rj   rf   rl   r   rw   	conv_funcr    ter   r7   s                       @r+   createEditorz CollectionsDelegate.createEditor\   s
   ==a005577(--///<<>>A4U## 	 (q|| 4 5 5+.0	2 2F
 ''%**,,,t0	NN5))E}t  #	 #	 #	!&&(((XX^^C((+F,,,W$$&HH2H 2 3 3 &'' 5q "E  F  F FGG q "4  5  5 5G$KKMM1W::NNHVNDDF F F %'' 
D:<< 
D !? @ @GG
   !C D DG
 $T[[]]AgJJ%,^^6^%B%BD D D 	 	 	!&&((( qzz    )--./ / / FFFFF	 kkmm##E**uucl33 0t{{}}7M 0(/// 	 eRW%% r	!&&(((4c5$788 n	 n	JJJJJJ&&f555FLL$++--*B*B*D*D"*  , , ,vt%++--03h(H (H (H I I I4RU-> ?@@ e	
*,,_,000000 [///F))%sX)NN %**,,,vt%++--03h(H (H (H I I I4	00 Y	
*,,	++# ,
 100000(5//C [///F))#S8)LL %**,,,MMMMIvt%++--03h1:(< (< (< = = = 4rx CDD F	L
22?2888888$_F333F))%s);; %**,,,vt%++--03h(H (H (H I I I4x}-- :	o :	 %**,,,teX%677 
=*5@@@F$

% $ $ $-22444#tt$ 'uV<<<F''---x8KLLLMMM%**,,,E"" %	s5zzBD000B!!%(( D#E3-5fF F F""64ekkmm7=39A,C ,C ,C D D D 4e$$ 	_ 	 %**,,,t"&111x8KLLLMMM##BL111 %**,,,& & & & & &#^!	# # #F
 vt%++--3903h(H (H (H I I I 4s?   
D% %L0CJBJL)A+LL] #]>=]>c                 @    | j         t          |          <   |j                            t          |          f fd	           |j                            t          |          f fd	            j                                         |                                 d S )Nc                 .                         |           S r%   )editor_acceptedeidr)   s    r+   rx   z3CollectionsDelegate.create_dialog.<locals>.<lambda>      D,@,@,E,E r,   c                 .                         |           S r%   )editor_rejectedr   s    r+   rx   z3CollectionsDelegate.create_dialog.<locals>.<lambda>  r   r,   )r'   idacceptedconnectrejectedr   r   show)r)   rl   rM   s   `  r+   r   z!CollectionsDelegate.create_dialog  s    $(bjj! "6

EEEEE	G 	G 	G "6

EEEEE	G 	G 	G""$$$r,   c                    | j         |         }|d         sq|d                             |d                   }|d                                         }|                    dd           }|                     | ||                     	 | j                             |           n# t          $ r Y nw xY w|                                  d S )Nrj   r/   rm   rl   r{   c                     | S r%   rE   vs    r+   rx   z5CollectionsDelegate.editor_accepted.<locals>.<lambda>&      1 r,   )r'   get_index_from_keyr0   getr5   popKeyErrorfree_memoryr)   	editor_idrM   r2   r7   r   s         r+   r   z#CollectionsDelegate.editor_accepted!  s    }Y'J 	4M44T%[AAEN,,..E55INN5))E"2"2333	Mi(((( 	 	 	D	s   B# #
B0/B0c                     	 | j                             |           n# t          $ r Y nw xY w|                                  d S r%   )r'   r   r   r   )r)   r   s     r+   r   z#CollectionsDelegate.editor_rejected0  sX    	Mi(((( 	 	 	D	s    
**c                 \    	 | j                                          dS # t          $ r Y dS 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  sD    	*//11111 	 	 	DD	s    
++c                     |                                  }	 | j                            |           n# t          $ r Y nw xY w| j                            |t
          j                   dS )z&Overriding method commitAndCloseEditorN)sender
commitDatar   AttributeErrorcloseEditorr   NoHint)r)   rl   s     r+   commitAndCloseEditorz(CollectionsDelegate.commitAndCloseEditor@  sp    	O  (((( 	 	 	D	f&;&BCCCCCs   1 
>>c                 :   |                      |          }t          |t                    rft          |          r"	 t	          |d          }n# t
          $ r Y nw xY wt          |          st          |          }|                    |           dS t          |t                    r|
                    |           dS t          |t                    rI|                    t          |                                |                                                     dS dS )zJ
        Overriding method setEditorData
        Model --> Editor
        utf8N)r0   r   r   r   r   rV   r   reprsetTextr   setDater	   setDateTimer   r   r   )r)   rl   r2   r7   s       r+   setEditorDataz!CollectionsDelegate.setEditorDataK  s   
 u%%fi(( 	F&& *5&99EE    D!%(( $UNN5!!!!!	** 	FNN5!!!!!.. 	Fyuzz||DDEEEEE	F 	Fs   A 
AAc           
      
   t          |d          r"t          |                                d          rt          |d          sdS t          |t                    r|                                }	 t          t          |          |                     |          d          }n# t          $ rK}t          j
        |t          d          t          d          t          |          z             Y d}~dS d}~ww xY wt          |t                    ra|                                }t          j        |                                |                                |                                          }nt          |t&                    r|                                }|                                }|                                }t          j        |                                |                                |                                |                                |                                |                                |                                dz            }nt5          d	          |                     ||           dS )
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   rV   r   r   r   r   r   r   r   yearmonthdayr	   dateTimer   hourminutesecondmsecr   r5   )	r)   rl   r/   r2   r7   r   qdate	qdatetimeqtimes	            r+   setModelDataz CollectionsDelegate.setModelData_  s.   
 UM** 	 1 1 3 3[AA	uk22	 Ffi(( 	<KKMME	(U););)-)>)>7<> > >    $VQ{^^%& (F &) &)+.s88&45 5 5  	** 	<KKMMEM%**,,uyy{{KKEE.. 
	<))INN$$ENN$$E%ejjllEKKMM599;;&+jjllELLNN&+llnnejjll46GI IEE
 :;;;ue$$$$$s   /2B# #
C8-A C33C8c                    |                                                                  }t          |t                    r|                                }|                                }|                    |          }|                    |          }|                    |          }	|                    |          }
|	                    |||	|
           dS t          t          |                               |||           dS )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   rL   r   rowViewportPositioncolumnViewportPositioncolumnWidth	rowHeightsetGeometrysuperr#   updateEditorGeometry)r)   rl   r   r2   
table_viewrL   r   y0x0widthheight	__class__s              r+   r   z(CollectionsDelegate.updateEditorGeometry  s     ]]__++--
j*-- 
	'))++C\\^^F//44B226::B**622E))#..Fr2uf55555%t,,AA' ' ' ' 'r,   r%   )F)__name__
__module____qualname____doc__r   r   r   r   r&   r0   r5   r[   r   r   r   r   r   r   r   r   r   __classcell__r   s   @r+   r#   r#   )   s-       )) &"(&((vxx   2 2 22 2 2  Bz z z zx        	D 	D 	DF F 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 DelegateNc                 X    t                               | |           d | _        d | _        d S r%   )r#   r&   current_indexold_objr(   s     r+   r&   zToggleColumnDelegate.__init__  s+    $$T6222!r,   c                     | j         rA| j        <| j         }| j        |                                                    |          _        dS dS dS )z6Discart changes made to the current object in edition.N)r  r  r/   treeItemobjr1   s     r+   restore_objectz#ToggleColumnDelegate.restore_object  sR     	=$,":&E04EKKMM""5))---	= 	=":":r,   c                     |                                 r.|                                                    |          j        }|S dS )zGet object value in index.N)r.   r/   r  r  r6   s      r+   r0   zToggleColumnDelegate.get_value  s?    ==?? 	KKMM**5115EL	 	r,   c                     |                                 r*|                                                    ||           d S d S r%   r4   r6   s      r+   r5   zToggleColumnDelegate.set_value  r8   r,   c           
      F   |                      |          rlt          j        |                                 t	          d          t	          d          t          j        t          j        z            }|t          j        k    rdS 	 |                     |          	                                 | _	        n)# t          $ r t          j                  | _	        Y nw xY wdS nj# t          $ r]}t          j        |                                 t	          d          t	          d          t          |          z             Y d}~dS d}~ww xY w|| _        |                                                    |          j        }t'          t(          t*          f          p(|                                 j        pt/                     }t'          t0          t*          t(          t2          f          rddlm}  ||          }	|	                    ||                                                                 |	           |                     |	t3          |                                |	||
                     dS t'          t>          j         t>          j!        j"        f          rxt>          j         tF          uretI          |          }	|	%                    ||          sdS |                     |	t3          |                                |	||
                     dS t'          tL          j'        j'                  rt>          j         tF          urtL          j'        tF          urt?          j(                  }
tI          |          }	|	%                    |
||          sdS fd}|                     |	t3          |                                |	|||                     dS t'          tR          j*        tR          j+        tR          j,        f          rwtR          j*        tF          urdt[          |          }	|	%                    |          sdS |                     |	t3          |                                |	||
                     dS t'          t\          j/                  r|rdS t'          t\          j.                  rta          |          }	ntc          |          }	|	2                    d           |	3                    ti          tj                               |	S tm                    rto                    dk    rttq          d|          }|%                              rLtq          |||          }	|                     |	t3          |                                |	||
                     dS ts                    r]|rdS tu          |          }	|	3                    ti          tj                               |	;                    tx          j=                   |	S dS )r]   r_   r`   Nrc   zuSpyder was unable to retrieve the value of this variable from the console.<br><br>The error message was:<br><i>%s</i>r   re   rg   rh   rk   rn   c                 P    t           j                            | j                  S rq   rs   rv   s    r+   rx   z3ToggleColumnDelegate.createEditor.<locals>.<lambda>  ry   r,   rz   r|   Tr}   r   r   )>r[   r   r   r*   r   r   r   r0   copyr  r   deepcopyrV   r   r   r  r/   r   obj_namer   r>   r=   rj   r   r<   r?   r   rf   r   r   r   r   r   r   r   r   r   r   r   rt   rb   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   rm   rj   rf   rl   rw   r   r   r7   s                @r+   r   z!ToggleColumnDelegate.createEditor  s7   U## 	 (q|| 4 5 5+.0	2 2F
 ''t	NN5))E4$zz||! 4 4 4#}U334}t  	 	 	 qzz    "0!4!456 6 6 FFFFF	 #kkmm##E**3uucl33 0t{{}}7M 0(/// 	 edC566 M	JJJJJJ&&f555FLL$++--*B*B*D*D"*  , , ,vt%++--03h(H (H (H I I I4RU-> ?@@ D	
*,, ///F))%sX)NN vt%++--03h(H (H (H I I I4	00 ;	
*,,*1L1L(5//C ///F))#S8)LL MMMMIvt%++--03h1:(< (< (< = = = 4rx CDD /	L
22$F333F))%s);; vt%++--03h(H (H (H I I I4x}-- &	 	teX%677 =*5@@@FF&uV<<<F''---x8KLLLMMME"" 	s5zzBD000B!!%(( D#E3-5fF F F""64ekkmm7=39A,C ,C ,C D D D 4e$$ 	 t"&111x8KLLLMMM##BL111  4s=   C" B4 3C" 4#CC" CC" "
E	,AEE	c                 l   | j         |         }|d         s^| j        rW| j        }|d                                         }|                    dd           }|                     | ||                     	 | j                             |           n# t          $ r Y nw xY w|                                  dS )z3Actions to execute when the editor has been closed.rj   rl   r{   c                     | S r%   rE   r   s    r+   rx   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 	4D$6 	4&EN,,..E55INN5))E"2"2333	Mi(((( 	 	 	D	s   5B 
BBc                     |                                   t          t          |                               |           dS )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9)DDDDDr,   r%   )r   r   r   r   r&   r  r0   r5   r   r   r   r   r   s   @r+   r   r     s        $$   
= = =  2 2 2n n n`   E E E E E E E E Er,   r   )3r  r   rQ   rS   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!   rN   rU   r#   r   rE   r,   r+   <module>r     sh          $ # # # # # - - - - - - - - - -O O O O O O O O O O O O O O O O O O0 0 0 0 0 0 0 0 0 0 0 08 8 8 8 8 8 8 8 8 8 A @ @ @ @ @ @ @ @ @ 3 3 3 3 3 3 & & & & & & M M M M M M M M M M K K K K K K      I I I I I I  r' r' r' r' r'- r' r' r'jZE ZE ZE ZE ZE. ZE ZE ZE ZE ZEr,   