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