2 * Copyright (c) 2020 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
24 using namespace Dali::Toolkit;
25 using Dali::Toolkit::ProgressBar;
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";
34 const Vector4 BACKGROUND_COLOUR( 1.0f, 1.0f, 1.0f, 0.15f );
37 const int MARGIN_SIZE = 10;
38 const int TOP_MARGIN = 85;
39 const int CIRCULAR_PROGRESS_BAR_SIZE = 64;
41 const unsigned int TIMER_TIMEOUT_TIME = 50;
42 const float PROGRESS_INCREMENT_VALUE = 0.01f;
47 * @brief Shows how to create a default progress bar and custom styled progress bars.
49 class ProgressBarExample: public ConnectionTracker
53 ProgressBarExample( Application& application )
54 : mApplication( application )
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 );
65 void Create( Application& application )
67 // The Init signal is received once (only) during the Application lifetime
69 // Respond to key events
70 application.GetWindow().KeyEventSignal().Connect( this, &ProgressBarExample::OnKeyEvent );
72 // Creates a default view with a default tool bar.
73 // The view is added to the window.
75 mContentLayer = DemoHelper::CreateView( application,
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 );
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 ) );
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));
102 for( unsigned int i = 0; i < contentTable.GetRows(); ++i )
104 contentTable.SetFitHeight( i );
107 contentTable.SetProperty( Actor::Property::POSITION, Vector2( 0.0f, TOP_MARGIN ));
108 mContentLayer.Add( contentTable );
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 );
118 for( unsigned int i = 0; i < progressBackground.GetRows(); ++i )
120 progressBackground.SetFitHeight( i );
123 contentTable.Add( progressBackground );
124 progressBackground.Add( mProgressBarDefault );
125 progressBackground.Add( mProgressBarCircular );
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 ) );
134 for( unsigned int i = 0; i < buttonBackground.GetRows(); ++i )
136 buttonBackground.SetFitHeight( i );
139 contentTable.Add( buttonBackground );
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 );
147 buttonBackground.Add( mResetProgressButton );
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 );
155 buttonBackground.Add( mSetIndeterminateButton );
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 );
163 buttonBackground.Add( mChangeThemeButton );
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 );
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 );
180 return ( mProgressValue < 1.0f ); // Only call again if progress has NOT got to the end
183 bool OnResetProgressButtonSelected( Toolkit::Button button )
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 );
195 void OnValueChanged( ProgressBar progressBar, float value, float secondaryValue )
197 std::stringstream newLabel;
198 newLabel.precision( 2 );
199 newLabel << std::fixed << "current : " << value << " / loaded : " << secondaryValue;
201 mProgressBarDefault.SetProperty(ProgressBar::Property::LABEL_VISUAL, newLabel.str() );
204 bool OnSetIndeterminateButtonSelected( Toolkit::Button button )
206 if( mProgressBarDefault.GetProperty<bool>(ProgressBar::Property::INDETERMINATE))
208 mProgressBarDefault.SetProperty(ProgressBar::Property::INDETERMINATE, false);
209 mProgressBarCircular.SetProperty( ProgressBar::Property::INDETERMINATE, false );
213 mProgressBarDefault.SetProperty(ProgressBar::Property::INDETERMINATE, true);
214 mProgressBarCircular.SetProperty( ProgressBar::Property::INDETERMINATE, true );
219 bool OnChangeThemeButtonSelected( Toolkit::Button button )
221 StyleManager styleManager = StyleManager::Get();
225 styleManager.ApplyTheme( THEME_PATH );
226 isDefaultTheme = false;
230 styleManager.ApplyDefaultTheme();
231 isDefaultTheme = true;
235 void OnKeyEvent( const KeyEvent& event )
237 if( event.GetState() == KeyEvent::DOWN )
239 if( IsKey( event, Dali::DALI_KEY_ESCAPE ) || IsKey( event, Dali::DALI_KEY_BACK ) )
241 // Exit application when click back or escape.
249 Application& mApplication;
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;
264 int DALI_EXPORT_API main( int argc, char **argv )
266 Application application = Application::New( &argc, &argv );
267 ProgressBarExample test( application );
268 application.MainLoop();