B
    ²ô`eP  ã               @   s¦  d Z dZe ZddlZddlZddlZddlmZmZ ddl	m
Z
mZ ddlmZmZmZ e e¡ZddhZG d	d
„ d
ejƒZdd„ ZG dd„ dejƒZdd„ Zdd„ Zdd„ ZG dd„ dejƒZdd„ ZG dd„ dej ƒZ!G dd„ dej ƒZ"G dd„ dej ƒZ#G dd „ d ej$ƒZ%d6d"d#„Z&ed$kr¢d%d&„ Z'd'd(„ Z(e'ƒ Z)d)d*„ Z*e+d+e&e)d,d-e*d.ƒ e(ƒ Z,e+d+e&e,d/ƒƒ e'ƒ Z)e(ƒ Z,e+d+e&e,d0d1fe)d2d3fe)d4d5ffd/ƒƒ dS )7aÞ  
formlayout
==========

Module creating Qt form dialogs/layouts to edit various type of parameters


formlayout License Agreement (MIT License)
------------------------------------------

Copyright (c) 2009 Pierre Raybaut

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
z1.0.10é    N)ÚIntegralÚReal)ÚcbookÚcolors)ÚQtGuiÚ	QtWidgetsÚQtCoreÚtitleÚlabelc               @   sZ   e Zd ZdZe ej¡Zddd„Z	dd„ Z
dd„ Ze ej¡d	d
„ ƒZe ejee¡ZdS )ÚColorButtonz$
    Color choosing push button
    Nc             C   sH   t j | |¡ |  dd¡ |  t dd¡¡ | j | j	¡ t
 ¡ | _d S )Né   é   )r   ÚQPushButtonÚ__init__ZsetFixedSizeZsetIconSizer   ZQSizeÚclickedÚconnectÚchoose_colorr   ÚQColorÚ_color)ÚselfÚparent© r   úX/home/dcms/DCMS/lib/python3.7/site-packages/matplotlib/backends/qt_editor/_formlayout.pyr   ?   s
    zColorButton.__init__c             C   s2   t j | j|  ¡ dt jj¡}| ¡ r.|  |¡ d S )NÚ )r   ZQColorDialogZgetColorr   ZparentWidgetZShowAlphaChannelZisValidÚ	set_color)r   Úcolorr   r   r   r   F   s
    
zColorButton.choose_colorc             C   s   | j S )N)r   )r   r   r   r   Ú	get_colorM   s    zColorButton.get_colorc             C   sJ   || j krF|| _ | j | j ¡ t |  ¡ ¡}| |¡ |  t |¡¡ d S )N)	r   ÚcolorChangedÚemitr   ZQPixmapZiconSizeÚfillZsetIconÚQIcon)r   r   Zpixmapr   r   r   r   P   s    

zColorButton.set_color)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ÚSignalr   r   r   r   r   r   ZSlotr   ZPropertyr   r   r   r   r   r   9   s   
	r   c             C   sH   t  ¡ }yt | ¡}W n" tk
r8   t d|  ¡ |S X |j|Ž  |S )z'Create a QColor from a matplotlib colorzIgnoring invalid color %r)r   r   ÚmcolorsZto_rgbaÚ
ValueErrorr   Z_warn_externalZsetRgbF)r   ÚqcolorZrgbar   r   r   Ú	to_qcolor\   s    
r)   c               @   s2   e Zd ZdZddd„Zdd„ Zdd„ Zd	d
„ ZdS )ÚColorLayoutz"Color-specialized QLineEdit layoutNc             C   s†   t j | ¡ t|tjƒst‚t  tj	| 
¡ dd|¡| _| jj | j¡ |  | j¡ t|ƒ| _|| j_| jj | j¡ |  | j¡ d S )NT)Ú
keep_alpha)r   ÚQHBoxLayoutr   Ú
isinstancer   r   ÚAssertionErrorÚ	QLineEditr&   Úto_hexÚgetRgbFÚlineeditZeditingFinishedr   Úupdate_colorÚ	addWidgetr   Úcolorbtnr   r   Úupdate_text)r   r   r   r   r   r   r   j   s    
zColorLayout.__init__c             C   s   |   ¡ }t|ƒ}|| j_d S )N)Útextr)   r5   r   )r   r   r(   r   r   r   r3   v   s    zColorLayout.update_colorc             C   s   | j  tj| ¡ dd¡ d S )NT)r+   )r2   ZsetTextr&   r0   r1   )r   r   r   r   r   r6   {   s    zColorLayout.update_textc             C   s
   | j  ¡ S )N)r2   r7   )r   r   r   r   r7   ~   s    zColorLayout.text)N)r!   r"   r#   r$   r   r3   r6   r7   r   r   r   r   r*   h   s
   
r*   c                s   ‡ fdd„t  ¡  ¡ D ƒS )zCheck if font is installedc                s   g | ]}t |ƒˆ kr|‘qS r   )Ústr)Ú.0Zfam)Úfontr   r   ú
<listcomp>„   s    z%font_is_installed.<locals>.<listcomp>)r   ZQFontDatabaseZfamilies)r:   r   )r:   r   Úfont_is_installed‚   s    r<   c             C   s   t | tƒrLt| ƒdkrLt| d ƒrLt | d tƒrLt | d tƒrLt | d tƒsPdS t ¡ }| \}}}}| |¡ | 	|¡ | 
|¡ | |¡ |S )zf
    Create a QFont from tuple:
        (family [string], size [int], italic [bool], bold [bool])
    é   r   é   é   é   N)r-   ÚtupleÚlenr<   r   Úboolr   ZQFontZ	setFamilyÚsetPointSizeÚ	setItalicÚsetBold)Útupr:   ÚfamilyÚsizeÚitalicÚboldr   r   r   Útuple_to_qfontˆ   s    



rL   c             C   s$   t |  ¡ ƒt|  ¡ ƒ|  ¡ |  ¡ fS )N)r8   rH   ÚintÚ	pointSizerJ   rK   )r:   r   r   r   Úqfont_to_tupleœ   s    rO   c               @   s"   e Zd ZdZddd„Zdd„ ZdS )Ú
FontLayoutzFont selectionNc             C   sB  t j | ¡ t|ƒ}|d k	s t‚t  |¡| _| j |¡ |  | jdddd¡ t  	|¡| _
| j
 d¡ tddƒtdddƒd•}| ¡ }||krž| |¡ | ¡  | j
 dd„ |D ƒ¡ | j
 | |¡¡ |  | j
dd¡ t  |  d¡|¡| _| j | ¡ ¡ |  | jdd¡ t  |  d¡|¡| _| j | ¡ ¡ |  | jdd¡ d S )Nr   r>   éÿÿÿÿTé   r   é   r?   é$   é0   éH   c             S   s   g | ]}t |ƒ‘qS r   )r8   )r9   Úsr   r   r   r;   µ   s    z'FontLayout.__init__.<locals>.<listcomp>ZItalicZBold)rT   rU   rV   )r   ÚQGridLayoutr   rL   r.   ZQFontComboBoxrH   ZsetCurrentFontr4   Ú	QComboBoxrI   ZsetEditableÚrangerN   ÚappendÚsortÚaddItemsÚsetCurrentIndexÚindexÚ	QCheckBoxÚtrrJ   Z
setCheckedrK   )r   Úvaluer   r:   ZsizelistrI   r   r   r   r   £   s,    
zFontLayout.__init__c             C   sF   | j  ¡ }| | j ¡ ¡ | | j ¡ ¡ | t| j	 
¡ ƒ¡ t|ƒS )N)rH   ZcurrentFontrE   rJ   Z	isCheckedrF   rK   rD   rM   rI   ZcurrentTextrO   )r   r:   r   r   r   Úget_fontÃ   s
    
zFontLayout.get_font)N)r!   r"   r#   r$   r   rc   r   r   r   r   rP   ¡   s   
 rP   c             C   s(   |   ¡ }|  ¡  |d¡d }|tjjkS )Nr   )r7   Ú	validatorÚvalidater   ÚQDoubleValidatorZ
Acceptable)Zeditr7   Ústater   r   r   Úis_edit_validË   s    rh   c               @   s6   e Zd Ze ¡ Zddd„Zdd„ Zdd	„ Zd
d„ Z	dS )Ú
FormWidgetr   FNc             C   sn   t j | |¡ t |¡| _g | _t  | ¡| _|sB| j 	dddd¡ |rj| j 
t  |¡¡ | j 
t  d¡¡ dS )aµ  
        Parameters
        ----------
        data : list of (label, value) pairs
            The data to be edited in the form.
        comment : str, optional

        with_margin : bool, default: False
            If False, the form elements reach to the border of the widget.
            This is the desired behavior if the FormWidget is used as a widget
            alongside with other widgets such as a QComboBox, which also do
            not have a margin around them.
            However, a margin can be desired if the FormWidget is the only
            widget within a container, e.g. a tab in a QTabWidget.
        parent : QWidget or None
            The parent widget.
        r   ú N)r   ÚQWidgetr   ÚcopyÚdeepcopyÚdataÚwidgetsZQFormLayoutÚ
formlayoutÚsetContentsMarginsÚaddRowÚQLabel)r   rn   ÚcommentÚwith_marginr   r   r   r   r   Õ   s    zFormWidget.__init__c             C   s&   |   ¡ }xt|tjƒs |  ¡ }q
W |S )zReturn FormDialog instance)r   r-   r   ÚQDialog)r   Údialogr   r   r   Ú
get_dialogñ   s    zFormWidget.get_dialogc                sþ  xö| j D ]ê\}}|d krP|d krP| j t d¡t d¡¡ | j d ¡ q
nŒ|d kr|| j t |¡¡ | j d ¡ q
n`t|ƒd k	r–t|| ƒ}nF| 	¡ t
kr¾t |¡r¾tt|ƒ| ƒ}nt|tƒrØt || ¡}nt|ttfƒr¨t|tƒrút|ƒ}| d¡}t | ¡}t|d ttfƒr@dd„ |D ƒ}dd„ |D ƒ}n|}| |¡ ||krd| |¡}n6||krz| |¡}n t|tƒsšt d|||¡ d}| |¡ n4t|tƒrät | ¡}|rÔ| tj j!¡ n| tj j"¡ nøt|tƒrt #| ¡}| $dd¡ | %|¡ nÊt|t&ƒr„t t'|ƒ| ¡}| (d¡ | )t* +|¡¡ | ,¡  -t .d	¡¡ |  /¡ ‰ ˆ  0|¡ |j1 2‡ fd
d„¡ nXt|t3j3ƒr¨t 4| ¡}| 5|¡ n4t|t3j6ƒrÌt 7| ¡}| 8|¡ nt t'|ƒ| ¡}| j ||¡ | j |¡ q
W d S )Nrj   r   c             S   s   g | ]\}}|‘qS r   r   )r9   ÚkeyZ_valr   r   r   r;     s    z$FormWidget.setup.<locals>.<listcomp>c             S   s   g | ]\}}|‘qS r   r   )r9   Ú_keyÚvalr   r   r   r;     s    z,index '%s' is invalid (label: %s, value: %s)i 6eÄi Êš;ÚCc                s   ˆ   ¡ S )N)Úupdate_buttons)r7   )rw   r   r   Ú<lambda>6  ó    z"FormWidget.setup.<locals>.<lambda>)9rn   rp   rr   r   rs   ro   r[   rL   rP   ÚlowerÚ	BLACKLISTr&   Úis_color_liker*   r)   r-   r8   r/   ÚlistrA   ÚpoprY   r]   r_   r   Ú_logÚwarningr^   rC   r`   ZsetCheckStater   ÚQtÚCheckedZ	UncheckedZQSpinBoxZsetRangeZsetValuer   ÚreprZsetCursorPositionZsetValidatorr   rf   rd   Ú	setLocaleZQLocalerx   Úregister_float_fieldZtextChangedr   ÚdatetimeZQDateTimeEditZsetDateTimeÚdateZ	QDateEditZsetDate)r   r
   rb   ÚfieldZselindexÚkeysr   )rw   r   Úsetupø   s~    














zFormWidget.setupc             C   sb  g }xVt | jƒD ]F\}\}}| j| }|d kr8qnt|ƒd k	rP| ¡ }n t|tƒsdt |¡rrt| 	¡ ƒ}nÞt|t
tfƒr¶t| ¡ ƒ}t|d t
tfƒr¬|| d }n|| }nšt|tƒrÒ| ¡ tjjk}n~t|tƒrêt| ¡ ƒ}nft|tƒrtt| 	¡ ƒƒ}nHt|tjƒr$| ¡  ¡ }n,t|tjƒr@| ¡  ¡ }ntt| 	¡ ƒƒ}| |¡ qW |S )Nr   )Ú	enumeratern   ro   rL   rc   r-   r8   r&   r‚   r7   rƒ   rA   rM   ZcurrentIndexrC   Z
checkStater   r‡   rˆ   r   rb   r   ÚfloatrŒ   ZdateTimeZtoPyDateTimer   ZtoPyDateÚevalr[   )r   Z	valuelistr_   r
   rb   rŽ   r   r   r   ÚgetB  s6    



zFormWidget.get)r   FN)
r!   r"   r#   r   r%   r}   r   rx   r   r”   r   r   r   r   ri   Ò   s
   
Jri   c               @   s.   e Zd Ze ¡ Zd	dd„Zdd„ Zdd„ ZdS )
ÚFormComboWidgetr   Nc             C   s°   t j | |¡ t  ¡ }|  |¡ t  ¡ | _| | j¡ t  | ¡| _	| | j	¡ | jj
 | j	j¡ g | _xD|D ]<\}}}| j |¡ t||| d}| j	 |¡ | j |¡ qlW d S )N)rt   r   )r   rk   r   ÚQVBoxLayoutÚ	setLayoutrY   Zcomboboxr4   ZQStackedWidgetZstackwidgetZcurrentIndexChangedr   r^   Ú
widgetlistZaddItemri   r[   )r   Údatalistrt   r   Úlayoutrn   r	   Úwidgetr   r   r   r   f  s    


zFormComboWidget.__init__c             C   s   x| j D ]}| ¡  qW d S )N)r˜   r   )r   r›   r   r   r   r   y  s    zFormComboWidget.setupc             C   s   dd„ | j D ƒS )Nc             S   s   g | ]}|  ¡ ‘qS r   )r”   )r9   r›   r   r   r   r;   ~  s    z'FormComboWidget.get.<locals>.<listcomp>)r˜   )r   r   r   r   r”   }  s    zFormComboWidget.get)r   N)	r!   r"   r#   r   r%   r}   r   r   r”   r   r   r   r   r•   c  s   
r•   c               @   s.   e Zd Ze ¡ Zd	dd„Zdd„ Zdd„ ZdS )
ÚFormTabWidgetr   Nc       	      C   s¼   t j | |¡ t  ¡ }t  ¡ | _| | j¡ | dddd¡ |  |¡ g | _	xj|D ]b\}}}t
|d ƒdkr|t||| d}nt|d|| d}| j ||¡}| j ||¡ | j	 |¡ qRW d S )Nr   r@   )rt   r   T)ru   rt   r   )r   rk   r   r–   Z
QTabWidgetZ	tabwidgetr4   rq   r—   r˜   rB   r•   ri   ZaddTabZsetTabToolTipr[   )	r   r™   rt   r   rš   rn   r	   r›   r_   r   r   r   r   „  s    

zFormTabWidget.__init__c             C   s   x| j D ]}| ¡  qW d S )N)r˜   r   )r   r›   r   r   r   r   –  s    zFormTabWidget.setupc             C   s   dd„ | j D ƒS )Nc             S   s   g | ]}|  ¡ ‘qS r   )r”   )r9   r›   r   r   r   r;   ›  s    z%FormTabWidget.get.<locals>.<listcomp>)r˜   )r   r   r   r   r”   š  s    zFormTabWidget.get)r   N)	r!   r"   r#   r   r%   r}   r   r   r”   r   r   r   r   rœ     s   
rœ   c               @   sJ   e Zd ZdZddd„Zdd„ Zdd	„ Zd
d„ Zdd„ Zdd„ Z	dd„ Z
dS )Ú
FormDialogzForm Dialogr   Nc       
      C   sP  t j | |¡ || _t|d d ttfƒr<t||| d| _n2t	|d ƒdkr^t
||| d| _nt||| d| _t  ¡ }| | j¡ g | _| j ¡  t  t jjt jjB ¡ | _}| jj | j¡ | jd k	rä| t jj¡}	|	j | j¡ |j | j¡ |j | j¡ | |¡ |  |¡ |  |¡ t|t j!ƒsBt  "¡  #¡  $t j%j&¡}|  '|¡ d S )Nr   )rt   r   r@   )(r   rv   r   Úapply_callbackr-   rƒ   rA   rœ   Ú
formwidgetrB   r•   ri   r–   r4   Úfloat_fieldsr   ÚQDialogButtonBoxÚOkZCancelÚbboxr}   r   Z	addButtonÚApplyr   ÚapplyÚacceptedÚacceptZrejectedÚrejectr—   ZsetWindowTitler   r    rk   ÚstyleZstandardIconZQStyleZSP_MessageBoxQuestionZsetWindowIcon)
r   rn   r	   rt   Úiconr   r¥   rš   r£   Z	apply_btnr   r   r   r      s:    






zFormDialog.__init__c             C   s   | j  |¡ d S )N)r    r[   )r   rŽ   r   r   r   r‹   Ê  s    zFormDialog.register_float_fieldc             C   s\   d}x| j D ]}t|ƒsd}qW x6tjjtjjfD ]"}| j |¡}|d k	r2| |¡ q2W d S )NTF)	r    rh   r   r¡   r¢   r¤   r£   ÚbuttonZ
setEnabled)r   ZvalidrŽ   Zbtn_typeZbtnr   r   r   r}   Í  s    zFormDialog.update_buttonsc             C   s   | j  ¡ | _tj | ¡ d S )N)rŸ   r”   rn   r   rv   r§   )r   r   r   r   r§   Ø  s    zFormDialog.acceptc             C   s   d | _ tj | ¡ d S )N)rn   r   rv   r¨   )r   r   r   r   r¨   Ü  s    zFormDialog.rejectc             C   s   |   | j ¡ ¡ d S )N)rž   rŸ   r”   )r   r   r   r   r¥   à  s    zFormDialog.applyc             C   s   | j S )zReturn form result)rn   )r   r   r   r   r”   ã  s    zFormDialog.get)r   r   NNN)r!   r"   r#   r$   r   r‹   r}   r§   r¨   r¥   r”   r   r   r   r   r   ž  s    
)r   r   c             C   s:   t j ¡ rt  g ¡}t| |||||ƒ}| ¡ r6| ¡ S dS )aà  
    Create form dialog and return result
    (if Cancel button is pressed, return None)

    data: datalist, datagroup
    title: str
    comment: str
    icon: QIcon instance
    parent: parent QWidget
    apply: apply callback (function)

    datalist: list/tuple of (field_name, field_value)
    datagroup: list/tuple of (datalist *or* datagroup, title, comment)

    -> one field for each member of a datalist
    -> one tab for each member of a top-level datagroup
    -> one page (of a multipage widget, each page can be selected with a combo
       box) for each member of a datagroup inside a datagroup

    Supported types for field_value:
      - int, float, str, unicode, bool
      - colors: in Qt-compatible text form, i.e. in hex format or name
                (red, ...) (automatically detected from a string)
      - list/tuple:
          * the first element will be the selected index (or value)
          * the other elements can be couples (key, value) or only values
    N)r   ZQApplicationZ
startingUpr   Úexec_r”   )rn   r	   rt   rª   r   r¥   Z_apprw   r   r   r   Úfeditè  s
    

r­   Ú__main__c               C   sT   ddddddgfddd	d
ddddgfdddddddt  ddd¡fdt   ddd¡fgS )N)r8   zthis is a stringrƒ   r   Ú1Ú3Ú4Zlist2z--)ÚnoneÚNone)z--ZDashed)z-.ZDashDot)ú-ZSolid)ZstepsZSteps)ú:ZDotted)r’   g333333ó?)NzOther:)rM   r   )r:   )ZArialé
   FT)r   z#123409)rC   Tr   iÚ  r¶   rŒ   )rŒ   r   r   r   r   r   Úcreate_datalist_example  s    r·   c              C   s"   t ƒ } | ddf| ddf| ddffS )Nz
Category 1zCategory 1 commentz
Category 2zCategory 2 commentz
Category 3zCategory 3 comment)r·   )r™   r   r   r   Úcreate_datagroup_example   s    r¸   c             C   s   t d| ƒ d S )Nzdata:)Úprint)rn   r   r   r   Ú
apply_test)  s    rº   zresult:ZExamplezThis is just an <b>example</b>.)r	   rt   r¥   zGlobal titlezTitle 1zTab 1 commentzTitle 2zTab 2 commentzTitle 3zTab 3 comment)r   r   NNN)-r$   Ú__version__Ú__license__rl   rŒ   ÚloggingZnumbersr   r   Z
matplotlibr   r   r&   Zmatplotlib.backends.qt_compatr   r   r   Ú	getLoggerr!   r…   r   r   r   r)   r,   r*   r<   rL   rO   rX   rP   rh   rk   ri   r•   rœ   rv   r   r­   r·   r¸   r™   rº   r¹   Z	datagroupr   r   r   r   Ú<module>!   sP   
#* J
&



