Use Property:LABEL instead of SetLabelText for buttons
[platform/core/uifw/dali-demo.git] / examples / image-view / image-view-example.cpp
1 /*
2  * Copyright (c) 2015 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 #include "shared/view.h"
19 #include <dali/dali.h>
20 #include <dali-toolkit/dali-toolkit.h>
21
22 using namespace Dali;
23
24 namespace
25 {
26
27 const char* BACKGROUND_IMAGE( DEMO_IMAGE_DIR "background-gradient.jpg" );
28 const char* TOOLBAR_IMAGE( DEMO_IMAGE_DIR "top-bar.png" );
29 const char* APPLICATION_TITLE( "Image view" );
30
31 const char* IMAGE_PATH[] = {
32     DEMO_IMAGE_DIR "blocks-ball.png",
33     DEMO_IMAGE_DIR "gallery-small-23.jpg",
34     DEMO_IMAGE_DIR "selection-popup-bg.2.9.png",
35     DEMO_IMAGE_DIR "heartsframe.9.png",
36 };
37
38 const unsigned int NUM_IMAGES = sizeof(IMAGE_PATH) / sizeof(char*);
39
40 const unsigned int COLUMNS = 3;
41 const unsigned int ROWS = 4;
42
43 }  // namespace
44
45 class ImageViewController: public ConnectionTracker
46 {
47  public:
48
49   ImageViewController( Application& application )
50     : mApplication( application ),
51       mCurrentPositionToggle( 0, 0 ),
52       mCurrentPositionImage( 0, 0 ),
53       mToggleOff( true ),
54       mImageIdx( 1 )
55   {
56     // Connect to the Application's Init signal
57     mApplication.InitSignal().Connect( this, &ImageViewController::Create );
58   }
59
60   ~ImageViewController()
61   {
62     // Nothing to do here
63   }
64
65   void Create( Application& application )
66   {
67     // The Init signal is received once (only) during the Application lifetime
68
69     // Creates a default view with a default tool bar.
70     // The view is added to the stage.
71     mContentLayer = DemoHelper::CreateView( application,
72                                             mView,
73                                             mToolBar,
74                                             BACKGROUND_IMAGE,
75                                             TOOLBAR_IMAGE,
76                                             APPLICATION_TITLE );
77
78
79     mTable = Toolkit::TableView::New( ROWS, COLUMNS );
80     mTable.SetAnchorPoint( AnchorPoint::CENTER );
81     mTable.SetParentOrigin( ParentOrigin::CENTER );
82     mTable.SetResizePolicy( ResizePolicy::SIZE_FIXED_OFFSET_FROM_PARENT, Dimension::ALL_DIMENSIONS );
83     Vector3 offset( -50.0f, -350.0f, 0.0f );
84     mTable.SetSizeModeFactor( offset );
85
86     mContentLayer.Add( mTable );
87
88     for( unsigned int y = 0; y < ROWS; ++y )
89     {
90       for( unsigned int x = 0; x < COLUMNS; ++x )
91       {
92         mImageViews[x][y] = Toolkit::ImageView::New( IMAGE_PATH[ 0 ] );
93         mImageViews[x][y].SetParentOrigin( ParentOrigin::CENTER );
94         mImageViews[x][y].SetAnchorPoint( AnchorPoint::CENTER );
95         mImageViews[x][y].SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
96
97         mTable.AddChild( mImageViews[x][y], Toolkit::TableView::CellPosition( y, x ) );
98       }
99     }
100
101     Toolkit::TableView buttonsTable = Toolkit::TableView::New( 3, 1 );
102     buttonsTable.SetAnchorPoint( AnchorPoint::BOTTOM_CENTER );
103     buttonsTable.SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
104     buttonsTable.SetFitHeight( 0 );
105     buttonsTable.SetFitHeight( 1 );
106     buttonsTable.SetFitHeight( 2 );
107     buttonsTable.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
108
109     Toolkit::PushButton button = Toolkit::PushButton::New();
110     button.SetProperty( Toolkit::Button::Property::LABEL, "Toggle on/off stage" );
111     button.SetParentOrigin( ParentOrigin::CENTER );
112     button.SetAnchorPoint( AnchorPoint::CENTER );
113     button.ClickedSignal().Connect( this, &ImageViewController::ToggleImageOnStage );
114     button.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
115     buttonsTable.AddChild( button, Toolkit::TableView::CellPosition( 0, 0 ) );
116
117     Toolkit::PushButton button2 = Toolkit::PushButton::New();
118     button2.SetProperty( Toolkit::Button::Property::LABEL, "Change Image" );
119     button2.SetParentOrigin( ParentOrigin::CENTER );
120     button2.SetAnchorPoint( AnchorPoint::CENTER );
121     button2.ClickedSignal().Connect( this, &ImageViewController::ChangeImageClicked );
122     button2.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
123     buttonsTable.AddChild( button2, Toolkit::TableView::CellPosition( 1, 0 ) );
124
125     mContentLayer.Add(buttonsTable);
126
127     Stage::GetCurrent().KeyEventSignal().Connect(this, &ImageViewController::OnKeyEvent);
128   }
129
130 private:
131   bool ToggleImageOnStage( Toolkit::Button button )
132   {
133     Toolkit::ImageView imageView =  mImageViews[ mCurrentPositionToggle.columnIndex ][ mCurrentPositionToggle.rowIndex ];
134
135     if( mToggleOff )
136     {
137       imageView.Unparent();
138     }
139     else
140     {
141       mTable.AddChild( imageView, mCurrentPositionToggle );
142     }
143
144     ++mCurrentPositionToggle.columnIndex;
145     if( mCurrentPositionToggle.columnIndex == COLUMNS )
146     {
147       mCurrentPositionToggle.columnIndex = 0;
148       ++mCurrentPositionToggle.rowIndex;
149     }
150     if( mCurrentPositionToggle.rowIndex == ROWS )
151     {
152       mCurrentPositionToggle.rowIndex = 0;
153       mToggleOff = !mToggleOff;
154     }
155
156     return true;
157   }
158
159   bool ChangeImageClicked( Toolkit::Button button )
160   {
161     Toolkit::ImageView imageView =  mImageViews[ mCurrentPositionImage.columnIndex ][ mCurrentPositionImage.rowIndex ];
162
163     imageView.SetImage( IMAGE_PATH[ mImageIdx ] );
164
165     ++mCurrentPositionImage.columnIndex;
166     if( mCurrentPositionImage.columnIndex == COLUMNS )
167     {
168       mCurrentPositionImage.columnIndex = 0;
169       ++mCurrentPositionImage.rowIndex;
170     }
171     if( mCurrentPositionImage.rowIndex == ROWS )
172     {
173       mCurrentPositionImage.rowIndex = 0;
174       ++mImageIdx;
175
176       if( mImageIdx == NUM_IMAGES )
177       {
178         mImageIdx = 0;
179       }
180     }
181
182     return true;
183   }
184
185   /**
186    * Main key event handler
187    */
188   void OnKeyEvent(const KeyEvent& event)
189   {
190     if(event.state == KeyEvent::Down)
191     {
192       if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
193       {
194         mApplication.Quit();
195       }
196     }
197   }
198
199 private:
200   Application&  mApplication;
201
202   Toolkit::Control           mView;                              ///< The View instance.
203   Toolkit::ToolBar           mToolBar;                           ///< The View's Toolbar.
204   Layer                      mContentLayer;                      ///< Content layer
205   Toolkit::TableView         mTable;
206   Toolkit::ImageView        mImageViews[ COLUMNS ][ ROWS ];
207
208   Toolkit::TableView::CellPosition mCurrentPositionToggle;
209   Toolkit::TableView::CellPosition mCurrentPositionImage;
210
211   bool mToggleOff;
212   int mImageIdx;
213
214 };
215
216 void RunTest( Application& application )
217 {
218   ImageViewController test( application );
219
220   application.MainLoop();
221 }
222
223 // Entry point for Linux & Tizen applications
224 //
225 int DALI_EXPORT_API main( int argc, char **argv )
226 {
227   Application application = Application::New( &argc, &argv, DEMO_THEME_PATH );
228
229   RunTest( application );
230
231   return 0;
232 }