Salome HOME
refs #610: correct processing of open new document, when create calculation case...
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_LandCoverMapOp.cxx
1 // Copyright (C) 2014-2015  EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
6 //
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10 // Lesser General Public License for more details.
11 //
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
19 #include "HYDROGUI_LandCoverMapOp.h"
20
21 #include "HYDROGUI_Module.h"
22 #include "HYDROGUI_OCCDisplayer.h"
23 #include "HYDROGUI_Operations.h"
24 #include "HYDROGUI_LandCoverMapDlg.h"
25 #include "HYDROGUI_Tool2.h"
26 #include "HYDROGUI_UpdateFlags.h"
27 #include "HYDROGUI_DataObject.h"
28 #include "HYDROGUI_ShapeLandCoverMap.h"
29 #include "HYDROGUI_OCCSelector.h"
30
31 #include <HYDROData_Iterator.h>
32 #include <HYDROData_StricklerTable.h>
33 #include <HYDROData_PolylineXY.h>
34 #include <HYDROData_Object.h>
35
36 #include <OCCViewer_ViewManager.h>
37 #include <OCCViewer_ViewModel.h>
38 #include <OCCViewer_ViewWindow.h>
39 #include <OCCViewer_ViewPort3d.h>
40
41 #include <OCCViewer_ViewManager.h>
42
43 #include <SalomeApp_Study.h>
44 #include <LightApp_Application.h>
45 #include <LightApp_SelectionMgr.h>
46 #include <LightApp_DataOwner.h>
47 #include <SUIT_ViewWindow.h>
48 #include <SUIT_DataObject.h>
49
50 #include <TopoDS.hxx>
51 #include <TopoDS_Face.hxx>
52 #include <TopTools_ListOfShape.hxx>
53 #include <TopTools_ListIteratorOfListOfShape.hxx>
54 #include <AIS_Shape.hxx>
55
56 #include <QApplication>
57 #include <QMouseEvent>
58
59 HYDROGUI_LandCoverMapOp::HYDROGUI_LandCoverMapOp( HYDROGUI_Module* theModule, const int theOperationId )
60 : HYDROGUI_Operation( theModule ),
61   myOperationId( theOperationId ),
62   myPreviewPrs( 0 ),
63   myPolylineFacePreviewPrs( 0 )
64 {
65   switch( myOperationId )
66   {
67     case CreateLandCoverMapId:
68       setName( tr( "CREATE_LAND_COVER_MAP" ) );
69       break;
70     case AddLandCoverId:
71       setName( tr( "ADD_LAND_COVER" ) );
72       break;
73     case RemoveLandCoverId:
74       setName( tr( "REMOVE_LAND_COVER" ) );
75       break;
76     case SplitLandCoverId:
77       setName( tr( "SPLIT_LAND_COVER" ) );
78       break;
79     case MergeLandCoverId:
80       setName( tr( "MERGE_LAND_COVER" ) );
81       break;
82     case ChangeLandCoverTypeId:
83       setName( tr( "CHANGE_LAND_COVER_TYPE" ) );
84       break;
85   }  
86 }
87
88 HYDROGUI_LandCoverMapOp::~HYDROGUI_LandCoverMapOp()
89 {
90   closePreview();
91 }
92
93 void HYDROGUI_LandCoverMapOp::startOperation()
94 {
95   HYDROGUI_Operation::startOperation();
96
97   HYDROGUI_LandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverMapDlg*>( inputPanel() );
98   if ( !aPanel )
99     return;
100
101   aPanel->blockSignals( true );
102
103   aPanel->reset();
104
105   // Set name of the created/edited land cover map object
106   QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_LAND_COVER_MAP_NAME" ) );
107   if ( myOperationId != CreateLandCoverMapId )
108   {
109     if ( isApplyAndClose() )
110       myEditedObject = Handle(HYDROData_LandCoverMap)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
111     
112     // Construct a list of names of all land cover map objects defined within the data model
113     QStringList aLandCoverMapNames;
114     HYDROData_Iterator anIterator( doc(), KIND_LAND_COVER_MAP );
115     for( ; anIterator.More(); anIterator.Next() )
116     {
117       Handle(HYDROData_LandCoverMap) aLandCoverObj =
118         Handle(HYDROData_LandCoverMap)::DownCast( anIterator.Current() );       
119       if ( !aLandCoverObj.IsNull() )
120         aLandCoverMapNames.append( aLandCoverObj->GetName() );
121     }
122     
123     //aLandCoverMapNames.sort();
124     aPanel->setObjectNames( aLandCoverMapNames );
125
126     if ( myEditedObject.IsNull() )
127     {
128       if ( !aLandCoverMapNames.empty() )
129       {
130         anObjectName = aLandCoverMapNames.first();
131         if ( !anObjectName.isEmpty())
132         {
133           Handle(HYDROData_LandCoverMap) anObject =
134             Handle(HYDROData_LandCoverMap)::DownCast( HYDROGUI_Tool::FindObjectByName( module(), anObjectName ) );
135           if( !anObject.IsNull() )
136             myEditedObject = anObject;
137         }
138       }
139     }
140     else
141       anObjectName = myEditedObject->GetName();
142   }  
143   aPanel->setObjectName( anObjectName );
144
145   closePreview();
146   if ( myOperationId != CreateLandCoverMapId )
147     onCreatePreview();
148
149   aPanel->blockSignals( false );
150
151   module()->update( UF_OCCViewer | UF_FitAll );
152 }
153
154 void HYDROGUI_LandCoverMapOp::abortOperation()
155 {
156   closePreview();
157
158   HYDROGUI_Operation::abortOperation();
159
160   SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( module()->getApp()->activeStudy() );
161   if ( aStudy )
162     module()->update( UF_OCCViewer | UF_FitAll );
163 }
164
165 void HYDROGUI_LandCoverMapOp::commitOperation()
166 {
167   closePreview();
168
169   HYDROGUI_Operation::commitOperation();
170
171   module()->update( UF_OCCViewer | UF_FitAll );
172 }
173
174 HYDROGUI_InputPanel* HYDROGUI_LandCoverMapOp::createInputPanel() const
175 {
176   HYDROGUI_LandCoverMapDlg* aPanel = new HYDROGUI_LandCoverMapDlg( module(), getName(), myOperationId );
177   connect( aPanel, SIGNAL( landCoverMapChanged( const QString& ) ),
178            this, SLOT( onLandCoverMapChanged( const QString& ) ) );
179   connect( aPanel, SIGNAL( polylineFaceChanged() ),
180            this, SLOT( onPolylineFaceChanged() ) );
181   return aPanel;
182 }
183
184 bool HYDROGUI_LandCoverMapOp::processApply( int& theUpdateFlags,
185                                             QString& theErrorMsg,
186                                             QStringList& theBrowseObjectsEntries )
187 {
188   HYDROGUI_LandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverMapDlg*>( inputPanel() );
189   if ( !aPanel )
190     return false;
191
192   // Check name of the created/edited object
193   QString anObjectName = aPanel->getObjectName().simplified();
194   if ( anObjectName.isEmpty() )
195   {
196     theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
197     return false;
198   }
199
200   if ( myOperationId == CreateLandCoverMapId || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
201   {
202     // check that there are no other objects with the same name in the document
203     Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
204     if( !anObject.IsNull() )
205     {
206       theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
207       return false;
208     }
209   }
210
211   Handle(HYDROData_PolylineXY) aPolyline;
212   Handle(HYDROData_Object) aFace;
213
214   TopTools_ListOfShape aFacesSelectedInViewer;
215
216   // Get polyline/face selected in combo-box
217   if ( myOperationId == CreateLandCoverMapId || myOperationId == AddLandCoverId || myOperationId == SplitLandCoverId )
218   {
219     Handle(HYDROData_Entity) aPolylineFace = aPanel->getPolylineFace();
220     aPolyline = Handle(HYDROData_PolylineXY)::DownCast( aPolylineFace );
221     aFace = Handle(HYDROData_Object)::DownCast( aPolylineFace );
222     if ( aPolyline.IsNull() && aFace.IsNull() )
223     {
224       theErrorMsg = tr( "POLYLINE_FACE_NOT_DEFINED" );
225       return false;
226     }
227   }
228   // Get face(s) selected in the 3d viewer
229   else if ( myOperationId == RemoveLandCoverId || 
230             myOperationId == MergeLandCoverId || 
231             myOperationId == ChangeLandCoverTypeId )
232   {
233     if ( myPreviewPrs )
234       // Fill in aFacesSelectedInViewer list
235       getSelectedShapes( aFacesSelectedInViewer );
236   }
237
238   // Get selected Strickler type
239   QString aSelectedStricklerType;
240   if ( myOperationId == CreateLandCoverMapId || 
241        myOperationId == AddLandCoverId || 
242        myOperationId == MergeLandCoverId ||
243        myOperationId == ChangeLandCoverTypeId )
244   {
245     aSelectedStricklerType = aPanel->getSelectedStricklerTypeName();
246     if ( aSelectedStricklerType.isEmpty() )
247     {
248       theErrorMsg = tr( "STRICKLER_TYPE_NOT_DEFINED" );
249       return false;
250     }
251   }
252
253   // Create / find the new / edited land cover map object
254   Handle(HYDROData_LandCoverMap) aLandCoverMapObj = myOperationId != CreateLandCoverMapId ? myEditedObject :
255     Handle(HYDROData_LandCoverMap)::DownCast( doc()->CreateObject( KIND_LAND_COVER_MAP ) );
256   if ( aLandCoverMapObj.IsNull() )
257   {
258     theErrorMsg = tr( "LAND_COVER_MAP_UNDEFINED" );
259     return false;
260   }
261
262   // Set land cover map name
263   aLandCoverMapObj->SetName( anObjectName );
264   
265   // Add land cover to new / edited land cover map
266   if ( myOperationId == CreateLandCoverMapId || myOperationId == AddLandCoverId )
267   {
268     bool aLandCoverAdded = false;
269     if ( !aPolyline.IsNull() )
270       aLandCoverAdded = aLandCoverMapObj->Add( aPolyline, aSelectedStricklerType );
271     else if ( !aFace.IsNull() )
272       aLandCoverAdded = aLandCoverMapObj->Add( aFace, aSelectedStricklerType );
273     if ( !aLandCoverAdded )
274     {
275       theErrorMsg = tr( "LAND_COVER_NOT_ADDED" );
276       return false;
277     }
278   }
279
280   // Remove land cover from edited land cover map
281   if ( myOperationId == RemoveLandCoverId )
282   {
283     bool aLandCoverRemoved = false;
284     if ( !aFacesSelectedInViewer.IsEmpty() )
285     {
286       aLandCoverRemoved = aLandCoverMapObj->Remove( aFacesSelectedInViewer );
287       if ( !aLandCoverRemoved )
288       {
289         theErrorMsg = tr( "LAND_COVER_NOT_REMOVED" );
290         return false;
291       }
292     }
293   }
294
295   // Split land cover(s) inside edited land cover map
296   if ( myOperationId == SplitLandCoverId )
297   {
298     bool aLandCoverSplit = false;
299     if ( !aPolyline.IsNull() )
300       aLandCoverSplit = aLandCoverMapObj->Split( aPolyline );
301     else if ( !aFace.IsNull() )
302     {
303       // Get the complete boundary of the object face as the splitting polyline
304       QList<TopoDS_Shape> aBoundShapes;
305       QStringList aBoundNames;
306       aFace->GetBoundaries( aBoundShapes, aBoundNames );
307
308       for( int i=0, n=aBoundShapes.size(); i<n; i++ )
309       {
310         TopoDS_Shape aShape = aBoundShapes[i];
311         if( aShape.IsNull() )
312           continue;
313
314         bool aSplitResult = aLandCoverMapObj->Split( aShape );
315         aLandCoverSplit = ( i==0 ? aSplitResult : aLandCoverSplit && aSplitResult );
316       }
317     }
318     if ( !aLandCoverSplit )
319     {
320       theErrorMsg = tr( "LAND_COVER_NOT_SPLIT" );
321       return false;
322     }
323   }
324
325   // Merge land covers inside edited land cover map
326   if ( myOperationId == MergeLandCoverId )
327   {
328     bool aLandCoverMerged = false;
329     if ( !aFacesSelectedInViewer.IsEmpty() )
330     {
331       aLandCoverMerged = aLandCoverMapObj->Merge( aFacesSelectedInViewer, aSelectedStricklerType );
332       if ( !aLandCoverMerged )
333       {
334         theErrorMsg = tr( "LAND_COVER_NOT_MERGED" );
335         return false;
336       }
337     }
338   }
339
340   // Change Strickler type for land cover(s) inside edited land cover map
341   if ( myOperationId == ChangeLandCoverTypeId )
342   {
343     bool aLandCoverChangeType = false;
344     if ( !aFacesSelectedInViewer.IsEmpty() )
345     {
346       aLandCoverChangeType = aLandCoverMapObj->ChangeType( aFacesSelectedInViewer, aSelectedStricklerType );
347       if ( !aLandCoverChangeType )
348       {
349         theErrorMsg = tr( "LAND_COVER_TYPE_NOT_CHANGED" );
350         return false;
351       }
352     }
353   }
354     
355   // Update land cover map object and close preview
356   aLandCoverMapObj->Update();
357
358   closePreview();
359
360   // Publish the newly created land cover map in the Object Browser
361   module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), aLandCoverMapObj, true );
362   if ( myOperationId == CreateLandCoverMapId )
363   {
364     QString anEntry = HYDROGUI_DataObject::dataObjectEntry( aLandCoverMapObj );
365     theBrowseObjectsEntries.append( anEntry );
366   }
367
368   // Update presentation of land cover object in the 3d viewer
369   module()->setIsToUpdate( aLandCoverMapObj );
370   module()->getOCCDisplayer()->SetToUpdateColorScale();
371
372   theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
373
374   if ( myOperationId == CreateLandCoverMapId )
375     module()->enableLCMActions();
376
377   return true;
378 }
379
380 void HYDROGUI_LandCoverMapOp::onLandCoverMapChanged( const QString& theName )
381 {
382   // If the edited land cover map was changed in the combo-box, update myEditedObject
383   if ( myOperationId != CreateLandCoverMapId )
384   {
385     myEditedObject = Handle(HYDROData_LandCoverMap)::DownCast( HYDROGUI_Tool::FindObjectByName( module(), theName ) );
386     if ( !myEditedObject.IsNull() )
387     {
388       // Show preview of the newly selected land cover map
389       closePreview();
390       onCreatePreview();
391     }
392   }
393 }
394
395 void HYDROGUI_LandCoverMapOp::onPolylineFaceChanged()
396 {
397   HYDROGUI_LandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverMapDlg*>( inputPanel() );
398   if ( !aPanel )
399     return;
400
401   LightApp_DataOwner* aPolylineFaceDataOwner = NULL;
402   Handle(HYDROData_Entity) aPolylineFace = aPanel->getPolylineFace();
403   if ( !aPolylineFace.IsNull() )
404   {
405     // Select chosen polyline/face in the Object Browser, if it is not selected yet
406     // (i.e. object was chosen not in the Object Browser or 3d Viewer, but in combo-box)
407     aPolylineFaceDataOwner = new LightApp_DataOwner( HYDROGUI_DataObject::dataObjectEntry( aPolylineFace ) );  
408     LightApp_SelectionMgr* aSelectionMgr = module()->getApp()->selectionMgr();
409     if ( aSelectionMgr )
410     {
411       bool bIsAlreadySelected = false;
412       SUIT_DataOwnerPtrList aSelectedOwners;
413       aSelectionMgr->selected( aSelectedOwners );
414       foreach( SUIT_DataOwner* aSUITOwner, aSelectedOwners )
415       {
416         if ( LightApp_DataOwner* anOwner = dynamic_cast<LightApp_DataOwner*>( aSUITOwner ) )
417         {
418           if ( anOwner->entry() == aPolylineFaceDataOwner->entry() )
419           {
420             bIsAlreadySelected = true;
421             break;
422           }
423         }
424       }
425       if ( !bIsAlreadySelected )
426       {
427         SUIT_DataOwnerPtrList aList( true );
428         aList.append( SUIT_DataOwnerPtr( aPolylineFaceDataOwner ) );
429         aSelectionMgr->setSelected( aList );
430       }
431     }
432
433     // Show Preview of selected polyline/face
434     Handle(HYDROData_PolylineXY) aPolyline = Handle(HYDROData_PolylineXY)::DownCast( aPolylineFace );
435     Handle(HYDROData_Object) aFace = Handle(HYDROData_Object)::DownCast( aPolylineFace );
436     if ( !aPolyline.IsNull() || !aFace.IsNull() )
437     {
438       TopoDS_Shape aTopoDSShape;
439       if ( !aPolyline.IsNull() )
440         aTopoDSShape = aPolyline->GetShape();
441       else
442         aTopoDSShape = aFace->GetTopShape();
443
444       OCCViewer_ViewManager* aViewManager = ::qobject_cast<OCCViewer_ViewManager*>( 
445         module()->getApp()->getViewManager( OCCViewer_Viewer::Type(), true ) );
446       if ( aViewManager )
447       {
448         if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
449         {
450           if ( myPolylineFacePreviewPrs )
451           {
452             delete myPolylineFacePreviewPrs;
453             myPolylineFacePreviewPrs = 0;
454           }
455
456           int aViewerId = (size_t)aViewer;
457           if ( !module()->isObjectVisible( aViewerId, aPolylineFace ) )
458           {
459             Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
460             if ( !aCtx.IsNull() )
461             {
462               myPolylineFacePreviewPrs = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
463               aCtx->ClearSelected();
464
465               myPolylineFacePreviewPrs->setBorderColor( Qt::white, false, false );
466               myPolylineFacePreviewPrs->setShape( aTopoDSShape, true, true, !aPolyline.IsNull() ? AIS_WireFrame : AIS_Shaded );
467
468               module()->update( UF_OCCViewer | UF_FitAll );
469             }       
470           }
471         }
472       }
473     }
474   }
475 }
476
477 void HYDROGUI_LandCoverMapOp::onCreatePreview()
478 {
479   HYDROGUI_LandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverMapDlg*>( inputPanel() );
480   if ( !aPanel )
481     return;
482
483   QApplication::setOverrideCursor( Qt::WaitCursor );  
484
485   LightApp_Application* anApp = module()->getApp();
486   if ( !getPreviewManager() )
487     setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>( 
488                        anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
489   OCCViewer_ViewManager* aViewManager = getPreviewManager();
490   if ( aViewManager && !myPreviewPrs )
491   {
492     if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
493     {
494       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
495       if ( !aCtx.IsNull() )
496       {
497         if ( myOperationId == RemoveLandCoverId || 
498              myOperationId == MergeLandCoverId ||
499              myOperationId == ChangeLandCoverTypeId )
500         {
501           disconnect(aViewManager, SIGNAL(mousePress(SUIT_ViewWindow*, QMouseEvent*)), 
502             aViewer, SLOT(onMousePress(SUIT_ViewWindow*, QMouseEvent*)));
503           disconnect(aViewManager, SIGNAL(mouseRelease(SUIT_ViewWindow*, QMouseEvent*)), 
504             aViewer, SLOT(onMouseRelease(SUIT_ViewWindow*, QMouseEvent*)));        
505
506           connect(aViewManager, SIGNAL(mousePress(SUIT_ViewWindow*, QMouseEvent*)), 
507               this, SLOT(onMousePress(SUIT_ViewWindow*, QMouseEvent*)));
508           connect(aViewManager, SIGNAL(mouseRelease(SUIT_ViewWindow*, QMouseEvent*)),
509             this, SLOT(onMouseRelease(SUIT_ViewWindow*, QMouseEvent*)));
510
511           LightApp_SelectionMgr* aSelectionMgr = module()->getApp()->selectionMgr();
512           if ( aSelectionMgr )
513           {
514             QList<SUIT_Selector*> aSelectorList;
515             aSelectionMgr->selectors( aViewManager->getType(), aSelectorList );
516             QList<SUIT_Selector*>::iterator anIter, anIterEnd = aSelectorList.end();
517             for( anIter = aSelectorList.begin(); anIter != anIterEnd; anIter++ )
518             {
519               HYDROGUI_OCCSelector* aHydroSelector = dynamic_cast<HYDROGUI_OCCSelector*>( *anIter );
520               if ( aHydroSelector )
521               {
522                 disconnect( aHydroSelector->viewer(), SIGNAL( deselection() ), aHydroSelector, SLOT( onDeselection() ) );
523                 connect( this, SIGNAL( deselection() ), aHydroSelector, SLOT( onDeselection() ) );
524               }
525             }
526           }
527         
528           connect( this, SIGNAL( selectionChanged() ), this, SLOT( onViewerSelectionChanged() ) );
529         }
530         else
531           connect( aViewer, SIGNAL( selectionChanged() ), this, SLOT( onViewerSelectionChanged() ) );
532         myPreviewPrs = new HYDROGUI_ShapeLandCoverMap( module()->getOCCDisplayer(), aCtx, myEditedObject, getPreviewZLayer()/*, theIsScalarMapMode*/ );
533       }
534     }
535   }
536
537   if ( aViewManager && myPreviewPrs && !myEditedObject.IsNull() )
538   {
539     if ( myOperationId == RemoveLandCoverId || 
540          myOperationId == MergeLandCoverId ||
541          myOperationId == ChangeLandCoverTypeId )
542       myPreviewPrs->setSelectionMode( AIS_Shape::SelectionMode( TopAbs_FACE ) ); 
543     myPreviewPrs->update( false, false );
544     
545     if ( myOperationId == ChangeLandCoverTypeId )
546       selectLandCoverInPreview();
547   }
548   
549   module()->update( UF_OCCViewer | UF_FitAll );
550
551   QApplication::restoreOverrideCursor();  
552 }
553
554 void HYDROGUI_LandCoverMapOp::onViewerSelectionChanged()
555 {
556   HYDROGUI_LandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverMapDlg*>( inputPanel() );
557   if ( !aPanel )
558     return;
559
560   int aNbSelected = getNbSelected();
561
562   if ( myOperationId == RemoveLandCoverId || myOperationId == ChangeLandCoverTypeId )
563     // Enable Apply, Apply and Close buttons only if at least one face (land cover) is selected in the 3d viewer
564     aPanel->setApplyEnabled( aNbSelected > 0 );
565   else if ( myOperationId == MergeLandCoverId )
566     // Enable Apply, Apply and Close buttons only if at least two faces (land covers) are selected in the 3d viewer
567     aPanel->setApplyEnabled( aNbSelected > 1 );
568
569   if ( myOperationId == MergeLandCoverId || myOperationId == ChangeLandCoverTypeId )
570   {
571     if ( aNbSelected == 1 && !myEditedObject.IsNull() )
572     {
573       TopTools_ListOfShape aFacesSelectedInViewer;
574       getSelectedShapes( aFacesSelectedInViewer );
575       if ( aFacesSelectedInViewer.Extent() == 1 )
576       {
577         QString aType = myEditedObject->StricklerType( TopoDS::Face( aFacesSelectedInViewer.First() ) );
578         if ( !aType.isEmpty() )
579           aPanel->setSelectedStricklerTypeName( aType );
580       }
581     }
582   }
583
584   if ( myOperationId == RemoveLandCoverId || myOperationId == MergeLandCoverId || myOperationId == ChangeLandCoverTypeId )
585     aPanel->updateSelectedLandCoversLabel( aNbSelected );
586 }
587
588 void HYDROGUI_LandCoverMapOp::onMousePress(SUIT_ViewWindow* theWindow, QMouseEvent* theEvent)
589 {
590   myStartPnt.setX(theEvent->x()); myStartPnt.setY(theEvent->y());
591 }
592
593 void HYDROGUI_LandCoverMapOp::onMouseRelease(SUIT_ViewWindow* theWindow, QMouseEvent* theEvent)
594 {
595   if (theEvent->button() != Qt::LeftButton) return;
596   if (!theWindow->inherits("OCCViewer_ViewWindow")) return;
597
598   OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*) theWindow;
599   if (!aView )
600     return;
601
602   OCCViewer_ViewManager* aViewManager = getPreviewManager();
603   if ( !aViewManager )
604     return;
605   
606   OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer();
607   if ( !aViewer )
608     return;
609
610   Handle(AIS_InteractiveContext) aCtx = getInteractiveContext();
611   if ( aCtx.IsNull() )
612     return;
613
614   myEndPnt.setX(theEvent->x()); myEndPnt.setY(theEvent->y());
615
616   if (myStartPnt == myEndPnt)
617   {
618     if ( !aViewer->isPreselectionEnabled() ) {
619       Handle(V3d_View) aView3d = aView->getViewPort()->getView();
620       if ( !aView3d.IsNull() ) {
621         aCtx->MoveTo(myEndPnt.x(), myEndPnt.y(), aView3d);
622       }
623     }
624
625     Handle(StdSelect_ViewerSelector3d) aMainSelector = aCtx->MainSelector();
626     if ( aMainSelector.IsNull() )
627       return;
628     const Standard_Integer aDetectedNb = aMainSelector->NbPicked();
629     if ( aDetectedNb == 0 )
630     {
631       aCtx->ClearSelected( false );
632       emit deselection();
633     }
634
635     for (Standard_Integer aDetIter = 1; aDetIter <= aDetectedNb; ++aDetIter)
636     {
637       Handle(SelectMgr_EntityOwner) anOwner = aMainSelector->Picked (aDetIter);
638       aCtx->AddOrRemoveSelected( anOwner, Standard_False );
639     }
640   }
641   else
642   {
643     aCtx->ShiftSelect(myStartPnt.x(), myStartPnt.y(),
644                       myEndPnt.x(), myEndPnt.y(),
645                       aView->getViewPort()->getView(), Standard_False );
646   }
647
648   aCtx->UpdateCurrentViewer();
649   emit selectionChanged();  
650 }
651
652 void HYDROGUI_LandCoverMapOp::closePreview()
653 {
654   if ( myPreviewPrs )
655   {
656     delete myPreviewPrs;
657     myPreviewPrs = 0;
658   }
659
660   if ( myPolylineFacePreviewPrs )
661   {
662     delete myPolylineFacePreviewPrs;
663     myPolylineFacePreviewPrs = 0;
664   }
665
666   HYDROGUI_LandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverMapDlg*>( inputPanel() );
667   if ( !aPanel )
668     return;
669
670   if ( myOperationId == RemoveLandCoverId || myOperationId == MergeLandCoverId || myOperationId == ChangeLandCoverTypeId )
671     aPanel->setApplyEnabled( false );
672
673   if ( myOperationId == RemoveLandCoverId || 
674        myOperationId == MergeLandCoverId ||
675        myOperationId == ChangeLandCoverTypeId )
676   {
677     OCCViewer_ViewManager* aViewManager = getPreviewManager();
678     if ( aViewManager )
679     {
680       if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
681       {
682         disconnect(aViewManager, SIGNAL(mousePress(SUIT_ViewWindow*, QMouseEvent*)), 
683           this, SLOT(onMousePress(SUIT_ViewWindow*, QMouseEvent*)));
684         disconnect(aViewManager, SIGNAL(mouseRelease(SUIT_ViewWindow*, QMouseEvent*)),
685           this, SLOT(onMouseRelease(SUIT_ViewWindow*, QMouseEvent*)));
686         connect(aViewManager, SIGNAL(mousePress(SUIT_ViewWindow*, QMouseEvent*)), 
687           aViewer, SLOT(onMousePress(SUIT_ViewWindow*, QMouseEvent*)));
688         connect(aViewManager, SIGNAL(mouseRelease(SUIT_ViewWindow*, QMouseEvent*)), 
689           aViewer, SLOT(onMouseRelease(SUIT_ViewWindow*, QMouseEvent*)));  
690
691         LightApp_SelectionMgr* aSelectionMgr = module()->getApp()->selectionMgr();
692         if ( aSelectionMgr )
693         {
694           QList<SUIT_Selector*> aSelectorList;
695           aSelectionMgr->selectors( aViewManager->getType(), aSelectorList );
696           QList<SUIT_Selector*>::iterator anIter, anIterEnd = aSelectorList.end();
697           for( anIter = aSelectorList.begin(); anIter != anIterEnd; anIter++ )
698           {
699             HYDROGUI_OCCSelector* aHydroSelector = dynamic_cast<HYDROGUI_OCCSelector*>( *anIter );
700             if ( aHydroSelector )
701             {
702               disconnect( this, SIGNAL( deselection() ), aHydroSelector, SLOT( onDeselection() ) );
703               connect( aHydroSelector->viewer(), SIGNAL( deselection() ), aHydroSelector, SLOT( onDeselection() ) );            
704             }
705           }
706         }
707       }
708     }
709   }
710 }
711
712 Handle(AIS_InteractiveContext) HYDROGUI_LandCoverMapOp::getInteractiveContext()
713 {
714   OCCViewer_ViewManager* aViewManager = getPreviewManager();
715   Handle(AIS_InteractiveContext) aCtx = NULL;
716   if ( aViewManager ) {
717     if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() ) {
718       aCtx = aViewer->getAISContext();
719     }
720   }
721   return aCtx;
722 }
723
724 void HYDROGUI_LandCoverMapOp::getSelectedShapes( TopTools_ListOfShape& theSelectedShapes )
725 {
726   Handle(AIS_InteractiveContext) aCtx = getInteractiveContext();
727   if ( !aCtx.IsNull() )
728   {
729     for ( aCtx->InitSelected(); aCtx->MoreSelected(); aCtx->NextSelected() )
730     {
731       TopoDS_Shape aSelectedShape = aCtx->SelectedShape();
732       if ( aSelectedShape.IsNull() )
733         continue;
734
735       theSelectedShapes.Append( aSelectedShape );
736     }
737   }
738 }
739
740 int HYDROGUI_LandCoverMapOp::getNbSelected()
741 {
742   int aNbSelected = 0;
743
744   Handle(AIS_InteractiveContext) aCtx = getInteractiveContext();
745   if ( !aCtx.IsNull() )
746   {
747     for ( aCtx->InitSelected(); aCtx->MoreSelected(); aCtx->NextSelected() )
748     {
749       TopoDS_Shape aSelectedShape = aCtx->SelectedShape();
750       if ( aSelectedShape.IsNull() )
751         continue;
752       aNbSelected++;
753     }
754   }
755
756   return aNbSelected;
757 }
758
759 void HYDROGUI_LandCoverMapOp::selectLandCoverInPreview()
760 {
761   if ( myPreviewPrs && !myEditedObject.IsNull() && myEditedObject->GetLCCount() == 1 )
762   {
763     OCCViewer_ViewManager* aViewManager = getPreviewManager();
764     if ( !aViewManager )
765       return;
766     
767     Handle(AIS_InteractiveContext) aCtx = getInteractiveContext();
768     if ( aCtx.IsNull() )
769       return;
770     
771     OCCViewer_ViewWindow* aViewWindow = (OCCViewer_ViewWindow*)aViewManager->getActiveView();
772     if ( !aViewWindow )
773       return;
774
775     OCCViewer_ViewPort3d* aViewPort = aViewWindow->getViewPort();
776     if ( !aViewPort )
777       return;
778
779     Handle(V3d_View) aView = aViewPort->getView();
780     if ( aView.IsNull() )
781       return;
782
783     aCtx->ShiftSelect( 0, 0, aViewPort->width(), aViewPort->height(), aView, Standard_False );
784     aCtx->UpdateCurrentViewer();
785     emit selectionChanged();
786   }
787 }