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