Salome HOME
Hexa6 : Mise ajour des sources
[modules/hexablock.git] / src / HEXABLOCKGUI / HEXABLOCKGUI_VtkDocumentGraphicView.cxx
1 // Copyright (C) 2009-2013  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 //#define _DEVDEBUG_
21
22 #include <sstream>
23
24 #include <iostream>
25
26 #include <math.h>
27
28 #include "utilities.h"
29
30 #include <QtGui>
31
32
33 #include <LightApp_Application.h>
34
35 #include <SUIT_ViewManager.h>
36 #include <SUIT_ViewWindow.h>
37 #include <SVTK_ViewManager.h>
38 #include <SVTK_ViewModel.h>
39 #include <SVTK_ViewWindow.h>
40 #include <SVTK_Prs.h>
41 #include <SALOME_Actor.h>
42 #include <VTKViewer_Algorithm.h>
43 #include <SalomeApp_Study.h>
44
45 // VTK includes
46 #include <vtkRenderer.h>
47 #include <vtkActorCollection.h>
48 #include <vtkUnstructuredGrid.h>
49 #include <vtkRenderWindowInteractor.h>
50 #include <vtkSmartPointer.h>
51 #include <SVTK_View.h>
52 // test tutorial (sphere)
53 #include <vtkPolyDataMapper.h>
54 #include <vtkSphereSource.h>
55
56 // test point (cf. SMESHGUI)
57 #include <vtkIdList.h>
58 #include <vtkCellArray.h>
59 #include <vtkUnsignedCharArray.h>
60 #include <vtkUnstructuredGrid.h>
61 #include <vtkDataSetMapper.h>
62 #include <vtkProperty.h>
63 #include <vtkLineSource.h>
64
65 #include <vtkLine.h>
66 #include <vtkQuad.h>
67 #include <vtkHexahedron.h>
68
69 #include "vtkLookupTable.h"
70 #include "vtkPoints.h"
71 #include "vtkCellArray.h"
72 #include "vtkFloatArray.h"
73 #include "vtkPolyData.h"
74 #include "vtkPolyDataMapper.h"
75 #include "vtkActor.h"
76 #include "vtkPointData.h"
77 #include "vtkProperty.h"
78
79
80 // #include "vtkStructuredGridReader.h"
81 #include "vtkUnstructuredGridReader.h"
82
83 #include <VTKViewer_CellLocationsArray.h>
84
85
86
87
88
89 #ifndef M_PI
90 #define M_PI 3.1415927
91 #endif
92
93 #include "HEXABLOCKGUI_Trace.hxx"
94 #include "HEXABLOCKGUI_DocumentModel.hxx"
95 #include "HEXABLOCKGUI_VtkDocumentGraphicView.hxx"
96 #include "HEXABLOCKGUI.hxx"
97
98
99
100 using namespace std;
101 using namespace HEXABLOCK::GUI;
102 using namespace HEXA_NS;
103 // !!! ceci est ni pipe ni un actor mais un mélange des deux
104 Document_Actor::Document_Actor( Document* doc, const QString& entry ):
105           SALOME_Actor(),
106           _doc( doc )
107 {
108     DEBTRACE("Document_Actor::Document_Actor " << entry.toLatin1() );
109     const char* docName = ((doc != NULL) ? doc->getName() : "");
110     Handle(SALOME_InteractiveObject) anIO = new SALOME_InteractiveObject( entry.toLatin1(), "HEXABLOCK", docName );//,theName); CS_TODO
111     setIO(anIO);
112     vtkUnstructuredGrid* aGrid = getUnstructuredGrid();
113     vtkDataSetMapper* aMapper = vtkDataSetMapper::New();
114     aMapper->SetInput(aGrid);
115     aGrid->Delete();
116
117     SetVisibility( true );//VisibilityOff();
118     SetPickable( true ); //PickableOff();//
119     SetMapper( aMapper );
120     aMapper->Delete();
121
122     vtkProperty* aProp = vtkProperty::New();
123
124 //    aProp->SetRepresentationToSurface();
125     aProp->SetRepresentationToWireframe();
126 //    aProp->SetRepresentationToPoints();
127     aProp->EdgeVisibilityOn ();
128     aProp->SetPointSize(5);
129     SetProperty( aProp );
130     aProp->Delete();
131 //    SetPointRepresentation(true);
132 }
133
134 Document_Actor::~Document_Actor()
135 {
136
137 }
138
139
140 vtkUnstructuredGrid* Document_Actor::getUnstructuredGrid()
141 {
142     vtkUnstructuredGrid* theGrid = vtkUnstructuredGrid::New();
143
144     _doc->reorderFaces(); //CS_TEST
145
146     map <int,vtkIdType>   vtkNodeId;
147     map <vtkIdType, int>  hexaNodeId;
148
149     // Create points
150     vtkPoints* aPoints = vtkPoints::New();
151     int nbVertex = _doc->countVertex();
152     aPoints->SetNumberOfPoints( nbVertex );
153
154     Vertex* v = NULL;
155     int vertexId;
156     for ( int i=0; i <nbVertex; ++i ){
157         v = _doc->getVertex(i);
158         aPoints->SetPoint( i, v->getX(), v->getY(), v->getZ() );
159         vertexId = reinterpret_cast<intptr_t>(v); //v->getId();
160         vtkNodeId [ vertexId ] = i;
161         hexaNodeId[ i ] = vertexId ;
162         //     vtkNodeId [ vertexId ] = i+1;
163         //     hexaNodeId[ i+1 ] = vertexId ;
164     }
165
166     theGrid->SetPoints( aPoints );
167     aPoints->Delete();
168     //   theGrid->SetCells( 0, 0, 0, 0, 0 );
169
170
171     // Calculate cells size
172     int nb0DElement = _doc->countVertex();
173     int nbEdge      = _doc->countEdge();
174     int nbFace      = _doc->countQuad();
175     int nbVolume    = _doc->countHexa();
176
177     vtkIdType aCellsSize =  2*nb0DElement + 3*nbEdge + ( 4 + 1 )*nbFace + ( 8 + 1 )*nbVolume;
178     vtkIdType aNbCells   =  nb0DElement + nbEdge + nbFace + nbVolume;
179
180     // Create cells
181     vtkCellArray* aConnectivity = vtkCellArray::New();
182     aConnectivity->Allocate( aCellsSize, 0 );
183
184     vtkUnsignedCharArray* aCellTypesArray = vtkUnsignedCharArray::New();
185     aCellTypesArray->SetNumberOfComponents( 1 );
186     aCellTypesArray->Allocate( aNbCells * aCellTypesArray->GetNumberOfComponents() );
187
188     vtkIdList *anIdList = vtkIdList::New();
189     vtkIdType iVtkElem = 0;
190     //   vtkIdType iVtkElem = 1; //CS_TEST
191     int       iHexaElem;
192
193     // VERTEX
194     for ( int i=0; i<nb0DElement; ++i ){
195         anIdList->SetNumberOfIds( 1 );
196         v = _doc->getVertex(i);
197         iHexaElem = reinterpret_cast<intptr_t>(v);//v->getId();
198         vtkElemsId[iHexaElem] = iVtkElem;
199         hexaElemsId[iVtkElem] = iHexaElem;
200         anIdList->SetId(0, vtkNodeId[iHexaElem]);
201         aConnectivity->InsertNextCell( anIdList );
202         aCellTypesArray->InsertNextValue( VTK_VERTEX );//getCellType( aType, anElem->IsPoly(), aNbNodes ) );
203         ++iVtkElem;
204     }
205
206     // EDGE
207     Edge* e = NULL;
208     Vertex* vertexElem = NULL;
209     for ( int i=0; i<nbEdge; ++i ){
210         anIdList->SetNumberOfIds( 2 );
211         e = _doc->getEdge(i);
212         iHexaElem = reinterpret_cast<intptr_t>(e); //e->getId();
213         vtkElemsId[iHexaElem] = iVtkElem;
214         hexaElemsId[iVtkElem] = iHexaElem;
215
216         for( vtkIdType j = 0; j< 2; ++j ){ //j< e->countVertex(); ++j ){
217             vertexElem = e->getVertex( j );
218             anIdList->SetId( j, vtkNodeId[ reinterpret_cast<intptr_t>(vertexElem) ] );//vertexElem->getId() ]);
219         }
220         aConnectivity->InsertNextCell( anIdList );
221         aCellTypesArray->InsertNextValue( VTK_LINE );//getCellType( aType, anElem->IsPoly(), aNbNodes ) );
222         ++iVtkElem;
223     }
224
225     // QUAD
226     Quad* q = NULL;
227     for ( int i=0; i<nbFace; ++i ){
228         anIdList->SetNumberOfIds( 4 );
229         q = _doc->getQuad(i);
230         iHexaElem = reinterpret_cast<intptr_t>(q); //q->getId();
231         vtkElemsId[iHexaElem] = iVtkElem;
232         hexaElemsId[iVtkElem] = iHexaElem;
233
234         for( vtkIdType j = 0; j< 4; ++j ){
235             vertexElem = q->getVertex( j );
236             anIdList->SetId( j, vtkNodeId[ reinterpret_cast<intptr_t>(vertexElem) ] );//vertexElem->getId() ]);
237         }
238         aConnectivity->InsertNextCell( anIdList );
239         aCellTypesArray->InsertNextValue( VTK_QUAD );//getCellType( aType, anElem->IsPoly(), aNbNodes ) );
240         ++iVtkElem;
241     }
242
243     // HEXA
244     Hexa* h = NULL;
245     std::map<int, int> connectivity;
246     connectivity[0] = 0;
247     connectivity[1] = 1;
248     connectivity[2] = 3;
249     connectivity[3] = 2;
250     connectivity[4] = 4;
251     connectivity[5] = 5;
252     connectivity[6] = 7;
253     connectivity[7] = 6;
254     for ( int i=0; i<nbVolume; ++i ){
255         anIdList->SetNumberOfIds( 8 );
256         h = _doc->getHexa(i);
257         iHexaElem = reinterpret_cast<intptr_t>(h); //q->getId();
258         vtkElemsId[iHexaElem] = iVtkElem;
259         hexaElemsId[iVtkElem] = iHexaElem;
260
261         for( vtkIdType j = 0; j< 8; ++j ){
262             vertexElem = h->getVertex( j );// );
263             anIdList->SetId( connectivity[j], vtkNodeId[ reinterpret_cast<intptr_t>(vertexElem) ]);//vertexElem->getId() ]);
264         }
265         aConnectivity->InsertNextCell( anIdList );
266         aCellTypesArray->InsertNextValue( VTK_HEXAHEDRON );
267         ++iVtkElem;
268     }
269
270
271     // 0        1      2     3        4     5      6      7
272     // V_ACE, V_ACF, V_ADE, V_ADF, V_BCE, V_BCF, V_BDE, V_BDF,
273     //
274     // 0        1     3      2        4    5      7      6
275
276     // Insert cells in grid
277     VTKViewer_CellLocationsArray* aCellLocationsArray = VTKViewer_CellLocationsArray::New();
278     aCellLocationsArray->SetNumberOfComponents( 1 );
279     aCellLocationsArray->SetNumberOfTuples( aNbCells );
280     //   std::cout << "aNbCells =>" << aNbCells << std::endl;
281
282     aConnectivity->InitTraversal();
283     for( vtkIdType idType = 0, *pts, npts; aConnectivity->GetNextCell( npts, pts ); idType++ ){
284         aCellLocationsArray->SetValue( idType, aConnectivity->GetTraversalLocation( npts ) );
285     }
286     theGrid->SetCells( aCellTypesArray, aCellLocationsArray,aConnectivity );
287
288     aCellLocationsArray->Delete();
289     aCellTypesArray->Delete();
290     aConnectivity->Delete();
291     anIdList->Delete();
292     //std::cout << "theGrid->GetNumberOfCells()" << theGrid->GetNumberOfCells() << std::endl;
293
294     return theGrid;
295 }
296
297 // =============================================================== Abu : debut
298 // ===================================================== Constructeur
299 Associate_Actor::Associate_Actor( Document* doc, const QString& entry)
300 : SALOME_Actor(), _doc( doc )
301 {
302     DEBTRACE("Associate_Actor::Associate_Actor " << entry.toLatin1() );
303     const char* docName = ((doc != NULL) ? doc->getName() : "");
304     Handle(SALOME_InteractiveObject) anIO = new SALOME_InteractiveObject( entry.toLatin1(), "HEXABLOCK", docName );//,theName); CS_TODO
305     setIO(anIO);
306     vtkUnstructuredGrid* aGrid = getUnstructuredGrid();
307
308     vtkDataSetMapper* aMapper = vtkDataSetMapper::New();
309     aMapper->SetInput(aGrid);
310     aGrid->Delete();
311
312     SetVisibility( true );//VisibilityOff();
313     SetPickable( true ); //PickableOff();//
314     SetMapper( aMapper );
315     aMapper->Delete();
316
317     vtkProperty* aProp = vtkProperty::New();
318     aProp->SetColor(0,255,0);
319 //    aProp->SetRepresentationToSurface();
320     aProp->SetRepresentationToWireframe();
321 //    aProp->SetRepresentationToPoints();
322     aProp->EdgeVisibilityOn ();
323     aProp->SetPointSize(5);
324     SetProperty( aProp );
325     aProp->Delete();
326 //    SetPointRepresentation(true);
327 }
328 // ===================================================== getUnstructuredGrid
329
330 vtkUnstructuredGrid* Associate_Actor::getUnstructuredGrid()
331 {
332     vtkUnstructuredGrid* theGrid = vtkUnstructuredGrid::New();
333
334     _doc->reorderFaces(); //CS_TEST
335
336     std::map<int,vtkIdType>   vtkNodeId;
337     std::map<vtkIdType, int>  hexaNodeId;
338
339     // Create points
340     vtkPoints* aPoints = vtkPoints::New();
341     int nbVertex = _doc->countVertex();
342     aPoints->SetNumberOfPoints( nbVertex );
343
344     Vertex* v = NULL;
345     int vertexId;
346     for ( int i=0; i <nbVertex; ++i ){
347         v = _doc->getVertex(i);
348         aPoints->SetPoint( i, v->getX()+6, v->getY()+6, v->getZ() );
349         vertexId = reinterpret_cast<intptr_t>(v); //v->getId();
350         vtkNodeId [ vertexId ] = i;
351         hexaNodeId[ i ] = vertexId ;
352         //     vtkNodeId [ vertexId ] = i+1;
353         //     hexaNodeId[ i+1 ] = vertexId ;
354     }
355
356     theGrid->SetPoints( aPoints );
357     aPoints->Delete();
358     //   theGrid->SetCells( 0, 0, 0, 0, 0 );
359
360     // Calculate cells size
361     int nb0DElement = _doc->countVertex();
362     int nbEdge      = _doc->countEdge();
363     int nbFace      = _doc->countQuad();
364     int nbVolume    = _doc->countHexa();
365
366     vtkIdType aCellsSize =  2*nb0DElement + 3*nbEdge + ( 4 + 1 )*nbFace + ( 8 + 1 )*nbVolume;
367     vtkIdType aNbCells   =  nb0DElement + nbEdge + nbFace + nbVolume;
368
369     // Create cells
370     vtkCellArray* aConnectivity = vtkCellArray::New();
371     aConnectivity->Allocate( aCellsSize, 0 );
372
373     vtkUnsignedCharArray* aCellTypesArray = vtkUnsignedCharArray::New();
374     aCellTypesArray->SetNumberOfComponents( 1 );
375     aCellTypesArray->Allocate( aNbCells * aCellTypesArray->GetNumberOfComponents() );
376
377     vtkIdList *anIdList = vtkIdList::New();
378     vtkIdType iVtkElem = 0;
379     //   vtkIdType iVtkElem = 1; //CS_TEST
380     int       iHexaElem;
381
382     // VERTEX
383     for ( int i=0; i<nb0DElement; ++i ){
384         anIdList->SetNumberOfIds( 1 );
385         v = _doc->getVertex(i);
386         iHexaElem = reinterpret_cast<intptr_t>(v);//v->getId();
387         vtkElemsId[iHexaElem] = iVtkElem;
388         hexaElemsId[iVtkElem] = iHexaElem;
389         anIdList->SetId(0, vtkNodeId[iHexaElem]);
390         aConnectivity->InsertNextCell( anIdList );
391         aCellTypesArray->InsertNextValue( VTK_VERTEX );//getCellType( aType, anElem->IsPoly(), aNbNodes ) );
392         ++iVtkElem;
393     }
394
395     // EDGE
396     Edge* e = NULL;
397     Vertex* vertexElem = NULL;
398     for ( int i=0; i<nbEdge; ++i ){
399         anIdList->SetNumberOfIds( 2 );
400         e = _doc->getEdge(i);
401         iHexaElem = reinterpret_cast<intptr_t>(e); //e->getId();
402         vtkElemsId[iHexaElem] = iVtkElem;
403         hexaElemsId[iVtkElem] = iHexaElem;
404
405         for( vtkIdType j = 0; j< 2; ++j ){ //j< e->countVertex(); ++j ){
406             vertexElem = e->getVertex( j );
407             anIdList->SetId( j, vtkNodeId[ reinterpret_cast<intptr_t>(vertexElem) ] );//vertexElem->getId() ]);
408         }
409         aConnectivity->InsertNextCell( anIdList );
410         aCellTypesArray->InsertNextValue( VTK_LINE );//getCellType( aType, anElem->IsPoly(), aNbNodes ) );
411         ++iVtkElem;
412     }
413
414     // QUAD
415     Quad* q = NULL;
416     for ( int i=0; i<nbFace; ++i ){
417         anIdList->SetNumberOfIds( 4 );
418         q = _doc->getQuad(i);
419         iHexaElem = reinterpret_cast<intptr_t>(q); //q->getId();
420         vtkElemsId[iHexaElem] = iVtkElem;
421         hexaElemsId[iVtkElem] = iHexaElem;
422
423         for( vtkIdType j = 0; j< 4; ++j ){
424             vertexElem = q->getVertex( j );
425             anIdList->SetId( j, vtkNodeId[ reinterpret_cast<intptr_t>(vertexElem) ] );//vertexElem->getId() ]);
426         }
427         aConnectivity->InsertNextCell( anIdList );
428         aCellTypesArray->InsertNextValue( VTK_QUAD );//getCellType( aType, anElem->IsPoly(), aNbNodes ) );
429         ++iVtkElem;
430     }
431
432     // HEXA
433     Hexa* h = NULL;
434     std::map<int, int> connectivity;
435     connectivity[0] = 0;
436     connectivity[1] = 1;
437     connectivity[2] = 3;
438     connectivity[3] = 2;
439     connectivity[4] = 4;
440     connectivity[5] = 5;
441     connectivity[6] = 7;
442     connectivity[7] = 6;
443     for ( int i=0; i<nbVolume; ++i ){
444         anIdList->SetNumberOfIds( 8 );
445         h = _doc->getHexa(i);
446         iHexaElem = reinterpret_cast<intptr_t>(h); //q->getId();
447         vtkElemsId[iHexaElem] = iVtkElem;
448         hexaElemsId[iVtkElem] = iHexaElem;
449
450         for( vtkIdType j = 0; j< 8; ++j ){
451             vertexElem = h->getVertex( j );// );
452             anIdList->SetId( connectivity[j], vtkNodeId[ reinterpret_cast<intptr_t>(vertexElem) ]);//vertexElem->getId() ]);
453         }
454         aConnectivity->InsertNextCell( anIdList );
455         aCellTypesArray->InsertNextValue( VTK_HEXAHEDRON );
456         ++iVtkElem;
457     }
458
459
460     // 0        1      2     3        4     5      6      7
461     // V_ACE, V_ACF, V_ADE, V_ADF, V_BCE, V_BCF, V_BDE, V_BDF,
462     //
463     // 0        1     3      2        4    5      7      6
464
465     // Insert cells in grid
466     VTKViewer_CellLocationsArray* aCellLocationsArray = VTKViewer_CellLocationsArray::New();
467     aCellLocationsArray->SetNumberOfComponents( 1 );
468     aCellLocationsArray->SetNumberOfTuples( aNbCells );
469     //   std::cout << "aNbCells =>" << aNbCells << std::endl;
470
471     aConnectivity->InitTraversal();
472     for( vtkIdType idType = 0, *pts, npts; aConnectivity->GetNextCell( npts, pts ); idType++ ){
473         aCellLocationsArray->SetValue( idType, aConnectivity->GetTraversalLocation( npts ) );
474     }
475     theGrid->SetCells( aCellTypesArray, aCellLocationsArray,aConnectivity );
476
477     aCellLocationsArray->Delete();
478     aCellTypesArray->Delete();
479     aConnectivity->Delete();
480     anIdList->Delete();
481
482     return theGrid;
483 }
484
485
486 // -----------------------------------------------------------------------
487 //          VtkDocumentGraphicView implementation
488 // -----------------------------------------------------------------------
489 VtkDocumentGraphicView::VtkDocumentGraphicView( DocumentModel* documentModel,
490         SVTK_ViewWindow* view, QWidget* parent ):
491         QAbstractItemView(parent),
492         viewWindow( view ),
493         documentActor( NULL ),
494         associateActor (NULL), // Abu
495         _currentChanged( false ),
496         firstUpdate(true),
497         selectionMode(-1)
498 {
499
500     //Model
501     setModel(documentModel);
502     patternDataModel     = new PatternDataModel(parent);
503     patternBuilderModel  = new PatternBuilderModel(parent);
504     patternGeomModel     = new PatternGeomModel(parent);
505     groupsModel          = new GroupsModel(parent);
506     meshModel            = new MeshModel(parent);
507
508     patternDataModel->setSourceModel(documentModel);
509     patternBuilderModel->setSourceModel(documentModel);
510     patternGeomModel->setSourceModel(documentModel);
511     groupsModel->setSourceModel(documentModel);
512     meshModel->setSourceModel(documentModel);
513
514     //Selection Model
515     patternDataSelectionModel    = new PatternDataSelectionModel(patternDataModel);
516     patternBuilderSelectionModel = new PatternBuilderSelectionModel(patternBuilderModel, patternDataSelectionModel);
517     patternGeomSelectionModel    = new PatternGeomSelectionModel(patternGeomModel);
518     groupsSelectionModel         = new GroupsSelectionModel(groupsModel);
519     meshSelectionModel           = new MeshSelectionModel(meshModel );
520
521     setSelectionModel(patternDataSelectionModel);
522 }
523
524 //Loads the graphic view document
525 void VtkDocumentGraphicView::loadDocument()
526 {
527     DocumentModel* docModel = getDocumentModel();
528     if (docModel == NULL) return;
529
530     docModel->load();
531 }
532
533 //Loads the document from the file in this graphic view
534 void VtkDocumentGraphicView::loadDocument(const QString& file)
535 {
536     if ( file.isEmpty() ) return;
537     getDocumentModel()->load(file);
538 }
539
540 //Saves the graphic view's document in the file
541 void VtkDocumentGraphicView::saveDocument(const QString& file)
542 {
543     if (file.isEmpty()) return;
544     getDocumentModel()->save(file);
545 }
546
547
548 VtkDocumentGraphicView::~VtkDocumentGraphicView()
549 {
550
551     //Delete Model
552     delete patternDataModel;
553     delete patternBuilderModel;
554     delete groupsModel;
555     delete meshModel;
556
557     //Delete Selection Model/ Disconnect signals on delete (Qt)
558 //    delete patternDataSelectionModel;
559 //    delete patternBuilderSelectionModel;
560 //    delete groupsSelectionModel;
561 //    delete meshSelectionModel;
562 }
563
564 void VtkDocumentGraphicView::setWindowTitle(const QString& title)
565 {
566     viewWindow->setWindowTitle( QString("hexablock : ") + title );
567 }
568
569 void VtkDocumentGraphicView::onPatternDatachanged()
570 {
571     update();
572 }
573
574 void VtkDocumentGraphicView::removeActor()
575 {
576     if ( documentActor != NULL && viewWindow != NULL){
577           viewWindow->RemoveActor( documentActor );
578           documentActor->Delete();
579     }
580 }
581
582 void VtkDocumentGraphicView::update()
583 {
584     DocumentModel*   theModel = dynamic_cast<DocumentModel*>( model() );
585     if (theModel == NULL || viewWindow == NULL) return;
586
587     Document*  theDocumentImpl  = theModel->documentImpl();
588     QString    theDocumentEntry = theModel->documentEntry();
589
590     if ( documentActor ){
591         viewWindow->RemoveActor( documentActor );
592         documentActor->Delete();
593     }
594     documentActor  = new Document_Actor( theDocumentImpl, theDocumentEntry );
595     viewWindow->AddActor( documentActor );
596
597     viewWindow->getRenderer()->Render();
598     viewWindow->Repaint();
599     if (firstUpdate)
600     {
601         viewWindow->onFitAll();
602         firstUpdate = false;
603     }
604 }
605
606 void VtkDocumentGraphicView::setVertexSelection()
607 {
608   setSelectionMode(NodeSelection);
609 // //  NodeSelection,
610 // //  CellSelection,
611 // //  EdgeOfCellSelection,
612 // //  EdgeSelection,
613 // //  FaceSelection,
614 // //  VolumeSelection,
615 // //  ActorSelection };
616 }
617
618 void VtkDocumentGraphicView::setEdgeSelection()
619 {
620   setSelectionMode(EdgeSelection);
621 }
622
623 void VtkDocumentGraphicView::setQuadSelection()
624 {
625   setSelectionMode(FaceSelection);
626 }
627
628 void VtkDocumentGraphicView::setHexaSelection()
629 {
630 //  SetSelectionMode(VolumeSelection);
631   setSelectionMode(FaceSelection); //temporary for hexa selection debug
632   selectionMode = HEXA_TREE;
633 //  aProp->FaceVisibilityOff (); //would debug hexa preselection, selection???
634 }
635
636 void VtkDocumentGraphicView::setAllSelection()
637 {
638   setSelectionMode(ActorSelection);
639   selectionMode = -1;
640 }
641
642 void  VtkDocumentGraphicView::setSelectionMode(Selection_Mode theMode)
643 {
644     if (viewWindow == NULL || selectionMode == theMode)
645         return;
646
647     HEXABLOCKGUI::selectionMgr()->clearSelected();
648     viewWindow->SetSelectionMode( theMode );
649     selectionMode = theMode;
650 }
651
652 void VtkDocumentGraphicView::setSelectionMode( const QModelIndex& eltIndex )
653 {
654   QVariant treeVariant = eltIndex.data( HEXA_TREE_ROLE );
655   if ( !treeVariant.isValid() ) return;
656   int eltType = treeVariant.toInt();
657
658   if ( selectionMode == eltType ) return;
659
660   switch ( eltType ){
661     case VERTEX_TREE :
662     case GEOMPOINT_TREE:
663     case GEOMPOINT_DIR_TREE:
664     case VERTEX_DIR_TREE : setVertexSelection(); break;
665     case EDGE_TREE :
666     case GEOMEDGE_TREE:
667     case GEOMEDGE_DIR_TREE:
668     case EDGE_DIR_TREE :   setEdgeSelection(); break;
669     case QUAD_TREE :
670     case GEOMFACE_TREE:
671     case GEOMFACE_DIR_TREE:
672     case QUAD_DIR_TREE :   setQuadSelection();   break;
673     case HEXA_TREE :
674     case GEOMSHAPE_TREE:
675     case GEOMSHAPE_DIR_TREE:
676     case HEXA_DIR_TREE :   setHexaSelection();  break;
677     case PROPAGATION_TREE :
678     case PROPAGATION_DIR_TREE :   setEdgeSelection(); break;
679     default: setAllSelection();
680 //  CellSelection,
681 //  EdgeOfCellSelection,
682 //  VolumeSelection,
683 //  ActorSelection
684   }
685 }
686
687 void VtkDocumentGraphicView::getSelected(SALOME_ListIO& selectedObjects)
688 {
689     HEXABLOCKGUI::selectionMgr()->selectedObjects( selectedObjects, SVTK_Viewer::Type());
690 }
691
692 void VtkDocumentGraphicView::clearSelection()
693 {
694     if (viewWindow != NULL)
695     {
696 //        viewWindow->unHighlightAll();
697         setSelectionMode(ActorSelection);
698     }
699 }
700
701 void VtkDocumentGraphicView::highlight( const QModelIndex& ielt )
702 {
703
704     QModelIndexList indexList;
705     indexList << ielt;
706     highlight(indexList);
707
708 }
709
710 void VtkDocumentGraphicView::highlight( const QModelIndexList& elts )
711 {
712
713     if (elts.size() == 0 || viewWindow == NULL || documentActor == NULL)
714         return;
715     SVTK_Selector* selector = viewWindow->GetSelector();
716     if ( selector == NULL )
717         return;
718     //   Set selection mode in VTK view
719 //    viewWindow->SetSelectionMode(VolumeSelection);
720
721     // --- elements highlight ---
722     TColStd_IndexedMapOfInteger aMap;
723     int vtkElemsId;
724     QString eltEntry;
725
726     foreach( const QModelIndex& iElt, elts ){
727         if (iElt.isValid())
728         {
729             eltEntry = iElt.data( HEXA_ENTRY_ROLE ).toString();
730             vtkElemsId = documentActor->vtkElemsId[ eltEntry.toInt() ];
731             if ( vtkElemsId > 0 )
732                 aMap.Add( vtkElemsId );
733         }
734     }
735     selector->AddOrRemoveIndex( documentActor->getIO(), aMap, false );
736     viewWindow->highlight( documentActor->getIO(), true, true );
737 }
738
739
740 void VtkDocumentGraphicView::highlightGroups( const QModelIndex& eltIndex )
741 {
742     if ( groupsModel == NULL || viewWindow == NULL || documentActor == NULL) return;
743     SVTK_Selector* selector = viewWindow->GetSelector();
744     if ( selector == NULL ) return;
745
746     QVariant treeVariant        = eltIndex.data( HEXA_TREE_ROLE );
747     if ( !treeVariant.isValid())
748         return;
749
750     int eltType  = treeVariant.toInt();
751     if ( eltType != GROUP_TREE )
752         return;
753
754     //Get elements to highlight
755     DocumentModel::Group kind;
756     QModelIndexList iElements = groupsModel->getGroupElements( eltIndex, kind );
757     if (iElements.count() == 0) return;
758
759     // ---------- elements highlight --------------
760
761     // Set selection mode in VTK view
762     switch (kind){
763      case HexaCell: case HexaNode: viewWindow->SetSelectionMode(VolumeSelection); break;
764      case QuadCell: case QuadNode: viewWindow->SetSelectionMode(FaceSelection);   break;
765      case EdgeCell: case EdgeNode: viewWindow->SetSelectionMode(EdgeSelection);   break;
766      case VertexNode: viewWindow->SetSelectionMode(NodeSelection); break;
767     }
768
769     TColStd_MapOfInteger aMap;
770     int vtkElemsId;
771     QString eltEntry;
772
773     foreach( const QModelIndex& iElt, iElements ){
774         if (iElt.isValid())
775         {
776             eltEntry = iElt.data( HEXA_ENTRY_ROLE ).toString();
777             vtkElemsId = documentActor->vtkElemsId[ eltEntry.toInt() ];
778             if ( vtkElemsId > 0 ) aMap.Add( vtkElemsId );
779         }
780     }
781
782     selector->AddOrRemoveIndex( documentActor->getIO(), aMap, false );
783     viewWindow->highlight( documentActor->getIO(), true, true );
784     documentActor->highlight( false );
785
786 }
787
788
789 void VtkDocumentGraphicView::highlightPropagation( const QModelIndex& eltIndex )
790 {
791     if ( meshModel == NULL || viewWindow == NULL || documentActor == NULL) return;
792     SVTK_Selector* selector = viewWindow->GetSelector();
793     if ( selector == NULL ) return;
794
795     QVariant treeVariant        = eltIndex.data( HEXA_TREE_ROLE );
796     if ( !treeVariant.isValid())
797         return;
798
799     int eltType  = treeVariant.toInt();
800     if ( eltType != PROPAGATION_TREE )
801         return;
802
803     // Get elements to highlight
804     QModelIndexList iEdges = meshModel->getPropagation( eltIndex );
805     if (iEdges.count() == 0) return;
806
807     // ---------- elements highlight --------------
808
809     // Set selection mode in VTK view
810     viewWindow->SetSelectionMode(EdgeSelection);
811
812     TColStd_MapOfInteger aMap;
813     int vtkElemsId;
814     QString edgeEntry;
815
816     foreach( const QModelIndex& iEdge, iEdges ){
817         if (iEdge.isValid())
818         {
819             edgeEntry = iEdge.data( HEXA_ENTRY_ROLE ).toString();
820             vtkElemsId = documentActor->vtkElemsId[ edgeEntry.toInt() ];
821             if ( vtkElemsId > 0 ) aMap.Add( vtkElemsId );
822         }
823     }
824
825     selector->AddOrRemoveIndex( documentActor->getIO(), aMap, false );
826     viewWindow->highlight( documentActor->getIO(), true, true );
827     documentActor->highlight( false );
828
829 }
830
831
832
833
834 /********************************************************************************
835  *                   ABSTRACT METHOD ( MUST BE IMPLEMENTED )
836  ********************************************************************************/
837
838 /*
839     Returns the item that covers the coordinate given in the view.
840  */
841
842 QModelIndex VtkDocumentGraphicView::indexAt(const QPoint &point) const
843 {
844     return QModelIndex();
845 }
846
847 void VtkDocumentGraphicView::scrollTo(const QModelIndex &index, ScrollHint)
848 {
849 }
850
851
852 /*
853     Returns the position of the item in viewport coordinates.
854  */
855 QRect VtkDocumentGraphicView::visualRect(const QModelIndex &index) const
856 {
857     return QRect();
858 }
859
860 // PROTECTED :
861 int VtkDocumentGraphicView::horizontalOffset() const
862 {
863     return horizontalScrollBar()->value();
864 }
865
866 bool VtkDocumentGraphicView::isIndexHidden(const QModelIndex &index) const
867 {
868     return false;
869 }
870
871 QModelIndex VtkDocumentGraphicView::moveCursor(QAbstractItemView::CursorAction cursorAction,
872         Qt::KeyboardModifiers /*modifiers*/)
873 {
874     QModelIndex current = currentIndex();
875     return current;
876 }
877
878 /*
879     Find the indices corresponding to the extent of the selection.
880  */
881 void VtkDocumentGraphicView::setSelection(const QRect &rect, QItemSelectionModel::SelectionFlags command)
882 {
883 }
884
885
886 int VtkDocumentGraphicView::verticalOffset() const
887 {
888     return verticalScrollBar()->value();
889 }
890
891 /*
892     Returns a region corresponding to the selection in viewport coordinates.
893  */
894 QRegion VtkDocumentGraphicView::visualRegionForSelection(const QItemSelection &selection) const
895 {
896     return QRegion();
897 }
898
899
900 /********************************************************************************
901  *                   PROTECTED SLOTS
902  ********************************************************************************/
903 void VtkDocumentGraphicView::closeEditor ( QWidget * editor, QAbstractItemDelegate::EndEditHint hint )
904 {
905 }
906
907 void VtkDocumentGraphicView::commitData ( QWidget * editor )
908 {
909 }
910
911 void VtkDocumentGraphicView::currentChanged( const QModelIndex & current, const QModelIndex & previous )
912 {
913     _currentChanged = true;
914 }
915
916 void VtkDocumentGraphicView::dataChanged ( const QModelIndex & topLeft, const QModelIndex & bottomRight )
917 {
918     update();
919     _currentChanged = false;
920 }
921
922 void VtkDocumentGraphicView::editorDestroyed ( QObject * editor )
923 {
924 }
925
926 void VtkDocumentGraphicView::rowsAboutToBeRemoved ( const QModelIndex & parent, int start, int end )
927 {
928 }
929
930 void VtkDocumentGraphicView::rowsInserted ( const QModelIndex & parent, int start, int end )
931 {
932 }
933
934 void VtkDocumentGraphicView::selectionChanged( const QItemSelection & selected, const QItemSelection & deselected )
935 {
936 }
937
938 void VtkDocumentGraphicView::updateGeometries ()
939 {
940 }
941
942
943 void VtkDocumentGraphicView::setModel ( QAbstractItemModel * model )
944 {
945     QAbstractItemView::setModel( model );
946
947 //    PatternDataModel* pdm = dynamic_cast<PatternDataModel*>(model);
948 //    if (pdm)
949 //       connect( pdm, SIGNAL(patternDataChanged() ), this,  SLOT ( onPatternDatachanged() ) );
950
951     DocumentModel* dm = dynamic_cast<DocumentModel*>(model);
952     if (dm){
953 //        setWindowTitle( dm->getName() );
954         connect( dm, SIGNAL(patternDataChanged() ), this,  SLOT ( onPatternDatachanged() ) );
955 //        connect( dm, SIGNAL( nameChanged(const QString&) ), this,  SLOT ( setWindowTitle(const QString&) ) );
956     }
957 }
958
959 // void DocumentGraphicView::loadVTK( const QString&  path ) //CS_TEST
960 // {
961 //   std::cout << "DocumentGraphicView::loadVTK=>"<<std::endl;
962 //   QFile file(path);
963 //   if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
964 //     return;
965 // //   QByteArray vtkData = file.readAll ();
966 //   QString vtkData = file.readAll ();
967 //   vtkData.replace(",",".");
968 //
969 //
970 //   SVTK_ViewWindow* myVTKViewWindow = dynamic_cast<SVTK_ViewWindow*>(_suitView);
971 //
972 //   // vtkStructuredGridReader
973 //   vtkUnstructuredGridReader* r = vtkUnstructuredGridReader::New();
974 // //   r->SetFileName( path.toLocal8Bit().constData() );
975 //   r->SetInputString( vtkData.toLocal8Bit().constData() );
976 //   r->SetReadFromInputString( true );
977 //   r->Update();
978 //
979 //   vtkUnstructuredGrid* myGrid = r->GetOutput();//vtkUnstructuredGrid::New();
980 //   std::cout << "GetNumberOfCells =>"<< myGrid->GetNumberOfCells();
981 //   // Create and display actor
982 //
983 //   vtkDataSetMapper* myMapper = vtkDataSetMapper::New();
984 //   myMapper->SetInput(myGrid);
985 //
986 // //   if ( myPreviewActor ){
987 // //     myVTKViewWindow->RemoveActor(myPreviewActor);
988 // //     myPreviewActor->Delete();
989 // //   }
990 //
991 //   SALOME_Actor* myPreviewActor = SALOME_Actor::New();
992 //   myPreviewActor = SALOME_Actor::New();
993 //   Handle(SALOME_InteractiveObject) anIO = new SALOME_InteractiveObject(QString::number( reinterpret_cast<intptr_t>(_hexaDocument) ),"HEXABLOCK");//,theName);
994 //   myPreviewActor->setIO(anIO);
995 //
996 // //   myPreviewActor->PickableOff();
997 //   myPreviewActor->SetVisibility( true );//VisibilityOff();
998 //   myPreviewActor->SetPickable( true );
999 //   myPreviewActor->SetMapper(myMapper);
1000 //
1001 //   vtkProperty* aProp = vtkProperty::New();
1002 // //   aProp->SetRepresentationToWireframe();
1003 //   aProp->SetRepresentationToSurface();
1004 //   aProp->EdgeVisibilityOn ();
1005 //
1006 // //   aProp->SetColor(10, 10, 250);
1007 //   aProp->SetPointSize(5);
1008 //   myPreviewActor->SetProperty(aProp);
1009 //   aProp->Delete();
1010 //
1011 //   /*vtkProperty* myBackProp = vtkProperty::New();
1012 //   GetColor( "SMESH", "backface_color", aBackRGB[0], aBackRGB[1], aBackRGB[2], QColor( 0, 0, 255 ) );
1013 //   myBackProp->SetColor( aBackRGB[0], aBackRGB[1], aBackRGB[2] );
1014 //   myPreviewActor->SetBackfaceProperty( myBackProp );
1015 //   myBackProp->Delete()*/;
1016 //   myVTKViewWindow->AddActor(myPreviewActor);
1017 //   myVTKViewWindow->getRenderer()->Render();
1018 //   myVTKViewWindow->Repaint();
1019 //   myVTKViewWindow->onFitAll();
1020 //
1021 //   myVTKViewWindow->SetSelectionMode( ActorSelection );
1022 // // myVTKViewWindow->SetSelectionMode( NodeSelection );
1023 // // myVTKViewWindow->SetSelectionMode( EdgeSelection );
1024 // // myVTKViewWindow->SetSelectionMode( FaceSelection );
1025 // }