1 // Copyright (C) 2007-2008 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.
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
22 // File : SALOMEDSImpl_AttributeTreeNode.cxx
23 // Author : Sergey RUIN
26 #include "SALOMEDSImpl_AttributeTreeNode.hxx"
31 const std::string& SALOMEDSImpl_AttributeTreeNode::GetDefaultTreeID()
33 static std::string TreeNodeID ("0E1C36E6-379B-4d90-AC37-17A14310E648");
38 SALOMEDSImpl_AttributeTreeNode::SALOMEDSImpl_AttributeTreeNode()
39 :SALOMEDSImpl_GenericAttribute("AttributeTreeNode"), myFather(NULL), myPrevious(NULL), myNext(NULL), myFirst(NULL)
43 SALOMEDSImpl_AttributeTreeNode* SALOMEDSImpl_AttributeTreeNode::Set (const DF_Label& L, const std::string& ID)
45 SALOMEDSImpl_AttributeTreeNode* TN = NULL;
47 if (!(TN=(SALOMEDSImpl_AttributeTreeNode*)L.FindAttribute(ID))) {
48 TN = new SALOMEDSImpl_AttributeTreeNode ();
56 //=======================================================================
58 //purpose : Returns GUID of the TreeNode
59 //=======================================================================
60 const std::string& SALOMEDSImpl_AttributeTreeNode::ID() const
65 //=======================================================================
67 //purpose : Add <TN> as last child of me
68 //=======================================================================
69 bool SALOMEDSImpl_AttributeTreeNode::Append (SALOMEDSImpl_AttributeTreeNode* TN)
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.
82 TN->SetPrevious(NULL); // Deconnects from previous.
85 SALOMEDSImpl_AttributeTreeNode* Last = GetFirst();
86 while (Last && Last->HasNext()) {
87 Last = Last->GetNext();
90 TN->SetPrevious(Last);
95 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
100 //=======================================================================
102 //purpose : Add <TN> as first child of me
103 //=======================================================================
104 bool SALOMEDSImpl_AttributeTreeNode::Prepend (SALOMEDSImpl_AttributeTreeNode* TN)
108 if (!(TN->ID() == myTreeID) ) throw DFexception("SALOMEDSImpl_AttributeTreeNode::Prepend : uncompatible GUID");
110 if(TN->Label() == Label()) throw DFexception("Attempt of self linking");
112 TN->SetPrevious(NULL);
114 TN->SetNext(GetFirst());
115 GetFirst()->SetPrevious(TN);
123 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
129 //=======================================================================
130 //function : InsertBefore
131 //purpose : Inserts the TreeNode <TN> before me
132 //=======================================================================
133 bool SALOMEDSImpl_AttributeTreeNode::InsertBefore (SALOMEDSImpl_AttributeTreeNode* TN)
137 if (!(TN->ID() == myTreeID) ) throw DFexception("SALOMEDSImpl_AttributeTreeNode::InsertBefore : uncompatible GUID");
139 if(TN->Label() == Label()) throw DFexception("Attempt of self linking");
141 TN->SetFather(GetFather());
142 TN->SetPrevious(GetPrevious());
146 GetFather()->SetFirst(TN);
148 GetPrevious()->SetNext(TN);
152 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
157 //=======================================================================
158 //function : InsertAfter
159 //purpose : Inserts the TreeNode <TN> after me
160 //=======================================================================
161 bool SALOMEDSImpl_AttributeTreeNode::InsertAfter (SALOMEDSImpl_AttributeTreeNode* TN)
165 if(TN->Label() == Label()) throw DFexception("Attempt of self linking");
167 if (!(TN->ID() == myTreeID) ) throw DFexception("SALOMEDSImpl_AttributeTreeNode::InsertAfter : uncompatible GUID");
169 TN->SetFather(GetFather());
170 TN->SetPrevious(this);
171 TN->SetNext(GetNext());
173 if (HasNext()) GetNext()->SetPrevious(TN);
177 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
182 //=======================================================================
184 //purpose : Removees the function from the function tree
185 //=======================================================================
186 bool SALOMEDSImpl_AttributeTreeNode::Remove ()
190 if (IsRoot()) return true;
193 GetFather()->SetFirst(GetNext());
195 GetPrevious()->SetNext(GetNext());
198 if (HasPrevious()) GetNext()->SetPrevious(GetPrevious());
199 else GetNext()->SetPrevious(NULL);
202 if (HasPrevious()) GetPrevious()->SetNext(NULL);
205 if (GetFather()->HasFirst()) {
206 if (this == GetFather()->GetFirst()) {
208 GetFather()->SetFirst(GetNext());
210 else GetFather()->SetFirst(NULL);
218 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
223 //=======================================================================
226 //=======================================================================
227 int SALOMEDSImpl_AttributeTreeNode::Depth () const
230 SALOMEDSImpl_AttributeTreeNode* current = (SALOMEDSImpl_AttributeTreeNode*)this;
233 current = current->GetFather();
238 //=======================================================================
239 //function : SetTreeID
240 //purpose : Finds or creates a TreeNode attribute with explicit ID
242 //=======================================================================
243 void SALOMEDSImpl_AttributeTreeNode::SetTreeID (const std::string& explicitID)
245 myTreeID = explicitID;
247 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
251 //=======================================================================
252 //function : IsAscendant
254 //=======================================================================
255 bool SALOMEDSImpl_AttributeTreeNode::IsAscendant (const SALOMEDSImpl_AttributeTreeNode* ofTN) const
257 return ofTN->IsDescendant(this);
260 //=======================================================================
261 //function : IsDescendant
263 //=======================================================================
265 bool SALOMEDSImpl_AttributeTreeNode::IsDescendant (const SALOMEDSImpl_AttributeTreeNode* ofTN) const
267 SALOMEDSImpl_AttributeTreeNode* current = (SALOMEDSImpl_AttributeTreeNode*)this;
269 if (current->GetFather() == ofTN) return true;
270 current = current->GetFather();
275 //=======================================================================
276 //function : IsFather
278 //=======================================================================
280 bool SALOMEDSImpl_AttributeTreeNode::IsFather (const SALOMEDSImpl_AttributeTreeNode* ofTN) const
282 return (ofTN->GetFather() == this);
286 //=======================================================================
289 //=======================================================================
291 bool SALOMEDSImpl_AttributeTreeNode::IsChild (const SALOMEDSImpl_AttributeTreeNode* ofTN) const
293 return (myFather == ofTN);
296 //=======================================================================
298 //purpose : Returns Standard_True if the TreeNode is not attached to a
299 // TreeNode tree or hasn't an Father.
300 //=======================================================================
301 bool SALOMEDSImpl_AttributeTreeNode::IsRoot() const
303 if (!myFather && !myPrevious && !myNext)
308 //=======================================================================
310 //purpose : Returns the TreeNode which has no Father
311 //=======================================================================
312 SALOMEDSImpl_AttributeTreeNode* SALOMEDSImpl_AttributeTreeNode::Root() const
314 SALOMEDSImpl_AttributeTreeNode* O = (SALOMEDSImpl_AttributeTreeNode*)this;
315 while (O && O->HasFather())
320 //=======================================================================
321 //TreeNode : SetFather
322 //purpose : Sets the TreeNode F as Father of me
323 //=======================================================================
324 void SALOMEDSImpl_AttributeTreeNode::SetFather(const SALOMEDSImpl_AttributeTreeNode* F)
328 myFather = (SALOMEDSImpl_AttributeTreeNode*)F;
330 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
333 //=======================================================================
335 //purpose : Sets the TreeNode F next to me
336 //=======================================================================
337 void SALOMEDSImpl_AttributeTreeNode::SetNext(const SALOMEDSImpl_AttributeTreeNode* F)
341 myNext = (SALOMEDSImpl_AttributeTreeNode*)F;
343 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
347 //=======================================================================
348 //TreeNode : SetPrevious
349 //purpose : Sets the TreeNode F previous to me
350 //=======================================================================
351 void SALOMEDSImpl_AttributeTreeNode::SetPrevious(const SALOMEDSImpl_AttributeTreeNode* F)
355 myPrevious = (SALOMEDSImpl_AttributeTreeNode*)F;
357 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
360 //=======================================================================
361 //TreeNode : SetFirst
362 //purpose : Sets the TreeNode F as first in the TreeNode tree
363 //=======================================================================
364 void SALOMEDSImpl_AttributeTreeNode::SetFirst(const SALOMEDSImpl_AttributeTreeNode* F)
368 myFirst = (SALOMEDSImpl_AttributeTreeNode*)F;
370 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
373 //=======================================================================
374 //TreeNode : AfterAddition
375 //purpose : Connects the TreeNode to the tree.
376 //=======================================================================
377 void SALOMEDSImpl_AttributeTreeNode::AfterAddition()
380 myPrevious->SetNext(this);
383 myFather->SetFirst(this);
386 myNext->SetPrevious(this);
390 //=======================================================================
391 //TreeNode : BeforeForget
392 //purpose : Disconnect the TreeNode from the tree.
393 //=======================================================================
394 void SALOMEDSImpl_AttributeTreeNode::BeforeForget()
397 while (HasFirst()) GetFirst()->Remove();
400 //=======================================================================
403 //=======================================================================
404 void SALOMEDSImpl_AttributeTreeNode::Restore(DF_Attribute* other)
406 SALOMEDSImpl_AttributeTreeNode* F = dynamic_cast<SALOMEDSImpl_AttributeTreeNode*>(other);
407 myFather = F->myFather;
408 myPrevious = F->myPrevious;
410 myFirst = F->myFirst;
411 myTreeID = F->myTreeID;
414 //=======================================================================
416 //purpose : Method for Copy mechanism
417 //=======================================================================
419 void SALOMEDSImpl_AttributeTreeNode::Paste(DF_Attribute* into)
421 SALOMEDSImpl_AttributeTreeNode* intof = dynamic_cast<SALOMEDSImpl_AttributeTreeNode*>(into);
422 intof->SetFather(myFather);
423 intof->SetNext(myNext);
424 intof->SetPrevious(myPrevious);
425 intof->SetFirst(myFirst);
426 intof->SetTreeID(myTreeID);
429 //=======================================================================
430 //TreeNode : NewEmpty
431 //purpose : Returns new empty TreeNode attribute
432 //=======================================================================
434 DF_Attribute* SALOMEDSImpl_AttributeTreeNode::NewEmpty() const
436 SALOMEDSImpl_AttributeTreeNode* T = new SALOMEDSImpl_AttributeTreeNode();
437 T->SetTreeID(myTreeID);
441 string SALOMEDSImpl_AttributeTreeNode::Type()
443 char* aNodeName = new char[127];
444 sprintf(aNodeName, "AttributeTreeNodeGUID%s", ID().c_str());
445 string ret(aNodeName);
451 string SALOMEDSImpl_AttributeTreeNode::Save()
453 string aFather, aPrevious, aNext, aFirst;
455 if (HasFather()) aFather = GetFather()->Label().Entry(); else aFather = "!";
456 if (HasPrevious()) aPrevious = GetPrevious()->Label().Entry(); else aPrevious = "!";
457 if (HasNext()) aNext = GetNext()->Label().Entry(); else aNext = "!";
458 if (HasFirst()) aFirst = GetFirst()->Label().Entry(); else aFirst = "!";
461 aLength += aFather.size() + aPrevious.size() + aNext.size() + aFirst.size();
462 char* aResult = new char[aLength];
463 sprintf(aResult, "%s %s %s %s", aFather.c_str(), aPrevious.c_str(), aNext.c_str(), aFirst.c_str());
469 void SALOMEDSImpl_AttributeTreeNode::Load(const string& value)
471 char* aCopy = (char*)value.c_str();
472 char* adr = strtok(aCopy, " ");
475 SALOMEDSImpl_AttributeTreeNode* aDepNode = NULL;
477 if (adr && adr[0] != '!') {
478 aLabel = DF_Label::Label(Label(), adr, true);
479 if (!(aDepNode=(SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(ID())))
480 aDepNode = SALOMEDSImpl_AttributeTreeNode::Set(aLabel, ID());
485 adr = strtok(NULL, " ");
486 if (adr && adr[0] != '!') {
487 aLabel = DF_Label::Label(Label(), adr, true);
488 if (!(aDepNode=(SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(ID())))
489 aDepNode = SALOMEDSImpl_AttributeTreeNode::Set(aLabel, ID());
490 SetPrevious(aDepNode);
493 adr = strtok(NULL, " ");
494 if (adr && adr[0] != '!') {
495 aLabel = DF_Label::Label(Label(), adr, true);
496 if (!(aDepNode=(SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(ID())))
497 aDepNode = SALOMEDSImpl_AttributeTreeNode::Set(aLabel, ID());
501 adr = strtok(NULL, " ");
502 if (adr && adr[0] != '!') {
503 aLabel = DF_Label::Label(Label(), adr, true);
504 if (!(aDepNode=(SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(ID())))
505 aDepNode = SALOMEDSImpl_AttributeTreeNode::Set(aLabel, ID());