1 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 #include "image-view-impl.h"
7 #include <dali/public-api/images/resource-image.h>
8 #include <dali/public-api/object/type-registry.h>
9 #include <dali/devel-api/object/type-registry-helper.h>
10 #include <dali/devel-api/scripting/scripting.h>
13 #include <dali-toolkit/public-api/controls/image-view/image-view.h>
14 #include <dali-toolkit/devel-api/controls/renderer-factory/renderer-factory.h>
15 #include <dali-toolkit/internal/controls/renderers/image/image-renderer.h>
31 return Toolkit::ImageView::New();
34 // Setup properties, signals and actions using the type-registry.
35 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::ImageView, Toolkit::Control, Create );
36 DALI_PROPERTY_REGISTRATION( Toolkit, ImageView, "image", MAP, IMAGE )
37 DALI_TYPE_REGISTRATION_END()
39 } // anonymous namespace
43 ImageView::ImageView()
44 : Control( ControlBehaviour( ACTOR_BEHAVIOUR_NONE ) )
48 ImageView::~ImageView()
52 Toolkit::ImageView ImageView::New()
54 ImageView* impl = new ImageView();
56 Toolkit::ImageView handle = Toolkit::ImageView( *impl );
58 // Second-phase init of the implementation
59 // This can only be done after the CustomActor connection has been made...
65 /////////////////////////////////////////////////////////////
67 void ImageView::SetImage( Image image )
69 if( ( mImage != image ) ||
70 ! mUrl.empty() || // If we're changing from a URL type to an Image type
71 ! mPropertyMap.Empty() ) // If we're changing from a property map type to an Image type
79 Toolkit::RendererFactory::Get().ResetRenderer( mRenderer, self, image );
80 mImageSize = image ? ImageDimensions( image.GetWidth(), image.GetHeight() ) : ImageDimensions( 0, 0 );
86 void ImageView::SetImage( Property::Map map )
93 Toolkit::RendererFactory::Get().ResetRenderer( mRenderer, self, mPropertyMap );
96 Property::Value* widthValue = mPropertyMap.Find( "width" );
99 widthValue->Get( width );
103 Property::Value* heightValue = mPropertyMap.Find( "height" );
106 heightValue->Get( height );
109 mImageSize = ImageDimensions( width, height );
114 void ImageView::SetImage( const std::string& url, ImageDimensions size )
116 if( ( mUrl != url ) ||
117 mImage || // If we're changing from an Image type to a URL type
118 ! mPropertyMap.Empty() ) // If we're changing from a property map type to a URL type
121 mPropertyMap.Clear();
125 if( size.GetWidth() == 0u && size.GetHeight() == 0u )
127 mImageSize = ResourceImage::GetImageSize( mUrl );
135 Toolkit::RendererFactory::Get().ResetRenderer( mRenderer, self, mUrl, mImageSize );
141 Vector3 ImageView::GetNaturalSize()
145 size.x = mImageSize.GetWidth();
146 size.y = mImageSize.GetHeight();
147 size.z = std::min(size.x, size.y);
149 if( size.x > 0 && size.y > 0 )
155 // if no image then use Control's natural size
156 return Control::GetNaturalSize();
160 float ImageView::GetHeightForWidth( float width )
162 if( mImageSize.GetWidth() > 0 && mImageSize.GetHeight() > 0 )
164 return GetHeightForWidthBase( width );
168 return Control::GetHeightForWidth( width );
172 float ImageView::GetWidthForHeight( float height )
174 if( mImageSize.GetWidth() > 0 && mImageSize.GetHeight() > 0 )
176 return GetWidthForHeightBase( height );
180 return Control::GetWidthForHeight( height );
184 ///////////////////////////////////////////////////////////
189 void ImageView::OnStageConnection( int depth )
191 Control::OnStageConnection( depth );
195 CustomActor self = Self();
196 mRenderer.SetOnStage( self );
200 void ImageView::OnStageDisconnection()
204 CustomActor self = Self();
205 mRenderer.SetOffStage( self );
208 Control::OnStageDisconnection();
212 ///////////////////////////////////////////////////////////
217 void ImageView::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
219 Toolkit::ImageView imageView = Toolkit::ImageView::DownCast( Dali::BaseHandle( object ) );
225 case Toolkit::ImageView::Property::IMAGE:
227 std::string imageUrl;
228 if( value.Get( imageUrl ) )
230 ImageView& impl = GetImpl( imageView );
231 impl.SetImage( imageUrl, ImageDimensions() );
234 // if its not a string then get a Property::Map from the property if possible.
236 if( value.Get( map ) )
238 ImageView& impl = GetImpl( imageView );
239 impl.SetImage( map );
248 Property::Value ImageView::GetProperty( BaseObject* object, Property::Index propertyIndex )
250 Property::Value value;
252 Toolkit::ImageView imageview = Toolkit::ImageView::DownCast( Dali::BaseHandle( object ) );
256 switch ( propertyIndex )
258 case Toolkit::ImageView::Property::IMAGE:
260 ImageView& impl = GetImpl( imageview );
261 if ( !impl.mUrl.empty() )
265 else if( impl.mImage )
268 Scripting::CreatePropertyMap( impl.mImage, map );
271 else if( !impl.mPropertyMap.Empty() )
273 value = impl.mPropertyMap;
283 } // namespace Internal
284 } // namespace Toolkit