Salome HOME
Merge from V6_main 01/04/2013
[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_DocumentModel.hxx"
51 #include "HEXABLOCKGUI_DocumentItem.hxx"
52 #include "HEXABLOCKGUI.hxx"
53
54
55 #include <SVTK_Selector.h>
56 #include <SVTK_ViewModel.h>
57
58
59 #include <SalomeApp_Study.h>
60 #include <SalomeApp_Application.h>
61 #include <SALOME_Actor.h>
62 #include <SALOME_ListIO.hxx>
63 #include <SALOME_ListIteratorOfListIO.hxx>
64
65 #include <QMultiMap>
66
67 #include "GEOMBase.h"
68 #include "MyGEOMBase_Helper.hxx"
69
70 #include <OCCViewer_ViewModel.h>
71 #include <SVTK_ViewModel.h>
72 #include <SalomeApp_Study.h>
73 #include <SalomeApp_Application.h>
74 #include <LightApp_SelectionMgr.h>
75 #include <SALOME_ListIteratorOfListIO.hxx>
76 #include <TopExp.hxx>
77 #include <TopExp_Explorer.hxx>
78 #include <TopoDS_Iterator.hxx>
79 #include <TopTools_MapOfShape.hxx>
80 #include <TopTools_IndexedMapOfShape.hxx>
81 #include <TColStd_IndexedMapOfInteger.hxx>
82
83
84 //#define _DEVDEBUG_
85 using namespace std;
86 using namespace HEXABLOCK::GUI;
87
88
89 // //===========================================================================
90 //                              SelectionModel
91 // //===========================================================================
92
93 SelectionModel::SelectionModel( QAbstractItemModel * model ):
94 QItemSelectionModel( model ),
95 //_theModelSelectionChanged(false),
96 //_theVtkSelectionChanged(false),
97 //_theGeomSelectionChanged(false),
98 ignoreSignal(false),
99 salomeNothingSelected(true)
100 {
101 }
102
103
104 SelectionModel::~SelectionModel()
105 {
106   disconnect( HEXABLOCKGUI::selectionMgr(), SIGNAL( currentSelectionChanged() ), this, SLOT( salomeSelectionChanged() ) );
107 }
108
109
110 QModelIndex SelectionModel::indexBy( int role, const QString& value )
111 {
112   QModelIndex eltIndex; // element (vertex, edge, quad) of model
113   const QAbstractItemModel* theModel = model();
114   if ( !theModel ) return eltIndex;
115   QModelIndexList theIndexes = theModel->match( theModel->index(0, 0),
116                                           role,
117                                           value,
118                                           1,
119                                           Qt::MatchRecursive | Qt::MatchContains );//Qt::MatchFixedString );
120   if ( theIndexes.count()>0 )
121     eltIndex = theIndexes[0] ;
122   return eltIndex;
123 }
124
125
126 QModelIndex SelectionModel::indexBy( int role, const QVariant& var )
127 {
128   QModelIndex eltIndex; // element (vertex, edge, quad) of model
129   const QAbstractItemModel* theModel = model();
130   if ( !theModel ) return eltIndex;
131   QModelIndexList theIndexes = theModel->match( theModel->index(0, 0),
132                                           role,
133                                           var,
134                                           1,
135                                           Qt::MatchRecursive /*| Qt::MatchContains*/ );//Qt::MatchFixedString );
136   if ( theIndexes.count()>0 )
137     eltIndex = theIndexes[0] ;
138   return eltIndex;
139 }
140
141 QModelIndex SelectionModel::indexOf( const QString& anEntry, int role )
142 {
143   QModelIndex eltIndex; // element (vertex, edge, quad) of model
144   const QAbstractItemModel* theModel = model();
145   if ( !theModel ) return eltIndex;
146
147   QModelIndexList theIndexes = theModel->match( theModel->index(0, 0),
148                                           role,
149                                           anEntry,
150                                           1,
151                                           Qt::MatchRecursive | Qt::MatchContains );//Qt::MatchFixedString );
152   if ( theIndexes.count()>0 )
153     eltIndex = theIndexes[0] ;
154   return eltIndex;
155 }
156
157
158 QModelIndexList SelectionModel::indexListOf( const QString& anEntry, int role )
159 {
160   QModelIndexList theIndexes; // element (vertex, edge, quad) of model
161   const QAbstractItemModel* theModel = model();
162   if ( !theModel ) return theIndexes;
163   theIndexes = theModel->match( theModel->index(0, 0),
164                                           role,
165                                           anEntry,
166                                           -1,
167                                           Qt::MatchRecursive | Qt::MatchContains );//Qt::MatchFixedString );
168   return theIndexes;
169 }
170
171
172
173 void SelectionModel::salomeSelectionChanged()
174 {
175     if (HEXABLOCKGUI::assocInProgress &&
176             HEXABLOCKGUI::getActiveViewType() == HEXABLOCKGUI::OCC) return;
177
178   try {
179
180     if ( HEXABLOCKGUI::selectionMgr() == NULL ) return;
181     SALOME_ListIO salomeSelected;
182 //     _salomeSelectionMgr->selectedObjects( salomeSelected, SVTK_Viewer::Type() );//salomeSelected.Extent()
183     if (HEXABLOCKGUI::getActiveViewType() == HEXABLOCKGUI::VTK)
184     {
185         HEXABLOCKGUI::selectionMgr()->selectedObjects( salomeSelected, SVTK_Viewer::Type());
186     }
187     else if (HEXABLOCKGUI::getActiveViewType() == HEXABLOCKGUI::OCC)
188     {
189         HEXABLOCKGUI::selectionMgr()->selectedObjects( salomeSelected, "", false);
190     }
191
192 //    HEXABLOCKGUI::selectionMgr()->selectedObjects( salomeSelected, NULL, false );
193     if ( salomeSelected.IsEmpty() ){
194       salomeNothingSelected = true;
195       clearSelection();
196       return;
197     }
198
199     Handle(SALOME_InteractiveObject) anIObject;
200     SALOME_ListIteratorOfListIO it(salomeSelected);
201
202     //Handle selection according to the source
203     if (HEXABLOCKGUI::getActiveViewType() == HEXABLOCKGUI::VTK)
204     {
205         for( ; it.More(); it.Next()){
206             anIObject = it.Value(); //anIObject->getName()
207             vtkSelectionChanged( anIObject ); // or VTK?...
208         }
209     }
210     else if (HEXABLOCKGUI::getActiveViewType() == HEXABLOCKGUI::OCC)
211     {
212         for( ; it.More(); it.Next()){
213             anIObject = it.Value(); //anIObject->getName()
214             geomSelectionChanged( anIObject );// or VTK?...
215         }
216     }
217
218     salomeNothingSelected = false;
219   } catch ( ... ) {
220       MESSAGE("*  Unknown exception was cought !!!");
221   }
222 }
223
224
225 QModelIndexList SelectionModel::getSelectionFromModel(const Handle(SALOME_InteractiveObject)& anIObject)
226 {
227    QModelIndexList selectedIndexes;
228
229    //verify if the IOBject is valid and from VTK selection
230    bool fromVTK  = ( strcmp("HEXABLOCK", anIObject->getComponentDataType()) == 0 );
231    if ( !fromVTK || !anIObject->hasEntry() || HEXABLOCKGUI::currentDocGView->getViewWindow() == NULL)
232       return selectedIndexes;
233
234    QString anIOEntry = anIObject->getEntry();
235    int anhexaElemsId;
236    QString aText = "";
237
238    //extract vtk selection from the model -----------
239    if ( GetNameOfSelectedElements( HEXABLOCKGUI::currentDocGView->getViewWindow(), anIObject, aText ) <= 0 )
240       return selectedIndexes;
241
242    Document_Actor* docActor = dynamic_cast<Document_Actor*>( findActorByEntry( HEXABLOCKGUI::currentDocGView->getViewWindow(),
243                                                                                        anIOEntry.toLatin1() ) );
244    if ( !docActor ) return selectedIndexes;
245    QStringList idList = aText.split(" ");
246    foreach( const QString& id, idList )
247    {
248       if (!id.isEmpty())
249          {
250             //find selection in the model
251             anhexaElemsId = docActor->hexaElemsId[ id.toInt() ];
252             anIOEntry = QString::number( anhexaElemsId );
253
254             selectedIndexes << indexOf( anIOEntry, HEXA_ENTRY_ROLE );
255          }
256    }
257    return selectedIndexes;
258 }
259
260 //Returns the element of the model which is associated to the Interactive object
261 QModelIndexList SelectionModel::getSelectionAssociactions(const Handle(SALOME_InteractiveObject)& anIObject)
262 {
263    QModelIndexList assocsIndexes;
264    DocumentModel               *docModel = NULL;
265    const QSortFilterProxyModel *pModel   = dynamic_cast<const QSortFilterProxyModel *>( model() );
266    if ( pModel != NULL)
267        docModel = dynamic_cast<DocumentModel*>( pModel->sourceModel() );
268
269    if ( docModel == NULL) return assocsIndexes;
270
271    //verify if the selection is really from OCC View
272    bool fromGEOM = ( strcmp("GEOM", anIObject->getComponentDataType()) == 0 );
273    if ( !fromGEOM || HEXABLOCKGUI::selectionMgr() == NULL ||
274          HEXABLOCKGUI::currentOccGView->getViewWindow() == NULL)
275       return assocsIndexes;
276
277 //   QString aName;
278    GEOM::GEOM_Object_var aGeomObj = GEOMBase::ConvertIOinGEOMObject( anIObject );
279
280    //extract associated elements in the model
281    if ( GEOMBase::IsShape(aGeomObj) ){
282 //         aName = GEOMBase::GetName(aGeomObj);
283          TColStd_IndexedMapOfInteger anIndexes;
284          HEXABLOCKGUI::selectionMgr()->GetIndexes(anIObject, anIndexes);
285          if (anIndexes.Extent() == 0) return assocsIndexes;
286          QString aGeomObjStudyEntry = aGeomObj->GetStudyEntry();
287
288          for( int i = 1; i <= anIndexes.Extent(); ++i)
289             {
290                QString aGeomObjModelEntry = docModel->getGeomObjName(aGeomObjStudyEntry) + "," + QString::number(anIndexes(i)) + ";";
291                assocsIndexes.append(indexListOf(aGeomObjModelEntry, HEXA_ASSOC_ENTRY_ROLE));
292                if (assocsIndexes.count() > 0)
293                   return assocsIndexes;    //Premature: On purpose for Monoselection
294             }
295    }
296
297    return assocsIndexes;
298 }
299
300
301 // //===========================================================================
302 //                              PatternDataSelectionModel
303 // //===========================================================================
304
305 PatternDataSelectionModel::PatternDataSelectionModel( QAbstractItemModel * model ):
306 SelectionModel( model )
307 {
308   connect( this, SIGNAL( currentChanged( const QModelIndex &, const QModelIndex & ) ),
309            this, SLOT( onCurrentChanged( const QModelIndex & , const QModelIndex & ) ), Qt::UniqueConnection );
310   connect( this, SIGNAL( selectionChanged( const QItemSelection & , const QItemSelection & ) ),
311            this, SLOT( onSelectionChanged( const QItemSelection & , const QItemSelection & ) ), Qt::UniqueConnection );
312
313 }
314
315 PatternDataSelectionModel::~PatternDataSelectionModel()
316 {
317   disconnect( this, SIGNAL( currentChanged( const QModelIndex &, const QModelIndex & ) ),
318           this, SLOT( onCurrentChanged( const QModelIndex & , const QModelIndex & ) ) );
319   disconnect( this, SIGNAL( selectionChanged( const QItemSelection & , const QItemSelection & ) ),
320           this, SLOT( onSelectionChanged( const QItemSelection & , const QItemSelection & ) ) );
321 }
322
323
324 void PatternDataSelectionModel::onCurrentChanged( const QModelIndex & current, const QModelIndex & previous )
325 {
326   //Setting the selection mode of the selected item from the treeview
327   HEXABLOCKGUI::currentDocGView->setSelectionMode( current );
328   HEXABLOCKGUI::currentOccGView->setSelectionMode( current );
329 }
330
331 void PatternDataSelectionModel::highlightEltsWithAssocs(const QModelIndexList& elts)
332 {
333         try {
334             /*if ( !_theVtkSelectionChanged )*/
335             HEXABLOCKGUI::currentDocGView->highlight(elts);
336             /*if ( !_theGeomSelectionChanged )*/
337             HEXABLOCKGUI::currentOccGView->highlight(elts);
338         } catch ( ... ) {
339                 MESSAGE("Unknown exception was cought !!!");
340         }
341 }
342
343 QModelIndexList PatternDataSelectionModel::getGeomAssociations(const QModelIndex& dataIndex)
344 {
345     QModelIndexList geomIndexList;
346     HEXA_NS::SubShape* ssh;
347     HEXA_NS::EdgeShape* sh;
348     if (HEXABLOCKGUI::currentDocGView == NULL) return geomIndexList;
349
350     DocumentModel               *docModel = NULL;
351     const QSortFilterProxyModel *pModel   = dynamic_cast<const QSortFilterProxyModel *>( model() );
352     PatternGeomModel* pGModel = HEXABLOCKGUI::currentDocGView->getPatternGeomModel();
353     PatternGeomSelectionModel* pGSModel = HEXABLOCKGUI::currentDocGView->getPatternGeomSelectionModel();
354
355     if ( pModel != NULL)
356         docModel = dynamic_cast<DocumentModel*>( pModel->sourceModel() );
357
358     if (docModel == NULL || pGModel == NULL || pGSModel == NULL)
359         return geomIndexList;
360
361     HEXA_NS::Vertex* vertex = docModel->getHexaPtr<HEXA_NS::Vertex*>(dataIndex);
362     HEXA_NS::Edge*   edge   = docModel->getHexaPtr<HEXA_NS::Edge*>(dataIndex);
363     HEXA_NS::Quad*   quad   = docModel->getHexaPtr<HEXA_NS::Quad*>(dataIndex);
364     QModelIndex index = pModel->mapToSource(dataIndex);
365
366     if (vertex != NULL)
367         geomIndexList << docModel->getVertexAssociation( index/*dataIndex*/ );
368     else if (edge != NULL)
369     {
370         QList<DocumentModel::GeomObj> assocs = docModel->getEdgeAssociations( index/*dataIndex*/ );
371         foreach( const DocumentModel::GeomObj& anAssoc, assocs )
372         {
373             QString id = anAssoc.shapeName+","+anAssoc.subid;
374             ssh = docModel->getGeomPtr(id);
375             sh = dynamic_cast<HEXA_NS::EdgeShape*>(ssh);
376             if (sh != NULL)
377                 geomIndexList << /*pGModel->mapToSource(*/pGSModel->indexBy( HEXA_DATA_ROLE, QVariant::fromValue(sh))/*)*/;
378         }
379     }
380     else if ( quad != NULL)
381         geomIndexList = docModel->getQuadAssociations( index/*dataIndex*/ );
382
383
384     return geomIndexList;
385 }
386
387
388 void PatternDataSelectionModel::onSelectionChanged( const QItemSelection & selected, const QItemSelection & deselected )
389 {
390 //    if (ignoreSignal)
391 //    {
392 //        MESSAGE("PatternDataSelectionModel: ignoring this signal");
393 //        ignoreSignal = false;
394 //        return;
395 //    }
396     //need to highlight the associated geom obj in the geometry tree
397     //first get the association
398     QModelIndexList associatedGeomIndexes;
399     QModelIndexList indexes = selected.indexes();
400     highlightEltsWithAssocs(indexes);
401
402 //    //highlight in geom
403 //    if (HEXABLOCKGUI::currentDocGView == NULL || indexes.count() == 0) return;
404 //    associatedGeomIndexes = getGeomAssociations(indexes[0]); //Mono Selection
405 //    if (associatedGeomIndexes.count() == 0)
406 //    {
407 //        MESSAGE("ON SELECTION CHANGED: no association found: returning...");
408 //        HEXABLOCKGUI::currentDocGView->getPatternGeomSelectionModel()->setIgnoreSignal(false);
409 //        return;
410 //    }
411 //    if (associatedGeomIndexes[0].isValid())
412 //    {
413 ////        MESSAGE("Association found: but not selecting... now returning");
414 ////        return;
415 //        //tell geom selection model to ignore this signal
416 //        HEXABLOCKGUI::currentDocGView->getPatternGeomSelectionModel()->setIgnoreSignal(true);
417 //
418 //        //selection of the associated element in the geometry tree
419 ////        HEXABLOCKGUI::currentDocGView->getPatternGeomSelectionModel()->setCurrentIndex( associatedGeomIndexes[0],
420 ////                                                                                        QItemSelectionModel::Clear );
421 //        const QSortFilterProxyModel *pModel   = dynamic_cast<const QSortFilterProxyModel *>( HEXABLOCKGUI::currentDocGView->getPatternGeomSelectionModel()->model() );
422 //        if (pModel == NULL)
423 //        {
424 //            HEXABLOCKGUI::currentDocGView->getPatternGeomSelectionModel()->setIgnoreSignal(false);
425 //            return;
426 //        }
427 //        MESSAGE("SELECTING THE GEOM TREE INDEX NOW: ");
428 //        MESSAGE(HEXABLOCKGUI::currentDocGView->getDocumentModel()->getHexaPtr(associatedGeomIndexes[0])->getName());
429 //
430 //        HEXABLOCKGUI::currentDocGView->getPatternGeomSelectionModel()->setCurrentIndex( associatedGeomIndexes[0],
431 //                                                                                        QItemSelectionModel::ClearAndSelect );
432 //    }
433 }
434
435
436 void PatternDataSelectionModel::geomSelectionChanged( const Handle(SALOME_InteractiveObject)& anIObject )
437 {
438    if (HEXABLOCKGUI::assocInProgress) return;
439
440 //   QModelIndexList assocsIndexes = getSelectionAssociactions(anIObject);
441    PatternGeomSelectionModel* pgsm = NULL;
442    if (HEXABLOCKGUI::currentDocGView != NULL)
443        pgsm = HEXABLOCKGUI::currentDocGView->getPatternGeomSelectionModel();
444    if (pgsm == NULL) return;
445
446    QModelIndex geomModelIndex = pgsm->getModelIndex(anIObject);
447    clearSelection();
448    if (!geomModelIndex.isValid()) return;
449    pgsm->setCurrentIndex( geomModelIndex, QItemSelectionModel::Clear );  //CS_TEST
450    pgsm->setCurrentIndex( geomModelIndex, QItemSelectionModel::SelectCurrent );
451 }
452
453
454 void PatternDataSelectionModel::vtkSelectionChanged( const Handle(SALOME_InteractiveObject)& anIObject )
455 {
456    QModelIndexList selectedIndexes =  getSelectionFromModel(anIObject);
457    if (selectedIndexes.count() == 0)
458       {
459          clearSelection();
460          return;
461       }
462    //    _theVtkSelectionChanged = true;
463    QModelIndex anIOIndex = selectedIndexes[0]; //Monoselection
464
465    //Temporary Debug for hexa selection -----------------------------
466    if (HEXABLOCKGUI::currentDocGView->getSelectionMode() == HEXA_TREE)
467       {
468          DocumentModel               *docModel = NULL;
469          const QSortFilterProxyModel *pModel   = dynamic_cast<const QSortFilterProxyModel *>( model() );
470
471          if ( pModel != NULL){
472                docModel = dynamic_cast<DocumentModel*>( pModel->sourceModel() );
473                if ( docModel != NULL && anIOIndex.isValid())
474                   {
475                      //get the selected quad
476                      HEXA_NS::Quad* quad = docModel->getHexaPtr<HEXA_NS::Quad*>(anIOIndex);
477
478                      if (quad != NULL)
479                         {
480                            //get the hexa the quad belongs to
481                            HEXA_NS::Hexa* hexa = docModel->getQuadHexa(quad);
482                            if (hexa != NULL) //convert the hexa to a QModelIndex so we can select it in the model
483                               anIOIndex = indexBy( HEXA_DATA_ROLE, QVariant::fromValue(hexa));
484                            else
485                               {
486                                  SUIT_MessageBox::critical( 0,
487                                        tr("HexaBlock"),
488                                        tr("The Hexahedron this quad belongs to has been deleted!"));
489                                  return;
490                               }
491                         }
492                   }
493          }
494       }//end if HEXA_TREE----------------------------------------------
495
496    //select the element in the model
497    setCurrentIndex( anIOIndex, QItemSelectionModel::Clear );
498    setCurrentIndex( anIOIndex, QItemSelectionModel::SelectCurrent );
499    //    _theVtkSelectionChanged = false;
500 }
501
502 // //===========================================================================
503 //                              PatternGeomSelectionModel
504 // //===========================================================================
505
506 PatternGeomSelectionModel::PatternGeomSelectionModel( QAbstractItemModel * model ):
507 SelectionModel( model )
508 {
509   connect( this, SIGNAL( currentChanged( const QModelIndex &, const QModelIndex & ) ),
510            this, SLOT( onCurrentChanged( const QModelIndex & , const QModelIndex & ) ) );
511   connect( this, SIGNAL( selectionChanged( const QItemSelection & , const QItemSelection & ) ),
512            this, SLOT( onSelectionChanged( const QItemSelection & , const QItemSelection & ) ), Qt::UniqueConnection );
513 }
514
515 PatternGeomSelectionModel::~PatternGeomSelectionModel()
516 {
517   disconnect( this, SIGNAL( currentChanged( const QModelIndex &, const QModelIndex & ) ),
518           this, SLOT( onCurrentChanged( const QModelIndex & , const QModelIndex & ) ) );
519   disconnect( this, SIGNAL( selectionChanged( const QItemSelection & , const QItemSelection & ) ),
520           this, SLOT( onSelectionChanged( const QItemSelection & , const QItemSelection & ) ) );
521 }
522
523
524 void PatternGeomSelectionModel::onCurrentChanged( const QModelIndex & current, const QModelIndex & previous )
525 {
526   //Setting the selection mode of the selected item from the treeview}
527   HEXABLOCKGUI::currentDocGView->setSelectionMode( current );
528   HEXABLOCKGUI::currentOccGView->setSelectionMode( current );
529
530 }
531
532 void PatternGeomSelectionModel::onSelectionChanged( const QItemSelection & selected, const QItemSelection & deselected )
533 {
534     //don't handle selection signals coming from the data tree
535 //    if (ignoreSignal)
536 //    {
537 //        MESSAGE("PatternGeomSelectionModel: IGNORING THE SIGNAL:!");
538 //        ignoreSignal = false;
539 //        return;
540 //    }
541
542     DocumentModel               *docModel = NULL;
543     const QSortFilterProxyModel *pModel   = dynamic_cast<const QSortFilterProxyModel *>( model() );
544     if ( pModel != NULL)
545         docModel = dynamic_cast<DocumentModel*>( pModel->sourceModel());
546
547     QModelIndexList indexes = selected.indexes();
548     if (indexes.count() == 0 || docModel == NULL) return;
549
550     //find the corresponding element in the model
551     HEXA_NS::SubShape* eltSubShape = dynamic_cast<HEXA_NS::SubShape*>(docModel->getHexaPtr(indexes[0]));
552     if (eltSubShape == NULL) return;
553
554     HEXA_NS::NewShape* subShapeParent = eltSubShape->getParentShape();
555     if (subShapeParent == NULL) return;
556
557     QString parentShapeName = subShapeParent->getName();
558     QString subId = QString::number(eltSubShape->getIdent());
559     QString aGeomObjModelEntry = parentShapeName + "," + subId + ";";
560
561     //get the model of the data tree
562     if (HEXABLOCKGUI::currentDocGView == NULL || HEXABLOCKGUI::currentDocGView->getPatternDataSelectionModel() == NULL)
563         return;
564
565     QModelIndexList assocsIndexes = HEXABLOCKGUI::currentDocGView->getPatternDataSelectionModel()->indexListOf(aGeomObjModelEntry, HEXA_ASSOC_ENTRY_ROLE);
566
567     //selection of the associated elements
568     if (assocsIndexes.count() == 1 &&  assocsIndexes[0].isValid())
569     {
570 //        HEXABLOCKGUI::currentDocGView->getPatternDataSelectionModel()->setIgnoreSignal(true);
571         //Select associated element in the model => highlight in vtk view
572 //        HEXABLOCKGUI::currentDocGView->getPatternDataSelectionModel()->setCurrentIndex( assocsIndexes[0], QItemSelectionModel::Clear );  //CS_TEST
573         HEXABLOCKGUI::currentDocGView->getPatternDataSelectionModel()->setCurrentIndex( assocsIndexes[0], QItemSelectionModel::ClearAndSelect );
574         HEXABLOCKGUI::currentDocGView->getPatternDataSelectionModel()->highlightEltsWithAssocs(assocsIndexes);
575     }
576     else
577     {
578         /*more than one element associated to this geom object: just highlight in the views,
579           not in the model (because of mono selection in the model).*/
580
581         //hight the selected geom obj in the occ view
582         if (HEXABLOCKGUI::currentOccGView != NULL)
583         {
584             QMultiMap<QString, int>  entrySubIDs;
585             entrySubIDs.insert(parentShapeName, eltSubShape->getIdent());
586
587             HEXABLOCKGUI::currentOccGView->highlight(entrySubIDs);
588         }
589         //...and its associated elements in the vtk view
590         HEXABLOCKGUI::currentDocGView->highlight(assocsIndexes);
591     }
592 }
593
594 QModelIndex PatternGeomSelectionModel::getModelIndex(const Handle(SALOME_InteractiveObject)& anIObject)
595 {
596     QModelIndex result;
597     DocumentModel               *docModel = NULL;
598     const QSortFilterProxyModel *pModel   = dynamic_cast<const QSortFilterProxyModel *>( model() );
599     if ( pModel != NULL)
600         docModel = dynamic_cast<DocumentModel*>( pModel->sourceModel());
601
602     if ( docModel == NULL) return result;
603
604     //verify if the selection is really from OCC View
605     bool fromGEOM = ( strcmp("GEOM", anIObject->getComponentDataType()) == 0 );
606     if ( !fromGEOM || HEXABLOCKGUI::selectionMgr() == NULL ||
607             HEXABLOCKGUI::currentOccGView->getViewWindow() == NULL)
608         return result;
609
610     //   QString aName;
611     GEOM::GEOM_Object_var aGeomObj = GEOMBase::ConvertIOinGEOMObject( anIObject );
612
613     //extract associated elements in the model
614     if ( GEOMBase::IsShape(aGeomObj) ){
615         //         aName = GEOMBase::GetName(aGeomObj);
616         TColStd_IndexedMapOfInteger anIndexes;
617         HEXABLOCKGUI::selectionMgr()->GetIndexes(anIObject, anIndexes);
618         if (anIndexes.Extent() == 0) return result;
619         QString aGeomObjStudyEntry = aGeomObj->GetStudyEntry();
620         QString aGeomObjModelEntry = docModel->getGeomObjName(aGeomObjStudyEntry) + "," + QString::number(anIndexes(1));
621
622         HEXA_NS::SubShape* ssh = docModel->getGeomPtr(aGeomObjModelEntry);
623         if (ssh != NULL)
624         {
625             MESSAGE("Tranna get: " << aGeomObjModelEntry.toStdString() <<  ", " << ssh->getName());
626         }
627         else
628         {
629             MESSAGE("YOUR SHAPE IS NULL");
630         }
631         MESSAGE("Tranna get: " << aGeomObjModelEntry.toStdString());
632         HEXA_NS::VertexShape* vSh =  dynamic_cast<HEXA_NS::VertexShape*>(ssh);
633         HEXA_NS::EdgeShape* eSh =  dynamic_cast<HEXA_NS::EdgeShape*>(ssh);
634         HEXA_NS::FaceShape* fSh =  dynamic_cast<HEXA_NS::FaceShape*>(ssh);
635
636         if (vSh != NULL)
637             result = indexBy( HEXA_DATA_ROLE, QVariant::fromValue(vSh));
638         else if (eSh != NULL)
639         {
640             MESSAGE("=====> An EDGE: "<< eSh->getName());
641             result = indexBy( HEXA_DATA_ROLE, QVariant::fromValue(eSh));
642         }
643         else if (fSh != NULL)
644             result = indexBy( HEXA_DATA_ROLE, QVariant::fromValue(fSh));
645     }
646     return result;
647 }
648
649 // //===========================================================================
650 //                              GroupsSelectionModel
651 // //===========================================================================
652
653 GroupsSelectionModel::GroupsSelectionModel( QAbstractItemModel * model ):
654 SelectionModel( model )
655 {
656   connect( this, SIGNAL( currentChanged( const QModelIndex &, const QModelIndex & ) ),
657            this, SLOT( onCurrentChanged( const QModelIndex & , const QModelIndex & ) ), Qt::UniqueConnection );
658   connect( this, SIGNAL( selectionChanged( const QItemSelection & , const QItemSelection & ) ),
659            this, SLOT( onSelectionChanged( const QItemSelection & , const QItemSelection & ) ), Qt::UniqueConnection );
660 }
661
662 GroupsSelectionModel::~GroupsSelectionModel()
663 {
664   disconnect( this, SIGNAL( currentChanged( const QModelIndex &, const QModelIndex & ) ),
665           this, SLOT( onCurrentChanged( const QModelIndex & , const QModelIndex & ) ) );
666   disconnect( this, SIGNAL( selectionChanged( const QItemSelection & , const QItemSelection & ) ),
667           this, SLOT( onSelectionChanged( const QItemSelection & , const QItemSelection & ) ) );
668 }
669
670
671 void GroupsSelectionModel::onSelectionChanged( const QItemSelection & selected, const QItemSelection & deselected )
672 {
673   try {
674     //     if ( _salomeSelectionMgr == NULL ) return;
675     //     _salomeSelectionMgr->clearSelected();
676     //   erasePreview(true);
677     QModelIndexList indexes = selected.indexes();
678     for( QModelIndexList::const_iterator i_index = indexes.begin(); i_index != indexes.end(); ++i_index ){
679       HEXABLOCKGUI::currentDocGView->highlightGroups( *i_index );
680     }
681
682     // CS_BP todo SALOMEGUI_Swig.cxx:370
683     //   indexes = deselected.indexes();
684     //   for( QModelIndexList::const_iterator i_index = indexes.begin(); i_index != indexes.end(); ++i_index )
685     //     _unselectSalome( *i_index);
686   } catch ( ... ) {
687     MESSAGE("Unknown exception was cought !!!");
688   }
689
690 }
691
692
693 // //===========================================================================
694 //                              MeshSelectionModel
695 // //===========================================================================
696
697
698 MeshSelectionModel::MeshSelectionModel( QAbstractItemModel * model ):
699 SelectionModel( model )
700 {
701   connect( this, SIGNAL( currentChanged( const QModelIndex &, const QModelIndex & ) ),
702            this, SLOT( onCurrentChanged( const QModelIndex & , const QModelIndex & ) ), Qt::UniqueConnection );
703   connect( this, SIGNAL( selectionChanged( const QItemSelection & , const QItemSelection & ) ),
704            this, SLOT( onSelectionChanged( const QItemSelection & , const QItemSelection & ) ), Qt::UniqueConnection );
705 }
706
707
708 MeshSelectionModel::~MeshSelectionModel()
709 {
710   disconnect( this, SIGNAL( currentChanged( const QModelIndex &, const QModelIndex & ) ),
711           this, SLOT( onCurrentChanged( const QModelIndex & , const QModelIndex & ) ) );
712   disconnect( this, SIGNAL( selectionChanged( const QItemSelection & , const QItemSelection & ) ),
713           this, SLOT( onSelectionChanged( const QItemSelection & , const QItemSelection & ) ) );
714 }
715
716
717 void MeshSelectionModel::onSelectionChanged( const QItemSelection & selected, const QItemSelection & deselected )
718 {
719   try {
720
721     QModelIndexList indexes = selected.indexes();
722     for( QModelIndexList::const_iterator i_index = indexes.begin(); i_index != indexes.end(); ++i_index ){
723       HEXABLOCKGUI::currentDocGView->highlightPropagation( *i_index );
724     }
725
726     // CS_BP todo SALOMEGUI_Swig.cxx:370
727     //   indexes = deselected.indexes();
728     //   for( QModelIndexList::const_iterator i_index = indexes.begin(); i_index != indexes.end(); ++i_index )
729     //     _unselectSalome( *i_index);
730   } catch ( ... ) {
731     MESSAGE("Unknown exception was cought !!!");
732   }
733
734 }
735
736
737
738
739
740
741
742
743
744 // //=================================================================================
745 // // function : activateSelection
746 // // purpose  : Activate selection in accordance with myEditCurrentArgument
747 // //=================================================================================
748 // void PatternDataSelectionModel::activateSelection()
749 // {
750 //   erasePreview(false);
751 //
752 //   // local selection
753 //   if (!myObject->_is_nil() && !isAllSubShapes())
754 //   {
755 //     GEOM_Displayer* aDisplayer = getDisplayer();
756 //     SALOME_View* view = GEOM_Displayer::GetActiveView();
757 //     if (view) {
758 //       CORBA::String_var aMainEntry = myObject->GetStudyEntry();
759 //       Handle(SALOME_InteractiveObject) io = new SALOME_InteractiveObject (aMainEntry.in(), "GEOM", "TEMP_IO");
760 //       if (view->isVisible(io)) {
761 //         aDisplayer->Erase(myObject, false, false);
762 //         myIsHiddenMain = true;
763 //       }
764 //     }
765 //
766 //     int prevDisplayMode = aDisplayer->SetDisplayMode(0);
767 //
768 //     SUIT_ViewWindow* aViewWindow = 0;
769 //     SUIT_Study* activeStudy = SUIT_Session::session()->activeApplication()->activeStudy();
770 //     if (activeStudy)
771 //       aViewWindow = SUIT_Session::session()->activeApplication()->desktop()->activeWindow();
772 //     if (aViewWindow == 0) return;
773 //
774 //     SUIT_ViewManager* aViewManager = aViewWindow->getViewManager();
775 //     if (aViewManager->getType() != OCCViewer_Viewer::Type() &&
776 //         aViewManager->getType() != SVTK_Viewer::Type())
777 //       return;
778 //
779 //     SUIT_ViewModel* aViewModel = aViewManager->getViewModel();
780 //     SALOME_View* aView = dynamic_cast<SALOME_View*>(aViewModel);
781 //     if (aView == 0) return;
782 //
783 //     //TopoDS_Shape aMainShape = GEOM_Client::get_client().GetShape(GeometryGUI::GetGeomGen(), myObject);
784 //
785 //     TopTools_IndexedMapOfShape aSubShapesMap;
786 //     TopExp::MapShapes(myShape, aSubShapesMap);
787 //     CORBA::String_var aMainEntry = myObject->GetStudyEntry();
788 //     QString anEntryBase = aMainEntry.in();
789 //
790 //     TopExp_Explorer anExp (myShape, (TopAbs_ShapeEnum)shapeType());
791 //     for (; anExp.More(); anExp.Next())
792 //     {
793 //       TopoDS_Shape aSubShape = anExp.Current();
794 //       int index = aSubShapesMap.FindIndex(aSubShape);
795 //       QString anEntry = anEntryBase + QString("_%1").arg(index);
796 //
797 //       SALOME_Prs* aPrs = aDisplayer->buildSubshapePresentation(aSubShape, anEntry, aView);
798 //       if (aPrs) {
799 //         displayPreview(aPrs, true, false); // append, do not update
800 //       }
801 //     }
802 //     aDisplayer->UpdateViewer();
803 //     aDisplayer->SetDisplayMode(prevDisplayMode);
804 //   }
805 //
806 //   globalSelection(GEOM_ALLSHAPES);
807 // }
808
809
810
811
812 // SUIT_DataOwnerPtrList aList;
813 //   ObjectList::iterator anIter;
814 //   for ( anIter = objects.begin(); anIter != objects.end(); ++anIter )
815 //   {
816 //     QString anEntry = getEntry( *anIter );
817 //     LightApp_DataOwner* anOwher = new LightApp_DataOwner( anEntry );
818 //     aList.append( anOwher );
819 //   }
820 //
821 //   SUIT_Session* session = SUIT_Session::session();
822 //   SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( session->activeApplication() );
823 //   if ( !app )
824 //     return false;
825 //
826 //   LightApp_SelectionMgr* aMgr = app->selectionMgr();
827 //   if ( !aMgr )
828 //     return false;
829 // _getVTKViewWindow()
830 //   aMgr->setSelecte_getVTKViewWindow()d( aList, false );
831
832
833
834
835
836 //   SMESH_Actor* anActor = SMESH::FindActorByObject(myMesh);
837 //   if (!anActor || !anActor->hasIO())
838 //     return;
839 //
840 //   Handle(SALOME_InteractiveObject) anIO = anActor->getIO();
841 //   //mySelectionMgr->clearSelected();
842 //   //mySelectionMgr->AddIObject(anIO, false);
843 //   SALOME_ListIO aList;
844 //   aList.Append(anIO);
845 //   mySelectionMgr->setSelectedObjects(aList, false);
846 //
847 //   // Remove filter corresponding to the current type from viewer
848 //   int aType = myTable->GetType();
849 //   int aFilterId = SMESH::UnknownFilter;
850 //   if      (aType == SMESH::EDGE  ) aFilterId = SMESH::EdgeFilter;
851 //   else if (aType == SMESH::FACE  ) aFilterId = SMESH::FaceFilter;
852 //   else if (aType == SMESH::VOLUME) aFilterId = SMESH::VolumeFilter;
853 //   Handle(VTKViewer_Filter) aFilter = SMESH::GetFilter(aFilterId);
854 //   SMESH::RemoveFilter(aFilterId);
855 //
856 //   // get vtk ids
857 //   TColStd_MapOfInteger aMap;
858 //   QList<int>::const_iterator anIter;
859 //   for (anIter = theIds.begin(); anIter != theIds.end(); ++anIter) {
860 //     aMap.Add(*anIter);
861 //   }
862 //
863 //   // Set new selection
864 //   activeViewWindow->AddOrRemoveIndex(anIO, aMap, false);
865 //   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
866 //     aViewWindow->highlight( anIO, true, true );
867 //
868 //   // insert previously stored filter in viewer if necessary
869 //   if (!aFilter.IsNull())
870 //     SMESH::SetFilter(aFilter);
871
872
873
874
875
876
877
878
879
880 //     //CS_TEST
881 //  if(SVTK_Selector* aSelector = aView->GetSelector()){
882 //             const SALOME_ListIO& aListIO = aSelector->StoredIObjects();
883 //             SALOME_ListIteratorOfListIO anIter(aListIO);
884 //             for(; anIter.More(); anIter.Next()){
885 //               Handle(SALOME_InteractiveObject) anIO = anIter.Value();
886 //     _PTR(Study) aStudy  = HEXABLOCKGUI::activeStudy()->studyDS();
887 //     _PTR(SObject) aSObj = aStudy->FindObjectID( anEntry.toStdString().c_str() );
888 //     LightApp_DataObject*      o = HEXABLOCKGUI::activeStudy()->findObjectByEntry(anEntry.toStdString().c_str());
889 //     //CS_TEST
890
891
892
893
894
895 // void PatternDataSelectionModel::test()
896 // {
897 //   if (myBusy) return;
898 //   myMeshActor = 0;
899 //
900 //   SALOME_ListIO aList;
901 //   mySelectionMgr->selectedObjects(aList,SVTK_Viewer::Type());
902 //
903 //   if (aList.Extent() == 1) {
904 //     Handle(SALOME_InteractiveObject) anIO = aList.First();
905 //     myMeshActor = SMESH::FindActorByEntry(anIO->getEntry());
906 //     if(myMeshActor){
907 //       QString aText;
908 //       if (SMESH::GetNameOfSelectedNodes(activeViewWindow,anIO,aText) == 1) {
909 //         if(SMDS_Mesh* aMesh = myMeshActor->GetObject()->GetMesh()) {
910 //           if(const SMDS_MeshNode* aNode = aMesh->FindNode(aText.toInt())) {
911 //             myBusy = true;
912 //             myId->setText(aText);
913 //             myX->SetValue(aNode->X());
914 //             myY->SetValue(aNode->Y());
915 //             myZ->SetValue(aNode->Z());
916 //             myBusy = false;
917 //             erasePreview(); // avoid overlapping of a selection and a preview
918 //             updateButtons();
919 //             return;
920 //           }
921 //         }
922 //       }
923 //     }
924 //   }
925 //
926 //   reset();
927 // }
928
929
930 // //CS_TEST
931 // SUIT_DataOwnerPtrList aOList;
932 // LightApp_DataOwner* anOwher = new LightApp_DataOwner( "0:1:1:1:2" );
933 // aOList.append( anOwher );
934 // _salomeSelectionMgr->setSelected( aOList, false );
935 // //CS_TEST
936
937 // void PatternDataSelectionModel::highlightGEOM( const QModelIndex & anEltIndex )
938 // {
939 //   std::cout << "PatternDataSelectionModel::highlightGEOM go find ASSOCIATION for"<< anEltIndex.data().toString().toStdString()<<std::endl;
940 // // HEXABLOCKGUI::currentDocGView->getViewWindow()
941 //
942 //   QList<DocumentModel::GeomObj> assocList;
943 //
944 //   DocumentModel               *docModel = NULL;
945 //   const QSortFilterProxyModel *pModel   = NULL;
946 // //   const QStandardItemModel    *smodel
947 // //   const QAbstractItemModel    *theModel = NULL;
948 //
949 //   pModel = dynamic_cast<const QSortFilterProxyModel *>( model() );
950 //
951 //   std::cout << "pModel "<< pModel << std::endl;
952 //   if ( pModel ){
953 //     std::cout << "if ( pModel ){"<< std::endl;
954 //     docModel = dynamic_cast<DocumentModel*>( pModel->sourceModel() );
955 //     if ( docModel ){
956 //       std::cout << "if ( docModel ){"<< std::endl;
957 //       assocList = docModel->getAssociations( pModel->mapToSource(anEltIndex) );
958 //     }
959 //   }
960 //
961 //
962 //   _PTR(Study)   aStudy = GetActiveStudyDocument();
963 //   _PTR(SObject) aSChild;
964 //   CORBA::Object_var aCorbaObj = CORBA::Object::_nil();
965 //   GEOM::GEOM_Object_var assoc;
966 // //   foreach( const QString& entry, geomEntries ){
967 // // struct GeomObj
968 // //         {
969 // //           QString name;
970 // //           QString entry;
971 // //           QString brep;
972 // //           double  start;
973 // //           double  end;
974 // //         };
975 //
976 //   QVariant treeVariant = pModel->mapToSource(anEltIndex).data( HEXA_TREE_ROLE );
977 //   int eltType;
978 //   if ( !treeVariant.isValid() ) return;
979 //   eltType = treeVariant.toInt();
980 //
981 // //   GEOM::GeomObjPtr
982 // //   GEOM::GEOM_Object_ptr firstLine;  //firstLine.nullify();//
983 //   GEOM::GEOM_Object_var firstLine = GEOM::GEOM_Object::_nil();  //GEOM::GeomObjPtr
984 //   GEOM::GEOM_Object_var lastLine  = GEOM::GEOM_Object::_nil();
985 //   double firstParameter = 0.2; //CS_TODO
986 //   double lastParameter  = 0.4; //CS_TODO
987 //
988 //   foreach( const DocumentModel::GeomObj& anAssoc, assocList ){
989 //     std::cout << "FOUND=> " << anAssoc.entry.toStdString() << std::endl;
990 //     aSChild = aStudy->FindObjectID( anAssoc.entry.toStdString() );
991 //     aCorbaObj = corbaObj(aSChild);
992 //     assoc = GEOM::GEOM_Object::_narrow(aCorbaObj);
993 //
994 //     if ( !CORBA::is_nil(assoc) ){
995 //       std::cout << "geom to highlight =>" << anAssoc.name.toStdString() << std::endl;
996 // //       objects.push_back(assoc._retn());
997 // //       if ( eltType == EDGE_TREE ){
998 // //           if ( CORBA::is_nil(firstLine) ){
999 // //             firstLine = GEOM::GEOM_Object::_duplicate( assoc._retn() );
1000 // //           }
1001 // //           lastLine = GEOM::GEOM_Object::_duplicate( assoc._retn() );
1002 // //       }
1003 //       displayPreview( assoc._retn(),
1004 //                       true, //false, //append,
1005 //                       false,//true, //false, //activate,
1006 //                       false,//true,//update,
1007 //                       4,//lineWidth,
1008 //                       1,//-1,//displayMode,
1009 //                       Quantity_NOC_RED );
1010 //     } else {
1011 //       std::cout << "not a geom =>" << anAssoc.name.toStdString()<< std::endl;
1012 //     }
1013 //   }
1014 // //     std::cout << "CORBA::is_nil(firstLine) =>" << CORBA::is_nil(firstLine) << std::endl;
1015 // //     std::cout << "CORBA::is_nil(lastLine) =>"  << CORBA::is_nil(lastLine) << std::endl;
1016 // //     std::cout << "test" << ( !( CORBA::is_nil(firstLine) and !CORBA::is_nil(lastLine) ) ) << std::endl;
1017 //  /*
1018 //   if ( !( CORBA::is_nil(firstLine) and !CORBA::is_nil(lastLine) ) ){
1019 //     GEOM::GEOM_IBasicOperations_var anOper = _geomEngine->GetIBasicOperations( getStudyId() );
1020 //     GEOM::GEOM_Object_var firstPoint = anOper->MakePointOnCurve( firstLine, firstParameter );
1021 //     GEOM::GEOM_Object_var lastPoint  = anOper->MakePointOnCurve( lastLine, lastParameter );
1022 //
1023 // //     std::cout << "firstPoint->_is_nil() =>" << firstPoint->_is_nil() << std::endl;
1024 //     std::cout << "lastPoint->_is_nil() =>"  << lastPoint->_is_nil() << std::endl;
1025 //     if ( !( CORBA::is_nil(firstPoint) ) )// !firstPoint->_is_nil() )
1026 //         displayPreview( firstPoint._retn(), true );
1027 //     if ( !( CORBA::is_nil(lastPoint) ) )//if ( !lastPoint->_is_nil() )
1028 //         displayPreview( lastPoint._retn(), true );
1029 //   }*/
1030 // }
1031 //