Back to index

salome-gui  6.5.0
CAM_Study.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 #include "CAM_Study.h"
00024 
00025 #include "CAM_DataModel.h"
00026 #include "CAM_DataObject.h"
00027 #include "CAM_Module.h"
00028 
00041 CAM_Study::CAM_Study( SUIT_Application* app )
00042 : SUIT_Study( app )
00043 {
00044 }
00045 
00049 CAM_Study::~CAM_Study()
00050 {
00051 }
00052 
00060 void CAM_Study::closeDocument( bool permanently )
00061 {
00062   for( QList<CAM_DataModel*>::const_iterator it = myDataModels.begin(); 
00063        it != myDataModels.end(); ++it )
00064     (*it)->close();
00065 
00066   SUIT_Study::closeDocument( permanently );
00067 }
00068 
00074 bool CAM_Study::appendDataModel( const CAM_DataModel* dm )
00075 {
00076   return insertDataModel( dm, myDataModels.count() );
00077 }
00078 
00088 bool CAM_Study::insertDataModel( const CAM_DataModel* dm, const CAM_DataModel* other )
00089 {
00090   int idx = myDataModels.indexOf( (CAM_DataModel*)other );
00091   return insertDataModel( dm, idx < 0 ? idx : idx + 1 );
00092 }
00093 
00101 bool CAM_Study::insertDataModel( const CAM_DataModel* dm, const int idx )
00102 {
00103   if ( !dm || myDataModels.indexOf( (CAM_DataModel*)dm ) != -1 )
00104     return false;
00105 
00106   int pos = idx < 0 ? myDataModels.count() : idx;
00107   myDataModels.insert( qMin( pos, (int)myDataModels.count() ), (CAM_DataModel*)dm );
00108 
00109   connect( dm, SIGNAL( rootChanged( const CAM_DataModel* ) ), SLOT( updateModelRoot( const CAM_DataModel* ) ) );
00110 
00111   dataModelInserted( dm );
00112 
00113   return true;
00114 }
00115 
00121 bool CAM_Study::removeDataModel( const CAM_DataModel* dm )
00122 {
00123   if ( !dm )
00124     return true;
00125 
00126   CAM_ModuleObject* aModelRoot = dynamic_cast<CAM_ModuleObject*>( dm->root() );
00127   if ( aModelRoot )
00128     aModelRoot->setDataModel( 0 );
00129 
00130   return myDataModels.removeAll( (CAM_DataModel*)dm );
00131 }
00132 
00138 bool CAM_Study::containsDataModel( const CAM_DataModel* dm ) const
00139 {
00140   return myDataModels.contains( (CAM_DataModel*)dm );
00141 }
00142 
00147 void CAM_Study::dataModels( ModelList& lst ) const
00148 {
00149   lst.clear();
00150   for( QList<CAM_DataModel*>::const_iterator it = myDataModels.begin(); 
00151        it != myDataModels.end(); ++it )
00152     lst.append( *it );
00153 }
00154 
00162 void CAM_Study::dataModelInserted( const CAM_DataModel* dModel )
00163 {
00164   CAM_DataModel* dm = (CAM_DataModel*)dModel;
00165 
00166   if ( isSaved() ) // need to load data model from an exisitng file?
00167     openDataModel( studyName(), dm );
00168   else // no, just need to update data model's connection to study tree 
00169        //(some application may want to show model's root in a study tree even if a model is empty)
00170     dm->create( this );
00171   updateModelRoot( dm );
00172 }
00173 
00181 bool CAM_Study::openDataModel( const QString&, CAM_DataModel* )
00182 {
00183   return false;
00184 }
00185 
00193 bool CAM_Study::saveDataModel( const QString&, CAM_DataModel* )
00194 {
00195   return false;
00196 }
00197 
00202 void CAM_Study::updateModelRoot( const CAM_DataModel* dm )
00203 {
00204   if ( !root() )
00205     return;
00206 
00207   DataObjectList childList;
00208   root()->children( childList );
00209   CAM_DataObject* curRoot = 0;
00210   QString aName = dm->root() ? dm->root()->name() : dm->module()->moduleName();
00211   int i = 0;
00212   for ( int n = childList.count(); i < n; i++ ) {
00213     if ( childList.at( i )->name() == aName ) {
00214       curRoot = dynamic_cast<CAM_DataObject*>( childList.at( i ) );
00215       break;
00216     }
00217   }
00218 
00219   if ( curRoot == dm->root() )
00220     return;
00221 
00222   // replacing old data model root with a new one - old root deleted here !
00223   if ( curRoot )
00224     root()->replaceChild( curRoot, dm->root(), true );
00225   else {
00226     int idx = myDataModels.indexOf( (CAM_DataModel*)dm );
00227     if ( idx != -1 )
00228       root()->insertChild( dm->root(), idx );
00229   }
00230 }