#ifndef CT_ITEMDRAWABLECOLLECTIONBUILDERT_HPP #define CT_ITEMDRAWABLECOLLECTIONBUILDERT_HPP #include "ct_tools/itemdrawable/ct_itemdrawablecollectionbuildert.h" #include "ct_result/tools/iterator/ct_resultgroupiterator.h" #include "ct_result/tools/iterator/ct_resultitemiterator.h" #include "ct_result/model/outModel/abstract/ct_outabstractresultmodel.h" #include "ct_itemdrawable/model/outModel/abstract/ct_outabstractgroupmodel.h" #include "ct_result/ct_resultgroup.h" template CT_ItemDrawableCollectionBuilderT::CT_ItemDrawableCollectionBuilderT() { } template bool CT_ItemDrawableCollectionBuilderT::buildFrom(const CT_VirtualAbstractStep *step, bool forceSearchModels) { m_collection.clear(); m_onlyFindModels = forceSearchModels; if(step == NULL) return false; recursiveBuildFromStep(step, forceSearchModels); return true; } template template void CT_ItemDrawableCollectionBuilderT::onlyKeepsTheseTypesInCollection4() { QMutableListIterator it(m_collection); while(it.hasNext()) { CT_ItemDrawableCollectionHierarchyStep &pas = it.next(); QMutableListIterator itR(pas.results); while(itR.hasNext()) { CT_ItemDrawableCollectionHierarchyResult &par = itR.next(); if(m_onlyFindModels) { QMutableListIterator itA(par.modelsCollection); while(itA.hasNext()) { CT_OutAbstractItemModel *model = itA.next(); if((dynamic_cast(model->itemDrawable()) == NULL) && (dynamic_cast(model->itemDrawable()) == NULL) && (dynamic_cast(model->itemDrawable()) == NULL) && (dynamic_cast(model->itemDrawable()) == NULL)) { itA.remove(); } } QList lTmp; itA = lTmp; if(par.modelsCollection.isEmpty()) itR.remove(); } else { QMutableListIterator itA(par.collection); while(itA.hasNext()) { CT_AbstractSingularItemDrawable *item = itA.next(); if((dynamic_cast(item) == NULL) && (dynamic_cast(item) == NULL) && (dynamic_cast(item) == NULL) && (dynamic_cast(item) == NULL)) { itA.remove(); } } QList lTmp; itA = lTmp; if(par.collection.isEmpty()) itR.remove(); } } QList< CT_ItemDrawableCollectionHierarchyResult > lTmp; itR = lTmp; if(pas.results.isEmpty()) it.remove(); } } template const QList& CT_ItemDrawableCollectionBuilderT::collection() const { return m_collection; } template void CT_ItemDrawableCollectionBuilderT::recursiveBuildFromStep(const CT_VirtualAbstractStep *step, bool findOnlyModels) { CT_ItemDrawableCollectionHierarchyStep hi; hi.step = (CT_VirtualAbstractStep*)step; if(findOnlyModels) { QList< QList > results = step->getAllOutResultModels(); QListIterator< QList > it(results); while(it.hasNext()) { QList subList = it.next(); foreach (CT_OutAbstractResultModelGroup *r, subList) { CT_ItemDrawableCollectionHierarchyResult hir; hir.modelResult = (CT_OutAbstractResultModel*)r; recursiveBuildFromModels(QList() << (CT_OutAbstractModel*)r, hir, findOnlyModels); if(!hir.modelsCollection.isEmpty()) hi.results.append(hir); } } } else { QList results = step->getResults(); QListIterator it(results); while(it.hasNext()) { CT_ItemDrawableCollectionHierarchyResult hir; hir.result = it.next(); recursiveBuildFromModels(QList() << ((CT_OutAbstractModel*)hir.result->model()->rootModel()), hir, findOnlyModels); if(!hir.collection.isEmpty()) hi.results.append(hir); } } if(!hi.results.empty()) m_collection.append(hi); QList steps = step->getStepChildList(); while(!steps.isEmpty()) recursiveBuildFromStep(dynamic_cast(steps.takeFirst()), findOnlyModels); } template void CT_ItemDrawableCollectionBuilderT::recursiveBuildFromModels(QList models, CT_ItemDrawableCollectionHierarchyResult &hir, bool findOnlyModels) { QListIterator itM(models); while(itM.hasNext()) { CT_OutAbstractModel *model = itM.next(); if((model->result() != NULL) || findOnlyModels) { CT_AbstractItemDrawable *item = NULL; const CT_OutAbstractSingularItemModel *itemModel = dynamic_cast(model); const CT_OutAbstractGroupModel *grModel = dynamic_cast(model); if(itemModel != NULL) item = itemModel->itemDrawable(); else if(grModel != NULL) item = grModel->group(); if(item != NULL) { if((dynamic_cast(item) != NULL) || (dynamic_cast(item) != NULL) || (dynamic_cast(item) != NULL) || (dynamic_cast(item) != NULL)) { if(grModel != NULL) { if(findOnlyModels) { hir.modelsCollection.append((CT_OutAbstractGroupModel*)grModel); } else { CT_ResultGroupIterator it((CT_ResultGroup*)model->result(), grModel); while(it.hasNext()) hir.collection.append((CT_AbstractSingularItemDrawable*)it.next()); } } else { if(findOnlyModels) { hir.modelsCollection.append((CT_OutAbstractSingularItemModel*)itemModel); } else { CT_ResultItemIterator it((CT_ResultGroup*)model->result(), itemModel); while(it.hasNext()) hir.collection.append((CT_AbstractSingularItemDrawable*)it.next()); } } } } // TODO if it's an attribute !! recursiveBuildFromModels(model->childrensStaticCast(), hir, findOnlyModels); } } } #endif // CT_ITEMDRAWABLECOLLECTIONBUILDERT_HPP