[Tizen] Restore behavior of Uploaded and LoadingFinished signal
[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/devel-api/visuals/visual-properties-devel.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/internal/visuals/visual-base-data-impl.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   // Don't bother comparing if we had a visual previously, just drop old visual and create new one
77   mImage = image;
78   mUrl.clear();
79   mPropertyMap.Clear();
80
81   mVisual =  Toolkit::VisualFactory::Get().CreateVisual( image );
82   RegisterVisual( Toolkit::ImageView::Property::IMAGE, mVisual  );
83
84   RelayoutRequest();
85 }
86
87 void ImageView::SetImage( const Property::Map& map )
88 {
89   // Comparing a property map is too expensive so just creating a new visual
90   mPropertyMap = map;
91   mUrl.clear();
92   mImage.Reset();
93
94   mVisual =  Toolkit::VisualFactory::Get().CreateVisual( mPropertyMap );
95   RegisterVisual( Toolkit::ImageView::Property::IMAGE, mVisual  );
96
97   RelayoutRequest();
98 }
99
100 void ImageView::SetImage( const std::string& url, ImageDimensions size )
101 {
102   // Don't bother comparing if we had a visual previously, just drop old visual and create new one
103   mUrl = url;
104   mImage.Reset();
105   mPropertyMap.Clear();
106
107   mVisual =  Toolkit::VisualFactory::Get().CreateVisual( url, size );
108   RegisterVisual( Toolkit::ImageView::Property::IMAGE, mVisual );
109
110   RelayoutRequest();
111 }
112
113 Image ImageView::GetImage() const
114 {
115   return mImage;
116 }
117
118 void ImageView::EnablePreMultipliedAlpha( bool preMultipled )
119 {
120   if( mVisual )
121   {
122     Toolkit::GetImplementation( mVisual ).EnablePreMultipliedAlpha( preMultipled );
123   }
124 }
125
126 bool ImageView::IsPreMultipliedAlphaEnabled() const
127 {
128   if( mVisual )
129   {
130     return Toolkit::GetImplementation( mVisual ).IsPreMultipliedAlphaEnabled();
131   }
132   return false;
133 }
134
135 void ImageView::SetDepthIndex( int depthIndex )
136 {
137   if( mVisual )
138   {
139     mVisual.SetDepthIndex( depthIndex );
140   }
141 }
142
143 void ImageView::OnStageConnection( int depth )
144 {
145   if( mImage )
146   {
147     mImage.UploadedSignal().Emit( mImage );
148   }
149
150   Dali::ResourceImage resourceImage = Dali::ResourceImage::DownCast( mImage );
151   if( resourceImage )
152   {
153     resourceImage.LoadingFinishedSignal().Emit( resourceImage );
154   }
155
156   Control::OnStageConnection( depth ); // Enabled visuals will be put on stage
157 }
158
159 Vector3 ImageView::GetNaturalSize()
160 {
161   if( mVisual )
162   {
163     Vector2 rendererNaturalSize;
164     mVisual.GetNaturalSize( rendererNaturalSize );
165     return Vector3( rendererNaturalSize );
166   }
167
168   // if no visual then use Control's natural size
169   return Control::GetNaturalSize();
170 }
171
172 float ImageView::GetHeightForWidth( float width )
173 {
174   if( mVisual )
175   {
176     return mVisual.GetHeightForWidth( width );
177   }
178   else
179   {
180     return Control::GetHeightForWidth( width );
181   }
182 }
183
184 float ImageView::GetWidthForHeight( float height )
185 {
186   if( mVisual )
187   {
188     return mVisual.GetWidthForHeight( height );
189   }
190   else
191   {
192     return Control::GetWidthForHeight( height );
193   }
194 }
195
196 void ImageView::OnRelayout( const Vector2& size, RelayoutContainer& container )
197 {
198   Control::OnRelayout( size, container );
199
200   if( mVisual )
201   {
202     // Pass in an empty map which uses default transform values meaning our visual fills the control
203     // Should provide a transform that handles aspect ratio according to image size
204     mVisual.SetTransformAndSize( Property::Map(), size );
205   }
206 }
207
208 ///////////////////////////////////////////////////////////
209 //
210 // Properties
211 //
212
213 void ImageView::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
214 {
215   Toolkit::ImageView imageView = Toolkit::ImageView::DownCast( Dali::BaseHandle( object ) );
216
217   if ( imageView )
218   {
219     ImageView& impl = GetImpl( imageView );
220     switch ( index )
221     {
222       case Toolkit::ImageView::Property::RESOURCE_URL:
223       {
224         std::string imageUrl;
225         if( value.Get( imageUrl ) )
226         {
227           impl.SetImage( imageUrl, ImageDimensions() );
228         }
229         break;
230       }
231
232       case Toolkit::ImageView::Property::IMAGE:
233       {
234         std::string imageUrl;
235         Property::Map* map;
236         if( value.Get( imageUrl ) )
237         {
238           impl.SetImage( imageUrl, ImageDimensions() );
239         }
240         // if its not a string then get a Property::Map from the property if possible.
241         else
242         {
243           map = value.GetMap();
244           if( map )
245           {
246             Property::Value* shaderValue = map->Find( Toolkit::DevelVisual::Property::SHADER, CUSTOM_SHADER );
247             // set image only if property map contains image information other than custom shader
248             if( map->Count() > 1u ||  !shaderValue )
249             {
250               impl.SetImage( *map );
251             }
252             // the property map contains only the custom shader
253             else if( ( impl.mVisual )&&( map->Count() == 1u )&&( shaderValue ) )
254             {
255               Property::Map* shaderMap = shaderValue->GetMap();
256               if( shaderMap )
257               {
258                 Internal::Visual::Base& visual = Toolkit::GetImplementation( impl.mVisual );
259                 visual.SetCustomShader( *shaderMap );
260                 if( imageView.OnStage() )
261                 {
262                   // force to create new core renderer to use the newly set shader
263                   visual.SetOffStage( imageView );
264                   visual.SetOnStage( imageView );
265                 }
266               }
267             }
268           }
269         }
270         break;
271       }
272
273       case Toolkit::ImageView::Property::PRE_MULTIPLIED_ALPHA:
274       {
275         bool isPre;
276         if( value.Get( isPre ) )
277         {
278           impl.EnablePreMultipliedAlpha( isPre );
279         }
280         break;
281       }
282     }
283   }
284 }
285
286 Property::Value ImageView::GetProperty( BaseObject* object, Property::Index propertyIndex )
287 {
288   Property::Value value;
289
290   Toolkit::ImageView imageview = Toolkit::ImageView::DownCast( Dali::BaseHandle( object ) );
291
292   if ( imageview )
293   {
294     ImageView& impl = GetImpl( imageview );
295     switch ( propertyIndex )
296     {
297       case Toolkit::ImageView::Property::RESOURCE_URL:
298       {
299         if ( !impl.mUrl.empty() )
300         {
301           value = impl.mUrl;
302         }
303         break;
304       }
305
306       case Toolkit::ImageView::Property::IMAGE:
307       {
308         if ( !impl.mUrl.empty() )
309         {
310           value = impl.mUrl;
311         }
312         else if( impl.mImage )
313         {
314           Property::Map map;
315           Scripting::CreatePropertyMap( impl.mImage, map );
316           value = map;
317         }
318         else if( !impl.mPropertyMap.Empty() )
319         {
320           value = impl.mPropertyMap;
321         }
322         break;
323       }
324
325       case Toolkit::ImageView::Property::PRE_MULTIPLIED_ALPHA:
326       {
327         value = impl.IsPreMultipliedAlphaEnabled();
328         break;
329       }
330     }
331   }
332
333   return value;
334 }
335
336 } // namespace Internal
337 } // namespace Toolkit
338 } // namespace Dali