Back to index

salome-med  6.5.0
MEDGUI.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 //  MED MEDGUI : MED component GUI implemetation
00024 //  File   : MEDGUI.cxx
00025 //  Module : MED
00026 //
00027 #include "MEDGUI.h"
00028 
00029 #include "MEDGUIFileContentDial.h"
00030 #include "MEDGUIDataBaseDockWidget.hxx"
00031 #include "MEDGUISelection.hxx"
00032 
00033 #include <MED_version.h>
00034 
00035 #include "Utils_ORB_INIT.hxx"
00036 #include "Utils_SINGLETON.hxx"
00037 #include "utilities.h"
00038 
00039 #include <SALOME_LifeCycleCORBA.hxx>
00040 #include <SALOME_InteractiveObject.hxx>
00041 #include <SALOME_ListIO.hxx>
00042 #include <SalomeApp_Tools.h>
00043 
00044 #include <SUIT_MessageBox.h>
00045 #include <SUIT_Tools.h>
00046 #include <SUIT_FileDlg.h>
00047 #include <SUIT_ResourceMgr.h>
00048 
00049 #include <CAM_Application.h>
00050 #include <SalomeApp_Application.h>
00051 #include <SalomeApp_DataModel.h>
00052 #include <SalomeApp_Study.h>
00053 #include <SALOMEDSClient_Study.hxx>
00054 #include <SALOMEDSClient_SObject.hxx>
00055 
00056 #include <LightApp_SelectionMgr.h>
00057 
00058 #include <QAction>
00059 #include <QIcon>
00060 #include <QInputDialog>
00061 #include <QLineEdit>
00062 #include <QKeyEvent>
00063 #include <QDockWidget>
00064 
00065 static CORBA::ORB_var   _orb;
00066 
00067 //=============================================================================
00071 //=============================================================================
00072 MedGUI::MedGUI() : LightApp_Module( "MED" ),SalomeApp_Module( "MED" )
00073 {
00074 }
00075 
00076 void MedGUI::createMedAction( const int id, const QString& po_id, const QString& icon_id )
00077 {
00078   QWidget* parent = application()->desktop();
00079   SUIT_ResourceMgr* mgr = application()->resourceMgr();
00080 
00081   QPixmap pix;
00082   QIcon icon;
00083   if( !icon_id.isEmpty() )
00084     pix = mgr->loadPixmap( "MED", tr( (const char*)icon_id.toLatin1() ) );
00085 //   else
00086 //     pix = mgr->loadPixmap( "MED", tr( QString( "ICO_" )+po_id ) );
00087 
00088   if ( !pix.isNull() )
00089     icon = QIcon( pix );
00090 
00091   createAction( id,
00092                 tr( (const char*)("TOP_" + po_id).toLatin1() ),
00093                 icon,
00094                 tr( (const char*)("MEN_" + po_id).toLatin1() ),
00095                 tr( (const char*)("STB_" + po_id).toLatin1() ),
00096                 0,
00097                 parent,
00098                 false,
00099                 this,
00100                 SLOT( onGUIEvent() ) );
00101 
00102   if ( action( id ) )
00103     action( id )->setObjectName( QString( "Action %1" ).arg( id ) );
00104 }
00105 
00106 //=============================================================================
00110 //=============================================================================
00111 void MedGUI::initialize( CAM_Application* app )
00112 {
00113   SalomeApp_Module::initialize( app );
00114 
00115   //QWidget* parent = application()->desktop();
00116 
00117   createMedAction( 931, "MESHSEL", "ICO_TB_MESHSEL" );
00118   createMedAction( 932, "FIELDSEL", "ICO_TB_FIELDSEL" );
00119   createMedAction( 934, "DUMPMESH" );
00120   createMedAction( 935, "DUMPSUBMESH" );
00121   createMedAction( 936, "EXPLORE", "ICO_TB_EXPLORE");
00122 
00123   createMedAction( 4031, "MESHSEL", "ICO_TB_MESHSEL" );
00124   createMedAction( 4032, "FIELDSEL", "ICO_TB_FIELDSEL" );
00125   createMedAction( 4034, "EXPLORE", "ICO_TB_EXPLORE");
00126 
00127   int MedId = createMenu( tr( "MED" ), -1, 50, 10 );
00128   createMenu( separator(), MedId, 10 );
00129   createMenu( 931, MedId, 11 );
00130   createMenu( 932, MedId, 11 );
00131   createMenu( 934, MedId, 11 );
00132   createMenu( 935, MedId, 11 );
00133   createMenu( 936, MedId, 11 );
00134 
00135   int medTb = createTool( tr( "TB_MED" ) );
00136   createTool( 4031, medTb );
00137   createTool( 4032, medTb );
00138   createTool( 4034, medTb );
00139 
00140   _data_base = new MEDGUIDataBaseDockWidget(application(),application()->desktop());
00141   application()->desktop()->addDockWidget(Qt::LeftDockWidgetArea,_data_base);
00142 }
00143 
00144 QString MedGUI::engineIOR() const
00145 {
00146   QString anIOR( "" );
00147   SALOME_MED::MED_Gen_ptr aMedGen = InitMedGen();
00148   if ( !CORBA::is_nil( aMedGen) )
00149   {
00150     CORBA::String_var objStr = getApp()->orb()->object_to_string( aMedGen );
00151     anIOR = QString( objStr.in() );
00152   }
00153   return anIOR;
00154 }
00155 
00156 void MedGUI::windows( QMap<int, int>& mappa ) const
00157 {
00158   mappa.clear();
00159   mappa.insert( SalomeApp_Application::WT_ObjectBrowser, Qt::LeftDockWidgetArea );
00160   mappa.insert( SalomeApp_Application::WT_PyConsole, Qt::BottomDockWidgetArea );
00161 }
00162 
00163 //=============================================================================
00167 //=============================================================================
00168 #include <QMetaObject>
00169 #include <QtxAction.h>
00170 void MedGUI::onGUIEvent()
00171 {
00172   const QtxAction* obj = qobject_cast<const QtxAction*>(sender());
00173   if ( !obj )
00174     return;
00175   int id = actionId( obj );
00176   if ( id != -1 )
00177     OnGUIEvent( id );
00178 }
00179 
00180 //=============================================================================
00184 //=============================================================================
00185 void MedGUI::EmitSignalCloseAllDialogs()
00186 {
00187   emit SignalCloseAllDialogs();
00188 }
00189 
00190 //=============================================================================
00194 //=============================================================================
00195 bool MedGUI::deactivateModule( SUIT_Study* study )
00196 {
00197   setMenuShown( false );
00198   setToolShown( false );
00199 
00200   _data_base->setVisible( false );
00201   _data_base->toggleViewAction()->setVisible( false );
00202 
00203   disconnect( application()->desktop(), SIGNAL( windowActivated( SUIT_ViewWindow* ) ),
00204              this, SLOT( onWindowActivated( SUIT_ViewWindow* ) ) );
00205 
00206   EmitSignalCloseAllDialogs();
00207 
00208   return SalomeApp_Module::deactivateModule( study );
00209 }
00210 
00211 //=============================================================================
00215 //=============================================================================
00216 bool MedGUI::activateModule( SUIT_Study* study )
00217 {
00218   bool res = SalomeApp_Module::activateModule( study );
00219 
00220   setMenuShown( true );
00221   setToolShown( true );
00222 
00223   _data_base->setVisible( true );
00224   _data_base->toggleViewAction()->setVisible( true );
00225 
00226   connect( application()->desktop(), SIGNAL( windowActivated( SUIT_ViewWindow* ) ),
00227           this, SLOT( onWindowActivated( SUIT_ViewWindow* ) ) );
00228   return res;
00229 }
00230 
00231 //=============================================================================
00235 //=============================================================================
00236 void MedGUI::onWindowActivated( SUIT_ViewWindow* )
00237 {
00238 }
00239 
00240 //=============================================================================
00244 //=============================================================================
00245 bool MedGUI::OnGUIEvent (int theCommandID)
00246 {
00247   setOrb();
00248 
00249   SalomeApp_Study* myActiveStudy = dynamic_cast< SalomeApp_Study* >( application()->activeStudy() );
00250   if( !myActiveStudy )
00251     return false;
00252 
00253   _PTR(Study) aStudy = myActiveStudy->studyDS();
00254   //SALOME_NamingService* myNameService = parent->getNameService();
00255 
00256   QString file;
00257   QStringList filtersList ;
00258 
00259   filtersList.append( tr("MED_MEN_IMPORT_MED") );
00260   filtersList.append( tr("MED_MEN_ALL_FILES") ) ;
00261 
00262   SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( application() );
00263   if( !app )
00264     return false;
00265 
00266   switch (theCommandID)
00267     {
00268     case 4031:
00269     case 9031:
00270     case 931:
00271       {
00272         MESSAGE("command " << theCommandID << " activated");
00273 
00274         QString myStudyName = myActiveStudy->studyName();
00275         bool ok=FALSE;
00276 //      int myStudyId = myActiveStudy->id();
00277 
00278         // load MED engine
00279         SALOME_MED::MED_Gen_ptr medgen = InitMedGen();
00280 
00281         // Selection du Fichier
00282         file = SUIT_FileDlg::getFileName(application()->desktop(),
00283                                         "",
00284                                         filtersList,
00285                                         tr("MED_MEN_IMPORT"),
00286                                         true);
00287 
00288         // Selection du Maillage
00289         if (!file.isEmpty() )
00290           {
00291             SCRUTE((const char*)file.toLatin1());
00292             QString meshName;
00293             meshName = QInputDialog::getText( application()->desktop(),
00294                                               tr("MED_INF_MESHNAME"),
00295                                               QString::null,
00296                                               QLineEdit::Normal,
00297                                               QString::null,
00298                                               &ok );
00299             if ( ok && !meshName.isEmpty() )
00300               {
00301                   try
00302                     {
00303                       medgen->readMeshInFile( (const char*)file.toLatin1(),
00304                                               (const char*)myStudyName.toLatin1(),
00305                                               (const char*)meshName.toLatin1() );
00306                       if (myActiveStudy->studyDS()->GetProperties()->IsLocked()) {
00307                         SUIT_MessageBox::warning ( application()->desktop(),
00308                                                    tr("WRN_WARNING"),
00309                                                    tr("WRN_STUDY_LOCKED") );
00310                         //QObject::tr("BUT_OK")); by default
00311                       }
00312                     }
00313                   catch (const SALOME::SALOME_Exception & S_ex)
00314                     {
00315                       SalomeApp_Tools::QtCatchCorbaException(S_ex);
00316                     }
00317                 }
00318                 updateObjBrowser();
00319           }
00320         break;
00321       }
00322     case 4032:
00323     case 9032:
00324     case 932:
00325       {
00326         MESSAGE("command " << theCommandID << " activated");
00327 
00328         QString myStudyName = myActiveStudy->studyName();
00329         bool ok=FALSE;
00330 //      int myStudyId = myActiveStudy->id();
00331 
00332         // load MED engine
00333         SALOME_MED::MED_Gen_ptr medgen = InitMedGen();
00334 
00335         // Selection du Fichier
00336         QString anInitialPath = "";
00337         if ( SUIT_FileDlg::getLastVisitedPath().isEmpty() )
00338           anInitialPath = QDir::currentPath();
00339 
00340         file = SUIT_FileDlg::getFileName(application()->desktop(),
00341                                         anInitialPath,
00342                                         filtersList,
00343                                         tr("MED_MEN_IMPORT"),
00344                                         true);
00345 
00346         // Selection du Maillage
00347         if (!file.isEmpty() )
00348           {
00349             SCRUTE((const char*)file.toLatin1());
00350             QString fieldName;
00351             fieldName = QInputDialog::getText( application()->desktop(),
00352                                                tr("MED_INF_FIELDNAME"),
00353                                                QString::null,
00354                                                QLineEdit::Normal,
00355                                                QString::null,
00356                                                &ok );
00357             if ( ok && !fieldName.isEmpty())
00358               {
00359                 try
00360                   {
00361                     medgen->readFieldInFile( (const char*)file.toLatin1(),
00362                                              (const char*)myStudyName.toLatin1(),
00363                                              (const char*)fieldName.toLatin1(),
00364                                              -1,
00365                                              -1 );
00366                     if (myActiveStudy->studyDS()->GetProperties()->IsLocked()) {
00367                       SUIT_MessageBox::warning ( application()->desktop(),
00368                                                  tr("WRN_WARNING"),
00369                                                  tr("WRN_STUDY_LOCKED") );
00370                       //tr("BUT_OK")); by default
00371                     }
00372                   }
00373                 catch (const SALOME::SALOME_Exception & S_ex)
00374                   {
00375                     SalomeApp_Tools::QtCatchCorbaException(S_ex);
00376                   }
00377                 updateObjBrowser ();
00378               }
00379           }
00380         break;
00381       }
00382     case 934:
00383       {
00384         //Handle(SMESH_TypeFilter) aMeshFilter = new SMESH_TypeFilter( MESH );
00385 
00386         SALOME_ListIO list;
00387         SalomeApp_Application* app = getApp();
00388         LightApp_SelectionMgr* mgr = app ? app->selectionMgr() : NULL;
00389         if( mgr )
00390           mgr->selectedObjects( list );
00391         //Sel->AddFilter(aMeshFilter) ;
00392 
00393         int nbSel = list.Extent();
00394         if ( nbSel == 1 )
00395           {
00396             //      SMESH::SMESH_Mesh_var aM;
00397             SALOME_MED::MESH_var aMesh;
00398             Handle(SALOME_InteractiveObject) IObject = list.First();
00399             if ( IObject->hasEntry() )
00400               {
00401                 _PTR(SObject) aMorSM = aStudy->FindObjectID( IObject->getEntry() );
00402                 if ( aMorSM )
00403                   {
00404                     _PTR(GenericAttribute) anAttr;
00405                     _PTR(AttributeIOR)     anIOR;
00406                     if (aMorSM->FindAttribute(anAttr, "AttributeIOR"))
00407                       {
00408                         anIOR = anAttr;
00409                         aMesh = SALOME_MED::MESH::_narrow( _orb->string_to_object(anIOR->Value().c_str()) );
00410                         if ( aMesh->_is_nil() )
00411                         {
00412                           SUIT_MessageBox::warning
00413                             ( application()->desktop(),
00414                               tr ("MED_WRN_WARNING"),
00415                               tr ("MED_INF_NOTIMPL") );
00416                           break;
00417                         }
00418                         DumpMesh( aMesh );
00419                         //Sel->ClearFilters() ;
00420                       }
00421                     else
00422                       {
00423                         SUIT_MessageBox::warning
00424                           ( application()->desktop(),
00425                             tr ("MED_WRN_WARNING"),
00426                             tr ("MED_INF_NOIOR") );
00427                         //tr ("MED_BUT_OK") ); by default
00428                         break;
00429                       }
00430                   }
00431               }
00432           }
00433         break;
00434       }
00435 
00436     case 935:
00437       {
00438         //Handle(SMESH_TypeFilter) aSubMeshFilter = new SMESH_TypeFilter( SUBMESH );
00439 
00440         SALOME_ListIO list;
00441         SalomeApp_Application* app = getApp();
00442         LightApp_SelectionMgr* mgr = app ? app->selectionMgr() : NULL;
00443         if( mgr )
00444           mgr->selectedObjects( list );
00445 
00446         //Sel->AddFilter(aSubMeshFilter) ;
00447 
00448         int nbSel = list.Extent();
00449         if ( nbSel == 1 )
00450           {
00451             //      SMESH::SMESH_subMesh_var aSubM;
00452             SALOME_MED::FAMILY_var aFam;
00453             Handle(SALOME_InteractiveObject) IObject = list.First();
00454             if ( IObject->hasEntry() )
00455               {
00456                 _PTR(SObject) aMorSM = aStudy->FindObjectID( IObject->getEntry() );
00457                 if ( aMorSM )
00458                   {
00459                     _PTR(GenericAttribute) anAttr;
00460                     _PTR(AttributeIOR)     anIOR;
00461                     if (aMorSM->FindAttribute(anAttr, "AttributeIOR"))
00462                       {
00463                         anIOR = anAttr;
00464                       }
00465                     else
00466                       {
00467                         SUIT_MessageBox::warning
00468                           ( application()->desktop(),
00469                             tr ("MED_WRN_WARNING"),
00470                             tr ("MED_INFNOIOR") );
00471                         //  tr ("MED_BUT_OK") ); by default
00472                         break;
00473                       }
00474                   }
00475               }
00476           }
00477         break;
00478       }
00479       case 936 :
00480       case 4034 :
00481         {
00482           
00483           MEDGUIFileContentDial* mfcd = new MEDGUIFileContentDial(_data_base, application()->desktop());
00484           mfcd->show();
00485           break;
00486         }
00487     }
00488 
00489   app->updateActions(); //SRN: To update a Save button in the toolbar
00490 
00491   return true;
00492 }
00493 
00494 
00495 //=============================================================================
00499 //=============================================================================
00500 bool MedGUI::OnMousePress (QMouseEvent* pe ,
00501                            SUIT_ViewWindow* wnd )
00502 {
00503   MESSAGE("MedGUI::OnMousePress");
00504   return true;
00505 }
00506 
00507 //=============================================================================
00511 //=============================================================================
00512 bool MedGUI::OnMouseMove (QMouseEvent* pe ,
00513                           SUIT_ViewWindow* wnd )
00514 {
00515   //   MESSAGE("MedGUI::OnMouseMouve");
00516   return true;
00517 }
00518 
00519 //=============================================================================
00523 //=============================================================================
00524 bool MedGUI::OnKeyPress (QKeyEvent* pe,
00525                          SUIT_ViewWindow* wnd)
00526 {
00527   MESSAGE("MedGUI::OnKeyPress");
00528 
00529   return true;
00530 }
00531 
00532 //=============================================================================
00536 //=============================================================================
00537 bool MedGUI::DumpMesh( SALOME_MED::MESH_var MEDMesh)
00538 {
00539 
00540   if ( MEDMesh->_is_nil() )
00541     {
00542       return false;
00543     }
00544 
00545   std::string name = MEDMesh->getName();
00546   SCRUTE(name);
00547 
00548   int dim2 = MEDMesh->getSpaceDimension();
00549   SCRUTE(dim2);
00550 
00551   int k = MEDMesh->getNumberOfNodes() ;
00552   SCRUTE(k);
00553   SALOME_TYPES::ListOfDouble_var coords = MEDMesh->getCoordinates( SALOME_MED::MED_FULL_INTERLACE );
00554   int i = 0;
00555   int lu = 0;
00556   while (lu < k ) {
00557     if (dim2==3)
00558       {
00559         MESSAGE ( " Coordinates  X = " << coords[i] << " Y = " << coords[i+1] << " Z = " << coords[i+2] );
00560         i = i + 3;
00561       }
00562     else
00563       {
00564         MESSAGE ( " Coordinates  X = " << coords[i] << " Y = " << coords[i+1] );
00565         i = i + 2;
00566       }
00567     lu=lu+1;
00568   }
00569 
00570   int nbfam=MEDMesh->getNumberOfFamilies(SALOME_MED::MED_NODE);
00571   SCRUTE(nbfam);
00572   SALOME_MED::Family_array_var Families=MEDMesh->getFamilies(SALOME_MED::MED_NODE) ;
00573 
00574   for (k=0;  k < nbfam; k++) {
00575     SCRUTE(k);
00576     std::string nomFam=Families[k]->getName();
00577     SCRUTE(nomFam);
00578     SALOME_TYPES::ListOfLong_var tabnoeuds=Families[k]->getNumber(SALOME_MED::MED_NONE);
00579     for (int l=0;l<(int)tabnoeuds->length();l++)
00580       SCRUTE(tabnoeuds[l]);
00581   }
00582 
00583   return true;
00584 }
00585 
00586 //=============================================================================
00590 //=============================================================================
00591 bool MedGUI::DumpSubMesh( SALOME_MED::FAMILY_var Fam )
00592 {
00593 
00594   if ( Fam->_is_nil() )
00595     return false;
00596 
00597   SALOME_TYPES::ListOfLong_var tabnoeuds=Fam->getNumber(SALOME_MED::MED_NONE);
00598   for (int l=0;l<(int)tabnoeuds->length();l++)
00599     SCRUTE(tabnoeuds[l]);
00600 
00601   return true;
00602 }
00603 
00604 //=============================================================================
00608 //=============================================================================
00609 SALOME_MED::MED_Gen_ptr MedGUI::InitMedGen() const
00610 {
00611   SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( application() );
00612   Engines::EngineComponent_var comp =
00613     SALOME_LifeCycleCORBA(app->namingService()).FindOrLoad_Component( "FactoryServer", "MED" );
00614 
00615   MESSAGE("_________________________________________");
00616   SALOME_MED::MED_Gen_var clr = SALOME_MED::MED_Gen::_narrow(comp);
00617   ASSERT(!CORBA::is_nil(clr));
00618   return clr._retn();
00619 }
00620 
00621 //=============================================================================
00625 //=============================================================================
00626 void MedGUI::setOrb()
00627 {
00628   try {
00629     ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance();
00630     ASSERT(SINGLETON_<ORB_INIT>::IsAlreadyExisting());
00631     _orb = init( 0 , 0 );
00632   } catch (...) {
00633     INFOS("internal error : orb not found");
00634     _orb = 0;
00635   }
00636   ASSERT(! CORBA::is_nil(_orb));
00637 }
00638 
00639 extern "C" {
00640   Standard_EXPORT CAM_Module* createModule() {
00641     return new MedGUI();
00642   }
00643   
00644   Standard_EXPORT char* getModuleVersion() {
00645       return (char*)MED_VERSION_STR;
00646   }
00647 }