Refactor: Extract constants and reduce code duplication

- Extract DEFAULT_EVENT_SHAPE constant to replace 'square' magic string
- Create _index_event_refs() helper method to reduce duplication in _build_event_to_person_index()
- Use DEFAULT_EVENT_SHAPE constant in _draw_shape() default parameter

These changes improve maintainability by reducing code duplication and
replacing magic strings with named constants.
This commit is contained in:
Daniel Viegas 2025-11-30 01:20:07 +01:00
parent 72bce841f4
commit 99f0cd3236

View File

@ -129,6 +129,7 @@ FONT_SIZE_LARGE = 24
# Visual Effect Constants
MARKER_HOVER_SIZE_MULTIPLIER = 1.3
MARKER_SELECTED_SIZE_MULTIPLIER = 1.2
DEFAULT_EVENT_SHAPE = 'square' # Default shape for events without specific shape mapping
GRADIENT_BRIGHTNESS_OFFSET = 0.2
GRADIENT_DARKNESS_OFFSET = 0.1
SHADOW_OFFSET_X = 1
@ -1826,24 +1827,30 @@ class MyTimelineView(NavigationView):
continue
# Check primary events first (these take precedence)
for event_ref in person.get_primary_event_ref_list():
event_handle = event_ref.ref
# Only store if not already mapped (primary events take priority)
if event_handle not in self._event_to_person_cache:
self._event_to_person_cache[event_handle] = person
self._index_event_refs(person.get_primary_event_ref_list(), person)
# Check all events (for events not in primary list)
for event_ref in person.get_event_ref_list():
event_handle = event_ref.ref
# Only store if not already mapped
if event_handle not in self._event_to_person_cache:
self._event_to_person_cache[event_handle] = person
self._index_event_refs(person.get_event_ref_list(), person)
except (AttributeError, KeyError):
continue
except (AttributeError, KeyError) as e:
logger.warning(f"Error building event-to-person index from database: {e}", exc_info=True)
def _index_event_refs(self, event_ref_list: List[Any], person: 'Person') -> None:
"""
Index event references from a list, mapping event handles to person.
Args:
event_ref_list: List of event references from a person object.
person: The person object to associate with the events.
"""
for event_ref in event_ref_list:
event_handle = event_ref.ref
# Only store if not already mapped (to preserve priority of primary events)
if event_handle not in self._event_to_person_cache:
self._event_to_person_cache[event_handle] = person
def _find_person_for_event(self, event: 'Event') -> Optional['Person']:
"""
Find a primary person associated with an event using the cached index.
@ -3107,7 +3114,7 @@ class MyTimelineView(NavigationView):
# Get color and shape
color = EVENT_COLORS.get(event_type_value, DEFAULT_EVENT_COLOR)
shape = EVENT_SHAPES.get(event_type_value, 'square')
shape = EVENT_SHAPES.get(event_type_value, DEFAULT_EVENT_SHAPE)
marker_size = EVENT_MARKER_SIZE
# Increase size if hovered or selected
@ -3206,7 +3213,7 @@ class MyTimelineView(NavigationView):
'hexagon': self._draw_hexagon,
}
drawer = shape_drawers.get(shape, self._draw_square) # Default to square
drawer = shape_drawers.get(shape, shape_drawers[DEFAULT_EVENT_SHAPE])
drawer(context, x, y, size)
def draw_event_label(self, context: cairo.Context, x: float, y: float,