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