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