Changeset View
Changeset View
Standalone View
Standalone View
step/worldmodel.cc
Show First 20 Lines • Show All 118 Lines • ▼ Show 20 Line(s) | 104 | { | |||
---|---|---|---|---|---|
119 | return true; | 119 | return true; | ||
120 | } | 120 | } | ||
121 | 121 | | |||
122 | class CommandNewItem: public QUndoCommand | 122 | class CommandNewItem: public QUndoCommand | ||
123 | { | 123 | { | ||
124 | public: | 124 | public: | ||
125 | CommandNewItem(WorldModel* worldModel, StepCore::Item* item, StepCore::ItemGroup* parent, bool create) | 125 | CommandNewItem(WorldModel* worldModel, StepCore::Item* item, StepCore::ItemGroup* parent, bool create) | ||
126 | : _worldModel(worldModel), _item(item), _parent(parent), _create(create), _shouldDelete(create) { | 126 | : _worldModel(worldModel), _item(item), _parent(parent), _create(create), _shouldDelete(create) { | ||
127 | if(!create) findLinks(static_cast<StepCore::ItemGroup*>(_worldModel->world())); | 127 | if(!create) findLinks(item, static_cast<StepCore::ItemGroup*>(_worldModel->world())); | ||
128 | } | 128 | } | ||
129 | ~CommandNewItem() { if(_shouldDelete) delete _item; } | 129 | ~CommandNewItem() { if(_shouldDelete) delete _item; } | ||
130 | 130 | | |||
131 | void redo() Q_DECL_OVERRIDE; | 131 | void redo() Q_DECL_OVERRIDE; | ||
132 | void undo() Q_DECL_OVERRIDE; | 132 | void undo() Q_DECL_OVERRIDE; | ||
133 | 133 | | |||
134 | protected: | 134 | protected: | ||
135 | void findLinks(StepCore::ItemGroup* group); | 135 | void findLinks(StepCore::Item* itemToMatch, StepCore::ItemGroup* groupToSearch); | ||
136 | void removeItem(); | 136 | void removeItem(); | ||
137 | void readdItem(); | 137 | void readdItem(); | ||
138 | 138 | | |||
139 | WorldModel* _worldModel; | 139 | WorldModel* _worldModel; | ||
140 | StepCore::Item* _item; | 140 | StepCore::Item* _item; | ||
141 | StepCore::ItemGroup* _parent; | 141 | StepCore::ItemGroup* _parent; | ||
142 | bool _create; | 142 | bool _create; | ||
143 | bool _shouldDelete; | 143 | bool _shouldDelete; | ||
144 | 144 | | |||
145 | typedef QPair<StepCore::Object*, const StepCore::MetaProperty*> Link; | 145 | typedef QPair<StepCore::Object*, const StepCore::MetaProperty*> Link; | ||
146 | QList<Link> _links; | 146 | QList<Link> _links; | ||
147 | }; | 147 | }; | ||
148 | 148 | | |||
149 | void CommandNewItem::findLinks(StepCore::ItemGroup* group) | 149 | void CommandNewItem::findLinks(StepCore::Item* itemToMatch, StepCore::ItemGroup* groupToSearch) | ||
150 | { | 150 | { | ||
151 | StepCore::ItemList::const_iterator end = group->items().end(); | 151 | StepCore::ItemGroup* itemToDeleteGroup = dynamic_cast<StepCore::ItemGroup*>(_item); | ||
152 | for(StepCore::ItemList::const_iterator it = group->items().begin(); it != end; ++it) { | 152 | | ||
153 | const StepCore::MetaObject* mobj = (*it)->metaObject(); | 153 | for (StepCore::Item *item : groupToSearch->items()) { | ||
154 | if (item == itemToMatch) // no need to find links in itself | ||||
155 | continue; | ||||
156 | if (itemToDeleteGroup && itemToDeleteGroup->contains(item)) // no need to find links if the item is a child of the item that is being deleted | ||||
157 | continue; | ||||
158 | const StepCore::MetaObject* mobj = item->metaObject(); | ||||
154 | for(int i=0; i<mobj->propertyCount(); ++i) { | 159 | for(int i=0; i<mobj->propertyCount(); ++i) { | ||
155 | if(mobj->property(i)->userTypeId() != qMetaTypeId<StepCore::Object*>()) continue; | 160 | if(mobj->property(i)->userTypeId() != qMetaTypeId<StepCore::Object*>()) continue; | ||
156 | if(_item == mobj->property(i)->readVariant(*it).value<StepCore::Object*>()) | 161 | if(itemToMatch == mobj->property(i)->readVariant(item).value<StepCore::Object*>()) | ||
157 | _links << qMakePair(static_cast<StepCore::Object*>(*it), mobj->property(i)); | 162 | _links << qMakePair(static_cast<StepCore::Object*>(item), mobj->property(i)); | ||
158 | } | 163 | } | ||
159 | if(mobj->inherits<StepCore::ItemGroup>()) | 164 | if(mobj->inherits<StepCore::ItemGroup>()) | ||
160 | findLinks(static_cast<StepCore::ItemGroup*>(*it)); | 165 | findLinks(itemToMatch, static_cast<StepCore::ItemGroup*>(item)); | ||
166 | } | ||||
167 | | ||||
168 | // If the item being deleted is a group, be sure we also find links for all its children | ||||
169 | StepCore::ItemGroup* itemToMatchGroup = dynamic_cast<StepCore::ItemGroup*>(itemToMatch); | ||||
170 | if (itemToMatchGroup) { | ||||
171 | for (StepCore::Item *child : itemToMatchGroup->items()) { | ||||
172 | findLinks(child, groupToSearch); | ||||
173 | } | ||||
161 | } | 174 | } | ||
162 | } | 175 | } | ||
163 | 176 | | |||
164 | void CommandNewItem::removeItem() | 177 | void CommandNewItem::removeItem() | ||
165 | { | 178 | { | ||
166 | foreach(const Link& link, _links) | 179 | foreach(const Link& link, _links) | ||
167 | link.second->writeVariant(link.first, QVariant::fromValue<StepCore::Object*>(NULL)); | 180 | link.second->writeVariant(link.first, QVariant::fromValue<StepCore::Object*>(NULL)); | ||
168 | //qDebug("%d links removed", _links.count()); | 181 | //qDebug("%d links removed", _links.count()); | ||
▲ Show 20 Lines • Show All 925 Lines • Show Last 20 Lines |