2 * Copyright (c) 2016 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.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;
39 const unsigned int TIMER_TIMEOUT_TIME = 50;
40 const float PROGRESS_INCREMENT_VALUE = 0.01f;
45 * @brief Shows how to create a default progress bar and custom styled progress bars.
47 class ProgressBarExample: public ConnectionTracker
51 ProgressBarExample( Application& application )
52 : mApplication( application )
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 );
63 void Create( Application& application )
65 // The Init signal is received once (only) during the Application lifetime
67 // Respond to key events
68 Stage::GetCurrent().KeyEventSignal().Connect( this, &ProgressBarExample::OnKeyEvent );
70 // Creates a default view with a default tool bar.
71 // The view is added to the stage.
73 mContentLayer = DemoHelper::CreateView( application,
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 );
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));
94 for( unsigned int i = 0; i < contentTable.GetRows(); ++i )
96 contentTable.SetFitHeight( i );
99 contentTable.SetPosition( 0.0f, TOP_MARGIN );
100 mContentLayer.Add( contentTable );
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 );
110 for( unsigned int i = 0; i < progressBackground.GetRows(); ++i )
112 progressBackground.SetFitHeight( i );
115 contentTable.Add( progressBackground );
116 progressBackground.Add( mProgressBarDefault );
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 ) );
125 for( unsigned int i = 0; i < buttonBackground.GetRows(); ++i )
127 buttonBackground.SetFitHeight( i );
130 contentTable.Add( buttonBackground );
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 );
138 buttonBackground.Add( mResetProgressButton );
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 );
146 buttonBackground.Add( mSetIndeterminateButton );
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 );
154 buttonBackground.Add( mChangeThemeButton );
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 );
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);
169 return ( mProgressValue < 1.0f ); // Only call again if progress has NOT got to the end
172 bool OnResetProgressButtonSelected( Toolkit::Button button )
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);
182 void OnValueChanged( ProgressBar progressBar, float value, float secondaryValue )
184 std::stringstream newLabel;
185 newLabel.precision( 2 );
186 newLabel << std::fixed << "current : " << value << " / loaded : " << secondaryValue;
188 mProgressBarDefault.SetProperty(ProgressBar::Property::LABEL_VISUAL, newLabel.str() );
191 bool OnSetIndeterminateButtonSelected( Toolkit::Button button )
193 if( mProgressBarDefault.GetProperty<bool>(ProgressBar::Property::INDETERMINATE))
195 mProgressBarDefault.SetProperty(ProgressBar::Property::INDETERMINATE, false);
199 mProgressBarDefault.SetProperty(ProgressBar::Property::INDETERMINATE, true);
204 bool OnChangeThemeButtonSelected( Toolkit::Button button )
206 StyleManager styleManager = StyleManager::Get();
210 styleManager.ApplyTheme( THEME_PATH );
211 isDefaultTheme = false;
215 styleManager.ApplyDefaultTheme();
216 isDefaultTheme = true;
220 void OnKeyEvent( const KeyEvent& event )
222 if( event.state == KeyEvent::Down )
224 if( IsKey( event, Dali::DALI_KEY_ESCAPE ) || IsKey( event, Dali::DALI_KEY_BACK ) )
226 // Exit application when click back or escape.
234 Application& mApplication;
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;
248 int DALI_EXPORT_API main( int argc, char **argv )
250 Application application = Application::New( &argc, &argv );
251 ProgressBarExample test( application );
252 application.MainLoop();