Update progress bar example
[platform/core/uifw/dali-demo.git] / examples / progress-bar / progress-bar-example.cpp
index 593e297..f12fd28 100644 (file)
@@ -25,7 +25,7 @@ using Dali::Toolkit::ProgressBar;
 
 namespace
 {
-
+const char * const THEME_PATH( DEMO_STYLE_DIR "progress-bar-example-theme.json" ); ///< The theme used for this example
 const char* const BACKGROUND_IMAGE = DEMO_IMAGE_DIR "background-gradient.jpg";
 const char* const TOOLBAR_IMAGE = DEMO_IMAGE_DIR "top-bar.png";
 const char* const TOOLBAR_TITLE = "Progress Bar";
@@ -36,11 +36,14 @@ const Vector4 BACKGROUND_COLOUR( 1.0f, 1.0f, 1.0f, 0.15f );
 const int MARGIN_SIZE = 10;
 const int TOP_MARGIN = 85;
 
+const unsigned int TIMER_TIMEOUT_TIME = 50;
+const float PROGRESS_INCREMENT_VALUE = 0.01f;
+
 }  // namespace
 
-/** This example shows how to create and use PROGRESS BAR.
+/**
+ * @brief Shows how to create a default progress bar and custom styled progress bars.
  */
-
 class ProgressBarExample: public ConnectionTracker
 {
 public:
@@ -50,13 +53,12 @@ public:
   {
     // Connect to the Application's Init signal
     mProgressValue = 0.0f;
+    mSecondaryProgressValue = 0.1f;
+    isDefaultTheme = true;
     mApplication.InitSignal().Connect( this, &ProgressBarExample::Create );
   }
 
-  ~ProgressBarExample()
-  {
-    // Nothing to do here
-  }
+private:
 
   void Create( Application& application )
   {
@@ -75,11 +77,12 @@ public:
                                             TOOLBAR_IMAGE,
                                             TOOLBAR_TITLE );
 
-    mProgressBar = ProgressBar::New();
-    mProgressBar.SetParentOrigin(ParentOrigin::TOP_CENTER);
-    mProgressBar.SetAnchorPoint(AnchorPoint::TOP_CENTER);
-    mProgressBar.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH);
-    mProgressBar.SetResizePolicy(ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT);
+    mProgressBarDefault = ProgressBar::New();
+    mProgressBarDefault.SetParentOrigin(ParentOrigin::TOP_CENTER);
+    mProgressBarDefault.SetAnchorPoint(AnchorPoint::TOP_CENTER);
+    mProgressBarDefault.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH);
+    mProgressBarDefault.SetResizePolicy(ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT);
+    mProgressBarDefault.ValueChangedSignal().Connect( this, &ProgressBarExample::OnValueChanged );
 
     Toolkit::TableView contentTable = Toolkit::TableView::New(2, 1);
     contentTable.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH);
@@ -103,12 +106,17 @@ public:
     progressBackground.SetBackgroundColor( BACKGROUND_COLOUR );
     progressBackground.SetCellPadding( Size( MARGIN_SIZE, MARGIN_SIZE ) );
     progressBackground.SetRelativeWidth( 0, 1.0f );
-    progressBackground.SetFitHeight( 0 );
+
+    for( unsigned int i = 0; i < progressBackground.GetRows(); ++i )
+    {
+      progressBackground.SetFitHeight( i );
+    }
+
     contentTable.Add( progressBackground );
-    progressBackground.Add( mProgressBar );
+    progressBackground.Add( mProgressBarDefault );
 
     // Create buttons
-    Toolkit::TableView buttonBackground = Toolkit::TableView::New( 2, 1 );
+    Toolkit::TableView buttonBackground = Toolkit::TableView::New( 3, 1 );
     buttonBackground.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
     buttonBackground.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
     buttonBackground.SetBackgroundColor( BACKGROUND_COLOUR );
@@ -121,73 +129,126 @@ public:
 
     contentTable.Add( buttonBackground );
 
-    mSetProgressButton = Toolkit::PushButton::New();
-    mSetProgressButton.SetLabelText( "Set Progress" );
-    mSetProgressButton.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
-    mSetProgressButton.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
-    mSetProgressButton.ClickedSignal().Connect( this, &ProgressBarExample::OnSetProgressButtonSelected );
-
-    buttonBackground.Add( mSetProgressButton );
-
     mResetProgressButton = Toolkit::PushButton::New();
-    mResetProgressButton.SetLabelText( "Reset Progress" );
+    mResetProgressButton.SetProperty( Toolkit::Button::Property::LABEL, "Reset" );
     mResetProgressButton.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
     mResetProgressButton.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
     mResetProgressButton.ClickedSignal().Connect( this, &ProgressBarExample::OnResetProgressButtonSelected );
 
     buttonBackground.Add( mResetProgressButton );
- }
-
- bool OnResetProgressButtonSelected( Toolkit::Button button )
- {
-   mProgressValue = 0.0f;
-   mProgressBar.SetProperty(ProgressBar::Property::PROGRESS_VALUE, 0.0f);
-   return true;
- }
-
- bool OnSetProgressButtonSelected( Toolkit::Button button )
- {
-   mProgressValue += 0.1f;
-   mProgressBar.SetProperty(ProgressBar::Property::PROGRESS_VALUE, mProgressValue);
-   return true;
- }
-
- void OnKeyEvent( const KeyEvent& event )
- {
-   if( event.state == KeyEvent::Down )
-   {
-     if( IsKey( event, Dali::DALI_KEY_ESCAPE ) || IsKey( event, Dali::DALI_KEY_BACK ) )
-     {
-       // Exit application when click back or escape.
-       mApplication.Quit();
-     }
-   }
- }
 
-private:
+    mSetIndeterminateButton = Toolkit::PushButton::New();
+    mSetIndeterminateButton.SetProperty( Toolkit::Button::Property::LABEL, "Toggle Indeterminate" );
+    mSetIndeterminateButton.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
+    mSetIndeterminateButton.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
+    mSetIndeterminateButton.ClickedSignal().Connect( this, &ProgressBarExample::OnSetIndeterminateButtonSelected );
+
+    buttonBackground.Add( mSetIndeterminateButton );
+
+    mChangeThemeButton = Toolkit::PushButton::New();
+    mChangeThemeButton.SetProperty( Toolkit::Button::Property::LABEL, "Change Theme" );
+    mChangeThemeButton.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
+    mChangeThemeButton.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
+    mChangeThemeButton.ClickedSignal().Connect( this, &ProgressBarExample::OnChangeThemeButtonSelected );
+
+    buttonBackground.Add( mChangeThemeButton );
+
+    // Create a timer to update the progress of all progress bars
+    mTimer = Timer::New( TIMER_TIMEOUT_TIME );
+    mTimer.TickSignal().Connect( this, &ProgressBarExample::OnTimerTick );
+    mTimer.Start();
+  }
+
+  bool OnTimerTick()
+  {
+    mProgressValue += PROGRESS_INCREMENT_VALUE;
+    mSecondaryProgressValue = mProgressValue + 0.1f;
+    mProgressBarDefault.SetProperty(ProgressBar::Property::PROGRESS_VALUE, mProgressValue);
+    mProgressBarDefault.SetProperty(ProgressBar::Property::SECONDARY_PROGRESS_VALUE, mSecondaryProgressValue);
+
+    return ( mProgressValue < 1.0f ); // Only call again if progress has NOT got to the end
+  }
+
+  bool OnResetProgressButtonSelected( Toolkit::Button button )
+  {
+    mProgressValue = 0.0f;
+    mSecondaryProgressValue = 0.1f;
+    mProgressBarDefault.SetProperty(ProgressBar::Property::PROGRESS_VALUE, 0.0f);
+    mProgressBarDefault.SetProperty(ProgressBar::Property::SECONDARY_PROGRESS_VALUE, 0.1f);
+    mTimer.Start();
+    return true;
+  }
+
+  void OnValueChanged( ProgressBar progressBar, float value, float secondaryValue )
+  {
+    std::stringstream newLabel;
+    newLabel.precision( 2 );
+    newLabel << std::fixed << "current : " << value << " / loaded : "  << secondaryValue;
+
+    mProgressBarDefault.SetProperty(ProgressBar::Property::LABEL_VISUAL, newLabel.str() );
+  }
+
+  bool OnSetIndeterminateButtonSelected( Toolkit::Button button )
+  {
+    if( mProgressBarDefault.GetProperty<bool>(ProgressBar::Property::INDETERMINATE))
+    {
+      mProgressBarDefault.SetProperty(ProgressBar::Property::INDETERMINATE, false);
+    }
+    else
+    {
+      mProgressBarDefault.SetProperty(ProgressBar::Property::INDETERMINATE, true);
+    }
+    return true;
+  }
+
+  bool OnChangeThemeButtonSelected( Toolkit::Button button )
+  {
+    StyleManager styleManager = StyleManager::Get();
+
+    if( isDefaultTheme )
+    {
+      styleManager.ApplyTheme( THEME_PATH );
+      isDefaultTheme = false;
+    }
+    else
+    {
+      styleManager.ApplyDefaultTheme();
+      isDefaultTheme = true;
+    }
+    return true;
+  }
+  void OnKeyEvent( const KeyEvent& event )
+  {
+    if( event.state == KeyEvent::Down )
+    {
+      if( IsKey( event, Dali::DALI_KEY_ESCAPE ) || IsKey( event, Dali::DALI_KEY_BACK ) )
+      {
+        // Exit application when click back or escape.
+        mApplication.Quit();
+      }
+    }
+  }
+
+  // Data
 
   Application&      mApplication;
+  Timer             mTimer;
   Toolkit::Control  mView;                              ///< The View instance.
   Toolkit::ToolBar  mToolBar;                           ///< The View's Toolbar.
   Layer             mContentLayer;                      ///< Content layer.
-  ProgressBar       mProgressBar;
-  Toolkit::PushButton mSetProgressButton;
+  ProgressBar       mProgressBarDefault;
   Toolkit::PushButton mResetProgressButton;
+  Toolkit::PushButton mSetIndeterminateButton;
+  Toolkit::PushButton mChangeThemeButton;
   float mProgressValue;
+  float mSecondaryProgressValue;
+  bool isDefaultTheme;
 };
 
-void RunTest( Application& application )
+int DALI_EXPORT_API main( int argc, char **argv )
 {
+  Application application = Application::New( &argc, &argv );
   ProgressBarExample test( application );
   application.MainLoop();
-}
-
-// Entry point for Linux & Tizen applications
-int DALI_EXPORT_API main( int argc, char **argv )
-{
-  Application application = Application::New( &argc, &argv, DEMO_THEME_PATH );
-
-  RunTest( application );
-
   return 0;
 }