Updates for const->constexpr
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / styling / style-manager-impl.cpp
index 7e08cf4..a5a02dd 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2018 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.
 #include "style-manager-impl.h"
 
 // EXTERNAL INCLUDES
-#include <dali/devel-api/adaptor-framework/singleton-service.h>
+#include <dali/devel-api/common/singleton-service.h>
 #include <dali/public-api/object/type-registry.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/devel-api/asset-manager/asset-manager.h>
+#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/public-api/styling/style-manager.h>
 namespace
 {
 
-const char* LANDSCAPE_QUALIFIER = "landscape";
+//const char* LANDSCAPE_QUALIFIER = "landscape";
 const char* PORTRAIT_QUALIFIER  = "portrait";
-const char* FONT_SIZE_QUALIFIER = "FontSize";
+const char* FONT_SIZE_QUALIFIER = "fontsize";
 
-const char* DEFAULT_THEME = DALI_STYLE_DIR "dali-toolkit-default-theme.json";
+const char* DEFAULT_THEME_FILE_NAME = "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/";
+const char* DEFAULT_TOOLKIT_PACKAGE_PATH = "/toolkit/";
+
+#if defined(DEBUG_ENABLED)
+Debug::Filter* gLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_STYLE");
+#endif
 
 } // namespace
 
@@ -103,10 +109,12 @@ Toolkit::StyleManager StyleManager::Get()
 StyleManager::StyleManager()
 : mDefaultFontSize( -1 ),
   mDefaultFontFamily(""),
-  mFeedbackStyle( NULL )
+  mDefaultThemeFilePath(),
+  mFeedbackStyle( nullptr )
 {
   // Add theme builder constants
-  mThemeBuilderConstants[ PACKAGE_PATH_KEY ] = DEFAULT_PACKAGE_PATH;
+  const std::string dataReadOnlyDir = AssetManager::GetDaliDataReadOnlyPath();
+  mThemeBuilderConstants[ PACKAGE_PATH_KEY ] = dataReadOnlyDir + DEFAULT_TOOLKIT_PACKAGE_PATH;
   mThemeBuilderConstants[ APPLICATION_RESOURCE_PATH_KEY ] = Application::GetResourcePath();
 
   mStyleMonitor = StyleMonitor::Get();
@@ -116,6 +124,10 @@ StyleManager::StyleManager()
     mDefaultFontSize = mStyleMonitor.GetDefaultFontSize();
   }
 
+  // Set the full path for the default style theme.
+  const std::string styleDirPath = AssetManager::GetDaliStylePath();
+  mDefaultThemeFilePath = styleDirPath + DEFAULT_THEME_FILE_NAME;
+
   // Sound & haptic style
   mFeedbackStyle = new FeedbackStyle();
 }
@@ -132,8 +144,7 @@ void StyleManager::ApplyTheme( const std::string& themeFile )
 
 void StyleManager::ApplyDefaultTheme()
 {
-  std::string empty;
-  SetTheme( empty );
+  SetTheme(mDefaultThemeFilePath);
 }
 
 const std::string& StyleManager::GetDefaultFontFamily() const
@@ -227,31 +238,80 @@ Toolkit::StyleManager::StyleChangedSignalType& StyleManager::ControlStyleChangeS
 void StyleManager::SetTheme( const std::string& themeFile )
 {
   bool themeLoaded = false;
+  bool loading = false;
 
-  mThemeBuilder = CreateBuilder( mThemeBuilderConstants );
-
-  // Always load the default theme first, then merge in the custom theme if present
-  themeLoaded = LoadJSON( mThemeBuilder, DEFAULT_THEME );
+  // If we haven't loaded a theme, or the stored theme file is empty, or
+  // the previously loaded theme is different to the requested theme,
+  // first reset the builder and load the default theme.
+  if( ! mThemeBuilder || mThemeFile.empty() || mThemeFile.compare( themeFile ) != 0 )
+  {
+    loading = true;
+    mThemeBuilder = CreateBuilder( mThemeBuilderConstants );
+    themeLoaded = LoadJSON( mThemeBuilder, mDefaultThemeFilePath ); // Sets themeLoaded to true if theme exists
+  }
 
-  if( ! themeFile.empty() )
+  if( themeFile.compare(mDefaultThemeFilePath) != 0 )
   {
-    mThemeFile = themeFile;
-    themeLoaded = LoadJSON( mThemeBuilder, mThemeFile );
+    // The theme is different to the default: Merge it
+    loading = true;
+    themeLoaded |= LoadJSON( mThemeBuilder, themeFile );
   }
 
-  if( themeLoaded )
+  if( loading )
   {
-    if(mFeedbackStyle)
+    mThemeFile = themeFile;
+
+    if( themeLoaded )
     {
-      mFeedbackStyle->StyleChanged( mThemeFile, StyleChange::THEME_CHANGE );
+      // We've successfully loaded the theme file
+      if(mFeedbackStyle)
+      {
+        mFeedbackStyle->StyleChanged( mThemeFile, StyleChange::THEME_CHANGE );
+      }
+
+      EmitStyleChangeSignals(StyleChange::THEME_CHANGE);
+    }
+    else
+    {
+      // We tried to load a theme, but it failed. Ensure the builder is reset
+      mThemeBuilder.Reset();
+      mThemeFile.clear();
     }
+  }
+}
+
+const Property::Map StyleManager::GetConfigurations()
+{
+  DALI_LOG_STREAM( gLogFilter, Debug::Concise, "GetConfigurations()\n On entry, mThemeBuilder: " << (bool(mThemeBuilder)?"Created":"Empty") << "  mThemeFile: " << mThemeFile);
 
-    EmitStyleChangeSignals(StyleChange::THEME_CHANGE);
+  Property::Map result;
+  if( mThemeBuilder )
+  {
+    result = mThemeBuilder.GetConfigurations();
   }
   else
   {
-    mThemeBuilder.Reset();
+    DALI_LOG_STREAM( gLogFilter, Debug::Concise, "GetConfigurations()  Loading default theme" );
+
+    bool themeLoaded = false;
+
+    mThemeBuilder = CreateBuilder( mThemeBuilderConstants );
+
+    // Load default theme because this is first try to load stylesheet.
+    themeLoaded = LoadJSON( mThemeBuilder, mDefaultThemeFilePath );
+    mThemeFile = mDefaultThemeFilePath;
+
+    if( themeLoaded )
+    {
+      result = mThemeBuilder.GetConfigurations();
+    }
+    DALI_LOG_STREAM( gLogFilter, Debug::Concise, "  themeLoaded" << (themeLoaded?"success":"failure") );
   }
+
+  DALI_LOG_STREAM( gLogFilter, Debug::Concise, "GetConfigurations()\n On exit, result Count: " << (result.Count() != 0) );
+  DALI_LOG_STREAM( gLogFilter, Debug::Verbose, "          result: " << result );
+
+  return result;
 }
 
 bool StyleManager::LoadFile( const std::string& filename, std::string& stringOut )
@@ -291,33 +351,39 @@ bool StyleManager::LoadJSON( Toolkit::Builder builder, const std::string& jsonFi
   }
 }
 
-void StyleManager::CollectQualifiers( StringList& qualifiersOut )
+static void CollectQualifiers( std::vector<std::string>& qualifiersOut )
 {
   // Append the relevant qualifier for orientation
-  int orientation = 0; // Get the orientation from the system
-  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;
-    }
-  }
+  // 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 ) );
+
 }
 
-void StyleManager::BuildQualifiedStyleName( const std::string& styleName, const StringList& qualifiers, std::string& qualifiedStyleOut )
+/**
+ * @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 )
 {
   qualifiedStyleOut.append( styleName );
 
-  for( StringList::const_iterator it = qualifiers.begin(), itEnd = qualifiers.end(); it != itEnd; ++it )
+  for( std::vector<std::string>::const_iterator it = qualifiers.begin(),
+         itEnd = qualifiers.end(); it != itEnd; ++it )
   {
     const std::string& str = *it;
 
@@ -326,34 +392,53 @@ void StyleManager::BuildQualifiedStyleName( const std::string& styleName, const
   }
 }
 
-void StyleManager::ApplyStyle( Toolkit::Builder builder, Toolkit::Control control )
+static bool GetStyleNameForControl( Toolkit::Builder builder, Toolkit::Control control, std::string& styleName)
 {
-  std::string styleName = control.GetStyleName();
+  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;
+  std::vector<std::string> qualifiers;
   CollectQualifiers( qualifiers );
 
-  while( true )
+  bool found = 0;
+  std::string qualifiedStyleName;
+  do
   {
-    std::string qualifiedStyleName;
+    qualifiedStyleName.clear();
     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 )
+    if( GetImpl(builder).LookupStyleName( qualifiedStyleName ) )
+    {
+      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);
+
+  if(found)
+  {
+    styleName = qualifiedStyleName;
+  }
+  return found;
+}
+
+void StyleManager::ApplyStyle( Toolkit::Builder builder, Toolkit::Control control )
+{
+  std::string styleName = control.GetStyleName();
+  if( GetStyleNameForControl( builder, control, styleName ) )
+  {
+    builder.ApplyStyle( styleName, control );
   }
 
   if( mDefaultFontSize >= 0 )
@@ -365,6 +450,21 @@ void StyleManager::ApplyStyle( Toolkit::Builder builder, Toolkit::Control contro
   }
 }
 
+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 )
 {
   BuilderMap::iterator builderIt = mBuilderCache.find( key );