Salome HOME
Modif Hexablock.py
[modules/hexablock.git] / src / HEXABLOCKGUI / HEXABLOCKGUI_DocumentSelectionModel.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 #include <set>
21 #include <map>
22
23
24 #include "utilities.h"
25
26 #include <QtxWorkstack.h>
27 #include <STD_TabDesktop.h>
28 #include <STD_MDIDesktop.h>
29 #include <STD_SDIDesktop.h>
30
31 #include <SalomeApp_Tools.h>
32
33 #include "SOCC_ViewModel.h"
34 #include <OCCViewer_ViewManager.h>
35 #include <OCCViewer_ViewModel.h>
36 #include <OCCViewer_ViewWindow.h>
37
38 #include <SUIT_OverrideCursor.h>
39 #include <SUIT_MessageBox.h>
40 #include <SUIT_Session.h>
41 #include <SUIT_Selector.h>
42 #include <SUIT_Desktop.h>
43 #include <SUIT_ViewManager.h>
44 #include <SVTK_View.h>
45
46 #include "HEXABLOCKGUI_VtkDocumentGraphicView.hxx"
47 #include "HEXABLOCKGUI_OccGraphicView.hxx"
48 #include "HEXABLOCKGUI_SalomeTools.hxx"
49 #include "HEXABLOCKGUI_DocumentSelectionModel.hxx"
50 #include "HEXABLOCKGUI_DocumentItem.hxx"
51 #include "HEXABLOCKGUI.hxx"
52
53
54 #include <SVTK_Selector.h>
55 #include <SVTK_ViewModel.h>
56
57
58 #include <SalomeApp_Study.h>
59 #include <SalomeApp_Application.h>
60 #include <SALOME_Actor.h>
61 #include <SALOME_ListIO.hxx>
62 #include <SALOME_ListIteratorOfListIO.hxx>
63 #include <AIS_ListIteratorOfListOfInteractive.hxx>
64
65 #include <QMultiMap>
66
67 #include <OCCViewer_ViewModel.h>
68 #include <SVTK_ViewModel.h>
69 #include <SalomeApp_Study.h>
70 #include <SalomeApp_Application.h>
71 #include <LightApp_SelectionMgr.h>
72 #include <SALOME_ListIteratorOfListIO.hxx>
73 #include <TopExp.hxx>
74 #include <TopExp_Explorer.hxx>
75 #include <TopoDS_Iterator.hxx>
76 #include <TopTools_MapOfShape.hxx>
77 #include <TopTools_IndexedMapOfShape.hxx>
78 #include <TColStd_IndexedMapOfInteger.hxx>
79
80
81 //#define _DEVDEBUG_
82 using namespace std;
83 using namespace HEXABLOCK::GUI;
84
85
86 // //===========================================================================
87 //                              SelectionModel
88 // //===========================================================================
89
90 bool SelectionModel::infoMode = true;
91
92 SelectionModel::SelectionModel( QAbstractItemModel * model ):
93 QItemSelectionModel( model )
94 {
95
96 }
97
98 SelectionModel::~SelectionModel()
99 {
100   disconnect( HEXABLOCKGUI::selectionMgr(), SIGNAL( currentSelectionChanged() ), this, SLOT( salomeSelectionChanged() ) );
101 }
102
103
104 QModelIndex SelectionModel::indexBy( int role, const QString& value )
105 {
106   QModelIndex eltIndex; // element (vertex, edge, quad) of model
107   const QAbstractItemModel* theModel = model();
108   if ( !theModel ) return eltIndex;
109   QModelIndexList theIndexes = theModel->match( theModel->index(0, 0),
110                                           role,
111                                           value,
112                                           1,
113                                           Qt::MatchRecursive | Qt::MatchContains );//Qt::MatchFixedString );
114   if ( theIndexes.count()>0 )
115     eltIndex = theIndexes[0] ;
116   return eltIndex;
117 }
118
119
120 QModelIndex SelectionModel::indexBy( int role, const QVariant& var )
121 {
122   QModelIndex eltIndex; // element (vertex, edge, quad) of model
123   const QAbstractItemModel* theModel = model();
124   if ( !theModel ) return eltIndex;
125   QModelIndexList theIndexes = theModel->match( theModel->index(0, 0),
126                                           role,
127                                           var,
128                                           1,
129                                           Qt::MatchRecursive /*| Qt::MatchContains*/ );//Qt::MatchFixedString );
130   if ( theIndexes.count()>0 )
131     eltIndex = theIndexes[0] ;
132   return eltIndex;
133 }
134
135 QModelIndex SelectionModel::indexOf( const QString& anEntry, int role )
136 {
137   QModelIndex eltIndex; // element (vertex, edge, quad) of model
138   const QAbstractItemModel* theModel = model();
139   if ( !theModel ) return eltIndex;
140
141   QModelIndexList theIndexes = theModel->match( theModel->index(0, 0),
142                                           role,
143                                           anEntry,
144                                           1,
145                                           Qt::MatchRecursive | Qt::MatchContains );//Qt::MatchFixedString );
146   if ( theIndexes.count()>0 )
147     eltIndex = theIndexes[0] ;
148   return eltIndex;
149 }
150
151
152 QModelIndexList SelectionModel::indexListOf( const QString& anEntry, int role )
153 {
154   QModelIndexList theIndexes; // element (vertex, edge, quad) of model
155   const QAbstractItemModel* theModel = model();
156   if ( !theModel ) return theIndexes;
157   theIndexes = theModel->match( theModel->index(0, 0),
158                                 role,
159                                 anEntry,
160                                 -1,
161                                 Qt::MatchRecursive | Qt::MatchContains );//Qt::MatchFixedString );
162   return theIndexes;
163 }
164
165 DocumentModel* SelectionModel::getDocModel() const
166 {
167     DocumentModel* docModel = NULL;
168     const QSortFilterProxyModel *pModel   = dynamic_cast<const QSortFilterProxyModel *>( model() );
169     if ( pModel != NULL)
170         docModel = dynamic_cast<DocumentModel*>( pModel->sourceModel() );
171
172     return docModel;
173 }
174
175 void SelectionModel::showEltInfo(QModelIndex& elt)
176 {
177     DocumentModel* docModel = getDocModel();
178     if (!elt.isValid() || docModel == NULL)
179         return;
180
181     HEXA_NS::Vertex* vertex  = docModel->getHexaPtr<HEXA_NS::Vertex*>(elt);
182     HEXA_NS::Edge*   edge    = docModel->getHexaPtr<HEXA_NS::Edge*>(elt);
183     HEXA_NS::Quad*   quad    = docModel->getHexaPtr<HEXA_NS::Quad*>(elt);
184     HEXA_NS::Hexa*   hexa    = docModel->getHexaPtr<HEXA_NS::Hexa*>(elt);
185     HEXA_NS::Vector* vector  = docModel->getHexaPtr<HEXA_NS::Vector*>(elt);
186     HEXA_NS::Group*  group   = docModel->getHexaPtr<HEXA_NS::Group*>(elt);
187     HEXA_NS::Law*    law     = docModel->getHexaPtr<HEXA_NS::Law*>(elt);
188     HEXA_NS::Propagation* propagation = docModel->getHexaPtr<HEXA_NS::Propagation*>(elt);
189
190     HEXABLOCKGUI*    hexagui = HEXABLOCKGUI::getInstance();
191     if (vertex != NULL)
192         hexagui->showVertexInfoDialog(vertex);
193     else if (edge != NULL)
194         hexagui->showEdgeInfoDialog(edge);
195     else if (quad != NULL)
196         hexagui->showQuadInfoDialog(quad);
197     else if (hexa != NULL)
198         hexagui->showHexaInfoDialog(hexa);
199     else if (vector != NULL)
200         hexagui->showVectorInfoDialog(vector);
201     else if (group != NULL)
202         hexagui->showGroupInfoDialog(group);
203     else if (law != NULL)
204         hexagui->showLawInfoDialog(law);
205     else if (propagation != NULL)
206         hexagui->showPropagationInfoDialog(propagation);
207 }
208
209 void SelectionModel::salomeSelectionChanged()
210 {
211     // clear highlights and selections in the trees
212     PatternDataSelectionModel* pdsm = HEXABLOCKGUI::currentDocGView->getPatternDataSelectionModel();
213 //    PatternBuilderSelectionModel* pbsm = HEXABLOCKGUI::currentDocGView->getPatternBuilderSelectionModel();
214     PatternGeomSelectionModel* pgsm = HEXABLOCKGUI::currentDocGView->getPatternGeomSelectionModel();
215     pdsm->clearSelection();
216 //    pbsm->clearSelection();
217     pgsm->clearSelection();
218     pdsm->unhighlightTreeItems();
219     pgsm->unhighlightTreeItems();
220
221     try {
222         SALOME_ListIO selectedObjects;
223         Handle(SALOME_InteractiveObject) anIObject;
224
225         switch (HEXABLOCKGUI::getActiveViewType())
226         {
227         case HEXABLOCKGUI::VTK:
228         {
229             HEXABLOCKGUI::currentDocGView->getSelected(selectedObjects);
230             if ( selectedObjects.IsEmpty() )
231                 return;
232             anIObject = selectedObjects.First();
233             vtkSelectionChanged( anIObject );
234             break;
235         }
236         case HEXABLOCKGUI::OCC:
237             HEXABLOCKGUI::currentOccGView->getSelected(selectedObjects);
238             if ( selectedObjects.IsEmpty() )
239                 return;
240             anIObject = selectedObjects.First();
241             geomSelectionChanged( anIObject );
242             break;
243         }
244     } catch ( ... ) {
245         MESSAGE("*  Unknown selection exception!");
246     }
247 }
248
249
250 QModelIndexList SelectionModel::getSelectionFromModel(const Handle(SALOME_InteractiveObject)& anIObject)
251 {
252    QModelIndexList selectedIndexes;
253
254    //verify if the IOBject is valid and from VTK selection
255    bool fromVTK  = ( strcmp("HEXABLOCK", anIObject->getComponentDataType()) == 0 );
256    if ( !fromVTK || !anIObject->hasEntry() || HEXABLOCKGUI::currentDocGView->getViewWindow() == NULL)
257       return selectedIndexes;
258
259    QString anIOEntry = anIObject->getEntry();
260    int anhexaElemsId;
261    QString aText = "";
262
263    //extract vtk selection from the model -----------
264    if ( GetNameOfSelectedElements( HEXABLOCKGUI::currentDocGView->getViewWindow(), anIObject, aText ) <= 0 )
265       return selectedIndexes;
266
267    Document_Actor* docActor = dynamic_cast<Document_Actor*>( findActorByEntry( HEXABLOCKGUI::currentDocGView->getViewWindow(),
268                                                                                        anIOEntry.toLatin1() ) );
269    if ( !docActor ) return selectedIndexes;
270    QStringList idList = aText.split(" ");
271    foreach( const QString& id, idList )
272    {
273       if (!id.isEmpty())
274          {
275             //find selection in the model
276             anhexaElemsId = docActor->hexaElemsId[ id.toInt() ];
277             anIOEntry = QString::number( anhexaElemsId );
278
279             selectedIndexes << indexOf( anIOEntry, HEXA_ENTRY_ROLE );
280          }
281    }
282    return selectedIndexes;
283 }
284
285 // //===========================================================================
286 //                              PatternDataSelectionModel
287 // //===========================================================================
288
289 PatternDataSelectionModel::PatternDataSelectionModel( QAbstractItemModel * model ):
290 SelectionModel( model )
291 {
292   connect( this, SIGNAL( currentChanged( const QModelIndex &, const QModelIndex & ) ),
293            this, SLOT( onCurrentChanged( const QModelIndex & , const QModelIndex & ) ), Qt::UniqueConnection );
294   connect( this, SIGNAL( selectionChanged( const QItemSelection & , const QItemSelection & ) ),
295            this, SLOT( onSelectionChanged( const QItemSelection & , const QItemSelection & ) ), Qt::UniqueConnection );
296
297 }
298
299 PatternDataSelectionModel::~PatternDataSelectionModel()
300 {
301   disconnect( this, SIGNAL( currentChanged( const QModelIndex &, const QModelIndex & ) ),
302           this, SLOT( onCurrentChanged( const QModelIndex & , const QModelIndex & ) ) );
303   disconnect( this, SIGNAL( selectionChanged( const QItemSelection & , const QItemSelection & ) ),
304           this, SLOT( onSelectionChanged( const QItemSelection & , const QItemSelection & ) ) );
305 }
306
307
308 void PatternDataSelectionModel::onCurrentChanged( const QModelIndex & current, const QModelIndex & previous )
309 {
310   //Setting the selection mode of the selected item from the treeview
311   HEXABLOCKGUI::currentDocGView->setSelectionMode( current );
312   HEXABLOCKGUI::currentOccGView->setSelectionMode( current );
313 }
314
315 void PatternDataSelectionModel::highlightTreeItems(QModelIndexList& indexes,
316                                                    Qt::GlobalColor bgColor,
317                                                    Qt::GlobalColor fgColor, bool only)
318 {
319     if (indexes.count() == 0)
320         return;
321
322     if (only)
323         unhighlightTreeItems();
324
325     // * highlight item
326     QAbstractItemModel* theModel = (QAbstractItemModel*) model();
327     foreach( const QModelIndex& anItemIndex, indexes )
328     {
329         if (anItemIndex.isValid())
330         {
331             theModel->setData(anItemIndex, fgColor, Qt::ForegroundRole);
332             theModel->setData(anItemIndex, bgColor, Qt::BackgroundRole);
333             currentHighlightedItems << anItemIndex;
334         }
335     }
336
337     // * scroll to the first highlighted item in the tree
338     HEXABLOCKGUI* module = HEXABLOCKGUI::getInstance();
339     if (module != NULL && indexes[0].isValid())
340         module->getPatternDataTreeView()->scrollTo(indexes[0]);
341 }
342
343 void PatternDataSelectionModel::unhighlightTreeItems(bool clearSelected)
344 {
345     if (clearSelected)
346         clearSelection();
347     if (currentHighlightedItems.count() == 0)
348         return;
349
350     QAbstractItemModel* theModel = (QAbstractItemModel*) model();
351     foreach( const QModelIndex& anItemIndex, currentHighlightedItems)
352     {
353         if (anItemIndex.isValid())
354         {
355             theModel->setData(anItemIndex, Qt::darkGreen, Qt::ForegroundRole);
356             theModel->setData(anItemIndex, Qt::white, Qt::BackgroundRole);
357         }
358     }
359     currentHighlightedItems.clear();
360 }
361
362 QModelIndexList PatternDataSelectionModel::getGeomAssociations(const QModelIndex& dataIndex)
363 {
364     QModelIndexList geomIndexList;
365     DocumentModel*               docModel = getDocModel();
366     PatternGeomSelectionModel*   pGSModel = HEXABLOCKGUI::currentDocGView->getPatternGeomSelectionModel();
367     if (docModel == NULL || pGSModel == NULL)
368         return geomIndexList;
369
370     const QSortFilterProxyModel *pModel   = dynamic_cast<const QSortFilterProxyModel *>( model() );
371     HEXA_NS::Vertex* vertex = docModel->getHexaPtr<HEXA_NS::Vertex*>(dataIndex);
372     HEXA_NS::Edge*   edge   = docModel->getHexaPtr<HEXA_NS::Edge*>(dataIndex);
373     HEXA_NS::Quad*   quad   = docModel->getHexaPtr<HEXA_NS::Quad*>(dataIndex);
374     QModelIndex index = pModel->mapToSource(dataIndex);
375
376     if (vertex != NULL)
377         geomIndexList << docModel->getVertexAssociation( index );
378     else if (edge != NULL)
379         geomIndexList = docModel->getEdgeAssociations( index );
380     else if ( quad != NULL)
381         geomIndexList = docModel->getQuadAssociations( index );
382
383     return geomIndexList;
384 }
385
386 void PatternDataSelectionModel::onSelectionChanged( const QItemSelection & selected, const QItemSelection & deselected )
387 {
388     QModelIndexList indexes = selected.indexes();
389     int nbSelected = indexes.count();
390     if (nbSelected == 0)
391         return;
392
393     // ** unhighlight current highlighted items in the trees
394     PatternGeomSelectionModel* pgsm = HEXABLOCKGUI::currentDocGView->getPatternGeomSelectionModel();
395     if (pgsm == NULL)
396         return;
397     unhighlightTreeItems(false);
398
399     QModelIndexList associatedGeomIndexes = getGeomAssociations(indexes[0]); //Mono Selection
400     if (associatedGeomIndexes.count() == 0)
401         pgsm->unhighlightTreeItems();
402
403     // ** highlight association in geometry tree
404     pgsm->highlightTreeItems(associatedGeomIndexes);
405
406     // ** highlight association in the vtk view
407     HEXABLOCKGUI::currentDocGView->highlight(indexes);
408
409     // ** highlight association in the occ view
410     HEXABLOCKGUI::currentOccGView->highlight(indexes);
411
412     if (!infoMode || nbSelected > 1)
413         return;
414
415     // ** Show informations of the selected element
416     QModelIndex elt = indexes[0];
417     showEltInfo(elt);
418 }
419
420 void PatternDataSelectionModel::geomSelectionChanged( const Handle(SALOME_InteractiveObject)& anIObject )
421 {
422 //   if (HEXABLOCKGUI::assocInProgress) return;
423
424    PatternGeomSelectionModel*  pgsm = HEXABLOCKGUI::currentDocGView->getPatternGeomSelectionModel();
425    if (pgsm == NULL) return;
426
427    QModelIndex geomModelIndex = pgsm->getModelIndex(anIObject);
428    if (!geomModelIndex.isValid())
429        return;
430    pgsm->select( geomModelIndex, QItemSelectionModel::SelectCurrent );
431 }
432
433
434 void PatternDataSelectionModel::vtkSelectionChanged( const Handle(SALOME_InteractiveObject)& anIObject )
435 {
436     QModelIndexList selectedIndexes =  getSelectionFromModel(anIObject);
437     if (selectedIndexes.count() == 0)
438     {
439         clearSelection();
440         return;
441     }
442     QModelIndex anIOIndex = selectedIndexes[0]; //Monoselection
443
444     //Temporary Debug for hexa selection ------------------------------
445     if (HEXABLOCKGUI::currentDocGView->getSelectionMode() == HEXA_TREE)
446     {
447         DocumentModel* docModel = getDocModel();
448         if ( docModel != NULL && anIOIndex.isValid())
449         {
450             //get the selected quad
451             HEXA_NS::Quad* quad = docModel->getHexaPtr<HEXA_NS::Quad*>(anIOIndex);
452
453             if (quad != NULL)
454             {
455                 //get the hexa the quad belongs to
456                 HEXA_NS::Hexa* hexa = docModel->getQuadHexa(quad);
457                 if (hexa != NULL) //convert the hexa to a QModelIndex so we can select it in the model
458                     anIOIndex = indexBy( HEXA_DATA_ROLE, QVariant::fromValue(hexa));
459                 else
460                 {
461                     SUIT_MessageBox::critical( 0,
462                             tr("HexaBlock"),
463                             tr("The Hexahedron this quad belongs to has been deleted!"));
464                     return;
465                 }
466             }
467         }
468     }//end if HEXA_TREE-----------------------------------------------
469
470     //select the element in the model
471     select( anIOIndex, QItemSelectionModel::SelectCurrent );
472 }
473
474 // //===========================================================================
475 //                              PatternGeomSelectionModel
476 // //===========================================================================
477
478 PatternGeomSelectionModel::PatternGeomSelectionModel( QAbstractItemModel * model ):
479 SelectionModel( model )
480 {
481   connect( this, SIGNAL( currentChanged( const QModelIndex &, const QModelIndex & ) ),
482            this, SLOT( onCurrentChanged( const QModelIndex & , const QModelIndex & ) ) );
483   connect( this, SIGNAL( selectionChanged( const QItemSelection & , const QItemSelection & ) ),
484            this, SLOT( onSelectionChanged( const QItemSelection & , const QItemSelection & ) ), Qt::UniqueConnection );
485 }
486
487 PatternGeomSelectionModel::~PatternGeomSelectionModel()
488 {
489   disconnect( this, SIGNAL( currentChanged( const QModelIndex &, const QModelIndex & ) ),
490           this, SLOT( onCurrentChanged( const QModelIndex & , const QModelIndex & ) ) );
491   disconnect( this, SIGNAL( selectionChanged( const QItemSelection & , const QItemSelection & ) ),
492           this, SLOT( onSelectionChanged( const QItemSelection & , const QItemSelection & ) ) );
493 }
494
495 void PatternGeomSelectionModel::onCurrentChanged( const QModelIndex & current, const QModelIndex & previous )
496 {
497   //Setting the selection mode of the selected item from the treeview}
498   HEXABLOCKGUI::currentDocGView->setSelectionMode( current );
499   HEXABLOCKGUI::currentOccGView->setSelectionMode( current );
500 }
501
502 void PatternGeomSelectionModel::onSelectionChanged( const QItemSelection & selected, const QItemSelection & deselected )
503 {
504     DocumentModel* docModel = NULL;
505     const QSortFilterProxyModel *pModel = dynamic_cast<const QSortFilterProxyModel *>( model() );
506     if ( pModel != NULL)
507         docModel = dynamic_cast<DocumentModel*>(pModel->sourceModel());
508
509     QModelIndexList indexes = selected.indexes();
510     if (indexes.count() == 0 || docModel == NULL)
511         return;
512
513     // ** unhighlight current highlighted items in the trees
514     PatternDataSelectionModel* pdsm = HEXABLOCKGUI::currentDocGView->getPatternDataSelectionModel();
515     if (pdsm == NULL)
516         return;
517     unhighlightTreeItems(false);
518
519     //find the corresponding element in the model
520     HEXA_NS::SubShape* eltSubShape = dynamic_cast<HEXA_NS::SubShape*>(docModel->getHexaPtr(indexes[0]));
521     if (eltSubShape == NULL) return;
522
523     HEXA_NS::NewShape* subShapeParent = eltSubShape->getParentShape();
524     if (subShapeParent == NULL) return;
525
526     QString parentShapeName = subShapeParent->getName();
527     QString subId = QString::number(eltSubShape->getIdent());
528     QString aGeomObjModelEntry = parentShapeName + "," + subId + ";";
529
530     QModelIndexList assocsIndexes = pdsm->indexListOf(aGeomObjModelEntry, HEXA_ASSOC_ENTRY_ROLE);
531     if (assocsIndexes.count() == 0)
532         pdsm->unhighlightTreeItems();
533
534     // ** highlight associations in the data tree
535     pdsm->highlightTreeItems(assocsIndexes);
536
537     // ** highlight associations in the vtk view
538     HEXABLOCKGUI::currentDocGView->highlight(assocsIndexes);
539
540     // ** highlight the selected element in the occ view
541     QMultiMap<QString, int>  entrySubIDs;
542     entrySubIDs.insert(parentShapeName, eltSubShape->getIdent());
543     HEXABLOCKGUI::currentOccGView->highlight(entrySubIDs);
544 }
545
546 QModelIndex PatternGeomSelectionModel::getModelIndex(const Handle(SALOME_InteractiveObject)& anIObject)
547 {
548     QModelIndex result;
549     DocumentModel               *docModel = NULL;
550     const QSortFilterProxyModel *pModel   = dynamic_cast<const QSortFilterProxyModel *>( model() );
551     if ( pModel != NULL)
552         docModel = dynamic_cast<DocumentModel*>( pModel->sourceModel());
553
554     if ( docModel == NULL || HEXABLOCKGUI::selectionMgr() == NULL ||
555             HEXABLOCKGUI::currentOccGView->getViewWindow() == NULL)
556         return result;
557
558     //extract associated elements in the model
559     TColStd_IndexedMapOfInteger anIndexes;
560     HEXABLOCKGUI::selectionMgr()->GetIndexes(anIObject, anIndexes);
561     if (anIndexes.Extent() == 0) return result;
562     QString aGeomObjStudyEntry = anIObject->getEntry();
563     QString aGeomObjModelEntry = docModel->getGeomObjName(aGeomObjStudyEntry) + "," + QString::number(anIndexes(1));
564
565     HEXA_NS::SubShape*    ssh =  docModel->getGeomPtr(aGeomObjModelEntry);
566     HEXA_NS::VertexShape* vSh =  dynamic_cast<HEXA_NS::VertexShape*>(ssh);
567     HEXA_NS::EdgeShape*   eSh =  dynamic_cast<HEXA_NS::EdgeShape*>(ssh);
568     HEXA_NS::FaceShape*   fSh =  dynamic_cast<HEXA_NS::FaceShape*>(ssh);
569
570     if (vSh != NULL)
571         result = indexBy( HEXA_DATA_ROLE, QVariant::fromValue(vSh));
572     else if (eSh != NULL)
573         result = indexBy( HEXA_DATA_ROLE, QVariant::fromValue(eSh));
574     else if (fSh != NULL)
575         result = indexBy( HEXA_DATA_ROLE, QVariant::fromValue(fSh));
576
577     return result;
578 }
579
580 void PatternGeomSelectionModel::highlightTreeItems(QModelIndexList& indexes,
581                                                    Qt::GlobalColor bgColor,
582                                                    Qt::GlobalColor fgColor, bool only)
583 {
584     if (indexes.count() == 0)
585         return;
586
587     if (only)
588         unhighlightTreeItems();
589
590     // * highlight items
591     QAbstractItemModel* theModel = (QAbstractItemModel*) model();
592     foreach( const QModelIndex& anItemIndex, indexes )
593     {
594         if (anItemIndex.isValid())
595         {
596             theModel->setData(anItemIndex, fgColor, Qt::ForegroundRole);
597             theModel->setData(anItemIndex, bgColor, Qt::BackgroundRole);
598             currentHighlightedItems << anItemIndex;
599         }
600     }
601
602     // * scroll to the first highlighted item in the tree
603     HEXABLOCKGUI* module = HEXABLOCKGUI::getInstance();
604     if (module != NULL && indexes[0].isValid())
605         module->getPatternGeomTreeView()->scrollTo(indexes[0]);
606 }
607
608 void PatternGeomSelectionModel::unhighlightTreeItems(bool clearSelected)
609 {
610     if (clearSelected)
611         clearSelection();
612     if (currentHighlightedItems.count() == 0)
613         return;
614     QAbstractItemModel* theModel = (QAbstractItemModel*) model();
615     foreach( const QModelIndex& anItemIndex, currentHighlightedItems)
616     {
617         if (anItemIndex.isValid())
618         {
619             theModel->setData(anItemIndex, Qt::darkGreen, Qt::ForegroundRole);
620             theModel->setData(anItemIndex, Qt::white, Qt::BackgroundRole);
621         }
622     }
623     currentHighlightedItems.clear();
624 }
625
626 // //===========================================================================
627 //                              GroupsSelectionModel
628 // //===========================================================================
629 GroupsSelectionModel::GroupsSelectionModel( QAbstractItemModel * model ):
630 SelectionModel( model )
631 {
632   connect( this, SIGNAL( currentChanged( const QModelIndex &, const QModelIndex & ) ),
633            this, SLOT( onCurrentChanged( const QModelIndex & , const QModelIndex & ) ), Qt::UniqueConnection );
634   connect( this, SIGNAL( selectionChanged( const QItemSelection & , const QItemSelection & ) ),
635            this, SLOT( onSelectionChanged( const QItemSelection & , const QItemSelection & ) ), Qt::UniqueConnection );
636 }
637
638 GroupsSelectionModel::~GroupsSelectionModel()
639 {
640   disconnect( this, SIGNAL( currentChanged( const QModelIndex &, const QModelIndex & ) ),
641           this, SLOT( onCurrentChanged( const QModelIndex & , const QModelIndex & ) ) );
642   disconnect( this, SIGNAL( selectionChanged( const QItemSelection & , const QItemSelection & ) ),
643           this, SLOT( onSelectionChanged( const QItemSelection & , const QItemSelection & ) ) );
644 }
645
646 void GroupsSelectionModel::onSelectionChanged( const QItemSelection & selected, const QItemSelection & deselected )
647 {
648     QModelIndexList indexes = selected.indexes();
649     int nbSelected = indexes.count();
650     if (nbSelected == 0)
651         return;
652
653     // ** Highlight the group in the vtk view **/
654     try {
655         for( QModelIndexList::const_iterator i_index = indexes.begin(); i_index != indexes.end(); ++i_index ){
656             HEXABLOCKGUI::currentDocGView->highlightGroups( *i_index );
657         }
658     } catch ( ... ) {
659         MESSAGE("Unknown exception was cought !!!");
660     }
661
662     if (!infoMode || nbSelected > 1)
663         return;
664
665     // ** Show informations of the selected element **/
666     QModelIndex elt = indexes[0];
667     showEltInfo(elt);
668 }
669
670 // //===========================================================================
671 //                              MeshSelectionModel
672 // //===========================================================================
673 MeshSelectionModel::MeshSelectionModel( QAbstractItemModel * model ):
674         SelectionModel( model )
675 {
676     connect( this, SIGNAL( currentChanged( const QModelIndex &, const QModelIndex & ) ),
677             this, SLOT( onCurrentChanged( const QModelIndex & , const QModelIndex & ) ), Qt::UniqueConnection );
678     connect( this, SIGNAL( selectionChanged( const QItemSelection & , const QItemSelection & ) ),
679             this, SLOT( onSelectionChanged( const QItemSelection & , const QItemSelection & ) ), Qt::UniqueConnection );
680 }
681
682 MeshSelectionModel::~MeshSelectionModel()
683 {
684     disconnect( this, SIGNAL( currentChanged( const QModelIndex &, const QModelIndex & ) ),
685             this, SLOT( onCurrentChanged( const QModelIndex & , const QModelIndex & ) ) );
686     disconnect( this, SIGNAL( selectionChanged( const QItemSelection & , const QItemSelection & ) ),
687             this, SLOT( onSelectionChanged( const QItemSelection & , const QItemSelection & ) ) );
688 }
689
690
691 void MeshSelectionModel::onSelectionChanged( const QItemSelection & selected, const QItemSelection & deselected )
692 {
693     QModelIndexList indexes = selected.indexes();
694     int nbSelected = indexes.count();
695     if (nbSelected == 0)
696         return;
697
698     try {
699         for( QModelIndexList::const_iterator i_index = indexes.begin(); i_index != indexes.end(); ++i_index ){
700             HEXABLOCKGUI::currentDocGView->highlightPropagation( *i_index );
701         }
702     } catch ( ... ) {
703         MESSAGE("Unknown exception was cought !!!");
704     }
705
706     if (!infoMode || nbSelected > 1)
707         return;
708
709     // ** Show informations of the selected element **/
710     QModelIndex elt = indexes[0];
711     showEltInfo(elt);
712 }
713