Fix nested layout issue 52/182052/1
authorHeeyong Song <heeyong.song@samsung.com>
Wed, 20 Jun 2018 06:00:35 +0000 (15:00 +0900)
committerHeeyong Song <heeyong.song@samsung.com>
Wed, 20 Jun 2018 06:00:35 +0000 (15:00 +0900)
Change-Id: Ic739aa8787aa972c3fd6752f394537d15c40eb66

automated-tests/src/dali-toolkit/utc-Dali-Layouting.cpp
dali-toolkit/devel-api/layouting/layout-group-impl.cpp
dali-toolkit/devel-api/layouting/layout-group.cpp
dali-toolkit/devel-api/layouting/layout-item-impl.cpp
dali-toolkit/devel-api/layouting/layout-item-impl.h
dali-toolkit/devel-api/layouting/layout-parent-impl.h

index 1be1e66..5fbe1e7 100644 (file)
@@ -450,6 +450,67 @@ int UtcDaliLayouting_HboxLayout05(void)
   END_TEST;
 }
 
+int UtcDaliLayouting_HboxLayout06(void)
+{
+  ToolkitTestApplication application;
+  tet_infoline(" UtcDaliLayouting_HboxLayout06 - Test nested layouts");
+
+  Stage stage = Stage::GetCurrent();
+
+  auto rootControl = Control::New();
+  auto absoluteLayout = AbsoluteLayout::New();
+  DevelControl::SetLayout( rootControl, absoluteLayout );
+  rootControl.SetName( "AbsoluteLayout" );
+  stage.Add( rootControl );
+
+  auto hbox = Control::New();
+  auto hboxLayout = LinearLayout::New();
+  hboxLayout.SetOrientation( LinearLayout::Orientation::HORIZONTAL );
+  DevelControl::SetLayout( hbox, hboxLayout );
+  hbox.SetName( "HBox" );
+
+  std::vector< Control > controls;
+  controls.push_back( CreateLeafControl( 40, 40 ) );
+  controls.push_back( CreateLeafControl( 60, 40 ) );
+
+  for( auto&& iter : controls )
+  {
+    hbox.Add( iter );
+  }
+  hbox.SetParentOrigin( ParentOrigin::CENTER );
+  hbox.SetAnchorPoint( AnchorPoint::CENTER );
+  rootControl.Add( hbox );
+
+  // Ensure layouting happens
+  application.SendNotification();
+  application.Render();
+
+  // hbox centers elements vertically, it fills test harness stage, which is 480x800.
+  // hbox left justifies elements
+  DALI_TEST_EQUALS( controls[0].GetProperty<Vector3>( Actor::Property::POSITION ), Vector3( 0.0f, 0.0f, 0.0f ), 0.0001f, TEST_LOCATION );
+  DALI_TEST_EQUALS( controls[1].GetProperty<Vector3>( Actor::Property::POSITION ), Vector3( 40.0f, 0.0f, 0.0f ), 0.0001f, TEST_LOCATION );
+
+  DALI_TEST_EQUALS( controls[0].GetProperty<Vector3>( Actor::Property::SIZE ), Vector3( 40.0f, 40.0f, 0.0f ), 0.0001f, TEST_LOCATION );
+  DALI_TEST_EQUALS( controls[1].GetProperty<Vector3>( Actor::Property::SIZE ), Vector3( 60.0f, 40.0f, 0.0f ), 0.0001f, TEST_LOCATION );
+
+  DALI_TEST_EQUALS( hbox.GetProperty<Vector3>( Actor::Property::SIZE ), Vector3( 100.0f, 40.0f, 0.0f ), 0.0001f, TEST_LOCATION );
+
+  // Change a layout
+  auto newHBoxLayout = LinearLayout::New();
+  newHBoxLayout.SetOrientation( LinearLayout::Orientation::HORIZONTAL );
+  DevelControl::SetLayout( hbox, newHBoxLayout );
+
+  hbox.SetProperty( Toolkit::LayoutItem::ChildProperty::WIDTH_SPECIFICATION, ChildLayoutData::MATCH_PARENT );
+  hbox.SetProperty( Toolkit::LayoutItem::ChildProperty::HEIGHT_SPECIFICATION, ChildLayoutData::MATCH_PARENT );
+
+  application.SendNotification();
+  application.Render();
+
+  DALI_TEST_EQUALS( hbox.GetProperty<Vector3>( Actor::Property::SIZE ), Vector3( 480.0f, 800.0f, 0.0f ), 0.0001f, TEST_LOCATION );
+
+  END_TEST;
+}
+
 // Padding tests
 
 int UtcDaliLayouting_HboxLayout_Padding01(void)
index d6ab337..1f643cc 100644 (file)
@@ -71,6 +71,8 @@ Toolkit::LayoutGroup::LayoutId LayoutGroup::Add( LayoutItem& child )
   childLayout.child = &child;
   mImpl->mChildren.emplace_back( childLayout );
 
+  child.SetParent( this );
+
   auto owner = child.GetOwner();
 
   // If the owner does not have any LayoutItem child properties, add them
@@ -406,6 +408,20 @@ void LayoutGroup::OnInitialize()
     DevelActor::ChildAddedSignal( control ).Connect( mSlotDelegate, &LayoutGroup::ChildAddedToOwner );
     DevelActor::ChildRemovedSignal( control ).Connect( mSlotDelegate, &LayoutGroup::ChildRemovedFromOwner );
     DevelHandle::PropertySetSignal( control ).Connect( mSlotDelegate, &LayoutGroup::OnOwnerPropertySet );
+
+    if( control.GetParent() )
+    {
+      auto parent = Toolkit::Control::DownCast( control.GetParent() );
+      if( parent )
+      {
+        auto parentLayout = Toolkit::LayoutGroup::DownCast( DevelControl::GetLayout( parent ) );
+        if( parentLayout )
+        {
+          Internal::LayoutGroup& parentLayoutImpl = GetImplementation( parentLayout );
+          parentLayoutImpl.Add( *this );
+        }
+      }
+    }
   }
 }
 
@@ -416,7 +432,19 @@ void LayoutGroup::OnRegisterChildProperties( const std::string& containerType )
 
 void LayoutGroup::OnUnparent()
 {
+  // Remove children
   RemoveAll();
+
+  // Remove myself from parent
+  LayoutParent* parent = GetParent();
+  if( parent )
+  {
+    LayoutGroupPtr parentGroup( dynamic_cast< LayoutGroup* >( parent ) );
+    if( parentGroup )
+    {
+      parentGroup->Remove( *this );
+    }
+  }
 }
 
 void LayoutGroup::ChildAddedToOwner( Actor child )
index dd36ddc..b13da31 100644 (file)
@@ -27,6 +27,11 @@ LayoutGroup::LayoutGroup()
 {
 }
 
+LayoutGroup LayoutGroup::DownCast( BaseHandle handle )
+{
+  return LayoutGroup( dynamic_cast< Dali::Toolkit::Internal::LayoutGroup* >( handle.GetObjectPtr() ) );
+}
+
 LayoutGroup::LayoutId LayoutGroup::Add( LayoutItem& child )
 {
   return GetImplementation( *this ).Add( GetImplementation(child) );
index f7a6119..409ff46 100644 (file)
@@ -284,6 +284,11 @@ void LayoutItem::OnLayout( bool changed, LayoutLength left, LayoutLength top, La
 {
 }
 
+void LayoutItem::SetParent( LayoutParent* parent )
+{
+  mImpl->mLayoutParent = parent;
+}
+
 LayoutParent* LayoutItem::GetParent()
 {
   return mImpl->mLayoutParent;
index e59ca96..fe43f07 100644 (file)
@@ -168,6 +168,11 @@ public:
   static LayoutLength GetDefaultSize( LayoutLength size, MeasureSpec measureSpec );
 
   /**
+   * @copydoc LayoutParent::SetParent
+   */
+  virtual void SetParent( LayoutParent* parent ) override;
+
+  /**
    * @copydoc LayoutParent::GetParent
    */
   virtual LayoutParent* GetParent() override;
index 392a2ab..4ce1ab0 100644 (file)
@@ -34,6 +34,11 @@ class DALI_IMPORT_API LayoutParent
 {
 public:
   /**
+   * Set the parent of this layout.
+   */
+  virtual void SetParent( LayoutParent* parent ) = 0;
+
+  /**
    * Get the parent of this layout.
    */
   virtual LayoutParent* GetParent() = 0;