3
7ßL]%$  ã               @   s”   d dl mZ d dlmZmZmZmZmZ d dlmZm	Z	 d dl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 G d
d„ deƒZdS )é    )Úprint_function)ÚGridÚPyGridTableBaseÚGridCellAttrÚGridTableMessageÚGridCellFloatRenderer)ÚGRIDTABLE_NOTIFY_ROWS_DELETEDÚGRIDTABLE_NOTIFY_ROWS_APPENDED)ÚGRIDTABLE_NOTIFY_COLS_DELETEDÚGRIDTABLE_NOTIFY_COLS_APPENDED)Ú!GRIDTABLE_REQUEST_VIEW_GET_VALUES)ÚGRID_VALUE_BOOL)Ú
ALIGN_LEFTÚALIGN_CENTREÚColouré   )ÚDefaultRendererc               @   s¨   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd d!„ Zd"d#„ Zd$d%„ Zd&d'„ Zd(S ))ÚVirtualModelz„
    A custom wxGrid Table that expects a user supplied data source.
    THIS CLASS IS NOT LIMITED TO ONLY DISPLAYING LOG DATA!
    c             C   s2   t j| ƒ | j|ƒ || _| jƒ | _| jƒ | _dS )z�data is currently a list of the form
        [(rowname, dictionary),
        dictionary.get(colname, None) returns the data for a cell
        N)r   Ú__init__Úset_data_sourceÚcolnamesÚGetNumberRowsÚ_rowsÚGetNumberColsÚ_cols)ÚselfÚdataZcolumn_names© r   úG/tmp/pip-install-5sebvq2j/pyface/pyface/wx/spreadsheet/virtual_model.pyr      s
    


zVirtualModel.__init__c             C   s
   t | jƒS )N)Úlenr   )r   r   r   r   r   1   s    zVirtualModel.GetNumberColsc             C   s
   t | jƒS )N)r   Ú_data)r   r   r   r   r   4   s    zVirtualModel.GetNumberRowsc             C   s
   | j | S )N)r   )r   Úcolr   r   r   ÚGetColLabelValue7   s    zVirtualModel.GetColLabelValuec             C   s   | j | d S )Nr   )r    )r   Úrowr   r   r   ÚGetRowLabelValue:   s    zVirtualModel.GetRowLabelValuec             C   s    t | j| d j| j|ƒdƒƒS )Nr   Ú )Ústrr    Úgetr"   )r   r#   r!   r   r   r   ÚGetValue=   s    zVirtualModel.GetValuec             C   s   | j | d j| j|ƒdƒS )Nr   r%   )r    r'   r"   )r   r#   r!   r   r   r   ÚGetRawValue@   s    zVirtualModel.GetRawValuec             C   sR   t d|||f ƒ t d| j||ƒƒ || j| d | j|ƒ< t d| j||ƒƒ d S )NzSetting value %d %d %szBefore r   zAfter )Úprintr(   r    r"   )r   r#   r!   Úvaluer   r   r   ÚSetValueC   s    zVirtualModel.SetValuec             C   s   | j S )z4 The data structure we provide the data in.
        )r    )r   r   r   r   Úget_data_sourceV   s    zVirtualModel.get_data_sourcec             C   s
   || _ d S )N)r    )r   Úsourcer   r   r   r   [   s    zVirtualModel.set_data_sourcec             C   sÀ   |j ƒ  x†| j| jƒ ttf| j| jƒ ttfgD ]^\}}}}||k r`t	| |||| ƒ}|j
|ƒ q.||kr.t	| ||| ƒ}|j
|ƒ | j|ƒ q.W |jƒ  | jƒ | _| jƒ | _|jƒ  |jƒ  dS )zˆ
        (wxGrid) -> Reset the grid view.   Call this to
        update the grid if rows and columns have been added or deleted
        N)Z
BeginBatchr   r   r   r	   r   r   r
   r   r   ÚProcessTableMessageÚUpdateValuesZEndBatchZAdjustScrollbarsZForceRefresh)r   ÚgridÚcurrentÚnewZdelmsgZaddmsgÚmsgr   r   r   Ú	ResetViewc   s      


zVirtualModel.ResetViewc             C   s   t | tƒ}|j|ƒ dS )zUpdate all displayed valuesN)r   r   r/   )r   r1   r4   r   r   r   r0   ƒ   s    
zVirtualModel.UpdateValuesc             C   s    t d||ƒ tƒ }|jdƒ |S )NzOverridden GetAttr r   )r*   r   ZSetReadOnly)r   r#   r!   ZsomeExtraParameterÚattrr   r   r   Ú	GetAttr88‰   s    
zVirtualModel.GetAttr88c             C   sv   xpt | jƒD ]b\}}tƒ }|| jkr0| j| }n
| jd }|j|ƒ || jkrb| j| }|j|ƒ |j||ƒ qW dS )zw
        wxGrid -> update the column attributes to add the
        appropriate renderer given the column name.
        ZDEFAULT_RENDERERN)Ú	enumerater   r   Z	renderersZSetRendererZeditorsZ	SetEditorZ
SetColAttr)r   r1   r!   Zcolnamer6   ZrendererÚeditorr   r   r   Ú_updateColAttrs88•   s    





zVirtualModel._updateColAttrs88c             C   s"   |\}}t d|ƒ | jj|ƒ dS )z/ Append a tupe containing (name, data)
        z
Appending N)r*   r    Úappend)r   r#   Únamer   r   r   r   Ú	AppendRow»   s    
zVirtualModel.AppendRowc             C   sR   d}|dd… }|j ƒ  x$|D ]}| jj|| ƒ |d7 }qW t| jƒsNg | _dS )zb
        cols -> delete the columns from the dataset
        cols hold the column indices
        r   Nr   )Úsortr   Úpopr   r   )r   ÚcolsÚdeleteCountÚir   r   r   ÚDeleteCols88Æ   s    

zVirtualModel.DeleteCols88c             C   s"   |\}}t d|ƒ | jj|ƒ d S )Nz	Deleting )r*   r    Úremove)r   r#   r<   r   r   r   r   Ú	DeleteRowÙ   s    
zVirtualModel.DeleteRowc             C   sB   d}|dd… }|j ƒ  x$|D ]}| jj|| ƒ |d7 }qW dS )z\
        rows -> delete the rows from the dataset
        rows hold the row indices
        r   Nr   )r>   r    r?   )r   ZrowsrA   rB   r   r   r   ÚDeleteRows88Þ   s    
zVirtualModel.DeleteRows88c             C   sl   | j | }g }x,| jD ]"}|\}}|j|j|dƒ|fƒ qW |jƒ  g | _x|D ]\}}| jj|ƒ qPW dS )z£
        to do - never tested
        tried to rename data to _data and _data to _tmp_data
        col -> sort the data based on the column indexed by col
        N)r   r    r;   r'   r>   )r   r!   r<   Z	_tmp_datar#   ZrownameÚentryZ	sortvaluer   r   r   ÚSortColumn88ì   s    
zVirtualModel.SortColumn88N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r"   r$   r(   r)   r,   r-   r   r5   r0   r7   r:   r=   rC   rE   rF   rH   r   r   r   r   r      s(    &r   N)Ú
__future__r   Zwx.gridr   r   r   r   r   r   r	   r
   r   r   r   Zwxr   r   r   Zdefault_rendererr   r   r   r   r   r   Ú<module>   s   