a
    §ˆcØE  ã                   @   s¤  d Z ddlmZ ddl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 ddlmZmZmZmZmZmZmZmZ ddlmZmZmZ d	d
l m!Z! ej"j#ej"j$ej"j%dœZ&erÞej'j(ej'j)ej'j*dœZ+nej,j(ej,j)ej,j*dœZ+dd„ e+ -¡ D ƒZ.ejj/dejj0dejj1dejj2dejj3dejj4dejj5dejj6diZ7e�r^de7ejj8< e�r�ddlm9Z9 G dd„ deƒZ:G dd„ de9ƒZ;G dd„ de!ƒZ<dS ) z&Traits UI 'display only' video editor.é    )Úis_qt5)ÚQPointÚQtÚQUrlÚSignal)ÚQImageÚQPainterÚQPaletteÚQSizePolicy)ÚQAudioÚQMediaPlayerÚQVideoFrame)ÚQVideoWidget)ÚAnyÚBoolÚCallableÚFloatÚInstanceÚRangeÚStrÚobserve)ÚAspectRatioÚMediaStatusÚPlayerStateé   )ÚEditor)ÚignoreZkeepÚexpand)ÚstoppedÚplayingÚpausedc                 C   s   i | ]\}}||“qS © r!   )Ú.0ÚkeyÚvaluer!   r!   ú\/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/traitsui/qt4/video_editor.pyÚ
<dictcomp>2   ó    r&   Zno_mediaZloadingZloadedZstalledÚ	bufferingZbufferedÚendÚinvalidÚunknown)ÚQAbstractVideoSurfacec                       s>   e Zd ZdZd‡ fdd„	Zdd„ Zdd„ Z‡ fd	d
„Z‡  ZS )ÚImageWidgetz#Paints a QImage to the window body.Nc                    s^   dd l }tƒ  |¡ tƒ | _|jdd�| _d | _|  d ¡ |d u rTdd„ | jf| _	n|| _	d S )Nr   )r   r   é   )Úshapec                 S   s   | S ©Nr!   )ÚimageZbboxr!   r!   r%   Ú<lambda>U   r'   z&ImageWidget.__init__.<locals>.<lambda>)
ÚnumpyÚsuperÚ__init__r   r1   ÚzerosÚ	_np_imageÚpainterÚresizeEventÚ
image_func)ÚselfÚparentr:   Únp©Ú	__class__r!   r%   r5   K   s    
zImageWidget.__init__c                 C   s    |   ¡ }| ¡ | _| ¡ | _d S r0   )ÚsizeÚwidthÚheight)r;   ÚeventÚsr!   r!   r%   r9   Y   s    
zImageWidget.resizeEventc                 C   s(   |   || j| jf¡\| _| _|  ¡  d S r0   )r:   rA   rB   r1   r7   Úupdate)r;   r1   r!   r!   r%   ÚsetImage^   s    ÿzImageWidget.setImagec                    sT   t ƒ  |¡ | jd u rtƒ | _| j | ¡ | jrF| j tddƒ| j¡ | j ¡  d S )Nr   )	r4   Ú
paintEventr8   r   Úbeginr1   Z	drawImager   r)   ©r;   rC   r>   r!   r%   rG   d   s    
zImageWidget.paintEvent)NN)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r5   r9   rF   rG   Ú__classcell__r!   r!   r>   r%   r-   H   s
   r-   c                       s8   e Zd ZedgƒZd	‡ fdd„	Zdd„ Zdd„ Z‡  ZS )
ÚVideoSurfacer   Nc                    s   t ƒ  ¡  || _d S r0   )r4   r5   Úwidget)r;   rP   r>   r!   r%   r5   q   s    
zVideoSurface.__init__c                 C   s   t jgS r0   )r   ZFormat_RGB32)r;   Z
handleTyper!   r!   r%   ÚsupportedPixelFormatsu   s    z"VideoSurface.supportedPixelFormatsc              	   C   sZ   ddl m} t|ƒ}| |j¡ t| ¡ | ¡ | ¡ | 	¡ t 
| ¡ ¡ƒ}| j |¡ dS )Nr   )ÚQAbstractVideoBufferT)Úpyface.qt.QtMultimediarR   r   ÚmapZReadOnlyr   ÚbitsrA   rB   ZbytesPerLineZimageFormatFromPixelFormatZpixelFormatÚframeAvailableÚemit)r;   ÚframerR   Zcloned_framer1   r!   r!   r%   Úpresentx   s    ÿû	zVideoSurface.present)N)	rJ   rK   rL   r   rV   r5   rQ   rY   rN   r!   r!   r>   r%   rO   m   s   
rO   c                       sÈ  e Zd ZdZeeƒZeƒ Zeƒ Z	ee
ƒZeƒ Zeƒ Zeƒ Zeƒ Zeƒ ZeddƒZeƒ ZedƒZeddƒZedƒZeƒ ZedƒZeƒ Z dd	„ Z!d
d„ Z"dd„ Z#‡ f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/e0d&ƒd'd(„ ƒZ1e0d)ƒd*d+„ ƒZ2e0d,ƒd-d.„ ƒZ3e0d/ƒd0d1„ ƒZ4e0d2ƒd3d4„ ƒZ5e0d5ƒd6d7„ ƒZ6e0d8ƒd9d:„ ƒZ7e0d;ƒd<d=„ ƒZ8e0d>ƒd?d@„ ƒZ9dAdB„ Z:dCdD„ Z;dEdF„ Z<dGdH„ Z=dIdJ„ Z>dKdL„ Z?‡  Z@S )MÚVideoEditoraV  Traits UI 'display only' video editor.

    This editor uses the Qt QMultimedia machinery to display video streams
    to the screen.  Rather than being self-contained, the editor only concerns
    itself with displaying the video, and provides traits that control
    behaviour and provide internal state of the control during playback.
    r   éd   Fg        ç      Y@g      ð?c                 C   sb   | j d ur"| j j | jj¡ d | _ tƒ | _| j tjj	tjj	¡ | j 
tjj¡ | j | j¡ d S r0   )ÚsurfacerV   Ú
disconnectÚcontrolrF   r   ÚsetSizePolicyr
   ÚPolicyÚ	ExpandingÚsetBackgroundRoler	   Ú	ColorRoleÚWindowÚmedia_playerÚsetVideoOutput©r;   r!   r!   r%   Úupdate_to_regularÓ   s    
ÿzVideoEditor.update_to_regularc                 C   sf   t | jd�| _| j tjjtjj¡ | j tj	j
¡ t| jd�| _| jj | jj¡ | j | j¡ d S )N)r:   )rP   )r-   r:   r_   r`   r
   ra   rb   rc   r	   rd   re   rO   r]   rV   ÚconnectrF   rf   rg   rh   r!   r!   r%   Úupdate_to_functionalß   s    ÿz VideoEditor.update_to_functionalc                 C   sè   t ƒ | _| j tjjtjj¡ | j tjj	¡ t
rBtdtjƒ| _ntƒ | _|  ¡  | j | j¡ t
rt| j | j¡ n0ddlm} |ƒ | _| j | j¡ | j | j¡ |  ¡  |  ¡  |  ¡  |  ¡  |  ¡  |  ¡  |  ¡  |  ¡  dS )zÀInitialize the editor by creating the underlying toolkit widget.

        Parameters
        ----------
        parent : QWidget or None
            The parent widget for this widget.
        Nr   )ÚQAudioOutput)r   r_   r`   r
   ra   rb   rc   r	   rd   re   r   r   rO   rf   Ú_set_video_urlrg   ÚsetMutedÚmutedrS   rl   Ú_audioÚsetAudioOutputÚ_update_stateÚ_update_aspect_ratioÚ_update_mutedÚ_update_volumeÚ_update_playback_rateÚ_update_notify_intervalÚ_connect_signalsZset_tooltip)r;   r<   rl   r!   r!   r%   Úinitï   s0    ÿzVideoEditor.initc                    s<   |   ¡  | jd ur.| j d ¡ ts.| j d ¡ tƒ  ¡  d S r0   )Ú_disconnect_signalsrf   rg   r   rq   r4   Údisposerh   r>   r!   r%   r{     s    
zVideoEditor.disposec                 C   s   |   ¡  dS )z;Update the editor when the object trait changes externally.N)rm   rh   r!   r!   r%   Úupdate_editor   s    zVideoEditor.update_editorc                 C   s´   | j d ur°trP| j j | j¡ | j j | j¡ | j j | j¡ | j j	 | j
¡ n0| j j | j¡ | j j | j¡ | j j | j¡ | j j | j¡ | j j | j¡ | j j | j¡ d S r0   )rf   r   ÚstateChangedrj   Ú_state_changed_emittedÚerrorÚ_error_emittedÚbufferStatusChangedÚ_buffer_status_changed_emittedÚnotifyIntervalChangedÚ _notify_interval_changed_emittedÚplaybackStateChangedÚerrorOccurredÚbufferProgressChangedÚpositionChangedÚ_position_changed_emittedÚdurationChangedÚ_duration_changed_emittedÚmediaStatusChangedÚ_media_status_changed_emittedrh   r!   r!   r%   rx   (  s8    
ÿÿÿÿÿÿÿÿzVideoEditor._connect_signalsc                 C   s´   | j d ur°trP| j j | j¡ | j j | j¡ | j j | j¡ | j j	 | j
¡ n0| j j | j¡ | j j | j¡ | j j | j¡ | j j | j¡ | j j | j¡ | j j | j¡ d S r0   )rf   r   r}   r^   r~   r   r€   r�   r‚   rƒ   r„   r…   r†   r‡   rˆ   r‰   rŠ   r‹   rŒ   r�   rh   r!   r!   r%   rz   G  s8    
ÿÿÿÿÿÿÿÿzVideoEditor._disconnect_signalsc                 C   sP   t  | j¡}tr<ddlm} | ¡ r0||ƒ| _qB|d ƒ| _n|| _| j 	¡  d S )Nr   )ÚQMediaContent)
r   ZfromUserInputr$   r   rS   rŽ   ZisValidÚmedia_contentr_   ZupdateGeometry)r;   ZqurlrŽ   r!   r!   r%   rm   f  s    zVideoEditor._set_video_urlc                 C   s   t | | _d S r0   )Úreversed_state_mapÚstate)r;   r‘   r!   r!   r%   r~   t  s    z"VideoEditor._state_changed_emittedc                 C   s6   |   ¡ � |d | _W d   ƒ n1 s(0    Y  d S ©Ng     @�@)Zupdating_valueÚposition)r;   r“   r!   r!   r%   r‰   w  s    
z%VideoEditor._position_changed_emittedc                 C   s   |d | _ d S r’   )Úduration)r;   r”   r!   r!   r%   r‹   |  s    z%VideoEditor._duration_changed_emittedc                 C   s$   |t jjkr| j ¡ | _nd| _d S ©NÚ )r   ÚErrorZNoErrorrf   ZerrorStringÚvideo_error©r;   r   r!   r!   r%   r€     s    zVideoEditor._error_emittedc                 C   s   t | j ¡  | _d S r0   )Úmedia_status_maprf   ZmediaStatusÚmedia_statusr™   r!   r!   r%   r�   …  s    z)VideoEditor._media_status_changed_emittedc                 C   s*   t r| j ¡ | _nt| j ¡ d ƒ| _d S )Nr[   )r   rf   ZbufferStatusÚbufferÚintZbufferProgressr™   r!   r!   r%   r‚   ˆ  s    z*VideoEditor._buffer_status_changed_emittedc                 C   s   |d | _ d S r’   )Únotify_interval©r;   Úintervalr!   r!   r%   r„   Ž  s    z,VideoEditor._notify_interval_changed_emittedÚaspect_ratioc                 C   s   | j d ur|  ¡  d S r0   )r_   rs   rI   r!   r!   r%   Ú_aspect_ratio_observer“  s    
z"VideoEditor._aspect_ratio_observerr:   c                 C   s.   | j d u r|  ¡  nts"tdƒ‚n|  ¡  d S )Nz"image_func is not supported on Qt6)r:   ri   r   Ú
ValueErrorrk   rI   r!   r!   r%   Ú_image_func_observer˜  s
    


z VideoEditor._image_func_observerr�   c                 C   s6   d| _ | jd ur2tr$| j | j¡ n| j | j¡ d S r•   )r˜   rf   r   ZsetMediar�   Z	setSourcerI   r!   r!   r%   Ú_media_content_observer¡  s
    
z#VideoEditor._media_content_observerro   c                 C   s   | j d ur|  ¡  d S r0   )rf   rt   rI   r!   r!   r%   Ú_muted_observerª  s    
zVideoEditor._muted_observerÚplayback_ratec                 C   s   | j d ur|  ¡  d S r0   )rf   rv   rI   r!   r!   r%   Ú_playback_rate_observer¯  s    
z#VideoEditor._playback_rate_observerr“   c                 C   s*   | j d ur&| js&| j  t| jd ƒ¡ d S ©Niè  )rf   ZupdatingÚsetPositionr�   r“   rI   r!   r!   r%   Ú_position_observer´  s    zVideoEditor._position_observerr‘   c                 C   s   | j d ur|  ¡  d S r0   )rf   rr   rI   r!   r!   r%   Ú_state_observerº  s    
zVideoEditor._state_observerÚvolumec                 C   s   | j d ur|  ¡  d S r0   )rf   ru   rI   r!   r!   r%   Ú_volume_observer¿  s    
zVideoEditor._volume_observerrž   c                 C   s   | j d ur|  ¡  d S r0   )rf   rw   rI   r!   r!   r%   Ú_notify_interval_observerÄ  s    
z%VideoEditor._notify_interval_observerc                 C   s   | j  t| j ¡ d S r0   )r_   ZsetAspectRatioModeÚaspect_ratio_mapr¡   rh   r!   r!   r%   rs   Ë  s    z VideoEditor._update_aspect_ratioc                 C   s&   t r| j | j¡ n| j | j¡ d S r0   )r   rf   rn   ro   rp   rh   r!   r!   r%   rt   Î  s    zVideoEditor._update_mutedc                 C   s   | j  | j¡ d S r0   )rf   ZsetPlaybackRater§   rh   r!   r!   r%   rv   Ô  s    z!VideoEditor._update_playback_ratec                 C   sŒ   | j dkr | j ¡  | j ¡  nh| j dkrt| j ¡ }| ¡ }| ¡ }| j ¡  | j 	|d |d ¡ | j 	||¡ n| j dkrˆ| j 
¡  d S )Nr   r   r   r    )r‘   rf   Ústopr_   Zrepaintr@   rA   rB   ZplayÚresizeÚpause)r;   rD   ÚwÚhr!   r!   r%   rr   ×  s    





zVideoEditor._update_statec                 C   sF   t  | jd t jjt jj¡}tr6| j t	|d ƒ¡ n| j
 |¡ d S )Nr\   r[   )r   ZconvertVolumer­   ZVolumeScaleZLogarithmicVolumeScaleZLinearVolumeScaler   rf   Z	setVolumer�   rp   )r;   Zlinear_volumer!   r!   r%   ru   æ  s    ýzVideoEditor._update_volumec                 C   s"   t rt| jd ƒ}| j |¡ d S r©   )r   r�   rž   rf   ZsetNotifyIntervalrŸ   r!   r!   r%   rw   ñ  s    z#VideoEditor._update_notify_interval)ArJ   rK   rL   rM   r   r   r_   r   r]   r�   r   rf   r   r¡   r   r‘   r   r“   r”   r   r›   r   rœ   r   r˜   r   ro   r­   r§   r   r:   rž   rp   ri   rk   ry   r{   r|   rx   rz   rm   r~   r‰   r‹   r€   r�   r‚   r„   r   r¢   r¤   r¥   r¦   r¨   r«   r¬   r®   r¯   rs   rt   rv   rr   ru   rw   rN   r!   r!   r>   r%   rZ   Š   sr   	

'









rZ   N)=rM   Z	pyface.qtr   Zpyface.qt.QtCorer   r   r   r   Zpyface.qt.QtGuir   r   r	   r
   rS   r   r   r   Zpyface.qt.QtMultimediaWidgetsr   Z
traits.apir   r   r   r   r   r   r   r   Ztraitsui.editors.video_editorr   r   r   Zeditorr   ZAspectRatioModeZIgnoreAspectRatioZKeepAspectRatioZKeepAspectRatioByExpandingr°   ÚStateZStoppedStateZPlayingStateZPausedStateZ	state_mapZPlaybackStateÚitemsr�   ZNoMediaZLoadingMediaZLoadedMediaZStalledMediaZBufferingMediaZBufferedMediaZ
EndOfMediaZInvalidMediarš   ZUnknownMediaStatusr,   r-   rO   rZ   r!   r!   r!   r%   Ú<module>   sL   (ýýýø
%