Salome HOME
Merge branch V7_3_1_BR
[modules/hexablock.git] / src / HEXABLOCKGUI / HEXABLOCKGUI_VtkDocumentGraphicView.cxx
1 // Copyright (C) 2009-2014  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, or (at your option) any later version.
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 // VTK includes
23 #include <vtkRenderer.h>
24 #include <vtkUnstructuredGrid.h>
25 #include <vtkDataSetMapper.h>
26 #include "vtkCellArray.h"
27 #include "vtkPolyData.h"
28
29 #include <VTKViewer_CellLocationsArray.h>
30 #include <SVTK_ViewModel.h>
31
32 #include "HEXABLOCKGUI_Trace.hxx"
33 #include "HEXABLOCKGUI_VtkDocumentGraphicView.hxx"
34 #include "HEXABLOCKGUI.hxx"
35
36 #ifndef M_PI
37 #define M_PI 3.1415927
38 #endif
39
40 using namespace std;
41 using namespace HEXABLOCK::GUI;
42 using namespace HEXA_NS;
43
44 Document_Actor::Document_Actor( Document* doc, const QString& entry ):
45           SALOME_Actor(),
46           _doc( doc )
47 {
48     DEBTRACE("Document_Actor::Document_Actor " << entry.toLatin1() );
49     const char* docName = ((doc != NULL) ? doc->getName() : "");
50     Handle(SALOME_InteractiveObject) anIO = new SALOME_InteractiveObject( entry.toLatin1(), "HEXABLOCK", docName );//,theName); CS_TODO
51     setIO(anIO);
52     vtkUnstructuredGrid* aGrid = getUnstructuredGrid();
53     vtkDataSetMapper* aMapper = vtkDataSetMapper::New();
54     aMapper->SetInputData(aGrid); // saclay
55 //    aMapper->SetInput(aGrid);
56     aGrid->Delete();
57
58     SetVisibility( true );//VisibilityOff();
59     SetPickable( true ); //PickableOff();//
60     SetMapper( aMapper );
61     aMapper->Delete();
62
63     vtkProperty* aProp = vtkProperty::New();
64
65 //    aProp->SetRepresentationToSurface();
66     aProp->SetRepresentationToWireframe();
67 //    aProp->SetRepresentationToPoints();
68     aProp->EdgeVisibilityOn ();
69     aProp->SetPointSize(5);
70     SetProperty( aProp );
71     aProp->Delete();
72 //    SetPointRepresentation(true);
73 }
74
75 Document_Actor::~Document_Actor()
76 {
77
78 }
79
80
81 vtkUnstructuredGrid* Document_Actor::getUnstructuredGrid()
82 {
83     vtkUnstructuredGrid* theGrid = vtkUnstructuredGrid::New();
84
85     _doc->reorderFaces(); //CS_TEST
86
87     map <int,vtkIdType>   vtkNodeId;
88     map <vtkIdType, int>  hexaNodeId;
89
90     // Create points
91     vtkPoints* aPoints = vtkPoints::New();
92     int nbVertex = _doc->countVertex();
93     aPoints->SetNumberOfPoints( nbVertex );
94
95     Vertex* v = NULL;
96     int vertexId;
97     for ( int i=0; i <nbVertex; ++i ){
98         v = _doc->getVertex(i);
99         aPoints->SetPoint( i, v->getX(), v->getY(), v->getZ() );
100         vertexId = reinterpret_cast<intptr_t>(v); //v->getId();
101         vtkNodeId [ vertexId ] = i;
102         hexaNodeId[ i ] = vertexId ;
103         //     vtkNodeId [ vertexId ] = i+1;
104         //     hexaNodeId[ i+1 ] = vertexId ;
105     }
106
107     theGrid->SetPoints( aPoints );
108     aPoints->Delete();
109     //   theGrid->SetCells( 0, 0, 0, 0, 0 );
110
111
112     // Calculate cells size
113     int nb0DElement = _doc->countVertex();
114     int nbEdge      = _doc->countEdge();
115     int nbFace      = _doc->countQuad();
116     int nbVolume    = _doc->countHexa();
117
118     vtkIdType aCellsSize =  2*nb0DElement + 3*nbEdge + ( 4 + 1 )*nbFace + ( 8 + 1 )*nbVolume;
119     vtkIdType aNbCells   =  nb0DElement + nbEdge + nbFace + nbVolume;
120
121     // Create cells
122     vtkCellArray* aConnectivity = vtkCellArray::New();
123     aConnectivity->Allocate( aCellsSize, 0 );
124
125     vtkUnsignedCharArray* aCellTypesArray = vtkUnsignedCharArray::New();
126     aCellTypesArray->SetNumberOfComponents( 1 );
127     aCellTypesArray->Allocate( aNbCells * aCellTypesArray->GetNumberOfComponents() );
128
129     vtkIdList *anIdList = vtkIdList::New();
130     vtkIdType iVtkElem = 0;
131     //   vtkIdType iVtkElem = 1; //CS_TEST
132     int       iHexaElem;
133
134     // VERTEX
135     for ( int i=0; i<nb0DElement; ++i ){
136         anIdList->SetNumberOfIds( 1 );
137         v = _doc->getVertex(i);
138         iHexaElem = reinterpret_cast<intptr_t>(v);//v->getId();
139         vtkElemsId[iHexaElem] = iVtkElem;
140         hexaElemsId[iVtkElem] = iHexaElem;
141         anIdList->SetId(0, vtkNodeId[iHexaElem]);
142         aConnectivity->InsertNextCell( anIdList );
143         aCellTypesArray->InsertNextValue( VTK_VERTEX );//getCellType( aType, anElem->IsPoly(), aNbNodes ) );
144         ++iVtkElem;
145     }
146
147     // EDGE
148     Edge* e = NULL;
149     Vertex* vertexElem = NULL;
150     for ( int i=0; i<nbEdge; ++i ){
151         anIdList->SetNumberOfIds( 2 );
152         e = _doc->getEdge(i);
153         iHexaElem = reinterpret_cast<intptr_t>(e); //e->getId();
154         vtkElemsId[iHexaElem] = iVtkElem;
155         hexaElemsId[iVtkElem] = iHexaElem;
156
157         for( vtkIdType j = 0; j< 2; ++j ){ //j< e->countVertex(); ++j ){
158             vertexElem = e->getVertex( j );
159             anIdList->SetId( j, vtkNodeId[ reinterpret_cast<intptr_t>(vertexElem) ] );//vertexElem->getId() ]);
160         }
161         aConnectivity->InsertNextCell( anIdList );
162         aCellTypesArray->InsertNextValue( VTK_LINE );//getCellType( aType, anElem->IsPoly(), aNbNodes ) );
163         ++iVtkElem;
164     }
165
166     // QUAD
167     Quad* q = NULL;
168     for ( int i=0; i<nbFace; ++i ){
169         anIdList->SetNumberOfIds( 4 );
170         q = _doc->getQuad(i);
171         iHexaElem = reinterpret_cast<intptr_t>(q); //q->getId();
172         vtkElemsId[iHexaElem] = iVtkElem;
173         hexaElemsId[iVtkElem] = iHexaElem;
174
175         for( vtkIdType j = 0; j< 4; ++j ){
176             vertexElem = q->getVertex( j );
177             anIdList->SetId( j, vtkNodeId[ reinterpret_cast<intptr_t>(vertexElem) ] );//vertexElem->getId() ]);
178         }
179         aConnectivity->InsertNextCell( anIdList );
180         aCellTypesArray->InsertNextValue( VTK_QUAD );//getCellType( aType, anElem->IsPoly(), aNbNodes ) );
181         ++iVtkElem;
182     }
183
184     // HEXA
185     Hexa* h = NULL;
186     std::map<int, int> connectivity;
187     connectivity[0] = 0;
188     connectivity[1] = 1;
189     connectivity[2] = 3;
190     connectivity[3] = 2;
191     connectivity[4] = 4;
192     connectivity[5] = 5;
193     connectivity[6] = 7;
194     connectivity[7] = 6;
195     for ( int i=0; i<nbVolume; ++i ){
196         anIdList->SetNumberOfIds( 8 );
197         h = _doc->getHexa(i);
198         iHexaElem = reinterpret_cast<intptr_t>(h); //q->getId();
199         vtkElemsId[iHexaElem] = iVtkElem;
200         hexaElemsId[iVtkElem] = iHexaElem;
201
202         for( vtkIdType j = 0; j< 8; ++j ){
203             vertexElem = h->getVertex( j );// );
204             anIdList->SetId( connectivity[j], vtkNodeId[ reinterpret_cast<intptr_t>(vertexElem) ]);//vertexElem->getId() ]);
205         }
206         aConnectivity->InsertNextCell( anIdList );
207         aCellTypesArray->InsertNextValue( VTK_HEXAHEDRON );
208         ++iVtkElem;
209     }
210
211
212     // 0        1      2     3        4     5      6      7
213     // V_ACE, V_ACF, V_ADE, V_ADF, V_BCE, V_BCF, V_BDE, V_BDF,
214     //
215     // 0        1     3      2        4    5      7      6
216
217     // Insert cells in grid
218     VTKViewer_CellLocationsArray* aCellLocationsArray = VTKViewer_CellLocationsArray::New();
219     aCellLocationsArray->SetNumberOfComponents( 1 );
220     aCellLocationsArray->SetNumberOfTuples( aNbCells );
221     //   std::cout << "aNbCells =>" << aNbCells << std::endl;
222
223     aConnectivity->InitTraversal();
224     for( vtkIdType idType = 0, *pts, npts; aConnectivity->GetNextCell( npts, pts ); idType++ ){
225         aCellLocationsArray->SetValue( idType, aConnectivity->GetTraversalLocation( npts ) );
226     }
227     theGrid->SetCells( aCellTypesArray, aCellLocationsArray,aConnectivity );
228
229     aCellLocationsArray->Delete();
230     aCellTypesArray->Delete();
231     aConnectivity->Delete();
232     anIdList->Delete();
233     //std::cout << "theGrid->GetNumberOfCells()" << theGrid->GetNumberOfCells() << std::endl;
234
235     return theGrid;
236 }
237
238 // =============================================================== Abu : debut
239 // ===================================================== Constructeur
240 Associate_Actor::Associate_Actor( Document* doc, const QString& entry)
241 : SALOME_Actor(), _doc( doc )
242 {
243     DEBTRACE("Associate_Actor::Associate_Actor " << entry.toLatin1() );
244     const char* docName = ((doc != NULL) ? doc->getName() : "");
245     Handle(SALOME_InteractiveObject) anIO = new SALOME_InteractiveObject( entry.toLatin1(), "HEXABLOCK", docName );//,theName); CS_TODO
246     setIO(anIO);
247     vtkUnstructuredGrid* aGrid = getUnstructuredGrid();
248
249     vtkDataSetMapper* aMapper = vtkDataSetMapper::New();
250     aMapper->SetInputData(aGrid); // saclay
251 //    aMapper->SetInput (aGrid);
252     aGrid->Delete();
253
254     SetVisibility( true );//VisibilityOff();
255     SetPickable( true ); //PickableOff();//
256     SetMapper( aMapper );
257     aMapper->Delete();
258
259     vtkProperty* aProp = vtkProperty::New();
260     aProp->SetColor(0,255,0);
261 //    aProp->SetRepresentationToSurface();
262     aProp->SetRepresentationToWireframe();
263 //    aProp->SetRepresentationToPoints();
264     aProp->EdgeVisibilityOn ();
265     aProp->SetPointSize(5);
266     SetProperty( aProp );
267     aProp->Delete();
268 //    SetPointRepresentation(true);
269 }
270 // ===================================================== getUnstructuredGrid
271
272 vtkUnstructuredGrid* Associate_Actor::getUnstructuredGrid()
273 {
274     vtkUnstructuredGrid* theGrid = vtkUnstructuredGrid::New();
275
276     _doc->reorderFaces(); //CS_TEST
277
278     std::map<int,vtkIdType>   vtkNodeId;
279     std::map<vtkIdType, int>  hexaNodeId;
280
281     // Create points
282     vtkPoints* aPoints = vtkPoints::New();
283     int nbVertex = _doc->countVertex();
284     aPoints->SetNumberOfPoints( nbVertex );
285
286     Vertex* v = NULL;
287     int vertexId;
288     for ( int i=0; i <nbVertex; ++i ){
289         v = _doc->getVertex(i);
290         aPoints->SetPoint( i, v->getX()+6, v->getY()+6, v->getZ() );
291         vertexId = reinterpret_cast<intptr_t>(v); //v->getId();
292         vtkNodeId [ vertexId ] = i;
293         hexaNodeId[ i ] = vertexId ;
294         //     vtkNodeId [ vertexId ] = i+1;
295         //     hexaNodeId[ i+1 ] = vertexId ;
296     }
297
298     theGrid->SetPoints( aPoints );
299     aPoints->Delete();
300     //   theGrid->SetCells( 0, 0, 0, 0, 0 );
301
302     // Calculate cells size
303     int nb0DElement = _doc->countVertex();
304     int nbEdge      = _doc->countEdge();
305     int nbFace      = _doc->countQuad();
306     int nbVolume    = _doc->countHexa();
307
308     vtkIdType aCellsSize =  2*nb0DElement + 3*nbEdge + ( 4 + 1 )*nbFace + ( 8 + 1 )*nbVolume;
309     vtkIdType aNbCells   =  nb0DElement + nbEdge + nbFace + nbVolume;
310
311     // Create cells
312     vtkCellArray* aConnectivity = vtkCellArray::New();
313     aConnectivity->Allocate( aCellsSize, 0 );
314
315     vtkUnsignedCharArray* aCellTypesArray = vtkUnsignedCharArray::New();
316     aCellTypesArray->SetNumberOfComponents( 1 );
317     aCellTypesArray->Allocate( aNbCells * aCellTypesArray->GetNumberOfComponents() );
318
319     vtkIdList *anIdList = vtkIdList::New();
320     vtkIdType iVtkElem = 0;
321     //   vtkIdType iVtkElem = 1; //CS_TEST
322     int       iHexaElem;
323
324     // VERTEX
325     for ( int i=0; i<nb0DElement; ++i ){
326         anIdList->SetNumberOfIds( 1 );
327         v = _doc->getVertex(i);
328         iHexaElem = reinterpret_cast<intptr_t>(v);//v->getId();
329         vtkElemsId[iHexaElem] = iVtkElem;
330         hexaElemsId[iVtkElem] = iHexaElem;
331         anIdList->SetId(0, vtkNodeId[iHexaElem]);
332         aConnectivity->InsertNextCell( anIdList );
333         aCellTypesArray->InsertNextValue( VTK_VERTEX );//getCellType( aType, anElem->IsPoly(), aNbNodes ) );
334         ++iVtkElem;
335     }
336
337     // EDGE
338     Edge* e = NULL;
339     Vertex* vertexElem = NULL;
340     for ( int i=0; i<nbEdge; ++i ){
341         anIdList->SetNumberOfIds( 2 );
342         e = _doc->getEdge(i);
343         iHexaElem = reinterpret_cast<intptr_t>(e); //e->getId();
344         vtkElemsId[iHexaElem] = iVtkElem;
345         hexaElemsId[iVtkElem] = iHexaElem;
346
347         for( vtkIdType j = 0; j< 2; ++j ){ //j< e->countVertex(); ++j ){
348             vertexElem = e->getVertex( j );
349             anIdList->SetId( j, vtkNodeId[ reinterpret_cast<intptr_t>(vertexElem) ] );//vertexElem->getId() ]);
350         }
351         aConnectivity->InsertNextCell( anIdList );
352         aCellTypesArray->InsertNextValue( VTK_LINE );//getCellType( aType, anElem->IsPoly(), aNbNodes ) );
353         ++iVtkElem;
354     }
355
356     // QUAD
357     Quad* q = NULL;
358     for ( int i=0; i<nbFace; ++i ){
359         anIdList->SetNumberOfIds( 4 );
360         q = _doc->getQuad(i);
361         iHexaElem = reinterpret_cast<intptr_t>(q); //q->getId();
362         vtkElemsId[iHexaElem] = iVtkElem;
363         hexaElemsId[iVtkElem] = iHexaElem;
364
365         for( vtkIdType j = 0; j< 4; ++j ){
366             vertexElem = q->getVertex( j );
367             anIdList->SetId( j, vtkNodeId[ reinterpret_cast<intptr_t>(vertexElem) ] );//vertexElem->getId() ]);
368         }
369         aConnectivity->InsertNextCell( anIdList );
370         aCellTypesArray->InsertNextValue( VTK_QUAD );//getCellType( aType, anElem->IsPoly(), aNbNodes ) );
371         ++iVtkElem;
372     }
373
374     // HEXA
375     Hexa* h = NULL;
376     std::map<int, int> connectivity;
377     connectivity[0] = 0;
378     connectivity[1] = 1;
379     connectivity[2] = 3;
380     connectivity[3] = 2;
381     connectivity[4] = 4;
382     connectivity[5] = 5;
383     connectivity[6] = 7;
384     connectivity[7] = 6;
385     for ( int i=0; i<nbVolume; ++i ){
386         anIdList->SetNumberOfIds( 8 );
387         h = _doc->getHexa(i);
388         iHexaElem = reinterpret_cast<intptr_t>(h); //q->getId();
389         vtkElemsId[iHexaElem] = iVtkElem;
390         hexaElemsId[iVtkElem] = iHexaElem;
391
392         for( vtkIdType j = 0; j< 8; ++j ){
393             vertexElem = h->getVertex( j );// );
394             anIdList->SetId( connectivity[j], vtkNodeId[ reinterpret_cast<intptr_t>(vertexElem) ]);//vertexElem->getId() ]);
395         }
396         aConnectivity->InsertNextCell( anIdList );
397         aCellTypesArray->InsertNextValue( VTK_HEXAHEDRON );
398         ++iVtkElem;
399     }
400
401
402     // 0        1      2     3        4     5      6      7
403     // V_ACE, V_ACF, V_ADE, V_ADF, V_BCE, V_BCF, V_BDE, V_BDF,
404     //
405     // 0        1     3      2        4    5      7      6
406
407     // Insert cells in grid
408     VTKViewer_CellLocationsArray* aCellLocationsArray = VTKViewer_CellLocationsArray::New();
409     aCellLocationsArray->SetNumberOfComponents( 1 );
410     aCellLocationsArray->SetNumberOfTuples( aNbCells );
411     //   std::cout << "aNbCells =>" << aNbCells << std::endl;
412
413     aConnectivity->InitTraversal();
414     for( vtkIdType idType = 0, *pts, npts; aConnectivity->GetNextCell( npts, pts ); idType++ ){
415         aCellLocationsArray->SetValue( idType, aConnectivity->GetTraversalLocation( npts ) );
416     }
417     theGrid->SetCells( aCellTypesArray, aCellLocationsArray,aConnectivity );
418
419     aCellLocationsArray->Delete();
420     aCellTypesArray->Delete();
421     aConnectivity->Delete();
422     anIdList->Delete();
423
424     return theGrid;
425 }
426
427
428 // -----------------------------------------------------------------------
429 //          VtkDocumentGraphicView implementation
430 // -----------------------------------------------------------------------
431 VtkDocumentGraphicView::VtkDocumentGraphicView( DocumentModel* documentModel,
432         SVTK_ViewWindow* view, QWidget* parent ):
433         QAbstractItemView(parent),
434         viewWindow( view ),
435         documentActor( NULL ),
436         associateActor (NULL), // Abu
437         _currentChanged( false ),
438         firstUpdate(true),
439         selectionMode(-1)
440 {
441
442     //Model
443     setModel(documentModel);
444     patternDataModel     = new PatternDataModel(parent);
445     patternGeomModel     = new PatternGeomModel(parent);
446     groupsModel          = new GroupsModel(parent);
447     meshModel            = new MeshModel(parent);
448
449     patternDataModel->setSourceModel(documentModel);
450     patternGeomModel->setSourceModel(documentModel);
451     groupsModel->setSourceModel(documentModel);
452     meshModel->setSourceModel(documentModel);
453
454     //Selection Model
455     patternDataSelectionModel    = new PatternDataSelectionModel(patternDataModel);
456     patternGeomSelectionModel    = new PatternGeomSelectionModel(patternGeomModel);
457     groupsSelectionModel         = new GroupsSelectionModel(groupsModel);
458     meshSelectionModel           = new MeshSelectionModel(meshModel );
459
460     setSelectionModel(patternDataSelectionModel);
461 }
462
463 //Loads the graphic view document
464 void VtkDocumentGraphicView::loadDocument()
465 {
466     DocumentModel* docModel = getDocumentModel();
467     if (docModel == NULL) return;
468
469     docModel->load();
470 }
471
472 //Loads the document from the file in this graphic view
473 void VtkDocumentGraphicView::loadDocument(const QString& file)
474 {
475     if ( file.isEmpty() ) return;
476     getDocumentModel()->load(file);
477 }
478
479 //Saves the graphic view's document in the file
480 void VtkDocumentGraphicView::saveDocument(const QString& file)
481 {
482     if (file.isEmpty()) return;
483     getDocumentModel()->save(file);
484 }
485
486
487 VtkDocumentGraphicView::~VtkDocumentGraphicView()
488 {
489
490     //Delete Model
491     delete patternDataModel;
492     delete groupsModel;
493     delete meshModel;
494
495     //Delete Selection Model/ Disconnect signals on delete (Qt)
496 //    delete patternDataSelectionModel;
497 //    delete groupsSelectionModel;
498 //    delete meshSelectionModel;
499 }
500
501 void VtkDocumentGraphicView::setWindowTitle(const QString& title)
502 {
503     viewWindow->setWindowTitle( QString("hexablock : ") + title );
504 }
505
506 void VtkDocumentGraphicView::onPatternDatachanged()
507 {
508     update();
509 }
510
511 void VtkDocumentGraphicView::removeActor()
512 {
513     if ( documentActor != NULL && viewWindow != NULL){
514           viewWindow->RemoveActor( documentActor );
515           documentActor->Delete();
516     }
517 }
518
519 void VtkDocumentGraphicView::update()
520 {
521     DocumentModel*   theModel = dynamic_cast<DocumentModel*>( model() );
522     if (theModel == NULL || viewWindow == NULL) return;
523
524     Document*  theDocumentImpl  = theModel->documentImpl();
525     QString    theDocumentEntry = theModel->documentEntry();
526
527     if ( documentActor ){
528         viewWindow->RemoveActor( documentActor );
529         documentActor->Delete();
530     }
531     documentActor  = new Document_Actor( theDocumentImpl, theDocumentEntry );
532     viewWindow->AddActor( documentActor );
533
534     viewWindow->getRenderer()->Render();
535     viewWindow->Repaint();
536     if (firstUpdate)
537     {
538         viewWindow->onFitAll();
539         firstUpdate = false;
540     }
541 }
542
543 void VtkDocumentGraphicView::setVertexSelection()
544 {
545   setSelectionMode(NodeSelection);
546 }
547
548 void VtkDocumentGraphicView::setEdgeSelection()
549 {
550   setSelectionMode(EdgeSelection);
551 }
552
553 void VtkDocumentGraphicView::setQuadSelection()
554 {
555   setSelectionMode(FaceSelection);
556 }
557
558 void VtkDocumentGraphicView::setHexaSelection()
559 {
560 //  SetSelectionMode(VolumeSelection);
561   setSelectionMode(FaceSelection); //temporary for hexa selection debug
562   selectionMode = HEXA_TREE;
563 //  aProp->FaceVisibilityOff (); //would debug hexa preselection, selection???
564 }
565
566 void VtkDocumentGraphicView::setAllSelection()
567 {
568   setSelectionMode(ActorSelection);
569   selectionMode = -1;
570 }
571
572 void  VtkDocumentGraphicView::setSelectionMode(Selection_Mode theMode)
573 {
574     if (viewWindow == NULL || selectionMode == theMode)
575         return;
576
577     HEXABLOCKGUI::selectionMgr()->clearSelected();
578     viewWindow->SetSelectionMode( theMode );
579     selectionMode = theMode;
580 }
581
582 void VtkDocumentGraphicView::setSelectionMode( const QModelIndex& eltIndex )
583 {
584   QVariant treeVariant = eltIndex.data( HEXA_TREE_ROLE );
585   if ( !treeVariant.isValid() ) return;
586   int eltType = treeVariant.toInt();
587
588   if ( selectionMode == eltType ) return;
589
590   switch ( eltType ){
591     case VERTEX_TREE :
592     case GEOMPOINT_TREE:
593     case GEOMPOINT_DIR_TREE:
594     case VERTEX_DIR_TREE : setVertexSelection(); break;
595     case EDGE_TREE :
596     case GEOMEDGE_TREE:
597     case GEOMEDGE_DIR_TREE:
598     case EDGE_DIR_TREE :   setEdgeSelection(); break;
599     case QUAD_TREE :
600     case GEOMFACE_TREE:
601     case GEOMFACE_DIR_TREE:
602     case QUAD_DIR_TREE :   setQuadSelection();   break;
603     case HEXA_TREE :
604     case GEOMSHAPE_TREE:
605     case GEOMSHAPE_DIR_TREE:
606     case HEXA_DIR_TREE :   setHexaSelection();  break;
607     case PROPAGATION_TREE :
608     case PROPAGATION_DIR_TREE :   setEdgeSelection(); break;
609     default: setAllSelection();
610   }
611 }
612
613 void VtkDocumentGraphicView::getSelected(SALOME_ListIO& selectedObjects)
614 {
615     HEXABLOCKGUI::selectionMgr()->selectedObjects( selectedObjects, SVTK_Viewer::Type());
616 }
617
618 void VtkDocumentGraphicView::clearSelection()
619 {
620     if (viewWindow != NULL)
621     {
622 //        viewWindow->unHighlightAll();
623         setSelectionMode(ActorSelection);
624     }
625 }
626
627 void VtkDocumentGraphicView::highlight( const QModelIndex& ielt )
628 {
629
630     QModelIndexList indexList;
631     indexList << ielt;
632     highlight(indexList);
633
634 }
635
636 void VtkDocumentGraphicView::highlight( const QModelIndexList& elts )
637 {
638
639     if (elts.size() == 0 || viewWindow == NULL || documentActor == NULL)
640         return;
641     SVTK_Selector* selector = viewWindow->GetSelector();
642     if ( selector == NULL )
643         return;
644     //   Set selection mode in VTK view
645 //    viewWindow->SetSelectionMode(VolumeSelection);
646
647     // --- elements highlight ---
648     TColStd_IndexedMapOfInteger aMap;
649     int vtkElemsId;
650     QString eltEntry;
651
652     foreach( const QModelIndex& iElt, elts ){
653         if (iElt.isValid())
654         {
655             eltEntry = iElt.data( HEXA_ENTRY_ROLE ).toString();
656             vtkElemsId = documentActor->vtkElemsId[ eltEntry.toInt() ];
657             if ( vtkElemsId > 0 )
658                 aMap.Add( vtkElemsId );
659         }
660     }
661     selector->AddOrRemoveIndex( documentActor->getIO(), aMap, false );
662     viewWindow->highlight( documentActor->getIO(), true, true );
663 }
664
665
666 void VtkDocumentGraphicView::highlightGroups( const QModelIndex& eltIndex )
667 {
668     if ( groupsModel == NULL || viewWindow == NULL || documentActor == NULL) return;
669     SVTK_Selector* selector = viewWindow->GetSelector();
670     if ( selector == NULL ) return;
671
672     QVariant treeVariant        = eltIndex.data( HEXA_TREE_ROLE );
673     if ( !treeVariant.isValid())
674         return;
675
676     int eltType  = treeVariant.toInt();
677     if ( eltType != GROUP_TREE )
678         return;
679
680     //Get elements to highlight
681     DocumentModel::Group kind;
682     QModelIndexList iElements = groupsModel->getGroupElements( eltIndex, kind );
683     if (iElements.count() == 0) return;
684
685     // ---------- elements highlight --------------
686
687     // Set selection mode in VTK view
688     switch (kind){
689      case HexaCell: case HexaNode: viewWindow->SetSelectionMode(VolumeSelection); break;
690      case QuadCell: case QuadNode: viewWindow->SetSelectionMode(FaceSelection);   break;
691      case EdgeCell: case EdgeNode: viewWindow->SetSelectionMode(EdgeSelection);   break;
692      case VertexNode: viewWindow->SetSelectionMode(NodeSelection); break;
693     }
694
695     TColStd_MapOfInteger aMap;
696     int vtkElemsId;
697     QString eltEntry;
698
699     foreach( const QModelIndex& iElt, iElements ){
700         if (iElt.isValid())
701         {
702             eltEntry = iElt.data( HEXA_ENTRY_ROLE ).toString();
703             vtkElemsId = documentActor->vtkElemsId[ eltEntry.toInt() ];
704             if ( vtkElemsId > 0 ) aMap.Add( vtkElemsId );
705         }
706     }
707
708     selector->AddOrRemoveIndex( documentActor->getIO(), aMap, false );
709     viewWindow->highlight( documentActor->getIO(), true, true );
710     documentActor->highlight( false );
711
712 }
713
714
715 void VtkDocumentGraphicView::highlightPropagation( const QModelIndex& eltIndex )
716 {
717     if ( meshModel == NULL || viewWindow == NULL || documentActor == NULL) return;
718     SVTK_Selector* selector = viewWindow->GetSelector();
719     if ( selector == NULL ) return;
720
721     QVariant treeVariant        = eltIndex.data( HEXA_TREE_ROLE );
722     if ( !treeVariant.isValid())
723         return;
724
725     int eltType  = treeVariant.toInt();
726     if ( eltType != PROPAGATION_TREE )
727         return;
728
729     // Get elements to highlight
730     QModelIndexList iEdges = meshModel->getPropagation( eltIndex );
731     if (iEdges.count() == 0) return;
732
733     // ---------- elements highlight --------------
734
735     // Set selection mode in VTK view
736     viewWindow->SetSelectionMode(EdgeSelection);
737
738     TColStd_MapOfInteger aMap;
739     int vtkElemsId;
740     QString edgeEntry;
741
742     foreach( const QModelIndex& iEdge, iEdges ){
743         if (iEdge.isValid())
744         {
745             edgeEntry = iEdge.data( HEXA_ENTRY_ROLE ).toString();
746             vtkElemsId = documentActor->vtkElemsId[ edgeEntry.toInt() ];
747             if ( vtkElemsId > 0 ) aMap.Add( vtkElemsId );
748         }
749     }
750
751     selector->AddOrRemoveIndex( documentActor->getIO(), aMap, false );
752     viewWindow->highlight( documentActor->getIO(), true, true );
753     documentActor->highlight( false );
754
755 }
756
757
758
759
760 /********************************************************************************
761  *                   ABSTRACT METHOD ( MUST BE IMPLEMENTED )
762  ********************************************************************************/
763 /*
764     Returns the item that covers the coordinate given in the view.
765  */
766 QModelIndex VtkDocumentGraphicView::indexAt(const QPoint &point) const
767 {
768     return QModelIndex();
769 }
770
771 void VtkDocumentGraphicView::scrollTo(const QModelIndex &index, ScrollHint)
772 {
773 }
774
775 /*
776     Returns the position of the item in viewport coordinates.
777  */
778 QRect VtkDocumentGraphicView::visualRect(const QModelIndex &index) const
779 {
780     return QRect();
781 }
782
783 // PROTECTED :
784 int VtkDocumentGraphicView::horizontalOffset() const
785 {
786     return horizontalScrollBar()->value();
787 }
788
789 bool VtkDocumentGraphicView::isIndexHidden(const QModelIndex &index) const
790 {
791     return false;
792 }
793
794 QModelIndex VtkDocumentGraphicView::moveCursor(QAbstractItemView::CursorAction cursorAction,
795         Qt::KeyboardModifiers /*modifiers*/)
796 {
797     QModelIndex current = currentIndex();
798     return current;
799 }
800
801 /*
802     Find the indices corresponding to the extent of the selection.
803  */
804 void VtkDocumentGraphicView::setSelection(const QRect &rect, QItemSelectionModel::SelectionFlags command)
805 {
806 }
807
808
809 int VtkDocumentGraphicView::verticalOffset() const
810 {
811     return verticalScrollBar()->value();
812 }
813
814 /*
815     Returns a region corresponding to the selection in viewport coordinates.
816  */
817 QRegion VtkDocumentGraphicView::visualRegionForSelection(const QItemSelection &selection) const
818 {
819     return QRegion();
820 }
821
822
823 /********************************************************************************
824  *                   PROTECTED SLOTS
825  ********************************************************************************/
826 void VtkDocumentGraphicView::closeEditor ( QWidget * editor, QAbstractItemDelegate::EndEditHint hint )
827 {
828 }
829
830 void VtkDocumentGraphicView::commitData ( QWidget * editor )
831 {
832 }
833
834 void VtkDocumentGraphicView::currentChanged( const QModelIndex & current, const QModelIndex & previous )
835 {
836     _currentChanged = true;
837 }
838
839 void VtkDocumentGraphicView::dataChanged ( const QModelIndex & topLeft, const QModelIndex & bottomRight )
840 {
841     update();
842     _currentChanged = false;
843 }
844
845 void VtkDocumentGraphicView::editorDestroyed ( QObject * editor )
846 {
847 }
848
849 void VtkDocumentGraphicView::rowsAboutToBeRemoved ( const QModelIndex & parent, int start, int end )
850 {
851 }
852
853 void VtkDocumentGraphicView::rowsInserted ( const QModelIndex & parent, int start, int end )
854 {
855 }
856
857 void VtkDocumentGraphicView::selectionChanged( const QItemSelection & selected, const QItemSelection & deselected )
858 {
859 }
860
861 void VtkDocumentGraphicView::updateGeometries ()
862 {
863 }
864
865
866 void VtkDocumentGraphicView::setModel ( QAbstractItemModel * model )
867 {
868     QAbstractItemView::setModel( model );
869
870 //    PatternDataModel* pdm = dynamic_cast<PatternDataModel*>(model);
871 //    if (pdm)
872 //       connect( pdm, SIGNAL(patternDataChanged() ), this,  SLOT ( onPatternDatachanged() ) );
873
874     DocumentModel* dm = dynamic_cast<DocumentModel*>(model);
875     if (dm){
876 //        setWindowTitle( dm->getName() );
877         connect( dm, SIGNAL(patternDataChanged() ), this,  SLOT ( onPatternDatachanged() ) );
878 //        connect( dm, SIGNAL( nameChanged(const QString&) ), this,  SLOT ( setWindowTitle(const QString&) ) );
879     }
880 }