Esempio n. 1
0
void AXRController::recursiveMatchRulesToDisplayObjects(const HSSRule::p & rule, const std::vector<HSSDisplayObject::p> & scope, HSSContainer::p container, bool applyingInstructions)
{
    HSSInstruction::p instruction = rule->getInstruction();
    if (instruction && applyingInstructions) {
        switch (instruction->getInstructionType()) {
            case HSSNewInstruction:
            {
                std::string elementName = rule->getSelectorChain()->subject()->getElementName();
                unsigned i;
                unsigned argssize = 1;
                HSSParserNode::p argument = instruction->getArgument();
                if(argument){
                    if (argument->isA(HSSParserNodeTypeNumberConstant)) {
                        HSSNumberConstant::p argnum = boost::static_pointer_cast<HSSNumberConstant>(argument);
                        argssize = (int)argnum->getValue();
                    }
                }
                
                for (i=0; i<argssize; i++) {
                    HSSContainer::p newContainer = HSSContainer::p(new HSSContainer());
                    newContainer->setName(elementName);
                    newContainer->setElementName(elementName);;
                    this->add(newContainer);
                    rule->setThisObj(newContainer);
                    newContainer->rulesAdd(rule, (rule->getActiveByDefault() ? HSSRuleStateOn : HSSRuleStateOff ));
                    std_log1("created "+newContainer->getElementName());
                    newContainer->setNeedsRereadRules(true);
                    newContainer->setNeedsSurface(true);
                    newContainer->setDirty(true);
                    unsigned i, size;
                    this->currentContext.push(newContainer);
                    for (i=0, size=rule->childrenSize(); i<size; i++) {
                        const HSSRule::p childRule = rule->childrenGet(i);
                        this->recursiveMatchRulesToDisplayObjects(childRule, newContainer->getChildren(), newContainer, applyingInstructions);
                    }
                    newContainer->setNeedsRereadRules(true);
                    //newContainer->fireEvent(HSSEventTypeLoad);
                    this->currentContext.pop();
                }
                
                break;
            }
                
            case HSSMoveInstruction:
            {
                HSSContainer::p parent = container->getParent();
                if (parent) {
                    std::vector<HSSDisplayObject::p> moveScope = parent->getChildren();
                    this->setSelectorChain(rule->getSelectorChain());
                    std::vector< std::vector<HSSDisplayObject::p> > selection = this->selectHierarchical(moveScope, container);
                    unsigned i, j, k, size, size2, size3;
                    
                    this->currentContext.push(container);
                    //move the children over
                    for (i=0, size=selection.size(); i<size; i++) {
                        std::vector<HSSDisplayObject::p> inner = selection[i];
                        for (j=0, size2=inner.size(); j<size2; j++) {
                            HSSDisplayObject::p theDO = inner[j];
                            if(theDO != container){
                                theDO->removeFromParent();
                                this->add(theDO);
                                rule->setThisObj(theDO);
                                std_log1("moved "+theDO->getElementName());
                                theDO->rulesAdd(rule, (rule->getActiveByDefault() ? HSSRuleStateOn : HSSRuleStateOff ));
                                theDO->setNeedsRereadRules(true);
                                theDO->setNeedsSurface(true);
                                theDO->setDirty(true);
                                
                                if(theDO->isA(HSSObjectTypeContainer)){
                                    HSSContainer::p theContainer = boost::static_pointer_cast<HSSContainer>(theDO);
                                    this->currentContext.push(theContainer);
                                    //assign more rules
                                    for (k=0, size3=rule->childrenSize(); k<size3; k++) {
                                        const HSSRule::p childRule = rule->childrenGet(k);
                                        this->recursiveMatchRulesToDisplayObjects(childRule, theContainer->getChildren(), theContainer, applyingInstructions);
                                    }
                                    this->currentContext.pop();
                                }
                                
                                theDO->setNeedsRereadRules(true);
                                //theDO->fireEvent(HSSEventTypeLoad);
                            }
                            
                        }
                    }
                    this->currentContext.pop();
                }
                
                break;
            }
                
            case HSSDeleteInstruction:
            {
                //select the items to be deleted
                this->setSelectorChain(rule->getSelectorChain());
                std::vector< std::vector<HSSDisplayObject::p> > selection = this->selectHierarchical(scope, container);
                unsigned i, j, size, size2;
                for (i=0, size=selection.size(); i<size; i++) {
                    std::vector<HSSDisplayObject::p> inner = selection[i];
                    for (j=0, size2=inner.size(); j<size2; j++) {
                        inner[j]->removeFromParent();
                    }
                }
                break;
            }
                                
            default:
                break;
        }
        
    } else {
        HSSSelectorChain::p selectorChain = rule->getSelectorChain();
        if(selectorChain){
            this->setSelectorChain(selectorChain);
            
            //if it starts with a combinator, adjust the scope and selector chain
            bool useAdjustedScope = false;
            std::vector<HSSDisplayObject::p> adjustedScope;
            if(this->currentSelectorNode->isA(HSSParserNodeTypeCombinator)){
                useAdjustedScope = true;
                adjustedScope.push_back(container);
                this->currentChain->prepend(HSSSelector::p(new HSSSelector(container->getElementName())));
                this->currentChainSize ++;
                this->currentSelectorNode = this->currentChain->get(0);
            }
            
            //we observe the parent for dom changes
            container->observe(HSSObservablePropertyTreeChange, HSSObservablePropertyValue, rule.get(), new HSSValueChangedCallback<HSSRule>(rule.get(), &HSSRule::treeChanged));
            rule->setObservedTreeChanger(container.get());
            rule->setThisObj(container);
            
            rule->setOriginalScope(scope);
            std::vector< std::vector<HSSDisplayObject::p> > selection = this->selectHierarchical((useAdjustedScope ? adjustedScope : scope), container);
            unsigned i, j, k, size, size2, size3;
            for (i=0, size=selection.size(); i<size; i++) {
                std::vector<HSSDisplayObject::p> inner = selection[i];
                for (j=0, size2=inner.size(); j<size2; j++) {
                    const HSSDisplayObject::p & displayObject = inner[j];
                    std_log1("match "+displayObject->getElementName());
                    displayObject->rulesAdd(rule, (rule->getActiveByDefault() ? HSSRuleStateOn : HSSRuleStateOff ));
                    
                    displayObject->setNeedsRereadRules(true);
                    displayObject->setNeedsSurface(true);
                    displayObject->setDirty(true);
                    
                    //if it is a container it may have children
                    if(displayObject->isA(HSSObjectTypeContainer)){
                        HSSContainer::p selectedContainer = boost::static_pointer_cast<HSSContainer>(displayObject);
                        this->currentContext.push(selectedContainer);
                        for (k=0, size3=rule->childrenSize(); k<size3; k++) {
                            const HSSRule::p childRule = rule->childrenGet(k);
                            this->recursiveMatchRulesToDisplayObjects(childRule, selectedContainer->getChildren(), selectedContainer, applyingInstructions);
                        }
                        this->currentContext.pop();
                    }
                    
                    displayObject->setNeedsRereadRules(true);
                }
            }
        }
    }
}
Esempio n. 2
0
void HSSRequest::fire()
{
    AXRDocument* document = this->getController()->document();

    //if there is no target
    if (this->target->empty())
    {
        document->loadXMLFile(this->src);
    }
    else
    {
        switch (this->mode)
        {
        default:
        {
            AXRController::p controller = AXRController::p(new AXRController(document));
            XMLParser::p xmlParser(new XMLParser(controller.data()));
            HSSParser::p hssParser(new HSSParser(controller.data()));
            AXRBuffer::p baseFile = document->getFile();
            AXRBuffer::p newFile;
            try
            {
                newFile = document->getFile(this->src);
            }
            catch (const AXRError &e)
            {
                e.raise();
            }

            if (newFile)
            {
                bool loadingSuccess = xmlParser->loadFile(newFile);
                if (!loadingSuccess)
                {
                    AXRError("AXRDocument", "Could not load the XML file").raise();
                }
                else
                {
                    HSSContainer::p root = qSharedPointerCast<HSSContainer > (controller->getRoot());

                    if (root)
                    {
                        std::vector<QUrl> loadSheets = controller->loadSheetsGet();
                        for (std::vector<QUrl>::iterator sheetsIt = loadSheets.begin(); sheetsIt != loadSheets.end(); ++sheetsIt)
                        {
                            AXRBuffer::p hssfile;
                            try
                            {
                                hssfile = document->getFile(*sheetsIt);
                            }
                            catch (const AXRError &e)
                            {
                                e.raise();
                                continue;
                            }

                            if (!hssParser->loadFile(hssfile))
                            {
                                AXRError("AXRDocument", "Could not load the HSS file").raise();
                            }
                        }

                        controller->matchRulesToContentTree();

                        HSSSimpleSelection::const_iterator targetIt, childIt;
                        for (targetIt = this->target->begin(); targetIt != this->target->end(); ++targetIt)
                        {
                            const HSSDisplayObject::p & theDO = *targetIt;
                            HSSContainer::p theContainer = HSSContainer::asContainer(theDO);
                            if (theContainer)
                            {
                                theContainer->clear();
                                for (childIt = root->getChildren()->begin(); childIt != root->getChildren()->end(); ++childIt)
                                {
                                    const HSSDisplayObject::p & theChild = *childIt;
                                    theContainer->add(theChild);
                                }
                            }
                        }

                        root->setNeedsRereadRules(true);
                        root->recursiveReadDefinitionObjects();
                        root->handleEvent(HSSEventTypeLoad, NULL);
                        document->setNeedsDisplay(true);
                    }
                }
            }

            //                AXRDocument::tp document = AXRDocument::getInstance();
            //                AXRWrapper * document = document->getWrapper();
            //                AXRBuffer::p baseFile = document->getFile();
            //
            //                bool loadingSuccess = document->loadFile(baseFile->basePath+this->src, this->src);
            //                if(loadingSuccess){
            //                    unsigned i, size;
            //                    for (i=0, size=this->target.size(); i<size; ++i) {
            //                        std_log1("Adding loaded file to target");
            //
            //                        if(this->target[i]->isA(HSSObjectTypeContainer)){
            //                            const HSSContainer::p & theCont = qSharedPointerCast<HSSContainer>(this->target[i]);
            //                            const HSSContainer::p & loadedRoot = fileController.getRoot();
            //                            theCont->add(loadedRoot);
            //
            //                            unsigned j, k, size2, size3;
            //                            HSSSimpleSelection::p scope = theCont->getChildren();
            //                            for (j=0, size2=fileController.rulesSize(); j<size2; ++j) {
            //                                HSSRule::p & theRule = fileController.rulesGet(j);
            //                                theRule->childrenAdd(theRule);
            //                            }
            //                            for (j=0, size2=theCont->rulesSize(); j<size2; ++j) {
            //                                HSSRule::p theRule = theCont->rulesGet(j);
            //                                for (k=0, size3=theRule->childrenSize(); k<size3; ++k) {
            //                                    HSSRule::p childRule = theRule->childrenGet(k);
            //                                    this->getController()->recursiveMatchRulesToDisplayObjects(childRule, scope, theCont);
            //                                }
            //                            }
            //
            //                            theCont->recursiveReadDefinitionObjects();
            //                            theCont->setNeedsLayout(true);
            //                        }
            //                    }
            //                }
            break;
        }
        }
    }
}