1 // Copyright (C) 2005 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License.
9 // This library is distributed in the hope that it will be useful
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 // File : SALOMEDSImpl_AttributeTreeNode.cxx
21 // Author : Sergey RUIN
25 #include "SALOMEDSImpl_AttributeTreeNode.hxx"
29 const std::string& SALOMEDSImpl_AttributeTreeNode::GetDefaultTreeID()
31 static std::string TreeNodeID ("0E1C36E6-379B-4d90-AC37-17A14310E648");
36 SALOMEDSImpl_AttributeTreeNode::SALOMEDSImpl_AttributeTreeNode()
37 :SALOMEDSImpl_GenericAttribute("AttributeTreeNode"), myFather(NULL), myPrevious(NULL), myNext(NULL), myFirst(NULL)
41 SALOMEDSImpl_AttributeTreeNode* SALOMEDSImpl_AttributeTreeNode::Set (const DF_Label& L, const std::string& ID)
43 SALOMEDSImpl_AttributeTreeNode* TN = NULL;
45 if (!(TN=(SALOMEDSImpl_AttributeTreeNode*)L.FindAttribute(ID))) {
46 TN = new SALOMEDSImpl_AttributeTreeNode ();
54 //=======================================================================
56 //purpose : Returns GUID of the TreeNode
57 //=======================================================================
58 const std::string& SALOMEDSImpl_AttributeTreeNode::ID() const
63 //=======================================================================
65 //purpose : Add <TN> as last child of me
66 //=======================================================================
67 bool SALOMEDSImpl_AttributeTreeNode::Append (SALOMEDSImpl_AttributeTreeNode* TN)
71 if (!(TN->ID() == myTreeID)) throw DFexception("SALOMEDSImpl_AttributeTreeNode::Append : uncompatible GUID");
73 if(TN->Label() == Label()) throw DFexception("Attempt of self linking");
75 TN->SetNext(NULL); // Deconnects from next.
80 TN->SetPrevious(NULL); // Deconnects from previous.
83 SALOMEDSImpl_AttributeTreeNode* Last = GetFirst();
84 while (Last && Last->HasNext()) {
85 Last = Last->GetNext();
88 TN->SetPrevious(Last);
93 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
98 //=======================================================================
100 //purpose : Add <TN> as first child of me
101 //=======================================================================
102 bool SALOMEDSImpl_AttributeTreeNode::Prepend (SALOMEDSImpl_AttributeTreeNode* TN)
106 if (!(TN->ID() == myTreeID) ) throw DFexception("SALOMEDSImpl_AttributeTreeNode::Prepend : uncompatible GUID");
108 if(TN->Label() == Label()) throw DFexception("Attempt of self linking");
110 TN->SetPrevious(NULL);
112 TN->SetNext(GetFirst());
113 GetFirst()->SetPrevious(TN);
121 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
127 //=======================================================================
128 //function : InsertBefore
129 //purpose : Inserts the TreeNode <TN> before me
130 //=======================================================================
131 bool SALOMEDSImpl_AttributeTreeNode::InsertBefore (SALOMEDSImpl_AttributeTreeNode* TN)
135 if (!(TN->ID() == myTreeID) ) throw DFexception("SALOMEDSImpl_AttributeTreeNode::InsertBefore : uncompatible GUID");
137 if(TN->Label() == Label()) throw DFexception("Attempt of self linking");
139 TN->SetFather(GetFather());
140 TN->SetPrevious(GetPrevious());
144 GetFather()->SetFirst(TN);
146 GetPrevious()->SetNext(TN);
150 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
155 //=======================================================================
156 //function : InsertAfter
157 //purpose : Inserts the TreeNode <TN> after me
158 //=======================================================================
159 bool SALOMEDSImpl_AttributeTreeNode::InsertAfter (SALOMEDSImpl_AttributeTreeNode* TN)
163 if(TN->Label() == Label()) throw DFexception("Attempt of self linking");
165 if (!(TN->ID() == myTreeID) ) throw DFexception("SALOMEDSImpl_AttributeTreeNode::InsertAfter : uncompatible GUID");
167 TN->SetFather(GetFather());
168 TN->SetPrevious(this);
169 TN->SetNext(GetNext());
171 if (HasNext()) GetNext()->SetPrevious(TN);
175 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
180 //=======================================================================
182 //purpose : Removees the function from the function tree
183 //=======================================================================
184 bool SALOMEDSImpl_AttributeTreeNode::Remove ()
188 if (IsRoot()) return true;
191 GetFather()->SetFirst(GetNext());
193 GetPrevious()->SetNext(GetNext());
196 if (HasPrevious()) GetNext()->SetPrevious(GetPrevious());
197 else GetNext()->SetPrevious(NULL);
200 if (HasPrevious()) GetPrevious()->SetNext(NULL);
203 if (GetFather()->HasFirst()) {
204 if (this == GetFather()->GetFirst()) {
206 GetFather()->SetFirst(GetNext());
208 else GetFather()->SetFirst(NULL);
216 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
221 //=======================================================================
224 //=======================================================================
225 int SALOMEDSImpl_AttributeTreeNode::Depth () const
228 SALOMEDSImpl_AttributeTreeNode* current = (SALOMEDSImpl_AttributeTreeNode*)this;
231 current = current->GetFather();
236 //=======================================================================
237 //function : SetTreeID
238 //purpose : Finds or creates a TreeNode attribute with explicit ID
240 //=======================================================================
241 void SALOMEDSImpl_AttributeTreeNode::SetTreeID (const std::string& explicitID)
243 myTreeID = explicitID;
245 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
249 //=======================================================================
250 //function : IsAscendant
252 //=======================================================================
253 bool SALOMEDSImpl_AttributeTreeNode::IsAscendant (const SALOMEDSImpl_AttributeTreeNode* ofTN) const
255 return ofTN->IsDescendant(this);
258 //=======================================================================
259 //function : IsDescendant
261 //=======================================================================
263 bool SALOMEDSImpl_AttributeTreeNode::IsDescendant (const SALOMEDSImpl_AttributeTreeNode* ofTN) const
265 SALOMEDSImpl_AttributeTreeNode* current = (SALOMEDSImpl_AttributeTreeNode*)this;
267 if (current->GetFather() == ofTN) return true;
268 current = current->GetFather();
273 //=======================================================================
274 //function : IsFather
276 //=======================================================================
278 bool SALOMEDSImpl_AttributeTreeNode::IsFather (const SALOMEDSImpl_AttributeTreeNode* ofTN) const
280 return (ofTN->GetFather() == this);
284 //=======================================================================
287 //=======================================================================
289 bool SALOMEDSImpl_AttributeTreeNode::IsChild (const SALOMEDSImpl_AttributeTreeNode* ofTN) const
291 return (myFather == ofTN);
294 //=======================================================================
296 //purpose : Returns Standard_True if the TreeNode is not attached to a
297 // TreeNode tree or hasn't an Father.
298 //=======================================================================
299 bool SALOMEDSImpl_AttributeTreeNode::IsRoot() const
301 if (!myFather && !myPrevious && !myNext)
306 //=======================================================================
308 //purpose : Returns the TreeNode which has no Father
309 //=======================================================================
310 SALOMEDSImpl_AttributeTreeNode* SALOMEDSImpl_AttributeTreeNode::Root() const
312 SALOMEDSImpl_AttributeTreeNode* O = (SALOMEDSImpl_AttributeTreeNode*)this;
313 while (O && O->HasFather())
318 //=======================================================================
319 //TreeNode : SetFather
320 //purpose : Sets the TreeNode F as Father of me
321 //=======================================================================
322 void SALOMEDSImpl_AttributeTreeNode::SetFather(const SALOMEDSImpl_AttributeTreeNode* F)
326 myFather = (SALOMEDSImpl_AttributeTreeNode*)F;
328 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
331 //=======================================================================
333 //purpose : Sets the TreeNode F next to me
334 //=======================================================================
335 void SALOMEDSImpl_AttributeTreeNode::SetNext(const SALOMEDSImpl_AttributeTreeNode* F)
339 myNext = (SALOMEDSImpl_AttributeTreeNode*)F;
341 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
345 //=======================================================================
346 //TreeNode : SetPrevious
347 //purpose : Sets the TreeNode F previous to me
348 //=======================================================================
349 void SALOMEDSImpl_AttributeTreeNode::SetPrevious(const SALOMEDSImpl_AttributeTreeNode* F)
353 myPrevious = (SALOMEDSImpl_AttributeTreeNode*)F;
355 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
358 //=======================================================================
359 //TreeNode : SetFirst
360 //purpose : Sets the TreeNode F as first in the TreeNode tree
361 //=======================================================================
362 void SALOMEDSImpl_AttributeTreeNode::SetFirst(const SALOMEDSImpl_AttributeTreeNode* F)
366 myFirst = (SALOMEDSImpl_AttributeTreeNode*)F;
368 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
371 //=======================================================================
372 //TreeNode : AfterAddition
373 //purpose : Connects the TreeNode to the tree.
374 //=======================================================================
375 void SALOMEDSImpl_AttributeTreeNode::AfterAddition()
378 myPrevious->SetNext(this);
381 myFather->SetFirst(this);
384 myNext->SetPrevious(this);
388 //=======================================================================
389 //TreeNode : BeforeForget
390 //purpose : Disconnect the TreeNode from the tree.
391 //=======================================================================
392 void SALOMEDSImpl_AttributeTreeNode::BeforeForget()
395 while (HasFirst()) GetFirst()->Remove();
398 //=======================================================================
401 //=======================================================================
402 void SALOMEDSImpl_AttributeTreeNode::Restore(DF_Attribute* other)
404 SALOMEDSImpl_AttributeTreeNode* F = dynamic_cast<SALOMEDSImpl_AttributeTreeNode*>(other);
405 myFather = F->myFather;
406 myPrevious = F->myPrevious;
408 myFirst = F->myFirst;
409 myTreeID = F->myTreeID;
412 //=======================================================================
414 //purpose : Method for Copy mechanism
415 //=======================================================================
417 void SALOMEDSImpl_AttributeTreeNode::Paste(DF_Attribute* into)
419 SALOMEDSImpl_AttributeTreeNode* intof = dynamic_cast<SALOMEDSImpl_AttributeTreeNode*>(into);
420 intof->SetFather(myFather);
421 intof->SetNext(myNext);
422 intof->SetPrevious(myPrevious);
423 intof->SetFirst(myFirst);
424 intof->SetTreeID(myTreeID);
427 //=======================================================================
428 //TreeNode : NewEmpty
429 //purpose : Returns new empty TreeNode attribute
430 //=======================================================================
432 DF_Attribute* SALOMEDSImpl_AttributeTreeNode::NewEmpty() const
434 SALOMEDSImpl_AttributeTreeNode* T = new SALOMEDSImpl_AttributeTreeNode();
435 T->SetTreeID(myTreeID);
439 string SALOMEDSImpl_AttributeTreeNode::Type()
441 char* aNodeName = new char[127];
442 sprintf(aNodeName, "AttributeTreeNodeGUID%s", ID().c_str());
443 string ret(aNodeName);
449 string SALOMEDSImpl_AttributeTreeNode::Save()
451 string aFather, aPrevious, aNext, aFirst;
453 if (HasFather()) aFather = GetFather()->Label().Entry(); else aFather = "!";
454 if (HasPrevious()) aPrevious = GetPrevious()->Label().Entry(); else aPrevious = "!";
455 if (HasNext()) aNext = GetNext()->Label().Entry(); else aNext = "!";
456 if (HasFirst()) aFirst = GetFirst()->Label().Entry(); else aFirst = "!";
459 aLength += aFather.size() + aPrevious.size() + aNext.size() + aFirst.size();
460 char* aResult = new char[aLength];
461 sprintf(aResult, "%s %s %s %s", aFather.c_str(), aPrevious.c_str(), aNext.c_str(), aFirst.c_str());
467 void SALOMEDSImpl_AttributeTreeNode::Load(const string& value)
469 char* aCopy = (char*)value.c_str();
470 char* adr = strtok(aCopy, " ");
473 SALOMEDSImpl_AttributeTreeNode* aDepNode = NULL;
475 if (adr && adr[0] != '!') {
476 aLabel = DF_Label::Label(Label(), adr, true);
477 if (!(aDepNode=(SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(ID())))
478 aDepNode = SALOMEDSImpl_AttributeTreeNode::Set(aLabel, ID());
483 adr = strtok(NULL, " ");
484 if (adr && adr[0] != '!') {
485 aLabel = DF_Label::Label(Label(), adr, true);
486 if (!(aDepNode=(SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(ID())))
487 aDepNode = SALOMEDSImpl_AttributeTreeNode::Set(aLabel, ID());
488 SetPrevious(aDepNode);
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());
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());