Salome HOME
Merge branch 'BR_IMPROVEMENTS' of ssh://git.salome-platform.org/modules/hydro into...
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_Operation.cxx
1 // Copyright (C) 2007-2015  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "HYDROGUI_Operation.h"
24
25 #include "HYDROGUI_InputPanel.h"
26 #include "HYDROGUI_Module.h"
27 #include "HYDROGUI_Tool.h"
28 #include "HYDROGUI_OCCDisplayer.h"
29 #include "HYDROGUI_Shape.h"
30
31 #include <HYDROData_Document.h>
32 #include <HYDROData_Iterator.h>
33
34 #include <LightApp_Application.h>
35 #include <LightApp_SelectionMgr.h>
36
37 #include <SUIT_Desktop.h>
38 #include <SUIT_MessageBox.h>
39 #include <SUIT_Study.h>
40
41 #include <OCCViewer_ViewManager.h>
42 #include <OCCViewer_ViewWindow.h>
43 #include <OCCViewer_ViewPort3d.h>
44
45 #include <QApplication>
46
47 HYDROGUI_Operation::HYDROGUI_Operation( HYDROGUI_Module* theModule )
48 : LightApp_Operation(),
49   myModule( theModule ),
50   myPanel( 0 ),
51   myIsPrintErrorMessage( true ),
52   myIsTransactionOpened( false ),
53   myPreviewManager( 0 ),
54   myPreviewZLayer( -1 ),
55   myIsApplyAndClose( true )
56 {
57   connect( this, SIGNAL( helpContextModule( const QString&, const QString&,
58                                             const QString& ) ),
59            theModule->application(), SLOT( onHelpContextModule( const QString&,
60                                             const QString&, const QString& ) ) );
61 }
62
63 HYDROGUI_Operation::~HYDROGUI_Operation()
64 {
65 }
66
67 void HYDROGUI_Operation::setName( const QString& theName )
68 {
69   myName = theName;
70 }
71
72 const QString& HYDROGUI_Operation::getName() const
73 {
74   return myName;
75 }
76
77 HYDROGUI_InputPanel* HYDROGUI_Operation::inputPanel() const
78 {
79   if( !myPanel )
80   {
81     ( ( HYDROGUI_Operation* )this )->myPanel = createInputPanel();
82     connect( myPanel, SIGNAL( panelApplyAndClose() ), this, SLOT( onApplyAndClose() ) );
83     connect( myPanel, SIGNAL( panelApply() ),  this, SLOT( onApply() ) );
84     connect( myPanel, SIGNAL( panelCancel() ), this, SLOT( onCancel() ) );
85     connect( myPanel, SIGNAL( panelHelp() ), this, SLOT( onHelp() ) );
86   }
87   return myPanel;
88 }
89
90 SUIT_SelectionMgr* HYDROGUI_Operation::selectionMgr() const
91 {
92   return myModule->getApp()->selectionMgr();
93 }
94
95 HYDROGUI_Module* HYDROGUI_Operation::module() const
96 {
97   return myModule;
98 }
99
100 /**
101   * Returns Z layer of the operation preview.
102    \ returns a layer position
103  */
104 int HYDROGUI_Operation::getPreviewZLayer() const
105 {
106   return myPreviewZLayer;
107 }
108
109 /**
110  * Update Z layer for the operation preview.
111    \param theLayer a layer position
112  */
113 void HYDROGUI_Operation::updatePreviewZLayer( int theLayer )
114 {
115   setPreviewZLayer( theLayer );
116
117   HYDROGUI_Shape* aPreview = getPreviewShape();
118   if ( aPreview )
119     aPreview->setZLayer( getPreviewZLayer() );
120 }
121
122 /**
123  * Set Z layer for the operation preview.
124  \param theLayer a layer position
125  */
126 void HYDROGUI_Operation::setPreviewZLayer( int theLayer )
127 {
128   if ( theLayer != myPreviewZLayer )
129     myPreviewZLayer = theLayer;
130 }
131
132 /**
133  * Returns a shape preview of the operation
134  */
135 HYDROGUI_Shape* HYDROGUI_Operation::getPreviewShape() const
136 {
137   return 0;
138 }
139
140 /**
141  * Return the operation preview manager
142  */
143 OCCViewer_ViewManager* HYDROGUI_Operation::getPreviewManager()
144 {
145   return myPreviewManager;
146 }
147
148 /**
149  * Set the preview manager
150  */
151 void HYDROGUI_Operation::setPreviewManager( OCCViewer_ViewManager* theManager )
152 {
153   //No good: preview Z layer could be used by usual presentations
154   //if ( !theManager && myPreviewManager )
155   //  module()->getOCCDisplayer()->RemoveZLayer( myPreviewManager, getPreviewZLayer() );
156
157   myPreviewManager = theManager;
158
159   if ( myPreviewManager )
160     setPreviewZLayer( module()->getOCCDisplayer()->AddPreviewZLayer( myPreviewManager ) );
161 }
162
163 void HYDROGUI_Operation::setCursor()
164 {
165   if ( myPreviewManager )
166   {
167     QVector<SUIT_ViewWindow*> winList = myPreviewManager->getViews();
168     for ( QVector<SUIT_ViewWindow*>::iterator it = winList.begin(); it != winList.end(); ++it )
169     {
170       OCCViewer_ViewWindow* occWin = ::qobject_cast<OCCViewer_ViewWindow*>( *it );
171       if ( occWin )
172       {
173         OCCViewer_ViewPort3d* vp = occWin->getViewPort();
174         if ( vp )
175         {
176           // Save old cursor
177           myCursor = vp->cursor();
178           // Set specific cursor chosen in preferences
179           QCursor aCursor = module()->getPrefEditCursor();
180           vp->setDefaultCursor( aCursor.shape() );
181           vp->setCursor( *vp->getDefaultCursor() );
182         }
183       }
184     }
185   }
186 }
187
188 void HYDROGUI_Operation::restoreCursor()
189 {
190   if ( myPreviewManager )
191   {
192     QVector<SUIT_ViewWindow*> winList = myPreviewManager->getViews();
193     for ( QVector<SUIT_ViewWindow*>::iterator it = winList.begin(); it != winList.end(); ++it )
194     {
195       OCCViewer_ViewWindow* occWin = ::qobject_cast<OCCViewer_ViewWindow*>( *it );
196       if ( occWin )
197       {
198         OCCViewer_ViewPort3d* vp = occWin->getViewPort();
199         if ( vp )
200         {
201           // Restore old cursor
202           vp->setDefaultCursor( myCursor.shape() );
203           vp->setCursor( myCursor );
204         }
205       }
206     }
207   }
208 }
209
210 void HYDROGUI_Operation::setIsApplyAndClose( const bool theFlag )
211 {
212   myIsApplyAndClose = theFlag;
213 }
214
215 bool HYDROGUI_Operation::isApplyAndClose() const
216 {
217   return myIsApplyAndClose;
218 }
219
220 void HYDROGUI_Operation::apply()
221 {
222   QApplication::setOverrideCursor( Qt::WaitCursor );
223
224   startDocOperation();
225
226   int anUpdateFlags = 0;
227   QString anErrorMsg;
228
229   bool aResult = false;
230   QStringList aBrowseObjectsEntries;
231
232   try
233   {
234     aResult = processApply( anUpdateFlags, anErrorMsg, aBrowseObjectsEntries );
235   }
236   catch ( Standard_Failure )
237   {
238     Handle(Standard_Failure) aFailure = Standard_Failure::Caught();
239     anErrorMsg = aFailure->GetMessageString();
240     aResult = false;
241   }
242   catch ( ... )
243   {
244     aResult = false;
245   }
246   
247   QApplication::restoreOverrideCursor();
248
249   if ( aResult )
250   {
251     module()->update( anUpdateFlags );
252     commitDocOperation();
253     commit();
254     browseObjects( aBrowseObjectsEntries, myIsApplyAndClose );
255
256     if ( !myIsApplyAndClose && inputPanel() )
257       start();
258   }
259   else
260   {
261     // Abort document opeartion only if requested
262     if ( isToAbortOnApply() )
263       abortDocOperation();
264
265     printErrorMessage( anErrorMsg );
266  
267     // If the operation has no input panel - do abort
268     if ( !inputPanel() ) {
269       abort();
270     }
271   } 
272 }
273
274 void HYDROGUI_Operation::startOperation()
275 {
276   LightApp_Operation::startOperation();
277   myModule->getActiveOperations().push( this );
278
279   if( myIsApplyAndClose && inputPanel() )
280   {
281     myModule->getApp()->desktop()->addDockWidget( Qt::RightDockWidgetArea, inputPanel() );
282     inputPanel()->show();
283   }
284 }
285
286 void HYDROGUI_Operation::abortOperation()
287 {
288   LightApp_Operation::abortOperation();
289   closeInputPanel();
290 }
291
292 void HYDROGUI_Operation::commitOperation()
293 {
294   LightApp_Operation::commitOperation();
295   if ( myIsApplyAndClose )
296     closeInputPanel();
297 }
298
299 void HYDROGUI_Operation::stopOperation()
300 {
301   LightApp_Operation::stopOperation();
302
303   // pop the operation from the cached map of active operations
304   QStack<HYDROGUI_Operation*>& anOperations = myModule->getActiveOperations();
305   if ( !anOperations.empty() ) {
306     if ( anOperations.top() == this )
307       anOperations.pop();
308     else
309     {
310       // find in the stack the current operation and remove it from the stack
311       QVectorIterator<HYDROGUI_Operation*> aVIt( anOperations );
312       aVIt.toBack();
313       aVIt.previous(); // skip the top show/hide operation
314       while ( aVIt.hasPrevious() )
315       {
316         HYDROGUI_Operation* anOp = aVIt.previous();
317         if ( anOp == this )
318           anOperations.remove( anOperations.lastIndexOf( anOp ) );
319       }
320     }
321   }
322   // release the preview manager with removing the added preview Z layer
323   setPreviewManager( 0 );
324 }
325
326 void HYDROGUI_Operation::setDialogActive( const bool active )
327 {
328   LightApp_Operation::setDialogActive( active );
329   if( myPanel )
330   {
331     if( active )
332     {
333       myPanel->show();
334     }
335   }
336 }
337
338 HYDROGUI_InputPanel* HYDROGUI_Operation::createInputPanel() const
339 {
340   return NULL;
341 }
342
343 void HYDROGUI_Operation::closeInputPanel()
344 {
345   if( myPanel )
346   {
347     myModule->getApp()->desktop()->removeDockWidget( myPanel );
348     delete myPanel;
349     myPanel = 0;
350   }
351 }
352
353 bool HYDROGUI_Operation::processApply( int& theUpdateFlags,
354                                        QString& theErrorMsg,
355                                        QStringList& theBrowseObjectsEntries )
356 {
357   return false;
358 }
359
360 void HYDROGUI_Operation::processCancel()
361 {
362 }
363
364 void HYDROGUI_Operation::startDocOperation()
365 {
366   // Open transaction in the model document only if it not
367   // already opened by other operation (intended for nested operations)
368   if ( !doc()->IsOperation() )
369   {
370     doc()->StartOperation();
371     myIsTransactionOpened = true;
372   }
373 }
374
375 void HYDROGUI_Operation::abortDocOperation()
376 {
377   // Abort transaction in the model document only if it was 
378   // opened by this operation (intended for nested operations)
379   if ( myIsTransactionOpened && doc()->IsOperation() )
380   {
381     doc()->AbortOperation();
382     myIsTransactionOpened = false;
383   }
384 }
385
386 void HYDROGUI_Operation::commitDocOperation()
387 {
388   // Commit transaction in the model document only if it was 
389   // opened by this operation (intended for nested operations)
390   if ( myIsTransactionOpened && doc()->IsOperation() )
391   {
392     doc()->CommitOperation( HYDROGUI_Tool::ToExtString( getName() ) );
393     myIsTransactionOpened = false;
394   }
395 }
396
397 Handle_HYDROData_Document HYDROGUI_Operation::doc() const
398 {
399   return HYDROData_Document::Document( myModule->getStudyId() );
400 }
401
402 void HYDROGUI_Operation::onApplyAndClose()
403 {
404   myIsApplyAndClose = true;
405   apply();
406 }
407
408 void HYDROGUI_Operation::onApply()
409 {
410   myIsApplyAndClose = false;
411   apply();
412 }
413
414 void HYDROGUI_Operation::setPrintErrorMessage( const bool theIsPrint )
415 {
416   myIsPrintErrorMessage = theIsPrint;
417 }
418
419 void HYDROGUI_Operation::printErrorMessage( const QString& theErrorMsg )
420 {
421   if ( myIsPrintErrorMessage )
422   {
423     QString aMsg = tr( "INPUT_VALID_DATA" );
424     if( !theErrorMsg.isEmpty() )
425       aMsg.prepend( theErrorMsg + "\n" );
426     SUIT_MessageBox::critical( module()->getApp()->desktop(),
427                                tr( "INSUFFICIENT_INPUT_DATA" ),
428                                aMsg );
429
430   }
431   
432   myIsPrintErrorMessage = true;
433 }
434
435 void HYDROGUI_Operation::onCancel()
436 {
437   processCancel();
438   abort();
439   if ( myPanel )
440     abortOperation();
441   myIsApplyAndClose = true;
442 }
443
444 void HYDROGUI_Operation::onHelp()
445 {
446    emit helpContextModule( getHelpComponent(), getHelpFile(), getHelpContext() );
447 }
448
449 QString HYDROGUI_Operation::getHelpComponent() const
450 {
451    return module()->moduleName();
452 }
453
454 QString HYDROGUI_Operation::getHelpFile() const
455 {
456    QString aFileName = ((myName.isEmpty())? operationName() : myName);
457    return aFileName.replace(QRegExp("\\s"), "_").append(".html");
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 }