skin_manager.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # -*- coding: utf-8 -*-
  2. import configparser
  3. import os
  4. import sys
  5. import GdUI as UI
  6. import config_manager as config
  7. __Colors = {}
  8. __name = "default"
  9. __initialized = False
  10. __SKIN_FONT_FOLDER = "truetype"
  11. __SKIN_IMAGE_FOLDER = "images"
  12. __SKIN_CFG_COLORS_TAG = "Colors"
  13. __SKIN_CFG_FONTS_TAG = "Fonts"
  14. __SKIN_CFG_IMAGES_TAG = "Images"
  15. def __convert_html_color(hexstr):
  16. h = hexstr.lstrip('#')
  17. return tuple(int(h[i:i + 2], 16) for i in (0, 2, 4))
  18. def load_skin(name="default"):
  19. global __Colors, __name
  20. # Set some default values
  21. __Colors["background"] = (32, 32, 32)
  22. __Colors["high"] = (51, 166, 255)
  23. __Colors["text"] = (83, 83, 83)
  24. __Colors["front"] = (131, 199, 219)
  25. __Colors["url"] = (51, 166, 255)
  26. __Colors["line"] = (169, 169, 169)
  27. __Colors["title_bg"] = (228, 228, 228)
  28. __Colors["active"] = (175, 90, 0)
  29. __Colors["white"] = (255, 255, 255)
  30. __name = name
  31. # Now read from skin file
  32. cfg = configparser.ConfigParser()
  33. skin_folder = os.path.join(config.get("skinfolder"), name)
  34. cfgfile = os.path.join(skin_folder, "config.cfg")
  35. try:
  36. cfg.read(cfgfile)
  37. except Exception as e:
  38. if __name == "default":
  39. print("INTERNAL ERROR: Can't load default skin. Aborting")
  40. sys.exit(-1)
  41. # TODO: Turn that into a message box
  42. print("Skin configuration file for '{name}' is invalid, loading default skin".format(name=name))
  43. load_skin("default")
  44. return
  45. # Load skin colors
  46. if __SKIN_CFG_COLORS_TAG in cfg.sections():
  47. config_color = cfg.options(__SKIN_CFG_COLORS_TAG)
  48. for color in config_color:
  49. try:
  50. __Colors[color.lower()] = __convert_html_color(cfg.get(__SKIN_CFG_COLORS_TAG, color))
  51. except Exception:
  52. print("WARN: Skin '{name}': color value for '{color}' is invalid".format(name=name, color=color))
  53. # Load skin fonts
  54. if __SKIN_CFG_FONTS_TAG in cfg.sections():
  55. config_fonts = cfg.options(__SKIN_CFG_FONTS_TAG)
  56. for font in config_fonts:
  57. UI.FontManager.add_fontfile(font, os.path.join(skin_folder, __SKIN_FONT_FOLDER,
  58. cfg.get(__SKIN_CFG_FONTS_TAG, font)))
  59. # Load skin images
  60. if __SKIN_CFG_IMAGES_TAG in cfg.sections():
  61. config_images = cfg.options(__SKIN_CFG_IMAGES_TAG)
  62. for image in config_images:
  63. UI.ImageManager.add_image(image, os.path.join(skin_folder, __SKIN_IMAGE_FOLDER,
  64. cfg.get(__SKIN_CFG_IMAGES_TAG, image)))
  65. def get_color(name: str):
  66. name = name.lower()
  67. if name in __Colors:
  68. return __Colors[name]
  69. else:
  70. print("WARN: Skin '{name}': color '{color}' does not exist".format(name=name, color=name))
  71. return (255, 0, 0)
  72. if __initialized is False:
  73. load_skin("default")
  74. __initialized = True