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>
20 #include <dali-toolkit/devel-api/controls/progress-bar/progress-bar-devel.h>
23 using namespace Dali::Toolkit;
24 using Dali::Toolkit::ProgressBar;
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";
33 const Vector4 BACKGROUND_COLOUR( 1.0f, 1.0f, 1.0f, 0.15f );
36 const int MARGIN_SIZE = 10;
37 const int TOP_MARGIN = 85;
38 const int CIRCULAR_PROGRESS_BAR_SIZE = 64;
40 const unsigned int TIMER_TIMEOUT_TIME = 50;
41 const float PROGRESS_INCREMENT_VALUE = 0.01f;
46 * @brief Shows how to create a default progress bar and custom styled progress bars.
48 class ProgressBarExample: public ConnectionTracker
52 ProgressBarExample( Application& application )
53 : mApplication( application )
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 );
64 void Create( Application& application )
66 // The Init signal is received once (only) during the Application lifetime
68 // Respond to key events
69 Stage::GetCurrent().KeyEventSignal().Connect( this, &ProgressBarExample::OnKeyEvent );
71 // Creates a default view with a default tool bar.
72 // The view is added to the stage.
74 mContentLayer = DemoHelper::CreateView( application,
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 );
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 ) );
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));
101 for( unsigned int i = 0; i < contentTable.GetRows(); ++i )
103 contentTable.SetFitHeight( i );
106 contentTable.SetProperty( Actor::Property::POSITION, Vector2( 0.0f, TOP_MARGIN ));
107 mContentLayer.Add( contentTable );
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 );
117 for( unsigned int i = 0; i < progressBackground.GetRows(); ++i )
119 progressBackground.SetFitHeight( i );
122 contentTable.Add( progressBackground );
123 progressBackground.Add( mProgressBarDefault );
124 progressBackground.Add( mProgressBarCircular );
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 ) );
133 for( unsigned int i = 0; i < buttonBackground.GetRows(); ++i )
135 buttonBackground.SetFitHeight( i );
138 contentTable.Add( buttonBackground );
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 );
146 buttonBackground.Add( mResetProgressButton );
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 );
154 buttonBackground.Add( mSetIndeterminateButton );
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 );
162 buttonBackground.Add( mChangeThemeButton );
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 );
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 );
179 return ( mProgressValue < 1.0f ); // Only call again if progress has NOT got to the end
182 bool OnResetProgressButtonSelected( Toolkit::Button button )
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 );
194 void OnValueChanged( ProgressBar progressBar, float value, float secondaryValue )
196 std::stringstream newLabel;
197 newLabel.precision( 2 );
198 newLabel << std::fixed << "current : " << value << " / loaded : " << secondaryValue;
200 mProgressBarDefault.SetProperty(ProgressBar::Property::LABEL_VISUAL, newLabel.str() );
203 bool OnSetIndeterminateButtonSelected( Toolkit::Button button )
205 if( mProgressBarDefault.GetProperty<bool>(ProgressBar::Property::INDETERMINATE))
207 mProgressBarDefault.SetProperty(ProgressBar::Property::INDETERMINATE, false);
208 mProgressBarCircular.SetProperty( ProgressBar::Property::INDETERMINATE, false );
212 mProgressBarDefault.SetProperty(ProgressBar::Property::INDETERMINATE, true);
213 mProgressBarCircular.SetProperty( ProgressBar::Property::INDETERMINATE, true );
218 bool OnChangeThemeButtonSelected( Toolkit::Button button )
220 StyleManager styleManager = StyleManager::Get();
224 styleManager.ApplyTheme( THEME_PATH );
225 isDefaultTheme = false;
229 styleManager.ApplyDefaultTheme();
230 isDefaultTheme = true;
234 void OnKeyEvent( const KeyEvent& event )
236 if( event.state == KeyEvent::Down )
238 if( IsKey( event, Dali::DALI_KEY_ESCAPE ) || IsKey( event, Dali::DALI_KEY_BACK ) )
240 // Exit application when click back or escape.
248 Application& mApplication;
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;
263 int DALI_EXPORT_API main( int argc, char **argv )
265 Application application = Application::New( &argc, &argv );
266 ProgressBarExample test( application );
267 application.MainLoop();