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