SVACE issue resolved
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / styling / style-manager-impl.cpp
index 41f20e3..98596eb 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 // EXTERNAL INCLUDES
 #include <dali/devel-api/adaptor-framework/singleton-service.h>
 #include <dali/public-api/object/type-registry.h>
-#include <dali/devel-api/object/type-registry-helper.h>
+#include <dali/public-api/object/type-registry-helper.h>
 #include <dali/integration-api/debug.h>
+#include <dali/public-api/adaptor-framework/application.h>
 
 // INTERNAL INCLUDES
+#include <dali-toolkit/internal/builder/builder-impl.h>
 #include <dali-toolkit/public-api/controls/control.h>
 #include <dali-toolkit/public-api/controls/control-impl.h>
-#include <dali-toolkit/devel-api/styling/style-manager.h>
+#include <dali-toolkit/public-api/styling/style-manager.h>
 #include <dali-toolkit/internal/feedback/feedback-style.h>
 
 namespace
 {
 
-const char* LANDSCAPE_QUALIFIER = "landscape";
+//const char* LANDSCAPE_QUALIFIER = "landscape";
 const char* PORTRAIT_QUALIFIER  = "portrait";
-const char* FONT_SIZE_QUALIFIER = "font-size-";
+const char* FONT_SIZE_QUALIFIER = "fontsize";
 
 const char* DEFAULT_THEME = DALI_STYLE_DIR "dali-toolkit-default-theme.json";
 
 const char* PACKAGE_PATH_KEY = "PACKAGE_PATH";
+const char* APPLICATION_RESOURCE_PATH_KEY = "APPLICATION_RESOURCE_PATH";
+
 const char* DEFAULT_PACKAGE_PATH = DALI_DATA_READ_ONLY_DIR "/toolkit/";
 
 } // namespace
@@ -98,26 +102,23 @@ Toolkit::StyleManager StyleManager::Get()
 }
 
 StyleManager::StyleManager()
-: mOrientationDegrees( 0 ),  // Portrait
-  mDefaultFontSize( -1 ),
+: mDefaultFontSize( -1 ),
   mDefaultFontFamily(""),
-  mThemeFile( DEFAULT_THEME ),
   mFeedbackStyle( NULL )
 {
   // Add theme builder constants
   mThemeBuilderConstants[ PACKAGE_PATH_KEY ] = DEFAULT_PACKAGE_PATH;
+  mThemeBuilderConstants[ APPLICATION_RESOURCE_PATH_KEY ] = Application::GetResourcePath();
 
   mStyleMonitor = StyleMonitor::Get();
   if( mStyleMonitor )
   {
     mStyleMonitor.StyleChangeSignal().Connect( this, &StyleManager::StyleMonitorChange );
-
     mDefaultFontSize = mStyleMonitor.GetDefaultFontSize();
   }
 
   // Sound & haptic style
   mFeedbackStyle = new FeedbackStyle();
-
 }
 
 StyleManager::~StyleManager()
@@ -125,47 +126,22 @@ StyleManager::~StyleManager()
   delete mFeedbackStyle;
 }
 
-void StyleManager::SetOrientationValue( int orientation )
-{
-  if( orientation !=  mOrientationDegrees )
-  {
-    mOrientationDegrees = orientation;
-    // TODO: if orientation changed, apply the new style to all controls
-    // dont want to really do the whole load from file again if the bundle contains both portrait & landscape
-    SetTheme();
-  }
-}
-
-int StyleManager::GetOrientationValue()
+void StyleManager::ApplyTheme( const std::string& themeFile )
 {
-  return mOrientationDegrees;
+  SetTheme( themeFile );
 }
 
-void StyleManager::SetOrientation( Orientation orientation )
+void StyleManager::ApplyDefaultTheme()
 {
-  if( mOrientation )
-  {
-    mOrientation.ChangedSignal().Disconnect( this, &StyleManager::OnOrientationChanged );
-  }
-
-  OnOrientationChanged( orientation );
-
-  if( mOrientation )
-  {
-    mOrientation.ChangedSignal().Connect( this, &StyleManager::OnOrientationChanged );
-  }
+  std::string empty;
+  SetTheme( empty );
 }
 
-std::string StyleManager::GetDefaultFontFamily() const
+const std::string& StyleManager::GetDefaultFontFamily() const
 {
   return mDefaultFontFamily;
 }
 
-Orientation StyleManager::GetOrientation()
-{
-  return mOrientation;
-}
-
 void StyleManager::SetStyleConstant( const std::string& key, const Property::Value& value )
 {
   mStyleBuilderConstants[ key ] = value;
@@ -183,122 +159,11 @@ bool StyleManager::GetStyleConstant( const std::string& key, Property::Value& va
   return false;
 }
 
-void StyleManager::OnOrientationChanged( Orientation orientation )
-{
-  mOrientation = orientation;
-  // TODO: if orientation changed, apply the new style to all controls
-  // dont want to really do the whole load from file again if the bundle contains both portrait & landscape
-  SetTheme();
-}
-
-Toolkit::Builder StyleManager::CreateBuilder( const Property::Map& constants )
-{
-  Toolkit::Builder builder = Toolkit::Builder::New();
-  builder.AddConstants( constants );
-
-  return builder;
-}
-
-bool StyleManager::LoadJSON( Toolkit::Builder builder, const std::string& jsonFilePath )
-{
-  std::string fileString;
-  if( LoadFile( jsonFilePath, fileString ) )
-  {
-    builder.LoadFromString( fileString );
-    return true;
-  }
-  else
-  {
-    DALI_LOG_WARNING("Error loading file '%s'\n", jsonFilePath.c_str());
-    return false;
-  }
-}
-
-void StyleManager::CollectQualifiers( StringList& qualifiersOut )
-{
-  // Append the relevant qualifier for orientation
-  int orientation = mOrientationDegrees;
-
-  if( mOrientation )
-  {
-    orientation = mOrientation.GetDegrees();
-  }
-
-  switch( orientation )
-  {
-    case 90:
-    case 270:
-    {
-      qualifiersOut.push_back( std::string( LANDSCAPE_QUALIFIER ) );
-      break;
-    }
-    case 180:
-    case 0: // fall through
-    default:
-    {
-      qualifiersOut.push_back( std::string( PORTRAIT_QUALIFIER ) );
-      break;
-    }
-  }
-}
-
-void StyleManager::BuildQualifiedStyleName( const std::string& styleName, const StringList& qualifiers, std::string& qualifiedStyleOut )
-{
-  qualifiedStyleOut.append( styleName );
-
-  for( StringList::const_iterator it = qualifiers.begin(), itEnd = qualifiers.end(); it != itEnd; ++it )
-  {
-    const std::string& str = *it;
-
-    qualifiedStyleOut.append( "-" );
-    qualifiedStyleOut.append( str );
-  }
-}
-
-void StyleManager::ApplyStyle( Toolkit::Builder builder, Toolkit::Control control )
-{
-  std::string styleName = control.GetStyleName();
-
-  if( styleName.empty() )
-  {
-    // Convert control name to lower case
-    styleName = control.GetTypeName();
-    std::transform( styleName.begin(), styleName.end(), styleName.begin(), ::tolower );
-  }
-
-  // Apply the style after choosing the correct actual style (e.g. landscape or portrait)
-  StringList qualifiers;
-  CollectQualifiers( qualifiers );
-
-  while( true )
-  {
-    std::string qualifiedStyleName;
-    BuildQualifiedStyleName( styleName, qualifiers, qualifiedStyleName );
-
-    // Break if style found or we have tried the root style name (qualifiers is empty)
-    if( builder.ApplyStyle( qualifiedStyleName, control ) || qualifiers.size() == 0 )
-    {
-      break;
-    }
-
-    // Remove the last qualifier in an attempt to find a style that is valid
-    qualifiers.pop_back();
-  }
-
-  if( mDefaultFontSize >= 0 )
-  {
-    // Apply the style for logical font size
-    std::stringstream fontSizeQualifier;
-    fontSizeQualifier << styleName << "-" << FONT_SIZE_QUALIFIER << mDefaultFontSize;
-    builder.ApplyStyle( fontSizeQualifier.str(), control );
-  }
-}
-
 void StyleManager::ApplyThemeStyle( Toolkit::Control control )
 {
   if( !mThemeBuilder )
   {
-    RequestDefaultTheme();
+    ApplyDefaultTheme();
   }
 
   if( mThemeBuilder )
@@ -350,6 +215,46 @@ void StyleManager::ApplyStyle( Toolkit::Control control, const std::string& json
   }
 }
 
+Toolkit::StyleManager::StyleChangedSignalType& StyleManager::StyleChangedSignal()
+{
+  return mStyleChangedSignal;
+}
+
+Toolkit::StyleManager::StyleChangedSignalType& StyleManager::ControlStyleChangeSignal()
+{
+  return mControlStyleChangeSignal;
+}
+
+void StyleManager::SetTheme( const std::string& themeFile )
+{
+  bool themeLoaded = false;
+
+  mThemeBuilder = CreateBuilder( mThemeBuilderConstants );
+
+  // Always load the default theme first, then merge in the custom theme if present
+  themeLoaded = LoadJSON( mThemeBuilder, DEFAULT_THEME );
+
+  if( ! themeFile.empty() )
+  {
+    mThemeFile = themeFile;
+    themeLoaded = LoadJSON( mThemeBuilder, mThemeFile );
+  }
+
+  if( themeLoaded )
+  {
+    if(mFeedbackStyle)
+    {
+      mFeedbackStyle->StyleChanged( mThemeFile, StyleChange::THEME_CHANGE );
+    }
+
+    EmitStyleChangeSignals(StyleChange::THEME_CHANGE);
+  }
+  else
+  {
+    mThemeBuilder.Reset();
+  }
+}
+
 bool StyleManager::LoadFile( const std::string& filename, std::string& stringOut )
 {
   DALI_ASSERT_DEBUG( 0 != filename.length());
@@ -364,41 +269,141 @@ bool StyleManager::LoadFile( const std::string& filename, std::string& stringOut
   return false;
 }
 
-Toolkit::StyleManager::StyleChangeSignalType& StyleManager::StyleChangeSignal()
+Toolkit::Builder StyleManager::CreateBuilder( const Property::Map& constants )
 {
-  return mStyleChangeSignal;
+  Toolkit::Builder builder = Toolkit::Builder::New();
+  builder.AddConstants( constants );
+
+  return builder;
+}
+
+bool StyleManager::LoadJSON( Toolkit::Builder builder, const std::string& jsonFilePath )
+{
+  std::string fileString;
+  if( LoadFile( jsonFilePath, fileString ) )
+  {
+    builder.LoadFromString( fileString );
+    return true;
+  }
+  else
+  {
+    DALI_LOG_WARNING("Error loading file '%s'\n", jsonFilePath.c_str());
+    return false;
+  }
 }
 
-void StyleManager::RequestThemeChange( const std::string& themeFile )
+static void CollectQualifiers( std::vector<std::string>& qualifiersOut )
 {
-  mThemeFile = themeFile;
+  // Append the relevant qualifier for orientation
+  // int orientation = 0; // Get the orientation from the system
+  /*
+  //// To Do /////
+  Getting orientation from the system, and determine Qualifie LANDSCAPE or PORTRAIT
+  orientation  0, 180 : PORTRAIT_QUALIFIER (default)
+  orientation 90, 270 : LANDSCAPE_QUALIFIER
+  */
+
+  qualifiersOut.push_back( std::string( PORTRAIT_QUALIFIER ) );
 
-  // need to do style change synchronously as app might create a UI control on the next line
-  SetTheme();
 }
 
-void StyleManager::RequestDefaultTheme()
+/**
+ * @brief Construct a qualified style name out of qualifiers
+ *
+ * A qualifed style name will be in the format: style-qualifier0-qualifier1-qualifierN
+ *
+ * @param[in] styleName The root name of the style
+ * @param[in] qualifiers List of qualifier names
+ * @param[out] qualifiedStyleOut The qualified style name
+ */
+static void BuildQualifiedStyleName(
+  const std::string& styleName,
+  const std::vector<std::string>& qualifiers,
+  std::string& qualifiedStyleOut )
 {
-  RequestThemeChange( DEFAULT_THEME );
+  qualifiedStyleOut.append( styleName );
+
+  for( std::vector<std::string>::const_iterator it = qualifiers.begin(),
+         itEnd = qualifiers.end(); it != itEnd; ++it )
+  {
+    const std::string& str = *it;
+
+    qualifiedStyleOut.append( "-" );
+    qualifiedStyleOut.append( str );
+  }
 }
 
-void StyleManager::SetTheme()
+static bool GetStyleNameForControl( Toolkit::Builder builder, Toolkit::Control control, std::string& styleName)
 {
-  mThemeBuilder = CreateBuilder( mThemeBuilderConstants );
+  styleName = control.GetStyleName();
 
-  if( LoadJSON( mThemeBuilder, mThemeFile ) )
+  if( styleName.empty() )
   {
-    if(mFeedbackStyle)
+    styleName = control.GetTypeName();
+  }
+
+  // Apply the style after choosing the correct actual style (e.g. landscape or portrait)
+  std::vector<std::string> qualifiers;
+  CollectQualifiers( qualifiers );
+
+  bool found = 0;
+  std::string qualifiedStyleName;
+  do
+  {
+    qualifiedStyleName.clear();
+    BuildQualifiedStyleName( styleName, qualifiers, qualifiedStyleName );
+
+    // Break if style found or we have tried the root style name (qualifiers is empty)
+    if( GetImpl(builder).LookupStyleName( qualifiedStyleName ) )
     {
-      mFeedbackStyle->StyleChanged( mThemeFile, StyleChange::THEME_CHANGE );
+      found = true;
+      break;
+    }
+    if( qualifiers.size() == 0 )
+    {
+      break;
     }
+    // Remove the last qualifier in an attempt to find a style that is valid
+    qualifiers.pop_back();
+  } while (!found);
 
-    mStyleChangeSignal.Emit( Toolkit::StyleManager::Get(), StyleChange::THEME_CHANGE );
+  if(found)
+  {
+    styleName = qualifiedStyleName;
   }
-  else
+  return found;
+}
+
+void StyleManager::ApplyStyle( Toolkit::Builder builder, Toolkit::Control control )
+{
+  std::string styleName = control.GetStyleName();
+  if( GetStyleNameForControl( builder, control, styleName ) )
   {
-    mThemeBuilder.Reset();
+    builder.ApplyStyle( styleName, control );
+  }
+
+  if( mDefaultFontSize >= 0 )
+  {
+    // Apply the style for logical font size
+    std::stringstream fontSizeQualifier;
+    fontSizeQualifier << styleName << FONT_SIZE_QUALIFIER << mDefaultFontSize;
+    builder.ApplyStyle( fontSizeQualifier.str(), control );
+  }
+}
+
+const StylePtr StyleManager::GetRecordedStyle( Toolkit::Control control )
+{
+  if( mThemeBuilder )
+  {
+    std::string styleName = control.GetStyleName();
+
+    if( GetStyleNameForControl( mThemeBuilder, control, styleName ) )
+    {
+      const StylePtr style = GetImpl(mThemeBuilder).GetStyle( styleName );
+      return style;
+    }
   }
+  return StylePtr(NULL);
 }
 
 Toolkit::Builder StyleManager::FindCachedBuilder( const std::string& key )
@@ -435,24 +440,25 @@ void StyleManager::StyleMonitorChange( StyleMonitor styleMonitor, StyleChange::T
 
     case StyleChange::THEME_CHANGE:
     {
-      const std::string& newTheme = styleMonitor.GetTheme();
-      if( ! newTheme.empty() )
-      {
-        mThemeFile = newTheme;
-      }
-      else
-      {
-        mThemeFile = DEFAULT_THEME;
-      }
-
-      SetTheme();
+      SetTheme( styleMonitor.GetTheme() );
       break;
     }
   }
+  EmitStyleChangeSignals( styleChange );
+}
+
+void StyleManager::EmitStyleChangeSignals( StyleChange::Type styleChange )
+{
+  Toolkit::StyleManager styleManager = StyleManager::Get();
+
+  // Update Controls first
+  mControlStyleChangeSignal.Emit( styleManager, styleChange );
 
-  mStyleChangeSignal.Emit( Toolkit::StyleManager::Get(), styleChange );
+  // Inform application last
+  mStyleChangedSignal.Emit( styleManager, styleChange );
 }
 
+
 } // namespace Internal
 
 } // namespace Toolkit