#ifndef CT_ABSTRACTITEMDRAWABLEMODELT_HPP #define CT_ABSTRACTITEMDRAWABLEMODELT_HPP #include "ct_itemdrawable/model/abstract/ct_abstractsingularitemmodelt.h" template CT_AbstractSingularItemModelT::CT_AbstractSingularItemModelT(const QString &uniqueName, const QString &description, const QString &displayableName) : InheritedT(uniqueName, description, displayableName) { } template CT_AbstractSingularItemModelT::~CT_AbstractSingularItemModelT() { clearItemAttributes(); } template bool CT_AbstractSingularItemModelT::addItemAttribute(AttributModelT *attribute) { if(this->internalExistModelOrChildrensInTree(this->rootModel(), attribute) || attribute->uniqueName().isEmpty()) { delete attribute; return false; } internalAddItemAttribute(attribute); return true; } template bool CT_AbstractSingularItemModelT::addItemAttribute(AttributModelT *attribute, CT_AutoRenameModels &autoName) { CT_AbstractModel *root = this->rootModel(); // si on a déjà attribué un nouveau nom à un attribut précédent if(!autoName.models().isEmpty()) { // si l'ancien modèle n'est pas un attribut if(dynamic_cast(autoName.models().first()) == NULL) { delete attribute; // Il faut un autre CT_AutoRenameModels pour pouvoir ajouter cet attribut return false; } // on attribue le nom déjà défini au nouvel attribut attribute->setUniqueName(autoName.completeName()); // on vérifie qu'il n'existe pas déjà dans notre arborescence if(!this->internalExistModelOrChildrensInTree(root, attribute)) { // si c'est bon on l'ajoute internalAddItemAttribute(attribute); autoName.addModel(attribute); return true; } } else // sinon { // on met un nom par défaut autoName.setName("i"); autoName.setExtra(0); attribute->setUniqueName(autoName.completeName()); } // tant que l'attribut existe avec ce nom dans l'arborescence // ou tant qu'on ne peut pas renommer les anciens attributs avec // ce nouveau nom while(this->internalExistModelOrChildrensInTree(root, attribute) || !autoName.renameAllModels()) { // on renomme le nouvel attribut autoName.setExtra(autoName.extra()+1); attribute->setUniqueName(autoName.completeName()); } // on ajoute ce modele à l'AutoRename autoName.addModel(attribute); // on ajoute ce modele à notre liste internalAddItemAttribute(attribute); return true; } template bool CT_AbstractSingularItemModelT::removeItemAttribute(AttributModelT *attribute) { if(m_attributes.removeOne(attribute)) { itemAttributeRemoved(attribute); delete attribute; return true; } return false; } template const QList& CT_AbstractSingularItemModelT::itemAttributes() const { return m_attributes; } template AttributModelT* CT_AbstractSingularItemModelT::findItemAttribute(const QString &uniqueModelName) const { QListIterator it(itemAttributes()); while(it.hasNext()) { AttributModelT *att = it.next(); if(att->uniqueName() == uniqueModelName) return att; } return NULL; } template void CT_AbstractSingularItemModelT::clearItemAttributes() { while(!m_attributes.isEmpty()) { AttributModelT *attribute = m_attributes.takeFirst(); itemAttributeRemoved(attribute); delete attribute; } } template GroupModelT* CT_AbstractSingularItemModelT::parentGroup() const { return (GroupModelT*)this->parentModel(); } template QList CT_AbstractSingularItemModelT::childrens() const { QList l; QListIterator it(itemAttributes()); while(it.hasNext()) l.append(it.next()); return l; } template bool CT_AbstractSingularItemModelT::isEmpty() const { return m_attributes.isEmpty(); } template void CT_AbstractSingularItemModelT::internalAddItemAttribute(AttributModelT *attr) { this->staticSetParentModelToModel(attr, this); m_attributes.append(attr); itemAttributeAdded(attr); } template GroupModelT* CT_AbstractSingularItemModelT::rootGroup() const { GroupModelT *parent = parentGroup(); if(parent == NULL) return NULL; return dynamic_cast< GroupModelT* >(parent->rootGroup()); } #endif // CT_ABSTRACTITEMDRAWABLEMODELT_HPP