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