1 // Copyright (C) 2007-2013 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
23 #include "CAM_Study.h"
25 #include "CAM_DataModel.h"
26 #include "CAM_DataObject.h"
27 #include "CAM_Module.h"
31 \brief Represents document object in the CAM application architecture.
33 For each loaded module study contains the data model instance reference.
34 Provides all necessary functionality for data models management.
39 \param app parent application
41 CAM_Study::CAM_Study( SUIT_Application* app )
49 CAM_Study::~CAM_Study()
54 \brief Called when study is closed.
56 Closes all data models.
58 \param permanently if \c true close study permanently (not used in the base implemetation)
60 void CAM_Study::closeDocument( bool permanently )
62 for( QList<CAM_DataModel*>::const_iterator it = myDataModels.begin();
63 it != myDataModels.end(); ++it )
66 SUIT_Study::closeDocument( permanently );
70 \brief Append data model to the study.
71 \param dm data model being added
72 \return \c true on success and \c false on error
74 bool CAM_Study::appendDataModel( const CAM_DataModel* dm )
76 return insertDataModel( dm, myDataModels.count() );
80 \brief Insert data model \a dm after data model \a other
82 If \a other is 0, the data model is added to the end of list.
84 \param dm data model being added
85 \param other data model to be previous in the list
86 \return \c true on success and \c false on error
88 bool CAM_Study::insertDataModel( const CAM_DataModel* dm, const CAM_DataModel* other )
90 int idx = myDataModels.indexOf( (CAM_DataModel*)other );
91 return insertDataModel( dm, idx < 0 ? idx : idx + 1 );
95 \brief Insert data model \a dm with index \a idx.
97 \param dm data model being added
98 \param idx data model required index
99 \return \c true on success and \c false on error
101 bool CAM_Study::insertDataModel( const CAM_DataModel* dm, const int idx )
103 if ( !dm || myDataModels.indexOf( (CAM_DataModel*)dm ) != -1 )
106 int pos = idx < 0 ? myDataModels.count() : idx;
107 myDataModels.insert( qMin( pos, (int)myDataModels.count() ), (CAM_DataModel*)dm );
109 connect( dm, SIGNAL( rootChanged( const CAM_DataModel* ) ), SLOT( updateModelRoot( const CAM_DataModel* ) ) );
111 dataModelInserted( dm );
117 \brief Remove data model from the study.
118 \param dm data model being removed
119 \return \c true on success and \c false on error
121 bool CAM_Study::removeDataModel( const CAM_DataModel* dm )
126 CAM_ModuleObject* aModelRoot = dynamic_cast<CAM_ModuleObject*>( dm->root() );
128 aModelRoot->setDataModel( 0 );
130 return myDataModels.removeAll( (CAM_DataModel*)dm );
134 \brief Check if data model is contained in the list.
136 \return \c true if data model is in the list and \c false otherwise.
138 bool CAM_Study::containsDataModel( const CAM_DataModel* dm ) const
140 return myDataModels.contains( (CAM_DataModel*)dm );
144 \brief Get all data models.
145 \param lst returning list of data model.
147 void CAM_Study::dataModels( ModelList& lst ) const
150 for( QList<CAM_DataModel*>::const_iterator it = myDataModels.begin();
151 it != myDataModels.end(); ++it )
156 \brief Called when data model is inserted in the study.
158 Open data model \a dModel, if it is saved and update data tree.
160 \param dModel data model
162 void CAM_Study::dataModelInserted( const CAM_DataModel* dModel )
164 CAM_DataModel* dm = (CAM_DataModel*)dModel;
166 if ( isSaved() ) // need to load data model from an exisitng file?
167 openDataModel( studyName(), dm );
168 else // no, just need to update data model's connection to study tree
169 //(some application may want to show model's root in a study tree even if a model is empty)
171 updateModelRoot( dm );
175 \brief Called when data model is opened.
177 Base implementation does nothing and returns \c false.
179 \return \c true on success and \c false on error
181 bool CAM_Study::openDataModel( const QString&, CAM_DataModel* )
187 \brief Called when data model is saved.
189 Base implementation does nothing and returns \c false.
191 \return \c true on success and \c false on error
193 bool CAM_Study::saveDataModel( const QString&, CAM_DataModel* )
199 \brief Update data model root object.
200 \param dm data model being updated.
202 void CAM_Study::updateModelRoot( const CAM_DataModel* dm )
207 DataObjectList childList;
208 root()->children( childList );
209 CAM_DataObject* curRoot = 0;
210 QString aName = dm->root() ? dm->root()->name() : dm->module()->moduleName();
212 for ( int n = childList.count(); i < n; i++ ) {
213 if ( childList.at( i )->name() == aName ) {
214 curRoot = dynamic_cast<CAM_DataObject*>( childList.at( i ) );
219 if ( curRoot == dm->root() )
222 // replacing old data model root with a new one - old root deleted here !
224 root()->replaceChild( curRoot, dm->root(), true );
226 int idx = myDataModels.indexOf( (CAM_DataModel*)dm );
228 root()->insertChild( dm->root(), idx );