Use Property:LABEL instead of SetLabelText for buttons
[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     mApplication.InitSignal().Connect( this, &ProgressBarExample::Create );
57   }
58
59 private:
60
61   void Create( Application& application )
62   {
63     // The Init signal is received once (only) during the Application lifetime
64
65     // Respond to key events
66     Stage::GetCurrent().KeyEventSignal().Connect( this, &ProgressBarExample::OnKeyEvent );
67
68     // Creates a default view with a default tool bar.
69     // The view is added to the stage.
70
71     mContentLayer = DemoHelper::CreateView( application,
72                                             mView,
73                                             mToolBar,
74                                             BACKGROUND_IMAGE,
75                                             TOOLBAR_IMAGE,
76                                             TOOLBAR_TITLE );
77
78     mProgressBarDefault = ProgressBar::New();
79     mProgressBarDefault.SetParentOrigin(ParentOrigin::TOP_CENTER);
80     mProgressBarDefault.SetAnchorPoint(AnchorPoint::TOP_CENTER);
81     mProgressBarDefault.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH);
82     mProgressBarDefault.SetResizePolicy(ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT);
83
84     mProgressBarCustomStyle1 = ProgressBar::New();
85     mProgressBarCustomStyle1.SetStyleName( "ProgressBarCustomStyle1" );
86     mProgressBarCustomStyle1.SetParentOrigin(ParentOrigin::TOP_CENTER);
87     mProgressBarCustomStyle1.SetAnchorPoint(AnchorPoint::TOP_CENTER);
88     mProgressBarCustomStyle1.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH);
89     mProgressBarCustomStyle1.SetResizePolicy(ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT);
90
91     mProgressBarCustomStyle2 = ProgressBar::New();
92     mProgressBarCustomStyle2.SetStyleName( "ProgressBarCustomStyle2" );
93     mProgressBarCustomStyle2.SetParentOrigin(ParentOrigin::TOP_CENTER);
94     mProgressBarCustomStyle2.SetAnchorPoint(AnchorPoint::TOP_CENTER);
95     mProgressBarCustomStyle2.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH);
96     mProgressBarCustomStyle2.SetResizePolicy(ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT);
97
98     Toolkit::TableView contentTable = Toolkit::TableView::New(2, 1);
99     contentTable.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH);
100     contentTable.SetResizePolicy(ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT);
101     contentTable.SetAnchorPoint(AnchorPoint::TOP_LEFT);
102     contentTable.SetParentOrigin(ParentOrigin::TOP_LEFT);
103     contentTable.SetCellPadding(Size(MARGIN_SIZE, MARGIN_SIZE * 0.5f));
104
105     for( unsigned int i = 0; i < contentTable.GetRows(); ++i )
106     {
107       contentTable.SetFitHeight( i );
108     }
109
110     contentTable.SetPosition( 0.0f, TOP_MARGIN );
111     mContentLayer.Add( contentTable );
112
113     // Image selector for progress bar
114     Toolkit::TableView progressBackground = Toolkit::TableView::New( 3, 1 );
115     progressBackground.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
116     progressBackground.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
117     progressBackground.SetBackgroundColor( BACKGROUND_COLOUR );
118     progressBackground.SetCellPadding( Size( MARGIN_SIZE, MARGIN_SIZE ) );
119     progressBackground.SetRelativeWidth( 0, 1.0f );
120
121     for( unsigned int i = 0; i < progressBackground.GetRows(); ++i )
122     {
123       progressBackground.SetFitHeight( i );
124     }
125
126     contentTable.Add( progressBackground );
127     progressBackground.Add( mProgressBarDefault );
128     progressBackground.Add( mProgressBarCustomStyle1 );
129     progressBackground.Add( mProgressBarCustomStyle2 );
130
131     // Create buttons
132     Toolkit::TableView buttonBackground = Toolkit::TableView::New( 1, 1 );
133     buttonBackground.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
134     buttonBackground.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
135     buttonBackground.SetBackgroundColor( BACKGROUND_COLOUR );
136     buttonBackground.SetCellPadding( Size( MARGIN_SIZE, MARGIN_SIZE ) );
137
138     for( unsigned int i = 0; i < buttonBackground.GetRows(); ++i )
139     {
140       buttonBackground.SetFitHeight( i );
141     }
142
143     contentTable.Add( buttonBackground );
144
145     mResetProgressButton = Toolkit::PushButton::New();
146     mResetProgressButton.SetProperty( Toolkit::Button::Property::LABEL, "Reset" );
147     mResetProgressButton.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
148     mResetProgressButton.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
149     mResetProgressButton.ClickedSignal().Connect( this, &ProgressBarExample::OnResetProgressButtonSelected );
150
151     buttonBackground.Add( mResetProgressButton );
152
153     // Create a timer to update the progress of all progress bars
154     mTimer = Timer::New( TIMER_TIMEOUT_TIME );
155     mTimer.TickSignal().Connect( this, &ProgressBarExample::OnTimerTick );
156     mTimer.Start();
157   }
158
159   bool OnTimerTick()
160   {
161     mProgressValue += PROGRESS_INCREMENT_VALUE;
162     mProgressBarDefault.SetProperty(ProgressBar::Property::PROGRESS_VALUE, mProgressValue);
163     mProgressBarCustomStyle1.SetProperty(ProgressBar::Property::PROGRESS_VALUE, mProgressValue);
164     mProgressBarCustomStyle2.SetProperty(ProgressBar::Property::PROGRESS_VALUE, mProgressValue);
165
166     return ( mProgressValue < 1.0f ); // Only call again if progress has NOT got to the end
167   }
168
169   bool OnResetProgressButtonSelected( Toolkit::Button button )
170   {
171     mProgressValue = 0.0f;
172     mProgressBarDefault.SetProperty(ProgressBar::Property::PROGRESS_VALUE, 0.0f);
173     mProgressBarCustomStyle1.SetProperty(ProgressBar::Property::PROGRESS_VALUE, 0.0f);
174     mProgressBarCustomStyle2.SetProperty(ProgressBar::Property::PROGRESS_VALUE, 0.0f);
175     mTimer.Start();
176     return true;
177   }
178
179   void OnKeyEvent( const KeyEvent& event )
180   {
181     if( event.state == KeyEvent::Down )
182     {
183       if( IsKey( event, Dali::DALI_KEY_ESCAPE ) || IsKey( event, Dali::DALI_KEY_BACK ) )
184       {
185         // Exit application when click back or escape.
186         mApplication.Quit();
187       }
188     }
189   }
190
191   // Data
192
193   Application&      mApplication;
194   Timer             mTimer;
195   Toolkit::Control  mView;                              ///< The View instance.
196   Toolkit::ToolBar  mToolBar;                           ///< The View's Toolbar.
197   Layer             mContentLayer;                      ///< Content layer.
198   ProgressBar       mProgressBarDefault;
199   ProgressBar       mProgressBarCustomStyle1;
200   ProgressBar       mProgressBarCustomStyle2;
201   Toolkit::PushButton mResetProgressButton;
202   float mProgressValue;
203 };
204
205 int DALI_EXPORT_API main( int argc, char **argv )
206 {
207   Application application = Application::New( &argc, &argv, THEME_PATH );
208   ProgressBarExample test( application );
209   application.MainLoop();
210   return 0;
211 }