Skip to content

Commit 47f9b33

Browse files
committed
feat: add a font button to change the title font
1 parent 05fd7a7 commit 47f9b33

File tree

7 files changed

+89
-6
lines changed

7 files changed

+89
-6
lines changed

pretty_gpx/common/drawing/utils/fonts.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,10 @@
1111
class FontEnum(Enum):
1212
"""Font Enum."""
1313
ANNOTATION = FontProperties(weight="bold")
14-
TITLE = FontProperties(fname=os.path.join(FONTS_DIR, "Lobster 1.4.otf"))
15-
STATS = FontProperties(fname=os.path.join(FONTS_DIR, "Lobster 1.4.otf"))
14+
TITLE = (FontProperties(family="Lobster 1.4.otf", fname=os.path.join(FONTS_DIR, "Lobster 1.4.otf")),
15+
FontProperties(family="Arial"),
16+
FontProperties(family="Verdana"),
17+
FontProperties(family="Times New Roman"),
18+
FontProperties(family="Georgia"),
19+
FontProperties(family="Comic Sans MS"))
20+
STATS = FontProperties(family="Lobster 1.4.otf", fname=os.path.join(FONTS_DIR, "Lobster 1.4.otf"))

pretty_gpx/rendering_modes/city/drawing/city_params.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,5 +123,5 @@ def default() -> "CityParams":
123123

124124
centered_title_font_color="cyan",
125125
centered_title_font_size=A4Float(mm=20),
126-
centered_title_fontproperties=FontEnum.TITLE.value
126+
centered_title_fontproperties=FontEnum.TITLE.value[0]
127127
)

pretty_gpx/rendering_modes/mountain/drawing/mountain_params.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,5 +93,5 @@ def default() -> "MountainParams":
9393

9494
centered_title_font_color="cyan",
9595
centered_title_font_size=A4Float(mm=20),
96-
centered_title_fontproperties=FontEnum.TITLE.value
96+
centered_title_fontproperties=FontEnum.TITLE.value[0]
9797
)

pretty_gpx/rendering_modes/multi_mountain/drawing/multi_mountain_params.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,5 +93,5 @@ def default() -> "MultiMountainParams":
9393

9494
centered_title_font_color="cyan",
9595
centered_title_font_size=A4Float(mm=20),
96-
centered_title_fontproperties=FontEnum.TITLE.value
96+
centered_title_fontproperties=FontEnum.TITLE.value[0]
9797
)

pretty_gpx/ui/pages/city/page.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ def update_drawer_params(self) -> None:
6464
self.drawer.params.profile_font_color = theme.background_color
6565
self.drawer.params.centered_title_font_color = theme.point_color
6666

67+
self.drawer.params.centered_title_fontproperties = self.font.value
68+
6769
for cat in [ScatterPointCategory.CITY_BRIDGE,
6870
ScatterPointCategory.CITY_POI_DEFAULT,
6971
ScatterPointCategory.CITY_POI_GREAT,

pretty_gpx/ui/pages/template/ui_input.py

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from nicegui import ui
99

1010
from pretty_gpx.common.utils.utils import safe
11-
11+
from matplotlib.font_manager import FontProperties
1212

1313
@dataclass
1414
class UiInput:
@@ -61,6 +61,73 @@ def _value_str(self) -> str:
6161
return val
6262

6363

64+
@dataclass
65+
class UiFontsMenu:
66+
"""NiceGui menu to select a font in a list."""
67+
68+
button: ui.button
69+
menu: ui.menu
70+
fonts: tuple[FontProperties, ...]
71+
72+
@classmethod
73+
def create(cls,
74+
*,
75+
fonts_l: tuple[FontProperties, ...],
76+
tooltip: str,
77+
on_change: Callable[[], Awaitable[None]]) -> Self:
78+
79+
button = ui.button(fonts_l[0].get_name()).props('outline flat') \
80+
.classes('bg-white text-black w-48 justify-start') \
81+
.style('border: 1px solid #ddd; border-radius: 4px; position: relative;')
82+
83+
with button:
84+
ui.icon('arrow_drop_down').classes('ml-auto')
85+
ui.tooltip(tooltip)
86+
87+
with ui.menu().props('anchor="bottom left"') as menu: # Ensure menu opens below button
88+
with ui.column().classes('no-wrap'):
89+
for font in fonts_l:
90+
font_name = font.get_name()
91+
92+
def create_click_handler(selected_font):
93+
async def handler():
94+
button.text = selected_font
95+
menu.close()
96+
await on_change()
97+
return handler
98+
99+
ui.button(font_name) \
100+
.props('flat no-caps text-left') \
101+
.classes('w-48') \
102+
.style(f'font-family: {font_name}; padding: 8px 16px;') \
103+
.on('click', create_click_handler(font_name))
104+
105+
def toggle_menu():
106+
if menu.visible:
107+
menu.close()
108+
else:
109+
menu.open()
110+
111+
button.on('click', toggle_menu)
112+
113+
return cls(button, menu, fonts_l)
114+
115+
116+
@dataclass
117+
class UiFontsMenuFontProp(UiFontsMenu):
118+
"""NiceGUI Str Dropdown Wrapper."""
119+
120+
@property
121+
def value(self) -> FontProperties:
122+
"""Return the value."""
123+
str_to_font = {font.get_name(): font for font in self.fonts}
124+
font_output = str_to_font.get(self.button.text, None)
125+
if font_output is None:
126+
raise KeyError
127+
else:
128+
return font_output
129+
130+
64131
@dataclass
65132
class UiDropdownStr(UiDropdown):
66133
"""NiceGUI Str Dropdown Wrapper."""

pretty_gpx/ui/pages/template/ui_manager.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@
1818
from pretty_gpx.common.drawing.utils.color_theme import LightTheme
1919
from pretty_gpx.common.drawing.utils.drawer import DrawerMultiTrack
2020
from pretty_gpx.common.drawing.utils.drawer import DrawerSingleTrack
21+
from pretty_gpx.common.drawing.utils.fonts import FontEnum
2122
from pretty_gpx.common.layout.paper_size import PAPER_SIZES
2223
from pretty_gpx.common.layout.paper_size import PaperSize
2324
from pretty_gpx.common.utils.logger import logger
2425
from pretty_gpx.common.utils.profile import profile_parallel
2526
from pretty_gpx.ui.pages.template.ui_input import UiInputFloat
2627
from pretty_gpx.ui.pages.template.ui_input import UiInputStr
28+
from pretty_gpx.ui.pages.template.ui_input import UiFontsMenuFontProp
2729
from pretty_gpx.ui.pages.template.ui_plot import UiPlot
2830
from pretty_gpx.ui.pages.template.ui_toggle import UiToggle
2931
from pretty_gpx.ui.utils.run import run_cpu_bound
@@ -123,6 +125,7 @@ class UiManager(Generic[T], ABC):
123125
paper_size: UiToggle[PaperSize]
124126
title: UiInputStr
125127
dist_km: UiInputFloat
128+
font: UiFontsMenuFontProp
126129
dark_mode_switch: ui.switch
127130
theme: UiToggle[DarkTheme] | UiToggle[LightTheme]
128131

@@ -201,6 +204,12 @@ def __init__(self, drawer: T) -> None:
201204
self.dist_km = UiInputFloat.create(label='Distance (km)', value="", on_enter=self.on_click_update,
202205
tooltip="Press Enter to override distance from GPX")
203206

207+
208+
209+
self.font = UiFontsMenuFontProp.create(fonts_l=FontEnum.TITLE.value,
210+
on_change=self.on_click_update,
211+
tooltip="Override title font")
212+
204213
#
205214
# New fields will be added here by the subclass
206215
#

0 commit comments

Comments
 (0)