implemented theme loading with QFontDatabase by themes with the main/fonts key

This commit is contained in:
anne 2023-06-25 00:51:23 +02:00
parent 66d6e44864
commit 906cd60ff2
4 changed files with 46 additions and 7 deletions

View file

@ -1112,7 +1112,13 @@ def themeChecker(theme):
"main/icon", "main/icon",
"main/windowtitle", "main/windowtitle",
"main/style", "main/style",
"main/fonts",
"main/background-image", "main/background-image",
"main/sounds/namealarm",
"main/sounds/alertsound",
"main/sounds/memosound",
"main/sounds/ceasesound",
"main/sounds/honk",
"main/menubar/style", "main/menubar/style",
"main/menu/menuitem", "main/menu/menuitem",
"main/menu/style", "main/menu/style",

View file

@ -1540,6 +1540,8 @@ class PesterWindow(MovingWindow):
# Load font # Load font
QtGui.QFontDatabase.addApplicationFont( QtGui.QFontDatabase.addApplicationFont(
os.path.join("fonts", "alternian", "AllisDaedric-VYWz.otf") os.path.join("fonts", "alternian", "AllisDaedric-VYWz.otf")
# ~lisanne "alternian lol" TODO: make the parsetools 'alternianTagBegin' lex part use the theme's "main/alternian-font-family" value instead of just assuming "AllisDaedric"
# This way themes can change what the alternian font looks like
) )
self.pcUpdate[str, str].connect(self.updateMsg) self.pcUpdate[str, str].connect(self.updateMsg)
@ -1990,7 +1992,26 @@ class PesterWindow(MovingWindow):
self.choosetheme = PesterChooseTheme(self.config, self.theme, self) self.choosetheme = PesterChooseTheme(self.config, self.theme, self)
self.choosetheme.exec() self.choosetheme.exec()
def initTheme(self, theme): def initTheme(self, theme):
# First doing the fonts because any style may depend on it
QtGui.QFontDatabase.removeAllApplicationFonts() # GOODBYE previous fonts
QtGui.QFontDatabase.addApplicationFont(
os.path.join("fonts", "alternian", "AllisDaedric-VYWz.otf")
) # haha oops we still need that one!!! for now!! (check `~lisanne "alternian lol" TODO` up above)
for font_path in theme["main/fonts"]:
# ~lisanne : loads fonts from the `main/fonts` key in a theme
# Note that this wont load fonts from inherited themes
# that seems fine imo, esp since u could still load them through `$path/../inheritedtheme/somefont.ttf`
PchumLog.debug("Loading font "+ font_path)
fontID = QtGui.QFontDatabase.addApplicationFont(font_path)
if fontID == -1:
PchumLog.error("Failed loading font: "+font_path)
else:
PchumLog.debug(f"Font families: {(QtGui.QFontDatabase.applicationFontFamilies(fontID))} (id: {fontID})")
self.resize(*theme["main/size"]) self.resize(*theme["main/size"])
self.setWindowIcon(PesterIcon(theme["main/icon"])) self.setWindowIcon(PesterIcon(theme["main/icon"]))
self.setWindowTitle(theme["main/windowtitle"]) self.setWindowTitle(theme["main/windowtitle"])

View file

@ -2,6 +2,7 @@
{"style": "background-repeat: no-repeat;", {"style": "background-repeat: no-repeat;",
"background-image": "$path/pcbg.png", "background-image": "$path/pcbg.png",
"size": [232, 380], "size": [232, 380],
"fonts": [],
"icon": "$path/trayicon.png", "icon": "$path/trayicon.png",
"newmsgicon": "$path/trayicon2.png", "newmsgicon": "$path/trayicon2.png",
"windowtitle": "PESTERCHUM 6.0", "windowtitle": "PESTERCHUM 6.0",

View file

@ -984,12 +984,23 @@ class pesterTheme(dict):
raise e raise e
return v return v
def pathHook(self, d): def pathHook(self, dict):
for k, v in d.items(): # This converts strings containing $path into the proper paths
if isinstance(v, str): # Honestly ive never even seen this Template stuff before. very funky!
s = Template(v) for key, value in dict.items():
d[k] = s.safe_substitute(path=self.path) if isinstance(value, str):
return d templ = Template(value)
dict[key] = templ.safe_substitute(path=self.path)
elif isinstance(value, list):
# ~lisanne : for dealing with 'main/fonts' which is an array which contains filepaths with $
# probably good to have for future additions
for idx,item in enumerate(value):
item = value[idx]
if isinstance(item, str):
# not very DRY of me >:3c
templ = Template(item)
value[idx] = templ.safe_substitute(path=self.path)
return dict
def get(self, key, default): def get(self, key, default):
keys = key.split("/") keys = key.split("/")