src/hbcore/cssparser/hblayeredstyleloader_p.cpp
changeset 30 80e4d18b72f5
parent 28 b7da29130b0e
equal deleted inserted replaced
28:b7da29130b0e 30:80e4d18b72f5
   114 }
   114 }
   115 
   115 
   116 void HbLayeredStyleLoader::init(Concern con)
   116 void HbLayeredStyleLoader::init(Concern con)
   117 {
   117 {
   118     mConcern = con;
   118     mConcern = con;
   119     if (con != Concern_All) {
   119     if (con == Concern_All) {
   120         HbLayeredStyleLoader *allStack = getStack(Concern_All);
   120         mAllStack = 0;
   121         if (allStack) {
   121     } else {
   122             mUsedLayers = allStack->mUsedLayers;
   122         mAllStack = getStack(Concern_All);
       
   123         if (mAllStack) {
       
   124             mUsedLayers = mAllStack->mUsedLayers;
   123         }
   125         }
   124     }
   126     }
   125 #ifdef HB_USETHEMESERVER
   127 #ifdef HB_USETHEMESERVER
   126     mServerHbCssMisses = HbThemeClient::global()->getSharedMissedHbCss();
   128     mServerHbCssMisses = HbThemeClient::global()->getSharedMissedHbCss();
   127 #else
   129 #else
   306 QVector<int> HbLayeredStyleLoader::loadDir(const QString &dirPath, QDir::SortFlags sort,
   308 QVector<int> HbLayeredStyleLoader::loadDir(const QString &dirPath, QDir::SortFlags sort,
   307         LayerPriority priority)
   309         LayerPriority priority)
   308 {
   310 {
   309     QVector<int> fileList;
   311     QVector<int> fileList;
   310 
   312 
       
   313     bool dirPathHasSeparator = dirPath.endsWith(QDir::separator());
       
   314 
   311     QDir dir(dirPath, CSS_FILTER, sort, QDir::Readable | QDir::Files);
   315     QDir dir(dirPath, CSS_FILTER, sort, QDir::Readable | QDir::Files);
   312     QStringList files = dir.entryList();
   316     QStringList files = dir.entryList();
       
   317     fileList.reserve(files.count());
       
   318 
   313     for (QStringList::Iterator it = files.begin(); it != files.end(); ++it) {
   319     for (QStringList::Iterator it = files.begin(); it != files.end(); ++it) {
   314         QString cssPath = dirPath;
   320         QString cssPath = dirPath;
   315         if (!dirPath.endsWith(QDir::separator())) {
   321         if (!dirPathHasSeparator) {
   316             cssPath.append(QDir::separator());
   322             cssPath.append(QDir::separator());
   317         }
   323         }
   318         cssPath.append(*it);
   324         cssPath.append(*it);
   319         int result = load(cssPath, priority);
   325         int result = load(cssPath, priority);
   320         if (result) {
   326         if (result) {
   406         loader->clearLayerFileList(mConcern, priority);
   412         loader->clearLayerFileList(mConcern, priority);
   407     }
   413     }
   408 }
   414 }
   409 
   415 
   410 
   416 
   411 static inline bool qcss_selectorStyleRuleLessThan(
   417 static inline bool qcss_selectorStyleRuleLessThan(const HbCss::WeightedRule &lhs, 
   412     const QPair<int, HbCss::StyleRule> &lhs, const QPair<int, HbCss::StyleRule> &rhs)
   418                                                   const HbCss::WeightedRule &rhs)
   413 {
   419 {
   414     return lhs.first < rhs.first;
   420     return lhs.first < rhs.first;
   415 }
   421 }
   416 
   422 
   417 static inline bool qcss_selectorDeclarationLessThan(
   423 static inline bool qcss_selectorDeclarationLessThan(const HbCss::WeightedDeclaration &lhs, 
   418     const QPair<int, HbCss::Declaration> &lhs, const QPair<int, HbCss::Declaration> &rhs)
   424                                                     const HbCss::WeightedDeclaration &rhs)
   419 {
   425 {
   420     return lhs.first < rhs.first;
   426     return lhs.first < rhs.first;
   421 }
   427 }
   422 
   428 
   423 
   429 
   424 bool HbLayeredStyleLoader::hasOrientationSpecificStyleRules(HbStyleSelector::NodePtr node) const
   430 bool HbLayeredStyleLoader::hasOrientationSpecificStyleRules(HbStyleSelector::NodePtr node) const
   425 {
   431 {
   426     HbWidget* widget = (HbWidget*) node.ptr;
   432     HbWidget* widget = (HbWidget*) node.ptr;
   427     HbWidgetStyleLoader *loader = HbWidgetStyleLoader::instance();
   433     HbWidgetStyleLoader *loader = HbWidgetStyleLoader::instance();
   428     loader->loadCss(widget);
   434     loader->loadCss(widget);
   429     
       
   430     QVector<QVector<HbCss::WeightedRule> > weightedRulesList;
       
   431     HbLayeredStyleLoader *allStack = mConcern == Concern_All ? 0 : getStack(Concern_All);
       
   432 
   435 
   433     QListIterator<LayerPriority> iter(mUsedLayers);
   436     QListIterator<LayerPriority> iter(mUsedLayers);
   434     while (iter.hasNext()) {
   437     while (iter.hasNext()) {
   435         LayerPriority priority = iter.next();
   438         LayerPriority priority = iter.next();
   436         QMap<LayerPriority, Layer>::const_iterator it = mStyleLayers.constFind(priority);
   439         QMap<LayerPriority, Layer>::const_iterator it = mStyleLayers.constFind(priority);
   437         if (it != mStyleLayers.constEnd()) {
   440         if (it != mStyleLayers.constEnd()) {
   438             if (it->styleSelector.hasOrientationSpecificStyleRules(node)) {
   441             if (it->styleSelector.hasOrientationSpecificStyleRules(node)) {
   439                 return true;
   442                 return true;
   440             }
   443             }
   441         }
   444         }
   442         if (allStack) {
   445         if (mAllStack) {
   443             QMap<LayerPriority, Layer>::const_iterator allIt = 
   446             QMap<LayerPriority, Layer>::const_iterator allIt = 
   444                 allStack->mStyleLayers.constFind(priority);
   447                 mAllStack->mStyleLayers.constFind(priority);
   445             if (allIt != allStack->mStyleLayers.constEnd()) {
   448             if (allIt != mAllStack->mStyleLayers.constEnd()) {
   446                 if (allIt->styleSelector.hasOrientationSpecificStyleRules(node)) {
   449                 if (allIt->styleSelector.hasOrientationSpecificStyleRules(node)) {
   447                     return true;
   450                     return true;
   448                 }
   451                 }
   449             }
   452             }
   450         }
   453         }
   465     const char *extraPseudo) const
   468     const char *extraPseudo) const
   466 {
   469 {
   467     HbWidget* widget = (HbWidget*) node.ptr;
   470     HbWidget* widget = (HbWidget*) node.ptr;
   468     HbWidgetStyleLoader *loader = HbWidgetStyleLoader::instance();
   471     HbWidgetStyleLoader *loader = HbWidgetStyleLoader::instance();
   469     loader->loadCss(widget);
   472     loader->loadCss(widget);
   470     
   473 
   471     QVector<QVector<HbCss::WeightedDeclaration> > weightedDeclsList;
   474     QList<HbCss::WeightedDeclaration> weightedDecls;
   472     HbLayeredStyleLoader *allStack = mConcern == Concern_All ? 0 : getStack(Concern_All);
       
   473 
   475 
   474     QListIterator<LayerPriority> iter(mUsedLayers);
   476     QListIterator<LayerPriority> iter(mUsedLayers);
   475     while (iter.hasNext()) {
   477     while (iter.hasNext()) {
   476         LayerPriority priority = iter.next();
   478         LayerPriority priority = iter.next();
   477         QMap<LayerPriority, Layer>::const_iterator it = mStyleLayers.constFind(priority);
   479         QMap<LayerPriority, Layer>::const_iterator it = mStyleLayers.constFind(priority);
   478         if (it != mStyleLayers.constEnd()) {
   480         if (it != mStyleLayers.constEnd()) {
   479             weightedDeclsList.append(
   481             it->styleSelector.weightedDeclarationsForNode(
   480                 it->styleSelector.weightedDeclarationsForNode(node, orientation, extraPseudo));
   482                 node, 
   481         }
   483                 orientation, 
   482         if (allStack) {
   484                 &weightedDecls, 
       
   485                 extraPseudo);
       
   486         }
       
   487         if (mAllStack) {
   483             QMap<LayerPriority, Layer>::const_iterator allIt = 
   488             QMap<LayerPriority, Layer>::const_iterator allIt = 
   484                 allStack->mStyleLayers.constFind(priority);
   489                 mAllStack->mStyleLayers.constFind(priority);
   485             if (allIt != allStack->mStyleLayers.constEnd()) {
   490             if (allIt != mAllStack->mStyleLayers.constEnd()) {
   486                 weightedDeclsList.append(
   491                 allIt->styleSelector.weightedDeclarationsForNode(
   487                     allIt->styleSelector.weightedDeclarationsForNode(
   492                     node, 
   488                         node, 
   493                     orientation, 
   489                         orientation, 
   494                     &weightedDecls,
   490                         extraPseudo));
   495                     extraPseudo);
   491             }
   496             }
   492         }
   497         }
   493     }
   498     }
   494     
   499     
   495     QVector<HbCss::WeightedDeclaration> weightedDecls;
   500     if (weightedDecls.count() > 1) {
   496     int count = 0;
   501         qStableSort(weightedDecls.begin(), weightedDecls.end(), qcss_selectorDeclarationLessThan);
   497     for (int i=0; i<weightedDeclsList.count(); i++) {
   502     }
   498         count += weightedDeclsList.at(i).count();
       
   499     }
       
   500     weightedDecls.reserve(count);
       
   501     for (int i=0; i<weightedDeclsList.count(); i++) {
       
   502         weightedDecls += weightedDeclsList.at(i);
       
   503     }
       
   504     qStableSort(weightedDecls.begin(), weightedDecls.end(), qcss_selectorDeclarationLessThan);
       
   505 
   503 
   506     HbVector<HbCss::Declaration> decls;
   504     HbVector<HbCss::Declaration> decls;
       
   505     decls.reserve(weightedDecls.count());
   507     for (int j = 0; j < weightedDecls.count(); j++)
   506     for (int j = 0; j < weightedDecls.count(); j++)
   508         decls += weightedDecls.at(j).second;
   507         decls += weightedDecls.at(j).second;
   509     return decls;
   508     return decls;
   510 }
   509 }
   511 
   510 
   517 */
   516 */
   518 HbVector<HbCss::StyleRule> HbLayeredStyleLoader::styleRulesForNode(HbStyleSelector::NodePtr node,
   517 HbVector<HbCss::StyleRule> HbLayeredStyleLoader::styleRulesForNode(HbStyleSelector::NodePtr node,
   519         const Qt::Orientation orientation) const
   518         const Qt::Orientation orientation) const
   520 {
   519 {
   521     HbWidget* widget = (HbWidget*) node.ptr;
   520     HbWidget* widget = (HbWidget*) node.ptr;
   522     HbWidgetStyleLoader *loader = HbWidgetStyleLoader::instance();
   521     HbWidgetStyleLoader::instance()->loadCss(widget);
   523     loader->loadCss(widget);
   522 
   524     
   523     QList<HbCss::WeightedRule> weightedRules;
   525     QVector<QVector<HbCss::WeightedRule> > weightedRulesList;
       
   526     HbLayeredStyleLoader *allStack = mConcern == Concern_All ? 0 : getStack(Concern_All);
       
   527 
   524 
   528     QListIterator<LayerPriority> iter(mUsedLayers);
   525     QListIterator<LayerPriority> iter(mUsedLayers);
   529     while (iter.hasNext()) {
   526     while (iter.hasNext()) {
   530         LayerPriority priority = iter.next();
   527         LayerPriority priority = iter.next();
   531         QMap<LayerPriority, Layer>::const_iterator it = mStyleLayers.constFind(priority);
   528         QMap<LayerPriority, Layer>::const_iterator it = mStyleLayers.constFind(priority);
   532         if (it != mStyleLayers.constEnd()) {
   529         if (it != mStyleLayers.constEnd()) {
   533             weightedRulesList.append(
   530             it->styleSelector.weightedStyleRulesForNode(node, orientation, &weightedRules);
   534                 it->styleSelector.weightedStyleRulesForNode(node, orientation));
   531         }
   535         }
   532         if (mAllStack) {
   536         if (allStack) {
       
   537             QMap<LayerPriority, Layer>::const_iterator allIt = 
   533             QMap<LayerPriority, Layer>::const_iterator allIt = 
   538                 allStack->mStyleLayers.constFind(priority);
   534                 mAllStack->mStyleLayers.constFind(priority);
   539             if (allIt != allStack->mStyleLayers.constEnd()) {
   535             if (allIt != mAllStack->mStyleLayers.constEnd()) {
   540                 weightedRulesList.append(
   536                 allIt->styleSelector.weightedStyleRulesForNode(node, orientation, &weightedRules);
   541                     allIt->styleSelector.weightedStyleRulesForNode(node, orientation));
   537             }
   542             }
   538         }
   543         }
   539     }
   544     }
   540     if (weightedRules.count() > 1) {
   545     
   541         qStableSort(weightedRules.begin(), weightedRules.end(), qcss_selectorStyleRuleLessThan);
   546     QVector<HbCss::WeightedRule> weightedRules;
   542     }
   547     int count = 0;
   543 
   548     for (int i=0; i<weightedRulesList.count(); i++) {
       
   549         count += weightedRulesList.at(i).count();
       
   550     }
       
   551     weightedRules.reserve(count);
       
   552     for (int i=0; i<weightedRulesList.count(); i++) {
       
   553         weightedRules += weightedRulesList.at(i);
       
   554     }
       
   555     qStableSort(weightedRules.begin(), weightedRules.end(), qcss_selectorStyleRuleLessThan);
       
   556 
       
   557     HbVector<HbCss::StyleRule> rules;
       
   558 #ifdef LAYEREDSTYLELOADER_DEBUG
   544 #ifdef LAYEREDSTYLELOADER_DEBUG
   559     qDebug() << "Style rules for" << widget->metaObject()->className();
   545     qDebug() << "Style rules for" << widget->metaObject()->className();
   560     qDebug("\n%s", HbCssFormatter::weightedStyleRulesToString(weightedRules).toLatin1().constData());
   546     qDebug("\n%s", HbCssFormatter::weightedStyleRulesToString(weightedRules).toLatin1().constData());
   561 #endif
   547 #endif
   562     rules.reserve(count);
   548 
   563     for (int j = 0; j < weightedRules.count(); j++) {
   549     HbVector<HbCss::StyleRule> rules;
       
   550     rules.reserve(weightedRules.count());
       
   551     for (int j = 0; j < weightedRules.count(); j++)
   564         rules += weightedRules.at(j).second;
   552         rules += weightedRules.at(j).second;
   565     }
       
   566     return rules;
   553     return rules;
   567 }
   554 }
   568 
   555 
   569 
   556 
   570 /*!
   557 /*!
   571      Provides the variable rule sets for the loaded CSS files.
   558      Provides the variable rule sets for the loaded CSS files.
   572 */
   559 */
   573 void HbLayeredStyleLoader::variableRuleSets(QHash<quint32, HbCss::Declaration> *variables) const
   560 void HbLayeredStyleLoader::variableRuleSets(QHash<quint32, HbCss::Declaration> *variables) const
   574 {
   561 {
   575     HbLayeredStyleLoader *allStack = mConcern == Concern_All ? 0 : getStack(Concern_All);
       
   576     
       
   577     QListIterator<LayerPriority> iter(mUsedLayers);
   562     QListIterator<LayerPriority> iter(mUsedLayers);
   578     while (iter.hasNext()) {
   563     while (iter.hasNext()) {
   579         LayerPriority priority = iter.next();
   564         LayerPriority priority = iter.next();
   580         QMap<LayerPriority, Layer>::const_iterator it = mStyleLayers.constFind(priority);
   565         QMap<LayerPriority, Layer>::const_iterator it = mStyleLayers.constFind(priority);
   581         if (it != mStyleLayers.constEnd()) {
   566         if (it != mStyleLayers.constEnd()) {
   591                 if (!mDefaultVariables.count()) {
   576                 if (!mDefaultVariables.count()) {
   592                     it->styleSelector.variableRuleSets(&mDefaultVariables);
   577                     it->styleSelector.variableRuleSets(&mDefaultVariables);
   593                 }
   578                 }
   594             }
   579             }
   595         }
   580         }
   596         if (allStack) {
   581         if (mAllStack) {
   597             QMap<LayerPriority, Layer>::const_iterator allIt = 
   582             QMap<LayerPriority, Layer>::const_iterator allIt = 
   598                 allStack->mStyleLayers.constFind(priority);
   583                 mAllStack->mStyleLayers.constFind(priority);
   599             if (allIt != allStack->mStyleLayers.constEnd()) {
   584             if (allIt != mAllStack->mStyleLayers.constEnd()) {
   600                 allIt->styleSelector.variableRuleSets(variables);
   585                 allIt->styleSelector.variableRuleSets(variables);
   601             }
   586             }
   602         }
   587         }
   603     }
   588     }
   604 }
   589 }