1 // Copyright (C) 2007-2016 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 //=======================================================================
68 bool SALOMEDSImpl_AttributeTreeNode::Append (SALOMEDSImpl_AttributeTreeNode* TN)
72 if (!(TN->ID() == myTreeID)) throw DFexception("SALOMEDSImpl_AttributeTreeNode::Append : uncompatible GUID");
74 if(TN->Label() == Label()) throw DFexception("Attempt of self linking");
76 TN->SetNext(NULL); // Deconnects from next.
81 TN->SetPrevious(NULL); // Deconnects from previous.
84 SALOMEDSImpl_AttributeTreeNode* Last = GetFirst();
85 while (Last && Last->HasNext()) {
86 Last = Last->GetNext();
89 TN->SetPrevious(Last);
94 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
99 //=======================================================================
101 //purpose : Add <TN> as first child of me
102 //=======================================================================
103 bool SALOMEDSImpl_AttributeTreeNode::Prepend (SALOMEDSImpl_AttributeTreeNode* TN)
107 if (!(TN->ID() == myTreeID) ) throw DFexception("SALOMEDSImpl_AttributeTreeNode::Prepend : uncompatible GUID");
109 if(TN->Label() == Label()) throw DFexception("Attempt of self linking");
111 TN->SetPrevious(NULL);
113 TN->SetNext(GetFirst());
114 GetFirst()->SetPrevious(TN);
122 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
128 //=======================================================================
129 //function : InsertBefore
130 //purpose : Inserts the TreeNode <TN> before me
131 //=======================================================================
132 bool SALOMEDSImpl_AttributeTreeNode::InsertBefore (SALOMEDSImpl_AttributeTreeNode* TN)
136 if (!(TN->ID() == myTreeID) ) throw DFexception("SALOMEDSImpl_AttributeTreeNode::InsertBefore : uncompatible GUID");
138 if(TN->Label() == Label()) throw DFexception("Attempt of self linking");
140 TN->SetFather(GetFather());
141 TN->SetPrevious(GetPrevious());
145 GetFather()->SetFirst(TN);
147 GetPrevious()->SetNext(TN);
151 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
156 //=======================================================================
157 //function : InsertAfter
158 //purpose : Inserts the TreeNode <TN> after me
159 //=======================================================================
160 bool SALOMEDSImpl_AttributeTreeNode::InsertAfter (SALOMEDSImpl_AttributeTreeNode* TN)
164 if(TN->Label() == Label()) throw DFexception("Attempt of self linking");
166 if (!(TN->ID() == myTreeID) ) throw DFexception("SALOMEDSImpl_AttributeTreeNode::InsertAfter : uncompatible GUID");
168 TN->SetFather(GetFather());
169 TN->SetPrevious(this);
170 TN->SetNext(GetNext());
172 if (HasNext()) GetNext()->SetPrevious(TN);
176 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
181 //=======================================================================
183 //purpose : Removees the function from the function tree
184 //=======================================================================
185 bool SALOMEDSImpl_AttributeTreeNode::Remove ()
189 if (IsRoot()) return true;
192 GetFather()->SetFirst(GetNext());
194 GetPrevious()->SetNext(GetNext());
197 if (HasPrevious()) GetNext()->SetPrevious(GetPrevious());
198 else GetNext()->SetPrevious(NULL);
201 if (HasPrevious()) GetPrevious()->SetNext(NULL);
204 if (GetFather()->HasFirst()) {
205 if (this == GetFather()->GetFirst()) {
207 GetFather()->SetFirst(GetNext());
209 else GetFather()->SetFirst(NULL);
217 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
222 //=======================================================================
225 //=======================================================================
226 int SALOMEDSImpl_AttributeTreeNode::Depth () const
229 SALOMEDSImpl_AttributeTreeNode* current = (SALOMEDSImpl_AttributeTreeNode*)this;
232 current = current->GetFather();
237 //=======================================================================
238 //function : SetTreeID
239 //purpose : Finds or creates a TreeNode attribute with explicit ID
241 //=======================================================================
242 void SALOMEDSImpl_AttributeTreeNode::SetTreeID (const std::string& explicitID)
244 myTreeID = explicitID;
246 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
250 //=======================================================================
251 //function : IsAscendant
253 //=======================================================================
254 bool SALOMEDSImpl_AttributeTreeNode::IsAscendant (const SALOMEDSImpl_AttributeTreeNode* ofTN) const
256 return ofTN->IsDescendant(this);
259 //=======================================================================
260 //function : IsDescendant
262 //=======================================================================
264 bool SALOMEDSImpl_AttributeTreeNode::IsDescendant (const SALOMEDSImpl_AttributeTreeNode* ofTN) const
266 SALOMEDSImpl_AttributeTreeNode* current = (SALOMEDSImpl_AttributeTreeNode*)this;
268 if (current->GetFather() == ofTN) return true;
269 current = current->GetFather();
274 //=======================================================================
275 //function : IsFather
277 //=======================================================================
279 bool SALOMEDSImpl_AttributeTreeNode::IsFather (const SALOMEDSImpl_AttributeTreeNode* ofTN) const
281 return (ofTN->GetFather() == this);
285 //=======================================================================
288 //=======================================================================
290 bool SALOMEDSImpl_AttributeTreeNode::IsChild (const SALOMEDSImpl_AttributeTreeNode* ofTN) const
292 return (myFather == ofTN);
295 //=======================================================================
297 //purpose : Returns Standard_True if the TreeNode is not attached to a
298 // TreeNode tree or hasn't an Father.
299 //=======================================================================
300 bool SALOMEDSImpl_AttributeTreeNode::IsRoot() const
302 if (!myFather && !myPrevious && !myNext)
307 //=======================================================================
309 //purpose : Returns the TreeNode which has no Father
310 //=======================================================================
311 SALOMEDSImpl_AttributeTreeNode* SALOMEDSImpl_AttributeTreeNode::Root() const
313 SALOMEDSImpl_AttributeTreeNode* O = (SALOMEDSImpl_AttributeTreeNode*)this;
314 while (O && O->HasFather())
319 //=======================================================================
320 //TreeNode : SetFather
321 //purpose : Sets the TreeNode F as Father of me
322 //=======================================================================
323 void SALOMEDSImpl_AttributeTreeNode::SetFather(const SALOMEDSImpl_AttributeTreeNode* F)
327 myFather = (SALOMEDSImpl_AttributeTreeNode*)F;
329 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
332 //=======================================================================
334 //purpose : Sets the TreeNode F next to me
335 //=======================================================================
336 void SALOMEDSImpl_AttributeTreeNode::SetNext(const SALOMEDSImpl_AttributeTreeNode* F)
340 myNext = (SALOMEDSImpl_AttributeTreeNode*)F;
342 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
346 //=======================================================================
347 //TreeNode : SetPrevious
348 //purpose : Sets the TreeNode F previous to me
349 //=======================================================================
350 void SALOMEDSImpl_AttributeTreeNode::SetPrevious(const SALOMEDSImpl_AttributeTreeNode* F)
354 myPrevious = (SALOMEDSImpl_AttributeTreeNode*)F;
356 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
359 //=======================================================================
360 //TreeNode : SetFirst
361 //purpose : Sets the TreeNode F as first in the TreeNode tree
362 //=======================================================================
363 void SALOMEDSImpl_AttributeTreeNode::SetFirst(const SALOMEDSImpl_AttributeTreeNode* F)
367 myFirst = (SALOMEDSImpl_AttributeTreeNode*)F;
369 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
372 //=======================================================================
373 //TreeNode : AfterAddition
374 //purpose : Connects the TreeNode to the tree.
375 //=======================================================================
376 void SALOMEDSImpl_AttributeTreeNode::AfterAddition()
379 myPrevious->SetNext(this);
382 myFather->SetFirst(this);
385 myNext->SetPrevious(this);
389 //=======================================================================
390 //TreeNode : BeforeForget
391 //purpose : Disconnect the TreeNode from the tree.
392 //=======================================================================
393 void SALOMEDSImpl_AttributeTreeNode::BeforeForget()
396 while (HasFirst()) GetFirst()->Remove();
399 //=======================================================================
402 //=======================================================================
403 void SALOMEDSImpl_AttributeTreeNode::Restore(DF_Attribute* other)
405 SALOMEDSImpl_AttributeTreeNode* F = dynamic_cast<SALOMEDSImpl_AttributeTreeNode*>(other);
406 myFather = F->myFather;
407 myPrevious = F->myPrevious;
409 myFirst = F->myFirst;
410 myTreeID = F->myTreeID;
413 //=======================================================================
415 //purpose : Method for Copy mechanism
416 //=======================================================================
418 void SALOMEDSImpl_AttributeTreeNode::Paste(DF_Attribute* into)
420 SALOMEDSImpl_AttributeTreeNode* intof = dynamic_cast<SALOMEDSImpl_AttributeTreeNode*>(into);
421 intof->SetFather(myFather);
422 intof->SetNext(myNext);
423 intof->SetPrevious(myPrevious);
424 intof->SetFirst(myFirst);
425 intof->SetTreeID(myTreeID);
428 //=======================================================================
429 //TreeNode : NewEmpty
430 //purpose : Returns new empty TreeNode attribute
431 //=======================================================================
433 DF_Attribute* SALOMEDSImpl_AttributeTreeNode::NewEmpty() const
435 SALOMEDSImpl_AttributeTreeNode* T = new SALOMEDSImpl_AttributeTreeNode();
436 T->SetTreeID(myTreeID);
440 std::string SALOMEDSImpl_AttributeTreeNode::Type()
442 char* aNodeName = new char[127];
443 sprintf(aNodeName, "AttributeTreeNodeGUID%s", ID().c_str());
444 std::string ret(aNodeName);
450 std::string SALOMEDSImpl_AttributeTreeNode::Save()
452 std::string aFather, aPrevious, aNext, aFirst;
454 if (HasFather()) aFather = GetFather()->Label().Entry(); else aFather = "!";
455 if (HasPrevious()) aPrevious = GetPrevious()->Label().Entry(); else aPrevious = "!";
456 if (HasNext()) aNext = GetNext()->Label().Entry(); else aNext = "!";
457 if (HasFirst()) aFirst = GetFirst()->Label().Entry(); else aFirst = "!";
460 aLength += aFather.size() + aPrevious.size() + aNext.size() + aFirst.size();
461 char* aResult = new char[aLength];
462 sprintf(aResult, "%s %s %s %s", aFather.c_str(), aPrevious.c_str(), aNext.c_str(), aFirst.c_str());
463 std::string ret(aResult);
468 void SALOMEDSImpl_AttributeTreeNode::Load(const std::string& value)
470 char* aCopy = (char*)value.c_str();
471 char* adr = strtok(aCopy, " ");
474 SALOMEDSImpl_AttributeTreeNode* aDepNode = NULL;
476 if (adr && adr[0] != '!') {
477 aLabel = DF_Label::Label(Label(), adr, true);
478 if (!(aDepNode=(SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(ID())))
479 aDepNode = SALOMEDSImpl_AttributeTreeNode::Set(aLabel, ID());
484 adr = strtok(NULL, " ");
485 if (adr && adr[0] != '!') {
486 aLabel = DF_Label::Label(Label(), adr, true);
487 if (!(aDepNode=(SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(ID())))
488 aDepNode = SALOMEDSImpl_AttributeTreeNode::Set(aLabel, ID());
489 SetPrevious(aDepNode);
492 adr = strtok(NULL, " ");
493 if (adr && adr[0] != '!') {
494 aLabel = DF_Label::Label(Label(), adr, true);
495 if (!(aDepNode=(SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(ID())))
496 aDepNode = SALOMEDSImpl_AttributeTreeNode::Set(aLabel, ID());
500 adr = strtok(NULL, " ");
501 if (adr && adr[0] != '!') {
502 aLabel = DF_Label::Label(Label(), adr, true);
503 if (!(aDepNode=(SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(ID())))
504 aDepNode = SALOMEDSImpl_AttributeTreeNode::Set(aLabel, ID());