Salome HOME
Abu : Mise a jour HEXABLOCK
[modules/hexablock.git] / src / HEXABLOCKGUI / HEXABLOCKGUI.cxx
1 // Copyright (C) 2009-2013  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 //#define _DEVDEBUG_
21
22 #include <cassert>
23
24
25 #include <QInputDialog>
26 #include <QIcon>
27 #include <QStackedWidget>
28
29 #include <QtxTreeView.h>
30 #include <SUIT_MessageBox.h>
31 #include <SUIT_Session.h>
32 #include <SUIT_ResourceMgr.h>
33 #include <SUIT_Desktop.h>
34 #include <SUIT_ViewManager.h>
35 #include <SUIT_ViewWindow.h>
36 #include <SUIT_DataObject.h>
37 #include <SUIT_DataBrowser.h>
38 #include <SUIT_FileDlg.h>
39
40 #include <SVTK_ViewManager.h>
41 #include <SVTK_ViewModel.h>
42 #include <SVTK_ViewWindow.h>
43
44 #include <VTKViewer_ViewModel.h>
45 #include <vtkActor.h>
46 #include <vtkActorCollection.h>
47 #include <VTKViewer_Algorithm.h>
48 #include <vtkRenderer.h>
49
50
51
52 #include <SOCC_ViewModel.h>
53 #include <SOCC_ViewWindow.h>
54 #include <OCCViewer_ViewWindow.h>
55 #include <SALOME_Prs.h>
56 #include <SOCC_Prs.h>
57 #include <SOCC.h>
58
59 // SALOME KERNEL includes
60 #include <SALOMEDS_Study.hxx>
61 #include <SALOMEDSClient_StudyBuilder.hxx>
62 #include <SALOMEDSClient_SComponent.hxx>
63 #include <SALOMEDSClient_ClientFactory.hxx>
64 #include <SALOMEDSClient_IParameters.hxx>
65
66
67
68 #include <SalomeApp_Study.h>
69 #include <SalomeApp_Module.h>
70 #include <SalomeApp_Application.h>
71 #include <SalomeApp_DataModel.h>
72
73
74 #include <SALOME_ListIO.hxx>
75 #include <SALOME_ListIteratorOfListIO.hxx>
76
77
78 #include <SALOME_LifeCycleCORBA.hxx>
79
80 #include <LightApp_VTKSelector.h>
81
82 #include <OCCViewer_ViewManager.h>
83
84 #include <QtxPopupMgr.h>
85
86 #include "Resource.hxx"
87
88 #include "HEXABLOCKGUI.hxx"
89 #include "HEXABLOCKGUI_Export.hxx"
90 #include "HEXABLOCKGUI_Trace.hxx"
91 #include "HEXABLOCKGUI_Resource.hxx"
92
93 #include "HEXABLOCKGUI_DocumentModel.hxx"
94 #include "HEXABLOCKGUI_DocumentSelectionModel.hxx"
95 //#include "HEXABLOCKGUI_DocumentDelegate.hxx"
96 #include "HEXABLOCKGUI_DocumentPanel.hxx"
97 #include "HEXABLOCKGUI_VtkDocumentGraphicView.hxx"
98 #include "HEXABLOCKGUI_OccGraphicView.hxx"
99 #include "HEXABLOCKGUI_GraphicViewsHandler.hxx"
100
101 #include <HEXABLOCK_version.h>
102
103 #include "MyBasicGUI_PointDlg.hxx"
104
105 // #include "HEXABLOCK.hxx"                del Hexa6
106 // #include "HexDocument_impl.hxx"         del Hexa6
107 #include "Hex.hxx"
108
109
110 #include <Visual3d_ViewManager.hxx>
111 #include <Graphic3d_Structure.hxx>
112 #include <Graphic3d_Group.hxx>
113 #include <V3d_PerspectiveView.hxx>
114 #include <V3d_AmbientLight.hxx>
115 #include <Graphic3d_GraphicDevice.hxx>
116 #include <Graphic3d_Array1OfVertex.hxx>
117 #include <V3d_DirectionalLight.hxx>
118 #include <Xw_Window.hxx>
119 #include <V3d_TypeOfShadingModel.hxx>
120 #include <TCollection_ExtendedString.hxx>
121
122 #include <BRepAlgoAPI_Fuse.hxx>
123
124 #include <AIS_ListIteratorOfListOfInteractive.hxx>
125
126
127 #define DW_MINIMUM_WIDTH       50
128 #define DWINPUT_MINIMUM_HEIGHT 50
129 #define DWINPUT_MINIMUM_WIDTH  50//255
130
131 using namespace std;
132 using namespace HEXABLOCK::GUI;
133
134 int  HEXABLOCKGUI::_oldStudyId = -1;
135
136 // HEXABLOCK_ORB::HEXABLOCK_Gen_var HEXABLOCKGUI::_hexaEngine  = HEXABLOCK_ORB::HEXABLOCK_Gen::_nil();
137 //
138 VtkDocumentGraphicView* HEXABLOCKGUI::currentDocGView = NULL;
139 OccGraphicView*         HEXABLOCKGUI::currentOccGView = NULL;
140 bool                    HEXABLOCKGUI::assocInProgress = false;
141 SalomeApp_Application*  HEXABLOCKGUI::myApplication = NULL;
142
143 HEXABLOCKGUI::HEXABLOCKGUI() :
144           SalomeApp_Module( "HEXABLOCK" ),
145 //          LightApp_Module( "HEXABLOCK" ),
146           _menuId(190),
147           _dwPattern(0),
148           _dwAssociation(0),
149           _dwGroups(0),
150           _dwMesh(0),
151           _dwObjectBrowser(0),
152           _dwInputPanel(0),
153           _patternDataTreeView(0),
154 //          _patternBuilderTreeView(0),
155           _patternGeomTreeView(0),
156           _groupsTreeView(0),
157           _meshTreeView(0),
158 //          _treeViewDelegate(0),
159           _isSaved( false ),
160           moduleActivatedOnce(false),
161           _vertexDiag(0),
162           _edgeDiag(0),
163           _quadDiag(0),
164           _hexaDiag(0),
165           _vectorDiag(0),
166           _cylinderDiag(0),
167           _pipeDiag(0),
168           _makeGridDiag(0),
169           _makeCylinderDiag(0),
170           _makePipeDiag(0),
171           _makeCylindersDiag(0),
172           _makePipesDiag(0),
173           _removeHexaDiag(0),
174           _prismQuadDiag(0),
175           _joinQuadDiag(0),
176           _mergeDiag(0),
177           _disconnectDiag(0),
178           _cutEdgeDiag(0),
179           _makeTransformationDiag(0),
180           _makeSymmetryDiag(0),
181           _performTransformationDiag(0),
182           _performSymmetryDiag(0),
183           _vertexAssocDiag(0),
184           _edgeAssocDiag(0),
185           _quadAssocDiag(0),
186           _groupDiag(0),
187           _lawDiag(0),
188           _propagationDiag(0),
189           _computeMeshDiag(0),
190           _replaceHexaDiag(0),
191           _quadRevolutionDiag(0),
192           _makeHemiSphereDiag(0),
193           _modelInfoDiag(NULL),
194           _addShapeDiag(NULL),
195           _vertexInfoDialog(NULL),
196           _edgeInfoDialog(NULL),
197           _quadInfoDialog(NULL),
198           _hexaInfoDialog(NULL),
199           _vectorInfoDialog(NULL),
200           _groupInfoDialog(NULL),
201           _lawInfoDialog(NULL),
202           _propagationInfoDialog(NULL),
203           currentDialog(NULL),
204           lastOccPrs(NULL),
205           lastVtkDocGView(NULL)
206 {
207     DEBTRACE("HEXABLOCKGUI::HEXABLOCKGUI");
208
209     graphicViewsHandler = new GraphicViewsHandler();
210     loadDocLastPath = new QDir();
211     saveDocLastPath = new QDir();
212     hexa_root       = HEXA_NS::Hex::getInstance ();
213 }
214
215 HEXABLOCKGUI::~HEXABLOCKGUI()
216 {
217 }
218
219
220 SalomeApp_Study* HEXABLOCKGUI::activeStudy()
221 {
222     SUIT_Application* app = SUIT_Session::session()->activeApplication();
223     if( app )
224         return dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
225     else
226         return NULL;
227 }
228
229
230 // Gets an reference to the module's engine
231 /****************************************************
232 HEXABLOCK_ORB::HEXABLOCK_Gen_ptr HEXABLOCKGUI::InitHEXABLOCKGen( SalomeApp_Application* app )
233 {
234     Engines::EngineComponent_var comp = app->lcc()->FindOrLoad_Component( "FactoryServer", "HEXABLOCK" );
235     HEXABLOCK_ORB::HEXABLOCK_Gen_ptr clr = HEXABLOCK_ORB::HEXABLOCK_Gen::_narrow(comp);
236     ASSERT(!CORBA::is_nil(clr));
237     return clr;
238 }
239  **************************************************** */
240
241 void HEXABLOCKGUI::initialize( CAM_Application* app )
242 {
243     DEBTRACE("HEXABLOCKGUI::initialize");
244     SalomeApp_Module::initialize( app );
245
246     // _hexaEngine = InitHEXABLOCKGen( dynamic_cast<SalomeApp_Application*>( app ) );
247
248     DEBTRACE(app << "  " << application() << " " << application()->desktop() << " " << aParent);
249
250     SUIT_ResourceMgr* aResourceMgr = app->resourceMgr();
251     setResource(aResourceMgr);
252
253     createAndFillDockWidget();
254     createActions();
255     createMenus();
256     createTools();
257     studyActivated();
258 }
259
260 void HEXABLOCKGUI::viewManagers( QStringList& list ) const
261 {
262     DEBTRACE("HEXABLOCKGUI::viewManagers");
263     //   list.append( QxScene_Viewer::Type() );
264     //   list.append( OCCViewer_Viewer::Type() );
265     //   list.append( SVTK_Viewer::Type() );
266 }
267
268
269 bool HEXABLOCKGUI::activateModule( SUIT_Study* theStudy )
270 {
271     DEBTRACE("HEXABLOCKGUI::activateModule");
272
273     /*HexaBlock can be launched in only one application*/
274     if (myApplication == NULL)
275         //remember the first application
276         myApplication = dynamic_cast<SalomeApp_Application*>( SUIT_Session::session()->activeApplication() );
277
278     //application verification: if it's not the one known by HexaBlock,
279     //leave without activating
280     SalomeApp_Application* currentApplication =  dynamic_cast<SalomeApp_Application*>( SUIT_Session::session()->activeApplication() );
281     if (myApplication != currentApplication) return false;
282
283     bool bOk = SalomeApp_Module::activateModule( theStudy );
284     if ( !bOk ) return false;
285
286     showDockWidgets(true);
287
288     connect( getApp()->desktop(), SIGNAL( windowActivated( SUIT_ViewWindow* ) ),
289             this, SLOT(onWindowActivated( SUIT_ViewWindow* )), Qt::UniqueConnection );
290     connect( getApp()->objectBrowser()->treeView(),SIGNAL( clicked(const QModelIndex&) ),
291             this, SLOT( onObjectBrowserClick(const QModelIndex&) ), Qt::UniqueConnection );
292
293     LightApp_SelectionMgr* sm = getApp()->selectionMgr();
294
295     SUIT_ViewManager* vm;
296     ViewManagerList OCCViewManagers, VTKViewManagers;
297
298     application()->viewManagers( OCCViewer_Viewer::Type(), OCCViewManagers );
299     QListIterator<SUIT_ViewManager*> itOCC( OCCViewManagers );
300     while ( itOCC.hasNext() && (vm = itOCC.next()) )
301         myOCCSelectors.append( new HEXABLOCKGUI_OCCSelector( ((OCCViewer_ViewManager*)vm)->getOCCViewer(), sm ) );
302
303     application()->viewManagers( SVTK_Viewer::Type(), VTKViewManagers );
304     QListIterator<SUIT_ViewManager*> itVTK( VTKViewManagers );
305     while ( itVTK.hasNext() && (vm = itVTK.next()) )
306         myVTKSelectors.append( new LightApp_VTKSelector( dynamic_cast<SVTK_Viewer*>( vm->getViewModel() ), sm ) );
307
308     //NPAL 19674
309     SALOME_ListIO selected;
310     sm->selectedObjects( selected );
311     sm->clearSelected();
312
313     // disable OCC selectors
314     getApp()->selectionMgr()->setEnabled( false, OCCViewer_Viewer::Type() );
315     QListIterator<HEXABLOCKGUI_OCCSelector*> itOCCSel( myOCCSelectors );
316     while ( itOCCSel.hasNext() )
317         if ( HEXABLOCKGUI_OCCSelector* sr = itOCCSel.next() )
318             sr->setEnabled(true);
319
320     // disable VTK selectors
321     getApp()->selectionMgr()->setEnabled( false, SVTK_Viewer::Type() );
322     QListIterator<LightApp_VTKSelector*> itVTKSel( myVTKSelectors );
323     while ( itVTKSel.hasNext() )
324         if ( LightApp_VTKSelector* sr = itVTKSel.next() )
325             sr->setEnabled(true);
326
327     sm->setSelectedObjects( selected, true );   //NPAL 19674
328
329     //connect close signal to vtk view manager
330     vm = getApp()->getViewManager(SVTK_Viewer::Type(), false);
331     if (vm != NULL)
332     {
333         connect( vm, SIGNAL( tryCloseView( SUIT_ViewWindow * ) ),
334                 this, SLOT( onWindowClosed(SUIT_ViewWindow *) ), Qt::UniqueConnection );
335     }
336
337     //connect close signal to occ view manager
338     vm = getApp()->getViewManager(OCCViewer_Viewer::Type(), false);
339     if (vm != NULL)
340     {
341         connect( vm, SIGNAL( tryCloseView( SUIT_ViewWindow * ) ),
342                 this, SLOT( onWindowClosed(SUIT_ViewWindow *) ), Qt::UniqueConnection );
343     }
344
345     /* ************************************   TODO Hexa6
346     _hexaEngine->SetCurrentStudy(SALOMEDS::Study::_nil());
347     if ( SalomeApp_Study* s = dynamic_cast<SalomeApp_Study*>( theStudy ))
348         if ( _PTR(Study) aStudy = s->studyDS()) {
349             _hexaEngine->SetCurrentStudy( _CAST(Study,aStudy)->GetStudy() );
350             updateObjBrowser(); // objects can be removed
351         }
352      ************************************ */
353
354     if (currentOccGView != NULL && currentOccGView->getViewWindow() != NULL)
355             currentOccGView->getViewWindow()->installEventFilter(this);
356
357     if (currentDocGView != NULL)
358     {
359         switchModel(currentDocGView);
360         showAllMenus();
361
362         if (currentDocGView->getViewWindow() != NULL)
363             currentDocGView->getViewWindow()->installEventFilter(this);
364     }
365     else
366         initialMenus();
367
368     return bOk;
369 }
370
371
372 bool HEXABLOCKGUI::deactivateModule( SUIT_Study* theStudy )
373 {
374     disconnect( getApp()->desktop(), SIGNAL( windowActivated( SUIT_ViewWindow* ) ),
375             this, SLOT(onWindowActivated( SUIT_ViewWindow* )) );
376     disconnect( getApp()->objectBrowser()->treeView(),SIGNAL( clicked(const QModelIndex&) ),
377             this, SLOT( onObjectBrowserClick(const QModelIndex&) ) );
378
379     SVTK_ViewWindow* vtkView = (currentDocGView != NULL) ? currentDocGView->getViewWindow() : NULL;
380     if ( vtkView != NULL)
381     {
382         //default selectionMode in VTKView
383         vtkView->SetSelectionMode( ActorSelection );
384         vtkView->removeEventFilter(this);
385 //        vtkView->close();
386     }
387
388     OCCViewer_ViewWindow* occView = (currentOccGView != NULL) ? currentOccGView->getViewWindow() : NULL;
389     if (occView != NULL)
390     {
391         //defaut selectionMode in OccView
392         selectionMgr()->clearSelected();
393         currentOccGView->globalSelection();
394         occView->removeEventFilter(this);
395 //        occView->close();
396     }
397
398     qDeleteAll(myOCCSelectors);
399     myOCCSelectors.clear();
400     getApp()->selectionMgr()->setEnabled( true, OCCViewer_Viewer::Type() );
401
402     qDeleteAll(myVTKSelectors);
403     myVTKSelectors.clear();
404     getApp()->selectionMgr()->setEnabled( true, SVTK_Viewer::Type() );
405
406     bool bOk = SalomeApp_Module::deactivateModule( theStudy );
407
408     //switch off current document graphic view
409     switchOffGraphicView(currentDocGView);
410
411     //...and hide menus
412     setMenuShown( false );
413     setToolShown( false );
414     showDockWidgets( false );
415     if (_dwInputPanel)
416         _dwInputPanel->close();
417
418     hideActor();
419
420     return bOk;
421 }
422
423 SALOMEDS::Study_var HEXABLOCKGUI::ClientStudyToStudy (_PTR(Study) theStudy)
424 {
425   SALOME_NamingService *aNamingService = SalomeApp_Application::namingService();
426   CORBA::Object_var aSMObject = aNamingService->Resolve("/myStudyManager");
427   SALOMEDS::StudyManager_var aStudyManager = SALOMEDS::StudyManager::_narrow(aSMObject);
428   int aStudyID = theStudy->StudyId();
429   SALOMEDS::Study_var aDSStudy = aStudyManager->GetStudyByID(aStudyID);
430   return aDSStudy._retn();
431 }
432
433 void HEXABLOCKGUI::addInStudy(QMap<QString, TopoDS_Shape>& topo_shapes,
434                               QMap<QString, QString>& docShapesEntry,
435                               QMap<QString, QString>& docShapesName)
436 {
437     if (topo_shapes.size() == 0 || currentOccGView == NULL || currentDocGView == NULL)
438         return; //there's nothing to publish in this case
439
440     SalomeApp_Study* appStudy = HEXABLOCKGUI::activeStudy();
441     if(!appStudy) return;
442     _PTR(Study) aStudy = appStudy->studyDS();
443     SALOMEDS::Study_var aDSStudy = ClientStudyToStudy( aStudy );
444     SALOMEDS::StudyBuilder_var     aBuilder (aDSStudy->NewBuilder());
445     QString entry = currentDocGView->getDocumentModel()->documentEntry();
446     SALOMEDS::SObject_var aFatherSO = aDSStudy->FindObjectID( qPrintable(entry) );
447     if (aFatherSO->_is_nil())
448         return;
449
450     // * get a presentation
451     bool isNewPrs = false;
452     SOCC_Prs* prs = getOccPrs(currentDocGView);
453     if (prs == NULL)
454     {
455         prs = new SOCC_Prs();
456         isNewPrs = true;
457     }
458
459     // * init the presentation with the given shapes
460     QMap<QString, TopoDS_Shape>::const_iterator i = topo_shapes.constBegin();
461     while (i != topo_shapes.constEnd())
462     {
463         QString shapeName  = i.key();
464         TopoDS_Shape shapeValue = i.value();
465         Handle(AIS_Shape) ais_shape = new AIS_Shape(shapeValue);
466         SALOMEDS::SObject_var aSO = aBuilder->NewObject(aFatherSO);
467         QString objStudyEntry = aSO->GetID(); //the entry of the published object
468         Handle( SALOME_InteractiveObject ) io = new SALOME_InteractiveObject(objStudyEntry.toStdString().c_str(),
469                                                                     "HEXABLOCK", shapeName.toStdString().c_str());
470         ais_shape->SetOwner(io);
471         prs->AddObject(ais_shape);
472         aSO->SetAttrString("AttributeName", shapeName.toStdString().c_str());
473         docShapesEntry[shapeName] = objStudyEntry;
474         docShapesName[objStudyEntry] = shapeName;
475         currentOccGView->addShape(objStudyEntry, ais_shape);
476         i++; //handle next shape
477     }
478     // * set the preview
479     if (isNewPrs)
480         prs->AddObject(currentOccGView->getPreviewShape());
481
482     occPrs[currentDocGView] = prs;
483     getApp()->updateObjectBrowser();
484     showOnlyActor();
485 }
486
487
488 bool HEXABLOCKGUI::renameAllowed( const QString& entry) const
489 {
490     SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
491     SalomeApp_Study* appStudy = app ? dynamic_cast<SalomeApp_Study*>( app->activeStudy() ) : 0;
492     SalomeApp_DataObject* obj = appStudy ? dynamic_cast<SalomeApp_DataObject*>(appStudy->findObjectByEntry(entry)) : 0;
493
494     return (app && appStudy && obj && !appStudy->isComponent(entry) && !obj->isReference());
495 }
496
497
498 bool HEXABLOCKGUI::renameObject( const QString& entry, const QString& name)
499 {
500     bool result = false;
501
502     // Pas de renommage des shapes pour le moment, juste les documents
503     VtkDocumentGraphicView* dgview = getDocGViewByEntry(entry);
504     if (dgview == NULL || dgview->getDocumentModel() == NULL)
505         return result;
506
507     SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( SUIT_Session::session()->activeApplication());
508     SalomeApp_Study* appStudy = app ? dynamic_cast<SalomeApp_Study*>( app->activeStudy() ) : 0;
509
510     if(!appStudy)
511         return result;
512
513     _PTR(Study) aStudy = appStudy->studyDS();
514
515     if(!aStudy)
516         return result;;
517
518     _PTR(SObject) obj ( aStudy->FindObjectID(qPrintable(entry)) );
519     _PTR(GenericAttribute) anAttr;
520     if ( obj ){
521         if ( obj->FindAttribute(anAttr, "AttributeName") ){
522             _PTR(AttributeName) aName (anAttr);
523             DocumentModel* docModel = dgview->getDocumentModel();
524             docModel->setName( name );
525             aName->SetValue( name.toLatin1().data() );
526             getApp()->updateObjectBrowser();
527             _dwPattern->setWindowTitle( name );
528             result = true;
529         }
530     }
531     return result;
532 }
533
534 // --- Default windows
535 void HEXABLOCKGUI::windows( QMap<int, int>& theMap ) const
536 {
537     DEBTRACE("HEXABLOCKGUI::windows");
538     theMap.clear();
539     theMap.insert( SalomeApp_Application::WT_ObjectBrowser, Qt::LeftDockWidgetArea );
540     theMap.insert( SalomeApp_Application::WT_PyConsole,     Qt::BottomDockWidgetArea );
541 }
542
543 QString  HEXABLOCKGUI::engineIOR() const
544 {
545     DEBTRACE("HEXABLOCKGUI::engineIOR");
546
547     /* ************************************   TODO Hexa6
548     CORBA::ORB_var anORB = getApp()->orb();
549     CORBA::String_var anIOR = anORB->object_to_string(_hexaEngine);
550     return QString( anIOR.in() );
551        ************************************ */
552     return "";
553 }
554
555 VtkDocumentGraphicView* HEXABLOCKGUI::getOrCreateDocument(SalomeApp_DataObject* studyObject)
556 {
557     if (docs.count(studyObject->entry()) == 0)
558     {
559         //Object's document not acquired yet
560         VtkDocumentGraphicView* dgview;
561         HEXA_NS::Hex* directory = HEXA_NS::Hex::getInstance();
562         if (directory == NULL)
563             return NULL;    //No working director found
564
565         //------------------ Load the document -----------------
566         HEXA_NS::Document* document = directory->findDocument(studyObject->name().toStdString());
567         if (document == NULL)
568             return NULL;    //No document found
569
570         DocumentModel* docModel = new DocumentModel(document, studyObject->entry(), this );
571         if (!docModel) {
572             MESSAGE("Doc model creation failed!");
573             return NULL;
574         }
575         dgview = graphicViewsHandler->createDocumentGraphicView( docModel, NULL, application()->desktop() );
576         if (dgview == NULL) return NULL;
577
578         //update the data tree
579         docs[studyObject->entry()] = dgview;
580     }
581
582     return docs[studyObject->entry()];
583 }
584
585 void HEXABLOCKGUI::onObjectBrowserClick(const QModelIndex& index)
586 {
587     // ** we want to switch automatically to the right view windows
588
589     //first, find selected item
590     QString itemEntry;
591     DataObjectList dol = getApp()->objectBrowser()->getSelected();
592     if (dol.isEmpty()) return;
593     SalomeApp_DataObject* item = dynamic_cast<SalomeApp_DataObject*>(dol[0]);
594     if (!item || item->parent()->name().compare(tr("HEXABLOCK")) != 0 ||
595             item->entry().isEmpty())
596         return;
597
598     // find the document corresponding to it
599     VtkDocumentGraphicView* docGView = NULL;
600     if (docs.contains(item->entry()))
601         docGView = docs[item->entry()];
602     else
603         docGView = getOrCreateDocument(item);
604
605     //Init OCC if necessary
606     if (currentOccGView == NULL)
607     {
608         currentOccGView = new OccGraphicView(graphicViewsHandler->createOccWindow(), application()->desktop());
609         currentOccGView->getViewWindow()->installEventFilter(this);
610     }
611     else if (currentOccGView->getViewWindow() == NULL)
612     {
613         currentOccGView->setViewWindow(graphicViewsHandler->createOccWindow());
614         currentOccGView->getViewWindow()->installEventFilter(this);
615     }
616
617     //Init the vtk view if necessary
618     if (docGView->getViewWindow() == NULL)
619     {
620         if (currentDocGView == NULL || currentDocGView->getViewWindow() == NULL)
621         {
622             docGView->setViewWindow(graphicViewsHandler->createVtkWindow());
623             docGView->getViewWindow()->installEventFilter(this);
624             showAllMenus();
625         }
626         else
627             docGView->setViewWindow(currentDocGView->getViewWindow());
628
629         if (docGView->getDocumentActor() == NULL)
630             docGView->update();
631     }
632
633     if (docGView == NULL || docGView == currentDocGView)
634     {
635         showOnlyActor();
636         return;
637     }
638
639     //update the current document
640     switchModel(docGView);
641 }
642
643
644 void HEXABLOCKGUI::onWindowActivated( SUIT_ViewWindow* svw)
645 {
646     DEBTRACE("HEXABLOCKGUI::onWindowActivated");
647 }
648
649 void HEXABLOCKGUI::onWindowClosed( SUIT_ViewWindow* svw)
650 {
651     DEBTRACE("HEXABLOCKGUI::onWindowClosed");
652
653     //Decharger le model correspondant (VTK)
654     SVTK_ViewWindow* window = dynamic_cast<SVTK_ViewWindow*>(svw);
655     if (window != NULL)
656     {
657         //VTK clean close
658         if (currentDocGView != NULL && currentDocGView->getViewWindow() == window)
659         { //HexaBlock Vtk Window has been closed
660
661             if (currentDialog != NULL) currentDialog->close();
662 //            if (_treeViewDelegate != NULL) _treeViewDelegate->closeDialog();
663             if (currentDocGView != NULL)
664                 currentDocGView->setViewWindow(NULL);
665
666             initialMenus();
667             return;
668         }
669
670         //SAVE DOCUMENT
671         /*VtkDocumentGraphicView* dgview = graphicViewsHandler->getDocumentGraphicView(window);
672         if (dgview == NULL) return; //The window has no document to close
673
674         //Save the document? (Ask only if the document is not empty
675         int answerCode = SUIT_MessageBox::question(
676                             0,
677                             tr("Close"),
678                             tr("Save the document ?"),
679                             SUIT_MessageBox::Yes | SUIT_MessageBox::Cancel,
680                             SUIT_MessageBox::Cancel);
681         if ( answerCode == SUIT_MessageBox::Cancel) return; //abort closing
682         else if (answerCode == SUIT_MessageBox::Yes)
683             saveDocument(); //Save the document
684         */
685 //        switchOffGraphicView(window); //disconnect all connected signals
686 //        currentVtkView = NULL;
687 //        graphicViewsHandler->closeDocumentGraphicView(window);
688
689         //create a new one (empty document)
690 //        newDocument();
691 //        currentVtkView->setWindowTitle(tr("HEXABLOCK") + " : " + currentVtkView->windowTitle());
692     }
693
694     //OCC clean close
695     if (currentOccGView != NULL && svw == currentOccGView->getViewWindow())
696     {
697         if (_vertexAssocDiag != NULL) _vertexAssocDiag->clear();
698         if (_edgeAssocDiag != NULL) _edgeAssocDiag->clear();
699         if (_quadAssocDiag != NULL) _quadAssocDiag->clear();
700         if (currentDialog != NULL) currentDialog->close();
701 //        if (_treeViewDelegate != NULL) _treeViewDelegate->closeDialog();
702
703         currentOccGView->getViewWindow()->removeEventFilter(this);
704         currentOccGView->setViewWindow(NULL);
705     }
706 }
707
708 void HEXABLOCKGUI::onViewManagerAdded( SUIT_ViewManager*  vm)
709 {
710     DEBTRACE("HEXABLOCKGUI::onViewManagerAdded");
711
712     connect( vm, SIGNAL( tryCloseView( SUIT_ViewWindow * ) ),
713             this, SLOT( onWindowClosed(SUIT_ViewWindow *) ), Qt::UniqueConnection );
714
715     if ( vm && vm->getType() == OCCViewer_Viewer::Type() )
716       {
717         LightApp_SelectionMgr* sm = getApp()->selectionMgr();
718         myOCCSelectors.append( new HEXABLOCKGUI_OCCSelector( ((OCCViewer_ViewManager*)vm)->getOCCViewer(), sm ) );
719         // disable OCC selectors
720         getApp()->selectionMgr()->setEnabled( false, OCCViewer_Viewer::Type() );
721         QListIterator<HEXABLOCKGUI_OCCSelector*> itOCCSel( myOCCSelectors );
722         while ( itOCCSel.hasNext() )
723           if ( HEXABLOCKGUI_OCCSelector* sr = itOCCSel.next() )
724             sr->setEnabled(true);
725       }
726       else if ( vm->getType() == SVTK_Viewer::Type() )
727       {
728         LightApp_SelectionMgr* sm = getApp()->selectionMgr();
729         myVTKSelectors.append( new LightApp_VTKSelector( dynamic_cast<SVTK_Viewer*>( vm->getViewModel() ), sm ) );
730
731         // disable VTK selectors
732         getApp()->selectionMgr()->setEnabled( false, SVTK_Viewer::Type() );
733         QListIterator<LightApp_VTKSelector*> itVTKSel( myVTKSelectors );
734         while ( itVTKSel.hasNext() )
735           if ( LightApp_VTKSelector* sr = itVTKSel.next() )
736             sr->setEnabled(true);
737       }
738 }
739
740 void HEXABLOCKGUI::onViewManagerRemoved( SUIT_ViewManager* vm)
741 {
742     DEBTRACE("HEXABLOCKGUI::::onViewManagerRemoved");
743
744     SUIT_ViewModel* viewer = vm->getViewModel();
745     if (vm != NULL && vm->getType() == SVTK_Viewer::Type())
746     {
747         //VTK View Manager removed
748         if (getApp()->activeModule()->moduleName().compare("HEXABLOCK") == 0)
749         {
750             //Process only if it's our view manager
751             SUIT_ViewManager* myVm = NULL;
752             if (currentDocGView != NULL && currentDocGView->getViewWindow() != NULL)
753                 myVm = currentDocGView->getViewWindow()->getViewManager();
754
755             if (myVm == vm)
756             {
757                 //close opened dialog
758                 if (currentDialog != NULL) currentDialog->close();
759 //                if (_treeViewDelegate != NULL) _treeViewDelegate->closeDialog();
760
761                 initialMenus();
762             }
763         }
764
765         //remove its selector
766         QListIterator<LightApp_VTKSelector*> itVTKSel( myVTKSelectors );
767         while ( itVTKSel.hasNext() )
768             if ( LightApp_VTKSelector* sr = itVTKSel.next() )
769                 if ( sr->viewer() == viewer )
770                 {
771                     delete myVTKSelectors.takeAt( myVTKSelectors.indexOf( sr ) );
772                     break;
773                 }
774         return;
775     }
776
777     //OCC
778     if ( vm && vm->getType() == OCCViewer_Viewer::Type() )
779     {
780         //OCC View removed: remove its selector
781         QListIterator<HEXABLOCKGUI_OCCSelector*> itOCCSel( myOCCSelectors );
782         while ( itOCCSel.hasNext() )
783             if ( HEXABLOCKGUI_OCCSelector* sr = itOCCSel.next() )
784                 if ( sr->viewer() == viewer )
785                 {
786                     delete myOCCSelectors.takeAt( myOCCSelectors.indexOf( sr ) );
787                     break;
788                 }
789     }
790 }
791
792 void HEXABLOCKGUI::onSelectionChanged( const QItemSelection & selected, const QItemSelection & deselected  )
793 {
794     QModelIndexList l = selected.indexes();
795     if ( l.count() == 0 ) return;
796     if ( !l[0].isValid() ) return;
797     VtkDocumentGraphicView* currentGraphicView = getCurrentVtkGraphicView();
798     if ( currentGraphicView == NULL) return;
799
800     QTreeView* theTree = NULL;
801     if ( sender() == currentGraphicView->getPatternDataSelectionModel() ){
802         theTree = _patternDataTreeView;
803     }
804     /*else if ( sender() == currentGraphicView->getPatternBuilderSelectionModel() ){
805         theTree = _patternBuilderTreeView;
806     }*/
807     else if ( sender() == currentGraphicView->getPatternGeomSelectionModel() ) {
808         theTree =  _patternGeomTreeView;
809     }
810     else if ( sender() == currentGraphicView->getGroupsSelectionModel() ){
811         theTree = _groupsTreeView;
812     } else if ( sender() == currentGraphicView->getMeshSelectionModel() ){
813         theTree = _meshTreeView;
814     }
815     if ( theTree ){
816         theTree->scrollTo ( l[0] );
817     }
818 }
819
820 bool HEXABLOCKGUI::createSComponent() //addComponent
821 {
822     DEBTRACE("HEXABLOCKGUI::createSComponent");
823     // --- Find or create (if not done yet) "HEXABLOCK" SComponent in the study
824
825     _PTR(Study)            aStudy = (( SalomeApp_Study* )(getApp()->activeStudy()))->studyDS();
826     _PTR(StudyBuilder)     aBuilder (aStudy->NewBuilder());
827     _PTR(GenericAttribute) anAttr;
828     _PTR(AttributeName)    aName;
829     _PTR(AttributeGraphic) aGraphic;
830
831     _PTR(SComponent) aComponent = aStudy->FindComponent("HEXABLOCK");
832     if ( !aComponent ){ //Create hexablock component if there's not one yet
833         aComponent = aBuilder->NewComponent("HEXABLOCK");
834         anAttr = aBuilder->FindOrCreateAttribute(aComponent, "AttributeName");
835         aName = _PTR(AttributeName) (anAttr);
836         aName->SetValue(getApp()->moduleTitle("HEXABLOCK").toStdString());
837
838         anAttr = aBuilder->FindOrCreateAttribute(aComponent, "AttributePixMap");
839         _PTR(AttributePixMap) aPixmap(anAttr);
840         aPixmap->SetPixMap(tr("ICO_MODULE_HEXABLOCK_SMALL").toStdString());
841
842         aBuilder->DefineComponentInstance(aComponent, engineIOR().toStdString());
843         DEBTRACE("HEXABLOCKGUI::createSComponent engineIOR=>"<<engineIOR().toStdString());
844         return true;
845     }
846     return false;
847 }
848
849 void HEXABLOCKGUI::setResource(SUIT_ResourceMgr* r)
850 {
851     DEBTRACE("HEXABLOCKGUI::setResource");
852     _myresource = new HEXABLOCKGUI_Resource(r);
853     _myresource->preferencesChanged();
854 }
855
856 void HEXABLOCKGUI::createPreferences()
857 {
858     DEBTRACE("HEXABLOCKGUI::createPreferences");
859     _myresource->createPreferences(this);
860 }
861
862 void HEXABLOCKGUI::preferencesChanged( const QString& sect, const QString& name )
863 {
864     DEBTRACE("HEXABLOCKGUI::preferencesChanged");
865     _myresource->preferencesChanged(sect, name);
866 //    if(name=="userCatalog")
867 //        _genericGui->getCatalogWidget()->addCatalogFromFile(Resource::userCatalog.toStdString());
868 }
869
870 void HEXABLOCKGUI::studyActivated() //CS_TODO
871 {
872     int newStudyId = getApp()->activeStudy()->id();
873     DEBTRACE("HEXABLOCKGUI::studyActivated " << _oldStudyId << " " << newStudyId);
874 //    if (_oldStudyId != -1)
875 //    {
876 //        _studyContextMap[_oldStudyId] = QtGuiContext::getQtCurrent();
877 //        if (_studyContextMap.count(newStudyId))
878 //        {
879 //            DEBTRACE("switch to valid context " << QtGuiContext::getQtCurrent() << " " << _studyContextMap[newStudyId]);
880 //            QtGuiContext::setQtCurrent(_studyContextMap[newStudyId]);
881 //        }
882 //        else
883 //        {
884 //            DEBTRACE("no switch to null context");
885 //        }
886 //    }
887     _oldStudyId = newStudyId;
888 }
889
890 void HEXABLOCKGUI::treeContextMenu(const QPoint& aPosition)
891 {
892     QModelIndex currentIndex = _patternDataTreeView->currentIndex();
893     QVariant currentAssocVariant;
894     QString currentAssocEntry;
895
896     currentAssocVariant = currentIndex.data( HEXA_ASSOC_ENTRY_ROLE );
897     currentAssocEntry    = currentIndex.data( HEXA_ASSOC_ENTRY_ROLE ).toString();
898     if ( currentAssocVariant.isValid() && !currentAssocEntry.isEmpty() ){
899         QMenu menu( _patternDataTreeView );
900         //Remove association
901         QAction *clearAct = menu.addAction( "Remove association(s)" );
902         connect( clearAct, SIGNAL(triggered()), this, SLOT(clearAssociations()), Qt::UniqueConnection );
903         menu.exec( _patternDataTreeView->mapToGlobal( aPosition) );
904     }
905     else {
906
907         VtkDocumentGraphicView* currentVtkGraphicView = getCurrentVtkGraphicView();
908         if (currentVtkGraphicView == NULL) return;
909         QStandardItem *item = currentVtkGraphicView->getPatternDataModel()->itemFromIndex(currentIndex);
910
911         //We don't do anything for single items
912         if ( item->type() == VERTEXITEM || item->type() == EDGEITEM ||
913                 item->type() == QUADITEM || item->type() == HEXAITEM)
914             return;
915
916         //No associations for HEXA
917         QVariant treeVariant = currentIndex.data( HEXA_TREE_ROLE );
918         if ( !treeVariant.isValid() ) return;
919         int eltType = treeVariant.toInt();
920         if (eltType == HEXA_DIR_TREE) return;
921         QMenu menu( _patternDataTreeView );
922
923         //Show association(s)
924         QAction *showAssocAct = menu.addAction( "Show associations" );
925         connect( showAssocAct, SIGNAL(triggered()), this, SLOT(showAssociations()), Qt::UniqueConnection );
926
927         //Clear all associations
928         QAction *clearAllAssocAct = menu.addAction( "Remove all associations" );
929         connect( clearAllAssocAct, SIGNAL(triggered()), this, SLOT(clearAllAssociations()), Qt::UniqueConnection );
930
931         menu.exec( _patternDataTreeView->mapToGlobal( aPosition) );
932     }
933 }
934
935
936 void HEXABLOCKGUI::createAndFillDockWidget()
937 {
938     QMainWindow *aParent = application()->desktop();
939
940     // Create dock widget (3 dock)
941
942     //1) *********** user input panel ( contain user's edit dialog box )
943     _dwInputPanel = new QDockWidget(aParent);
944     _dwInputPanel->setVisible(false);
945     _dwInputPanel->setWindowTitle("Input Panel");
946 //    _dwInputPanel->setMinimumWidth(DWINPUT_MINIMUM_WIDTH); // --- force a minimum until display
947
948 //    _treeViewDelegate = new DocumentDelegate(_dwInputPanel);
949
950     //2) ************* document data ( Pattern, Association, Mesh ) in treeview representation
951     //      Pattern
952     _dwPattern = new QDockWidget(aParent);
953     _dwPattern->setVisible(false);
954     _dwPattern->setWindowTitle("Model");
955     _dwPattern->setMinimumWidth(DW_MINIMUM_WIDTH); // --- force a minimum until display
956
957     QFrame*      patternFrame  = new QFrame(_dwPattern);
958     patternFrame->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Preferred);
959     QVBoxLayout* patternLayout = new QVBoxLayout(patternFrame);
960     patternLayout->setSizeConstraint(QLayout::SetMaximumSize);
961     QSplitter *splitter = new QSplitter(Qt::Vertical,patternFrame);
962     _patternDataTreeView    = new QTreeView(patternFrame);//_dwPattern);
963 //    _patternBuilderTreeView = new QTreeView(patternFrame);                  // ---> TO REMOVE
964     _patternGeomTreeView    = new QTreeView(patternFrame);
965     splitter->addWidget(_patternDataTreeView);
966 //    splitter->addWidget(_patternBuilderTreeView);                           // ---> TO REMOVE
967     splitter->addWidget(_patternGeomTreeView);
968     patternLayout->addWidget(splitter);
969
970     _patternDataTreeView->setEditTriggers(QAbstractItemView::NoEditTriggers/*QAbstractItemView::DoubleClicked*/);
971     _patternDataTreeView->setSelectionMode(QAbstractItemView::SingleSelection/*QAbstractItemView::MultiSelection*/);
972 //    _patternDataTreeView->setItemDelegate(_treeViewDelegate);
973
974
975 //    _patternBuilderTreeView->setEditTriggers(QAbstractItemView::AllEditTriggers);
976 //    _patternBuilderTreeView->setItemDelegate(_treeViewDelegate);
977
978     _patternGeomTreeView->setEditTriggers(QAbstractItemView::NoEditTriggers);
979     _patternGeomTreeView->setSelectionMode(QAbstractItemView::SingleSelection/*QAbstractItemView::MultiSelection*/);
980
981     _dwPattern->setWidget(patternFrame);
982     patternFrame->show();
983
984     //      Groups
985     _dwGroups = new QDockWidget(aParent);
986
987     _dwGroups->setVisible(false);
988     _dwGroups->setWindowTitle("Groups");
989     _dwGroups->setMinimumWidth(DW_MINIMUM_WIDTH); // --- force a minimum until display
990     _groupsTreeView = new QTreeView(_dwGroups);
991     _groupsTreeView->setEditTriggers(QAbstractItemView::NoEditTriggers);
992 //    _groupsTreeView->setItemDelegate(_treeViewDelegate);
993     _dwGroups->setWidget(_groupsTreeView);
994     _groupsTreeView->show();
995
996     //      Mesh
997     _dwMesh = new QDockWidget(aParent);
998     _dwMesh->setVisible(false);
999     _dwMesh->setWindowTitle("Mesh");
1000     _dwMesh->setMinimumWidth(DW_MINIMUM_WIDTH); // --- force a minimum until display
1001     _meshTreeView = new QTreeView(_dwMesh);
1002     _meshTreeView->setEditTriggers(QAbstractItemView::NoEditTriggers);
1003     _meshTreeView->setSelectionMode(QAbstractItemView::SingleSelection);
1004 //    _meshTreeView->setItemDelegate(_treeViewDelegate);
1005     _dwMesh->setWidget(_meshTreeView);
1006     _meshTreeView->show();
1007
1008     QDockWidget *_dwObjectBrowser = 0;
1009     QWidget* wid = getApp()->objectBrowser()->treeView();
1010     QWidget *w   = wid->parentWidget();
1011     while ( w && !_dwObjectBrowser ) {
1012         _dwObjectBrowser = ::qobject_cast<QDockWidget*>( w );
1013         w = w->parentWidget();
1014     }
1015     _dwObjectBrowser->setMinimumWidth(DW_MINIMUM_WIDTH); // --- force a minimum until display
1016     _dwObjectBrowser->setWindowTitle("Study");
1017
1018     // dock widget position
1019     aParent->addDockWidget( Qt::LeftDockWidgetArea, _dwObjectBrowser );
1020 //    aParent->addDockWidget( Qt::LeftDockWidgetArea, _dwInputPanel );
1021     aParent->addDockWidget( Qt::RightDockWidgetArea, _dwInputPanel );
1022
1023     aParent->tabifyDockWidget( _dwObjectBrowser, _dwPattern );
1024     aParent->tabifyDockWidget( _dwPattern, _dwGroups );
1025     aParent->tabifyDockWidget( _dwGroups, _dwMesh );
1026
1027
1028 #if QT_VERSION >= 0x040500
1029     aParent->setTabPosition(Qt::AllDockWidgetAreas, Resource::tabPanelsUp? QTabWidget::North: QTabWidget::South);
1030 #endif
1031
1032
1033     // popup menu on data tree view
1034     _patternDataTreeView->setContextMenuPolicy(Qt::CustomContextMenu);
1035     connect(_patternDataTreeView,
1036             SIGNAL(customContextMenuRequested(const QPoint &)),
1037             this,
1038             SLOT(treeContextMenu(const QPoint &)),
1039             Qt::UniqueConnection );
1040 }
1041
1042 void HEXABLOCKGUI::createActions()
1043 {
1044     QMainWindow *aParent = application()->desktop();
1045     SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
1046
1047     // Document
1048     _newAct = createAction(_menuId++,
1049             tr("Create a new document"),
1050             resMgr->loadPixmap("HEXABLOCK", tr("ICON_NEW_DOCUMENT")),
1051             tr("New Document"),
1052             tr("Create a new document"),
1053             0, aParent, false, this, SLOT(newDocument()));
1054     _newAct->setShortcut( Qt::CTRL + Qt::SHIFT + Qt::Key_N ); // --- QKeySequence::New ambiguous in SALOME
1055
1056     _importAct = createAction(_menuId++,
1057             tr("Load a document"),
1058             resMgr->loadPixmap("HEXABLOCK", tr("ICON_LOAD_DOCUMENT")),
1059             tr("Load Document"),
1060             tr("Load a document"),
1061             0, aParent, false, this, SLOT(loadDocument()));
1062     _importAct->setShortcut(Qt::CTRL + Qt::SHIFT + Qt::Key_O); // --- QKeySequence::Open ambiguous in SALOME
1063
1064     _saveAct = createAction(_menuId++,
1065             tr("Save the document"),
1066             resMgr->loadPixmap("HEXABLOCK", tr("ICON_SAVE_DOCUMENT")),
1067             tr("Save Document"),
1068             tr("Save the document"),
1069             0, aParent, false, this, SLOT(saveDocument()));
1070
1071     // Pattern Data creation
1072     _addVertex = createAction(_menuId++,
1073             tr("Create a vertex"),
1074             resMgr->loadPixmap("HEXABLOCK", tr("ICON_ADD_VERTEX")),
1075             tr("Add Vertex"),
1076             tr("Create a new vertex"),
1077             0, aParent, false, this, SLOT(addVertex()));
1078
1079     _addEdge = createAction(_menuId++,
1080             tr("Create an edge"),
1081             resMgr->loadPixmap("HEXABLOCK", tr("ICON_ADD_EDGE")),
1082             tr("Add Edge"),
1083             tr("Create a new edge"),
1084             0, aParent, false, this, SLOT(addEdge()));
1085
1086     _addQuad = createAction(_menuId++,
1087             tr("Create a quadrangle"),
1088             resMgr->loadPixmap("HEXABLOCK", tr("ICON_ADD_QUAD")),
1089             tr("Add Quadrangle"),
1090             tr("Create a new quadrangle"),
1091             0, aParent, false, this, SLOT(addQuad()));
1092
1093     _addHexa = createAction(_menuId++,
1094             tr("Create an hexahedron"),
1095             resMgr->loadPixmap("HEXABLOCK", tr("ICON_ADD_HEXA")),
1096             tr("Add Hexahedron"),
1097             tr("Create a new hexahedron"),
1098             0, aParent, false, this, SLOT(addHexa()));
1099
1100     // Builder Data creation
1101     _addVector = createAction(_menuId++,
1102             tr("Create a vector"),
1103             resMgr->loadPixmap( "HEXABLOCK", tr("ICON_ADD_VECTOR")),
1104             tr("Add Vector"),
1105             tr("Create a new vector"),
1106             0, aParent, false, this, SLOT(addVector()));
1107
1108 //    _addCylinder = createAction(_menuId++,
1109 //            tr("Create a cylinder"),
1110 //            resMgr->loadPixmap("HEXABLOCK", tr("ICON_ADD_CYLINDER")),
1111 //            tr("Add Cylinder"),
1112 //            tr("Create a new cylinder"),
1113 //            0, aParent, false, this,  SLOT(addCylinder()));
1114
1115 //    _addPipe = createAction(_menuId++,
1116 //            tr("Create a pipe"),
1117 //            resMgr->loadPixmap("HEXABLOCK", tr("ICON_ADD_PIPE")),
1118 //            tr("Add Pipe"),
1119 //            tr("Create a new pipe"),
1120 //            0, aParent, false, this, SLOT(addPipe()));
1121
1122     _makeGrid = createAction(_menuId++,
1123             tr("Make a grid"),
1124             resMgr->loadPixmap("HEXABLOCK", tr("ICON_MAKE_GRID")),
1125             tr("Make Grid"),
1126             tr("Make a grid"),
1127             0, aParent, false, this,  SLOT(makeGrid()));
1128
1129     _makeCylinder = createAction(_menuId++,
1130             tr("Make a cylinder"),
1131             resMgr->loadPixmap("HEXABLOCK", tr("ICON_MAKE_CYLINDER")),
1132             tr("Make Cylinder"),
1133             tr("Make a cylinder"),
1134             0, aParent, false, this, SLOT(makeCylinder()));
1135
1136     _makePipe = createAction(_menuId++,
1137             tr("Make a pipe"),
1138             resMgr->loadPixmap("HEXABLOCK", tr("ICON_MAKE_PIPE")),
1139             tr("Make Pipe"),
1140             tr("Make a pipe"),
1141             0, aParent, false, this, SLOT(makePipe()));
1142
1143     _makeCylinders = createAction(_menuId++,
1144             tr("Make cylinders"),
1145             resMgr->loadPixmap("HEXABLOCK", tr("ICON_MAKE_CYLINDERS")),
1146             tr("Make Cylinders"),
1147             tr("Make cylinders"),
1148             0, aParent, false, this, SLOT(makeCylinders()));
1149
1150     _makePipes = createAction(_menuId++,
1151             tr("Make pipes"),
1152             resMgr->loadPixmap("HEXABLOCK", tr("ICON_MAKE_PIPES")),
1153             tr("Make Pipes"),
1154             tr("Make pipes"),
1155             0, aParent, false, this, SLOT(makePipes()));
1156
1157     _makeHemiSphere = createAction(_menuId++,
1158             tr("Make an hemisphere"),
1159             resMgr->loadPixmap("HEXABLOCK", tr("ICON_MAKE_HEMISPHERE")),
1160             tr("Make Hemisphere"),
1161             tr("Make an hemisphere"),
1162             0, aParent, false, this, SLOT(makeHemiSphere()));
1163
1164     // Pattern Data edition
1165     _removeHexa = createAction(_menuId++,
1166             tr("Remove hexahedra"),
1167             resMgr->loadPixmap("HEXABLOCK", tr("ICON_REMOVE_HEXA")),
1168             tr("Remove Hexahedra"),
1169             tr("Remove hexahedra"),
1170             0, aParent, false, this, SLOT(removeHexa()));
1171
1172     _prismQuad = createAction(_menuId++,
1173             tr("Prism quadrangles"),
1174             resMgr->loadPixmap( "HEXABLOCK", tr("ICON_PRISM_QUAD")),
1175             tr("Prism Quadrangles"),
1176             tr("Prism quadrangles"),
1177             0, aParent, false, this, SLOT(prismQuad()));
1178
1179     _joinQuad = createAction(_menuId++,
1180             tr("Join quadrangles"),
1181             resMgr->loadPixmap("HEXABLOCK", tr("ICON_JOIN_QUAD")),
1182             tr("Join Quadrangles"),
1183             tr("Join quadrangles with hexahedra"),
1184             0, aParent, false, this, SLOT(joinQuad()));
1185
1186     _merge = createAction(_menuId++,
1187             tr("Merge"),
1188             resMgr->loadPixmap("HEXABLOCK", tr("ICON_MERGE")),
1189             tr("Merge"),
1190             tr("Merge"),
1191             0, aParent, false, this, SLOT(merge()));
1192
1193     _disconnect = createAction(_menuId++,
1194             tr("Disconnect"),
1195             resMgr->loadPixmap("HEXABLOCK", tr("ICON_DISCONNECT")),
1196             tr("Disconnect"),
1197             tr("Disconnect edges, vertexes or quadrangles"),
1198             0, aParent, false, this, SLOT(disconnectElts()));
1199
1200     _cutEdge = createAction(_menuId++,
1201             tr("Cut edge"),
1202             resMgr->loadPixmap("HEXABLOCK", tr("ICON_CUT_EDGE")),
1203             tr("Cut Edge"),
1204             tr("Cut edge"),
1205             0, aParent, false, this, SLOT(cutEdge()));
1206
1207     _makeTransformation = createAction(_menuId++,
1208             tr("Make transformation"),
1209             resMgr->loadPixmap("HEXABLOCK", tr("ICON_MAKE_TRANSFORMATION")),
1210             tr("Make Transformation"),
1211             tr("Make transformation"),
1212             0, aParent, false, this, SLOT(makeTransformation()));
1213
1214     _makeSymmetry = createAction(_menuId++,
1215             tr("Make symmetry"),
1216             resMgr->loadPixmap("HEXABLOCK", tr("ICON_MAKE_SYMMETRY")),
1217             tr("Make Symmetry"),
1218             tr("Make symmetry"),
1219             0, aParent, false, this, SLOT(makeSymmetry()));
1220
1221     _performTransformation = createAction(_menuId++,
1222             tr("Perform transformation"),
1223             resMgr->loadPixmap("HEXABLOCK", tr("ICON_PERFORM_TRANSFORMATION")),
1224             tr("Perform Transformation"),
1225             tr("Perform transformation"),
1226             0, aParent, false, this, SLOT(performTransformation()));
1227
1228     _performSymmetry = createAction(_menuId++,
1229             tr("Perform symmetry"),
1230             resMgr->loadPixmap("HEXABLOCK", tr("ICON_PERFORM_SYMMETRY")),
1231             tr("Perform Symmetry"),
1232             tr("Perform symmetry"),
1233             0, aParent, false, this, SLOT(performSymmetry()));
1234
1235     _replaceHexa = createAction(_menuId++,
1236             tr("Replace hexahedron"),
1237             resMgr->loadPixmap("HEXABLOCK", tr("ICON_REPLACE_HEXA")),
1238             tr("Replace Hexahedron"),
1239             tr("Replace hexahedron"),
1240             0, aParent, false, this, SLOT(replaceHexa()));
1241
1242     _quadRevolution = createAction(_menuId++,
1243             tr("Quad revolution"),
1244             resMgr->loadPixmap("HEXABLOCK", tr("ICON_QUAD_REVOLUTION")),
1245             tr("Quad Revolution"),
1246             tr("Quad revolution"),
1247             0, aParent, false, this, SLOT(quadRevolution()));
1248
1249     // Association
1250     _assocVertex = createAction(_menuId++,
1251             tr("Make vertex association"),
1252             resMgr->loadPixmap("HEXABLOCK", tr("ICON_VERTEX_ASSOCIATION")),
1253             tr("Make Vertex association"),
1254             tr("Make vertex association"),
1255             0, aParent, false, this, SLOT(assocVertex()));
1256
1257     _assocEdge = createAction(_menuId++,
1258             tr("Make edge association"),
1259             resMgr->loadPixmap("HEXABLOCK", tr("ICON_EDGE_ASSOCIATION")),
1260             tr("Make Edge Association"),
1261             tr("Make edge association"),
1262             0, aParent, false, this, SLOT(assocEdge()));
1263
1264     _assocQuad = createAction(_menuId++,
1265             tr("Make quad association"),
1266             resMgr->loadPixmap("HEXABLOCK", tr("ICON_QUAD_ASSOCIATION")),
1267             tr("Make Quad Association"),
1268             tr("Make quad association"),
1269             0, aParent, false, this, SLOT(assocQuad()));
1270
1271     // Group
1272     _addGroup = createAction(_menuId++,
1273             tr("Add a group"),
1274             resMgr->loadPixmap("HEXABLOCK", tr("ICON_ADD_GROUP")),
1275             tr("Add Group"),
1276             tr("Add a group"),
1277             0, aParent, false, this, SLOT(addGroup()));
1278
1279     _removeGroup = createAction(_menuId++,
1280             tr("Remove a group"),
1281             resMgr->loadPixmap("HEXABLOCK", tr("ICON_REMOVE_GROUP")),
1282             tr("Remove Group"),
1283             tr("Remove a group"),
1284             0, aParent, false, this, SLOT(removeGroup()));
1285
1286     // Mesh
1287     _addLaw = createAction(_menuId++,
1288             tr("Add a law"),
1289             resMgr->loadPixmap("HEXABLOCK", tr("ICON_ADD_LAW")),
1290             tr("Add Law"),
1291             tr("Add a law"),
1292             0, aParent, false, this, SLOT(addLaw()));
1293
1294     _removeLaw = createAction(_menuId++,
1295             tr("Remove a law"),
1296             resMgr->loadPixmap("HEXABLOCK", tr("ICON_REMOVE_LAW")),
1297             tr("Remove Law"),
1298             tr("Remove a law"),
1299             0, aParent, false, this, SLOT(removeLaw()));
1300
1301     _setPropagation = createAction(_menuId++,
1302             tr("Set propagation"),
1303             resMgr->loadPixmap("HEXABLOCK", tr("ICON_SET_PROPAGATION")),
1304             tr("Set Propagation"),
1305             tr("Set propagation"),
1306             0, aParent, false, this, SLOT(setPropagation()));
1307
1308     _computeMesh = createAction(_menuId++,
1309             tr("Compute mesh"),
1310             resMgr->loadPixmap("HEXABLOCK", tr("ICON_COMPUTE_MESH")),
1311             tr("Compute Mesh"),
1312             tr("Compute mesh"),
1313             0, aParent, false, this, SLOT(computeMesh()));
1314
1315     _showModelInfoAct = createAction(_menuId++,
1316             tr("Show Model Info"),
1317             resMgr->loadPixmap("HEXABLOCK", tr("ICON_MODEL_INFO")),
1318             tr("MODEL_INFO"),
1319             tr("MODEL_INFO"),
1320             0, aParent, false, this, SLOT(showModelInfo()));
1321
1322     _addShapeAct = createAction(_menuId++,
1323             tr("Add Shape"),
1324             resMgr->loadPixmap("HEXABLOCK", tr("ICON_GEOM_ASSOCIATION")),
1325             tr("ADD_SHAPE"),
1326             tr("ADD_SHAPE"),
1327             0, aParent, false, this, SLOT(addShape()));
1328
1329 //   _newAct->setShortcut( Qt::CTRL + Qt::SHIFT + Qt::Key_N ); // --- QKeySequence::New ambiguous in SALOME
1330 //   QAction* createAction(const int id,
1331 //                         const QString& toolTip,
1332 //                         const QIcon& icon,
1333 //                         const QString& menu,
1334 //                         const QString& status,
1335 //                         const int shortCut,
1336 //                         QObject* parent =0,
1337 //                         bool checkable = false,
1338 //                         QObject* receiver =0,
1339 //                         const char* member =0);
1340
1341     _sep1 =  separator();
1342     _sep2 =  separator();
1343     _sep3 =  separator();
1344     _sep4 =  separator();
1345 }
1346
1347
1348 void HEXABLOCKGUI::createMenus()
1349 {
1350     int aMenuId, subMenuId,aMenuIdMain;
1351
1352     aMenuIdMain = createMenu( tr("MEN_FILE"), -1, -1);
1353
1354     aMenuId = createMenu( tr("HEXABLOCK"), aMenuIdMain, -1, 10 );
1355     createMenu( actionId(_newAct), aMenuId );
1356     createMenu( actionId(_importAct), aMenuId );
1357     createMenu( _saveAct, aMenuId );
1358
1359
1360     int aMenuDocId = createMenu( tr( "Document" ), -1, -1, 30 );
1361     createMenu( _newAct, aMenuDocId );//, 10
1362     createMenu( _importAct, aMenuDocId );
1363     createMenu( _saveAct, aMenuDocId );
1364
1365     aMenuId = createMenu( tr("Model"), -1, -1, 30 );
1366     subMenuId = createMenu( tr( "Construction" ), aMenuId, -1 );
1367     createMenu( _addVertex, subMenuId );
1368     createMenu( _addEdge,   subMenuId );
1369     createMenu( _addQuad,   subMenuId );
1370     createMenu( _addHexa,   subMenuId );
1371     createMenu( _sep1, subMenuId);
1372
1373     // Pattern Builder
1374     createMenu( _addVector, subMenuId );
1375 //    createMenu( _addCylinder, subMenuId );
1376 //    createMenu( _addPipe, subMenuId );
1377
1378     createMenu( _sep2, subMenuId);
1379
1380     createMenu( _makeGrid,  subMenuId ); //Cartesian, Cylindrical, Spherical
1381     createMenu( _makeHemiSphere,subMenuId );
1382
1383     createMenu( _sep3, subMenuId);
1384
1385     createMenu( _makeCylinder, subMenuId );
1386     createMenu( _makePipe,     subMenuId );
1387     createMenu( _makeCylinders,subMenuId );
1388     createMenu( _makePipes,    subMenuId );
1389
1390     // Pattern Data Edition
1391     subMenuId = createMenu( tr( "Operation" ), aMenuId, -1 );
1392     createMenu( _removeHexa, subMenuId );
1393     createMenu( _prismQuad,  subMenuId );
1394     createMenu( _joinQuad,   subMenuId );
1395     createMenu( _merge,      subMenuId );
1396     createMenu( _disconnect, subMenuId );
1397     createMenu( _cutEdge,    subMenuId );
1398     createMenu( _makeTransformation, subMenuId );
1399     createMenu( _makeSymmetry,       subMenuId );
1400     createMenu( _performTransformation,   subMenuId );
1401     createMenu( _performSymmetry,         subMenuId );
1402     createMenu( _replaceHexa,    subMenuId );
1403     createMenu( _quadRevolution, subMenuId );
1404     createMenu( _sep4, aMenuId );
1405     createMenu( _showModelInfoAct, aMenuId );
1406
1407     // Association
1408     aMenuId = createMenu( tr("Association"), -1, -1, 30 );
1409     createMenu( _addShapeAct, aMenuId );
1410     createMenu( _assocVertex, aMenuId );
1411     createMenu( _assocEdge,   aMenuId );
1412     createMenu( _assocQuad,   aMenuId );
1413
1414     // Group
1415     aMenuId = createMenu( tr("Groups"), -1, -1, 30 );
1416     createMenu( _addGroup,    aMenuId );
1417     createMenu( _removeGroup, aMenuId ); //CS_TODO
1418
1419     // Law
1420     aMenuId = createMenu( tr("Mesh "), -1, -1, 30 );
1421     createMenu( _addLaw,    aMenuId );
1422     createMenu( _removeLaw, aMenuId );
1423     createMenu( _setPropagation, aMenuId );
1424     createMenu( _computeMesh, aMenuId );
1425
1426     //show model infomation menu
1427     QString aRule = "$component={'HEXABLOCK'}";
1428     popupMgr()->insert( _showModelInfoAct, -1, -1 );
1429     popupMgr()->setRule( _showModelInfoAct, aRule);
1430
1431     //associate shape to a document menu
1432     popupMgr()->insert( _addShapeAct, -1, -1 );
1433     popupMgr()->setRule( _addShapeAct, aRule );
1434
1435     //-------------------------------------------------
1436     // Display / Erase
1437     //-------------------------------------------------
1438     //  QString lc = "$";        // VSR : instead of QtxPopupSelection::defEquality();
1439     //  QString dc = "selcount"; // VSR : instead of QtxPopupSelection::defSelCountParam()
1440     //  QString isNotEmpty("numberOfNodes <> 0");
1441     //  QString aClient = QString( "%1client in {%2}" ).arg( lc ).arg( "'VTKViewer'" );
1442     //  QString aType = QString( "%1type in {%2}" ).arg( lc );
1443     ////  aType = aType.arg( mesh_part );
1444     //
1445     //  aClient = "($client in {'VTKViewer' 'ObjectBrowser'})";
1446     //  QString anActiveVTK = QString("activeView = '%1'").arg(SVTK_Viewer::Type());
1447     //  QString aSelCount = QString( "%1 > 0" ).arg( dc );
1448
1449 //    popupMgr()->insert( separator(), -1, -1 );
1450 //    QString aRule = "$component={'HEXABLOCK'}";/* and  ( type='(" + aClient + " and " +
1451 //                aType + " and " + aSelCount + " and " + anActiveVTK + " and " + isNotEmpty + " %1 ) )";*/
1452 //    //  MESSAGE("aClient = " << aClient.toStdString());
1453 //    //  MESSAGE("aType = " << aType.toStdString());
1454 //    //  MESSAGE("aSelCount = " << aSelCount.toStdString());
1455 //    //  MESSAGE("anActiveVTK = " << anActiveVTK.toStdString());
1456 //    //  MESSAGE("isNotEmpty = " << isNotEmpty.toStdString());
1457 //    //  MESSAGE("aRule = " << aRule.toStdString());
1458 //    popupMgr()->insert( _showAct, -1, -1 ); // DISPLAY
1459 //    popupMgr()->setRule( _showAct, aRule.arg( "and (not isVisible)" ), QtxPopupMgr::VisibleRule );
1460 //
1461 //    popupMgr()->insert( _hideAct, -1, -1 ); // ERASE
1462 //    popupMgr()->setRule( _hideAct, aRule.arg( "and isVisible" ), QtxPopupMgr::VisibleRule );
1463 //
1464 //    popupMgr()->insert( _showOnlyAct, -1, -1 ); // DISPLAY_ONLY
1465 //    popupMgr()->setRule(_showOnlyAct, aRule.arg( "" ), QtxPopupMgr::VisibleRule );
1466 //
1467 //    popupMgr()->insert( separator(), -1, -1 );
1468 }
1469
1470
1471 void HEXABLOCKGUI::createTools()
1472 {
1473     int aToolId;
1474
1475     aToolId = createTool ( tr( "HexaBlock Toolbar" ) );
1476     createTool( _newAct, aToolId );
1477     createTool( _importAct, aToolId );
1478     createTool( _saveAct, aToolId );
1479     createTool( _showModelInfoAct, aToolId );
1480     //   createTool( _testAct, aToolId );
1481     //createTool( separator(), aToolId );
1482
1483     // Pattern Data
1484     aToolId = createTool ( tr( "Construction" ) );
1485     createTool( _addVertex, aToolId );
1486     createTool( _addEdge, aToolId );
1487     createTool( _addQuad, aToolId );
1488     createTool( _addHexa, aToolId );
1489
1490     createTool( _sep1, aToolId );
1491
1492     // Pattern Builder
1493     createTool( _addVector, aToolId );
1494 //    createTool( _addCylinder, aToolId );
1495 //    createTool( _addPipe, aToolId );
1496
1497     createTool( _sep2, aToolId );
1498
1499     createTool( _makeGrid,  aToolId );
1500     createTool( _makeHemiSphere,   aToolId );
1501
1502     createTool( _sep3, aToolId );
1503
1504     createTool( _makeCylinder, aToolId );
1505     createTool( _makePipe,     aToolId );
1506     createTool( _makeCylinders,aToolId );
1507     createTool( _makePipes,    aToolId );
1508
1509     // Pattern Data Edition
1510     aToolId = createTool ( tr( "Operation" ) );
1511     createTool( _removeHexa, aToolId );
1512     createTool( _prismQuad,  aToolId );
1513     createTool( _joinQuad,   aToolId );
1514     createTool( _merge,      aToolId );
1515     createTool( _disconnect, aToolId );
1516     createTool( _cutEdge,    aToolId );
1517     createTool( _makeTransformation, aToolId );
1518     createTool( _makeSymmetry,       aToolId );
1519     createTool( _performTransformation,   aToolId );
1520     createTool( _performSymmetry,         aToolId );
1521     createTool( _replaceHexa,    aToolId );
1522     createTool( _quadRevolution, aToolId );
1523
1524     // Association
1525     aToolId = createTool ( tr( "Association" ) );
1526     createTool( _assocVertex, aToolId );
1527     createTool( _assocEdge, aToolId );
1528     createTool( _assocQuad, aToolId );
1529     createTool( _addShapeAct, aToolId );
1530
1531     // Group
1532     aToolId = createTool ( tr( "Group" ) );
1533     createTool( _addGroup,    aToolId );
1534     createTool( _removeGroup, aToolId ); //CS_TODO
1535
1536     // Law
1537     aToolId = createTool ( tr( "Mesh" ) );
1538     createTool( _addLaw,    aToolId );
1539     createTool( _removeLaw, aToolId );
1540     createTool( _setPropagation, aToolId );
1541     createTool( _computeMesh, aToolId );
1542
1543 }
1544
1545 void HEXABLOCKGUI::initialMenus()
1546 {
1547     showDocumentMenus( true );
1548     showPatternMenus( false );
1549     showAssociationMenus( false );
1550     showGroupsMenus( false );
1551     showMeshMenus( false );
1552 }
1553
1554 void HEXABLOCKGUI::showAllMenus()
1555 {
1556     showDocumentMenus( true );
1557     showPatternMenus( true );
1558     showAssociationMenus( true );
1559     showGroupsMenus( true );
1560     showMeshMenus( true );
1561 }
1562
1563
1564 void HEXABLOCKGUI::showDocumentMenus(bool show)
1565 {
1566     DEBTRACE("HEXABLOCKGUI::showDocumentMenus " << show);
1567     show = true; //
1568     setMenuShown(_newAct, show);
1569     setToolShown(_newAct, show);
1570     setMenuShown(_importAct, show);
1571     setToolShown(_importAct, show);
1572     setMenuShown(_showModelInfoAct, show);
1573     setToolShown(_showModelInfoAct, show);
1574     setMenuShown(_saveAct, show);
1575     setToolShown(_saveAct, show);
1576 }
1577
1578 void HEXABLOCKGUI::showPatternMenus(bool show)
1579 {
1580     DEBTRACE("HEXABLOCKGUI::showPatternMenus " << show);
1581     DEBTRACE("getCurrentModel()  " << getCurrentModel()  );
1582     if ( show && !getCurrentModel() ) return;
1583
1584     setMenuShown(_addVertex, show );
1585     setToolShown(_addVertex, show);
1586     setMenuShown(_addEdge,  show );
1587     setToolShown(_addEdge, show);
1588     setMenuShown(_addQuad,  show );
1589     setToolShown(_addQuad, show);
1590     setMenuShown(_addHexa,  show );
1591     setToolShown(_addHexa, show);
1592
1593     setMenuShown(_sep1, show );
1594     setToolShown(_sep1, show);
1595
1596     setMenuShown( _addVector,  show );
1597     setToolShown( _addVector, show);
1598 //    setMenuShown( _addCylinder,  show );
1599 //    setToolShown( _addCylinder, show);
1600 //    setMenuShown( _addPipe,  show );
1601 //    setToolShown( _addPipe, show);
1602
1603     setMenuShown(_sep2, show );
1604     setToolShown(_sep2, show);
1605
1606     setMenuShown( _makeGrid,  show );
1607     setToolShown( _makeGrid, show);
1608     setMenuShown( _makePipe,  show );
1609     setToolShown( _makePipe, show);
1610
1611     setMenuShown(_sep3, show );
1612     setToolShown(_sep3, show);
1613
1614     setMenuShown( _makeCylinder,  show );
1615     setToolShown( _makeCylinder, show);
1616     setMenuShown( _makeCylinders,  show );
1617     setToolShown( _makeCylinders, show);
1618     setMenuShown( _makePipes,  show );
1619     setToolShown( _makePipes, show);
1620     setMenuShown( _makeHemiSphere,  show );
1621     setToolShown( _makeHemiSphere, show);
1622
1623     // Pattern Data Edition
1624     setMenuShown( _removeHexa,  show );
1625     setToolShown( _removeHexa, show);
1626     setMenuShown( _prismQuad,  show );
1627     setToolShown( _prismQuad, show);
1628     setMenuShown( _joinQuad,  show );
1629     setToolShown( _joinQuad, show);
1630     setMenuShown( _merge,  show );
1631     setToolShown( _merge, show);
1632     setMenuShown( _disconnect,  show );
1633     setToolShown( _disconnect, show);
1634     setMenuShown( _cutEdge,  show );
1635     setToolShown( _cutEdge, show);
1636     setMenuShown( _makeTransformation,  show );
1637     setToolShown( _makeTransformation, show);
1638     setMenuShown( _makeSymmetry,  show );
1639     setToolShown( _makeSymmetry, show);
1640     setMenuShown( _performTransformation,  show );
1641     setToolShown( _performTransformation, show);
1642     setMenuShown( _performSymmetry,  show );
1643     setToolShown( _performSymmetry, show);
1644     setMenuShown( _replaceHexa,  show );
1645     setToolShown( _replaceHexa, show);
1646     setMenuShown( _quadRevolution,  show );
1647     setToolShown( _quadRevolution, show);
1648
1649     setMenuShown( _sep4, show );
1650     setMenuShown( _showModelInfoAct, show );
1651 }
1652
1653
1654 void HEXABLOCKGUI::showAssociationMenus(bool show)
1655 {
1656     DEBTRACE("HEXABLOCKGUI::showAssociationMenus" << show);
1657     if ( show && !getCurrentModel() ) return;
1658
1659     // Association Edition
1660     setMenuShown( _assocVertex,  show );
1661     setToolShown( _assocVertex, show );
1662     setMenuShown( _assocEdge,  show );
1663     setToolShown( _assocEdge, show );
1664     setMenuShown( _assocQuad,  show );
1665     setToolShown( _assocQuad, show );
1666     setMenuShown( _addShapeAct, show );
1667     setToolShown( _addShapeAct, show );
1668
1669 }
1670
1671 void HEXABLOCKGUI::showGroupsMenus(bool show)
1672 {
1673     DEBTRACE("HEXABLOCKGUI::showGroupsMenus" << show);
1674     if ( show && !getCurrentModel() ) return;
1675     setMenuShown( _addGroup,  show );
1676     setToolShown( _addGroup, show);
1677     setMenuShown( _removeGroup ,  show );
1678     setToolShown( _removeGroup , show);
1679 }
1680
1681 void HEXABLOCKGUI::showMeshMenus(bool show)
1682 {
1683     DEBTRACE("HEXABLOCKGUI::showMeshMenus" << show);
1684     if ( show && !getCurrentModel() ) return;
1685     setMenuShown( _addLaw,  show );
1686     setToolShown( _addLaw, show);
1687     setMenuShown( _removeLaw,  show );
1688     setToolShown( _removeLaw, show);;
1689     setMenuShown( _setPropagation,  show );
1690     setToolShown( _setPropagation, show);
1691     setMenuShown( _computeMesh, show);
1692     setToolShown( _computeMesh, show);
1693 }
1694
1695 void HEXABLOCKGUI::showVtkActor()
1696 {
1697     VtkDocumentGraphicView* currentVtkGView = getCurrentVtkGraphicView();
1698     if (currentVtkGView == NULL || currentVtkGView->isEmpty() ||
1699             currentVtkGView->getDocumentActor() == NULL)
1700         return;
1701
1702     SVTK_ViewWindow* vtkView = currentVtkGView->getViewWindow();
1703     if (vtkView == NULL)
1704         return;
1705
1706     SUIT_ViewManager* vman = vtkView->getViewManager();
1707     SalomeApp_Study* aStudy = HEXABLOCKGUI::activeStudy();
1708
1709     //show only the current actor -----------------
1710     vtkView->setFocus();
1711     //    vtkView->DisplayOnly(currentVtkGView->getDocumentActor()->getIO());
1712     Document_Actor *lastDocActor;
1713     if (lastVtkDocGView != NULL)
1714     {
1715         lastDocActor = lastVtkDocGView->getDocumentActor();
1716         Handle(SALOME_InteractiveObject) lastActorIO = lastDocActor->getIO();
1717         if (!lastActorIO.IsNull() && lastActorIO->hasEntry())
1718         {
1719             vtkView->Erase(lastActorIO);
1720             aStudy->setObjectProperty(vman->getId(), lastActorIO->getEntry(),  "Visibility", 0 );
1721             displayer()->setVisibilityState(lastActorIO->getEntry(), Qtx::HiddenState);
1722         }
1723     }
1724     currentVtkGView->update();
1725     Handle(SALOME_InteractiveObject) anIO = currentVtkGView->getDocumentActor()->getIO();
1726     if (!anIO.IsNull() && anIO->hasEntry())
1727     {
1728         vtkView->Display(anIO);
1729         aStudy->setObjectProperty(vman->getId(), anIO->getEntry(),  "Visibility", 1 );
1730         displayer()->setVisibilityState(anIO->getEntry(), Qtx::ShownState);
1731     }
1732     vtkView->onFitAll();
1733 }
1734
1735 void HEXABLOCKGUI::showOccActor()
1736 {
1737     if (currentOccGView == NULL)
1738         return;
1739     OCCViewer_ViewWindow* occView = currentOccGView->getViewWindow();
1740     if (occView == NULL)
1741         return;
1742     SALOME_View* vf = dynamic_cast<SALOME_View*>(occView->getViewManager()->getViewModel());
1743     if (vf == NULL)
1744         return;
1745
1746     if (lastOccPrs != NULL)
1747         vf->Erase(lastOccPrs);
1748     currentOccGView->globalSelection();
1749     SOCC_Prs* prs = getOccPrs(currentDocGView);
1750     currentOccGView->setPrs(prs);
1751     vf->Display(prs);
1752     vf->Repaint();
1753     occView->onFitAll();
1754 }
1755
1756 void HEXABLOCKGUI::hideVtkActor()
1757 {
1758     VtkDocumentGraphicView* currentVtkGView = getCurrentVtkGraphicView();
1759     if (currentVtkGView == NULL || currentVtkGView->isEmpty() ||
1760         currentVtkGView->getViewWindow() == NULL ||
1761         currentVtkGView->getDocumentActor() == NULL) return;
1762
1763     currentVtkGView->getViewWindow()->Erase(currentVtkGView->getDocumentActor()->getIO());
1764     currentVtkGView->getViewWindow()->onResetView();
1765
1766     //update the visibility state now
1767     SalomeApp_Study* aStudy = HEXABLOCKGUI::activeStudy();
1768     SUIT_ViewManager* vman = currentVtkGView->getViewWindow()->getViewManager();
1769     if (aStudy == NULL || vman == NULL) return;
1770
1771     Handle(SALOME_InteractiveObject) anIO = currentVtkGView->getDocumentActor()->getIO();
1772     aStudy->setObjectProperty(vman->getId(), anIO->getEntry(),  "Visibility", 0 );
1773     displayer()->setVisibilityState(anIO->getEntry(), Qtx::HiddenState);
1774 }
1775
1776 void HEXABLOCKGUI::hideOccActor()
1777 {
1778     OCCViewer_ViewWindow* occView = currentOccGView == NULL ? NULL : currentOccGView->getViewWindow();
1779     VtkDocumentGraphicView* currentVtkGView = getCurrentVtkGraphicView();
1780     DocumentModel* docModel = (currentVtkGView == NULL ? NULL : currentVtkGView->getDocumentModel());
1781     if (occView == NULL || docModel == NULL)
1782         return;
1783     SALOME_View* vf = dynamic_cast<SALOME_View*>(occView->getViewManager()->getViewModel());
1784     if (vf == NULL)
1785         return;
1786
1787     SOCC_Prs* currentOccPrs = getOccPrs(currentDocGView);
1788     if (currentOccPrs != NULL)
1789         vf->Erase(currentOccPrs);
1790     vf->Repaint();
1791     occView->onResetView();
1792 }
1793
1794 void HEXABLOCKGUI::showOnlyActor()
1795 {
1796     showVtkActor();
1797     showOccActor();
1798 }
1799
1800 void HEXABLOCKGUI::hideActor()
1801 {
1802     hideVtkActor();
1803     hideOccActor();
1804 }
1805
1806
1807 void HEXABLOCKGUI::showDockWidgets(bool isVisible)
1808 {
1809     DEBTRACE("HEXABLOCKGUI::showDockWidgets " << isVisible);
1810
1811     if (_dwObjectBrowser) _dwObjectBrowser->setVisible(isVisible);
1812     if (_dwObjectBrowser) _dwObjectBrowser->toggleViewAction()->setVisible(isVisible);
1813
1814     if (_dwPattern) _dwPattern->setVisible(isVisible);
1815     if (_dwPattern) _dwPattern->toggleViewAction()->setVisible(isVisible);
1816
1817     if (_dwMesh) _dwMesh->setVisible(isVisible);
1818     if (_dwMesh) _dwMesh->toggleViewAction()->setVisible(isVisible);
1819
1820     if (_dwGroups) _dwGroups->setVisible(isVisible);
1821     if (_dwGroups) _dwGroups->toggleViewAction()->setVisible(isVisible);
1822 }
1823
1824
1825 //clear all used dialogs by the current model
1826 void HEXABLOCKGUI::clearDialogs()
1827 {
1828     std::set<HexaBaseDialog*>::const_iterator debut (currentModelDialogs.begin()),
1829                                               fin   (currentModelDialogs.end());
1830     for(;debut!=fin;++debut) (*debut)->clear();
1831
1832     currentModelDialogs.clear(); //empty the used dialogs list
1833 }
1834
1835 HexaBaseDialog* HEXABLOCKGUI::getDlgBox(VtkDocumentGraphicView* dgview)
1836 {
1837     if (!gViewDlgBox.contains(dgview)) return NULL;
1838     return gViewDlgBox[dgview];
1839 }
1840
1841 void HEXABLOCKGUI::switchOnGraphicView(VtkDocumentGraphicView* dgview)
1842 {
1843    if (dgview == NULL) return;
1844
1845    switchOffGraphicView(dgview, false); //to avoid double connect
1846
1847    //connect signals
1848    connect( selectionMgr(), SIGNAL( currentSelectionChanged() ),
1849          dgview->getPatternDataSelectionModel(), SLOT( salomeSelectionChanged() ), Qt::UniqueConnection );
1850    connect( dgview->getPatternDataSelectionModel(), SIGNAL( selectionChanged(const QItemSelection &, const QItemSelection &) ),
1851          this, SLOT( onSelectionChanged(const QItemSelection &, const QItemSelection &) ), Qt::UniqueConnection );
1852 //   connect( dgview->getPatternBuilderSelectionModel(), SIGNAL( selectionChanged(const QItemSelection &, const QItemSelection &) ),
1853 //         this, SLOT( onSelectionChanged(const QItemSelection &, const QItemSelection &) ), Qt::UniqueConnection );
1854    connect( dgview->getPatternGeomSelectionModel(), SIGNAL( selectionChanged(const QItemSelection &, const QItemSelection &) ),
1855             this, SLOT( onSelectionChanged(const QItemSelection &, const QItemSelection &) ), Qt::UniqueConnection );
1856    connect( dgview->getGroupsSelectionModel(), SIGNAL( selectionChanged(const QItemSelection &, const QItemSelection &) ),
1857          this, SLOT( onSelectionChanged(const QItemSelection &, const QItemSelection &) ), Qt::UniqueConnection );
1858    connect( dgview->getMeshSelectionModel(), SIGNAL( selectionChanged(const QItemSelection &, const QItemSelection &) ),
1859          this, SLOT( onSelectionChanged(const QItemSelection &, const QItemSelection &) ), Qt::UniqueConnection );
1860
1861    showAllMenus();
1862 }
1863
1864 void HEXABLOCKGUI::switchOffGraphicView(VtkDocumentGraphicView* dgview, bool saveCurrentDlg)
1865 {
1866    if (dgview == NULL) return;
1867
1868    //disconnect signals
1869    disconnect( selectionMgr(), SIGNAL( currentSelectionChanged() ),
1870          dgview->getPatternDataSelectionModel(), SLOT( salomeSelectionChanged() ) );
1871    disconnect( dgview->getPatternDataSelectionModel(), SIGNAL( selectionChanged(const QItemSelection &, const QItemSelection &) ),
1872          this, SLOT( onSelectionChanged(const QItemSelection &, const QItemSelection &) ) );
1873 //   disconnect( dgview->getPatternBuilderSelectionModel(), SIGNAL( selectionChanged(const QItemSelection &, const QItemSelection &) ),
1874 //         this, SLOT( onSelectionChanged(const QItemSelection &, const QItemSelection &) ) );
1875    disconnect( dgview->getPatternGeomSelectionModel(), SIGNAL( selectionChanged(const QItemSelection &, const QItemSelection &) ),
1876             this, SLOT( onSelectionChanged(const QItemSelection &, const QItemSelection &) ) );
1877    disconnect( dgview->getGroupsSelectionModel(), SIGNAL( selectionChanged(const QItemSelection &, const QItemSelection &) ),
1878          this, SLOT( onSelectionChanged(const QItemSelection &, const QItemSelection &) ) );
1879    disconnect( dgview->getMeshSelectionModel(), SIGNAL( selectionChanged(const QItemSelection &, const QItemSelection &) ),
1880          this, SLOT( onSelectionChanged(const QItemSelection &, const QItemSelection &) ) );
1881
1882    if (saveCurrentDlg)
1883        gViewDlgBox[dgview] = currentDialog;
1884
1885    //close opened dialog
1886    if (currentDialog != NULL) currentDialog->close();
1887 //   if (_treeViewDelegate != NULL) _treeViewDelegate->closeDialog();
1888
1889    initialMenus();
1890
1891 }
1892
1893 void HEXABLOCKGUI::switchModel(VtkDocumentGraphicView* dgview)
1894 {
1895     DEBTRACE("HEXABLOCKGUI::switchModel " << dgview);
1896
1897     if (dgview == NULL)
1898     {
1899         MESSAGE("HEXABLOCKGUI::switchModel : no need to switch!");
1900         return;
1901     }
1902
1903     //switch off the current document graphic view (disconnect signals...)
1904     if (currentDocGView != NULL)
1905     {
1906         //Hide current actor
1907         hideActor();
1908         switchOffGraphicView(currentDocGView);
1909     }
1910
1911     //clear the dialogs used by the current model so they can be used by the new model
1912     if (currentDocGView != dgview) clearDialogs();
1913
1914     _patternDataTreeView->setModel(dgview->getPatternDataModel());
1915 //    _patternBuilderTreeView->setModel(dgview->getPatternBuilderModel());
1916     _patternGeomTreeView->setModel(dgview->getPatternGeomModel());
1917     _groupsTreeView->setModel(dgview->getGroupsModel());
1918     _meshTreeView->setModel(dgview->getMeshModel());
1919
1920     _patternDataTreeView->setSelectionModel(dgview->getPatternDataSelectionModel());
1921     _patternDataTreeView->setEditTriggers(QAbstractItemView::NoEditTriggers);
1922
1923 //    _patternBuilderTreeView->setSelectionModel(dgview->getPatternBuilderSelectionModel());
1924 //    _patternBuilderTreeView->setEditTriggers(QAbstractItemView::AllEditTriggers);
1925
1926     _patternGeomTreeView->setSelectionModel(dgview->getPatternGeomSelectionModel());
1927     _patternGeomTreeView->setEditTriggers(QAbstractItemView::NoEditTriggers);
1928
1929     _groupsTreeView->setSelectionModel(dgview->getGroupsSelectionModel());
1930     _groupsTreeView->setEditTriggers(QAbstractItemView::NoEditTriggers);
1931
1932     _meshTreeView->setSelectionModel(dgview->getMeshSelectionModel());
1933     _meshTreeView->setEditTriggers(QAbstractItemView::NoEditTriggers);
1934
1935
1936     // = * init occ view * =
1937     if (currentOccGView != NULL && currentOccGView->getViewWindow() == NULL)
1938     {
1939         currentOccGView->setViewWindow(graphicViewsHandler->createOccWindow());
1940         currentOccGView->getViewWindow()->installEventFilter(this);
1941     }
1942     // ==
1943
1944
1945     // = * init vtk view * =
1946     if (currentDocGView != NULL)
1947     {
1948         if (currentDocGView->getViewWindow() != NULL)
1949             dgview->setViewWindow(currentDocGView->getViewWindow());
1950         else
1951         {
1952             dgview->setViewWindow(graphicViewsHandler->createVtkWindow());
1953             dgview->getViewWindow()->installEventFilter(this);
1954         }
1955     }
1956
1957     // ==
1958
1959     switchOnGraphicView(dgview);
1960
1961     if (currentDocGView != NULL)
1962     {
1963         lastVtkDocGView = currentDocGView;
1964         lastOccPrs = getOccPrs(currentDocGView);
1965     }
1966     else
1967     {
1968         lastVtkDocGView = dgview;
1969         lastOccPrs = getOccPrs(dgview);
1970     }
1971
1972     currentDocGView = dgview;
1973     showOnlyActor();
1974     currentDocGView->getDocumentModel()->refresh();
1975     _dwPattern->setWindowTitle(currentDocGView->getDocumentModel()->getName());
1976     showAllMenus();
1977 }
1978
1979 DocumentModel* HEXABLOCKGUI::getCurrentModel()
1980 {
1981     if (currentDocGView == NULL) return NULL;
1982     return currentDocGView->getDocumentModel();
1983 }
1984
1985
1986 HEXABLOCKGUI::ViewType HEXABLOCKGUI::getActiveViewType()
1987 {
1988     SalomeApp_Application* anApp = dynamic_cast<SalomeApp_Application*>( SUIT_Session::session()->activeApplication() );
1989     if (anApp == NULL) return UNKNOWN;
1990     SUIT_ViewManager* activeVm = anApp->activeViewManager();
1991     if (activeVm == NULL) return UNKNOWN;
1992
1993    QString vmType = activeVm->getType();
1994    if ( (vmType == SVTK_Viewer::Type()) || (vmType == VTKViewer_Viewer::Type()) )
1995        return VTK;
1996    else if ( vmType == OCCViewer_Viewer::Type() )
1997        return OCC;
1998
1999    return UNKNOWN;
2000 }
2001
2002 HEXABLOCKGUI* HEXABLOCKGUI::getInstance()
2003 {
2004     SalomeApp_Application* anApp = dynamic_cast<SalomeApp_Application*>( SUIT_Session::session()->activeApplication() );
2005     if (anApp == NULL)
2006         return NULL;
2007
2008     return  dynamic_cast<HEXABLOCKGUI*>(anApp->activeModule());
2009 }
2010
2011 pair <QString, HEXA_NS::Document*> HEXABLOCKGUI::newHexaDocument()
2012 {
2013     // Create Document from HEXABLOCK ENGINE
2014     // WARNING : IN HEXABLOCK component,  GUI and ENGINE share the same process
2015     // HEXABLOCK_ORB::Document_ptr docIn = _hexaEngine->addDocument("default"); // Perime  Hexa6
2016
2017     // looking doc impl ( c++ )
2018     // ^Hexa6 Document_impl* dServant = DownCast<Document_impl*>( docIn );
2019     // ^Hexa6 ASSERT( dServant );
2020     // ^Hexa6 if ( dServant)
2021         // ^Hexa6 doc = dServant->GetImpl();
2022
2023     HEXA_NS::Document* doc = hexa_root->addDocument ("default");
2024     QString docEntry = addDocInStudy (doc);
2025
2026     // looking for docEntry
2027     /* ****************************************************************
2028     if ( !CORBA::is_nil(docIn) ){
2029         CORBA::String_var anIOR = SalomeApp_Application::orb()->object_to_string( docIn );
2030         QString docIOR = anIOR.in();
2031         SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
2032         if ( !docIOR.isEmpty() ) {
2033             _PTR(SObject) SO( study->studyDS()->FindObjectIOR( docIOR.toLatin1().constData() ) );
2034             if ( SO )
2035                 docEntry = SO->GetID().c_str();
2036         }
2037     }
2038        **************************************************************** */
2039     DEBTRACE("HEXABLOCKGUI::newHexaDocument docEntry "<<docEntry.toStdString());
2040
2041     return make_pair (docEntry, doc);
2042 }
2043
2044
2045 void HEXABLOCKGUI::newDocument()
2046 {
2047     DEBTRACE("HEXABLOCKGUI::newDocument");
2048
2049     createSComponent();
2050     QMainWindow *aParent = application()->desktop();
2051     QWidget *central = aParent->centralWidget();
2052     if (central)
2053         central->setFocus();
2054     else
2055         DEBTRACE("No Central Widget");
2056
2057     //Init OCC
2058     if (currentOccGView == NULL)
2059     {
2060         currentOccGView = new OccGraphicView(graphicViewsHandler->createOccWindow(),
2061                 application()->desktop());
2062         currentOccGView->getViewWindow()->installEventFilter(this);
2063     }
2064     else if (currentOccGView->getViewWindow() == NULL)
2065     {
2066         currentOccGView->setViewWindow(graphicViewsHandler->createOccWindow());
2067         currentOccGView->getViewWindow()->installEventFilter(this);
2068     }
2069
2070     //NEW DOCUMENT
2071     // Create Document from HEXABLOCK ENGINE
2072     pair <QString, HEXA_NS::Document*> docEntry_Doc ( newHexaDocument() );
2073
2074     VtkDocumentGraphicView* newGraphicView = NULL;
2075     //One document at a time
2076     if (currentDocGView != NULL)
2077     {
2078         if (currentDocGView->getViewWindow() == NULL)
2079         {
2080             currentDocGView->setViewWindow(graphicViewsHandler->createVtkWindow());
2081             currentDocGView->getViewWindow()->installEventFilter(this);
2082         }
2083
2084           // Create a new document with the current window
2085           newGraphicView = graphicViewsHandler->createDocumentGraphicView(
2086                                         new DocumentModel( docEntry_Doc.second,
2087                                         docEntry_Doc.first, this ),
2088                                         currentDocGView->getViewWindow(),
2089                                         application()->desktop() );
2090     }
2091     else
2092     {
2093           // Create the new Document Graphic View with a new window
2094           newGraphicView = graphicViewsHandler->createDocumentGraphicView(
2095                                         new DocumentModel( docEntry_Doc.second,
2096                                         docEntry_Doc.first, this ),
2097                                         graphicViewsHandler->createVtkWindow(),
2098                                         application()->desktop() );
2099           newGraphicView->getViewWindow()->installEventFilter(this);
2100     }
2101
2102     if (newGraphicView == NULL)
2103         //We couldn't create a new document graphic view
2104         //we'd better show a dialog box info to inform the user
2105         return;
2106
2107     docs[docEntry_Doc.first] = newGraphicView;
2108     switchModel(newGraphicView); //select the corresponding document in the browser instead
2109
2110     getApp()->updateObjectBrowser();
2111     showAllMenus();
2112 }
2113
2114
2115 //Loads the model from the xmlFile in the current graphic view
2116 void HEXABLOCKGUI::loadDocument( const QString &inFile )
2117 {
2118     DEBTRACE("HEXABLOCKGUI::loadDocument");
2119
2120     createSComponent();
2121     QMainWindow *aParent = application()->desktop();
2122     QString selectedFile;
2123
2124     if ( inFile.isNull() ){
2125         QFileDialog dialog( aParent, tr("Open HexaBlock Document") ,
2126                 QString::null, tr( "XML-Files (*.xml);;All Files (*)" ) );
2127         dialog.setHistory( getQuickDirList() );
2128
2129         //use the last path as default
2130         if (!(loadDocLastPath->absolutePath()).isEmpty())
2131             dialog.setDirectory(*loadDocLastPath);
2132
2133         if (dialog.exec()){
2134             QStringList selectedFiles = dialog.selectedFiles();
2135             if (!selectedFiles.isEmpty())
2136                 selectedFile = selectedFiles.first();
2137
2138             //remember the selected path
2139             *loadDocLastPath = dialog.directory();
2140         }
2141     } else
2142         selectedFile = inFile;
2143
2144     if (selectedFile.isEmpty())
2145         return;
2146
2147     //we create a new document if necessary
2148     if (currentDocGView == NULL)
2149         //Need a new document
2150         newDocument();
2151     else if (currentDocGView->getViewWindow() == NULL) //there's a document without a view
2152     {
2153         currentDocGView->setViewWindow(graphicViewsHandler->createVtkWindow());
2154         currentDocGView->getViewWindow()->installEventFilter(this);
2155     }
2156
2157     if (!currentDocGView->isEmpty())
2158         //we can only have one document for a graphic view
2159         newDocument();
2160
2161     //we load the selected file in the current graphic view
2162     currentDocGView->loadDocument(selectedFile);
2163     DocumentModel* currentModel = getCurrentModel();
2164     renameObject( currentModel->documentEntry(), currentModel->getName() );
2165
2166     showOnlyActor();
2167 }
2168
2169 void HEXABLOCKGUI::saveDocument()
2170 {
2171     QMainWindow *aParent = application()->desktop();
2172
2173     QString aDocName = "document", aFilename;
2174     QStringList filter;
2175     filter.append( QObject::tr( "XML_FILES_FILTER" ) + " (*.xml)" );
2176     filter.append( QObject::tr( "ALL_FILES_FILTER" ) + " (*)" );
2177
2178     //use the last selected directory as default path for file selection
2179     aFilename =  saveDocLastPath->absolutePath();
2180     if (!aFilename.isEmpty())
2181         aFilename += QString("/") + aDocName;
2182
2183     //show the dialog for file selection
2184     aFilename = SUIT_FileDlg::getFileName( aParent,
2185             aFilename,
2186             filter,
2187             tr( "Save HexaBlock Document" ),
2188             false );
2189
2190     //save the document in the selected file
2191     if ( !aFilename.isEmpty() ) {
2192         getCurrentVtkGraphicView()->saveDocument( aFilename );
2193
2194         //Remember the path for next save
2195         QFileInfo fileInfo(aFilename);
2196         *saveDocLastPath = fileInfo.dir();
2197     }
2198     //CS_TODO save doc
2199 }
2200
2201
2202 void HEXABLOCKGUI::slot_modelChanged(const QModelIndex &topLeft, const QModelIndex  &bottomRight)
2203 {
2204     _patternDataTreeView->openPersistentEditor( topLeft );
2205 }
2206
2207 void HEXABLOCKGUI::_showDialogBox( HexaBaseDialog* diag )
2208 {
2209     if (diag == NULL || _dwInputPanel == NULL ||
2210             getCurrentVtkGraphicView() == NULL) return;
2211
2212     //mark this dialog as used by the current model
2213     currentModelDialogs.insert(diag);
2214
2215     //close the current dialog box info
2216 //    if (_treeViewDelegate != NULL)
2217 //        _treeViewDelegate->closeDialog();
2218
2219 //    if (_dwInputPanel->widget())
2220 //        _dwInputPanel->widget()->close();
2221
2222     //Temporary for debugging EdgeAssoc Faked InfoDialog
2223     if (diag == _edgeAssocDiag)
2224         diag->debugEdgeAssoc = true;
2225     else
2226         diag->debugEdgeAssoc = false;
2227
2228     if (diag == _vertexAssocDiag || diag ==  _edgeAssocDiag ||
2229                 diag ==  _quadAssocDiag || diag == _addShapeDiag)
2230         assocInProgress = true;
2231     else
2232         assocInProgress = false;
2233
2234     //show the dialog box in the dockwidget
2235     diag->resetSizeAndShow(_dwInputPanel);
2236
2237     //clear the current selections
2238     selectionMgr()->clearSelected();
2239
2240     currentDialog = diag;
2241     diag->setFocus();
2242
2243     if (currentDocGView != NULL)
2244         currentDocGView->getPatternDataSelectionModel()->setInfoMode(false);
2245 }
2246
2247 void HEXABLOCKGUI::showVertexInfoDialog(HEXA_NS::Vertex* vertex)
2248 {
2249     if (vertex == NULL || _dwInputPanel == NULL)
2250         return;
2251
2252     if (_vertexInfoDialog == NULL)
2253         _vertexInfoDialog = new VertexDialog(_dwInputPanel, HexaBaseDialog::INFO_MODE);
2254
2255     _vertexInfoDialog->setValue(vertex);
2256     _vertexInfoDialog->resetSizeAndShow(_dwInputPanel);
2257     currentDialog = _vertexInfoDialog;
2258 }
2259
2260 void HEXABLOCKGUI::showEdgeInfoDialog(HEXA_NS::Edge* edge)
2261 {
2262     if (edge == NULL || _dwInputPanel == NULL)
2263         return;
2264
2265     if (_edgeInfoDialog == NULL)
2266         _edgeInfoDialog = new EdgeDialog(_dwInputPanel, HexaBaseDialog::INFO_MODE);
2267
2268     _edgeInfoDialog->setValue(edge);
2269     _edgeInfoDialog->resetSizeAndShow(_dwInputPanel);
2270     currentDialog = _edgeInfoDialog;
2271 }
2272
2273 void HEXABLOCKGUI::showQuadInfoDialog(HEXA_NS::Quad* quad)
2274 {
2275     if (quad == NULL || _dwInputPanel == NULL)
2276         return;
2277
2278     if (_quadInfoDialog == NULL)
2279         _quadInfoDialog = new QuadDialog(_dwInputPanel, HexaBaseDialog::INFO_MODE);
2280
2281     _quadInfoDialog->setValue(quad);
2282     _quadInfoDialog->resetSizeAndShow(_dwInputPanel);
2283     currentDialog = _quadInfoDialog;
2284 }
2285
2286 void HEXABLOCKGUI::showHexaInfoDialog(HEXA_NS::Hexa* hexa)
2287 {
2288     if (hexa == NULL || _dwInputPanel == NULL)
2289         return;
2290
2291     if (_hexaInfoDialog == NULL)
2292         _hexaInfoDialog = new HexaDialog(_dwInputPanel, HexaBaseDialog::INFO_MODE);
2293
2294     _hexaInfoDialog->setValue(hexa);
2295     _hexaInfoDialog->resetSizeAndShow(_dwInputPanel);
2296     currentDialog = _hexaInfoDialog;
2297 }
2298
2299 void HEXABLOCKGUI::showVectorInfoDialog(HEXA_NS::Vector* vector)
2300 {
2301     if (vector == NULL || _dwInputPanel == NULL)
2302         return;
2303
2304     if (_vectorInfoDialog == NULL)
2305         _vectorInfoDialog = new VectorDialog(_dwInputPanel, HexaBaseDialog::INFO_MODE);
2306
2307     _vectorInfoDialog->setValue(vector);
2308     _vectorInfoDialog->resetSizeAndShow(_dwInputPanel);
2309     currentDialog = _vectorInfoDialog;
2310 }
2311
2312 void HEXABLOCKGUI::showGroupInfoDialog(HEXA_NS::Group* group)
2313 {
2314     if (group == NULL || _dwInputPanel == NULL)
2315         return;
2316
2317     if (_groupInfoDialog == NULL)
2318         _groupInfoDialog = new GroupDialog(_dwInputPanel, HexaBaseDialog::INFO_MODE);
2319
2320     _groupInfoDialog->setValue(group);
2321     _groupInfoDialog->resetSizeAndShow(_dwInputPanel);
2322     currentDialog = _groupInfoDialog;
2323 }
2324
2325 void HEXABLOCKGUI::showLawInfoDialog(HEXA_NS::Law* law)
2326 {
2327     if (law == NULL || _dwInputPanel == NULL)
2328         return;
2329
2330     if (_lawInfoDialog == NULL)
2331         _lawInfoDialog = new LawDialog(_dwInputPanel, HexaBaseDialog::INFO_MODE);
2332
2333     _lawInfoDialog->setValue(law);
2334     _lawInfoDialog->resetSizeAndShow(_dwInputPanel);
2335     currentDialog = _lawInfoDialog;
2336 }
2337
2338 void HEXABLOCKGUI::showPropagationInfoDialog(HEXA_NS::Propagation* propagation)
2339 {
2340     if (propagation == NULL || _dwInputPanel == NULL)
2341         return;
2342
2343     if (_propagationInfoDialog == NULL)
2344         _propagationInfoDialog = new PropagationDialog(_dwInputPanel, HexaBaseDialog::INFO_MODE);
2345
2346     _propagationInfoDialog->setValue(propagation);
2347     _propagationInfoDialog->resetSizeAndShow(_dwInputPanel);
2348     currentDialog = _propagationInfoDialog;
2349 }
2350
2351 void HEXABLOCKGUI::addVertex()
2352 {
2353     if ( !_vertexDiag ){
2354         _vertexDiag = new VertexDialog(_dwInputPanel, HexaBaseDialog::NEW_MODE);
2355     }
2356     _showDialogBox( _vertexDiag );
2357
2358     //set default name
2359     HEXA_NS::Document* doc = getCurrentModel() ? getCurrentModel()->getHexaDocument() : NULL;
2360     if (_vertexDiag != NULL && doc != NULL)
2361         _vertexDiag->name_le->setText(doc->getNextName(HEXA_NS::EL_VERTEX).c_str());
2362 }
2363
2364 void HEXABLOCKGUI::addEdge()
2365 {
2366     if ( !_edgeDiag ){
2367         _edgeDiag = new EdgeDialog( _dwInputPanel, HexaBaseDialog::NEW_MODE);
2368     }
2369     _showDialogBox( _edgeDiag );
2370
2371     //set default name
2372     HEXA_NS::Document* doc = getCurrentModel() ? getCurrentModel()->getHexaDocument() : NULL;
2373     if (_edgeDiag != NULL && doc != NULL)
2374         _edgeDiag->name_le->setText(doc->getNextName(HEXA_NS::EL_EDGE).c_str());
2375 }
2376
2377 void HEXABLOCKGUI::addQuad()
2378 {
2379     if ( !_quadDiag ){
2380         _quadDiag = new QuadDialog(_dwInputPanel, HexaBaseDialog::NEW_MODE);
2381
2382     }
2383     _showDialogBox( _quadDiag );
2384
2385     //set default name
2386     HEXA_NS::Document* doc = getCurrentModel() ? getCurrentModel()->getHexaDocument() : NULL;
2387     if (_quadDiag != NULL && doc != NULL)
2388         _quadDiag->name_le->setText(doc->getNextName(HEXA_NS::EL_QUAD).c_str());
2389 }
2390
2391 void HEXABLOCKGUI::addHexa()
2392 {
2393     if ( !_hexaDiag ){
2394         _hexaDiag = new HexaDialog(_dwInputPanel, HexaBaseDialog::NEW_MODE);
2395     }
2396     _showDialogBox( _hexaDiag );
2397
2398     //set default name
2399     HEXA_NS::Document* doc = getCurrentModel() ? getCurrentModel()->getHexaDocument() : NULL;
2400     if (_hexaDiag != NULL && doc != NULL)
2401         _hexaDiag->name_le->setText(doc->getNextName(HEXA_NS::EL_HEXA).c_str());
2402 }
2403
2404 void HEXABLOCKGUI::addVector()
2405 {
2406     if ( !_vectorDiag ){
2407         _vectorDiag = new VectorDialog(_dwInputPanel, HexaBaseDialog::NEW_MODE);
2408     }
2409     _showDialogBox( _vectorDiag );
2410
2411     //set default name
2412     HEXA_NS::Document* doc = getCurrentModel() ? getCurrentModel()->getHexaDocument() : NULL;
2413     if (_vectorDiag != NULL && doc != NULL)
2414         _vectorDiag->name_le->setText(doc->getNextName(HEXA_NS::EL_VECTOR).c_str());
2415 }
2416
2417 //void HEXABLOCKGUI::addCylinder()
2418 //{
2419 //    if ( !_cylinderDiag ){
2420 //        _cylinderDiag = new CylinderDialog(_dwInputPanel, HexaBaseDialog::NEW_MODE);
2421 //    }
2422 //    _showDialogBox( _cylinderDiag );
2423 //
2424 //    //set default name
2425 //    HEXA_NS::Document* doc = getCurrentModel() ? getCurrentModel()->getHexaDocument() : NULL;
2426 //    if (_cylinderDiag != NULL && doc != NULL)
2427 //        _cylinderDiag->name_le->setText(doc->getNextName(HEXA_NS::EL_CYLINDER).c_str());
2428 //}
2429
2430
2431 //void HEXABLOCKGUI::addPipe()
2432 //{
2433 //    if ( !_pipeDiag){
2434 //        _pipeDiag = new PipeDialog(_dwInputPanel, HexaBaseDialog::NEW_MODE);
2435 //    }
2436 //    _showDialogBox( _pipeDiag );
2437 //
2438 //    //set default name
2439 //    HEXA_NS::Document* doc = getCurrentModel() ? getCurrentModel()->getHexaDocument() : NULL;
2440 //    if (_pipeDiag != NULL && doc != NULL)
2441 //        _pipeDiag->name_le->setText(doc->getNextName(HEXA_NS::EL_CYLINDER).c_str());
2442 //}
2443
2444
2445 void HEXABLOCKGUI::makeGrid()
2446 {
2447     if ( !_makeGridDiag ){
2448         _makeGridDiag = new MakeGridDialog(_dwInputPanel, HexaBaseDialog::NEW_MODE);
2449     }
2450     _showDialogBox( _makeGridDiag );
2451 }
2452
2453
2454 void HEXABLOCKGUI::makeCylinder()
2455 {
2456     if ( !_makeCylinderDiag ){
2457         _makeCylinderDiag = new MakeCylinderDialog(_dwInputPanel, HexaBaseDialog::NEW_MODE);
2458     }
2459     _showDialogBox( _makeCylinderDiag );
2460 }
2461
2462 void HEXABLOCKGUI::makePipe()
2463 {
2464     if ( !_makePipeDiag ){
2465         _makePipeDiag = new MakePipeDialog(_dwInputPanel, HexaBaseDialog::NEW_MODE);
2466     }
2467     _showDialogBox( _makePipeDiag );
2468 }
2469
2470 void HEXABLOCKGUI::makeCylinders()
2471 {
2472     if ( !_makeCylindersDiag ){
2473         _makeCylindersDiag = new MakeCylindersDialog(_dwInputPanel, HexaBaseDialog::NEW_MODE);
2474     }
2475     _showDialogBox( _makeCylindersDiag );
2476 }
2477
2478 void HEXABLOCKGUI::makePipes()
2479 {
2480     if ( !_makePipesDiag ){
2481         _makePipesDiag = new MakePipesDialog(_dwInputPanel, HexaBaseDialog::NEW_MODE);
2482     }
2483     _showDialogBox( _makePipesDiag );
2484 }
2485
2486
2487 void HEXABLOCKGUI::makeHemiSphere()  // NEW HEXA3
2488 {
2489     if ( !_makeHemiSphereDiag ){
2490         _makeHemiSphereDiag = new MakeHemiSphereDialog(_dwInputPanel, HexaBaseDialog::NEW_MODE);
2491     }
2492     _showDialogBox( _makeHemiSphereDiag );
2493 }
2494
2495
2496 void HEXABLOCKGUI::removeHexa()
2497 {
2498     if ( !_removeHexaDiag ){
2499         _removeHexaDiag = new RemoveHexaDialog(_dwInputPanel, HexaBaseDialog::NEW_MODE);
2500     }
2501     _showDialogBox( _removeHexaDiag );
2502 }
2503
2504
2505 void HEXABLOCKGUI::prismQuad()
2506 {
2507     if ( !_prismQuadDiag ){
2508         _prismQuadDiag = new PrismQuadDialog(_dwInputPanel, HexaBaseDialog::NEW_MODE);
2509     }
2510     _showDialogBox( _prismQuadDiag );
2511 }
2512
2513
2514 void HEXABLOCKGUI::joinQuad()
2515 {
2516     if ( !_joinQuadDiag ){
2517         _joinQuadDiag = new JoinQuadDialog(_dwInputPanel, HexaBaseDialog::NEW_MODE);
2518     }
2519     _showDialogBox( _joinQuadDiag );
2520 }
2521
2522 void HEXABLOCKGUI::merge()
2523 {
2524     if ( !_mergeDiag ){
2525         _mergeDiag = new MergeDialog(_dwInputPanel, HexaBaseDialog::NEW_MODE);
2526     }
2527     _showDialogBox( _mergeDiag );
2528 }
2529
2530 void HEXABLOCKGUI::disconnectElts()
2531 {
2532     if ( !_disconnectDiag ){
2533         _disconnectDiag = new DisconnectDialog(_dwInputPanel, HexaBaseDialog::NEW_MODE);
2534     }
2535     _showDialogBox( _disconnectDiag );
2536 }
2537
2538 void HEXABLOCKGUI::cutEdge()
2539 {
2540     if ( !_cutEdgeDiag ){
2541         _cutEdgeDiag = new CutEdgeDialog(_dwInputPanel, HexaBaseDialog::NEW_MODE);
2542     }
2543     _showDialogBox( _cutEdgeDiag );
2544 }
2545
2546 void HEXABLOCKGUI::makeTransformation()
2547 {
2548     if ( !_makeTransformationDiag ){
2549         _makeTransformationDiag = new MakeTransformationDialog(_dwInputPanel, HexaBaseDialog::NEW_MODE);
2550     }
2551     _showDialogBox( _makeTransformationDiag );
2552 }
2553
2554
2555 void HEXABLOCKGUI::makeSymmetry()
2556 {
2557     if ( !_makeSymmetryDiag ){
2558         _makeSymmetryDiag = new MakeSymmetryDialog(_dwInputPanel, HexaBaseDialog::NEW_MODE);
2559     }
2560     _showDialogBox( _makeSymmetryDiag );
2561 }
2562
2563
2564 void HEXABLOCKGUI::performTransformation()
2565 {
2566     if ( !_performTransformationDiag ){
2567         _performTransformationDiag = new PerformTransformationDialog(_dwInputPanel, HexaBaseDialog::NEW_MODE);
2568     }
2569     _showDialogBox( _performTransformationDiag );
2570 }
2571
2572
2573 void HEXABLOCKGUI::performSymmetry()
2574 {
2575     if ( !_performSymmetryDiag ){
2576         _performSymmetryDiag = new PerformSymmetryDialog(_dwInputPanel, HexaBaseDialog::NEW_MODE);
2577     }
2578     _showDialogBox( _performSymmetryDiag );
2579 }
2580
2581
2582 void HEXABLOCKGUI::replaceHexa()    // NEW HEXA3
2583 {
2584     if ( !_replaceHexaDiag ){
2585         _replaceHexaDiag = new ReplaceHexaDialog(_dwInputPanel, HexaBaseDialog::NEW_MODE);
2586     }
2587     _showDialogBox( _replaceHexaDiag );
2588 }
2589
2590
2591 void HEXABLOCKGUI::quadRevolution() // NEW HEXA3
2592 {
2593     if ( !_quadRevolutionDiag ){
2594         _quadRevolutionDiag = new QuadRevolutionDialog(_dwInputPanel, HexaBaseDialog::NEW_MODE);
2595     }
2596     _showDialogBox( _quadRevolutionDiag );
2597 }
2598
2599
2600 void HEXABLOCKGUI::assocVertex()
2601 {
2602         if (!_dwInputPanel) return;
2603         QWidget* d = dynamic_cast<SUIT_Desktop*>(_dwInputPanel->parent());
2604         if ( !_vertexAssocDiag  )
2605                 _vertexAssocDiag  = new VertexAssocDialog( d /*_dwInputPanel*/);
2606         _showDialogBox( _vertexAssocDiag );
2607 }
2608
2609
2610 void HEXABLOCKGUI::assocEdge()
2611 {
2612     if ( !_edgeAssocDiag )
2613         _edgeAssocDiag = new EdgeAssocDialog(_dwInputPanel, HexaBaseDialog::NEW_MODE);
2614     _showDialogBox( _edgeAssocDiag );
2615 }
2616
2617 void HEXABLOCKGUI::assocQuad()
2618 {
2619     if ( !_quadAssocDiag )
2620         _quadAssocDiag = new QuadAssocDialog(_dwInputPanel, HexaBaseDialog::NEW_MODE);
2621     _showDialogBox( _quadAssocDiag );
2622 }
2623
2624
2625 void HEXABLOCKGUI::addGroup()
2626 {
2627     if ( !_groupDiag ){
2628         _groupDiag = new GroupDialog(_dwInputPanel, HexaBaseDialog::NEW_MODE);
2629     }
2630     _showDialogBox( _groupDiag );
2631     if (_groupDiag != NULL && getCurrentModel() != NULL)
2632     {
2633         HEXA_NS::Document* doc = getCurrentModel()->getHexaDocument();
2634         if (doc == NULL) return;
2635         char defaultName[16];
2636         sprintf (defaultName, "g%04d", doc->countGroup());
2637         _groupDiag->name_le->setText(defaultName);
2638     }
2639 }
2640
2641 void HEXABLOCKGUI::removeGroup()
2642 {
2643     VtkDocumentGraphicView* currentDGView = getCurrentVtkGraphicView();
2644     if (currentDGView == NULL) return;
2645
2646     QItemSelectionModel *groupsSelectionModel = _groupsTreeView->selectionModel();
2647     QModelIndexList l = groupsSelectionModel->selectedIndexes();
2648     int nbGroupsRemoved = 0;
2649
2650     if ( l.isEmpty() ){
2651         SUIT_MessageBox::information( 0, tr( "CANNOT REMOVE GROUP" ),
2652                 tr( "No group selected!" ) );
2653         return;
2654     }
2655
2656     foreach( QModelIndex selected, l ){
2657         if ( selected.data(HEXA_TREE_ROLE) == GROUP_TREE ){
2658             selected = currentDGView->getGroupsModel()->mapToSource( selected );
2659             Q_ASSERT(selected.isValid());
2660
2661             //Confirm the deletion of the group
2662             if (SUIT_MessageBox::question(
2663                     0,
2664                     tr("Remove Group"),
2665                     tr("Remove group : %1 ?").arg(selected.data().toString()),
2666                     SUIT_MessageBox::Ok | SUIT_MessageBox::Cancel,
2667                     SUIT_MessageBox::Cancel
2668             ) == SUIT_MessageBox::Cancel) return;
2669
2670             bool removed = getCurrentModel()->removeGroup( selected );
2671             if ( !removed ) {
2672                 SUIT_MessageBox::critical( 0, tr( "ERR_ERROR" ),
2673                         tr( "CANNOT REMOVE %1" ).arg(selected.data().toString()) );
2674                 return;
2675             }
2676             nbGroupsRemoved++;
2677         }
2678     }
2679     if (!nbGroupsRemoved)
2680         SUIT_MessageBox::information( 0, tr( "CANNOT REMOVE GROUP" ),
2681                 tr( "No group selected!" ) );
2682 }
2683
2684 void HEXABLOCKGUI::addLaw()
2685 {
2686     if ( !_lawDiag ){
2687         _lawDiag = new LawDialog(_dwInputPanel, HexaBaseDialog::NEW_MODE);
2688     }
2689     _showDialogBox( _lawDiag );
2690     if (_lawDiag != NULL && getCurrentModel() != NULL)
2691     {
2692         HEXA_NS::Document* doc = getCurrentModel()->getHexaDocument();
2693         if (doc == NULL) return;
2694         char defaultName[16];
2695         sprintf (defaultName, "l%04d", doc->countLaw());
2696         _lawDiag->name_le->setText(defaultName);
2697     }
2698 }
2699
2700 void HEXABLOCKGUI::removeLaw()
2701 {
2702     VtkDocumentGraphicView* currentDGView = getCurrentVtkGraphicView();
2703     if (currentDGView == NULL) return;
2704
2705     QModelIndexList l = currentDGView->getMeshSelectionModel()->selectedIndexes();
2706     int nbLawsRemoved = 0;
2707
2708     if ( l.isEmpty() ){
2709         SUIT_MessageBox::information( 0, tr( "CANNOT REMOVE LAW" ),
2710                 tr( "No law selected!" ) );
2711         return;
2712     }
2713
2714     foreach( QModelIndex selected, l ){
2715         if ( selected.data(HEXA_TREE_ROLE) == LAW_TREE ){
2716             selected = currentDGView->getMeshModel()->mapToSource( selected );
2717             Q_ASSERT(selected.isValid());
2718
2719             //Confirm the deletion of the law
2720             if (SUIT_MessageBox::question(
2721                     0,
2722                     tr("Remove Law"),
2723                     tr("Remove law : %1 ?\nAll propagations having this law will have the default law.").arg(selected.data().toString()),
2724                     SUIT_MessageBox::Ok | SUIT_MessageBox::Cancel,
2725                     SUIT_MessageBox::Cancel
2726             ) == SUIT_MessageBox::Cancel) return;
2727
2728             bool removed = getCurrentModel()->removeLaw(selected);
2729             if ( !removed ) {
2730                 SUIT_MessageBox::critical( 0, tr( "ERR_ERROR" ),
2731                         tr( "CANNOT REMOVE %1" ).arg(selected.data().toString()) );
2732                 return;
2733             }
2734             nbLawsRemoved++;
2735         }
2736     }
2737     if (!nbLawsRemoved)
2738         SUIT_MessageBox::information( 0, tr( "CANNOT REMOVE LAW" ),
2739                                          tr( "No law selected!" ) );
2740
2741 }
2742
2743
2744 void HEXABLOCKGUI::setPropagation()
2745 {
2746     if ( !_propagationDiag )
2747         _propagationDiag = new PropagationDialog(_dwInputPanel, HexaBaseDialog::NEW_MODE);
2748
2749     _showDialogBox( _propagationDiag );
2750 }
2751
2752 // Dialog box to compute a mesh from a document
2753 // --------------------------------------------
2754 void HEXABLOCKGUI::computeMesh()
2755 {
2756     if ( !_computeMeshDiag )
2757         _computeMeshDiag = new ComputeMeshDialog(_dwInputPanel, HexaBaseDialog::NEW_MODE);
2758
2759     _showDialogBox( _computeMeshDiag );
2760 }
2761
2762 void HEXABLOCKGUI::clearAssociations()
2763 {
2764     VtkDocumentGraphicView* currentDGView = getCurrentVtkGraphicView();
2765     if (currentDGView == NULL) return;
2766
2767     //   QMessageBox::warning( 0, "windowTitle()", "clearAssociations" );
2768     QModelIndex iDataModel = _patternDataTreeView->currentIndex();
2769     QModelIndex iModel     = currentDGView->getPatternDataModel()->mapToSource(iDataModel);
2770
2771     getCurrentModel()->clearEltAssociations(iModel);
2772
2773     //  SUIT_MessageBox::information( 0, tr( "HEXA_INFO" ), tr( "ASSOCIATION CLEARED" ) );
2774 }
2775
2776 void HEXABLOCKGUI::showModelInfo()
2777 {
2778     //create the info dialog if not yet
2779     if (_modelInfoDiag == NULL)
2780         _modelInfoDiag = new ModelInfoDialog(_dwInputPanel);
2781
2782     //show the model informations dialog
2783     _showDialogBox( _modelInfoDiag );
2784 }
2785
2786 void HEXABLOCKGUI::addShape()
2787 {
2788     //create the info dialog if not yet
2789     if (_addShapeDiag == NULL)
2790         _addShapeDiag = new AddShapeDialog(_dwInputPanel);
2791
2792     //show the model informations dialog
2793     _showDialogBox( _addShapeDiag );
2794 }
2795
2796 void HEXABLOCKGUI::clearAllAssociations()
2797 {
2798     VtkDocumentGraphicView* currentDGView = getCurrentVtkGraphicView();
2799     if (currentDGView == NULL) return;
2800
2801     QModelIndex currentIndex = currentDGView->getPatternDataModel()->mapToSource(_patternDataTreeView->currentIndex());
2802     if (!currentIndex.isValid()) return;
2803     QVariant currentAssocVariant;
2804     QString currentAssocEntry;
2805
2806     //Confirm the deletion of the associations
2807     QVariant treeVariant = currentIndex.data( HEXA_TREE_ROLE );
2808     if ( !treeVariant.isValid() ) return;
2809     int eltType = treeVariant.toInt();
2810     QString typeStr = "";
2811     HEXA_NS::EnumElt hexaType;
2812     if (eltType == VERTEX_DIR_TREE)
2813     {
2814         typeStr = tr("TREE_ITEM_VERTEX").toUpper();
2815         hexaType = HEXA_NS::EL_VERTEX;
2816     }
2817     else if (eltType == EDGE_DIR_TREE)
2818     {
2819         typeStr = tr("TREE_ITEM_EDGE").toUpper();
2820         hexaType = HEXA_NS::EL_EDGE;
2821     }
2822     else if (eltType == QUAD_DIR_TREE)
2823     {
2824         typeStr = tr("TREE_ITEM_QUAD").toUpper();
2825         hexaType = HEXA_NS::EL_QUAD;
2826     }
2827
2828     if (SUIT_MessageBox::question(
2829             0,
2830             tr("CLEAR_ASSOCIATIONS"),
2831             tr("CLEAR_ALL_ASSOCIATIONS_ON") + " " + typeStr + "?",
2832             SUIT_MessageBox::Ok | SUIT_MessageBox::Cancel,
2833             SUIT_MessageBox::Cancel
2834     ) == SUIT_MessageBox::Cancel) return;
2835
2836
2837     DocumentModel* docModel = getCurrentModel();
2838     if (docModel == NULL) return;
2839     docModel->clearAssociation(hexaType);
2840 }
2841
2842 void HEXABLOCKGUI::showAssociations()
2843 {
2844     VtkDocumentGraphicView* currentDGView = getCurrentVtkGraphicView();
2845     if (currentDGView == NULL) return;
2846
2847     QModelIndexList elts;
2848     int currentChildIndex = 0;
2849     QVariant currentAssocVariant;
2850     QString currentAssocEntry;
2851     QModelIndex currentIndex = _patternDataTreeView->currentIndex();
2852     QModelIndex currentChild = currentIndex.child(currentChildIndex++, 0);
2853
2854     while( currentChild.isValid() ) {
2855
2856         currentAssocVariant = currentChild.data( HEXA_ASSOC_ENTRY_ROLE );
2857         currentAssocEntry = currentChild.data( HEXA_ASSOC_ENTRY_ROLE ).toString();
2858         if ( currentAssocVariant.isValid() && !currentAssocEntry.isEmpty() )
2859             elts << currentChild;
2860
2861         currentChild = currentChild.sibling(currentChildIndex++, 0);
2862     }
2863     currentDocGView->highlight(elts);
2864     currentOccGView->highlight(elts, false);
2865 }
2866
2867
2868 LightApp_SelectionMgr* HEXABLOCKGUI::selectionMgr()
2869 {
2870     SalomeApp_Application* anApp = dynamic_cast<SalomeApp_Application*>( SUIT_Session::session()->activeApplication() );
2871     if( anApp )
2872         return dynamic_cast<LightApp_SelectionMgr*>( anApp->selectionMgr() );
2873     else
2874         return NULL;
2875 }
2876
2877 bool HEXABLOCKGUI::eventFilter(QObject *obj, QEvent *event)
2878 {
2879     if ( event->type() == QEvent::Enter )
2880     {   // *** ENTER EVENT
2881
2882         if (currentDialog == NULL || !currentDialog->isVisible())
2883             return QObject::eventFilter(obj, event);
2884
2885         //  ** The window acquire the focus when the cursor enter
2886
2887             //OCC - window enter
2888         OCCViewer_ViewWindow* occWindow = dynamic_cast<OCCViewer_ViewWindow*>(obj);
2889         if ( occWindow != NULL)
2890             currentDialog->onWindowActivated(occWindow->getViewManager());
2891         else
2892             //VTK - window enter
2893         {
2894             SVTK_ViewWindow* vtkWindow = dynamic_cast<SVTK_ViewWindow*>(obj);
2895             if ( vtkWindow != NULL)
2896                 currentDialog->onWindowActivated(vtkWindow->getViewManager());
2897         }
2898     }
2899
2900     //standard event processing
2901     return QObject::eventFilter(obj, event);
2902 }
2903
2904 QStringList HEXABLOCKGUI::getQuickDirList()
2905 {
2906     QStringList dirList;
2907     SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
2908     if ( resMgr )
2909         dirList = resMgr->stringValue( "FileDlg", "QuickDirList" ).split( ';', QString::SkipEmptyParts );
2910
2911     return dirList;
2912 }
2913
2914
2915 // --- Export the module
2916
2917 extern "C"
2918 {
2919     HexaExport CAM_Module* createModule()
2920     {
2921         return new HEXABLOCKGUI();
2922     }
2923
2924     HexaExport char* getModuleVersion()
2925     {
2926         return (char*)HEXABLOCK_VERSION_STR;
2927     }
2928 }
2929
2930
2931
2932 // ******************** TESTS ***************************************************
2933
2934 void HEXABLOCKGUI::testDocument()
2935 {
2936     //CS_TEST
2937     // ----------
2938     //   QStandardItem *parentItem = getCurrentModel()->invisibleRootItem();
2939     //   QStandardItem *myItem =  new QStandardItem("MyItem");
2940     //   parentItem->appendRow(myItem);
2941     // //   ----------
2942     //   QModelIndex v0 = getCurrentModel()->addVertex(0., 0., 0.);
2943     //   QModelIndex v1 = getCurrentModel()->addVertex(5., 0., 0.);
2944     //   QModelIndex v2 = getCurrentModel()->addVertex(5., 5., 0.);
2945     //   QModelIndex v3 = getCurrentModel()->addVertex(0., 5., 0.);
2946     //   QModelIndex v4 = getCurrentModel()->addVertex(0., 0., 5.);
2947     //   QModelIndex v5 = getCurrentModel()->addVertex(5., 0., 5.);
2948     //   QModelIndex v6 = getCurrentModel()->addVertex(5., 5., 5.);
2949     //   QModelIndex v7 = getCurrentModel()->addVertex(0., 5., 5.);
2950     // //
2951     // //
2952     //   QModelIndex q0 = getCurrentModel()->addQuadVertices( v0, v1, v2, v3 );
2953     //   QModelIndex q1 = getCurrentModel()->addQuadVertices( v4, v5, v6, v7 );
2954     //   QModelIndex q2 = getCurrentModel()->addQuadVertices( v0, v3, v7, v4 );
2955     //   QModelIndex q3 = getCurrentModel()->addQuadVertices( v1, v2, v6, v5 );
2956     //   QModelIndex q4 = getCurrentModel()->addQuadVertices( v0, v1, v5, v4 );
2957     //   QModelIndex q5 = getCurrentModel()->addQuadVertices( v3, v2, v6, v7 );
2958     //
2959     //   QModelIndex h0 = getCurrentModel()->addHexaQuad( q0, q1, q2, q3, q4, q5 );
2960     //   QModelIndex vx = getCurrentModel()->addVector(1., 0., 0.);
2961     //   QModelIndex vy = getCurrentModel()->addVector(0., 1., 0.);
2962     //   QModelIndex vz = getCurrentModel()->addVector(0., 0., 1.);
2963     //
2964
2965
2966
2967     // //   QModelIndex orig1 = getCurrentModel()->addVertex (0, 0,0);
2968     // //   QModelIndex orig2 = getCurrentModel()->addVertex (50,0,0);
2969     // //   QModelIndex vz    = getCurrentModel()->addVector (0,0,1);
2970     // //   QModelIndex vx    = getCurrentModel()->addVector (1,0,0);
2971     // //
2972     // //   int nr  = 4;
2973     // //   int nri = 3;
2974     // //   int nre = nr;
2975     // //   int na = 9;
2976     // //   int nl = 5;
2977     // //
2978     // //   QModelIndex  cyl  = getCurrentModel()->addCylinder   (orig1, vz, nr, nl);
2979     // //   QModelIndex  pipe = getCurrentModel()->addPipe       (orig2, vz, nri, nre, nl);
2980     // //
2981     // //   getCurrentModel()->makeCylinder (cyl,  vx, nr, na, nl);
2982     // //   getCurrentModel()->makePipe(pipe, vx, nr, na, nl);
2983
2984
2985
2986     //   newMesh( "toto", 3, "FactoryServer");
2987     // newMesh
2988 }
2989
2990 void HEXABLOCKGUI::test_make_cart_grid()
2991 {
2992     QModelIndex orig1 = getCurrentModel()->addVertex( 0, 0, 0);
2993     QModelIndex orig2 = getCurrentModel()->addVertex( 10, 0, 0);
2994     QModelIndex orig3 = getCurrentModel()->addVertex( 0, 10, 0);
2995     QModelIndex orig4 = getCurrentModel()->addVertex( 10, 10, 0);
2996     QModelIndex orig5 = getCurrentModel()->addVertex( 0, 20, 0);
2997     QModelIndex orig6 = getCurrentModel()->addVertex( 10, 20, 0);
2998
2999     QModelIndex vz = getCurrentModel()->addVector(0, 0, 1);
3000     QModelIndex vx = getCurrentModel()->addVector(1, 0, 0);
3001
3002     //int dr = 1;
3003     //int dl = 1;
3004     //int nr = 2;
3005     //int nl = 3;
3006
3007     // QModelIndex c1 = getCurrentModel()->makeCylindrical(orig1, vx, vz, dr, 360, dl, nr, 4, nl, true);
3008     // QModelIndex c2 = getCurrentModel()->makeCylindrical(orig2, vx, vz, dr, 360, dl, nr, 8, nl, true);
3009     // QModelIndex c3 = getCurrentModel()->makeCylindrical(orig3, vx, vz, dr, 270, dl, nr, 8, nl, true);
3010     // QModelIndex c4 = getCurrentModel()->makeCylindrical(orig4, vx, vz, dr, 270, dl, nr, 7, nl, true);
3011     // QModelIndex c5 = getCurrentModel()->makeCylindrical(orig5, vx, vz, dr, 360, dl, nr, 5, nl, true);
3012     // QModelIndex c6 = getCurrentModel()->makeCylindrical(orig6, vx, vz, dr, 360, dl, nr, 6, nl, true);
3013
3014 }
3015
3016
3017 void HEXABLOCKGUI::test_make_elmts_transform()
3018 {
3019
3020 //    int size_x = 1;
3021 //    int size_y = 1;
3022 //    int size_z = 2;
3023 //
3024 //    QModelIndex orig = getCurrentModel()->addVertex(0, 0, 0);
3025 //    QModelIndex dirVr = getCurrentModel()->addVector(1, 1, 1);
3026
3027     //obsolete
3028 //    QModelIndex grid = getCurrentModel()->makeCartesian(orig, dirVr, size_x, size_y, size_z);//, 0, 0, 0);
3029     // orig.setScalar(2);
3030
3031     // file_name = os.path.join(os.environ['TMP'], 'transfo0.vtk')
3032     // getCurrentModel()->saveVtk(file_name)
3033
3034 //    QModelIndex devant = getCurrentModel()->addVector(5, 0, 0);
3035 //    QModelIndex grid2  = getCurrentModel()->makeTranslation(grid, devant);
3036
3037     // file_name = os.path.join(os.environ['TMP'], 'transfo_translation.vtk')
3038     // getCurrentModel()->saveVtk(file_name)
3039
3040 //    QModelIndex grid4 = getCurrentModel()->makeRotation(grid2, orig, dirVr, 45);
3041
3042
3043     // file_name = os.path.join(os.environ['TMP'], 'transfo_rotation.vtk');
3044     // getCurrentModel()->saveVtk(file_name);
3045
3046
3047     // print "...test make elements by transforming elements OK"
3048 }
3049 // =============================================================== addDocInSudy
3050 QString HEXABLOCKGUI::addDocInStudy (HEXA_NS::Document* document)
3051 {
3052     QString docEntry;
3053
3054     if (document == NULL)
3055         return docEntry;
3056
3057     QString docName  = document->getName ();
3058
3059     SalomeApp_Study* app_study = HEXABLOCKGUI::activeStudy();
3060     if (app_study == NULL)
3061         return docEntry;
3062
3063     _PTR(Study)         study    = app_study->studyDS();
3064     SALOMEDS::Study_var ds_study = ClientStudyToStudy (study);
3065     SALOMEDS::StudyBuilder_var aBuilder (ds_study->NewBuilder());
3066     QString entry = app_study->centry("HEXABLOCK");
3067     SALOMEDS::SObject_var aFatherSO = ds_study->FindObjectID( qPrintable(entry) );
3068     if (aFatherSO->_is_nil())
3069         return docEntry;
3070
3071     SALOMEDS::SObject_var aSO = aBuilder->NewObject(aFatherSO);
3072     if (aSO->_is_nil())
3073         return docEntry;
3074     aSO->SetAttrString("AttributeName", docName.toStdString().c_str());
3075     docEntry = aSO->GetID(); //the entry of the published object
3076
3077     return docEntry;
3078 }
3079
3080 //--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8
3081
3082 void HEXABLOCKGUI::test()
3083 {
3084     DEBTRACE("HEXABLOCKGUI::test");
3085     VtkDocumentGraphicView* currentDGView = getCurrentVtkGraphicView();
3086     if (currentDGView == NULL) return;
3087     currentDGView->update();
3088
3089 }
3090
3091 void HEXABLOCKGUI::test_association()
3092 {
3093     DEBTRACE("HEXABLOCKGUI::test_association");
3094     newDocument();
3095
3096     QModelIndex v0, v1, v2, v3, e0, e1, q0;
3097
3098     DocumentModel::GeomObj v0Assoc, v1Assoc, v2Assoc, v3Assoc;
3099     DocumentModel::GeomObj e0AssocA, e1AssocA, e1AssocB, e1AssocC;
3100     DocumentModel::GeomObj q0AssocA, q0AssocB;
3101
3102     DocumentModel::GeomObj v0Assoc_test, v1Assoc_test, v2Assoc_test, v3Assoc_test;
3103     QList<DocumentModel::GeomObj> e1Assocs_test;
3104     DocumentModel::GeomObj q0Assoc_test;
3105
3106     v0 = getCurrentModel()->addVertex(0, 0, 0);
3107     v1 = getCurrentModel()->addVertex(1, 0, 0);
3108     v2 = getCurrentModel()->addVertex(1, 1, 0);
3109     v3 = getCurrentModel()->addVertex(0, 1, 0);
3110     e0  = getCurrentModel()->addEdgeVertices(v0, v1);
3111     e1  = getCurrentModel()->addEdgeVertices(v1, v2);
3112     q0 = getCurrentModel()->addQuadVertices( v0, v1, v2, v3 );
3113
3114
3115     // >>> face3.GetEntry()'0:1:4'
3116     // >>> e1.GetEntry()'0:1:8'
3117     // >>> e2.GetEntry()'0:1:9'
3118     // >>> e3.GetEntry()'0:1:10'
3119     // >>> e4.GetEntry()'0:1:11'
3120     // >>> v1.GetEntry()'0:1:12'
3121     // >>> v2.GetEntry()'0:1:13'
3122     // >>> v3.GetEntry()'0:1:14'
3123     // >>> v4.GetEntry()'0:1:15'
3124
3125
3126     //   v0Assoc.name  = "geomV0";
3127     //   v0Assoc.entry = "0:1:1:1:3:6";//"0:1:12";
3128     //   v0Assoc.brep  = "brepV0";
3129     //   v1Assoc.name  = "geomV1";
3130     //   v1Assoc.entry = "0:1:1:1:3:7";//"0:1:13";
3131     //   v1Assoc.brep  = "brepV1";
3132     //   v2Assoc.name  = "geomV2";
3133     //   v2Assoc.entry = "0:1:1:1:3:8";//"0:1:14";
3134     //   v2Assoc.brep  = "brepV2";
3135     //   v3Assoc.name  = "geomV3";
3136     //   v3Assoc.entry = "0:1:1:1:3:9";//"0:1:15";
3137     //   v3Assoc.brep  = "brepV3";
3138     //
3139     //
3140     //
3141     //   e0AssocA.name   = "geomE0a";
3142     //   e0AssocA.entry  = "0:1:1:1:3:5";//"0:1:8";
3143     //   e0AssocA.brep  = "brepE0a";
3144     //   e0AssocA.start  = 0.10;
3145     //   e0AssocA.end    = 0.95;
3146     //
3147     //
3148     //
3149     //   e1AssocA.name   = "geomE1a";
3150     //   e1AssocA.entry  = "0:1:1:1:3:2";//"0:1:8";
3151     //   e1AssocA.brep  = "brepE1a";
3152     //   e1AssocA.start  = 0.12;
3153     //   e1AssocA.end    = 0.89;
3154     //   e1AssocB.name   = "geomE1b";
3155     //   e1AssocB.entry  = "0:1:1:1:3:3";//"0:1:9";
3156     //   e1AssocB.brep  = "brepE1b";
3157     //   e1AssocB.start  = 0.20;
3158     //   e1AssocB.end    = 0.80;
3159     //   e1AssocC.name   = "geomE1c";
3160     //   e1AssocC.entry  = "0:1:1:1:3:4";//"0:1:10";
3161     //   e1AssocC.brep  = "brepE1c";
3162     //   e1AssocC.start  = 0.16;
3163     //   e1AssocC.end    = 0.96;
3164     //
3165     //   q0AssocA.name   = "geomQuad";
3166     //   q0AssocA.entry  = "0:1:1:1:3";
3167     //   q0AssocA.brep   = "brepq0";
3168     //
3169     //   q0AssocB.name   = "geomQuad";
3170     //   q0AssocB.entry  = "0:1:1:1:4";
3171     //   q0AssocB.brep   = "brepq0";
3172     //
3173     //   getCurrentModel()->addAssociation( v0, v0Assoc );
3174     //   getCurrentModel()->addAssociation( v1, v1Assoc );
3175     //   getCurrentModel()->addAssociation( v2, v2Assoc );
3176     //   getCurrentModel()->addAssociation( v3, v3Assoc );
3177     //
3178     //
3179     //   getCurrentModel()->addAssociation( e0, e0AssocA );
3180     //
3181     //   getCurrentModel()->addAssociation( e1, e1AssocA );
3182     //   getCurrentModel()->addAssociation( e1, e1AssocB );
3183     //   getCurrentModel()->addAssociation( e1, e1AssocC );
3184     //
3185     //   getCurrentModel()->addAssociation( q0, q0AssocA );
3186     //   getCurrentModel()->addAssociation( q0, q0AssocB );
3187
3188
3189
3190
3191
3192     //   v0Assoc_test = getCurrentModel()->getAssociations( v0 )[0];
3193     //   v1Assoc_test = getCurrentModel()->getAssociations( v1 )[0];
3194     //   v2Assoc_test = getCurrentModel()->getAssociations( v2 )[0];
3195     //   v3Assoc_test = getCurrentModel()->getAssociations( v3 )[0];
3196     //   ASSERT( v0Assoc.name == v0Assoc_test.name );
3197     //   ASSERT( v1Assoc.name == v1Assoc_test.name );
3198     //   ASSERT( v2Assoc.name == v2Assoc_test.name );
3199     //   ASSERT( v3Assoc.name == v3Assoc_test.name );
3200     //   ASSERT( v0Assoc.entry == v0Assoc_test.entry );
3201     //   ASSERT( v1Assoc.entry == v1Assoc_test.entry );
3202     //   ASSERT( v2Assoc.entry == v2Assoc_test.entry );
3203     //   ASSERT( v3Assoc.entry == v3Assoc_test.entry );
3204     //
3205     //
3206     //   e1Assocs_test = getCurrentModel()->getAssociations( e1 );
3207     //   ASSERT( e1Assocs_test[0].name == e1AssocA.name );
3208     //   ASSERT( e1Assocs_test[1].name == e1AssocB.name );
3209     //   ASSERT( e1Assocs_test[2].name == e1AssocC.name );
3210     //   ASSERT( e1Assocs_test[0].entry == e1AssocA.entry );
3211     //   ASSERT( e1Assocs_test[1].entry == e1AssocB.entry );
3212     //   ASSERT( e1Assocs_test[2].entry == e1AssocC.entry );
3213     //   ASSERT( e1Assocs_test[0].start == e1AssocA.start );
3214     //   ASSERT( e1Assocs_test[1].start == e1AssocB.start );
3215     //   ASSERT( e1Assocs_test[2].start == e1AssocC.start );
3216     //   ASSERT( e1Assocs_test[0].end == e1AssocA.end );
3217     //   ASSERT( e1Assocs_test[1].end == e1AssocB.end );
3218     //   ASSERT( e1Assocs_test[2].end == e1AssocC.end );
3219     //
3220     //   q0Assoc_test = getCurrentModel()->getAssociations( q0 )[0];
3221     //   ASSERT( q0Assoc_test.name  == q0Assoc.name );
3222     //   ASSERT( q0Assoc_test.entry == q0Assoc.entry );
3223
3224
3225     DEBTRACE("HEXABLOCKGUI::test_association fin");
3226 }