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