Skip to content

Home Information - Your home, your data, organized.

Python

Feature/33 Dynamic initial entity sizing (#253)

Thiago Trautwein Santosb7bbd50e5ac6
diff --git a/src/hi/apps/entity/entity_manager.py b/src/hi/apps/entity/entity_manager.py
index 1753c7be..aa39f987 100644
--- a/src/hi/apps/entity/entity_manager.py
+++ b/src/hi/apps/entity/entity_manager.py
@@ -11,6 +11,7 @@ from hi.apps.common.singleton import Singleton
from hi.apps.location.path_geometry import PathGeometry
from hi.apps.entity.edit.forms import EntityPositionForm
from hi.apps.location.models import Location, LocationView
+from hi.hi_styles import EntityStyle
from .entity_pairing_manager import EntityPairingManager
from .enums import (
@@ -37,6 +38,8 @@ logger = logging.getLogger(__name__)
class EntityManager(Singleton):
+ DEFAULT_ICON_SIZE_PERCENT_OF_VIEWBOX = 10.0
+
def __init_singleton__(self):
self._change_listeners = list()
self.reload()
@@ -211,16 +214,37 @@ class EntityManager(Singleton):
# Default display in middle of current view
svg_x = location_view.svg_view_box.x + ( location_view.svg_view_box.width / 2.0 )
svg_y = location_view.svg_view_box.y + ( location_view.svg_view_box.height / 2.0 )
+ svg_scale = self._get_default_icon_scale( entity = entity, location_view = location_view )
entity_position = EntityPosition.objects.create(
entity = entity,
location = location_view.location,
svg_x = Decimal( svg_x ),
svg_y = Decimal( svg_y ),
- svg_scale = Decimal( 1.0 ),
+ svg_scale = svg_scale,
svg_rotate = Decimal( 0.0 ),
)
return entity_position
+
+ def _get_default_icon_scale( self,
+ entity : Entity,
+ location_view : LocationView ) -> Decimal:
+ view_box = location_view.svg_view_box
+ icon_view_box = EntityStyle.get_svg_icon_viewbox( entity.entity_type )
+
+ icon_max_dimension = max( icon_view_box.width, icon_view_box.height )
+ if icon_max_dimension <= 0:
+ return Decimal( '1.0' )
+
+ viewbox_min_dimension = min( view_box.width, view_box.height )
+ size_fraction = self.DEFAULT_ICON_SIZE_PERCENT_OF_VIEWBOX / 100.0
+ target_icon_size = viewbox_min_dimension * size_fraction
+ scale = target_icon_size / icon_max_dimension
+
+ position_bounds = location_view.location.svg_position_bounds
+ scale = max( position_bounds.min_scale, min( scale, position_bounds.max_scale ) )
+
+ return Decimal( str(scale) )
def add_entity_path_if_needed( self,
entity : Entity,
@@ -410,13 +434,14 @@ class EntityManager(Singleton):
# Preserve EntityPath and create/update EntityPosition
# This allows easy reversion when users change their mind
+ svg_scale = self._get_default_icon_scale( entity, location_view )
entity_position, created = EntityPosition.objects.get_or_create(
entity = entity,
location = location_view.location,
defaults = {
'svg_x': Decimal(center_x),
'svg_y': Decimal(center_y),
- 'svg_scale': Decimal(1.0),
+ 'svg_scale': svg_scale,
'svg_rotate': Decimal(0.0),
}
)
diff --git a/src/hi/apps/entity/tests/test_entity_manager.py b/src/hi/apps/entity/tests/test_entity_manager.py
index 75ef0799..98e57298 100644
--- a/src/hi/apps/entity/tests/test_entity_manager.py
+++ b/src/hi/apps/entity/tests/test_entity_manager.py
@@ -344,8 +344,11 @@ class TestEntityManager(BaseTestCase):
integration_name='test_integration',
)
- location = Location.objects.create(name='Test Location')
-
+ location = Location.objects.create(
+ name = 'Test Location',
+ svg_view_box_str = '100 200 400 300',
+ )
+
# Create a mock location view with known view box
view_box = SvgViewBox(x=100, y=200, width=400, height=300)
location_view = Mock()
@@ -366,7 +369,14 @@ class TestEntityManager(BaseTestCase):
self.assertEqual(entity_position.location, location)
self.assertEqual(entity_position.svg_x, expected_x)
self.assertEqual(entity_position.svg_y, expected_y)
- self.assertEqual(entity_position.svg_scale, Decimal('1.0'))
+ # Default icon size should be a percentage of the viewBox's smaller dimension.
+ expected_scale = (
+ Decimal('300')
+ * Decimal(str(manager.DEFAULT_ICON_SIZE_PERCENT_OF_VIEWBOX))
+ / Decimal('100')
+ / Decimal('64')
+ )
+ self.assertEqual(entity_position.svg_scale, expected_scale)
self.assertEqual(entity_position.svg_rotate, Decimal('0.0'))
# Test that calling again doesn't create duplicate
@@ -385,3 +395,49 @@ class TestEntityManager(BaseTestCase):
self.skipTest("Location models not available for testing")
return
+ def _create_scale_test_fixtures( self, viewbox_width, viewbox_height ):
+ from hi.apps.common.svg_models import SvgViewBox
+ from hi.apps.location.models import Location
+
+ entity = Entity.objects.create(
+ name = 'Scale Test Entity',
+ entity_type_str = str( EntityType.CAMERA ),
+ )
+ location = Location.objects.create(
+ name = 'Test Location',
+ svg_view_box_str = f'0 0 {viewbox_width} {viewbox_height}',
+ )
+ location_view = Mock()
+ location_view.location = location
+ location_view.svg_view_box = SvgViewBox(
+ x = 0, y = 0, width = viewbox_width, height = viewbox_height,
+ )
+ return entity, location, location_view
+
+ def test_get_default_icon_scale_zero_viewbox(self):
+ """Scale should clamp to min_scale when viewbox has zero dimension."""
+ manager = EntityManager()
+ entity, location, location_view = self._create_scale_test_fixtures( 0, 0 )
+
+ scale = manager._get_default_icon_scale( entity, location_view )
+ self.assertEqual( scale, Decimal( str( location.svg_position_bounds.min_scale ) ) )
+ return
+
+ def test_get_default_icon_scale_very_large_viewbox(self):
+ """Scale should clamp to max_scale for extremely large viewboxes."""
+ manager = EntityManager()
+ entity, location, location_view = self._create_scale_test_fixtures( 100000, 100000 )
+
+ scale = manager._get_default_icon_scale( entity, location_view )
+ self.assertEqual( scale, Decimal( str( location.svg_position_bounds.max_scale ) ) )
+ return
+
+ def test_get_default_icon_scale_very_small_viewbox(self):
+ """Scale should clamp to min_scale for tiny viewboxes."""
+ manager = EntityManager()
+ entity, location, location_view = self._create_scale_test_fixtures( 10, 10 )
+
+ scale = manager._get_default_icon_scale( entity, location_view )
+ self.assertEqual( scale, Decimal( str( location.svg_position_bounds.min_scale ) ) )
+ return
+