X-Git-Url: http://review.tizen.org/git/?p=platform%2Fcore%2Fuifw%2Fdali-toolkit.git;a=blobdiff_plain;f=dali-toolkit%2Fdevel-api%2Flayouting%2Flayout-group-impl.cpp;h=835aa66a3ceb10c54a5cee8100ea9e4faea70cf0;hp=d9ff6ffdc9e12e500ed6719da575275e7e264fb9;hb=16a6adbc9245b63956542dd3e0b811c5f469a3f6;hpb=1c3258c8dbc37546ff31137462758a8b3323bdff diff --git a/dali-toolkit/devel-api/layouting/layout-group-impl.cpp b/dali-toolkit/devel-api/layouting/layout-group-impl.cpp index d9ff6ff..835aa66 100644 --- a/dali-toolkit/devel-api/layouting/layout-group-impl.cpp +++ b/dali-toolkit/devel-api/layouting/layout-group-impl.cpp @@ -27,11 +27,12 @@ #include #include #include +#include namespace { #if defined(DEBUG_ENABLED) -Debug::Filter* gLogFilter = Debug::Filter::New( Debug::Concise, false, "LOG_LAYOUT" ); +Debug::Filter* gLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_LAYOUT" ); #endif } @@ -48,10 +49,17 @@ LayoutGroup::LayoutGroup() { } +LayoutGroupPtr LayoutGroup::New( Handle& owner ) +{ + LayoutGroupPtr layoutPtr = new LayoutGroup(); + return layoutPtr; +} + LayoutGroup::~LayoutGroup() { // An object with a unique_ptr to an opaque structure must define it's destructor in the translation unit // where the opaque structure is defined. It cannot use the default method in the header file. + RemoveAll(); } Toolkit::LayoutGroup::LayoutId LayoutGroup::Add( LayoutItem& child ) @@ -71,6 +79,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 @@ -98,7 +108,7 @@ void LayoutGroup::Remove( Toolkit::LayoutGroup::LayoutId childId ) { if( iter->layoutId == childId ) { - OnChildRemove( *iter->child.Get() ); + RemoveChild( *iter->child.Get() ); mImpl->mChildren.erase(iter); break; } @@ -112,7 +122,7 @@ void LayoutGroup::Remove( LayoutItem& child ) { if( iter->child.Get() == &child ) { - OnChildRemove( *iter->child.Get() ); + RemoveChild( *iter->child.Get() ); mImpl->mChildren.erase(iter); break; } @@ -120,11 +130,54 @@ void LayoutGroup::Remove( LayoutItem& child ) RequestLayout(); } +Toolkit::LayoutGroup::LayoutId LayoutGroup::Insert( LayoutItem& target, LayoutItem& child ) +{ + LayoutParent* oldParent = child.GetParent(); + if( oldParent ) + { + LayoutGroupPtr parentGroup( dynamic_cast< LayoutGroup* >( oldParent ) ); + if( parentGroup ) + { + parentGroup->Remove( child ); + } + } + + // Find target position + std::vector< Impl::ChildLayout >::iterator position; + for( auto iter = mImpl->mChildren.begin(); iter != mImpl->mChildren.end(); ++iter ) + { + if( iter->child.Get() == &target ) + { + position = iter; + break; + } + } + + Impl::ChildLayout childLayout; + childLayout.layoutId = mImpl->mNextLayoutId++; + childLayout.child = &child; + mImpl->mChildren.insert( position, childLayout ); + + child.SetParent( this ); + + auto owner = child.GetOwner(); + + // Inform deriving classes that this child has been added + OnChildAdd( *childLayout.child.Get() ); + + // Now listen to future changes to the child properties. + DevelHandle::PropertySetSignal(owner).Connect( this, &LayoutGroup::OnSetChildProperties ); + + RequestLayout(); + + return childLayout.layoutId; +} + void LayoutGroup::RemoveAll() { for( auto iter = mImpl->mChildren.begin() ; iter != mImpl->mChildren.end() ; ) { - OnChildRemove( *iter->child.Get() ); + RemoveChild( *iter->child.Get() ); iter = mImpl->mChildren.erase(iter); } } @@ -182,7 +235,7 @@ void LayoutGroup::DoRegisterChildProperties( const std::string& containerType ) void LayoutGroup::OnSetChildProperties( Handle& handle, Property::Index index, Property::Value value ) { - DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::OnSetChildProperties\n"); + DALI_LOG_STREAM( gLogFilter, Debug::Verbose, "LayoutGroup::OnSetChildProperties property(" << handle.GetPropertyName(index) << ")\n" ); if ( ( ( index >= CHILD_PROPERTY_REGISTRATION_START_INDEX ) && ( index <= CHILD_PROPERTY_REGISTRATION_MAX_INDEX ) ) @@ -190,12 +243,11 @@ void LayoutGroup::OnSetChildProperties( Handle& handle, Property::Index index, P ( index == Toolkit::Control::Property::MARGIN || index == Toolkit::Control::Property::PADDING ) ) { // If any child properties are set, must perform relayout - RequestLayout(); for( auto&& child : mImpl->mChildren ) { if( child.child->GetOwner() == handle ) { - child.child->SetLayoutRequested(); + child.child->RequestLayout(); break; } } @@ -234,10 +286,12 @@ void LayoutGroup::MeasureChild( LayoutItemPtr child, #if defined( DEBUG_ENABLED ) if ( control ) { - DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::MeasureChild natural size(%f, %f)\n", control.GetNaturalSize().width, control.GetNaturalSize().height ); + DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::MeasureChild(%s) natural size(%f, %f)\n", + control.GetName().c_str(), control.GetNaturalSize().width, control.GetNaturalSize().height ); } #endif + // Get last stored width and height specifications for the child auto desiredWidth = childOwner.GetProperty( Toolkit::LayoutItem::ChildProperty::WIDTH_SPECIFICATION ); auto desiredHeight = childOwner.GetProperty( Toolkit::LayoutItem::ChildProperty::HEIGHT_SPECIFICATION ); @@ -268,13 +322,13 @@ void LayoutGroup::MeasureChildWithMargins( LayoutItemPtr child, DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::MeasureChildWithMargins desiredWidth(%d)\n", desiredWidth ); MeasureSpec childWidthMeasureSpec = GetChildMeasureSpec( parentWidthMeasureSpec, - padding.start + padding.end + + LayoutLength( padding.start + padding.end ) + widthUsed, desiredWidth ); DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::MeasureChildWithMargins desiredHeight(%d)\n", desiredHeight ); MeasureSpec childHeightMeasureSpec = GetChildMeasureSpec( parentHeightMeasureSpec, - padding.top + padding.bottom + + LayoutLength( padding.top + padding.bottom )+ heightUsed, desiredHeight ); child->Measure( childWidthMeasureSpec, childHeightMeasureSpec ); @@ -289,9 +343,9 @@ MeasureSpec LayoutGroup::GetChildMeasureSpec( auto specMode = measureSpec.GetMode(); LayoutLength specSize = measureSpec.GetSize(); - auto size = std::max( LayoutLength(0), specSize - padding ); // reduce available size by the owners padding + LayoutLength size = std::max( LayoutLength(0), specSize - padding ); // reduce available size by the owners padding - MeasureSpec::IntType resultSize = 0; + LayoutLength resultSize = 0; MeasureSpec::Mode resultMode = MeasureSpec::Mode::UNSPECIFIED; switch( specMode ) @@ -383,8 +437,7 @@ MeasureSpec LayoutGroup::GetChildMeasureSpec( } } - DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::GetChildMeasureSpec resultSize(%u)\n", resultSize ); - + DALI_LOG_STREAM( gLogFilter, Debug::Verbose, "LayoutGroup::GetChildMeasureSpec resultSize(" << resultSize << ")\n" ); //noinspection ResourceType return MeasureSpec( resultSize, resultMode ); @@ -400,12 +453,51 @@ void LayoutGroup::OnInitialize() // Take ownership of existing children for( unsigned int childIndex = 0 ; childIndex < control.GetChildCount(); ++childIndex ) { - ChildAddedToOwner( control.GetChildAt( childIndex ) ); + ChildAddedToOwnerImpl( control.GetChildAt( childIndex ) ); } 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 ); + + unsigned int count = parent.GetChildCount(); + unsigned int index = static_cast< unsigned int >( control.GetProperty< int >( DevelActor::Property::SIBLING_ORDER ) ); + + // Find insertion position + while( ++index < count ) + { + auto sibling = Toolkit::Control::DownCast( parent.GetChildAt( index ) ); + if( sibling ) + { + auto siblingLayout = DevelControl::GetLayout( sibling ); + if( siblingLayout ) + { + Internal::LayoutItem& siblingLayoutImpl = GetImplementation( siblingLayout ); + parentLayoutImpl.Insert( siblingLayoutImpl, *this ); + break; + } + } + } + + if( index >= count ) + { + parentLayoutImpl.Add( *this ); + } + } + } + } + + RequestLayout( Dali::Toolkit::LayoutTransitionData::Type::ON_OWNER_SET ); } } @@ -416,14 +508,41 @@ void LayoutGroup::OnRegisterChildProperties( const std::string& containerType ) void LayoutGroup::OnUnparent() { + // Remove children RemoveAll(); + + auto control = Toolkit::Control::DownCast( GetOwner() ); + if( control ) + { + DevelActor::ChildAddedSignal( control ).Disconnect( mSlotDelegate, &LayoutGroup::ChildAddedToOwner ); + DevelActor::ChildRemovedSignal( control ).Disconnect( mSlotDelegate, &LayoutGroup::ChildRemovedFromOwner ); + DevelHandle::PropertySetSignal( control ).Disconnect( mSlotDelegate, &LayoutGroup::OnOwnerPropertySet ); + } +} + +void LayoutGroup::RemoveChild( LayoutItem& item ) +{ + item.SetParent( nullptr ); + OnChildRemove( item ); } void LayoutGroup::ChildAddedToOwner( Actor child ) { + ChildAddedToOwnerImpl( child ); + RequestLayout( Dali::Toolkit::LayoutTransitionData::Type::ON_CHILD_ADD, child, Actor() ); +} + +void LayoutGroup::ChildAddedToOwnerImpl( Actor child ) +{ LayoutItemPtr childLayout; Toolkit::Control control = Toolkit::Control::DownCast( child ); - DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::ChildAddedToOwner(%s)\n", control.GetName().c_str() ); + +#if defined(DEBUG_ENABLED) + auto parent = Toolkit::Control::DownCast( GetOwner() ); + DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::ChildAddedToOwner child control(%s) owner control(%s)\n", + control?control.GetName().c_str():"Invalid", + parent?parent.GetName().c_str():"Invalid" ); +#endif if( control ) // Can only support adding Controls, not Actors to layout { @@ -433,17 +552,52 @@ void LayoutGroup::ChildAddedToOwner( Actor child ) if( ! childLayout ) { - // If the child doesn't already have a layout, then create a LayoutItem for it. - childLayout = LayoutItem::New( control ); - childLayout->SetAnimateLayout( IsLayoutAnimated() ); // @todo this essentially forces animation inheritance. Bad? -#if defined(DEBUG_ENABLED) - auto desiredSize = control.GetNaturalSize(); - DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::ChildAddedToOwner desiredSize(%f,%f) (naturalSize)\n", desiredSize.width, desiredSize.height ); -#endif + // If the child doesn't already have a layout, then create a LayoutItem or LayoutGroup for it. + // If control behaviour flag set to Layout then set a LayoutGroup. + if( DevelControl::IsLayoutingRequired( control ) ) + { + DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::ChildAddedToOwner Creating default LayoutGroup for control:%s\n", + control?control.GetName().c_str():"Invalid" ); + childLayout = LayoutGroup::New( control ); + } + else + { + DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::ChildAddedToOwner Creating default LayoutItem for control:%s\n", + control?control.GetName().c_str():"Invalid" ); + childLayout = LayoutItem::New( control ); + childLayout->SetAnimateLayout( IsLayoutAnimated() ); // forces animation inheritance. + } + + DALI_LOG_STREAM( gLogFilter, Debug::Verbose, "LayoutGroup::ChildAddedToOwner child control:" << control.GetName() << + " desiredWidth: " << control.GetNaturalSize().width << + " desiredHeight:" << control.GetNaturalSize().height ); + childControlDataImpl.SetLayout( *childLayout.Get() ); + Vector3 size = child.GetTargetSize(); + // If the size of the control is set explicitly make sure that the control size + // stays the same after the layout except it is over written with match parent specs. + if ( size.x != 0 ) + { + childLayout->SetMinimumWidth( size.x ); + } + + if ( size.y != 0 ) + { + childLayout->SetMinimumHeight( size.y ); + } // Default layout data will be generated by Add(). } + else + { + DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::ChildAddedToOwner child(%s) already has a Layout\n", control.GetName().c_str() ); + LayoutGroupPtr layoutGroup( dynamic_cast< LayoutGroup* >( childLayout.Get() ) ); + if( !layoutGroup ) + { + // Set only in case of leaf children + childLayout->SetAnimateLayout( IsLayoutAnimated() ); + } + } Add( *childLayout.Get() ); } @@ -460,6 +614,7 @@ void LayoutGroup::ChildRemovedFromOwner( Actor child ) if( childLayout ) { Remove( *childLayout.Get() ); + RequestLayout( Dali::Toolkit::LayoutTransitionData::Type::ON_CHILD_REMOVE, child, Actor() ); } } } @@ -480,6 +635,197 @@ void LayoutGroup::OnOwnerPropertySet( Handle& handle, Property::Index index, Pro } } +void LayoutGroup::OnAnimationStateChanged( bool animateLayout ) +{ + // Change children's animation state + for( auto&& child : mImpl->mChildren ) + { + LayoutGroupPtr parentGroup( dynamic_cast< LayoutGroup* >( child.child.Get() ) ); + if( ! parentGroup ) + { + // Change state only in case of leaf children + child.child->SetAnimateLayout( animateLayout ); + } + } +} + +void LayoutGroup::OnMeasure( MeasureSpec widthMeasureSpec, MeasureSpec heightMeasureSpec ) +{ + auto childCount = GetChildCount(); + + DALI_LOG_STREAM( gLogFilter, Debug::Verbose, + "LayoutGroup::OnMeasure Actor Id:" << Actor::DownCast(GetOwner()).GetId() << + " Owner:" << Actor::DownCast(GetOwner()).GetName() << + " Child Count:" << childCount << + " MeasureSpecs( width:"< 0 ) + { + for( unsigned int i=0; iGetOwner()); + + // If child control has children check if a ResizePolicy is set on it. A LayoutItem could be a legacy container. + // A legacy container would need it's ResizePolicy to be applied as a MeasureSpec. + + // Check below will be true for legacy containers and controls with layout required set. + // Other layouts will have their own OnMeasure (a checked requirement) hence not execute LayoutGroup::OnMeasure. + // Controls which have set layout required will not be legacy controls hence should not have a ResizePolicy set. + // Only need to map the resize policy the first time as the Layouting system will then set it to FIXED. + if( childControl.GetChildCount() > 0 && ! mImpl->mResizePolicyMapped ) + { + // First pass, Static mappings that are not dependant on parent + SizeNegotiationMapper::SetLayoutParametersUsingResizePolicy( childControl, childLayout, Dimension::WIDTH ); + SizeNegotiationMapper::SetLayoutParametersUsingResizePolicy( childControl, childLayout, Dimension::HEIGHT ); + mImpl->mResizePolicyMapped = true; + } + + // Second pass, if any mappings were not possible due to parent size dependancies then calculate an exact desired size for child + if( true == childLayout->IsResizePolicyRequired() ) // No need to test child count as this flag would only be set if control had children. + { + // Get last stored width and height specifications for the child + LayoutLength desiredWidth = childControl.GetProperty( Toolkit::LayoutItem::ChildProperty::WIDTH_SPECIFICATION ); + LayoutLength desiredHeight = childControl.GetProperty( Toolkit::LayoutItem::ChildProperty::HEIGHT_SPECIFICATION ); + + DALI_LOG_INFO( gLogFilter, Debug::General, "LayoutGroup::MeasureChild Initial desired size pre ResizePolicy(%f,%f)\n", desiredWidth.AsInteger(), desiredHeight.AsInteger() ); + + childLayout->SetResizePolicyRequired( false ); // clear flag incase in case of changes before next Measure + SizeNegotiationMapper::GetSizeofChildForParentDependentResizePolicy( childControl, widthMeasureSpec, heightMeasureSpec, desiredWidth, desiredHeight ); + + // Parent dependant ResizePolicies become exact sizes so are now set on the child before it's measured. + childControl.SetProperty( Toolkit::LayoutItem::ChildProperty::WIDTH_SPECIFICATION, desiredWidth.AsInteger() ); + childControl.SetProperty( Toolkit::LayoutItem::ChildProperty::HEIGHT_SPECIFICATION, desiredHeight.AsInteger() ); + + DALI_LOG_INFO( gLogFilter, Debug::General, " LayoutGroup::OnMeasure ResizePolicy Required resulting size(%f,%f)\n", desiredWidth.AsInteger(), desiredHeight.AsInteger() ); + } + + // Get size of child + MeasureChild( childLayout, widthMeasureSpec, heightMeasureSpec ); + LayoutLength childWidth = childLayout->GetMeasuredWidth(); + LayoutLength childHeight = childLayout->GetMeasuredHeight(); + + Extents childMargin = childLayout->GetMargin(); + DALI_LOG_STREAM( gLogFilter, Debug::Verbose, "LayoutGroup::OnMeasure child " << childControl.GetName().c_str() << " width[" << childWidth << "] height[" << childHeight << "]\n" ); + + layoutWidth = std::max( layoutWidth, childWidth + childMargin.start + childMargin.end ); + layoutHeight = std::max( layoutHeight, childHeight + childMargin.top + childMargin.bottom ); + DALI_LOG_STREAM( gLogFilter, Debug::Verbose, "LayoutGroup::OnMeasure calculated child width[" << layoutWidth << "] height[" << layoutHeight << "]\n" ); + } + else + { + DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::OnMeasure Not a layout\n" ); + } + } + + Extents padding = GetPadding(); + layoutWidth += padding.start + padding.end; + layoutHeight += padding.top + padding.bottom; + } + else + { + DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::OnMeasure Getting default size as a leaf\n" ); + // LayoutGroup does not contain any children so must be a leaf + layoutWidth = GetDefaultSize( GetSuggestedMinimumWidth(), widthMeasureSpec ); + layoutHeight = GetDefaultSize( GetSuggestedMinimumHeight(), heightMeasureSpec ); + } + + // Can't exceed specified width + if( widthMode == MeasureSpec::Mode::EXACTLY ) + { + exactWidth = true; + } + else if ( widthMode == MeasureSpec::Mode::AT_MOST ) + { + layoutWidth = std::min( layoutWidth, widthSpecSize ); + } + + // Can't exceed specified height + if( heightMode == MeasureSpec::Mode::EXACTLY ) + { + exactHeight = true; + } + else if ( heightMode == MeasureSpec::Mode::AT_MOST ) + { + layoutHeight = std::min( layoutHeight, heightSpecSize ); + } + + layoutWidth = std::max( layoutWidth, GetSuggestedMinimumWidth() ); + layoutHeight = std::max( layoutHeight, GetSuggestedMinimumHeight() ); + + if( exactWidth ) + { + layoutWidth = widthSpecSize; + } + + if( exactHeight ) + { + layoutHeight = heightSpecSize; + } + + DALI_LOG_STREAM( gLogFilter, Debug::General, "LayoutGroup::OnMeasure Measured size(" << layoutWidth << "," << layoutHeight << ") for : " << Actor::DownCast(GetOwner()).GetName() << " \n" ); + SetMeasuredDimensions( MeasuredSize( layoutWidth ), MeasuredSize( layoutHeight ) ); +} + +void LayoutGroup::OnLayout( bool changed, LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom ) +{ + auto count = GetChildCount(); + + DALI_LOG_STREAM( gLogFilter, Debug::Verbose, "LayoutGroup OnLayout owner:" << ( ( Toolkit::Control::DownCast(GetOwner())) ? Toolkit::Control::DownCast(GetOwner()).GetName() : "invalid" ) << " childCount:" << count ); + + for( unsigned int childIndex = 0; childIndex < count; childIndex++) + { + LayoutItemPtr childLayout = GetChildAt( childIndex ); + if( childLayout != nullptr ) + { + + auto childOwner = childLayout->GetOwner(); + LayoutLength childWidth = childLayout->GetMeasuredWidth(); + LayoutLength childHeight = childLayout->GetMeasuredHeight(); + Extents childMargin = childLayout->GetMargin(); + auto control = Toolkit::Control::DownCast( childOwner ); + Extents padding = GetPadding(); + + auto childPosition = control.GetProperty< Vector3 >( Actor::Property::POSITION ); + auto anchorPoint = control.GetProperty< Vector3 >( Actor::Property::ANCHOR_POINT ); + + DALI_LOG_STREAM( gLogFilter, Debug::General, "LayoutGroup::OnLayout child[" << control.GetName() << + "] position(" << childPosition << ") child width[" << childWidth << "] height[" << childHeight << "]\n" ); + + // Margin and Padding only supported when child anchor point is TOP_LEFT. + int paddingAndMarginOffsetX = ( AnchorPoint::TOP_LEFT == anchorPoint ) ? ( padding.top + childMargin.top ) : 0; + int paddingAndMarginOffsetY = ( AnchorPoint::TOP_LEFT == anchorPoint ) ? ( padding.start + childMargin.start ) : 0; + DALI_LOG_INFO( gLogFilter, Debug::Verbose, "LayoutGroup::OnLayout paddingMargin offset(%d,%d)\n", paddingAndMarginOffsetX, paddingAndMarginOffsetY ); + + LayoutLength childLeft = childPosition.x + paddingAndMarginOffsetX; + LayoutLength childTop = childPosition.y + paddingAndMarginOffsetY; + + childLayout->Layout( childLeft, childTop, childLeft + childWidth, childTop + childHeight ); + } + } +} + + } // namespace Internal } // namespace Toolkit } // namespace Dali