[3.0] Resource ready signal for Controls (for ImageLoading)
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / image-view / image-view-impl.cpp
1 // Copyright (c) 2016 Samsung Electronics Co., Ltd.
2
3 // CLASS HEADER
4 #include "image-view-impl.h"
5
6 // EXTERNAL INCLUDES
7 #include <dali/public-api/images/resource-image.h>
8 #include <dali/public-api/object/type-registry.h>
9 #include <dali/public-api/object/type-registry-helper.h>
10 #include <dali/devel-api/scripting/scripting.h>
11
12 // INTERNAL INCLUDES
13 #include <dali-toolkit/public-api/controls/image-view/image-view.h>
14 #include <dali-toolkit/public-api/visuals/visual-properties.h>
15 #include <dali-toolkit/devel-api/visual-factory/visual-factory.h>
16 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
17 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
18 #include <dali-toolkit/devel-api/controls/control-devel.h>
19
20 namespace Dali
21 {
22
23 namespace Toolkit
24 {
25
26 namespace Internal
27 {
28
29 namespace
30 {
31
32 BaseHandle Create()
33 {
34   return Toolkit::ImageView::New();
35 }
36
37 // Setup properties, signals and actions using the type-registry.
38 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::ImageView, Toolkit::Control, Create );
39 DALI_PROPERTY_REGISTRATION( Toolkit, ImageView, "resourceUrl", STRING, RESOURCE_URL )
40 DALI_PROPERTY_REGISTRATION( Toolkit, ImageView, "image", MAP, IMAGE )
41 DALI_PROPERTY_REGISTRATION( Toolkit, ImageView, "preMultipliedAlpha", BOOLEAN, PRE_MULTIPLIED_ALPHA )
42
43 DALI_ANIMATABLE_PROPERTY_REGISTRATION_WITH_DEFAULT( Toolkit, ImageView, "pixelArea", Vector4(0.f, 0.f, 1.f, 1.f), PIXEL_AREA )
44 DALI_TYPE_REGISTRATION_END()
45
46 } // anonymous namespace
47
48 using namespace Dali;
49
50 ImageView::ImageView()
51 : Control( ControlBehaviour( CONTROL_BEHAVIOUR_DEFAULT ) )
52 {
53 }
54
55 ImageView::~ImageView()
56 {
57 }
58
59 Toolkit::ImageView ImageView::New()
60 {
61   ImageView* impl = new ImageView();
62
63   Toolkit::ImageView handle = Toolkit::ImageView( *impl );
64
65   // Second-phase init of the implementation
66   // This can only be done after the CustomActor connection has been made...
67   impl->Initialize();
68
69   return handle;
70 }
71
72 /////////////////////////////////////////////////////////////
73
74 void ImageView::SetImage( Image image )
75 {
76   if( ( mImage != image ) ||
77       ! mUrl.empty()      ||   // If we're changing from a URL type to an Image type
78       ! mPropertyMap.Empty() ) // If we're changing from a property map type to an Image type
79   {
80     mUrl.clear();
81     mPropertyMap.Clear();
82
83     mImage = image;
84
85     Actor self( Self() );
86     InitializeVisual( self, mVisual, image );
87     DevelControl::RegisterVisual( *this, Toolkit::ImageView::Property::IMAGE, mVisual  );
88     mImageSize = image ? ImageDimensions( image.GetWidth(), image.GetHeight() ) : ImageDimensions( 0, 0 );
89
90     RelayoutRequest();
91   }
92 }
93
94 void ImageView::SetImage( Property::Map map )
95 {
96   mUrl.clear();
97   mImage.Reset();
98   mPropertyMap = map;
99
100   Actor self( Self() );
101   InitializeVisual( self, mVisual, mPropertyMap );
102   DevelControl::RegisterVisual( *this, Toolkit::ImageView::Property::IMAGE, mVisual  );
103
104   Property::Value* widthValue = mPropertyMap.Find( "width" );
105   if( widthValue )
106   {
107     int width;
108     if( widthValue->Get( width ) )
109     {
110       mImageSize = ImageDimensions( width, mImageSize.GetHeight() );
111     }
112   }
113
114   Property::Value* heightValue = mPropertyMap.Find( "height" );
115   if( heightValue )
116   {
117     int height;
118     if( heightValue->Get( height ) )
119     {
120       mImageSize = ImageDimensions( mImageSize.GetWidth(), height );
121     }
122   }
123
124   RelayoutRequest();
125 }
126
127 void ImageView::SetImage( const std::string& url, ImageDimensions size )
128 {
129   if( ( mUrl != url ) ||
130         mImage        ||       // If we're changing from an Image type to a URL type
131       ! mPropertyMap.Empty() ) // If we're changing from a property map type to a URL type
132   {
133     mImage.Reset();
134     mPropertyMap.Clear();
135
136     mUrl = url;
137
138     if( size.GetWidth() != 0u && size.GetHeight() != 0u )
139     {
140       mImageSize = size;
141     }
142
143     Actor self( Self() );
144     InitializeVisual( self, mVisual, url, size );
145     DevelControl::RegisterVisual( *this, Toolkit::ImageView::Property::IMAGE, mVisual  );
146
147     mVisual.SetSize( mSizeSet );
148
149     RelayoutRequest();
150   }
151 }
152
153 Image ImageView::GetImage() const
154 {
155   return mImage;
156 }
157
158 void ImageView::EnablePreMultipliedAlpha( bool preMultipled )
159 {
160   if( mVisual )
161   {
162     Toolkit::GetImplementation( mVisual ).EnablePreMultipliedAlpha( preMultipled );
163   }
164 }
165
166 bool ImageView::IsPreMultipliedAlphaEnabled() const
167 {
168   if( mVisual )
169   {
170     return Toolkit::GetImplementation( mVisual ).IsPreMultipliedAlphaEnabled();
171   }
172   return false;
173 }
174
175 void ImageView::SetDepthIndex( int depthIndex )
176 {
177   if( mVisual )
178   {
179     mVisual.SetDepthIndex( depthIndex );
180   }
181 }
182
183 Vector3 ImageView::GetNaturalSize()
184 {
185   if( mVisual )
186   {
187     Vector2 rendererNaturalSize;
188     mVisual.GetNaturalSize( rendererNaturalSize );
189     return Vector3( rendererNaturalSize );
190   }
191
192   Vector3 size;
193   size.x = mImageSize.GetWidth();
194   size.y = mImageSize.GetHeight();
195
196   if( size.x > 0 && size.y > 0 )
197   {
198     size.z = std::min(size.x, size.y);
199     return size;
200   }
201   else
202   {
203     // if no image then use Control's natural size
204     return Control::GetNaturalSize();
205   }
206 }
207
208 float ImageView::GetHeightForWidth( float width )
209 {
210   if( mImageSize.GetWidth() > 0 && mImageSize.GetHeight() > 0 )
211   {
212     return GetHeightForWidthBase( width );
213   }
214   else
215   {
216     return Control::GetHeightForWidth( width );
217   }
218 }
219
220 float ImageView::GetWidthForHeight( float height )
221 {
222   if( mImageSize.GetWidth() > 0 && mImageSize.GetHeight() > 0 )
223   {
224     return GetWidthForHeightBase( height );
225   }
226   else
227   {
228     return Control::GetWidthForHeight( height );
229   }
230 }
231
232
233 ///////////////////////////////////////////////////////////
234 //
235 // Private methods
236 //
237
238 void ImageView::OnStageConnection( int depth )
239 {
240   Control::OnStageConnection( depth );
241
242   if( mVisual )
243   {
244     CustomActor self = Self();
245     mVisual.SetOnStage( self );
246   }
247 }
248
249 void ImageView::OnStageDisconnection()
250 {
251   if( mVisual )
252   {
253     CustomActor self = Self();
254     mVisual.SetOffStage( self );
255   }
256
257   Control::OnStageDisconnection();
258 }
259
260 void ImageView::OnSizeSet( const Vector3& targetSize )
261 {
262   Control::OnSizeSet( targetSize );
263   mSizeSet = targetSize;
264
265   if( mVisual )
266   {
267     Vector2 size( targetSize );
268     mVisual.SetSize( size );
269   }
270 }
271
272 ///////////////////////////////////////////////////////////
273 //
274 // Properties
275 //
276
277 void ImageView::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
278 {
279   Toolkit::ImageView imageView = Toolkit::ImageView::DownCast( Dali::BaseHandle( object ) );
280
281   if ( imageView )
282   {
283     ImageView& impl = GetImpl( imageView );
284     switch ( index )
285     {
286       case Toolkit::ImageView::Property::RESOURCE_URL:
287       {
288         std::string imageUrl;
289         if( value.Get( imageUrl ) )
290         {
291           impl.SetImage( imageUrl, ImageDimensions() );
292         }
293         break;
294       }
295
296       case Toolkit::ImageView::Property::IMAGE:
297       {
298         std::string imageUrl;
299         Property::Map map;
300         if( value.Get( imageUrl ) )
301         {
302           impl.SetImage( imageUrl, ImageDimensions() );
303         }
304         // if its not a string then get a Property::Map from the property if possible.
305         else if( value.Get( map ) )
306         {
307           Property::Value* shaderValue = map.Find( Toolkit::Visual::Property::SHADER, CUSTOM_SHADER );
308           // set image only if property map contains image information other than custom shader
309           if( map.Count() > 1u ||  !shaderValue )
310           {
311             impl.SetImage( map );
312           }
313           // the property map contains only the custom shader
314           else if(  impl.mVisual && map.Count() == 1u &&  shaderValue )
315           {
316             Property::Map shaderMap;
317             if( shaderValue->Get( shaderMap ) )
318             {
319               Internal::Visual::Base& visual = Toolkit::GetImplementation( impl.mVisual );
320               visual.SetCustomShader( shaderMap );
321               if( imageView.OnStage() )
322               {
323                 // force to create new core renderer to use the newly set shader
324                 visual.SetOffStage( imageView );
325                 visual.SetOnStage( imageView );
326               }
327             }
328           }
329         }
330         break;
331       }
332
333       case Toolkit::ImageView::Property::PRE_MULTIPLIED_ALPHA:
334       {
335         bool isPre;
336         if( value.Get( isPre ) )
337         {
338           impl.EnablePreMultipliedAlpha( isPre );
339         }
340         break;
341       }
342     }
343   }
344 }
345
346 Property::Value ImageView::GetProperty( BaseObject* object, Property::Index propertyIndex )
347 {
348   Property::Value value;
349
350   Toolkit::ImageView imageview = Toolkit::ImageView::DownCast( Dali::BaseHandle( object ) );
351
352   if ( imageview )
353   {
354     ImageView& impl = GetImpl( imageview );
355     switch ( propertyIndex )
356     {
357       case Toolkit::ImageView::Property::RESOURCE_URL:
358       {
359         if ( !impl.mUrl.empty() )
360         {
361           value = impl.mUrl;
362         }
363         break;
364       }
365
366       case Toolkit::ImageView::Property::IMAGE:
367       {
368         if ( !impl.mUrl.empty() )
369         {
370           value = impl.mUrl;
371         }
372         else if( impl.mImage )
373         {
374           Property::Map map;
375           Scripting::CreatePropertyMap( impl.mImage, map );
376           value = map;
377         }
378         else if( !impl.mPropertyMap.Empty() )
379         {
380           value = impl.mPropertyMap;
381         }
382         break;
383       }
384
385       case Toolkit::ImageView::Property::PRE_MULTIPLIED_ALPHA:
386       {
387         value = impl.IsPreMultipliedAlphaEnabled();
388         break;
389       }
390     }
391   }
392
393   return value;
394 }
395
396 } // namespace Internal
397 } // namespace Toolkit
398 } // namespace Dali