Prevented json array of numbers appending on merge
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / styling / style-manager-impl.cpp
index 08d61fd..85aa069 100644 (file)
@@ -34,7 +34,7 @@ namespace
 
 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";
 
@@ -100,7 +100,7 @@ Toolkit::StyleManager StyleManager::Get()
 StyleManager::StyleManager()
 : mOrientationDegrees( 0 ),  // Portrait
   mDefaultFontSize( -1 ),
-  mThemeFile( DEFAULT_THEME ),
+  mDefaultFontFamily(""),
   mFeedbackStyle( NULL )
 {
   // Add theme builder constants
@@ -116,7 +116,6 @@ StyleManager::StyleManager()
 
   // Sound & haptic style
   mFeedbackStyle = new FeedbackStyle();
-
 }
 
 StyleManager::~StyleManager()
@@ -131,7 +130,7 @@ void StyleManager::SetOrientationValue( int orientation )
     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();
+    SetTheme( mThemeFile );
   }
 }
 
@@ -160,6 +159,11 @@ Orientation StyleManager::GetOrientation()
   return mOrientation;
 }
 
+std::string StyleManager::GetDefaultFontFamily() const
+{
+  return mDefaultFontFamily;
+}
+
 void StyleManager::SetStyleConstant( const std::string& key, const Property::Value& value )
 {
   mStyleBuilderConstants[ key ] = value;
@@ -177,12 +181,120 @@ bool StyleManager::GetStyleConstant( const std::string& key, Property::Value& va
   return false;
 }
 
-void StyleManager::OnOrientationChanged( Orientation orientation )
+void StyleManager::RequestThemeChange( const std::string& themeFile )
 {
-  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();
+  SetTheme( themeFile );
+}
+
+void StyleManager::RequestDefaultTheme()
+{
+  std::string empty;
+  SetTheme( empty );
+}
+
+void StyleManager::ApplyThemeStyle( Toolkit::Control control )
+{
+  if( !mThemeBuilder )
+  {
+    RequestDefaultTheme();
+  }
+
+  if( mThemeBuilder )
+  {
+    ApplyStyle( mThemeBuilder, control );
+  }
+}
+
+void StyleManager::ApplyThemeStyleAtInit( Toolkit::Control control )
+{
+  ApplyThemeStyle( control );
+
+  if(mFeedbackStyle)
+  {
+    mFeedbackStyle->ObjectCreated( control );
+  }
+}
+
+void StyleManager::ApplyStyle( Toolkit::Control control, const std::string& jsonFileName, const std::string& styleName )
+{
+  bool builderReady = false;
+
+  // First look in the cache
+  Toolkit::Builder builder = FindCachedBuilder( jsonFileName );
+  if( builder )
+  {
+    builderReady = true;
+  }
+  else
+  {
+    // Merge theme and style constants
+    Property::Map constants( mThemeBuilderConstants );
+    constants.Merge( mStyleBuilderConstants );
+
+    // Create it
+    builder = CreateBuilder( constants );
+
+    if( LoadJSON( builder, jsonFileName ) )
+    {
+      CacheBuilder( builder, jsonFileName );
+      builderReady = true;
+    }
+  }
+
+  // Apply the style to the control
+  if( builderReady )
+  {
+    builder.ApplyStyle( styleName, control );
+  }
+}
+
+Toolkit::StyleManager::StyleChangeSignalType& StyleManager::StyleChangeSignal()
+{
+  return mStyleChangeSignal;
+}
+
+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 );
+    }
+
+    mStyleChangeSignal.Emit( Toolkit::StyleManager::Get(), StyleChange::THEME_CHANGE );
+  }
+  else
+  {
+    mThemeBuilder.Reset();
+  }
+}
+
+bool StyleManager::LoadFile( const std::string& filename, std::string& stringOut )
+{
+  DALI_ASSERT_DEBUG( 0 != filename.length());
+
+  // as toolkit is platform agnostic, it cannot load files from filesystem
+  // ask style monitor to load the style sheet
+  if( mStyleMonitor )
+  {
+    return mStyleMonitor.LoadThemeFile( filename, stringOut );
+  }
+
+  return false;
 }
 
 Toolkit::Builder StyleManager::CreateBuilder( const Property::Map& constants )
@@ -283,117 +395,19 @@ void StyleManager::ApplyStyle( Toolkit::Builder builder, Toolkit::Control contro
   {
     // Apply the style for logical font size
     std::stringstream fontSizeQualifier;
-    fontSizeQualifier << styleName << "-" << FONT_SIZE_QUALIFIER << mDefaultFontSize;
+    fontSizeQualifier << styleName << FONT_SIZE_QUALIFIER << mDefaultFontSize;
     builder.ApplyStyle( fontSizeQualifier.str(), control );
   }
 }
 
-void StyleManager::ApplyThemeStyle( Toolkit::Control control )
-{
-  if( !mThemeBuilder )
-  {
-    RequestDefaultTheme();
-  }
-
-  if( mThemeBuilder )
-  {
-    ApplyStyle( mThemeBuilder, control );
-  }
-}
-
-void StyleManager::ApplyThemeStyleAtInit( Toolkit::Control control )
-{
-  ApplyThemeStyle( control );
-
-  if(mFeedbackStyle)
-  {
-    mFeedbackStyle->ObjectCreated( control );
-  }
-}
-
-void StyleManager::ApplyStyle( Toolkit::Control control, const std::string& jsonFileName, const std::string& styleName )
-{
-  bool builderReady = false;
-
-  // First look in the cache
-  Toolkit::Builder builder = FindCachedBuilder( jsonFileName );
-  if( builder )
-  {
-    builderReady = true;
-  }
-  else
-  {
-    // Merge theme and style constants
-    Property::Map constants( mThemeBuilderConstants );
-    constants.Merge( mStyleBuilderConstants );
-
-    // Create it
-    builder = CreateBuilder( constants );
-
-    if( LoadJSON( builder, jsonFileName ) )
-    {
-      CacheBuilder( builder, jsonFileName );
-      builderReady = true;
-    }
-  }
-
-  // Apply the style to the control
-  if( builderReady )
-  {
-    builder.ApplyStyle( styleName, control );
-  }
-}
-
-bool StyleManager::LoadFile( const std::string& filename, std::string& stringOut )
-{
-  DALI_ASSERT_DEBUG( 0 != filename.length());
-
-  // as toolkit is platform agnostic, it cannot load files from filesystem
-  // ask style monitor to load the style sheet
-  if( mStyleMonitor )
-  {
-    return mStyleMonitor.LoadThemeFile( filename, stringOut );
-  }
-
-  return false;
-}
-
-Toolkit::StyleManager::StyleChangeSignalType& StyleManager::StyleChangeSignal()
-{
-  return mStyleChangeSignal;
-}
-
-void StyleManager::RequestThemeChange( const std::string& themeFile )
-{
-  mThemeFile = themeFile;
-
-  // need to do style change synchronously as app might create a UI control on the next line
-  SetTheme();
-}
-
-void StyleManager::RequestDefaultTheme()
+void StyleManager::OnOrientationChanged( Orientation orientation )
 {
-  RequestThemeChange( DEFAULT_THEME );
+  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( mThemeFile );
 }
 
-void StyleManager::SetTheme()
-{
-  mThemeBuilder = CreateBuilder( mThemeBuilderConstants );
-
-  if( LoadJSON( mThemeBuilder, mThemeFile ) )
-  {
-    if(mFeedbackStyle)
-    {
-      mFeedbackStyle->StyleChanged( mThemeFile, StyleChange::THEME_CHANGE );
-    }
-
-    mStyleChangeSignal.Emit( Toolkit::StyleManager::Get(), StyleChange::THEME_CHANGE );
-  }
-  else
-  {
-    mThemeBuilder.Reset();
-  }
-}
 
 Toolkit::Builder StyleManager::FindCachedBuilder( const std::string& key )
 {
@@ -417,6 +431,7 @@ void StyleManager::StyleMonitorChange( StyleMonitor styleMonitor, StyleChange::T
   {
     case StyleChange::DEFAULT_FONT_CHANGE:
     {
+      mDefaultFontFamily = styleMonitor.GetDefaultFontFamily();
       break;
     }
 
@@ -428,17 +443,7 @@ 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;
     }
   }