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:
parent
72bce841f4
commit
99f0cd3236
@ -129,6 +129,7 @@ FONT_SIZE_LARGE = 24
|
|||||||
# Visual Effect Constants
|
# Visual Effect Constants
|
||||||
MARKER_HOVER_SIZE_MULTIPLIER = 1.3
|
MARKER_HOVER_SIZE_MULTIPLIER = 1.3
|
||||||
MARKER_SELECTED_SIZE_MULTIPLIER = 1.2
|
MARKER_SELECTED_SIZE_MULTIPLIER = 1.2
|
||||||
|
DEFAULT_EVENT_SHAPE = 'square' # Default shape for events without specific shape mapping
|
||||||
GRADIENT_BRIGHTNESS_OFFSET = 0.2
|
GRADIENT_BRIGHTNESS_OFFSET = 0.2
|
||||||
GRADIENT_DARKNESS_OFFSET = 0.1
|
GRADIENT_DARKNESS_OFFSET = 0.1
|
||||||
SHADOW_OFFSET_X = 1
|
SHADOW_OFFSET_X = 1
|
||||||
@ -1826,24 +1827,30 @@ class MyTimelineView(NavigationView):
|
|||||||
continue
|
continue
|
||||||
|
|
||||||
# Check primary events first (these take precedence)
|
# Check primary events first (these take precedence)
|
||||||
for event_ref in person.get_primary_event_ref_list():
|
self._index_event_refs(person.get_primary_event_ref_list(), person)
|
||||||
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
|
|
||||||
|
|
||||||
# Check all events (for events not in primary list)
|
# Check all events (for events not in primary list)
|
||||||
for event_ref in person.get_event_ref_list():
|
self._index_event_refs(person.get_event_ref_list(), person)
|
||||||
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
|
|
||||||
|
|
||||||
except (AttributeError, KeyError):
|
except (AttributeError, KeyError):
|
||||||
continue
|
continue
|
||||||
except (AttributeError, KeyError) as e:
|
except (AttributeError, KeyError) as e:
|
||||||
logger.warning(f"Error building event-to-person index from database: {e}", exc_info=True)
|
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']:
|
def _find_person_for_event(self, event: 'Event') -> Optional['Person']:
|
||||||
"""
|
"""
|
||||||
Find a primary person associated with an event using the cached index.
|
Find a primary person associated with an event using the cached index.
|
||||||
@ -3107,7 +3114,7 @@ class MyTimelineView(NavigationView):
|
|||||||
|
|
||||||
# Get color and shape
|
# Get color and shape
|
||||||
color = EVENT_COLORS.get(event_type_value, DEFAULT_EVENT_COLOR)
|
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
|
marker_size = EVENT_MARKER_SIZE
|
||||||
|
|
||||||
# Increase size if hovered or selected
|
# Increase size if hovered or selected
|
||||||
@ -3206,7 +3213,7 @@ class MyTimelineView(NavigationView):
|
|||||||
'hexagon': self._draw_hexagon,
|
'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)
|
drawer(context, x, y, size)
|
||||||
|
|
||||||
def draw_event_label(self, context: cairo.Context, x: float, y: float,
|
def draw_event_label(self, context: cairo.Context, x: float, y: float,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user