Salome HOME
Initial version
[modules/gui.git] / src / CAM / CAM_Study.cxx
1 #include "CAM_Study.h"
2
3 #include "CAM_DataModel.h"
4 #include "CAM_DataObject.h"
5 #include "CAM_RootObject.h"
6 #include "CAM_Module.h"
7
8 CAM_Study::CAM_Study( SUIT_Application* app )
9 : SUIT_Study( app )
10 {
11 }
12
13 CAM_Study::~CAM_Study()
14 {
15 }
16
17 void CAM_Study::closeDocument()
18 {
19   for ( ModelListIterator it( myDataModels ); it.current(); ++it )
20     it.current()->close();
21
22   SUIT_Study::closeDocument();
23 }
24
25 bool CAM_Study::appendDataModel( const CAM_DataModel* dm )
26 {
27   return insertDataModel( dm, myDataModels.count() );
28 }
29
30 bool CAM_Study::insertDataModel( const CAM_DataModel* dm, const CAM_DataModel* other )
31 {
32   int idx = myDataModels.findRef( other );
33   return insertDataModel( dm, idx < 0 ? idx : idx + 1 );
34 }
35
36 bool CAM_Study::insertDataModel( const CAM_DataModel* dm, const int idx )
37 {
38   if ( !dm || myDataModels.findRef( dm ) != -1 )
39     return false;
40
41   int pos = idx < 0 ? myDataModels.count() : idx;
42   myDataModels.insert( QMIN( pos, (int)myDataModels.count() ), dm );
43
44   connect( dm, SIGNAL( rootChanged( const CAM_DataModel* ) ), SLOT( updateModelRoot( const CAM_DataModel* ) ) );
45
46   dataModelInserted( dm );
47
48   return true;
49 }
50
51 bool CAM_Study::removeDataModel( const CAM_DataModel* dm )
52 {
53   if ( !dm )
54     return true;
55
56   CAM_RootObject* aModelRoot = dynamic_cast<CAM_RootObject*>( dm->root() );
57   if ( aModelRoot )
58     aModelRoot->setDataModel( 0 );
59
60   return myDataModels.remove( dm );
61 }
62
63 bool CAM_Study::containsDataModel( const CAM_DataModel* dm ) const
64 {
65   return myDataModels.contains( dm );
66 }
67
68 void CAM_Study::dataModels( ModelList& lst ) const
69 {
70   lst.clear();
71   for ( ModelListIterator it( myDataModels ); it.current(); ++it )
72     lst.append( it.current() );
73 }
74
75 void CAM_Study::dataModelInserted( const CAM_DataModel* dModel )
76 {
77   CAM_DataModel* dm = (CAM_DataModel*)dModel;
78
79   if ( isSaved() ) // need to load data model from an exisitng file?
80     openDataModel( studyName(), dm );
81   else // no, just need to update data model's connection to study tree 
82        //(some application may want to show model's root in a study tree even if a model is empty)
83   {
84     dm->create( this );
85     updateModelRoot( dm );
86   }
87 }
88
89 bool CAM_Study::openDataModel( const QString&, CAM_DataModel* )
90 {
91   return false;
92 }
93
94 bool CAM_Study::saveDataModel( const QString&, CAM_DataModel* )
95 {
96   return false;
97 }
98
99 void CAM_Study::updateModelRoot( const CAM_DataModel* dm )
100 {
101   if ( !root() )
102     return;
103
104   DataObjectList childList;
105   root()->children( childList );
106   CAM_DataObject* curRoot = 0;
107   QString aName = dm->root() ? dm->root()->name() : dm->module()->moduleName();
108   int i = 0;
109   for ( int n = childList.count(); i < n; i++ ) {
110     if ( childList.at( i )->name() == aName ) {
111       curRoot = dynamic_cast<CAM_DataObject*>( childList.at( i ) );
112       break;
113     }
114   }
115
116   if ( curRoot == dm->root() )
117     return;
118
119   // replacing old data model root with a new one - old root deleted here !
120   if ( curRoot )
121     root()->replaceChild( curRoot, dm->root(), true );
122   else {
123     int idx = myDataModels.findRef( dm );
124     if ( idx != -1 )
125       root()->insertChild( dm->root(), idx );
126   }
127 }