Salome HOME
4b095607e27d3c87fb93c887c2a0b22d8c020c6d
[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   if ( myOperationId == RemoveLandCoverId || myOperationId == MergeLandCoverId || myOperationId == ChangeLandCoverTypeId )
378     aPanel->updateSelectedLandCoversLabel( getNbSelected() );
379
380   return true;
381 }
382
383 void HYDROGUI_LandCoverMapOp::onLandCoverMapChanged( const QString& theName )
384 {
385   // If the edited land cover map was changed in the combo-box, update myEditedObject
386   if ( myOperationId != CreateLandCoverMapId )
387   {
388     myEditedObject = Handle(HYDROData_LandCoverMap)::DownCast( HYDROGUI_Tool::FindObjectByName( module(), theName ) );
389     if ( !myEditedObject.IsNull() )
390     {
391       // Show preview of the newly selected land cover map
392       closePreview();
393       onCreatePreview();
394     }
395   }
396 }
397
398 void HYDROGUI_LandCoverMapOp::onPolylineFaceChanged()
399 {
400   HYDROGUI_LandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverMapDlg*>( inputPanel() );
401   if ( !aPanel )
402     return;
403
404   LightApp_DataOwner* aPolylineFaceDataOwner = NULL;
405   Handle(HYDROData_Entity) aPolylineFace = aPanel->getPolylineFace();
406   if ( !aPolylineFace.IsNull() )
407   {
408     // Select chosen polyline/face in the Object Browser, if it is not selected yet
409     // (i.e. object was chosen not in the Object Browser or 3d Viewer, but in combo-box)
410     aPolylineFaceDataOwner = new LightApp_DataOwner( HYDROGUI_DataObject::dataObjectEntry( aPolylineFace ) );  
411     LightApp_SelectionMgr* aSelectionMgr = module()->getApp()->selectionMgr();
412     if ( aSelectionMgr )
413     {
414       bool bIsAlreadySelected = false;
415       SUIT_DataOwnerPtrList aSelectedOwners;
416       aSelectionMgr->selected( aSelectedOwners );
417       foreach( SUIT_DataOwner* aSUITOwner, aSelectedOwners )
418       {
419         if ( LightApp_DataOwner* anOwner = dynamic_cast<LightApp_DataOwner*>( aSUITOwner ) )
420         {
421           if ( anOwner->entry() == aPolylineFaceDataOwner->entry() )
422           {
423             bIsAlreadySelected = true;
424             break;
425           }
426         }
427       }
428       if ( !bIsAlreadySelected )
429       {
430         SUIT_DataOwnerPtrList aList( true );
431         aList.append( SUIT_DataOwnerPtr( aPolylineFaceDataOwner ) );
432         aSelectionMgr->setSelected( aList );
433       }
434     }
435
436     // Show Preview of selected polyline/face
437     Handle(HYDROData_PolylineXY) aPolyline = Handle(HYDROData_PolylineXY)::DownCast( aPolylineFace );
438     Handle(HYDROData_Object) aFace = Handle(HYDROData_Object)::DownCast( aPolylineFace );
439     if ( !aPolyline.IsNull() || !aFace.IsNull() )
440     {
441       TopoDS_Shape aTopoDSShape;
442       if ( !aPolyline.IsNull() )
443         aTopoDSShape = aPolyline->GetShape();
444       else
445         aTopoDSShape = aFace->GetTopShape();
446
447       OCCViewer_ViewManager* aViewManager = ::qobject_cast<OCCViewer_ViewManager*>( 
448         module()->getApp()->getViewManager( OCCViewer_Viewer::Type(), true ) );
449       if ( aViewManager )
450       {
451         if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
452         {
453           if ( myPolylineFacePreviewPrs )
454           {
455             delete myPolylineFacePreviewPrs;
456             myPolylineFacePreviewPrs = 0;
457           }
458
459           int aViewerId = (size_t)aViewer;
460           if ( !module()->isObjectVisible( aViewerId, aPolylineFace ) )
461           {
462             Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
463             if ( !aCtx.IsNull() )
464             {
465               myPolylineFacePreviewPrs = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
466               aCtx->ClearSelected();
467
468               myPolylineFacePreviewPrs->setBorderColor( Qt::white, false, false );
469               myPolylineFacePreviewPrs->setShape( aTopoDSShape, true, true, !aPolyline.IsNull() ? AIS_WireFrame : AIS_Shaded );
470
471               module()->update( UF_OCCViewer | UF_FitAll );
472             }       
473           }
474         }
475       }
476     }
477   }
478 }
479
480 void HYDROGUI_LandCoverMapOp::onCreatePreview()
481 {
482   HYDROGUI_LandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverMapDlg*>( inputPanel() );
483   if ( !aPanel )
484     return;
485
486   QApplication::setOverrideCursor( Qt::WaitCursor );  
487
488   LightApp_Application* anApp = module()->getApp();
489   if ( !getPreviewManager() )
490     setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>( 
491                        anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
492   OCCViewer_ViewManager* aViewManager = getPreviewManager();
493   if ( aViewManager && !myPreviewPrs )
494   {
495     if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
496     {
497       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
498       if ( !aCtx.IsNull() )
499       {
500         if ( myOperationId == RemoveLandCoverId || 
501              myOperationId == MergeLandCoverId ||
502              myOperationId == ChangeLandCoverTypeId )
503         {
504           disconnect(aViewManager, SIGNAL(mousePress(SUIT_ViewWindow*, QMouseEvent*)), 
505             aViewer, SLOT(onMousePress(SUIT_ViewWindow*, QMouseEvent*)));
506           disconnect(aViewManager, SIGNAL(mouseRelease(SUIT_ViewWindow*, QMouseEvent*)), 
507             aViewer, SLOT(onMouseRelease(SUIT_ViewWindow*, QMouseEvent*)));        
508
509           connect(aViewManager, SIGNAL(mousePress(SUIT_ViewWindow*, QMouseEvent*)), 
510               this, SLOT(onMousePress(SUIT_ViewWindow*, QMouseEvent*)));
511           connect(aViewManager, SIGNAL(mouseRelease(SUIT_ViewWindow*, QMouseEvent*)),
512             this, SLOT(onMouseRelease(SUIT_ViewWindow*, QMouseEvent*)));
513
514           LightApp_SelectionMgr* aSelectionMgr = module()->getApp()->selectionMgr();
515           if ( aSelectionMgr )
516           {
517             QList<SUIT_Selector*> aSelectorList;
518             aSelectionMgr->selectors( aViewManager->getType(), aSelectorList );
519             QList<SUIT_Selector*>::iterator anIter, anIterEnd = aSelectorList.end();
520             for( anIter = aSelectorList.begin(); anIter != anIterEnd; anIter++ )
521             {
522               HYDROGUI_OCCSelector* aHydroSelector = dynamic_cast<HYDROGUI_OCCSelector*>( *anIter );
523               if ( aHydroSelector )
524               {
525                 disconnect( aHydroSelector->viewer(), SIGNAL( deselection() ), aHydroSelector, SLOT( onDeselection() ) );
526                 connect( this, SIGNAL( deselection() ), aHydroSelector, SLOT( onDeselection() ) );
527               }
528             }
529           }
530         
531           connect( this, SIGNAL( selectionChanged() ), this, SLOT( onViewerSelectionChanged() ) );
532         }
533         else
534           connect( aViewer, SIGNAL( selectionChanged() ), this, SLOT( onViewerSelectionChanged() ) );
535         myPreviewPrs = new HYDROGUI_ShapeLandCoverMap( module()->getOCCDisplayer(), aCtx, myEditedObject, getPreviewZLayer()/*, theIsScalarMapMode*/ );
536       }
537     }
538   }
539
540   if ( aViewManager && myPreviewPrs && !myEditedObject.IsNull() )
541   {
542     if ( myOperationId == RemoveLandCoverId || 
543          myOperationId == MergeLandCoverId ||
544          myOperationId == ChangeLandCoverTypeId )
545       myPreviewPrs->setSelectionMode( AIS_Shape::SelectionMode( TopAbs_FACE ) ); 
546     myPreviewPrs->update( false, false );
547     
548     if ( myOperationId == ChangeLandCoverTypeId )
549       selectLandCoverInPreview();
550   }
551   
552   module()->update( UF_OCCViewer | UF_FitAll );
553
554   QApplication::restoreOverrideCursor();  
555 }
556
557 void HYDROGUI_LandCoverMapOp::onViewerSelectionChanged()
558 {
559   HYDROGUI_LandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverMapDlg*>( inputPanel() );
560   if ( !aPanel )
561     return;
562
563   int aNbSelected = getNbSelected();
564
565   if ( myOperationId == RemoveLandCoverId || myOperationId == ChangeLandCoverTypeId )
566     // Enable Apply, Apply and Close buttons only if at least one face (land cover) is selected in the 3d viewer
567     aPanel->setApplyEnabled( aNbSelected > 0 );
568   else if ( myOperationId == MergeLandCoverId )
569     // Enable Apply, Apply and Close buttons only if at least two faces (land covers) are selected in the 3d viewer
570     aPanel->setApplyEnabled( aNbSelected > 1 );
571
572   if ( myOperationId == MergeLandCoverId || myOperationId == ChangeLandCoverTypeId )
573   {
574     if ( aNbSelected == 1 && !myEditedObject.IsNull() )
575     {
576       TopTools_ListOfShape aFacesSelectedInViewer;
577       getSelectedShapes( aFacesSelectedInViewer );
578       if ( aFacesSelectedInViewer.Extent() == 1 )
579       {
580         QString aType = myEditedObject->StricklerType( TopoDS::Face( aFacesSelectedInViewer.First() ) );
581         if ( !aType.isEmpty() )
582           aPanel->setSelectedStricklerTypeName( aType );
583       }
584     }
585   }
586
587   if ( myOperationId == RemoveLandCoverId || myOperationId == MergeLandCoverId || myOperationId == ChangeLandCoverTypeId )
588     aPanel->updateSelectedLandCoversLabel( aNbSelected );
589 }
590
591 void HYDROGUI_LandCoverMapOp::onMousePress(SUIT_ViewWindow* theWindow, QMouseEvent* theEvent)
592 {
593   myStartPnt.setX(theEvent->x()); myStartPnt.setY(theEvent->y());
594 }
595
596 void HYDROGUI_LandCoverMapOp::onMouseRelease(SUIT_ViewWindow* theWindow, QMouseEvent* theEvent)
597 {
598   if (theEvent->button() != Qt::LeftButton) return;
599   if (!theWindow->inherits("OCCViewer_ViewWindow")) return;
600
601   OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*) theWindow;
602   if (!aView )
603     return;
604
605   OCCViewer_ViewManager* aViewManager = getPreviewManager();
606   if ( !aViewManager )
607     return;
608   
609   OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer();
610   if ( !aViewer )
611     return;
612
613   Handle(AIS_InteractiveContext) aCtx = getInteractiveContext();
614   if ( aCtx.IsNull() )
615     return;
616
617   myEndPnt.setX(theEvent->x()); myEndPnt.setY(theEvent->y());
618
619   if (myStartPnt == myEndPnt)
620   {
621     if ( !aViewer->isPreselectionEnabled() ) {
622       Handle(V3d_View) aView3d = aView->getViewPort()->getView();
623       if ( !aView3d.IsNull() ) {
624         aCtx->MoveTo(myEndPnt.x(), myEndPnt.y(), aView3d);
625       }
626     }
627
628     Handle(StdSelect_ViewerSelector3d) aMainSelector = aCtx->MainSelector();
629     if ( aMainSelector.IsNull() )
630       return;
631     const Standard_Integer aDetectedNb = aMainSelector->NbPicked();
632     if ( aDetectedNb == 0 )
633     {
634       aCtx->ClearSelected( false );
635       emit deselection();
636     }
637
638     for (Standard_Integer aDetIter = 1; aDetIter <= aDetectedNb; ++aDetIter)
639     {
640       Handle(SelectMgr_EntityOwner) anOwner = aMainSelector->Picked (aDetIter);
641       aCtx->AddOrRemoveSelected( anOwner, Standard_False );
642     }
643   }
644   else
645   {
646     aCtx->ShiftSelect(myStartPnt.x(), myStartPnt.y(),
647                       myEndPnt.x(), myEndPnt.y(),
648                       aView->getViewPort()->getView(), Standard_False );
649   }
650
651   aCtx->UpdateCurrentViewer();
652   emit selectionChanged();  
653 }
654
655 void HYDROGUI_LandCoverMapOp::closePreview()
656 {
657   if ( myPreviewPrs )
658   {
659     delete myPreviewPrs;
660     myPreviewPrs = 0;
661   }
662
663   if ( myPolylineFacePreviewPrs )
664   {
665     delete myPolylineFacePreviewPrs;
666     myPolylineFacePreviewPrs = 0;
667   }
668
669   HYDROGUI_LandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverMapDlg*>( inputPanel() );
670   if ( !aPanel )
671     return;
672
673   if ( myOperationId == RemoveLandCoverId || myOperationId == MergeLandCoverId || myOperationId == ChangeLandCoverTypeId )
674     aPanel->setApplyEnabled( false );
675
676   if ( myOperationId == RemoveLandCoverId || 
677        myOperationId == MergeLandCoverId ||
678        myOperationId == ChangeLandCoverTypeId )
679   {
680     OCCViewer_ViewManager* aViewManager = getPreviewManager();
681     if ( aViewManager )
682     {
683       if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
684       {
685         disconnect(aViewManager, SIGNAL(mousePress(SUIT_ViewWindow*, QMouseEvent*)), 
686           this, SLOT(onMousePress(SUIT_ViewWindow*, QMouseEvent*)));
687         disconnect(aViewManager, SIGNAL(mouseRelease(SUIT_ViewWindow*, QMouseEvent*)),
688           this, SLOT(onMouseRelease(SUIT_ViewWindow*, QMouseEvent*)));
689         connect(aViewManager, SIGNAL(mousePress(SUIT_ViewWindow*, QMouseEvent*)), 
690           aViewer, SLOT(onMousePress(SUIT_ViewWindow*, QMouseEvent*)));
691         connect(aViewManager, SIGNAL(mouseRelease(SUIT_ViewWindow*, QMouseEvent*)), 
692           aViewer, SLOT(onMouseRelease(SUIT_ViewWindow*, QMouseEvent*)));  
693
694         LightApp_SelectionMgr* aSelectionMgr = module()->getApp()->selectionMgr();
695         if ( aSelectionMgr )
696         {
697           QList<SUIT_Selector*> aSelectorList;
698           aSelectionMgr->selectors( aViewManager->getType(), aSelectorList );
699           QList<SUIT_Selector*>::iterator anIter, anIterEnd = aSelectorList.end();
700           for( anIter = aSelectorList.begin(); anIter != anIterEnd; anIter++ )
701           {
702             HYDROGUI_OCCSelector* aHydroSelector = dynamic_cast<HYDROGUI_OCCSelector*>( *anIter );
703             if ( aHydroSelector )
704             {
705               disconnect( this, SIGNAL( deselection() ), aHydroSelector, SLOT( onDeselection() ) );
706               connect( aHydroSelector->viewer(), SIGNAL( deselection() ), aHydroSelector, SLOT( onDeselection() ) );            
707             }
708           }
709         }
710       }
711     }
712   }
713 }
714
715 Handle(AIS_InteractiveContext) HYDROGUI_LandCoverMapOp::getInteractiveContext()
716 {
717   OCCViewer_ViewManager* aViewManager = getPreviewManager();
718   Handle(AIS_InteractiveContext) aCtx = NULL;
719   if ( aViewManager ) {
720     if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() ) {
721       aCtx = aViewer->getAISContext();
722     }
723   }
724   return aCtx;
725 }
726
727 void HYDROGUI_LandCoverMapOp::getSelectedShapes( TopTools_ListOfShape& theSelectedShapes )
728 {
729   Handle(AIS_InteractiveContext) aCtx = getInteractiveContext();
730   if ( !aCtx.IsNull() )
731   {
732     for ( aCtx->InitSelected(); aCtx->MoreSelected(); aCtx->NextSelected() )
733     {
734       TopoDS_Shape aSelectedShape = aCtx->SelectedShape();
735       if ( aSelectedShape.IsNull() )
736         continue;
737
738       theSelectedShapes.Append( aSelectedShape );
739     }
740   }
741 }
742
743 int HYDROGUI_LandCoverMapOp::getNbSelected()
744 {
745   int aNbSelected = 0;
746
747   Handle(AIS_InteractiveContext) aCtx = getInteractiveContext();
748   if ( !aCtx.IsNull() )
749   {
750     for ( aCtx->InitSelected(); aCtx->MoreSelected(); aCtx->NextSelected() )
751     {
752       TopoDS_Shape aSelectedShape = aCtx->SelectedShape();
753       if ( aSelectedShape.IsNull() )
754         continue;
755       aNbSelected++;
756     }
757   }
758
759   return aNbSelected;
760 }
761
762 void HYDROGUI_LandCoverMapOp::selectLandCoverInPreview()
763 {
764   if ( myPreviewPrs && !myEditedObject.IsNull() && myEditedObject->GetLCCount() == 1 )
765   {
766     OCCViewer_ViewManager* aViewManager = getPreviewManager();
767     if ( !aViewManager )
768       return;
769     
770     Handle(AIS_InteractiveContext) aCtx = getInteractiveContext();
771     if ( aCtx.IsNull() )
772       return;
773     
774     OCCViewer_ViewWindow* aViewWindow = (OCCViewer_ViewWindow*)aViewManager->getActiveView();
775     if ( !aViewWindow )
776       return;
777
778     OCCViewer_ViewPort3d* aViewPort = aViewWindow->getViewPort();
779     if ( !aViewPort )
780       return;
781
782     Handle(V3d_View) aView = aViewPort->getView();
783     if ( aView.IsNull() )
784       return;
785
786     aCtx->ShiftSelect( 0, 0, aViewPort->width(), aViewPort->height(), aView, Standard_False );
787     aCtx->UpdateCurrentViewer();
788     emit selectionChanged();
789   }
790 }