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/
19 #include "CAM_Study.h"
21 #include "CAM_DataModel.h"
22 #include "CAM_DataObject.h"
23 #include "CAM_RootObject.h"
24 #include "CAM_Module.h"
27 CAM_Study::CAM_Study( SUIT_Application* app )
33 CAM_Study::~CAM_Study()
37 /*!Closing all data models and close document permanently(if \a permanently = true.)
38 * \param permanently - flag
40 void CAM_Study::closeDocument(bool permanently)
42 for ( ModelListIterator it( myDataModels ); it.current(); ++it )
43 it.current()->close();
45 SUIT_Study::closeDocument(permanently);
48 /*!Append data model to list.
49 * \param dm - data model for adding
51 bool CAM_Study::appendDataModel( const CAM_DataModel* dm )
53 return insertDataModel( dm, myDataModels.count() );
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
60 bool CAM_Study::insertDataModel( const CAM_DataModel* dm, const CAM_DataModel* other )
62 int idx = myDataModels.findRef( other );
63 return insertDataModel( dm, idx < 0 ? idx : idx + 1 );
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.
71 bool CAM_Study::insertDataModel( const CAM_DataModel* dm, const int idx )
73 if ( !dm || myDataModels.findRef( dm ) != -1 )
76 int pos = idx < 0 ? myDataModels.count() : idx;
77 myDataModels.insert( QMIN( pos, (int)myDataModels.count() ), dm );
79 connect( dm, SIGNAL( rootChanged( const CAM_DataModel* ) ), SLOT( updateModelRoot( const CAM_DataModel* ) ) );
81 dataModelInserted( dm );
86 /*! Remove data model from list
87 * \param dm data model
88 * \retval true - if all ok, else false.
90 bool CAM_Study::removeDataModel( const CAM_DataModel* dm )
95 CAM_RootObject* aModelRoot = dynamic_cast<CAM_RootObject*>( dm->root() );
97 aModelRoot->setDataModel( 0 );
99 return myDataModels.remove( dm );
102 /*!Check data model contains in list.
103 * \param dm - data model
104 * \retval true - if data model in list, else false.
106 bool CAM_Study::containsDataModel( const CAM_DataModel* dm ) const
108 return myDataModels.contains( dm );
111 /*!Gets list of all data models.
112 * \param lst - output data model list.
114 void CAM_Study::dataModels( ModelList& lst ) const
117 for ( ModelListIterator it( myDataModels ); it.current(); ++it )
118 lst.append( it.current() );
121 /*! Open data model \a dModel, if it saved*/
122 void CAM_Study::dataModelInserted( const CAM_DataModel* dModel )
124 CAM_DataModel* dm = (CAM_DataModel*)dModel;
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)
132 updateModelRoot( dm );
137 bool CAM_Study::openDataModel( const QString&, CAM_DataModel* )
143 bool CAM_Study::saveDataModel( const QString&, CAM_DataModel* )
148 /*! Public slot. Update model root.*/
149 void CAM_Study::updateModelRoot( const CAM_DataModel* dm )
154 DataObjectList childList;
155 root()->children( childList );
156 CAM_DataObject* curRoot = 0;
157 QString aName = dm->root() ? dm->root()->name() : dm->module()->moduleName();
159 for ( int n = childList.count(); i < n; i++ ) {
160 if ( childList.at( i )->name() == aName ) {
161 curRoot = dynamic_cast<CAM_DataObject*>( childList.at( i ) );
166 if ( curRoot == dm->root() )
169 // replacing old data model root with a new one - old root deleted here !
171 root()->replaceChild( curRoot, dm->root(), true );
173 int idx = myDataModels.findRef( dm );
175 root()->insertChild( dm->root(), idx );