Salome HOME
Refactoring. Hilight presentation on top.
[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 {
238   return false;
239 }
240
241 void HYDROGUI_Operation::processCancel()
242 {
243 }
244
245 void HYDROGUI_Operation::startDocOperation()
246 {
247   // Open transaction in the model document only if it not
248   // already opened by other operation (intended for nested operations)
249   if ( !doc()->IsOperation() )
250   {
251     doc()->StartOperation();
252     myIsTransactionOpened = true;
253   }
254 }
255
256 void HYDROGUI_Operation::abortDocOperation()
257 {
258   // Abort transaction in the model document only if it was 
259   // opened by this operation (intended for nested operations)
260   if ( myIsTransactionOpened && doc()->IsOperation() )
261   {
262     doc()->AbortOperation();
263     myIsTransactionOpened = false;
264   }
265 }
266
267 void HYDROGUI_Operation::commitDocOperation()
268 {
269   // Commit transaction in the model document only if it was 
270   // opened by this operation (intended for nested operations)
271   if ( myIsTransactionOpened && doc()->IsOperation() )
272   {
273     doc()->CommitOperation( HYDROGUI_Tool::ToExtString( getName() ) );
274     myIsTransactionOpened = false;
275   }
276 }
277
278 Handle_HYDROData_Document HYDROGUI_Operation::doc() const
279 {
280   return HYDROData_Document::Document( myModule->getStudyId() );
281 }
282
283 void HYDROGUI_Operation::onApply()
284 {
285   QApplication::setOverrideCursor( Qt::WaitCursor );
286
287   startDocOperation();
288
289   int anUpdateFlags = 0;
290   QString anErrorMsg;
291
292   bool aResult = false;
293   
294   try
295   {
296     aResult = processApply( anUpdateFlags, anErrorMsg );
297   }
298   catch ( Standard_Failure )
299   {
300     Handle(Standard_Failure) aFailure = Standard_Failure::Caught();
301     anErrorMsg = aFailure->GetMessageString();
302     aResult = false;
303   }
304   catch ( ... )
305   {
306     aResult = false;
307   }
308   
309   QApplication::restoreOverrideCursor();
310
311   if ( aResult )
312   {
313     module()->update( anUpdateFlags );
314     commitDocOperation();
315     commit();
316   }
317   else
318   {
319     // Abort document opeartion only if requested
320     if ( isToAbortOnApply() )
321       abortDocOperation();
322
323     printErrorMessage( anErrorMsg );
324  
325     // If the operation has no input panel - do abort
326     if ( !inputPanel() ) {
327       abort();
328     }
329   }
330 }
331
332 void HYDROGUI_Operation::setPrintErrorMessage( const bool theIsPrint )
333 {
334   myIsPrintErrorMessage = theIsPrint;
335 }
336
337 void HYDROGUI_Operation::printErrorMessage( const QString& theErrorMsg )
338 {
339   if ( myIsPrintErrorMessage )
340   {
341     QString aMsg = tr( "INPUT_VALID_DATA" );
342     if( !theErrorMsg.isEmpty() )
343       aMsg.prepend( theErrorMsg + "\n" );
344     SUIT_MessageBox::critical( module()->getApp()->desktop(),
345                                tr( "INSUFFICIENT_INPUT_DATA" ),
346                                aMsg );
347
348   }
349   
350   myIsPrintErrorMessage = true;
351 }
352
353 void HYDROGUI_Operation::onCancel()
354 {
355   processCancel();
356   abort();
357 }
358
359 void HYDROGUI_Operation::onHelp()
360 {
361    emit helpContextModule( getHelpComponent(), getHelpFile(), getHelpContext() );
362 }
363
364 QString HYDROGUI_Operation::getHelpComponent() const
365 {
366    return module()->moduleName();
367 }
368
369 QString HYDROGUI_Operation::getHelpFile() const
370 {
371    QString aFileName = ((myName.isEmpty())? operationName() : myName);
372    return aFileName.replace(QRegExp("\\s"), "_").append(".html");
373 }
374
375 QString HYDROGUI_Operation::getHelpContext() const
376 {
377    return QString();
378 }
379
380