1 // Copyright (C) 2007-2022 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // File : SALOMEDSImpl_AttributeTreeNode.cxx
24 // Author : Sergey RUIN
27 #include "SALOMEDSImpl_AttributeTreeNode.hxx"
30 const std::string& SALOMEDSImpl_AttributeTreeNode::GetDefaultTreeID()
32 static std::string TreeNodeID ("0E1C36E6-379B-4d90-AC37-17A14310E648");
37 SALOMEDSImpl_AttributeTreeNode::SALOMEDSImpl_AttributeTreeNode()
38 :SALOMEDSImpl_GenericAttribute("AttributeTreeNode"), myFather(NULL), myPrevious(NULL), myNext(NULL), myFirst(NULL)
42 SALOMEDSImpl_AttributeTreeNode* SALOMEDSImpl_AttributeTreeNode::Set (const DF_Label& L, const std::string& ID)
44 SALOMEDSImpl_AttributeTreeNode* TN = NULL;
46 if (!(TN=(SALOMEDSImpl_AttributeTreeNode*)L.FindAttribute(ID))) {
47 TN = new SALOMEDSImpl_AttributeTreeNode ();
55 //=======================================================================
57 //purpose : Returns GUID of the TreeNode
58 //=======================================================================
59 const std::string& SALOMEDSImpl_AttributeTreeNode::ID() const
64 //=======================================================================
66 //purpose : Add <TN> as last child of me
67 //return : index of TN under this, in C mode
68 //=======================================================================
69 bool SALOMEDSImpl_AttributeTreeNode::Append (SALOMEDSImpl_AttributeTreeNode* TN, int* childIndex)
73 if (!(TN->ID() == myTreeID)) throw DFexception("SALOMEDSImpl_AttributeTreeNode::Append : uncompatible GUID");
75 if(TN->Label() == Label()) throw DFexception("Attempt of self linking");
77 TN->SetNext(NULL); // Deconnects from next.
83 TN->SetPrevious(NULL); // Deconnects from previous.
86 SALOMEDSImpl_AttributeTreeNode* Last = GetFirst();
87 while (Last && Last->HasNext()) {
88 Last = Last->GetNext();
92 TN->SetPrevious(Last);
98 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
106 //=======================================================================
108 //purpose : Add <TN> as first child of me
109 //=======================================================================
110 bool SALOMEDSImpl_AttributeTreeNode::Prepend (SALOMEDSImpl_AttributeTreeNode* TN)
114 if (!(TN->ID() == myTreeID) ) throw DFexception("SALOMEDSImpl_AttributeTreeNode::Prepend : uncompatible GUID");
116 if(TN->Label() == Label()) throw DFexception("Attempt of self linking");
118 TN->SetPrevious(NULL);
120 TN->SetNext(GetFirst());
121 GetFirst()->SetPrevious(TN);
129 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
135 //=======================================================================
136 //function : InsertBefore
137 //purpose : Inserts the TreeNode <TN> before me
138 //=======================================================================
139 bool SALOMEDSImpl_AttributeTreeNode::InsertBefore (SALOMEDSImpl_AttributeTreeNode* TN)
143 if (!(TN->ID() == myTreeID) ) throw DFexception("SALOMEDSImpl_AttributeTreeNode::InsertBefore : uncompatible GUID");
145 if(TN->Label() == Label()) throw DFexception("Attempt of self linking");
147 TN->SetFather(GetFather());
148 TN->SetPrevious(GetPrevious());
152 GetFather()->SetFirst(TN);
154 GetPrevious()->SetNext(TN);
158 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
163 //=======================================================================
164 //function : InsertAfter
165 //purpose : Inserts the TreeNode <TN> after me
166 //=======================================================================
167 bool SALOMEDSImpl_AttributeTreeNode::InsertAfter (SALOMEDSImpl_AttributeTreeNode* TN)
171 if(TN->Label() == Label()) throw DFexception("Attempt of self linking");
173 if (!(TN->ID() == myTreeID) ) throw DFexception("SALOMEDSImpl_AttributeTreeNode::InsertAfter : uncompatible GUID");
175 TN->SetFather(GetFather());
176 TN->SetPrevious(this);
177 TN->SetNext(GetNext());
179 if (HasNext()) GetNext()->SetPrevious(TN);
183 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
188 //=======================================================================
190 //purpose : Removees the function from the function tree
191 //=======================================================================
192 bool SALOMEDSImpl_AttributeTreeNode::Remove ()
196 if (IsRoot()) return true;
199 GetFather()->SetFirst(GetNext());
201 GetPrevious()->SetNext(GetNext());
204 if (HasPrevious()) GetNext()->SetPrevious(GetPrevious());
205 else GetNext()->SetPrevious(NULL);
208 if (HasPrevious()) GetPrevious()->SetNext(NULL);
211 if (GetFather()->HasFirst()) {
212 if (this == GetFather()->GetFirst()) {
214 GetFather()->SetFirst(GetNext());
216 else GetFather()->SetFirst(NULL);
224 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
229 //=======================================================================
232 //=======================================================================
233 int SALOMEDSImpl_AttributeTreeNode::Depth () const
236 SALOMEDSImpl_AttributeTreeNode* current = (SALOMEDSImpl_AttributeTreeNode*)this;
239 current = current->GetFather();
244 //=======================================================================
245 //function : SetTreeID
246 //purpose : Finds or creates a TreeNode attribute with explicit ID
248 //=======================================================================
249 void SALOMEDSImpl_AttributeTreeNode::SetTreeID (const std::string& explicitID)
251 myTreeID = explicitID;
253 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
257 //=======================================================================
258 //function : IsAscendant
260 //=======================================================================
261 bool SALOMEDSImpl_AttributeTreeNode::IsAscendant (const SALOMEDSImpl_AttributeTreeNode* ofTN) const
263 return ofTN->IsDescendant(this);
266 //=======================================================================
267 //function : IsDescendant
269 //=======================================================================
271 bool SALOMEDSImpl_AttributeTreeNode::IsDescendant (const SALOMEDSImpl_AttributeTreeNode* ofTN) const
273 SALOMEDSImpl_AttributeTreeNode* current = (SALOMEDSImpl_AttributeTreeNode*)this;
275 if (current->GetFather() == ofTN) return true;
276 current = current->GetFather();
281 //=======================================================================
282 //function : IsFather
284 //=======================================================================
286 bool SALOMEDSImpl_AttributeTreeNode::IsFather (const SALOMEDSImpl_AttributeTreeNode* ofTN) const
288 return (ofTN->GetFather() == this);
292 //=======================================================================
295 //=======================================================================
297 bool SALOMEDSImpl_AttributeTreeNode::IsChild (const SALOMEDSImpl_AttributeTreeNode* ofTN) const
299 return (myFather == ofTN);
302 //=======================================================================
304 //purpose : Returns Standard_True if the TreeNode is not attached to a
305 // TreeNode tree or hasn't an Father.
306 //=======================================================================
307 bool SALOMEDSImpl_AttributeTreeNode::IsRoot() const
309 if (!myFather && !myPrevious && !myNext)
314 //=======================================================================
316 //purpose : Returns the TreeNode which has no Father
317 //=======================================================================
318 SALOMEDSImpl_AttributeTreeNode* SALOMEDSImpl_AttributeTreeNode::Root() const
320 SALOMEDSImpl_AttributeTreeNode* O = (SALOMEDSImpl_AttributeTreeNode*)this;
321 while (O && O->HasFather())
326 //=======================================================================
327 //TreeNode : SetFather
328 //purpose : Sets the TreeNode F as Father of me
329 //=======================================================================
330 void SALOMEDSImpl_AttributeTreeNode::SetFather(const SALOMEDSImpl_AttributeTreeNode* F)
334 myFather = (SALOMEDSImpl_AttributeTreeNode*)F;
336 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
339 //=======================================================================
341 //purpose : Sets the TreeNode F next to me
342 //=======================================================================
343 void SALOMEDSImpl_AttributeTreeNode::SetNext(const SALOMEDSImpl_AttributeTreeNode* F)
347 myNext = (SALOMEDSImpl_AttributeTreeNode*)F;
349 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
353 //=======================================================================
354 //TreeNode : SetPrevious
355 //purpose : Sets the TreeNode F previous to me
356 //=======================================================================
357 void SALOMEDSImpl_AttributeTreeNode::SetPrevious(const SALOMEDSImpl_AttributeTreeNode* F)
361 myPrevious = (SALOMEDSImpl_AttributeTreeNode*)F;
363 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
366 //=======================================================================
367 //TreeNode : SetFirst
368 //purpose : Sets the TreeNode F as first in the TreeNode tree
369 //=======================================================================
370 void SALOMEDSImpl_AttributeTreeNode::SetFirst(const SALOMEDSImpl_AttributeTreeNode* F)
374 myFirst = (SALOMEDSImpl_AttributeTreeNode*)F;
376 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
379 //=======================================================================
380 //TreeNode : AfterAddition
381 //purpose : Connects the TreeNode to the tree.
382 //=======================================================================
383 void SALOMEDSImpl_AttributeTreeNode::AfterAddition()
386 myPrevious->SetNext(this);
389 myFather->SetFirst(this);
392 myNext->SetPrevious(this);
396 //=======================================================================
397 //TreeNode : BeforeForget
398 //purpose : Disconnect the TreeNode from the tree.
399 //=======================================================================
400 void SALOMEDSImpl_AttributeTreeNode::BeforeForget()
403 while (HasFirst()) GetFirst()->Remove();
406 //=======================================================================
409 //=======================================================================
410 void SALOMEDSImpl_AttributeTreeNode::Restore(DF_Attribute* other)
412 SALOMEDSImpl_AttributeTreeNode* F = dynamic_cast<SALOMEDSImpl_AttributeTreeNode*>(other);
413 myFather = F->myFather;
414 myPrevious = F->myPrevious;
416 myFirst = F->myFirst;
417 myTreeID = F->myTreeID;
420 //=======================================================================
422 //purpose : Method for Copy mechanism
423 //=======================================================================
425 void SALOMEDSImpl_AttributeTreeNode::Paste(DF_Attribute* into)
427 SALOMEDSImpl_AttributeTreeNode* intof = dynamic_cast<SALOMEDSImpl_AttributeTreeNode*>(into);
428 intof->SetFather(myFather);
429 intof->SetNext(myNext);
430 intof->SetPrevious(myPrevious);
431 intof->SetFirst(myFirst);
432 intof->SetTreeID(myTreeID);
435 //=======================================================================
436 //TreeNode : NewEmpty
437 //purpose : Returns new empty TreeNode attribute
438 //=======================================================================
440 DF_Attribute* SALOMEDSImpl_AttributeTreeNode::NewEmpty() const
442 SALOMEDSImpl_AttributeTreeNode* T = new SALOMEDSImpl_AttributeTreeNode();
443 T->SetTreeID(myTreeID);
447 std::string SALOMEDSImpl_AttributeTreeNode::Type()
449 char* aNodeName = new char[127];
450 sprintf(aNodeName, "AttributeTreeNodeGUID%s", ID().c_str());
451 std::string ret(aNodeName);
457 std::string SALOMEDSImpl_AttributeTreeNode::Save()
459 std::string aFather, aPrevious, aNext, aFirst;
461 if (HasFather()) aFather = GetFather()->Label().Entry(); else aFather = "!";
462 if (HasPrevious()) aPrevious = GetPrevious()->Label().Entry(); else aPrevious = "!";
463 if (HasNext()) aNext = GetNext()->Label().Entry(); else aNext = "!";
464 if (HasFirst()) aFirst = GetFirst()->Label().Entry(); else aFirst = "!";
467 aLength += (int)(aFather.size() + aPrevious.size() + aNext.size() + aFirst.size()); //!< TODO: conversion from size_t to int
468 char* aResult = new char[aLength];
469 sprintf(aResult, "%s %s %s %s", aFather.c_str(), aPrevious.c_str(), aNext.c_str(), aFirst.c_str());
470 std::string ret(aResult);
475 void SALOMEDSImpl_AttributeTreeNode::Load(const std::string& value)
477 char* aCopy = (char*)value.c_str();
478 char* adr = strtok(aCopy, " ");
481 SALOMEDSImpl_AttributeTreeNode* aDepNode = NULL;
483 if (adr && adr[0] != '!') {
484 aLabel = DF_Label::Label(Label(), adr, true);
485 if (!(aDepNode=(SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(ID())))
486 aDepNode = SALOMEDSImpl_AttributeTreeNode::Set(aLabel, ID());
491 adr = strtok(NULL, " ");
492 if (adr && adr[0] != '!') {
493 aLabel = DF_Label::Label(Label(), adr, true);
494 if (!(aDepNode=(SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(ID())))
495 aDepNode = SALOMEDSImpl_AttributeTreeNode::Set(aLabel, ID());
496 SetPrevious(aDepNode);
499 adr = strtok(NULL, " ");
500 if (adr && adr[0] != '!') {
501 aLabel = DF_Label::Label(Label(), adr, true);
502 if (!(aDepNode=(SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(ID())))
503 aDepNode = SALOMEDSImpl_AttributeTreeNode::Set(aLabel, ID());
507 adr = strtok(NULL, " ");
508 if (adr && adr[0] != '!') {
509 aLabel = DF_Label::Label(Label(), adr, true);
510 if (!(aDepNode=(SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(ID())))
511 aDepNode = SALOMEDSImpl_AttributeTreeNode::Set(aLabel, ID());