Salome HOME
patch for crash in channel
[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.empty() ) {
186     if ( anOperations.top() == this )
187       anOperations.pop();
188     else
189     {
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   }
202   // release the preview manager with removing the added preview Z layer
203   setPreviewManager( 0 );
204 }
205
206 void HYDROGUI_Operation::setDialogActive( const bool active )
207 {
208   LightApp_Operation::setDialogActive( active );
209   if( myPanel )
210   {
211     if( active )
212     {
213       myPanel->show();
214     }
215   }
216 }
217
218 HYDROGUI_InputPanel* HYDROGUI_Operation::createInputPanel() const
219 {
220   return NULL;
221 }
222
223 void HYDROGUI_Operation::closeInputPanel()
224 {
225   if( myPanel )
226   {
227     myModule->getApp()->desktop()->removeDockWidget( myPanel );
228     delete myPanel;
229     myPanel = 0;
230   }
231 }
232
233 bool HYDROGUI_Operation::processApply( int& theUpdateFlags,
234                                        QString& theErrorMsg )
235 {
236   return false;
237 }
238
239 void HYDROGUI_Operation::processCancel()
240 {
241 }
242
243 void HYDROGUI_Operation::startDocOperation()
244 {
245   // Open transaction in the model document
246   if ( !doc()->IsOperation() )
247     doc()->StartOperation();
248 }
249
250 void HYDROGUI_Operation::abortDocOperation()
251 {
252   // Abort transaction in the model document
253   if ( doc()->IsOperation() )
254     doc()->AbortOperation();
255 }
256
257 void HYDROGUI_Operation::commitDocOperation()
258 {
259   // Commit transaction in the model document
260   if ( doc()->IsOperation() )
261     doc()->CommitOperation( HYDROGUI_Tool::ToExtString( getName() ) );
262 }
263
264 Handle_HYDROData_Document HYDROGUI_Operation::doc() const
265 {
266   return HYDROData_Document::Document( myModule->getStudyId() );
267 }
268
269 void HYDROGUI_Operation::onApply()
270 {
271   QApplication::setOverrideCursor( Qt::WaitCursor );
272
273   startDocOperation();
274
275   int anUpdateFlags = 0;
276   QString anErrorMsg;
277
278   bool aResult = false;
279   
280   try
281   {
282     aResult = processApply( anUpdateFlags, anErrorMsg );
283   }
284   catch ( Standard_Failure )
285   {
286     Handle(Standard_Failure) aFailure = Standard_Failure::Caught();
287     anErrorMsg = aFailure->GetMessageString();
288     aResult = false;
289   }
290   catch ( ... )
291   {
292     aResult = false;
293   }
294   
295   QApplication::restoreOverrideCursor();
296
297   if ( aResult )
298   {
299     module()->update( anUpdateFlags );
300     commitDocOperation();
301     commit();
302   }
303   else
304   {
305     // Abort document opeartion only if requested
306     if ( isToAbortOnApply() )
307       abortDocOperation();
308
309     printErrorMessage( anErrorMsg );
310  
311     // If the operation has no input panel - do abort
312     if ( !inputPanel() ) {
313       abort();
314     }
315   }
316 }
317
318 void HYDROGUI_Operation::setPrintErrorMessage( const bool theIsPrint )
319 {
320   myIsPrintErrorMessage = theIsPrint;
321 }
322
323 void HYDROGUI_Operation::printErrorMessage( const QString& theErrorMsg )
324 {
325   if ( myIsPrintErrorMessage )
326   {
327     QString aMsg = tr( "INPUT_VALID_DATA" );
328     if( !theErrorMsg.isEmpty() )
329       aMsg.prepend( theErrorMsg + "\n" );
330     SUIT_MessageBox::critical( module()->getApp()->desktop(),
331                                tr( "INSUFFICIENT_INPUT_DATA" ),
332                                aMsg );
333
334   }
335   
336   myIsPrintErrorMessage = true;
337 }
338
339 void HYDROGUI_Operation::onCancel()
340 {
341   processCancel();
342   abort();
343 }
344
345 void HYDROGUI_Operation::onHelp()
346 {
347    emit helpContextModule( getHelpComponent(), getHelpFile(), getHelpContext() );
348 }
349
350 QString HYDROGUI_Operation::getHelpComponent() const
351 {
352    return module()->moduleName();
353 }
354
355 QString HYDROGUI_Operation::getHelpFile() const
356 {
357    QString aFileName = ((myName.isEmpty())? operationName() : myName);
358    return aFileName.replace(QRegExp("\\s"), "_").append(".html");
359 }
360
361 QString HYDROGUI_Operation::getHelpContext() const
362 {
363    return QString();
364 }
365
366