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