2 * Copyright (c) 2017 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>
22 using namespace Dali::Toolkit;
23 using Dali::Toolkit::ProgressBar;
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";
32 const Vector4 BACKGROUND_COLOUR( 1.0f, 1.0f, 1.0f, 0.15f );
35 const int MARGIN_SIZE = 10;
36 const int TOP_MARGIN = 85;
38 const unsigned int TIMER_TIMEOUT_TIME = 50;
39 const float PROGRESS_INCREMENT_VALUE = 0.01f;
44 * @brief Shows how to create a default progress bar and custom styled progress bars.
46 class ProgressBarExample: public ConnectionTracker
50 ProgressBarExample( Application& application )
51 : mApplication( application )
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 );
62 void Create( Application& application )
64 // The Init signal is received once (only) during the Application lifetime
66 // Respond to key events
67 Stage::GetCurrent().KeyEventSignal().Connect( this, &ProgressBarExample::OnKeyEvent );
69 // Creates a default view with a default tool bar.
70 // The view is added to the stage.
72 mContentLayer = DemoHelper::CreateView( application,
79 mProgressBarDefault = ProgressBar::New();
80 mProgressBarDefault.SetParentOrigin(ParentOrigin::TOP_CENTER);
81 mProgressBarDefault.SetAnchorPoint(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 );
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.SetAnchorPoint(AnchorPoint::TOP_LEFT);
90 contentTable.SetParentOrigin(ParentOrigin::TOP_LEFT);
91 contentTable.SetCellPadding(Size(MARGIN_SIZE, MARGIN_SIZE * 0.5f));
93 for( unsigned int i = 0; i < contentTable.GetRows(); ++i )
95 contentTable.SetFitHeight( i );
98 contentTable.SetPosition( 0.0f, TOP_MARGIN );
99 mContentLayer.Add( contentTable );
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 );
109 for( unsigned int i = 0; i < progressBackground.GetRows(); ++i )
111 progressBackground.SetFitHeight( i );
114 contentTable.Add( progressBackground );
115 progressBackground.Add( mProgressBarDefault );
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 ) );
124 for( unsigned int i = 0; i < buttonBackground.GetRows(); ++i )
126 buttonBackground.SetFitHeight( i );
129 contentTable.Add( buttonBackground );
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 );
137 buttonBackground.Add( mResetProgressButton );
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 );
145 buttonBackground.Add( mSetIndeterminateButton );
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 );
153 buttonBackground.Add( mChangeThemeButton );
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 );
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);
168 return ( mProgressValue < 1.0f ); // Only call again if progress has NOT got to the end
171 bool OnResetProgressButtonSelected( Toolkit::Button button )
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);
181 void OnValueChanged( ProgressBar progressBar, float value, float secondaryValue )
183 std::stringstream newLabel;
184 newLabel.precision( 2 );
185 newLabel << std::fixed << "current : " << value << " / loaded : " << secondaryValue;
187 mProgressBarDefault.SetProperty(ProgressBar::Property::LABEL_VISUAL, newLabel.str() );
190 bool OnSetIndeterminateButtonSelected( Toolkit::Button button )
192 if( mProgressBarDefault.GetProperty<bool>(ProgressBar::Property::INDETERMINATE))
194 mProgressBarDefault.SetProperty(ProgressBar::Property::INDETERMINATE, false);
198 mProgressBarDefault.SetProperty(ProgressBar::Property::INDETERMINATE, true);
203 bool OnChangeThemeButtonSelected( Toolkit::Button button )
205 StyleManager styleManager = StyleManager::Get();
209 styleManager.ApplyTheme( THEME_PATH );
210 isDefaultTheme = false;
214 styleManager.ApplyDefaultTheme();
215 isDefaultTheme = true;
219 void OnKeyEvent( const KeyEvent& event )
221 if( event.state == KeyEvent::Down )
223 if( IsKey( event, Dali::DALI_KEY_ESCAPE ) || IsKey( event, Dali::DALI_KEY_BACK ) )
225 // Exit application when click back or escape.
233 Application& mApplication;
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;
247 int DALI_EXPORT_API main( int argc, char **argv )
249 Application application = Application::New( &argc, &argv );
250 ProgressBarExample test( application );
251 application.MainLoop();