
    Dfeu8                         d Z ddlZddlZ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
 ddlmZ ddlmZmZmZmZmZmZmZ dgZdZd	Zd
Z ej2                  e      Zd Zd Zd ZeedfdZeddfdZ 	 	 ddZ!ddZ"ddZ#y)zJ
Utilities to process and convert svg images to png using palette colors.
    N)QSize)QIcon)QApplication)IMAGES_PATHPACKAGE_PATHQRC_FILE_SUFFIXQSS_FILE_SUFFIXQSS_PATHSTYLES_SCSS_FILESVG_PATHbase_palettez
<RCC warning="WARNING! File created programmatically. All changes made in this file will be lost!">
  <qresource prefix="{resource_prefix}">
z    <file>rc/{fname}</file>zj
  </qresource>
  <qresource prefix="{style_prefix}">
      <file>{qss_file}</file>
  </qresource>
</RCC>
c                     |j                   }|j                  }|j                  }|j                  }| j	                  d      \  }}| ||dz   |z   ||dz   |z   ||dz   |z   |i}|S )zG
    Return map of files (i.e states) to color from given palette.
    .z
_disabled.z_focus.z	_pressed.)COLOR_BACKGROUND_4COLOR_ACCENT_5COLOR_ACCENT_2COLOR_TEXT_1split)	fnamepalettecolor_disabledcolor_focuscolor_pressedcolor_normalnameextfile_colorss	            9/usr/lib/python3/dist-packages/qdarkstyle/utils/images.py_get_file_color_mapr   ,   s     //N((K**M''LC ID#||c!>y3{S -	K     c                     t        | d      5 }|j                         }ddd       d}j                  ||      }t        |d      5 }|j                  |       ddd       y# 1 sw Y   DxY w# 1 sw Y   yxY w)z+
    Replace base svg with fill color.
    rNz#ff0000w)openreadreplacewrite)svg_pathtemp_svg_pathcolorfhdata
base_colornew_datas          r   _create_colored_svgr/   @   sy     
h	 wwy J||J.H	mS	! R
   s   A!A-!A*-A6c                 v   t        ||      }t        |       }|j                  |      }|j                         }t        j
                  j                  |      }t        j
                  j                  |      st	        j                  |       |j                  t        j
                  j                  |             y)z2
    Convert svg files to png files using Qt.
    N)r   r   pixmaptoImageospathdirnameexistsmakedirssaveabspath)	r(   png_pathheightwidthsizeiconr1   imgr5   s	            r   convert_svg_to_pngr@   N   sz     D?D[[F
..
Cggooh'G77>>'"
GHHRWW__X&'r    c                    t        g       }|*t        j                  d       t        j                  d       |j
                  *t        j                  d       t        j                  d       t        j                  j                  | d      }t        j                  j                  ||j
                  d      }t        j                  j                  ||j
                  d      }t        j                  d       t        j                  d	|        t        j                  d
|        t        j                  d|        t        |d      5 }|j                         }ddd       |j                         }	|	j                         D ]+  \  }
}j                  d|
z   dz   |j                               }- t        |d      5 }|j!                         ddd       t#        ||dd       ||fS # 1 sw Y   xY w# 1 sw Y   'xY w)zC
    Create palette image svg and png image on specified path.
    NzDPlease pass a palette class in order to create its associated images   $A QDarkStyle palette requires an ID!zbase_palette.svgzpalette.svgzpalette.pngzCreating palette image ...z
Base SVG: zTo SVG: zTo PNG: r"   z{{ z }}w+i  )r   _loggererrorsysexitIDr3   r4   joininfor$   r%   color_paletteitemsr&   lowerr'   r@   )base_svg_pathr4   r   _base_palette_svg_pathpalette_svg_pathpalette_png_pathr+   r,   rL   
color_namecolor_values               r   create_palette_imagerV   ^   s    	RA * 	+zz<=GGLL8JKww||D'**mDww||D'**mDLL-.LL:3456LL8,-./LL8,-./	#S	) Rwwy ))+M#0#6#6#8 M
K||EJ.68I8I8KLM 
	% 
 ')94F---  s   G, G8,G58Hc           
      	   t        g       }|*t        j                  d       t        j                  d       |j
                  *t        j                  d       t        j                  d       |s/t        j                  j                  t        |j
                  d      }t        j                         }t        j                  |       D cg c]  }|j                  d      s| }}d}dd	d
}t        j                  d       t        j                  d|         t        j                  d|        t        j                  d|        t        |      }	d}
d}g }t!               }t        |      }|j#                         D ]8  \  }}|}t        j%                  d| d|        |D ]  }|j'                  d      d   }|t(        vrt        j                  j                  | |      }t+        ||      }t        j-                  t.        j0                  dt        j                  j3                  |              |j#                         D ]  \  }}t        j                  j                  ||      }t5        |||       |j7                  d|      }t        j                  j                  ||      }t9        ||||       |
dz  }
t        j-                  t.        j0                  dt        j                  j3                  |              ||k(  st        j                  j3                  |      }t        j                  j3                  |      }d|z   dz   |z   }	 |j;                  |         |dz  }t        j%                  dt        j                  j3                  |              |j?                  |        ; t        j                  d|	        t        j                  d|        t        j                  d|        t        j                  d|
        t        j                  d|        t        j                  dt        |              t        j                  d|        yc c}w # t<        $ r Y 7w xY w)a  Create resources `rc` png image files from base svg files and palette.

    Search all SVG files in `base_svg_path` excluding IMAGE_BLACKLIST,
    change its colors using `palette` creating temporary SVG files, for each
    state generating PNG images for each size `heights`.

    Args:
        base_svg_path (str, optional): [description]. Defaults to SVG_PATH.
        rc_path (str, optional): [description].
        palette (Palette, optional): Palette.
    NBPlease pass a palette class in order to create its associated filerB   rC   rcz.svg    .pngz@2x.png)rZ   @   zCreating images ...zSVG folder: zTMP folder: zPNG folder: r   z Size HxW (px): z X r   )r   z  Working on: z   Creating: /z  Ignored blacklist: z# SVG files: z# SVG ignored: zSVG ignored: z# PNG files: z# RC links: z%# RC links in _style.scss not in RC: z#RC links in _style.scss not in RC: ) r   rE   rF   rG   rH   rI   r3   r4   rJ   r   tempfilemkdtemplistdirendswithrK   lenget_rc_links_from_scssrM   debugr   IMAGE_BLACKLISTr   logloggingNOTSETbasenamer/   r&   r@   remove
ValueErrorappend)rO   rc_pathr   rP   temp_dirf
svg_fnamesbase_heightheightsnum_svgnum_pngnum_ignorednum_ignored_listrc_listnum_rc_listr;   r   r<   	svg_fnamesvg_namer(   color_filescolor_svg_namer*   r)   	png_fnamer:   rc_basepng_baserc_names                                 r   create_imagesr      s    	RA ( 	)zz<='',,|WZZ>!HZZ6M!**V:L!MJMK G
 LL&'LL</0LL<z*+LL<y)**oGGK %&Gg,K}} '3(E7;<# "	3I s+A.H .77<<yA1)WMGNNnRWW=M=Mi=X<Y,Z[ .9->->-@ !)NE$&GGLL>$JM'-G . 6 6vs CI!ww||GY?H&}hNqLGKK-@P@PQZ@[?\0]^ ,"$''"2"27";#%77#3#3I#>"%-#"5"@!#NN73#!* q  5bgg6F6Fy6Q5RST ''	2E"	3'3R LL=	*+LL?;-01LL=!1 234LL=	*+LL<}-.LL8WGHLL6wi@AM Nr  * ! !s   
Q/!Q/Q44	R	 R	c                    g }|*t         j                  d       t        j                  d       |j                  *t         j                  d       t        j                  d       t
        j                  j                  t        |j                        }t
        j                  j                  |d      }|j                  t        z   }|j                  t        z   }t
        j                  j                  ||      }| dz   |j                  z   } |dz   |j                  z   }t         j                  d       t         j                  d|         t         j                  d	|        t         j                  d
|        t        t        j                  |            D ]M  }	t
        j                  j                  |	      d   dk(  s)|j                  t         j#                  |	             O t$        j#                  |       dj                  |      z   t&        j#                  ||      z   }
t         j                  d|        t)        |d      5 }|j+                  |
       ddd       y# 1 sw Y   yxY w)ay  
    Generate the QRC file programmatically.

    Search all RC folder for PNG images and create a QRC file.

    Args:
        resource_prefix (str, optional): Prefix used in resources.
            Defaults to 'qss_icons'.
        style_prefix (str, optional): Prefix used to this style.
            Defaults to 'qdarkstyle'.
        palette (Palette, optional): Palette.
    Nz;Please pass a palette class in order to create its qrc filerB   rC   rY   r]   zGenerating QRC file ...zResource prefix: zStyle prefix: zSearching in: r[   )r   )resource_prefix
)style_prefixqss_filezWriting in: r#   )rE   rF   rG   rH   rI   r3   r4   rJ   r   r	   r   rK   sortedr`   splitextrl   TEMPLATE_QRC_FILEformatTEMPLATE_QRC_HEADERTEMPLATE_QRC_FOOTERr$   r'   )r   r   r   filespalette_pathrm   r   qrc_fileqrc_filepathr   qrc_contentr+   s               r   generate_qrc_filer      s    E ! 	"zz<=77<<gjj9Lggll<.GzzO+HzzO+H77<<h7L%+gjj8O#%

2LLL*+LL$_$567LL>,01LL>'+, 

7+, @77E"1%/LL*111>?@
 '--o-NYYu%&(//\T\/]^K LL<~./ 
lC	  B
  s   I''I0c                    t         j                  j                  t        t              }t        |d      5 }|j                         }ddd       j                  d      }t        j                  d| z   dz         }g }|D ]L  }t        j                  ||      }|s|j                  d      }	|j                  |j                  d             N t        t        |            }|S # 1 sw Y   xY w)z
    Get all rc links from scss file returning the list of unique links.

    Args:
        pattern (str): regex pattern to find the links.

    Returns:
        list(str): list of unique links found.
    r"   Nr   ()rB   )r3   r4   rJ   r
   r   r$   r%   r   recompilesearchgrouprl   listset)
patternstyle_scss_filepathr+   r,   linescompiled_exprw   linematchr4   s
             r   rc   rc   '  s     '',,x1AB	!3	' 2wwy JJtE::cGmc12LG +		,-;;q>DNN5;;q>*	+ 3w< GN! s   C##C,c                    |*t         j                  d       t        j                  d       |j                  *t         j                  d       t        j                  d       |s.t
        j                  j                  t        |j                        }|j                  t        z   }t
        j                  j                  |      d   }d}d}d|z   |z   }d	|z   |z   }d
|z   |z   }	d|z   |z   }
d|z   |z   }d|z   |z   }d|z   |z   }d|z   |z   }t        j                         }t        j                  |       t
        j                  dk(  rdnd}| dv r2t         j                  d       	 t        j                   dd|d|	g|       | dv r1t         j                  d       	 t        j                   d|d|g|       | dv r2t         j                  d       	 t        j                   d d|d|g|       | d"v r1t         j                  d#       	 t        j                   d$|d|g|       | d&v r3t         j                  d'       	 t        j                   d(d)d*|d|
g|       | d,v rt         j                  d-       t%        |
d.      5 }|j'                         }ddd       j)                  d/d0      }t%        |d1      5 }|j+                  |       ddd       | d2vrt        j,                  |
       | d3v rrt         j                  d4       t%        |	d.      5 }|j'                         }ddd       j)                  d5d6      }t%        |d1      5 }|j+                  |       ddd       t        j                  |       y# t"        $ r t         j                  d       Y w xY w# t"        $ r t         j                  d       Y w xY w# t"        $ r t         j                  d!       Y w xY w# t"        $ r t         j                  d%       Y w xY w# t"        $ r t         j                  d+       Y w xY w# 1 sw Y   xY w# 1 sw Y   uxY w# 1 sw Y   +xY w# 1 sw Y   xY w)7a  
    Compile the QRC file converting it to _rc.py nad/or .rcc.

    When using an abstraction layer (QtPy/pyqtgraph) over a binging
    (PySide/PyQt), in the end, it changes the importing name.

    For all other `compile_for` that not 'qtpy', it prefixes the file name
    with `compile_for` value.

    Args:
        compile_for (list, optional): Prefix used in resources.
            Defaults to 'qtpy'. Possible values are 'qtpy', 'pyqtgraph',
            'pyqt', 'pyqt5', 'pyside', 'pyside2', 'qt', 'qt5', 'all'.
        qrc_path (str, optional): .qrc folder path.
            Defaults to None.
        palette (Palette, optional): Palette.
    NrX   rB   rC   r   z_rc.pyz.rccpyqt6_pyqt5_pyqt_pyside6_pyside2_pyside_ 
pyqtgraph_ntTF)pyqt	pyqtgraphallzCompiling using PyQt4 ...pyrcc4z-py3z-o)shellzYou must install pyrcc4)pyqt5r   zCompiling using PyQt5 ...pyrcc5zYou must install pyrcc5)pysider   zCompiling using PySide ...z
pyside-rcczYou must install pyside-rcc)pyside2r   zCompiling using PySide 2...zpyside2-rcczYou must install pyside2-rcc)pyside6qtpyr   zCompiling using PySide 6...zpyside6-rccz-gpythonzYou must install pyside6-rcc)r   r   zConverting for QtPy ...r"   zfrom PySide6z	from qtpyrD   )r   )r   r   zConverting for PyQtGraph ...z
from PyQt4zfrom pyqtgraph.Qt)rE   rF   rG   rH   rI   r3   r4   rJ   r   r   r   getcwdchdirr   rK   
subprocesscallFileNotFoundErrorr$   r%   r&   r'   rj   )compile_forqrc_pathr   r   filenamer   ext_cpy_file_pyqt6py_file_pyqt5py_file_pyqtpy_file_pyside6py_file_pyside2py_file_pysidepy_file_qtpypy_file_pyqtgraphold_cwdr   filefiledatas                      r   compile_qrc_filer   G  s   &  ( 	)zz<=77<<gjj9zzO+H ww)!,H
CE x'#-Mx'#-MX%+L 8+c1O 8+c1O)C/N=3&L$x/#5 iikGHHX GGtODE 2201	5OOXvx|LTYZ &&01	5OOXx}EUS ''12	9OO\68T>RZ_` ((23	:OO]HdOLTYZ 0023	:OO]D(HdO\dij o%.//3' 	#4yy{H	# ##NK@,% 	!JJx 	! k)IIo&**34,$ 	#yy{H	# ##L2EF#T* 	!dJJx 	! HHWy ! 	5MM34	5 ! 	5MM34	5 ! 	9MM78	9 ! 	:MM89	: ! 	:MM89	:	# 	#	! 	!	# 	#	! 	!s   &M4 N N> O# <P >P-5P:QQ4NNN;:N;>O O #PPP*)P*-P7:QQQ)	qss_icons
qdarkstyleN)z\/rc.*\.png)r   NN)$__doc__rg   r3   r   rG   r^   r   qtpy.QtCorer   
qtpy.QtGuir   qtpy.QtWidgetsr   r   r   r   r   r	   r
   r   r   re   r   r   r   	getLogger__name__rE   r   r/   r@   rV   r   r   rc   r    r    r   <module>r      s   
  	 	 
     '4 4 4 "" 
 2   '

H
%((  (0k!%'.T !)$ cBL AM"6r@zr    