B
    Ôã€\›:  ã               @   s  d Z ddlmZ ddlZddlZddlZddlZddlmZ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 ddlmZ dZd	d
dddddœZedddƒedddƒdddœZdd„ ZG dd„ dƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZ dS )aW  

Iconic Font
===========

A lightweight module handling iconic fonts.

It is designed to provide a simple way for creating QIcons from glyphs.

From a user's viewpoint, the main entry point is the ``IconicFont`` class which
contains methods for loading new iconic fonts with their character map and
methods returning instances of ``QIcon``.

é    )Úprint_functionN)ÚQObjectÚQPointÚQRectÚQt)ÚQColorÚQFontÚQFontDatabaseÚQIconÚQIconEngineÚQPainterÚQPixmap)ÚQApplication)ÚunichrFZ b06871f281fee6b241d60582ae9369b9Z 6a745dc6a0871f350b0219f5a2678838Z ab3b872bcb10de4ef62852b10e9ef103Z d6ac6c968cff1abcbf5d548828b9f6c6Z 207966b04c032d5b873fd595a211582eZ 023db9122f66b7d693bc52bcdf09e6b3)zfontawesome4.7-webfont.ttfz fontawesome5-regular-webfont.ttfzfontawesome5-solid-webfont.ttfzfontawesome5-brands-webfont.ttfzelusiveicons-webfont.ttfzmaterialdesignicons-webfont.ttfé2   é–   g      ð?)ÚcolorÚcolor_disabledÚopacityÚscale_factorc              K   sr   ddddddddd	d
ddddddddddddddddg}x4| D ],}||krX| | t |< q>d |¡}t|ƒ‚q>W dS )z?Set global defaults for the options passed to the icon painter.ÚactiveÚselectedÚdisabledÚonÚoffÚ	on_activeÚon_selectedÚon_disabledÚ
off_activeÚoff_selectedÚoff_disabledr   Úcolor_onÚ	color_offÚcolor_activeÚcolor_selectedr   Úcolor_on_selectedÚcolor_on_activeÚcolor_on_disabledÚcolor_off_selectedÚcolor_off_activeÚcolor_off_disabledÚ	animationÚoffsetr   zInvalid option '{0}'N)Ú_default_optionsÚformatÚKeyError)ÚkwargsZvalid_optionsÚkwÚerror© r3   ú4lib/python3.7/site-packages/qtawesome/iconic_font.pyÚset_global_defaults5   s    



r5   c               @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚCharIconPainterzChar icon painter.c          	   C   s&   x |D ]}|   ||||||¡ qW dS )zMain paint method.N)Ú_paint_icon)ÚselfÚiconicÚpainterÚrectÚmodeÚstateÚoptionsZoptr3   r3   r4   ÚpaintO   s    
zCharIconPainter.paintc             C   sœ  |  ¡  |d }|d }tjtj|d |d ftj|d |d ftj|d |d ftj|d	 |d
 fitjtj|d |d ftj|d |d ftj|d |d ftj|d |d fii}	|	| | \}}| t	|ƒ¡ dt
| ¡ |d  ƒ }
|d }| d¡}|dk	r| | ||¡ | | ||
¡¡ d|krht|ƒ}| |d d | ¡  |d d | ¡  ¡ | | dd¡¡ | |tjtjB |¡ | ¡  dS )zPaint a single icon.r   Úcharr!   r   r'   r   r&   r   r%   r   r"   r   r*   r    r)   r   r(   r   g      ì?r   Úprefixr+   Nr,   r   é   r   g      ð?)Zsaver
   ZOnZNormalZDisabledZActiveZSelectedZOffZsetPenr   ÚroundZheightÚgetZsetupZsetFontÚfontr   Ú	translateÚwidthZ
setOpacityZdrawTextr   ZAlignCenterZAlignVCenterZrestore)r8   r9   r:   r;   r<   r=   r>   r   r@   Zcolor_optionsZ	draw_sizerA   r+   r3   r3   r4   r7   T   sD    









zCharIconPainter._paint_iconN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r?   r7   r3   r3   r3   r4   r6   K   s   r6   c               @   s   e Zd ZdZdS )Ú	FontErrorzException for font errors.N)rH   rI   rJ   rK   r3   r3   r3   r4   rL      s   rL   c                   s0   e Zd ZdZ‡ fdd„Zdd„ Zdd„ Z‡  ZS )ÚCharIconEnginez<Specialization of QIconEngine used to draw font-based icons.c                s$   t t| ƒ ¡  || _|| _|| _d S )N)ÚsuperrM   Ú__init__r9   r:   r>   )r8   r9   r:   r>   )Ú	__class__r3   r4   rO   •   s    zCharIconEngine.__init__c             C   s   | j  | j||||| j¡ d S )N)r:   r?   r9   r>   )r8   r:   r;   r<   r=   r3   r3   r4   r?   ›   s    zCharIconEngine.paintc             C   s8   t |ƒ}| tj¡ |  t|ƒttddƒ|ƒ||¡ |S )Nr   )r   Zfillr   Ztransparentr?   r   r   r   )r8   Úsizer<   r=   Zpmr3   r3   r4   ÚpixmapŸ   s     zCharIconEngine.pixmap)rH   rI   rJ   rK   rO   r?   rR   Ú__classcell__r3   r3   )rP   r4   rM   ‘   s   rM   c                   sb   e Zd ZdZ‡ fd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d„ Z‡  ZS )Ú
IconicFontz%Main class for managing iconic fonts.c                sJ   t t| ƒ ¡  tƒ | _i | _i | _i | _i | _x|D ]}| j	|Ž  q4W dS )aÌ  IconicFont Constructor.

        Parameters
        ----------
        ``*args``: tuples
            Each positional argument is a tuple of 3 or 4 values:
            - The prefix string to be used when accessing a given font set,
            - The ttf font filename,
            - The json charmap filename,
            - Optionally, the directory containing these files. When not
              provided, the files will be looked for in ``./fonts/``.
        N)
rN   rT   rO   r6   r:   ÚpaintersÚfontnameÚcharmapÚ
icon_cacheÚ	load_font)r8   ÚargsZfargs)rP   r3   r4   rO   ª   s    
zIconicFont.__init__Nc          	   C   s>  dd„ }|dkr.t j t j t j t¡¡d¡}t ¡ dk	r:t 	t j ||¡¡}t 
|¡}|rn|d | j|< ntd t j ||¡¡ƒ‚tt j ||¡dƒ}tj||d| j|< W dQ R X ts:t |d¡}	|	dk	r:t ¡ }
tt j ||¡d	ƒ}| ¡ }|
 |¡ W dQ R X |
 ¡ }||	kr:td
 t j ||¡¡ƒ‚dS )aÔ  Loads a font file and the associated charmap.

        If ``directory`` is None, the files will be looked for in ``./fonts/``.

        Parameters
        ----------
        prefix: str
            Prefix string to be used when accessing a given font set
        ttf_filename: str
            Ttf font filename
        charmap_filename: str
            Charmap filename
        directory: str or None, optional
            Directory for font and charmap files
        c             S   s,   i }x"| D ]}t t| | dƒƒ||< q
W |S )Né   )r   Úint)ÚobjÚresultÚkeyr3   r3   r4   ÚhookÑ   s    
z"IconicFont.load_font.<locals>.hookNZfontsr   zÈFont at '{0}' appears to be empty. If you are on Windows 10, please read https://support.microsoft.com/en-us/kb/3053676 to know how to prevent Windows from blocking the fonts that come with QtAwesome.Úr)Zobject_hookÚrbzFont is corrupt at: '{0}')ÚosÚpathÚjoinÚdirnameÚrealpathÚ__file__r   Úinstancer	   ZaddApplicationFontZapplicationFontFamiliesrV   rL   r.   ÚopenÚjsonÚloadrW   ÚSYSTEM_FONTSÚ
MD5_HASHESrD   ÚhashlibZmd5ÚreadÚupdateZ	hexdigest)r8   rA   Zttf_filenameZcharmap_filenameZ	directoryr`   Zid_ZloadedFontFamiliesZcodesZttf_hashZhasherÚfZcontentZttf_calculated_hash_coder3   r3   r4   rY   À   s4    


zIconicFont.load_fontc          	   O   sÐ   d  ||¡}|| jkrÆ| di gt|ƒ ¡}|}t|ƒt|ƒkrVd  t|ƒ¡}t|ƒ‚t ¡ dk	r¶g }x4tt|ƒƒD ]$}|| }	| |  	|	||| ¡¡ qtW |}
|  
| j|
¡| j|< nt d¡ tƒ S | j| S )z>Return a QIcon object corresponding to the provided icon name.z{}{}r>   z$"options" must be a list of size {0}Nz9You need to have a running QApplication to use QtAwesome!)r.   rX   ÚpopÚlenÚ	Exceptionr   ri   ÚrangeÚappendÚ_parse_optionsÚ_icon_by_painterr:   ÚwarningsÚwarnr
   )r8   Únamesr0   Z	cache_keyZoptions_listÚgeneral_optionsr2   Zparsed_optionsÚiÚspecific_optionsZapi_optionsr3   r3   r4   Úiconü   s&    


zIconicFont.iconc                sÌ  t tf|Ž}| |¡ ddddddddd	d
ddg}| dˆ¡}| d|¡}| d|¡}| d|¡}	| d|	¡}
| d|¡}| d|	¡}| d|
¡}| d	|¡}| d
|	¡}| d|
¡}| d|¡}||||	|
|||||||dœ‰ ‡ ‡fdd„|D ƒ}|  |¡\}}| t t||fŽ ƒ¡ | d|i¡ | d¡}| d|¡ | d|d ¡ | d|d ¡ | d|d ¡ | d|d ¡ | d|d ¡ | d|¡ | d|d ¡ | d|d ¡ | d|d ¡ |S )Nr@   r   r   r   r   r   r   r   r   r   r   r    )r@   r   r   r   r   r   r   r   r   r   r   r    c                s   g | ]}ˆ   |ˆ¡‘qS r3   )rD   )Ú.0r1   )Ú	icon_dictÚnamer3   r4   ú
<listcomp><  s    z-IconicFont._parse_options.<locals>.<listcomp>rA   r   r!   r#   r$   r&   r%   r'   r   r"   r)   r(   r*   )Údictr-   rq   rD   Ú_get_prefix_charsÚzipÚ
setdefault)r8   r   r}   rƒ   r>   Zicon_kwr@   r   r   r   r   r   r   r   r   r   r   r    r|   rA   Úcharsr   r3   )r‚   rƒ   r4   rx     sZ    

zIconicFont._parse_optionsc             C   s   g }x‚|D ]z}d|kr||  d¡\}}|| jkrh|| j| krR| | j| | ¡ qzd ||¡}t|ƒ‚q„d |¡}t|ƒ‚q
tdƒ‚q
W ||fS )NÚ.z%Invalid icon name "{0}" in font "{1}"zInvalid font prefix "{0}"zInvalid icon name)ÚsplitrW   rw   r.   ru   )r8   r|   r‰   rƒ   rA   Únr2   r3   r3   r4   r†   Q  s    




zIconicFont._get_prefix_charsc             C   s2   t | j| ƒ}| |¡ |d dkr.| d¡ |S )z:Return a QFont corresponding to the given prefix and size.éÿÿÿÿÚsZSolid)r   rV   ZsetPixelSizeZsetStyleName)r8   rA   rQ   rE   r3   r3   r4   rE   e  s
    

zIconicFont.fontc             C   s   || j |< dS )a»  Associate a user-provided CharIconPainter to an icon name.

        The custom icon can later be addressed by calling
        icon('custom.NAME') where NAME is the provided name for that icon.

        Parameters
        ----------
        name: str
            name of the custom icon
        painter: CharIconPainter
            The icon painter, implementing
            ``paint(self, iconic, painter, rect, mode, state, options)``
        N)rU   )r8   rƒ   r:   r3   r3   r4   Úset_custom_iconm  s    zIconicFont.set_custom_iconc             K   s6   t tf|Ž}|| jkr,| j| }|  ||¡S tƒ S dS )z7Return the custom icon corresponding to the given name.N)r…   r-   rU   ry   r
   )r8   rƒ   r0   r>   r:   r3   r3   r4   Ú_custom_icon}  s
    

zIconicFont._custom_iconc             C   s   t | ||ƒ}t|ƒS )z3Return the icon corresponding to the given painter.)rM   r
   )r8   r:   r>   Zenginer3   r3   r4   ry   †  s    zIconicFont._icon_by_painter)N)rH   rI   rJ   rK   rO   rY   r€   rx   r†   rE   r   r   ry   rS   r3   r3   )rP   r4   rT   ¦   s   
<8	rT   )!rK   Z
__future__r   rk   rc   ro   rz   Zqtpy.QtCorer   r   r   r   Z
qtpy.QtGuir   r   r	   r
   r   r   r   Zqtpy.QtWidgetsr   Zsixr   rm   rn   r-   r5   r6   ru   rL   rM   rT   r3   r3   r3   r4   Ú<module>   s2   $

B