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