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