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