Encapsulation and harmonizing operators for LayoutLength
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / devel-api / layouting / layout-item-impl.cpp
index f6bd7f8..78cd532 100644 (file)
 #include <dali/public-api/object/type-registry-helper.h>
 #include <dali-toolkit/public-api/controls/control.h>
 #include <dali-toolkit/devel-api/layouting/layout-item-impl.h>
+#include <dali-toolkit/devel-api/layouting/layout-group-impl.h>
 #include <dali-toolkit/internal/layouting/layout-item-data-impl.h>
 
+namespace
+{
+
 #if defined(DEBUG_ENABLED)
-    Debug::Filter* gLayoutFilter = Debug::Filter::New( Debug::Verbose, false, "LOG_LAYOUT" );
+Debug::Filter* gLayoutFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_LAYOUT" );
 #endif
 
-namespace
-{
 const char* WIDTH_SPECIFICATION_NAME( "widthSpecification" );
 const char* HEIGHT_SPECIFICATION_NAME( "heightSpecification" );
 
@@ -77,13 +79,31 @@ void LayoutItem::Unparent()
   // Enable directly derived types to first remove children
   OnUnparent();
 
+  // Remove myself from parent
+  LayoutParent* parent = GetParent();
+  if( parent )
+  {
+    parent->Remove( *this );
+  }
+
+  // Remove parent reference
+  SetParent(nullptr);
+
   // Last, clear owner
   mImpl->mOwner = NULL;
 }
 
 void LayoutItem::SetAnimateLayout( bool animateLayout )
 {
+  auto owner = GetOwner();
+  auto actor = Actor::DownCast(owner);
+
+  DALI_LOG_INFO( gLayoutFilter, Debug::Verbose, "LayoutItem::SetAnimateLayout animateLayout(%s) owner(%s)\n", (animateLayout)?"true":"false",
+                 ( ( Actor::DownCast( owner) ) ? Actor::DownCast(owner).GetName().c_str() : "Invalid Actor" ) );
+
   mImpl->mAnimated = animateLayout;
+
+  OnAnimationStateChanged( animateLayout );
 }
 
 bool LayoutItem::IsLayoutAnimated() const
@@ -121,6 +141,8 @@ void LayoutItem::OnRegisterChildProperties( const std::string& containerType )
 
 void LayoutItem::Measure( MeasureSpec widthMeasureSpec, MeasureSpec heightMeasureSpec )
 {
+  DALI_LOG_TRACE_METHOD( gLayoutFilter );
+
   const bool forceLayout = mImpl->GetPrivateFlag( Impl::PRIVATE_FLAG_FORCE_LAYOUT );
 
   const bool specChanged =
@@ -137,38 +159,35 @@ void LayoutItem::Measure( MeasureSpec widthMeasureSpec, MeasureSpec heightMeasur
 
   const bool needsLayout = specChanged && ( !isSpecExactly || !matchesSpecSize );
 
-  if( forceLayout || needsLayout)
+  DALI_LOG_STREAM( gLayoutFilter, Debug::General, "LayoutItem::Measure("<<widthMeasureSpec<<", "<<heightMeasureSpec<<") Owner:"<<Actor::DownCast(GetOwner()).GetName() <<"  forceLayout="<<forceLayout<<", specChanged="<<specChanged<<", isSpecExactly="<<isSpecExactly<<", matchesSpecSize="<<matchesSpecSize<<", needsLayout="<<needsLayout <<std::endl <<(forceLayout||needsLayout?"  Remeasuring":"  NoChange"));
+
+  if( forceLayout || needsLayout )
   {
     mImpl->ClearPrivateFlag( Impl::PRIVATE_FLAG_MEASURED_DIMENSION_SET );
 
-    //resolveRtlPropertiesIfNeeded();
-
-    int cacheIndex = -1;  // = forceLayout ? -1 : mMeasureCache.indexOfKey(key);
-    if( cacheIndex < 0 ) //|| sIgnoreMeasureCache )
-    {
-      // measure ourselves, this should set the measured dimension flag back
-      OnMeasure( widthMeasureSpec, heightMeasureSpec );
-      mImpl->ClearPrivateFlag( Impl::PRIVATE_FLAG_MEASURE_NEEDED_BEFORE_LAYOUT );
-    }
-    else
-    {
-      mImpl->SetPrivateFlag( Impl::PRIVATE_FLAG_MEASURE_NEEDED_BEFORE_LAYOUT );
-    }
+    // measure ourselves, this should set the measured dimension flag back
+#if defined(DEBUG_ENABLED)
+    std::ostringstream o;
+    o<<widthMeasureSpec<<","<<heightMeasureSpec;
+    DALI_LOG_INFO( gLayoutFilter, Debug::General, "LayoutItem::Measure Calling %s OnMeasure( %s )\n", Actor::DownCast(GetOwner()).GetName().c_str(), o.str().c_str());
+#endif
+    OnMeasure( widthMeasureSpec, heightMeasureSpec );
+    mImpl->ClearPrivateFlag( Impl::PRIVATE_FLAG_MEASURE_NEEDED_BEFORE_LAYOUT );
 
     // flag not set, setMeasuredDimension() was not invoked, we raise an exception to warn the developer
     DALI_ASSERT_ALWAYS( mImpl->GetPrivateFlag( Impl::PRIVATE_FLAG_MEASURED_DIMENSION_SET ) &&
-                        "Layout's OnMeasure() did not set the measured dimension by calling setMeasuredDimension()" );
+                        "Layout's OnMeasure() Measured dimension flag not set" );
     mImpl->SetPrivateFlag( Impl::PRIVATE_FLAG_LAYOUT_REQUIRED );
   }
 
   mImpl->mOldWidthMeasureSpec = widthMeasureSpec;
   mImpl->mOldHeightMeasureSpec = heightMeasureSpec;
-
-  //mMeasureCache.put(key, ((long) mMeasuredWidth) << 32 | (long) mMeasuredHeight & 0xffffffffL); // suppress sign extension
 }
 
 void LayoutItem::Layout( LayoutLength l, LayoutLength t, LayoutLength r, LayoutLength b )
 {
+  DALI_LOG_TRACE_METHOD( gLayoutFilter );
+
   if( mImpl->GetPrivateFlag( Impl::PRIVATE_FLAG_MEASURE_NEEDED_BEFORE_LAYOUT ) )
   {
     OnMeasure( mImpl->mOldWidthMeasureSpec, mImpl->mOldHeightMeasureSpec );
@@ -211,7 +230,34 @@ void LayoutItem::SetMinimumHeight( LayoutLength minimumHeight )
 
 Extents LayoutItem::GetPadding() const
 {
-  return mImpl->mPadding;
+  Toolkit::Control control = Toolkit::Control::DownCast( mImpl->mOwner );
+  if( control )
+  {
+    Extents padding = control.GetProperty<Extents>( Toolkit::Control::Property::PADDING );
+
+    DALI_LOG_INFO( gLayoutFilter, Debug::Verbose, "LayoutItem::Padding for %s : (%d,%d,%d,%d) \n",
+                   control.GetName().c_str(),
+                   padding.start, padding.end, padding.top, padding.bottom
+                 );
+    return padding;
+  }
+  else
+  {
+    return Extents();
+  }
+}
+
+Extents LayoutItem::GetMargin() const
+{
+  Toolkit::Control control = Toolkit::Control::DownCast( mImpl->mOwner );
+  if ( control )
+  {
+    return control.GetProperty<Extents>( Toolkit::Control::Property::MARGIN );
+  }
+  else
+  {
+    return Extents();
+  }
 }
 
 LayoutLength LayoutItem::GetDefaultSize( LayoutLength size, MeasureSpec measureSpec )
@@ -228,6 +274,18 @@ LayoutLength LayoutItem::GetDefaultSize( LayoutLength size, MeasureSpec measureS
       break;
     }
     case MeasureSpec::Mode::AT_MOST:
+    {
+      LayoutLength tmp = specSize;
+      if( size < tmp )
+      {
+        result = size;
+      }
+      else
+      {
+        result = specSize;
+      }
+      break;
+    }
     case MeasureSpec::Mode::EXACTLY:
     {
       result = specSize;
@@ -239,6 +297,8 @@ LayoutLength LayoutItem::GetDefaultSize( LayoutLength size, MeasureSpec measureS
 
 void LayoutItem::OnMeasure( MeasureSpec widthMeasureSpec, MeasureSpec heightMeasureSpec)
 {
+  DALI_LOG_INFO( gLayoutFilter, Debug::Verbose, "LayoutItem::OnMeasure\n");
+
   SetMeasuredDimensions( GetDefaultSize( GetSuggestedMinimumWidth(), widthMeasureSpec ),
                          GetDefaultSize( GetSuggestedMinimumHeight(), heightMeasureSpec ) );
 }
@@ -247,6 +307,15 @@ void LayoutItem::OnLayout( bool changed, LayoutLength left, LayoutLength top, La
 {
 }
 
+void LayoutItem::SetParent( LayoutParent* parent )
+{
+  mImpl->mLayoutParent = parent;
+  if ( parent == nullptr )
+  {
+    mImpl->ClearPrivateFlag( Impl::PRIVATE_FLAG_FORCE_SET_FRAME );
+  }
+}
+
 LayoutParent* LayoutItem::GetParent()
 {
   return mImpl->mLayoutParent;
@@ -254,6 +323,11 @@ LayoutParent* LayoutItem::GetParent()
 
 void LayoutItem::RequestLayout()
 {
+  Toolkit::Control control = Toolkit::Control::DownCast( mImpl->mOwner );
+  if ( control )
+  {
+    DALI_LOG_INFO( gLayoutFilter, Debug::Verbose, "LayoutItem::RequestLayout %s\n", control.GetName().c_str());
+  }
   // @todo Enforce failure if called in Measure/Layout passes.
   mImpl->SetPrivateFlag( Impl::PRIVATE_FLAG_FORCE_LAYOUT );
   Toolkit::LayoutController layoutController = Toolkit::LayoutController::Get();
@@ -265,8 +339,15 @@ bool LayoutItem::IsLayoutRequested() const
   return mImpl->GetPrivateFlag( Impl::PRIVATE_FLAG_FORCE_LAYOUT );
 }
 
+void LayoutItem::SetLayoutRequested()
+{
+  mImpl->SetPrivateFlag( Impl::PRIVATE_FLAG_FORCE_LAYOUT );
+}
+
 void LayoutItem::SetMeasuredDimensions( MeasuredSize measuredWidth, MeasuredSize measuredHeight )
 {
+  DALI_LOG_STREAM( gLayoutFilter, Debug::Verbose, "LayoutItem::SetMeasuredDimensions width(" << measuredWidth.GetSize() << ") height(" << measuredHeight.GetSize() << ") \n" );
+
   mImpl->SetPrivateFlag( Impl::PRIVATE_FLAG_MEASURED_DIMENSION_SET );
   mImpl->mMeasuredWidth = measuredWidth;
   mImpl->mMeasuredHeight = measuredHeight;
@@ -299,7 +380,7 @@ LayoutLength LayoutItem::GetSuggestedMinimumWidth() const
   auto actor = Actor::DownCast(owner);
   auto naturalSize = actor ? actor.GetNaturalSize() : Vector3::ZERO;
 
-  return std::max( mImpl->mMinimumSize.GetWidth(), LayoutLength::IntType( naturalSize.width ) );
+  return std::max( mImpl->mMinimumSize.GetWidth(), LayoutLength( naturalSize.width ) );
 }
 
 LayoutLength LayoutItem::GetSuggestedMinimumHeight() const
@@ -308,7 +389,7 @@ LayoutLength LayoutItem::GetSuggestedMinimumHeight() const
   auto actor = Actor::DownCast(owner);
   auto naturalSize = actor ? actor.GetNaturalSize() : Vector3::ZERO;
 
-  return std::max( mImpl->mMinimumSize.GetHeight(), LayoutLength::IntType(naturalSize.height) );
+  return std::max( mImpl->mMinimumSize.GetHeight(), LayoutLength( naturalSize.height ) );
 }
 
 MeasuredSize LayoutItem::ResolveSizeAndState( LayoutLength size, MeasureSpec measureSpec, MeasuredSize::State childMeasuredState )
@@ -355,16 +436,16 @@ bool LayoutItem::SetFrame( LayoutLength left, LayoutLength top, LayoutLength rig
 {
   bool changed = false;
 
-  DALI_LOG_INFO( gLayoutFilter, Debug::Verbose, "LayoutItem::SetFrame(%d, %d, %d, %d)\n", left.mValue, top.mValue, right.mValue, bottom.mValue );
+  DALI_LOG_STREAM( gLayoutFilter, Debug::Verbose, "LayoutItem::SetFrame enter(" << left << ", " << top << ", " << right << ", " << bottom << ")\n" );
 
-  if( mImpl->mLeft != left || mImpl->mRight != right || mImpl->mTop != top || mImpl->mBottom != bottom )
+  if( mImpl->mLeft != left || mImpl->mRight != right || mImpl->mTop != top || mImpl->mBottom != bottom || !mImpl->GetPrivateFlag( Impl::PRIVATE_FLAG_FORCE_SET_FRAME ) )
   {
     changed = true;
 
-    auto oldWidth = mImpl->mRight - mImpl->mLeft;
-    auto oldHeight = mImpl->mBottom - mImpl->mTop;
-    auto newWidth = right - left;
-    auto newHeight = bottom - top;
+    LayoutLength oldWidth = mImpl->mRight - mImpl->mLeft;
+    LayoutLength oldHeight = mImpl->mBottom - mImpl->mTop;
+    LayoutLength newWidth = right - left;
+    LayoutLength newHeight = bottom - top;
     bool sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
 
     mImpl->mLeft = left;
@@ -372,7 +453,7 @@ bool LayoutItem::SetFrame( LayoutLength left, LayoutLength top, LayoutLength rig
     mImpl->mRight = right;
     mImpl->mBottom = bottom;
 
-    mImpl->SetPrivateFlag( Impl::PRIVATE_FLAG_HAS_BOUNDS );
+    mImpl->SetPrivateFlag( Impl::PRIVATE_FLAG_FORCE_SET_FRAME );
 
 
     // Reflect up to parent control
@@ -380,21 +461,27 @@ bool LayoutItem::SetFrame( LayoutLength left, LayoutLength top, LayoutLength rig
     auto actor = Actor::DownCast(owner);
     if( actor )
     {
+      DALI_LOG_STREAM( gLayoutFilter, Debug::Verbose, "LayoutItem::SetFrame owner(" << left << ", " << top << ", " << right << ", " << bottom << ")\n" );
+
       if( mImpl->mAnimated )
       {
         auto animation = Animation::New( 0.5f );
-        animation.AnimateTo( Property( actor, Actor::Property::POSITION ),
-                             Vector3( float(left.mValue), float(top.mValue), 0.0f ) );
-        animation.AnimateTo( Property( actor, Actor::Property::SIZE ),
-                             Vector3( right-left, bottom-top, 0.0f ) );
+        animation.AnimateTo( Property( actor, Actor::Property::POSITION_X ), left.AsInteger() );
+        animation.AnimateTo( Property( actor, Actor::Property::POSITION_Y ), top.AsInteger() );
+
+        animation.AnimateTo( Property( actor, Actor::Property::SIZE_WIDTH ), newWidth.AsInteger() );
+        animation.AnimateTo( Property( actor, Actor::Property::SIZE_HEIGHT ), newHeight.AsInteger() );
+
         animation.FinishedSignal().Connect( mSlotDelegate, &LayoutItem::OnLayoutAnimationFinished );
         animation.Play();
       }
       else
       {
         // @todo Collate into list of Property & Property::Value pairs.
-        actor.SetPosition( Vector3( float(left.mValue), float(top.mValue), 0.0f ) );
-        actor.SetSize( Vector3( right-left, bottom-top, 0.0f ) );
+        actor.SetX( left.AsInteger() );
+        actor.SetY( top.AsInteger() );
+        actor.SetProperty( Actor::Property::SIZE_WIDTH, newWidth.AsInteger() );
+        actor.SetProperty( Actor::Property::SIZE_HEIGHT, newHeight.AsInteger() );
       }
     }
 
@@ -403,6 +490,9 @@ bool LayoutItem::SetFrame( LayoutLength left, LayoutLength top, LayoutLength rig
       SizeChange( LayoutSize( newWidth, newHeight ), LayoutSize( oldWidth, oldHeight ) );
     }
   }
+
+  DALI_LOG_STREAM( gLayoutFilter, Debug::Verbose, "LayoutItem::SetFrame  exit(" << left << ", " << top << ", " << right << ", " << bottom << ")\n" );
+
   return changed;
 }
 
@@ -412,7 +502,7 @@ void LayoutItem::OnLayoutAnimationFinished( Animation& animation )
   auto actor = Actor::DownCast(owner);
   if( actor )
   {
-    actor.SetSize( Vector3( mImpl->mRight-mImpl->mLeft, mImpl->mBottom-mImpl->mTop, 0.0f ) );
+    actor.SetSize( Vector3( mImpl->mRight.AsInteger() - mImpl->mLeft.AsInteger(), mImpl->mBottom.AsInteger() - mImpl->mTop.AsInteger(), 0.0f ) );
   }
 }