#include "ct_readerresultaddingtools.h" #include "ct_result/model/outModel/tools/ct_outresultmodelgrouptocopypossibilities.h" #include "ct_result/model/outModel/tools/ct_iteratorgroupsmodelforresultmodelgroup.h" #include "ct_result/model/outModel/ct_outresultmodelgroup.h" #include "ct_tools/model/ct_outmodelcopyactionaddreaderresultmodelingroup.h" #include "ct_reader/abstract/ct_abstractreader.h" #include "ct_result/ct_resultgroup.h" #include "ct_model/tools/ct_modelsearchhelper.h" CT_ReaderResultAddingTools::CT_ReaderResultAddingTools() { } CT_ReaderResultAddingTools::~CT_ReaderResultAddingTools() { clear(); } void CT_ReaderResultAddingTools::clear() { QListIterator< QHash > > it(m_links); while(it.hasNext()) { QHash > h = it.next(); QHashIterator > itH(h); while(itH.hasNext()) { itH.next(); qDeleteAll(itH.value().begin(), itH.value().end()); } } m_links.clear(); } bool changeModelOfElement(CT_AbstractItemDrawable *item, const QHash &links) { // search the AutoRename tools created for the model of this group CT_AutoRenameModels *modelCreationAutoRename = links.value(item->model()->uniqueName(), NULL); if(modelCreationAutoRename == NULL) return false; item->setModel(modelCreationAutoRename->completeName()); return true; } bool changeModelOfItems(const QHash &itemsContainers, const QHash &links) { QHashIterator it(itemsContainers); while(it.hasNext()) { it.next(); if(!changeModelOfElement(it.value()->item(), links)) return false; } return true; } bool recursiveChangeModelOfGroups(const QHash &groupsContainers, const QHash &links) { QHashIterator it(groupsContainers); while(it.hasNext()) { it.next(); QList *groups = it.value()->getList(); QListIterator itG(*groups); while(itG.hasNext()) { CT_AbstractItemGroup *group = dynamic_cast(itG.next()); if(!recursiveChangeModelOfGroups(group->groups(), links)) return false; if(!changeModelOfItems(group->items(), links)) return false; if(!changeModelOfElement(group, links)) return false; } } return true; } bool addGroupToParentAndChangeModelOfChildrens(CT_AbstractItemGroup *groupToCompute, CT_StandardItemGroup *parent, const QHash &links) { if(!recursiveChangeModelOfGroups(groupToCompute->groups(), links)) return false; if(!changeModelOfItems(groupToCompute->items(), links)) return false; return parent->addGroup(groupToCompute); } bool CT_ReaderResultAddingTools::addReaderResults(const QString &inParentGroupModelName, const QString &inReaderModelName, CT_OutResultModelGroupToCopyPossibilities *resCpy, int &autoIndexForFindReader) { if(resCpy == NULL) return false; QListIterator it(resCpy->getModels()); while(it.hasNext()) { CT_OutResultModelGroupCopy *model = it.next(); CT_OutResultModelGroupToCopyPossibility *poss = model->outResultModelCopy(); CT_VirtualAbstractStep *step = model->step(); if((step == NULL) || (poss == NULL)) return false; CT_OutModelCopyActionAddReaderResultModelInGroup action(inParentGroupModelName, inReaderModelName); if(!action.execute(step, poss)) return false; QHash > linksByModel = action.takeAutoRenameLinksByGroupModel(); autoIndexForFindReader = m_links.size(); m_links.insert(autoIndexForFindReader, linksByModel); } return true; } bool CT_ReaderResultAddingTools::addReaderResults(const CT_AutoRenameModels &inParentGroupModelAutoRename, CT_AbstractReader *reader, CT_OutResultModelGroupToCopyPossibilities *resCpy, int &autoIndexForFindReader) { if(resCpy == NULL) return false; QListIterator it(resCpy->getModels()); while(it.hasNext()) { CT_OutResultModelGroupCopy *model = it.next(); CT_OutResultModelGroupToCopyPossibility *poss = model->outResultModelCopy(); CT_VirtualAbstractStep *step = model->step(); if((step == NULL) || (poss == NULL)) return false; CT_OutModelCopyActionAddReaderResultModelInGroup action(inParentGroupModelAutoRename, reader); if(!action.execute(step, poss)) return false; QHash > linksByModel = action.takeAutoRenameLinksByGroupModel(); autoIndexForFindReader = m_links.size(); m_links.insert(autoIndexForFindReader, linksByModel); } return true; } bool CT_ReaderResultAddingTools::addReaderResults(const QString &inParentGroupModelName, CT_AbstractReader *reader, CT_OutResultModelGroup *res, CT_VirtualAbstractStep *step, int &autoIndexForFindReader) { if(res == NULL) return false; // on récupère le groupe défini dans "createInResultModelListProtected()" // pour la recherche DEF_CT_AbstractGroupModelIn *inGroupModel = dynamic_cast< DEF_CT_AbstractGroupModelIn* >(PS_MODELS->searchModel(inParentGroupModelName, res, step)); assert(inGroupModel != NULL); QHash > linksByModel; CT_IteratorGroupsModelForResultModelGroup it(res); if(it.recursiveBeginIterateGroups(inGroupModel)) { DEF_CT_AbstractGroupModelOut *outGroupModel; while((outGroupModel = it.recursiveNextGroup()) != NULL) { if(!CT_OutModelCopyActionAddReaderResultModelInGroup::staticAddElementOfReaderToGroup(reader, outGroupModel, linksByModel)) { autoIndexForFindReader = m_links.size(); m_links.insert(autoIndexForFindReader, linksByModel); return false; } } } autoIndexForFindReader = m_links.size(); m_links.insert(autoIndexForFindReader, linksByModel); return true; } bool CT_ReaderResultAddingTools::addReaderResults(CT_ResultGroup *outRes, CT_StandardItemGroup *group, CT_AbstractReader *reader, int &autoIndexForFindReader) { if((autoIndexForFindReader < 0) || (autoIndexForFindReader >= m_links.size())) return false; // get links of this reader sorted by possibility (CT_AbstractModel* is one possiblity) QHash > linksByModel = m_links.at(autoIndexForFindReader); // check if links contains this possibility (the model of the group to add elements) if(!linksByModel.contains(group->model())) return false; // get links for this possibility (now we can link between a model defined in the reader and the new model created with a AutoRename tools) QHash links = linksByModel.value(group->model()); /////// recursive add groups and items /////// // get models of group of this reader QListIterator itGM(reader->outGroupsModel()); while(itGM.hasNext()) { CT_OutStdGroupModel *model = itGM.next(); // search the AutoRename tools created for this model (a group) CT_AutoRenameModels *modelCreationAutoRename = links.value(model->uniqueName(), NULL); if(modelCreationAutoRename == NULL) return false; // take groups of this reader and set it the new model (searched by completeName() of the AutoRename tools) QList groups = reader->takeGroupOfModel(model->uniqueName(), outRes, modelCreationAutoRename->completeName()); QListIterator itG(groups); while(itG.hasNext()) { bool ok = addGroupToParentAndChangeModelOfChildrens(itG.next(), group, links); Q_ASSERT_X(ok, "CT_ReaderResultAddingTools addReaderResults", "Problem to add a group or children of a reader !"); } } /////// add itemdrawables /////// QListIterator it(reader->outItemDrawableModels()); while(it.hasNext()) { CT_OutStdSingularItemModel *model = it.next(); // search the AutoRename tools created for this model (a itemdrawable) CT_AutoRenameModels *modelCreationAutoRename = links.value(model->uniqueName(), NULL); if(modelCreationAutoRename == NULL) return false; // take itemdrawables of this reader and set it the new model (searched by completeName() of the AutoRename tools) QList items = reader->takeItemDrawableOfModel(model->uniqueName(), outRes, modelCreationAutoRename->completeName()); QListIterator itI(items); while(itI.hasNext()) { bool ok = group->addItemDrawable(itI.next()); Q_ASSERT_X(ok, "CT_ReaderResultAddingTools addReaderResults", "Problem to add a item of a reader !"); } } return true; }