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