Salome HOME
Merge from V6_main 01/04/2013
[modules/med.git] / src / MEDGUI / MEDGUI.cxx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 //  MED MEDGUI : MED component GUI implemetation
24 //  File   : MEDGUI.cxx
25 //  Module : MED
26 //
27 #include "MEDGUI.h"
28
29 #include "MEDGUIFileContentDial.h"
30 #include "MEDGUIDataBaseDockWidget.hxx"
31 #include "MEDGUISelection.hxx"
32
33 #include <MED_version.h>
34
35 #include "Utils_ORB_INIT.hxx"
36 #include "Utils_SINGLETON.hxx"
37 #include "utilities.h"
38
39 #include <SALOME_LifeCycleCORBA.hxx>
40 #include <SALOME_InteractiveObject.hxx>
41 #include <SALOME_ListIO.hxx>
42 #include <SalomeApp_Tools.h>
43
44 #include <SUIT_MessageBox.h>
45 #include <SUIT_Tools.h>
46 #include <SUIT_FileDlg.h>
47 #include <SUIT_ResourceMgr.h>
48
49 #include <CAM_Application.h>
50 #include <SalomeApp_Application.h>
51 #include <SalomeApp_DataModel.h>
52 #include <SalomeApp_Study.h>
53 #include <SALOMEDSClient_Study.hxx>
54 #include <SALOMEDSClient_SObject.hxx>
55
56 #include <LightApp_SelectionMgr.h>
57
58 #include <QAction>
59 #include <QIcon>
60 #include <QInputDialog>
61 #include <QLineEdit>
62 #include <QKeyEvent>
63 #include <QDockWidget>
64
65 static CORBA::ORB_var   _orb;
66
67 //=============================================================================
68 /*!
69  *
70  */
71 //=============================================================================
72 MedGUI::MedGUI() : SalomeApp_Module( "MED" )
73 {
74 }
75
76 void MedGUI::createMedAction( const int id, const QString& po_id, const QString& icon_id )
77 {
78   QWidget* parent = application()->desktop();
79   SUIT_ResourceMgr* mgr = application()->resourceMgr();
80
81   QPixmap pix;
82   QIcon icon;
83   if( !icon_id.isEmpty() )
84     pix = mgr->loadPixmap( "MED", tr( (const char*)icon_id.toLatin1() ) );
85 //   else
86 //     pix = mgr->loadPixmap( "MED", tr( QString( "ICO_" )+po_id ) );
87
88   if ( !pix.isNull() )
89     icon = QIcon( pix );
90
91   createAction( id,
92                 tr( (const char*)("TOP_" + po_id).toLatin1() ),
93                 icon,
94                 tr( (const char*)("MEN_" + po_id).toLatin1() ),
95                 tr( (const char*)("STB_" + po_id).toLatin1() ),
96                 0,
97                 parent,
98                 false,
99                 this,
100                 SLOT( onGUIEvent() ) );
101
102   if ( action( id ) )
103     action( id )->setObjectName( QString( "Action %1" ).arg( id ) );
104 }
105
106 //=============================================================================
107 /*!
108  *
109  */
110 //=============================================================================
111 void MedGUI::initialize( CAM_Application* app )
112 {
113   SalomeApp_Module::initialize( app );
114
115   //QWidget* parent = application()->desktop();
116
117   createMedAction( 931, "MESHSEL", "ICO_TB_MESHSEL" );
118   createMedAction( 932, "FIELDSEL", "ICO_TB_FIELDSEL" );
119   createMedAction( 934, "DUMPMESH" );
120   createMedAction( 935, "DUMPSUBMESH" );
121   createMedAction( 936, "EXPLORE", "ICO_TB_EXPLORE");
122
123   createMedAction( 4031, "MESHSEL", "ICO_TB_MESHSEL" );
124   createMedAction( 4032, "FIELDSEL", "ICO_TB_FIELDSEL" );
125   createMedAction( 4034, "EXPLORE", "ICO_TB_EXPLORE");
126
127   int MedId = createMenu( tr( "MED" ), -1, 50, 10 );
128   createMenu( separator(), MedId, 10 );
129   createMenu( 931, MedId, 11 );
130   createMenu( 932, MedId, 11 );
131   createMenu( 934, MedId, 11 );
132   createMenu( 935, MedId, 11 );
133   createMenu( 936, MedId, 11 );
134
135   int medTb = createTool( tr( "TB_MED" ) );
136   createTool( 4031, medTb );
137   createTool( 4032, medTb );
138   createTool( 4034, medTb );
139
140   _data_base = new MEDGUIDataBaseDockWidget(application(),application()->desktop());
141   application()->desktop()->addDockWidget(Qt::LeftDockWidgetArea,_data_base);
142 }
143
144 QString MedGUI::engineIOR() const
145 {
146   QString anIOR( "" );
147   SALOME_MED::MED_Gen_ptr aMedGen = InitMedGen();
148   if ( !CORBA::is_nil( aMedGen) )
149   {
150     CORBA::String_var objStr = getApp()->orb()->object_to_string( aMedGen );
151     anIOR = QString( objStr.in() );
152   }
153   return anIOR;
154 }
155
156 void MedGUI::windows( QMap<int, int>& mappa ) const
157 {
158   mappa.clear();
159   mappa.insert( SalomeApp_Application::WT_ObjectBrowser, Qt::LeftDockWidgetArea );
160   mappa.insert( SalomeApp_Application::WT_PyConsole, Qt::BottomDockWidgetArea );
161 }
162
163 //=============================================================================
164 /*!
165  *
166  */
167 //=============================================================================
168 #include <QMetaObject>
169 #include <QtxAction.h>
170 void MedGUI::onGUIEvent()
171 {
172   const QtxAction* obj = qobject_cast<const QtxAction*>(sender());
173   if ( !obj )
174     return;
175   int id = actionId( obj );
176   if ( id != -1 )
177     OnGUIEvent( id );
178 }
179
180 //=============================================================================
181 /*!
182  *
183  */
184 //=============================================================================
185 void MedGUI::EmitSignalCloseAllDialogs()
186 {
187   emit SignalCloseAllDialogs();
188 }
189
190 //=============================================================================
191 /*!
192  *
193  */
194 //=============================================================================
195 bool MedGUI::deactivateModule( SUIT_Study* study )
196 {
197   setMenuShown( false );
198   setToolShown( false );
199
200   _data_base->setVisible( false );
201   _data_base->toggleViewAction()->setVisible( false );
202
203   disconnect( application()->desktop(), SIGNAL( windowActivated( SUIT_ViewWindow* ) ),
204              this, SLOT( onWindowActivated( SUIT_ViewWindow* ) ) );
205
206   EmitSignalCloseAllDialogs();
207
208   return SalomeApp_Module::deactivateModule( study );
209 }
210
211 //=============================================================================
212 /*!
213  *
214  */
215 //=============================================================================
216 bool MedGUI::activateModule( SUIT_Study* study )
217 {
218   bool res = SalomeApp_Module::activateModule( study );
219
220   setMenuShown( true );
221   setToolShown( true );
222
223   _data_base->setVisible( true );
224   _data_base->toggleViewAction()->setVisible( true );
225
226   connect( application()->desktop(), SIGNAL( windowActivated( SUIT_ViewWindow* ) ),
227           this, SLOT( onWindowActivated( SUIT_ViewWindow* ) ) );
228   return res;
229 }
230
231 //=============================================================================
232 /*!
233  *
234  */
235 //=============================================================================
236 void MedGUI::onWindowActivated( SUIT_ViewWindow* )
237 {
238 }
239
240 //=============================================================================
241 /*!
242  *
243  */
244 //=============================================================================
245 bool MedGUI::OnGUIEvent (int theCommandID)
246 {
247   setOrb();
248
249   SalomeApp_Study* myActiveStudy = dynamic_cast< SalomeApp_Study* >( application()->activeStudy() );
250   if( !myActiveStudy )
251     return false;
252
253   _PTR(Study) aStudy = myActiveStudy->studyDS();
254   //SALOME_NamingService* myNameService = parent->getNameService();
255
256   QString file;
257   QStringList filtersList ;
258
259   filtersList.append( tr("MED_MEN_IMPORT_MED") );
260   filtersList.append( tr("MED_MEN_ALL_FILES") ) ;
261
262   SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( application() );
263   if( !app )
264     return false;
265
266   switch (theCommandID)
267     {
268     case 4031:
269     case 9031:
270     case 931:
271       {
272         MESSAGE("command " << theCommandID << " activated");
273
274         QString myStudyName = myActiveStudy->studyName();
275         bool ok=FALSE;
276 //      int myStudyId = myActiveStudy->id();
277
278         // load MED engine
279         SALOME_MED::MED_Gen_ptr medgen = InitMedGen();
280
281         // Selection du Fichier
282         file = SUIT_FileDlg::getFileName(application()->desktop(),
283                                         "",
284                                         filtersList,
285                                         tr("MED_MEN_IMPORT"),
286                                         true);
287
288         // Selection du Maillage
289         if (!file.isEmpty() )
290           {
291             SCRUTE((const char*)file.toLatin1());
292             QString meshName;
293             meshName = QInputDialog::getText( application()->desktop(),
294                                               tr("MED_INF_MESHNAME"),
295                                               QString::null,
296                                               QLineEdit::Normal,
297                                               QString::null,
298                                               &ok );
299             if ( ok && !meshName.isEmpty() )
300               {
301                   try
302                     {
303                       medgen->readMeshInFile( (const char*)file.toLatin1(),
304                                               (const char*)myStudyName.toLatin1(),
305                                               (const char*)meshName.toLatin1() );
306                       if (myActiveStudy->studyDS()->GetProperties()->IsLocked()) {
307                         SUIT_MessageBox::warning ( application()->desktop(),
308                                                    tr("WRN_WARNING"),
309                                                    tr("WRN_STUDY_LOCKED") );
310                         //QObject::tr("BUT_OK")); by default
311                       }
312                     }
313                   catch (const SALOME::SALOME_Exception & S_ex)
314                     {
315                       SalomeApp_Tools::QtCatchCorbaException(S_ex);
316                     }
317                 }
318                 updateObjBrowser();
319           }
320         break;
321       }
322     case 4032:
323     case 9032:
324     case 932:
325       {
326         MESSAGE("command " << theCommandID << " activated");
327
328         QString myStudyName = myActiveStudy->studyName();
329         bool ok=FALSE;
330 //      int myStudyId = myActiveStudy->id();
331
332         // load MED engine
333         SALOME_MED::MED_Gen_ptr medgen = InitMedGen();
334
335         // Selection du Fichier
336         QString anInitialPath = "";
337         if ( SUIT_FileDlg::getLastVisitedPath().isEmpty() )
338           anInitialPath = QDir::currentPath();
339
340         file = SUIT_FileDlg::getFileName(application()->desktop(),
341                                         anInitialPath,
342                                         filtersList,
343                                         tr("MED_MEN_IMPORT"),
344                                         true);
345
346         // Selection du Maillage
347         if (!file.isEmpty() )
348           {
349             SCRUTE((const char*)file.toLatin1());
350             QString fieldName;
351             fieldName = QInputDialog::getText( application()->desktop(),
352                                                tr("MED_INF_FIELDNAME"),
353                                                QString::null,
354                                                QLineEdit::Normal,
355                                                QString::null,
356                                                &ok );
357             if ( ok && !fieldName.isEmpty())
358               {
359                 try
360                   {
361                     medgen->readFieldInFile( (const char*)file.toLatin1(),
362                                              (const char*)myStudyName.toLatin1(),
363                                              (const char*)fieldName.toLatin1(),
364                                              -1,
365                                              -1 );
366                     if (myActiveStudy->studyDS()->GetProperties()->IsLocked()) {
367                       SUIT_MessageBox::warning ( application()->desktop(),
368                                                  tr("WRN_WARNING"),
369                                                  tr("WRN_STUDY_LOCKED") );
370                       //tr("BUT_OK")); by default
371                     }
372                   }
373                 catch (const SALOME::SALOME_Exception & S_ex)
374                   {
375                     SalomeApp_Tools::QtCatchCorbaException(S_ex);
376                   }
377                 updateObjBrowser ();
378               }
379           }
380         break;
381       }
382     case 934:
383       {
384         //Handle(SMESH_TypeFilter) aMeshFilter = new SMESH_TypeFilter( MESH );
385
386         SALOME_ListIO list;
387         SalomeApp_Application* app = getApp();
388         LightApp_SelectionMgr* mgr = app ? app->selectionMgr() : NULL;
389         if( mgr )
390           mgr->selectedObjects( list );
391         //Sel->AddFilter(aMeshFilter) ;
392
393         int nbSel = list.Extent();
394         if ( nbSel == 1 )
395           {
396             //      SMESH::SMESH_Mesh_var aM;
397             SALOME_MED::MESH_var aMesh;
398             Handle(SALOME_InteractiveObject) IObject = list.First();
399             if ( IObject->hasEntry() )
400               {
401                 _PTR(SObject) aMorSM = aStudy->FindObjectID( IObject->getEntry() );
402                 if ( aMorSM )
403                   {
404                     _PTR(GenericAttribute) anAttr;
405                     _PTR(AttributeIOR)     anIOR;
406                     if (aMorSM->FindAttribute(anAttr, "AttributeIOR"))
407                       {
408                         anIOR = anAttr;
409                         aMesh = SALOME_MED::MESH::_narrow( _orb->string_to_object(anIOR->Value().c_str()) );
410                         if ( aMesh->_is_nil() )
411                         {
412                           SUIT_MessageBox::warning
413                             ( application()->desktop(),
414                               tr ("MED_WRN_WARNING"),
415                               tr ("MED_INF_NOTIMPL") );
416                           break;
417                         }
418                         DumpMesh( aMesh );
419                         //Sel->ClearFilters() ;
420                       }
421                     else
422                       {
423                         SUIT_MessageBox::warning
424                           ( application()->desktop(),
425                             tr ("MED_WRN_WARNING"),
426                             tr ("MED_INF_NOIOR") );
427                         //tr ("MED_BUT_OK") ); by default
428                         break;
429                       }
430                   }
431               }
432           }
433         break;
434       }
435
436     case 935:
437       {
438         //Handle(SMESH_TypeFilter) aSubMeshFilter = new SMESH_TypeFilter( SUBMESH );
439
440         SALOME_ListIO list;
441         SalomeApp_Application* app = getApp();
442         LightApp_SelectionMgr* mgr = app ? app->selectionMgr() : NULL;
443         if( mgr )
444           mgr->selectedObjects( list );
445
446         //Sel->AddFilter(aSubMeshFilter) ;
447
448         int nbSel = list.Extent();
449         if ( nbSel == 1 )
450           {
451             //      SMESH::SMESH_subMesh_var aSubM;
452             SALOME_MED::FAMILY_var aFam;
453             Handle(SALOME_InteractiveObject) IObject = list.First();
454             if ( IObject->hasEntry() )
455               {
456                 _PTR(SObject) aMorSM = aStudy->FindObjectID( IObject->getEntry() );
457                 if ( aMorSM )
458                   {
459                     _PTR(GenericAttribute) anAttr;
460                     _PTR(AttributeIOR)     anIOR;
461                     if (aMorSM->FindAttribute(anAttr, "AttributeIOR"))
462                       {
463                         anIOR = anAttr;
464                       }
465                     else
466                       {
467                         SUIT_MessageBox::warning
468                           ( application()->desktop(),
469                             tr ("MED_WRN_WARNING"),
470                             tr ("MED_INFNOIOR") );
471                         //  tr ("MED_BUT_OK") ); by default
472                         break;
473                       }
474                   }
475               }
476           }
477         break;
478       }
479       case 936 :
480       case 4034 :
481         {
482           
483           MEDGUIFileContentDial* mfcd = new MEDGUIFileContentDial(_data_base, application()->desktop());
484           mfcd->show();
485           break;
486         }
487     }
488
489   app->updateActions(); //SRN: To update a Save button in the toolbar
490
491   return true;
492 }
493
494
495 //=============================================================================
496 /*!
497  *
498  */
499 //=============================================================================
500 bool MedGUI::OnMousePress (QMouseEvent* pe ,
501                            SUIT_ViewWindow* wnd )
502 {
503   MESSAGE("MedGUI::OnMousePress");
504   return true;
505 }
506
507 //=============================================================================
508 /*!
509  *
510  */
511 //=============================================================================
512 bool MedGUI::OnMouseMove (QMouseEvent* pe ,
513                           SUIT_ViewWindow* wnd )
514 {
515   //   MESSAGE("MedGUI::OnMouseMouve");
516   return true;
517 }
518
519 //=============================================================================
520 /*!
521  *
522  */
523 //=============================================================================
524 bool MedGUI::OnKeyPress (QKeyEvent* pe,
525                          SUIT_ViewWindow* wnd)
526 {
527   MESSAGE("MedGUI::OnKeyPress");
528
529   return true;
530 }
531
532 //=============================================================================
533 /*!
534  *
535  */
536 //=============================================================================
537 bool MedGUI::DumpMesh( SALOME_MED::MESH_var MEDMesh)
538 {
539
540   if ( MEDMesh->_is_nil() )
541     {
542       return false;
543     }
544
545   std::string name = MEDMesh->getName();
546   SCRUTE(name);
547
548   int dim2 = MEDMesh->getSpaceDimension();
549   SCRUTE(dim2);
550
551   int k = MEDMesh->getNumberOfNodes() ;
552   SCRUTE(k);
553   SALOME_TYPES::ListOfDouble_var coords = MEDMesh->getCoordinates( SALOME_MED::MED_FULL_INTERLACE );
554   int i = 0;
555   int lu = 0;
556   while (lu < k ) {
557     if (dim2==3)
558       {
559         MESSAGE ( " Coordinates  X = " << coords[i] << " Y = " << coords[i+1] << " Z = " << coords[i+2] );
560         i = i + 3;
561       }
562     else
563       {
564         MESSAGE ( " Coordinates  X = " << coords[i] << " Y = " << coords[i+1] );
565         i = i + 2;
566       }
567     lu=lu+1;
568   }
569
570   int nbfam=MEDMesh->getNumberOfFamilies(SALOME_MED::MED_NODE);
571   SCRUTE(nbfam);
572   SALOME_MED::Family_array_var Families=MEDMesh->getFamilies(SALOME_MED::MED_NODE) ;
573
574   for (k=0;  k < nbfam; k++) {
575     SCRUTE(k);
576     std::string nomFam=Families[k]->getName();
577     SCRUTE(nomFam);
578     SALOME_TYPES::ListOfLong_var tabnoeuds=Families[k]->getNumber(SALOME_MED::MED_NONE);
579     for (int l=0;l<(int)tabnoeuds->length();l++)
580       SCRUTE(tabnoeuds[l]);
581   }
582
583   return true;
584 }
585
586 //=============================================================================
587 /*!
588  *
589  */
590 //=============================================================================
591 bool MedGUI::DumpSubMesh( SALOME_MED::FAMILY_var Fam )
592 {
593
594   if ( Fam->_is_nil() )
595     return false;
596
597   SALOME_TYPES::ListOfLong_var tabnoeuds=Fam->getNumber(SALOME_MED::MED_NONE);
598   for (int l=0;l<(int)tabnoeuds->length();l++)
599     SCRUTE(tabnoeuds[l]);
600
601   return true;
602 }
603
604 //=============================================================================
605 /*!
606  *
607  */
608 //=============================================================================
609 SALOME_MED::MED_Gen_ptr MedGUI::InitMedGen() const
610 {
611   SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( application() );
612   Engines::EngineComponent_var comp =
613     SALOME_LifeCycleCORBA(app->namingService()).FindOrLoad_Component( "FactoryServer", "MED" );
614
615   MESSAGE("_________________________________________");
616   SALOME_MED::MED_Gen_var clr = SALOME_MED::MED_Gen::_narrow(comp);
617   ASSERT(!CORBA::is_nil(clr));
618   return clr._retn();
619 }
620
621 //=============================================================================
622 /*!
623  *
624  */
625 //=============================================================================
626 void MedGUI::setOrb()
627 {
628   try {
629     ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance();
630     ASSERT(SINGLETON_<ORB_INIT>::IsAlreadyExisting());
631     _orb = init( 0 , 0 );
632   } catch (...) {
633     INFOS("internal error : orb not found");
634     _orb = 0;
635   }
636   ASSERT(! CORBA::is_nil(_orb));
637 }
638
639 extern "C" {
640   Standard_EXPORT CAM_Module* createModule() {
641     return new MedGUI();
642   }
643   
644   Standard_EXPORT char* getModuleVersion() {
645       return (char*)SALOMEMED_VERSION_STR;
646   }
647 }