1 // Copyright (C) 2005 OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 // File: SalomeApp_DataModel.cxx
20 // Created: 10/25/2004 10:36:06 AM
21 // Author: Sergey LITONIN
22 // Copyright (C) CEA 2004
24 #include "SalomeApp_DataModel.h"
25 #include "SalomeApp_Study.h"
26 #include "SalomeApp_DataObject.h"
27 #include "SalomeApp_Module.h"
28 #include "SalomeApp_Application.h"
29 #include "SalomeApp_Engine_i.hxx"
31 #include "LightApp_RootObject.h"
33 #include <CAM_DataObject.h>
35 #include <SUIT_Application.h>
36 #include <SUIT_ResourceMgr.h>
37 #include <SUIT_Session.h>
38 #include <SUIT_TreeSync.h>
39 #include <SUIT_DataObjectIterator.h>
41 #include <SALOMEconfig.h>
42 #include CORBA_SERVER_HEADER(SALOME_Exception)
44 typedef _PTR(SObject) kerPtr;
45 typedef SUIT_DataObject* suitPtr;
48 \class SalomeApp_DataModelSync
49 Auxiliary class for synchronizing tree of kernel objects and SUIT_DataObjects
51 class SalomeApp_DataModelSync
54 SalomeApp_DataModelSync( _PTR( Study ), SUIT_DataObject* );
56 suitPtr createItem( const kerPtr&, const suitPtr&, const suitPtr&, const bool ) const;
57 void deleteItemWithChildren( const suitPtr& ) const;
58 bool isEqual( const kerPtr&, const suitPtr& ) const;
59 kerPtr nullSrc() const;
60 suitPtr nullTrg() const;
61 void children( const kerPtr&, QValueList<kerPtr>& ) const;
62 void children( const suitPtr&, QValueList<suitPtr>& ) const;
63 suitPtr parent( const suitPtr& ) const;
64 bool isCorrect( const kerPtr& ) const;
65 void updateItem( const kerPtr&, const suitPtr& ) const;
68 _PTR( Study ) myStudy;
69 SUIT_DataObject* myRoot;
75 SalomeApp_DataModelSync::SalomeApp_DataModelSync( _PTR( Study ) aStudy, SUIT_DataObject* aRoot )
82 \return true if kernel object is correct (has non empty name or is reference)
84 bool SalomeApp_DataModelSync::isCorrect( const kerPtr& so ) const
87 QString name = so->GetName().c_str();
88 _PTR( GenericAttribute ) anAttr;
90 if ( so->FindAttribute(anAttr, "AttributeDrawable") )
92 _PTR(AttributeDrawable) aAttrDraw = anAttr;
93 isDraw = aAttrDraw->IsDrawable();
95 bool res = so && ( so->GetName().size() || so->ReferencedObject( refObj ) ) && isDraw;
100 Creates SUIT object by KERNEL object
101 \param so - corresponding KERNEL object
102 \param parent - parent for SUIT object
103 \param after - previous sibling for SUIT object
104 \param prepend - SUIT object must be added to start of children list
106 suitPtr SalomeApp_DataModelSync::createItem( const kerPtr& so,
107 const suitPtr& parent,
108 const suitPtr& after,
109 const bool prepend ) const
111 if( !isCorrect( so ) )
114 _PTR(SComponent) aSComp( so );
115 suitPtr nitem = aSComp ? new SalomeApp_ModuleObject( aSComp, 0 ) :
116 new SalomeApp_DataObject( so, 0 );
121 parent->children( ch );
122 int pos = ch.find( after );
124 parent->insertChild( nitem, pos+1 );
126 parent->appendChild( nitem );
129 parent->insertChild( nitem, 0 );
131 parent->appendChild( nitem );
133 myRoot->appendChild( nitem );
138 Deletes object with all children
139 \param p - SUIT object
141 void SalomeApp_DataModelSync::deleteItemWithChildren( const suitPtr& p ) const
148 DataObjectList::const_iterator anIt = ch.begin(), aLast = ch.end();
149 for( ; anIt!=aLast; anIt++ )
150 deleteItemWithChildren( *anIt );
155 \return true if objects correspond each other at all
156 \param p - kernel object
157 \param q - suit object
159 bool SalomeApp_DataModelSync::isEqual( const kerPtr& p, const suitPtr& q ) const
161 LightApp_ModuleObject* lobj = dynamic_cast<LightApp_ModuleObject*>( q );
162 SalomeApp_DataObject* sobj = dynamic_cast<SalomeApp_DataObject*>( q );
163 _PTR( SComponent ) aComp( p );
164 bool res = ( !p && !q ) ||
165 ( lobj && !sobj && aComp ) ||
166 ( sobj && isCorrect( p ) && p->GetID().c_str()==sobj->entry() );
171 \return null kernel object
173 kerPtr SalomeApp_DataModelSync::nullSrc() const
179 \return null suit object
181 suitPtr SalomeApp_DataModelSync::nullTrg() const
187 Fills list with children of kernel object
188 \param obj - kernel object
189 \param ch - list to be filled
191 void SalomeApp_DataModelSync::children( const kerPtr& obj, QValueList<kerPtr>& ch ) const
194 _PTR(ChildIterator) it ( myStudy->NewChildIterator( obj ) );
195 for( ; it->More(); it->Next() )
196 ch.append( it->Value() );
200 Fills list with children of SUIT object
201 \param p - SUIT object
202 \param ch - list to be filled
204 void SalomeApp_DataModelSync::children( const suitPtr& p, QValueList<suitPtr>& ch ) const
211 for( SUIT_DataObject* o = l.first(); o; o = l.next() )
217 \return parent of SUIT object
218 \param p - SUIT object
220 suitPtr SalomeApp_DataModelSync::parent( const suitPtr& p ) const
222 return p ? p->parent(): 0;
227 \param p - SUIT object
229 void SalomeApp_DataModelSync::updateItem( const kerPtr& obj, const suitPtr& ) const
234 Auxiliary function, shows SUIT tree
236 void showTree( SUIT_DataObject* root )
238 qDebug( root ? "<tree>" : "<empty tree>" );
242 SUIT_DataObjectIterator it( root, SUIT_DataObjectIterator::DepthLeft );
243 for( ; it.current(); ++it )
245 QString marg; marg.fill( ' ', 3*it.depth() );
246 QString nnn = "%1 '%2'";
247 qDebug( nnn.arg( marg ).arg( it.current()->name() ) );
254 SalomeApp_DataModel::SalomeApp_DataModel( CAM_Module* theModule )
255 : LightApp_DataModel( theModule )
262 SalomeApp_DataModel::~SalomeApp_DataModel()
269 bool SalomeApp_DataModel::open( const QString& name, CAM_Study* study, QStringList )
271 SalomeApp_Study* aDoc = dynamic_cast<SalomeApp_Study*>( study );
275 QString anId = getRootEntry( aDoc );
276 if ( anId.isEmpty() )
277 return true; // Probably nothing to load
279 _PTR(Study) aStudy ( aDoc->studyDS() ); // shared_ptr cannot be used here
280 _PTR(SComponent) aSComp ( aStudy->FindComponentID( std::string( anId.latin1() ) ) );
282 updateTree( aSComp, aDoc );
284 QStringList listOfFiles;
285 LightApp_DataModel::open(name, study, listOfFiles);
292 bool SalomeApp_DataModel::create( CAM_Study* theStudy )
294 update(NULL, (LightApp_Study*)theStudy);
301 void SalomeApp_DataModel::update( LightApp_DataObject*, LightApp_Study* study )
303 SalomeApp_Study* aSStudy = dynamic_cast<SalomeApp_Study*>(study);
304 LightApp_RootObject* studyRoot = 0;
305 _PTR(SComponent) sobj;
306 SalomeApp_DataObject* modelRoot = dynamic_cast<SalomeApp_DataObject*>( root() );
307 if ( !modelRoot ){ // not yet connected to a study -> try using <study> argument
309 aSStudy = dynamic_cast<SalomeApp_Study*>( getModule()->getApp()->activeStudy() );
311 studyRoot = dynamic_cast<LightApp_RootObject*>( aSStudy->root() );
312 QString anId = getRootEntry( aSStudy );
313 if ( !anId.isEmpty() ){ // if nothing is published in the study for this module -> do nothing
314 _PTR(Study) aStudy ( aSStudy->studyDS() );
315 sobj = aStudy->FindComponentID( std::string( anId.latin1() ) );
320 studyRoot = dynamic_cast<LightApp_RootObject*>( modelRoot->root() );
322 aSStudy = dynamic_cast<SalomeApp_Study*>( studyRoot->study() ); // <study> value should not change here theoretically, but just to make sure
324 _PTR(Study) aStudy ( aSStudy->studyDS() );
325 // modelRoot->object() cannot be reused here: it is about to be deleted by buildTree() soon
326 sobj = aStudy->FindComponentID( std::string( modelRoot->entry().latin1() ) );
330 if ( sobj && aSStudy )
331 updateTree( sobj, aSStudy );
335 Synchronizes kernel tree and suit data tree starting from component 'sobj'
337 SUIT_DataObject* SalomeApp_DataModel::synchronize( const _PTR( SComponent )& sobj, SalomeApp_Study* study )
339 if( !study || !study->root() || !sobj )
342 DataObjectList ch; study->root()->children( ch );
343 DataObjectList::const_iterator anIt = ch.begin(), aLast = ch.end();
344 SUIT_DataObject* suitObj = 0;
345 for( ; anIt!=aLast; anIt++ )
347 LightApp_DataObject* dobj = dynamic_cast<LightApp_DataObject*>( *anIt );
348 if( dobj && dobj->name() == sobj->GetName().c_str() )
355 SalomeApp_DataModelSync sync( study->studyDS(), study->root() );
357 if( !suitObj || dynamic_cast<SalomeApp_DataObject*>( suitObj ) )
358 return ::synchronize<kerPtr,suitPtr,SalomeApp_DataModelSync>( sobj, suitObj, sync );
366 void SalomeApp_DataModel::updateTree( const _PTR( SComponent )& comp, SalomeApp_Study* study )
368 SalomeApp_ModuleObject* aNewRoot = dynamic_cast<SalomeApp_ModuleObject*>( synchronize( comp, study ) );
371 aNewRoot->setDataModel( this );
379 SalomeApp_Module* SalomeApp_DataModel::getModule() const
381 return dynamic_cast<SalomeApp_Module*>( module() );
387 SalomeApp_Study* SalomeApp_DataModel::getStudy() const
389 if(!root()) return 0;
390 LightApp_RootObject* aRoot = dynamic_cast<LightApp_RootObject*>( root()->root() );
393 SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( aRoot->study() );
400 \return study entry corresponding to this data model
402 QString SalomeApp_DataModel::getRootEntry( SalomeApp_Study* study ) const
405 if ( root() && root()->root() ) { // data model already in a study
406 SalomeApp_DataObject* anObj = dynamic_cast<SalomeApp_DataObject*>( root() );
408 anEntry = anObj->entry();
410 else if ( study && study->studyDS() ) { // this works even if <myRoot> is null
411 _PTR(SComponent) aSComp( study->studyDS()->FindComponent( module()->name() ) );
413 anEntry = aSComp->GetID().c_str();