Salome HOME
421bb457a469962d82ad5ea4846642cec0b7b290
[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 /*!Constructor.*/
9 CAM_Study::CAM_Study( SUIT_Application* app )
10 : SUIT_Study( app )
11 {
12 }
13
14 /*!Destructor*/
15 CAM_Study::~CAM_Study()
16 {
17 }
18
19 /*!Closing all data models and close document permanently(if \a permanently = true.)
20  * \param permanently - flag
21  */
22 void CAM_Study::closeDocument(bool permanently)
23 {
24   for ( ModelListIterator it( myDataModels ); it.current(); ++it )
25     it.current()->close();
26
27   SUIT_Study::closeDocument(permanently);
28 }
29
30 /*!Append data model to list.
31  * \param dm - data model for adding
32  */
33 bool CAM_Study::appendDataModel( const CAM_DataModel* dm )
34 {
35   return insertDataModel( dm, myDataModels.count() );
36 }
37
38 /*!Insert data model \a dm after \a other
39  * \param dm - data model for adding
40  * \param other - previus data model for \a dm
41  */
42 bool CAM_Study::insertDataModel( const CAM_DataModel* dm, const CAM_DataModel* other )
43 {
44   int idx = myDataModels.findRef( other );
45   return insertDataModel( dm, idx < 0 ? idx : idx + 1 );
46 }
47
48 /*!Insert data model with index \a idx. \n
49  * \param dm - data model
50  * \param idx - index for inserting(must be no less zero)
51  * \retval true - if model added successful, else false.
52  */
53 bool CAM_Study::insertDataModel( const CAM_DataModel* dm, const int idx )
54 {
55   if ( !dm || myDataModels.findRef( dm ) != -1 )
56     return false;
57
58   int pos = idx < 0 ? myDataModels.count() : idx;
59   myDataModels.insert( QMIN( pos, (int)myDataModels.count() ), dm );
60
61   connect( dm, SIGNAL( rootChanged( const CAM_DataModel* ) ), SLOT( updateModelRoot( const CAM_DataModel* ) ) );
62
63   dataModelInserted( dm );
64
65   return true;
66 }
67
68 /*! Remove data model from list
69  * \param dm data model
70  * \retval true - if all ok, else false.
71  */
72 bool CAM_Study::removeDataModel( const CAM_DataModel* dm )
73 {
74   if ( !dm )
75     return true;
76
77   CAM_RootObject* aModelRoot = dynamic_cast<CAM_RootObject*>( dm->root() );
78   if ( aModelRoot )
79     aModelRoot->setDataModel( 0 );
80
81   return myDataModels.remove( dm );
82 }
83
84 /*!Check data model contains in list.
85  * \param dm - data model
86  * \retval true - if data model in list, else false.
87  */
88 bool CAM_Study::containsDataModel( const CAM_DataModel* dm ) const
89 {
90   return myDataModels.contains( dm );
91 }
92
93 /*!Gets list of all data models.
94  * \param lst - output data model list.
95  */
96 void CAM_Study::dataModels( ModelList& lst ) const
97 {
98   lst.clear();
99   for ( ModelListIterator it( myDataModels ); it.current(); ++it )
100     lst.append( it.current() );
101 }
102
103 /*! Open data model \a dModel, if it saved*/
104 void CAM_Study::dataModelInserted( const CAM_DataModel* dModel )
105 {
106   CAM_DataModel* dm = (CAM_DataModel*)dModel;
107
108   if ( isSaved() ) // need to load data model from an exisitng file?
109     openDataModel( studyName(), dm );
110   else // no, just need to update data model's connection to study tree 
111        //(some application may want to show model's root in a study tree even if a model is empty)
112   {
113     dm->create( this );
114     updateModelRoot( dm );
115   }
116 }
117
118 /*! \retval false*/
119 bool CAM_Study::openDataModel( const QString&, CAM_DataModel* )
120 {
121   return false;
122 }
123
124 /*! \retval false*/
125 bool CAM_Study::saveDataModel( const QString&, CAM_DataModel* )
126 {
127   return false;
128 }
129
130 /*! Public slot. Update model root.*/
131 void CAM_Study::updateModelRoot( const CAM_DataModel* dm )
132 {
133   if ( !root() )
134     return;
135
136   DataObjectList childList;
137   root()->children( childList );
138   CAM_DataObject* curRoot = 0;
139   QString aName = dm->root() ? dm->root()->name() : dm->module()->moduleName();
140   int i = 0;
141   for ( int n = childList.count(); i < n; i++ ) {
142     if ( childList.at( i )->name() == aName ) {
143       curRoot = dynamic_cast<CAM_DataObject*>( childList.at( i ) );
144       break;
145     }
146   }
147
148   if ( curRoot == dm->root() )
149     return;
150
151   // replacing old data model root with a new one - old root deleted here !
152   if ( curRoot )
153     root()->replaceChild( curRoot, dm->root(), true );
154   else {
155     int idx = myDataModels.findRef( dm );
156     if ( idx != -1 )
157       root()->insertChild( dm->root(), idx );
158   }
159 }