Further Setter/Getter public API removal from Dali::Actor
[platform/core/uifw/dali-demo.git] / examples / progress-bar / progress-bar-example.cpp
1 /*
2  * Copyright (c) 2017 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-toolkit/dali-toolkit.h>
20
21 using namespace Dali;
22 using namespace Dali::Toolkit;
23 using Dali::Toolkit::ProgressBar;
24
25 namespace
26 {
27 const char * const THEME_PATH( DEMO_STYLE_DIR "progress-bar-example-theme.json" ); ///< The theme used for this example
28 const char* const BACKGROUND_IMAGE = DEMO_IMAGE_DIR "background-gradient.jpg";
29 const char* const TOOLBAR_IMAGE = DEMO_IMAGE_DIR "top-bar.png";
30 const char* const TOOLBAR_TITLE = "Progress Bar";
31
32 const Vector4 BACKGROUND_COLOUR( 1.0f, 1.0f, 1.0f, 0.15f );
33
34 // Layout sizes
35 const int MARGIN_SIZE = 10;
36 const int TOP_MARGIN = 85;
37
38 const unsigned int TIMER_TIMEOUT_TIME = 50;
39 const float PROGRESS_INCREMENT_VALUE = 0.01f;
40
41 }  // namespace
42
43 /**
44  * @brief Shows how to create a default progress bar and custom styled progress bars.
45  */
46 class ProgressBarExample: public ConnectionTracker
47 {
48 public:
49
50   ProgressBarExample( Application& application )
51     : mApplication( application )
52   {
53     // Connect to the Application's Init signal
54     mProgressValue = 0.0f;
55     mSecondaryProgressValue = 0.1f;
56     isDefaultTheme = true;
57     mApplication.InitSignal().Connect( this, &ProgressBarExample::Create );
58   }
59
60 private:
61
62   void Create( Application& application )
63   {
64     // The Init signal is received once (only) during the Application lifetime
65
66     // Respond to key events
67     Stage::GetCurrent().KeyEventSignal().Connect( this, &ProgressBarExample::OnKeyEvent );
68
69     // Creates a default view with a default tool bar.
70     // The view is added to the stage.
71
72     mContentLayer = DemoHelper::CreateView( application,
73                                             mView,
74                                             mToolBar,
75                                             BACKGROUND_IMAGE,
76                                             TOOLBAR_IMAGE,
77                                             TOOLBAR_TITLE );
78
79     mProgressBarDefault = ProgressBar::New();
80     mProgressBarDefault.SetProperty( Actor::Property::PARENT_ORIGIN,ParentOrigin::TOP_CENTER);
81     mProgressBarDefault.SetProperty( Actor::Property::ANCHOR_POINT,AnchorPoint::TOP_CENTER);
82     mProgressBarDefault.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH);
83     mProgressBarDefault.SetResizePolicy(ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT);
84     mProgressBarDefault.ValueChangedSignal().Connect( this, &ProgressBarExample::OnValueChanged );
85
86     Toolkit::TableView contentTable = Toolkit::TableView::New(2, 1);
87     contentTable.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH);
88     contentTable.SetResizePolicy(ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT);
89     contentTable.SetProperty( Actor::Property::ANCHOR_POINT,AnchorPoint::TOP_LEFT);
90     contentTable.SetProperty( Actor::Property::PARENT_ORIGIN,ParentOrigin::TOP_LEFT);
91     contentTable.SetCellPadding(Size(MARGIN_SIZE, MARGIN_SIZE * 0.5f));
92
93     for( unsigned int i = 0; i < contentTable.GetRows(); ++i )
94     {
95       contentTable.SetFitHeight( i );
96     }
97
98     contentTable.SetProperty( Actor::Property::POSITION, Vector2( 0.0f, TOP_MARGIN ));
99     mContentLayer.Add( contentTable );
100
101     // Image selector for progress bar
102     Toolkit::TableView progressBackground = Toolkit::TableView::New( 1, 1 );
103     progressBackground.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
104     progressBackground.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
105     progressBackground.SetBackgroundColor( BACKGROUND_COLOUR );
106     progressBackground.SetCellPadding( Size( MARGIN_SIZE, MARGIN_SIZE ) );
107     progressBackground.SetRelativeWidth( 0, 1.0f );
108
109     for( unsigned int i = 0; i < progressBackground.GetRows(); ++i )
110     {
111       progressBackground.SetFitHeight( i );
112     }
113
114     contentTable.Add( progressBackground );
115     progressBackground.Add( mProgressBarDefault );
116
117     // Create buttons
118     Toolkit::TableView buttonBackground = Toolkit::TableView::New( 3, 1 );
119     buttonBackground.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
120     buttonBackground.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
121     buttonBackground.SetBackgroundColor( BACKGROUND_COLOUR );
122     buttonBackground.SetCellPadding( Size( MARGIN_SIZE, MARGIN_SIZE ) );
123
124     for( unsigned int i = 0; i < buttonBackground.GetRows(); ++i )
125     {
126       buttonBackground.SetFitHeight( i );
127     }
128
129     contentTable.Add( buttonBackground );
130
131     mResetProgressButton = Toolkit::PushButton::New();
132     mResetProgressButton.SetProperty( Toolkit::Button::Property::LABEL, "Reset" );
133     mResetProgressButton.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
134     mResetProgressButton.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
135     mResetProgressButton.ClickedSignal().Connect( this, &ProgressBarExample::OnResetProgressButtonSelected );
136
137     buttonBackground.Add( mResetProgressButton );
138
139     mSetIndeterminateButton = Toolkit::PushButton::New();
140     mSetIndeterminateButton.SetProperty( Toolkit::Button::Property::LABEL, "Toggle Indeterminate" );
141     mSetIndeterminateButton.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
142     mSetIndeterminateButton.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
143     mSetIndeterminateButton.ClickedSignal().Connect( this, &ProgressBarExample::OnSetIndeterminateButtonSelected );
144
145     buttonBackground.Add( mSetIndeterminateButton );
146
147     mChangeThemeButton = Toolkit::PushButton::New();
148     mChangeThemeButton.SetProperty( Toolkit::Button::Property::LABEL, "Change Theme" );
149     mChangeThemeButton.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
150     mChangeThemeButton.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
151     mChangeThemeButton.ClickedSignal().Connect( this, &ProgressBarExample::OnChangeThemeButtonSelected );
152
153     buttonBackground.Add( mChangeThemeButton );
154
155     // Create a timer to update the progress of all progress bars
156     mTimer = Timer::New( TIMER_TIMEOUT_TIME );
157     mTimer.TickSignal().Connect( this, &ProgressBarExample::OnTimerTick );
158     mTimer.Start();
159   }
160
161   bool OnTimerTick()
162   {
163     mProgressValue += PROGRESS_INCREMENT_VALUE;
164     mSecondaryProgressValue = mProgressValue + 0.1f;
165     mProgressBarDefault.SetProperty(ProgressBar::Property::PROGRESS_VALUE, mProgressValue);
166     mProgressBarDefault.SetProperty(ProgressBar::Property::SECONDARY_PROGRESS_VALUE, mSecondaryProgressValue);
167
168     return ( mProgressValue < 1.0f ); // Only call again if progress has NOT got to the end
169   }
170
171   bool OnResetProgressButtonSelected( Toolkit::Button button )
172   {
173     mProgressValue = 0.0f;
174     mSecondaryProgressValue = 0.1f;
175     mProgressBarDefault.SetProperty(ProgressBar::Property::PROGRESS_VALUE, 0.0f);
176     mProgressBarDefault.SetProperty(ProgressBar::Property::SECONDARY_PROGRESS_VALUE, 0.1f);
177     mTimer.Start();
178     return true;
179   }
180
181   void OnValueChanged( ProgressBar progressBar, float value, float secondaryValue )
182   {
183     std::stringstream newLabel;
184     newLabel.precision( 2 );
185     newLabel << std::fixed << "current : " << value << " / loaded : "  << secondaryValue;
186
187     mProgressBarDefault.SetProperty(ProgressBar::Property::LABEL_VISUAL, newLabel.str() );
188   }
189
190   bool OnSetIndeterminateButtonSelected( Toolkit::Button button )
191   {
192     if( mProgressBarDefault.GetProperty<bool>(ProgressBar::Property::INDETERMINATE))
193     {
194       mProgressBarDefault.SetProperty(ProgressBar::Property::INDETERMINATE, false);
195     }
196     else
197     {
198       mProgressBarDefault.SetProperty(ProgressBar::Property::INDETERMINATE, true);
199     }
200     return true;
201   }
202
203   bool OnChangeThemeButtonSelected( Toolkit::Button button )
204   {
205     StyleManager styleManager = StyleManager::Get();
206
207     if( isDefaultTheme )
208     {
209       styleManager.ApplyTheme( THEME_PATH );
210       isDefaultTheme = false;
211     }
212     else
213     {
214       styleManager.ApplyDefaultTheme();
215       isDefaultTheme = true;
216     }
217     return true;
218   }
219   void OnKeyEvent( const KeyEvent& event )
220   {
221     if( event.state == KeyEvent::Down )
222     {
223       if( IsKey( event, Dali::DALI_KEY_ESCAPE ) || IsKey( event, Dali::DALI_KEY_BACK ) )
224       {
225         // Exit application when click back or escape.
226         mApplication.Quit();
227       }
228     }
229   }
230
231   // Data
232
233   Application&      mApplication;
234   Timer             mTimer;
235   Toolkit::Control  mView;                              ///< The View instance.
236   Toolkit::ToolBar  mToolBar;                           ///< The View's Toolbar.
237   Layer             mContentLayer;                      ///< Content layer.
238   ProgressBar       mProgressBarDefault;
239   Toolkit::PushButton mResetProgressButton;
240   Toolkit::PushButton mSetIndeterminateButton;
241   Toolkit::PushButton mChangeThemeButton;
242   float mProgressValue;
243   float mSecondaryProgressValue;
244   bool isDefaultTheme;
245 };
246
247 int DALI_EXPORT_API main( int argc, char **argv )
248 {
249   Application application = Application::New( &argc, &argv );
250   ProgressBarExample test( application );
251   application.MainLoop();
252   return 0;
253 }