Salome HOME
refs #1457
[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     anErrorMsg = aFailure->GetMessageString();
236     aResult = false;
237   }
238   catch ( ... )
239   {
240     aResult = false;
241   }
242   
243   QApplication::restoreOverrideCursor();
244
245   if ( aResult )
246   {
247     module()->update( anUpdateFlags );
248     commitDocOperation();
249     commit();
250     browseObjects( aBrowseObjectsEntries, myIsApplyAndClose );
251
252     if ( !myIsApplyAndClose && inputPanel() )
253       start();
254   }
255   else if( !anErrorMsg.isEmpty() )
256   {
257     // Abort document opeartion only if requested
258     if ( isToAbortOnApply() )
259       abortDocOperation();
260
261     printErrorMessage( anErrorMsg );
262  
263     // If the operation has no input panel - do abort
264     if ( !inputPanel() ) {
265       abort();
266     }
267   } 
268 }
269
270 void HYDROGUI_Operation::startOperation()
271 {
272   LightApp_Operation::startOperation();
273   myModule->getActiveOperations().push( this );
274
275   if( myIsApplyAndClose && inputPanel() )
276   {
277     myModule->getApp()->desktop()->addDockWidget( Qt::RightDockWidgetArea, inputPanel() );
278     inputPanel()->show();
279   }
280 }
281
282 void HYDROGUI_Operation::abortOperation()
283 {
284   LightApp_Operation::abortOperation();
285   closeInputPanel();
286 }
287
288 void HYDROGUI_Operation::commitOperation()
289 {
290   LightApp_Operation::commitOperation();
291   if ( myIsApplyAndClose )
292     closeInputPanel();
293 }
294
295 void HYDROGUI_Operation::stopOperation()
296 {
297   LightApp_Operation::stopOperation();
298
299   // pop the operation from the cached map of active operations
300   QStack<HYDROGUI_Operation*>& anOperations = myModule->getActiveOperations();
301   if ( !anOperations.empty() ) {
302     if ( anOperations.top() == this )
303       anOperations.pop();
304     else
305     {
306       // find in the stack the current operation and remove it from the stack
307       QVectorIterator<HYDROGUI_Operation*> aVIt( anOperations );
308       aVIt.toBack();
309       aVIt.previous(); // skip the top show/hide operation
310       while ( aVIt.hasPrevious() )
311       {
312         HYDROGUI_Operation* anOp = aVIt.previous();
313         if ( anOp == this )
314           anOperations.remove( anOperations.lastIndexOf( anOp ) );
315       }
316     }
317   }
318   // release the preview manager with removing the added preview Z layer
319   setPreviewManager( 0 );
320 }
321
322 void HYDROGUI_Operation::setDialogActive( const bool active )
323 {
324   LightApp_Operation::setDialogActive( active );
325   if( myPanel )
326   {
327     if( active )
328     {
329       myPanel->show();
330     }
331   }
332 }
333
334 HYDROGUI_InputPanel* HYDROGUI_Operation::createInputPanel() const
335 {
336   return NULL;
337 }
338
339 void HYDROGUI_Operation::closeInputPanel()
340 {
341   if( myPanel )
342   {
343     myModule->getApp()->desktop()->removeDockWidget( myPanel );
344     delete myPanel;
345     myPanel = 0;
346   }
347 }
348
349 bool HYDROGUI_Operation::processApply( int& theUpdateFlags,
350                                        QString& theErrorMsg,
351                                        QStringList& theBrowseObjectsEntries )
352 {
353   return false;
354 }
355
356 void HYDROGUI_Operation::processCancel()
357 {
358 }
359
360 void HYDROGUI_Operation::startDocOperation()
361 {
362   // Open transaction in the model document only if it not
363   // already opened by other operation (intended for nested operations)
364   if ( !doc()->IsOperation() )
365   {
366     doc()->StartOperation();
367     myIsTransactionOpened = true;
368   }
369 }
370
371 void HYDROGUI_Operation::abortDocOperation()
372 {
373   // Abort transaction in the model document only if it was 
374   // opened by this operation (intended for nested operations)
375   if ( myIsTransactionOpened && doc()->IsOperation() )
376   {
377     doc()->AbortOperation();
378     myIsTransactionOpened = false;
379   }
380 }
381
382 void HYDROGUI_Operation::commitDocOperation()
383 {
384   // Commit transaction in the model document only if it was 
385   // opened by this operation (intended for nested operations)
386   if ( myIsTransactionOpened && doc()->IsOperation() )
387   {
388     doc()->CommitOperation( HYDROGUI_Tool::ToExtString( getName() ) );
389     myIsTransactionOpened = false;
390   }
391 }
392
393 Handle(HYDROData_Document) HYDROGUI_Operation::doc() const
394 {
395   return HYDROData_Document::Document( myModule->getStudyId() );
396 }
397
398 void HYDROGUI_Operation::onApplyAndClose()
399 {
400   myIsApplyAndClose = true;
401   apply();
402 }
403
404 void HYDROGUI_Operation::onApply()
405 {
406   myIsApplyAndClose = false;
407   apply();
408 }
409
410 void HYDROGUI_Operation::setPrintErrorMessage( const bool theIsPrint )
411 {
412   myIsPrintErrorMessage = theIsPrint;
413 }
414
415 void HYDROGUI_Operation::printErrorMessage( const QString& theErrorMsg )
416 {
417   if ( myIsPrintErrorMessage )
418   {
419     QString aMsg = tr( "INPUT_VALID_DATA" );
420     if( !theErrorMsg.isEmpty() )
421       aMsg.prepend( theErrorMsg + "\n" );
422     SUIT_MessageBox::critical( module()->getApp()->desktop(),
423                                tr( "INSUFFICIENT_INPUT_DATA" ),
424                                aMsg );
425
426   }
427   
428   myIsPrintErrorMessage = true;
429 }
430
431 void HYDROGUI_Operation::onCancel()
432 {
433   processCancel();
434   abort();
435   if ( myPanel )
436     abortOperation();
437   myIsApplyAndClose = true;
438 }
439
440 void HYDROGUI_Operation::onHelp()
441 {
442    emit helpContextModule( getHelpComponent(), getHelpFile(), getHelpContext() );
443 }
444
445 QString HYDROGUI_Operation::getHelpComponent() const
446 {
447    return module()->moduleName();
448 }
449
450 QString HYDROGUI_Operation::getHelpFile() const
451 {
452   QString aFileName = ((myName.isEmpty())? operationName() : myName);
453   aFileName = aFileName.toLower();
454   aFileName = aFileName.replace(QRegExp("\\s"), "_").append(".html");
455   aFileName.remove( "create_" );
456   aFileName.remove( "edit_" );
457   return aFileName;
458 }
459
460 QString HYDROGUI_Operation::getHelpContext() const
461 {
462    return QString();
463 }
464
465 void HYDROGUI_Operation::browseObjects( const QStringList& theBrowseObjectsEntries,
466                                         const bool theIsApplyAndClose/* = true*/ )
467 {
468   bool isOptimizedBrowse = true;
469   module()->getApp()->browseObjects( theBrowseObjectsEntries, theIsApplyAndClose, isOptimizedBrowse );
470 }