Merge "Remove unnecessarily exported signals and action names" into tizen
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / relayout-controller-impl.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // FILE HEADER
19
20 #include "relayout-controller-impl.h"
21
22 // EXTERNAL INCLUDES
23 #include <dali/public-api/actors/layer.h>
24 #include <dali/public-api/common/stage.h>
25 #include <dali/integration-api/debug.h>
26
27 #if defined(DEBUG_ENABLED)
28 #include <sstream>
29 #endif // defined(DEBUG_ENABLED)
30
31 // INTERNAL INCLUDES
32 #include <dali-toolkit/public-api/controls/text-view/text-view.h>
33
34 namespace Dali
35 {
36
37 namespace Toolkit
38 {
39
40 namespace Internal
41 {
42
43 namespace
44 {
45 #if defined(DEBUG_ENABLED)
46
47 Integration::Log::Filter* gLogFilter( Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_RELAYOUT_CONTROLLER") );
48
49 /**
50  * Prints out all the children of the given actor when debug is enabled.
51  *
52  * @param[in]  actor  The actor whose children to print.
53  * @param[in]  level  The number of " | " to put in front of the children.
54  */
55 void PrintChildren( Actor actor, int level )
56 {
57   std::ostringstream output;
58
59   for ( int t = 0; t < level; ++t )
60   {
61     output << " | ";
62   }
63
64   output << actor.GetTypeName();
65
66   output << " - Pos: " << actor.GetCurrentPosition() << " Size: " << actor.GetCurrentSize() << ",";
67
68   output << " (" << actor.GetObjectPtr() << ")" << std::endl;
69
70   DALI_LOG_INFO( gLogFilter, Debug::Verbose, output.str().c_str() );
71
72   ++level;
73   unsigned int numChildren = actor.GetChildCount();
74   for( unsigned int i=0; i<numChildren; ++i )
75   {
76     PrintChildren( actor.GetChildAt(i), level );
77   }
78   --level;
79 }
80
81 /**
82  * Prints the entire hierarchy of the scene.
83  */
84 void PrintHierarchy()
85 {
86   if ( gLogFilter->IsEnabledFor( Debug::Verbose ) )
87   {
88     PrintChildren( Stage().GetCurrent().GetRootLayer(), 0 );
89   }
90 }
91
92 #define PRINT_HIERARCHY PrintHierarchy()
93
94 #else // defined(DEBUG_ENABLED)
95
96 #define PRINT_HIERARCHY
97
98 #endif // defined(DEBUG_ENABLED)
99
100 /**
101  * Sets the target to source if the individual elements of source are NOT zero.
102  *
103  * @param[out]  target  The Vector2 elements to set if the source Vector2 elements are not 0.
104  * @param[in]   source  The Vector2 elements that are to be set to the target Vector2.
105  */
106 void SetIfNotZero( Vector2& target, const Vector2& source )
107 {
108   // Only set the width if it is non zero.
109   if ( !EqualsZero( source.width ) )
110   {
111     target.width  = source.width;
112   }
113
114   // Only set the height if it is non zero.
115   if ( !EqualsZero( source.height ) )
116   {
117     target.height = source.height;
118   }
119 }
120
121 /**
122  * Finds controls in the hierarchy of actor. It descends the tree if the actor has more Actors.
123  * If it is a control, it no longer descends the tree.
124  *
125  * @param[in]  actor  The actor in which controls should be found.
126  * @param[in]  size   The size that this actor and its children should be.
127  */
128 void FindControls( Actor actor, ControlStack& controls, Vector2 size )
129 {
130   Toolkit::Control control( Toolkit::Control::DownCast( actor ) );
131   if( control )
132   {
133     // If the control size has been set by the application / control, then we should try and honour that.
134     Vector2 controlSetSize( control.GetImplementation().GetSizeSet() );
135
136     // Only set the width and height if they are non zero.
137     SetIfNotZero( size, controlSetSize );
138
139     controls.push_back( ControlSizePair( control, size ) );
140   }
141   else
142   {
143     unsigned int numChildren = actor.GetChildCount();
144     for( unsigned int i=numChildren; i>0; --i )
145     {
146       FindControls( actor.GetChildAt(i-1), controls, size );
147     }
148   }
149 }
150
151 /**
152  * Pushes the controls in the container, to the stack.
153  *
154  * @param[in,out]  controlStack  The stack to push controls to.
155  * @param[in]      container     The container to push controls from.
156  */
157 void PushToStack( ControlStack& controlStack, const ActorSizeContainer& container )
158 {
159   for ( ActorSizeContainer::const_reverse_iterator iter = container.rbegin(), endIter = container.rend(); iter != endIter; ++iter )
160   {
161     FindControls( iter->first, controlStack, iter->second );
162   }
163 }
164
165 } // unnamed namespace
166
167 RelayoutControllerImpl::RelayoutControllerImpl( bool& relayoutFlag )
168 : mRelayoutFlag( relayoutFlag ),
169   mRelayoutConnection( false )
170 {
171   // make space for 32 controls to avoid having to copy construct a lot in the beginning
172   mControlStack.reserve( 32 );
173   mSizecontainer.reserve( 32 );
174 }
175
176 RelayoutControllerImpl::~RelayoutControllerImpl()
177 {
178 }
179
180 void RelayoutControllerImpl::Request()
181 {
182   //TODO use Relayout Request to set up logic to optimize relayout of the actors/controls in the scene
183
184   if( !mRelayoutConnection )
185   {
186     Stage stage = Stage::GetCurrent();
187     stage.EventProcessingFinishedSignal().Connect( this, &RelayoutControllerImpl::Relayout );
188     mRelayoutConnection = true;
189   }
190 }
191
192 void RelayoutControllerImpl::Relayout()
193 {
194   // only do something when requested
195   if( mRelayoutFlag )
196   {
197     // clear the flag as we're now doing the relayout
198     mRelayoutFlag = false;
199     PRINT_HIERARCHY;
200
201     mControlStack.clear(); // we do not release memory, just empty the container
202
203     // 1. Finds all top-level controls from the root actor and allocate them the size of the stage
204     //    These controls are paired with the stage size and added to the stack.
205     FindControls( Stage().GetCurrent().GetRootLayer(), mControlStack, Stage::GetCurrent().GetSize() );
206
207     // 2. Iterate through the stack until it's empty.
208     while ( !mControlStack.empty() )
209     {
210       ControlSizePair pair ( mControlStack.back() );
211       Toolkit::Control control ( pair.first );
212       Vector2 size ( pair.second );
213       mControlStack.pop_back();
214
215       DALI_LOG_INFO( gLogFilter, Debug::General, "Allocating %p (%.2f, %.2f)\n", control.GetObjectPtr(), size.width, size.height );
216
217       mSizecontainer.clear();
218       // 3. Negotiate the size with the current control. Pass it an empty container which the control
219       //    has to fill with all the actors it has not done any size negotiation for.
220       control.GetImplementation().NegotiateSize( size, mSizecontainer );
221
222       // 4. Push the controls from the actors in the container to the stack.
223       PushToStack( mControlStack, mSizecontainer );
224     }
225   }
226   // should not disconnect the signal as that causes some control size negotiations to not work correctly
227   // this algorithm needs more optimization as well
228 }
229
230 } // namespace Internal
231
232 } // namespace Toolkit
233
234 } // namespace Dali