[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) 2018 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/public-api/visuals/visual-properties.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   mImageSize()
69 {
70 }
71
72 ImageView::~ImageView()
73 {
74 }
75
76 Toolkit::ImageView ImageView::New()
77 {
78   ImageView* impl = new ImageView();
79
80   Toolkit::ImageView handle = Toolkit::ImageView( *impl );
81
82   // Second-phase init of the implementation
83   // This can only be done after the CustomActor connection has been made...
84   impl->Initialize();
85
86   return handle;
87 }
88
89 /////////////////////////////////////////////////////////////
90
91 void ImageView::OnInitialize()
92 {
93   // ImageView can relayout in the OnImageReady, alternative to a signal would be to have a upcall from the Control to ImageView
94   Dali::Toolkit::Control handle( GetOwner() );
95   handle.ResourceReadySignal().Connect( this, &ImageView::OnResourceReady );
96 }
97
98 void ImageView::SetImage( Image image )
99 {
100   // Don't bother comparing if we had a visual previously, just drop old visual and create new one
101   mImage = image;
102   mUrl.clear();
103   mPropertyMap.Clear();
104
105   Toolkit::Visual::Base visual =  Toolkit::VisualFactory::Get().CreateVisual( image );
106   if( visual )
107   {
108     if( !mVisual )
109     {
110       mVisual = visual;
111     }
112
113     if( !mShaderMap.Empty() )
114     {
115       Internal::Visual::Base& visualImpl = Toolkit::GetImplementation( visual );
116       visualImpl.SetCustomShader( mShaderMap );
117     }
118
119     DevelControl::RegisterVisual( *this, Toolkit::ImageView::Property::IMAGE, visual );
120   }
121   else
122   {
123     // Unregister the exsiting visual
124     DevelControl::UnregisterVisual( *this, Toolkit::ImageView::Property::IMAGE );
125
126     // Trigger a size negotiation request that may be needed when unregistering a visual.
127     RelayoutRequest();
128   }
129
130   Toolkit::DevelControl::RequestLayout( *this );
131 }
132
133 void ImageView::SetImage( const Property::Map& map )
134 {
135   // Comparing a property map is too expensive so just creating a new visual
136   mPropertyMap = map;
137   mUrl.clear();
138   mImage.Reset();
139
140   Toolkit::Visual::Base visual =  Toolkit::VisualFactory::Get().CreateVisual( mPropertyMap );
141   if( visual )
142   {
143     // Don't set mVisual until it is ready and shown. Getters will still use current visual.
144     if( !mVisual )
145     {
146       mVisual = visual;
147     }
148
149     if( !mShaderMap.Empty() )
150     {
151       Internal::Visual::Base& visualImpl = Toolkit::GetImplementation( visual );
152       visualImpl.SetCustomShader( mShaderMap );
153     }
154
155     DevelControl::RegisterVisual( *this, Toolkit::ImageView::Property::IMAGE, visual  );
156   }
157   else
158   {
159     // Unregister the exsiting visual
160     DevelControl::UnregisterVisual( *this, Toolkit::ImageView::Property::IMAGE );
161
162     // Trigger a size negotiation request that may be needed when unregistering a visual.
163     RelayoutRequest();
164   }
165
166   Toolkit::DevelControl::RequestLayout( *this );
167 }
168
169 void ImageView::SetImage( const std::string& url, ImageDimensions size )
170 {
171   // Don't bother comparing if we had a visual previously, just drop old visual and create new one
172   mUrl = url;
173   mImageSize = size;
174   mImage.Reset();
175   mPropertyMap.Clear();
176
177   // Don't set mVisual until it is ready and shown. Getters will still use current visual.
178   Toolkit::Visual::Base visual =  Toolkit::VisualFactory::Get().CreateVisual( url, size );
179   if( visual )
180   {
181     if( !mVisual )
182     {
183       mVisual = visual;
184     }
185
186     if( !mShaderMap.Empty() )
187     {
188       Internal::Visual::Base& visualImpl = Toolkit::GetImplementation( visual );
189       visualImpl.SetCustomShader( mShaderMap );
190     }
191
192     DevelControl::RegisterVisual( *this, Toolkit::ImageView::Property::IMAGE, visual );
193   }
194   else
195   {
196     // Unregister the exsiting visual
197     DevelControl::UnregisterVisual( *this, Toolkit::ImageView::Property::IMAGE );
198
199     // Trigger a size negotiation request that may be needed when unregistering a visual.
200     RelayoutRequest();
201   }
202
203   Toolkit::DevelControl::RequestLayout( *this );
204 }
205
206 Image ImageView::GetImage() const
207 {
208   return mImage;
209 }
210
211 void ImageView::EnablePreMultipliedAlpha( bool preMultipled )
212 {
213   if( mVisual )
214   {
215     Toolkit::GetImplementation( mVisual ).EnablePreMultipliedAlpha( preMultipled );
216   }
217 }
218
219 bool ImageView::IsPreMultipliedAlphaEnabled() const
220 {
221   if( mVisual )
222   {
223     return Toolkit::GetImplementation( mVisual ).IsPreMultipliedAlphaEnabled();
224   }
225   return false;
226 }
227
228 void ImageView::SetDepthIndex( int depthIndex )
229 {
230   if( mVisual )
231   {
232     mVisual.SetDepthIndex( depthIndex );
233   }
234 }
235
236 void ImageView::OnStageConnection( int depth )
237 {
238   if( mImage )
239   {
240     mImage.UploadedSignal().Emit( mImage );
241   }
242
243   Dali::ResourceImage resourceImage = Dali::ResourceImage::DownCast( mImage );
244   if( resourceImage )
245   {
246     resourceImage.LoadingFinishedSignal().Emit( resourceImage );
247   }
248
249   Control::OnStageConnection( depth ); // Enabled visuals will be put on stage
250 }
251
252 Vector3 ImageView::GetNaturalSize()
253 {
254   if( mVisual )
255   {
256     Vector2 rendererNaturalSize;
257     mVisual.GetNaturalSize( rendererNaturalSize );
258
259     Extents padding;
260     padding = Self().GetProperty<Extents>( Toolkit::Control::Property::PADDING );
261
262     rendererNaturalSize.width += ( padding.start + padding.end );
263     rendererNaturalSize.height += ( padding.top + padding.bottom );
264     return Vector3( rendererNaturalSize );
265   }
266
267   // if no visual then use Control's natural size
268   return Control::GetNaturalSize();
269 }
270
271 float ImageView::GetHeightForWidth( float width )
272 {
273   Extents padding;
274   padding = Self().GetProperty<Extents>( Toolkit::Control::Property::PADDING );
275
276   if( mVisual )
277   {
278     return mVisual.GetHeightForWidth( width ) + padding.top + padding.bottom;
279   }
280   else
281   {
282     return Control::GetHeightForWidth( width ) + padding.top + padding.bottom;
283   }
284 }
285
286 float ImageView::GetWidthForHeight( float height )
287 {
288   Extents padding;
289   padding = Self().GetProperty<Extents>( Toolkit::Control::Property::PADDING );
290
291   if( mVisual )
292   {
293     return mVisual.GetWidthForHeight( height ) + padding.start + padding.end;
294   }
295   else
296   {
297     return Control::GetWidthForHeight( height ) + padding.start + padding.end;
298   }
299 }
300
301 void ImageView::OnRelayout( const Vector2& size, RelayoutContainer& container )
302 {
303   Control::OnRelayout( size, container );
304
305   if( mVisual )
306   {
307     Property::Map transformMap = Property::Map();
308
309     Extents padding = Self().GetProperty<Extents>( Toolkit::Control::Property::PADDING );
310     const Visual::FittingMode fittingMode = Toolkit::GetImplementation(mVisual).GetFittingMode();
311
312     if( ( padding != Extents() ) || // If padding is not zero
313         ( fittingMode == Visual::FittingMode::FIT_KEEP_ASPECT_RATIO ) )
314     {
315       Dali::LayoutDirection::Type layoutDirection = static_cast<Dali::LayoutDirection::Type>(
316             Self().GetProperty( Dali::Actor::Property::LAYOUT_DIRECTION ).Get<int>() );
317
318       if( Dali::LayoutDirection::RIGHT_TO_LEFT == layoutDirection )
319       {
320         std::swap( padding.start, padding.end );
321       }
322
323       auto finalOffset = Vector2( padding.start, padding.top );
324
325       // remove padding from the size to know how much is left for the visual
326       auto finalSize = size - Vector2( padding.start + padding.end, padding.top + padding.bottom );
327
328       // Should provide a transform that handles aspect ratio according to image size
329       if( fittingMode == Visual::FittingMode::FIT_KEEP_ASPECT_RATIO )
330       {
331         auto availableVisualSize = finalSize;
332
333         Vector2 naturalSize;
334         mVisual.GetNaturalSize( naturalSize );
335
336         // scale to fit the padded area
337         finalSize = naturalSize * std::min( ( naturalSize.width  ? ( availableVisualSize.width  / naturalSize.width  ) : 0 ),
338                                             ( naturalSize.height ? ( availableVisualSize.height / naturalSize.height ) : 0 ) );
339
340         // calculate final offset within the padded area
341         finalOffset += ( availableVisualSize - finalSize ) * .5f;
342       }
343
344       // populate the transform map
345       transformMap.Add( Toolkit::Visual::Transform::Property::OFFSET, finalOffset )
346                   .Add( Toolkit::Visual::Transform::Property::OFFSET_POLICY,
347                       Vector2( Toolkit::Visual::Transform::Policy::ABSOLUTE, Toolkit::Visual::Transform::Policy::ABSOLUTE ) )
348                   .Add( Toolkit::Visual::Transform::Property::ORIGIN, Toolkit::Align::TOP_BEGIN )
349                   .Add( Toolkit::Visual::Transform::Property::ANCHOR_POINT, Toolkit::Align::TOP_BEGIN )
350                   .Add( Toolkit::Visual::Transform::Property::SIZE, finalSize )
351                   .Add( Toolkit::Visual::Transform::Property::SIZE_POLICY,
352                       Vector2( Toolkit::Visual::Transform::Policy::ABSOLUTE, Toolkit::Visual::Transform::Policy::ABSOLUTE ) );
353     }
354
355     mVisual.SetTransformAndSize( transformMap, size );
356   }
357 }
358
359 void ImageView::OnResourceReady( Toolkit::Control control )
360 {
361   // Visual ready so update visual attached to this ImageView, following call to RelayoutRequest will use this visual.
362   mVisual = DevelControl::GetVisual( *this, Toolkit::ImageView::Property::IMAGE );
363   Toolkit::DevelControl::RequestLayout( *this );
364 }
365
366 ///////////////////////////////////////////////////////////
367 //
368 // Properties
369 //
370
371 void ImageView::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
372 {
373   Toolkit::ImageView imageView = Toolkit::ImageView::DownCast( Dali::BaseHandle( object ) );
374
375   if ( imageView )
376   {
377     ImageView& impl = GetImpl( imageView );
378     switch ( index )
379     {
380       case Toolkit::ImageView::Property::RESOURCE_URL:
381       {
382         std::string imageUrl;
383         if( value.Get( imageUrl ) )
384         {
385           impl.SetImage( imageUrl, ImageDimensions() );
386         }
387         break;
388       }
389
390       case Toolkit::ImageView::Property::IMAGE:
391       {
392         std::string imageUrl;
393         Property::Map* map;
394         if( value.Get( imageUrl ) )
395         {
396           impl.SetImage( imageUrl, ImageDimensions() );
397         }
398         // if its not a string then get a Property::Map from the property if possible.
399         else
400         {
401           map = value.GetMap();
402           if( map )
403           {
404             Property::Value* shaderValue = map->Find( Toolkit::Visual::Property::SHADER, CUSTOM_SHADER );
405             // set image only if property map contains image information other than custom shader
406             if( map->Count() > 1u ||  !shaderValue )
407             {
408               impl.SetImage( *map );
409             }
410             // the property map contains only the custom shader
411             else if( ( map->Count() == 1u )&&( shaderValue ) )
412             {
413               Property::Map* shaderMap = shaderValue->GetMap();
414               if( shaderMap )
415               {
416                 impl.mShaderMap = *shaderMap;
417
418                 if( !impl.mUrl.empty() )
419                 {
420                   impl.SetImage( impl.mUrl, impl.mImageSize );
421                 }
422                 else if( impl.mImage )
423                 {
424                   impl.SetImage( impl.mImage );
425                 }
426                 else if( !impl.mPropertyMap.Empty() )
427                 {
428                   impl.SetImage( impl.mPropertyMap );
429                 }
430               }
431             }
432           }
433         }
434         break;
435       }
436
437       case Toolkit::ImageView::Property::PRE_MULTIPLIED_ALPHA:
438       {
439         bool isPre;
440         if( value.Get( isPre ) )
441         {
442           impl.EnablePreMultipliedAlpha( isPre );
443         }
444         break;
445       }
446     }
447   }
448 }
449
450 Property::Value ImageView::GetProperty( BaseObject* object, Property::Index propertyIndex )
451 {
452   Property::Value value;
453
454   Toolkit::ImageView imageview = Toolkit::ImageView::DownCast( Dali::BaseHandle( object ) );
455
456   if ( imageview )
457   {
458     ImageView& impl = GetImpl( imageview );
459     switch ( propertyIndex )
460     {
461       case Toolkit::ImageView::Property::RESOURCE_URL:
462       {
463         if ( !impl.mUrl.empty() )
464         {
465           value = impl.mUrl;
466         }
467         break;
468       }
469
470       case Toolkit::ImageView::Property::IMAGE:
471       {
472         if ( !impl.mUrl.empty() )
473         {
474           value = impl.mUrl;
475         }
476         else if( impl.mImage )
477         {
478           Property::Map map;
479           Scripting::CreatePropertyMap( impl.mImage, map );
480           value = map;
481         }
482         else
483         {
484           Property::Map map;
485           Toolkit::Visual::Base visual = DevelControl::GetVisual( impl, Toolkit::ImageView::Property::IMAGE );
486           if( visual )
487           {
488             visual.CreatePropertyMap( map );
489           }
490           value = map;
491         }
492         break;
493       }
494
495       case Toolkit::ImageView::Property::PRE_MULTIPLIED_ALPHA:
496       {
497         value = impl.IsPreMultipliedAlphaEnabled();
498         break;
499       }
500     }
501   }
502
503   return value;
504 }
505
506 } // namespace Internal
507 } // namespace Toolkit
508 } // namespace Dali