55import os
66import platform
77import sys
8- from pathlib import Path
98
109import qdarktheme
1110from qdarktheme .qtpy .QtCore import Qt , QTimer , Slot
1615
1716def _parse_args () -> argparse .Namespace :
1817 parser = argparse .ArgumentParser (description = "This program capture images of WidgetGallery." )
19- parser .add_argument (
20- "-d" ,
21- "--dir" ,
22- help = "Output dir path" ,
23- )
24- parser .add_argument (
25- "-i" ,
26- "--identifier" ,
27- help = "Image identifier" ,
28- )
18+ parser .add_argument ("--name" , help = "Image name" )
2919 return parser .parse_args ()
3020
3121
3222class _Application (QApplication ):
33- def __init__ (self , img_dir_path : str | None , img_identifier : str | None ) -> None :
23+ def __init__ (self , img_name : str ) -> None :
3424 super ().__init__ (sys .argv )
35- self ._img_dir_path = Path ("dist" if img_dir_path is None else img_dir_path )
36- self ._img_dir_path .mkdir (exist_ok = True )
37- if img_identifier is not None :
38- img_identifier = img_identifier .replace ("~=" , "-" )
39- self ._img_identifier = img_identifier
25+ self ._img_name = img_name .replace ("~=" , "-" )
4026
4127 if hasattr (Qt .ApplicationAttribute , "AA_UseHighDpiPixmaps" ):
4228 self .setAttribute (Qt .ApplicationAttribute .AA_UseHighDpiPixmaps ) # type: ignore
@@ -48,21 +34,14 @@ def __init__(self, img_dir_path: str | None, img_identifier: str | None) -> None
4834 def _capture_window_img (self ) -> None :
4935 for theme in qdarktheme .get_themes ():
5036 self .setStyleSheet (qdarktheme .load_stylesheet (theme ))
51- save_file_name = (
52- f"{ self ._img_identifier } -{ theme } .png" if self ._img_identifier else f"{ theme } .png"
53- )
54-
5537 self ._gallery .setGeometry (QGuiApplication .primaryScreen ().geometry ())
56- img_path = self ._img_dir_path / save_file_name
57- self ._gallery .grab ().save (img_path .as_posix ())
38+ self ._gallery .grab ().save (f"{ self ._img_name } -{ theme } .png" )
5839 self .exit ()
5940
6041
6142if __name__ == "__main__" :
6243 if platform .system () == "Linux" :
6344 os .environ ["QT_QPA_PLATFORM" ] = "offscreen"
64- args = _parse_args ()
65-
66- app = _Application (args .dir , args .identifier )
45+ app = _Application (_parse_args ().name )
6746 QTimer .singleShot (10 , app ._capture_window_img )
6847 app .exec ()
0 commit comments