[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 /*
2  * Copyright (c) 2017 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 // CLASS HEADER
19 #include "image-view-impl.h"
20
21 // EXTERNAL INCLUDES
22 #include <dali/public-api/images/resource-image.h>
23 #include <dali/public-api/object/type-registry.h>
24 #include <dali/public-api/object/type-registry-helper.h>
25 #include <dali/devel-api/scripting/scripting.h>
26
27 // INTERNAL INCLUDES
28 #include <dali-toolkit/public-api/controls/image-view/image-view.h>
29 #include <dali-toolkit/devel-api/controls/control-devel.h>
30 #include <dali-toolkit/devel-api/visuals/visual-properties-devel.h>
31 #include <dali-toolkit/devel-api/visual-factory/visual-factory.h>
32 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
33 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
34 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
35
36 namespace Dali
37 {
38
39 namespace Toolkit
40 {
41
42 namespace Internal
43 {
44
45 namespace
46 {
47
48 BaseHandle Create()
49 {
50   return Toolkit::ImageView::New();
51 }
52
53 // Setup properties, signals and actions using the type-registry.
54 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::ImageView, Toolkit::Control, Create );
55 DALI_PROPERTY_REGISTRATION( Toolkit, ImageView, "resourceUrl", STRING, RESOURCE_URL )
56 DALI_PROPERTY_REGISTRATION( Toolkit, ImageView, "image", MAP, IMAGE )
57 DALI_PROPERTY_REGISTRATION( Toolkit, ImageView, "preMultipliedAlpha", BOOLEAN, PRE_MULTIPLIED_ALPHA )
58
59 DALI_ANIMATABLE_PROPERTY_REGISTRATION_WITH_DEFAULT( Toolkit, ImageView, "pixelArea", Vector4(0.f, 0.f, 1.f, 1.f), PIXEL_AREA )
60 DALI_TYPE_REGISTRATION_END()
61
62 } // anonymous namespace
63
64 using namespace Dali;
65
66 ImageView::ImageView()
67 : Control( ControlBehaviour( CONTROL_BEHAVIOUR_DEFAULT ) )
68 {
69 }
70
71 ImageView::~ImageView()
72 {
73 }
74
75 Toolkit::ImageView ImageView::New()
76 {
77   ImageView* impl = new ImageView();
78
79   Toolkit::ImageView handle = Toolkit::ImageView( *impl );
80
81   // Second-phase init of the implementation
82   // This can only be done after the CustomActor connection has been made...
83   impl->Initialize();
84
85   return handle;
86 }
87
88 /////////////////////////////////////////////////////////////
89
90 void ImageView::SetImage( Image image )
91 {
92   // Don't bother comparing if we had a visual previously, just drop old visual and create new one
93   mImage = image;
94   mUrl.clear();
95   mPropertyMap.Clear();
96
97   mVisual =  Toolkit::VisualFactory::Get().CreateVisual( image );
98   DevelControl::RegisterVisual( *this, Toolkit::ImageView::Property::IMAGE, mVisual  );
99
100   RelayoutRequest();
101 }
102
103 void ImageView::SetImage( const Property::Map& map )
104 {
105   // Comparing a property map is too expensive so just creating a new visual
106   mPropertyMap = map;
107   mUrl.clear();
108   mImage.Reset();
109
110   mVisual =  Toolkit::VisualFactory::Get().CreateVisual( mPropertyMap );
111   DevelControl::RegisterVisual( *this, Toolkit::ImageView::Property::IMAGE, mVisual  );
112
113   RelayoutRequest();
114 }
115
116 void ImageView::SetImage( const std::string& url, ImageDimensions size )
117 {
118   // Don't bother comparing if we had a visual previously, just drop old visual and create new one
119   mUrl = url;
120   mImage.Reset();
121   mPropertyMap.Clear();
122
123   mVisual =  Toolkit::VisualFactory::Get().CreateVisual( url, size );
124   DevelControl::RegisterVisual( *this, Toolkit::ImageView::Property::IMAGE, mVisual );
125
126   RelayoutRequest();
127 }
128
129 Image ImageView::GetImage() const
130 {
131   return mImage;
132 }
133
134 void ImageView::EnablePreMultipliedAlpha( bool preMultipled )
135 {
136   if( mVisual )
137   {
138     Toolkit::GetImplementation( mVisual ).EnablePreMultipliedAlpha( preMultipled );
139   }
140 }
141
142 bool ImageView::IsPreMultipliedAlphaEnabled() const
143 {
144   if( mVisual )
145   {
146     return Toolkit::GetImplementation( mVisual ).IsPreMultipliedAlphaEnabled();
147   }
148   return false;
149 }
150
151 void ImageView::SetDepthIndex( int depthIndex )
152 {
153   if( mVisual )
154   {
155     mVisual.SetDepthIndex( depthIndex );
156   }
157 }
158
159 void ImageView::OnStageConnection( int depth )
160 {
161   if( mImage )
162   {
163     mImage.UploadedSignal().Emit( mImage );
164   }
165
166   Dali::ResourceImage resourceImage = Dali::ResourceImage::DownCast( mImage );
167   if( resourceImage )
168   {
169     resourceImage.LoadingFinishedSignal().Emit( resourceImage );
170   }
171
172   Control::OnStageConnection( depth ); // Enabled visuals will be put on stage
173 }
174
175 Vector3 ImageView::GetNaturalSize()
176 {
177   if( mVisual )
178   {
179     Vector2 rendererNaturalSize;
180     mVisual.GetNaturalSize( rendererNaturalSize );
181     return Vector3( rendererNaturalSize );
182   }
183
184   // if no visual then use Control's natural size
185   return Control::GetNaturalSize();
186 }
187
188 float ImageView::GetHeightForWidth( float width )
189 {
190   if( mVisual )
191   {
192     return mVisual.GetHeightForWidth( width );
193   }
194   else
195   {
196     return Control::GetHeightForWidth( width );
197   }
198 }
199
200 float ImageView::GetWidthForHeight( float height )
201 {
202   if( mVisual )
203   {
204     return mVisual.GetWidthForHeight( height );
205   }
206   else
207   {
208     return Control::GetWidthForHeight( height );
209   }
210 }
211
212 void ImageView::OnRelayout( const Vector2& size, RelayoutContainer& container )
213 {
214   Control::OnRelayout( size, container );
215
216   if( mVisual )
217   {
218     // Pass in an empty map which uses default transform values meaning our visual fills the control
219     // Should provide a transform that handles aspect ratio according to image size
220     mVisual.SetTransformAndSize( Property::Map(), size );
221   }
222 }
223
224 ///////////////////////////////////////////////////////////
225 //
226 // Properties
227 //
228
229 void ImageView::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
230 {
231   Toolkit::ImageView imageView = Toolkit::ImageView::DownCast( Dali::BaseHandle( object ) );
232
233   if ( imageView )
234   {
235     ImageView& impl = GetImpl( imageView );
236     switch ( index )
237     {
238       case Toolkit::ImageView::Property::RESOURCE_URL:
239       {
240         std::string imageUrl;
241         if( value.Get( imageUrl ) )
242         {
243           impl.SetImage( imageUrl, ImageDimensions() );
244         }
245         break;
246       }
247
248       case Toolkit::ImageView::Property::IMAGE:
249       {
250         std::string imageUrl;
251         Property::Map* map;
252         if( value.Get( imageUrl ) )
253         {
254           impl.SetImage( imageUrl, ImageDimensions() );
255         }
256         // if its not a string then get a Property::Map from the property if possible.
257         else
258         {
259           map = value.GetMap();
260           if( map )
261           {
262             Property::Value* shaderValue = map->Find( Toolkit::DevelVisual::Property::SHADER, CUSTOM_SHADER );
263             // set image only if property map contains image information other than custom shader
264             if( map->Count() > 1u ||  !shaderValue )
265             {
266               impl.SetImage( *map );
267             }
268             // the property map contains only the custom shader
269             else if( ( impl.mVisual )&&( map->Count() == 1u )&&( shaderValue ) )
270             {
271               Property::Map* shaderMap = shaderValue->GetMap();
272               if( shaderMap )
273               {
274                 Internal::Visual::Base& visual = Toolkit::GetImplementation( impl.mVisual );
275                 visual.SetCustomShader( *shaderMap );
276                 if( imageView.OnStage() )
277                 {
278                   // force to create new core renderer to use the newly set shader
279                   visual.SetOffStage( imageView );
280                   visual.SetOnStage( imageView );
281                 }
282               }
283             }
284           }
285         }
286         break;
287       }
288
289       case Toolkit::ImageView::Property::PRE_MULTIPLIED_ALPHA:
290       {
291         bool isPre;
292         if( value.Get( isPre ) )
293         {
294           impl.EnablePreMultipliedAlpha( isPre );
295         }
296         break;
297       }
298     }
299   }
300 }
301
302 Property::Value ImageView::GetProperty( BaseObject* object, Property::Index propertyIndex )
303 {
304   Property::Value value;
305
306   Toolkit::ImageView imageview = Toolkit::ImageView::DownCast( Dali::BaseHandle( object ) );
307
308   if ( imageview )
309   {
310     ImageView& impl = GetImpl( imageview );
311     switch ( propertyIndex )
312     {
313       case Toolkit::ImageView::Property::RESOURCE_URL:
314       {
315         if ( !impl.mUrl.empty() )
316         {
317           value = impl.mUrl;
318         }
319         break;
320       }
321
322       case Toolkit::ImageView::Property::IMAGE:
323       {
324         if ( !impl.mUrl.empty() )
325         {
326           value = impl.mUrl;
327         }
328         else if( impl.mImage )
329         {
330           Property::Map map;
331           Scripting::CreatePropertyMap( impl.mImage, map );
332           value = map;
333         }
334         else if( !impl.mPropertyMap.Empty() )
335         {
336           value = impl.mPropertyMap;
337         }
338         break;
339       }
340
341       case Toolkit::ImageView::Property::PRE_MULTIPLIED_ALPHA:
342       {
343         value = impl.IsPreMultipliedAlphaEnabled();
344         break;
345       }
346     }
347   }
348
349   return value;
350 }
351
352 } // namespace Internal
353 } // namespace Toolkit
354 } // namespace Dali