src/hbcore/cssparser/hbcssparser_p.cpp
changeset 28 b7da29130b0e
parent 21 4633027730f5
child 30 80e4d18b72f5
equal deleted inserted replaced
23:e6ad4ef83b23 28:b7da29130b0e
    42 #include "hbcssscanner_p.cpp"
    42 #include "hbcssscanner_p.cpp"
    43 #include "hbmemoryutils_p.h"
    43 #include "hbmemoryutils_p.h"
    44 #include "hblayeredstyleloader_p.h"
    44 #include "hblayeredstyleloader_p.h"
    45 #include "hbthemeindex_p.h"
    45 #include "hbthemeindex_p.h"
    46 #include "hblayoutparameters_p.h"
    46 #include "hblayoutparameters_p.h"
       
    47 #include "hbhash_p.h"
    47 
    48 
    48 using namespace HbCss;
    49 using namespace HbCss;
    49 
    50 
    50 const QString GLOBAL_CSS_SELECTOR = "*";
    51 const QString GLOBAL_CSS_SELECTOR = "*";
    51 
    52 
   179 }
   180 }
   180 
   181 
   181 #ifndef HB_BIN_CSS
   182 #ifndef HB_BIN_CSS
   182 ///////////////////////////////////////////////////////////////////////////////
   183 ///////////////////////////////////////////////////////////////////////////////
   183 // Value Extractor
   184 // Value Extractor
   184 ValueExtractor::ValueExtractor(const HbVector<Declaration> &decls, const HbDeviceProfile &profile)
       
   185 : declarations(decls), layoutParameters(0), currentProfile(profile)
       
   186 {
       
   187 }
       
   188 ValueExtractor::ValueExtractor(
   185 ValueExtractor::ValueExtractor(
   189     const HbVector<Declaration> &decls, 
   186     const HbVector<Declaration> &decls,
   190     const HbLayoutParameters &layoutParameters,
       
   191     const HbDeviceProfile &profile)
   187     const HbDeviceProfile &profile)
   192 : declarations(decls), layoutParameters(&layoutParameters), currentProfile(profile)
   188 : declarations(decls), layoutParameters(0), variables(0), currentProfile(profile)
   193 {
   189 {
   194 }
   190 }
   195 
   191 
   196 ValueExtractor::ValueExtractor(
   192 ValueExtractor::ValueExtractor( const HbDeviceProfile &profile)
   197     const HbVector<Declaration> &varDecls, 
   193 : layoutParameters(0), variables(0), currentProfile(profile)
   198     bool isVariable, 
   194 {
   199     const HbDeviceProfile &profile)
       
   200 : variableDeclarations(varDecls), layoutParameters(0), currentProfile(profile)
       
   201 {
       
   202     Q_UNUSED(isVariable)
       
   203     // Initialize to some profile.
       
   204     if ( currentProfile.isNull() ) {
       
   205         currentProfile = HbDeviceProfile::current();
       
   206     }
       
   207 }
       
   208 
       
   209 ValueExtractor::ValueExtractor(
       
   210     const HbLayoutParameters &layoutParameters,
       
   211     bool isVariable, 
       
   212     const HbDeviceProfile &profile)
       
   213 : layoutParameters(&layoutParameters), currentProfile(profile)
       
   214 {
       
   215     Q_UNUSED(isVariable)
       
   216     // Initialize to some profile.
   195     // Initialize to some profile.
   217     if ( currentProfile.isNull() ) {
   196     if ( currentProfile.isNull() ) {
   218         currentProfile = HbDeviceProfile::current();
   197         currentProfile = HbDeviceProfile::current();
   219     }
   198     }
   220 }
   199 }
   233         return currentProfile.ppmValue() * v.variant.toDouble();
   212         return currentProfile.ppmValue() * v.variant.toDouble();
   234     } else if (v.type == Value::Percentage) {
   213     } else if (v.type == Value::Percentage) {
   235         return v.variant.toDouble() / 100.0;
   214         return v.variant.toDouble() / 100.0;
   236     } else if (v.type == Value::Number) {
   215     } else if (v.type == Value::Number) {
   237         return v.variant.toDouble();
   216         return v.variant.toDouble();
       
   217     } else if (v.type == Value::Variable || v.type == Value::VariableNegative) {
       
   218         qreal factor = (v.type == Value::Variable) ? 1.0 : -1.0;
       
   219         qreal variableValue(0.0);
       
   220         bool tempOk = extractVariableValue((quint32)v.variant.toInt(), variableValue);
       
   221         if (ok) {
       
   222             *ok = tempOk;
       
   223         }
       
   224         return factor * variableValue;
   238     }
   225     }
   239 
   226 
   240     QString s = v.variant.toString();
   227     QString s = v.variant.toString();
   241     s.reserve(s.length());
   228     s.reserve(s.length());
   242 
   229 
   795 
   782 
   796     return rgb ? QColor::fromRgb(v1, v2, v3, alpha)
   783     return rgb ? QColor::fromRgb(v1, v2, v3, alpha)
   797                : QColor::fromHsv(v1, v2, v3, alpha);
   784                : QColor::fromHsv(v1, v2, v3, alpha);
   798 }
   785 }
   799 
   786 
   800 bool ValueExtractor::extractVariableValue(const QString& variableName, qreal& realValue)
   787 bool ValueExtractor::extractVariableValue(quint32 hashValue, HbCss::Value &value) const
   801 {
       
   802     bool variableFound = false;
       
   803     HbCss::Value value;
       
   804     if (extractVariableValue(variableName, value)) {
       
   805         realValue = asReal(value);
       
   806         variableFound = true;
       
   807     }
       
   808     return variableFound;    
       
   809 }
       
   810 
       
   811 bool ValueExtractor::extractVariableValue( const QString& variableName, HbCss::Value &val ) const
       
   812 {
   788 {
   813     bool variableFound = false;
   789     bool variableFound = false;
   814     if (layoutParameters && !layoutParameters->isEmpty()) {
   790     if (layoutParameters && !layoutParameters->isEmpty()) {
   815         HbLayoutParameters::const_iterator i = layoutParameters->find(variableName);
   791         HbLayoutParameters::const_iterator i = layoutParameters->find(hashValue);
   816         if (i != layoutParameters->end()) {
   792         if (i != layoutParameters->end()) {
   817             val = layoutParameters->value(i);
   793             value = layoutParameters->value(i);
   818             variableFound = true;
   794             variableFound = true;
   819         }
   795         }
   820     } else if (!variableDeclarationsHash.isEmpty()) {
   796     } else if (variables && !variables->isEmpty()) {
   821         QHash<QString, HbCss::Declaration>::const_iterator f = variableDeclarationsHash.find(variableName);
   797         QHash<quint32, HbCss::Declaration>::const_iterator f = variables->find(hashValue);
   822         if (f != variableDeclarationsHash.end() && !f.value().values.isEmpty()) {
   798         if (f != variables->end() && !f.value().values.isEmpty()) {
   823             val = f.value().values.first();
   799             value = f.value().values.first();
   824             variableFound = true;
   800             variableFound = true;
   825         }
   801         }
   826     } else {
       
   827         const int variableCount = variableDeclarations.count();
       
   828         for (int i=variableCount-1; i>=0; i--) {
       
   829             if (variableDeclarations.at(i).property == variableName ) {
       
   830                 val = variableDeclarations.at(i).values.first();
       
   831                 variableFound = true;
       
   832                 break;
       
   833             }
       
   834         }
       
   835     }
       
   836 
       
   837     //for variable cascading support
       
   838     if ( variableFound ) {
       
   839         if ( val.type == Value::Variable ){
       
   840             variableFound = extractVariableValue(val.variant.toString(), val);
       
   841         }
       
   842     }
   802     }
   843     return variableFound;
   803     return variableFound;
       
   804 }
       
   805 
       
   806 
       
   807 bool ValueExtractor::extractVariableValue(quint32 hashValue, qreal& value)
       
   808 {
       
   809     bool variableFound = false;
       
   810     HbCss::Value v;
       
   811     if (extractVariableValue(hashValue, v)) {
       
   812         value = asReal(v);
       
   813         variableFound = true;
       
   814     }
       
   815     return variableFound;    
       
   816 }
       
   817 
       
   818 bool ValueExtractor::extractVariableValue(const QString& variableName, HbCss::Value &value) const
       
   819 {
       
   820     return extractVariableValue(hbHash(variableName), value);
       
   821 }
       
   822 
       
   823 bool ValueExtractor::extractVariableValue(const QString& variableName, qreal& value)
       
   824 {
       
   825     return extractVariableValue(hbHash(variableName), value);
   844 }
   826 }
   845 
   827 
   846 bool ValueExtractor::extractExpressionValue(QString &expression, qreal &value)
   828 bool ValueExtractor::extractExpressionValue(QString &expression, qreal &value)
   847 {
   829 {
   848     // todo: invalid global variables are not checked because asReal() doesn't check them
   830     // todo: invalid global variables are not checked because asReal() doesn't check them
  1134         switch(decl.propertyId) {
  1116         switch(decl.propertyId) {
  1135         case Property_Color:
  1117         case Property_Color:
  1136             {
  1118             {
  1137             HbCss::Value value;
  1119             HbCss::Value value;
  1138             if ( decl.values.at(0).type == Value::Variable ) {
  1120             if ( decl.values.at(0).type == Value::Variable ) {
  1139                 const QString variableName = decl.values.at(0).variant.toString();
  1121                 quint32 hashValue = (quint32)decl.values.at(0).variant.toInt();
  1140                 HbThemeIndexResource resource(variableName);
  1122                 HbThemeIndexResource resource(hashValue);
  1141                 if (resource.isValid()) {
  1123                 if (resource.isValid()) {
  1142                     // Color value coming from index
  1124                     // Color value coming from index
  1143                     color = resource.colorValue();
  1125                     color = resource.colorValue();
  1144                 } else {
  1126                 } else {
  1145                     // Color value coming from custom css
  1127                     // Color value coming from custom css
  1146                     extractVariableValue( variableName, value );
  1128                     extractVariableValue( hashValue, value );
  1147                     color = parseColorValue(value);
  1129                     color = parseColorValue(value);
  1148                 }
  1130                 }
  1149             } else {
  1131             } else {
  1150                 value = decl.values.at(0);
  1132                 value = decl.values.at(0);
  1151                 color = parseColorValue(value);
  1133                 color = parseColorValue(value);
  1559         decls += weightedDecls.at(j).second;
  1541         decls += weightedDecls.at(j).second;
  1560 
  1542 
  1561     return decls;
  1543     return decls;
  1562 }
  1544 }
  1563 
  1545 
  1564 void StyleSelector::variableRuleSets(QHash<QString, HbCss::Declaration> *variables) const 
  1546 void StyleSelector::variableRuleSets(QHash<quint32, HbCss::Declaration> *variables) const 
  1565 {
  1547 {
  1566     HbVector<Declaration> decls;
  1548     HbVector<Declaration> decls;
  1567     const int styleSheetsCount = styleSheets.count();
  1549     const int styleSheetsCount = styleSheets.count();
  1568     for (int i=0; i<styleSheetsCount; i++) {
  1550     for (int i=0; i<styleSheetsCount; i++) {
  1569         const StyleSheet *styleSheet = styleSheets.at(i);
  1551         const StyleSheet *styleSheet = styleSheets.at(i);
  1570         const int variableRuleCount = styleSheet->variableRules.count();
  1552         const int variableRuleCount = styleSheet->variableRules.count();
  1571         for (int j=0; j<variableRuleCount; j++) {
  1553         for (int j=0; j<variableRuleCount; j++) {
  1572             decls = styleSheet->variableRules.at(j).declarations;
  1554             decls = styleSheet->variableRules.at(j).declarations;
  1573             const int declsCount = decls.count();
  1555             const int declsCount = decls.count();
  1574             for (int k=0; k<declsCount; k++) {
  1556             for (int k=0; k<declsCount; k++) {
  1575                 variables->insert(decls.at(k).property, decls.at(k));
  1557                 variables->insert(hbHash(decls.at(k).property), decls.at(k));
  1576             }
  1558             }
  1577         }
  1559         }
  1578     }
  1560     }
  1579 }
  1561 }
  1580 
  1562 
  2402                     value->variant = args;
  2384                     value->variant = args;
  2403                 }
  2385                 }
  2404                 //changes for variable support
  2386                 //changes for variable support
  2405                 else if (name == QLatin1String("var")) {
  2387                 else if (name == QLatin1String("var")) {
  2406                     value->type = Value::Variable;
  2388                     value->type = Value::Variable;
  2407                     value->variant = args;
  2389                     value->variant = (int)hbHash( args );
  2408                 } else if (name == QLatin1String("-var")) {                    
  2390                 } else if (name == QLatin1String("-var")) {                    
  2409                     value->type = Value::VariableNegative;
  2391                     value->type = Value::VariableNegative;
  2410                     value->variant = args;
  2392                     value->variant = (int)hbHash( args );
  2411                 } //change end
  2393                 } //change end
  2412                 //changes for expression support
  2394                 //changes for expression support
  2413                 else if (name == QLatin1String("expr")) {
  2395                 else if (name == QLatin1String("expr")) {
  2414                     value->type = Value::Expression;
  2396                     value->type = Value::Expression;
  2415                     value->variant = args;
  2397                     value->variant = args;