Salome HOME
trying to highlight selected points on bathymetry
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_Operation.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_Operation.h"
20
21 #include "HYDROGUI_InputPanel.h"
22 #include "HYDROGUI_Module.h"
23 #include "HYDROGUI_Tool.h"
24 #include "HYDROGUI_OCCDisplayer.h"
25 #include "HYDROGUI_Shape.h"
26
27 #include <HYDROData_Document.h>
28 #include <HYDROData_Iterator.h>
29
30 #include <LightApp_Application.h>
31 #include <LightApp_SelectionMgr.h>
32
33 #include <SUIT_Desktop.h>
34 #include <SUIT_MessageBox.h>
35 #include <SUIT_Study.h>
36
37 #include <OCCViewer_ViewManager.h>
38 #include <OCCViewer_ViewWindow.h>
39 #include <OCCViewer_ViewPort3d.h>
40
41 #include <QApplication>
42
43 #define _DEVDEBUG_
44 #include "HYDRO_trace.hxx"
45
46 HYDROGUI_Operation::HYDROGUI_Operation( HYDROGUI_Module* theModule )
47 : LightApp_Operation(),
48   myModule( theModule ),
49   myPanel( 0 ),
50   myIsPrintErrorMessage( true ),
51   myIsTransactionOpened( false ),
52   myPreviewManager( 0 ),
53   myPreviewZLayer( -1 ),
54   myIsApplyAndClose( true )
55 {
56   connect( this, SIGNAL( helpContextModule( const QString&, const QString&,
57                                             const QString& ) ),
58            theModule->application(), SLOT( onHelpContextModule( const QString&,
59                                             const QString&, const QString& ) ) );
60 }
61
62 HYDROGUI_Operation::~HYDROGUI_Operation()
63 {
64 }
65
66 void HYDROGUI_Operation::setName( const QString& theName )
67 {
68   myName = theName;
69 }
70
71 const QString& HYDROGUI_Operation::getName() const
72 {
73   return myName;
74 }
75
76 HYDROGUI_InputPanel* HYDROGUI_Operation::inputPanel() const
77 {
78   DEBTRACE("inputPanel");
79   if( !myPanel )
80   {
81     ( ( HYDROGUI_Operation* )this )->myPanel = createInputPanel();
82     if (myPanel)
83     {
84                 connect( myPanel, SIGNAL( panelApplyAndClose() ), this, SLOT( onApplyAndClose() ) );
85                 connect( myPanel, SIGNAL( panelApply() ),  this, SLOT( onApply() ) );
86                 connect( myPanel, SIGNAL( panelCancel() ), this, SLOT( onCancel() ) );
87                 connect( myPanel, SIGNAL( panelHelp() ), this, SLOT( onHelp() ) );
88     }
89   }
90   return myPanel;
91 }
92
93 SUIT_SelectionMgr* HYDROGUI_Operation::selectionMgr() const
94 {
95   return myModule->getApp()->selectionMgr();
96 }
97
98 HYDROGUI_Module* HYDROGUI_Operation::module() const
99 {
100   return myModule;
101 }
102
103 /**
104   * Returns Z layer of the operation preview.
105    \ returns a layer position
106  */
107 int HYDROGUI_Operation::getPreviewZLayer() const
108 {
109   return myPreviewZLayer;
110 }
111
112 /**
113  * Update Z layer for the operation preview.
114    \param theLayer a layer position
115  */
116 void HYDROGUI_Operation::updatePreviewZLayer( int theLayer )
117 {
118   setPreviewZLayer( theLayer );
119
120   HYDROGUI_Shape* aPreview = getPreviewShape();
121   if ( aPreview )
122     aPreview->setZLayer( getPreviewZLayer() );
123 }
124
125 /**
126  * Set Z layer for the operation preview.
127  \param theLayer a layer position
128  */
129 void HYDROGUI_Operation::setPreviewZLayer( int theLayer )
130 {
131   if ( theLayer != myPreviewZLayer )
132     myPreviewZLayer = theLayer;
133 }
134
135 /**
136  * Returns a shape preview of the operation
137  */
138 HYDROGUI_Shape* HYDROGUI_Operation::getPreviewShape() const
139 {
140   return 0;
141 }
142
143 /**
144  * Return the operation preview manager
145  */
146 OCCViewer_ViewManager* HYDROGUI_Operation::getPreviewManager()
147 {
148   return myPreviewManager;
149 }
150
151 /**
152  * Set the preview manager
153  */
154 void HYDROGUI_Operation::setPreviewManager( OCCViewer_ViewManager* theManager )
155 {
156   //No good: preview Z layer could be used by usual presentations
157   //if ( !theManager && myPreviewManager )
158   //  module()->getOCCDisplayer()->RemoveZLayer( myPreviewManager, getPreviewZLayer() );
159
160   myPreviewManager = theManager;
161
162   if ( myPreviewManager )
163     setPreviewZLayer( module()->getOCCDisplayer()->AddPreviewZLayer( myPreviewManager ) );
164 }
165
166 void HYDROGUI_Operation::setCursor()
167 {
168   if ( myPreviewManager )
169   {
170     QVector<SUIT_ViewWindow*> winList = myPreviewManager->getViews();
171     for ( QVector<SUIT_ViewWindow*>::iterator it = winList.begin(); it != winList.end(); ++it )
172     {
173       OCCViewer_ViewWindow* occWin = ::qobject_cast<OCCViewer_ViewWindow*>( *it );
174       if ( occWin )
175       {
176         OCCViewer_ViewPort3d* vp = occWin->getViewPort();
177         if ( vp )
178         {
179           // Save old cursor
180           myCursor = vp->cursor();
181           // Set specific cursor chosen in preferences
182           QCursor aCursor = module()->getPrefEditCursor();
183           vp->setDefaultCursor( aCursor.shape() );
184           vp->setCursor( *vp->getDefaultCursor() );
185         }
186       }
187     }
188   }
189 }
190
191 void HYDROGUI_Operation::restoreCursor()
192 {
193   if ( myPreviewManager )
194   {
195     QVector<SUIT_ViewWindow*> winList = myPreviewManager->getViews();
196     for ( QVector<SUIT_ViewWindow*>::iterator it = winList.begin(); it != winList.end(); ++it )
197     {
198       OCCViewer_ViewWindow* occWin = ::qobject_cast<OCCViewer_ViewWindow*>( *it );
199       if ( occWin )
200       {
201         OCCViewer_ViewPort3d* vp = occWin->getViewPort();
202         if ( vp )
203         {
204           // Restore old cursor
205           vp->setDefaultCursor( myCursor.shape() );
206           vp->setCursor( myCursor );
207         }
208       }
209     }
210   }
211 }
212
213 void HYDROGUI_Operation::setIsApplyAndClose( const bool theFlag )
214 {
215   myIsApplyAndClose = theFlag;
216 }
217
218 bool HYDROGUI_Operation::isApplyAndClose() const
219 {
220   return myIsApplyAndClose;
221 }
222
223 void HYDROGUI_Operation::apply()
224 {
225   QApplication::setOverrideCursor( Qt::WaitCursor );
226
227   startDocOperation();
228
229   int anUpdateFlags = 0;
230   QString anErrorMsg;
231
232   bool aResult = false;
233   QStringList aBrowseObjectsEntries;
234
235   try
236   {
237     aResult = processApply( anUpdateFlags, anErrorMsg, aBrowseObjectsEntries );
238   }
239   catch ( Standard_Failure )
240   {
241     Handle(Standard_Failure) aFailure = Standard_Failure::Caught();
242     if (aFailure)
243       anErrorMsg = aFailure->GetMessageString();
244     else
245       anErrorMsg = "failure unknown: catch ( Standard_Failure ) does not give access to failure!";
246     aResult = false;
247   }
248   catch ( ... )
249   {
250     aResult = false;
251   }
252   
253   QApplication::restoreOverrideCursor();
254
255   if ( aResult )
256   {
257     module()->update( anUpdateFlags );
258     commitDocOperation();
259     commit();
260     browseObjects( aBrowseObjectsEntries, myIsApplyAndClose );
261
262     if ( !myIsApplyAndClose && inputPanel() )
263       start();
264   }
265   else if( !anErrorMsg.isEmpty() )
266   {
267     // Abort document opeartion only if requested
268     if ( isToAbortOnApply() )
269       abortDocOperation();
270
271     printErrorMessage( anErrorMsg );
272  
273     // If the operation has no input panel - do abort
274     if ( !inputPanel() ) {
275       abort();
276     }
277   } 
278 }
279
280 void HYDROGUI_Operation::startOperation()
281 {
282   LightApp_Operation::startOperation();
283   myModule->getActiveOperations().push( this );
284
285   if( myIsApplyAndClose && inputPanel() )
286   {
287     myModule->getApp()->desktop()->addDockWidget( Qt::RightDockWidgetArea, inputPanel() );
288     inputPanel()->show();
289   }
290 }
291
292 void HYDROGUI_Operation::abortOperation()
293 {
294   LightApp_Operation::abortOperation();
295   closeInputPanel();
296 }
297
298 void HYDROGUI_Operation::commitOperation()
299 {
300   LightApp_Operation::commitOperation();
301   if ( myIsApplyAndClose )
302     closeInputPanel();
303 }
304
305 void HYDROGUI_Operation::stopOperation()
306 {
307   LightApp_Operation::stopOperation();
308
309   // pop the operation from the cached map of active operations
310   QStack<HYDROGUI_Operation*>& anOperations = myModule->getActiveOperations();
311   if ( !anOperations.empty() ) {
312     if ( anOperations.top() == this )
313       anOperations.pop();
314     else
315     {
316       // find in the stack the current operation and remove it from the stack
317       QVectorIterator<HYDROGUI_Operation*> aVIt( anOperations );
318       aVIt.toBack();
319       aVIt.previous(); // skip the top show/hide operation
320       while ( aVIt.hasPrevious() )
321       {
322         HYDROGUI_Operation* anOp = aVIt.previous();
323         if ( anOp == this )
324           anOperations.remove( anOperations.lastIndexOf( anOp ) );
325       }
326     }
327   }
328   // release the preview manager with removing the added preview Z layer
329   setPreviewManager( 0 );
330 }
331
332 void HYDROGUI_Operation::setDialogActive( const bool active )
333 {
334   LightApp_Operation::setDialogActive( active );
335   if( myPanel )
336   {
337     if( active )
338     {
339       myPanel->show();
340     }
341   }
342 }
343
344 HYDROGUI_InputPanel* HYDROGUI_Operation::createInputPanel() const
345 {
346   return NULL;
347 }
348
349 void HYDROGUI_Operation::closeInputPanel()
350 {
351   if( myPanel )
352   {
353     myModule->getApp()->desktop()->removeDockWidget( myPanel );
354     delete myPanel;
355     myPanel = 0;
356   }
357 }
358
359 bool HYDROGUI_Operation::processApply( int& theUpdateFlags,
360                                        QString& theErrorMsg,
361                                        QStringList& theBrowseObjectsEntries )
362 {
363   return false;
364 }
365
366 void HYDROGUI_Operation::processCancel()
367 {
368 }
369
370 void HYDROGUI_Operation::startDocOperation()
371 {
372   // Open transaction in the model document only if it not
373   // already opened by other operation (intended for nested operations)
374   if ( !doc()->IsOperation() )
375   {
376     doc()->StartOperation();
377     myIsTransactionOpened = true;
378   }
379 }
380
381 void HYDROGUI_Operation::abortDocOperation()
382 {
383   // Abort transaction in the model document only if it was 
384   // opened by this operation (intended for nested operations)
385   if ( myIsTransactionOpened && doc()->IsOperation() )
386   {
387     doc()->AbortOperation();
388     myIsTransactionOpened = false;
389   }
390 }
391
392 void HYDROGUI_Operation::commitDocOperation()
393 {
394   // Commit transaction in the model document only if it was 
395   // opened by this operation (intended for nested operations)
396   if ( myIsTransactionOpened && doc()->IsOperation() )
397   {
398     doc()->CommitOperation( HYDROGUI_Tool::ToExtString( getName() ) );
399     myIsTransactionOpened = false;
400   }
401 }
402
403 Handle(HYDROData_Document) HYDROGUI_Operation::doc() const
404 {
405   return HYDROData_Document::Document();
406 }
407
408 void HYDROGUI_Operation::onApplyAndClose()
409 {
410   myIsApplyAndClose = true;
411   apply();
412   restoreOCCViewerSelection();
413 }
414
415 void HYDROGUI_Operation::onApply()
416 {
417   myIsApplyAndClose = false;
418   apply();
419   restoreOCCViewerSelection();
420 }
421
422 void HYDROGUI_Operation::setPrintErrorMessage( const bool theIsPrint )
423 {
424   myIsPrintErrorMessage = theIsPrint;
425 }
426
427 void HYDROGUI_Operation::printErrorMessage( const QString& theErrorMsg )
428 {
429   if ( myIsPrintErrorMessage )
430   {
431     QString aMsg = tr( "INPUT_VALID_DATA" );
432     if( !theErrorMsg.isEmpty() )
433       aMsg.prepend( theErrorMsg + "\n" );
434     SUIT_MessageBox::critical( module()->getApp()->desktop(),
435                                tr( "INSUFFICIENT_INPUT_DATA" ),
436                                aMsg );
437
438   }
439   
440   myIsPrintErrorMessage = true;
441 }
442
443 void HYDROGUI_Operation::onCancel()
444 {
445   processCancel();
446   abort();
447   if ( myPanel )
448     abortOperation();
449   myIsApplyAndClose = true;
450   restoreOCCViewerSelection();
451 }
452
453 void HYDROGUI_Operation::restoreOCCViewerSelection()
454 {
455   DEBTRACE("restoreOCCViewerSelection");
456   LightApp_Application* anApp = module()->getApp();
457   LightApp_SelectionMgr* sm = anApp->selectionMgr();
458   if(sm)
459   {
460           sm->clearFilters(); // see GEOM_Displayer::GlobalSelection
461   }
462   OCCViewer_ViewManager* aViewManager =
463       dynamic_cast<OCCViewer_ViewManager*>( anApp->getViewManager( OCCViewer_Viewer::Type(), true ) );
464   if( aViewManager )
465   {
466         if( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
467         {
468           Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
469           if( !aCtx.IsNull() )
470           {
471                   DEBTRACE("AIScontext " << aCtx.get());
472                   aCtx->Deactivate();
473                   aCtx->Activate(0);
474                   aCtx->SetAutomaticHilight( Standard_True );
475                   aCtx->RemoveFilters();
476           }
477         }
478   }
479 }
480 void HYDROGUI_Operation::onHelp()
481 {
482    emit helpContextModule( getHelpComponent(), getHelpFile(), getHelpContext() );
483 }
484
485 QString HYDROGUI_Operation::getHelpComponent() const
486 {
487    return module()->moduleName();
488 }
489
490 QString HYDROGUI_Operation::getHelpFile() const
491 {
492   QString aFileName = ((myName.isEmpty())? operationName() : myName);
493   aFileName = aFileName.toLower();
494   aFileName = aFileName.replace(QRegExp("\\s"), "_").append(".html");
495   aFileName.remove( "create_" );
496   aFileName.remove( "edit_" );
497   return aFileName;
498 }
499
500 QString HYDROGUI_Operation::getHelpContext() const
501 {
502    return QString();
503 }
504
505 void HYDROGUI_Operation::browseObjects( const QStringList& theBrowseObjectsEntries,
506                                         const bool theIsApplyAndClose/* = true*/ )
507 {
508   bool isOptimizedBrowse = true;
509   module()->getApp()->browseObjects( theBrowseObjectsEntries, theIsApplyAndClose, isOptimizedBrowse );
510 }