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