Salome HOME
Add tests for drag and drop algo.
[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   if ( !theManager && myPreviewManager )
148     module()->getOCCDisplayer()->RemoveZLayer( myPreviewManager, getPreviewZLayer() );
149
150   myPreviewManager = theManager;
151
152   if ( myPreviewManager )
153     setPreviewZLayer( module()->getOCCDisplayer()->AddTopZLayer( myPreviewManager ) );
154 }
155
156 void HYDROGUI_Operation::startOperation()
157 {
158   LightApp_Operation::startOperation();
159   myModule->getActiveOperations().push( this );
160
161   if( inputPanel() )
162   {
163     myModule->getApp()->desktop()->addDockWidget( Qt::RightDockWidgetArea, inputPanel() );
164     inputPanel()->show();
165   }
166 }
167
168 void HYDROGUI_Operation::abortOperation()
169 {
170   LightApp_Operation::abortOperation();
171   closeInputPanel();
172 }
173
174 void HYDROGUI_Operation::commitOperation()
175 {
176   LightApp_Operation::commitOperation();
177   closeInputPanel();
178 }
179
180 void HYDROGUI_Operation::stopOperation()
181 {
182   LightApp_Operation::stopOperation();
183
184   // pop the operation from the cached map of active operations
185   QStack<HYDROGUI_Operation*>& anOperations = myModule->getActiveOperations();
186   if ( !anOperations.empty() ) {
187     if ( anOperations.top() == this )
188       anOperations.pop();
189     else
190     {
191       // find in the stack the current operation and remove it from the stack
192       QVectorIterator<HYDROGUI_Operation*> aVIt( anOperations );
193       aVIt.toBack();
194       aVIt.previous(); // skip the top show/hide operation
195       while ( aVIt.hasPrevious() )
196       {
197         HYDROGUI_Operation* anOp = aVIt.previous();
198         if ( anOp == this )
199           anOperations.remove( anOperations.lastIndexOf( anOp ) );
200       }
201     }
202   }
203   // release the preview manager with removing the added preview Z layer
204   setPreviewManager( 0 );
205 }
206
207 void HYDROGUI_Operation::setDialogActive( const bool active )
208 {
209   LightApp_Operation::setDialogActive( active );
210   if( myPanel )
211   {
212     if( active )
213     {
214       myPanel->show();
215     }
216   }
217 }
218
219 HYDROGUI_InputPanel* HYDROGUI_Operation::createInputPanel() const
220 {
221   return NULL;
222 }
223
224 void HYDROGUI_Operation::closeInputPanel()
225 {
226   if( myPanel )
227   {
228     myModule->getApp()->desktop()->removeDockWidget( myPanel );
229     delete myPanel;
230     myPanel = 0;
231   }
232 }
233
234 bool HYDROGUI_Operation::processApply( int& theUpdateFlags,
235                                        QString& theErrorMsg )
236 {
237   return false;
238 }
239
240 void HYDROGUI_Operation::processCancel()
241 {
242 }
243
244 void HYDROGUI_Operation::startDocOperation()
245 {
246   // Open transaction in the model document only if it not
247   // already opened by other operation (intended for nested operations)
248   if ( !doc()->IsOperation() )
249   {
250     doc()->StartOperation();
251     myIsTransactionOpened = true;
252   }
253 }
254
255 void HYDROGUI_Operation::abortDocOperation()
256 {
257   // Abort transaction in the model document only if it was 
258   // opened by this operation (intended for nested operations)
259   if ( myIsTransactionOpened && doc()->IsOperation() )
260   {
261     doc()->AbortOperation();
262     myIsTransactionOpened = false;
263   }
264 }
265
266 void HYDROGUI_Operation::commitDocOperation()
267 {
268   // Commit transaction in the model document only if it was 
269   // opened by this operation (intended for nested operations)
270   if ( myIsTransactionOpened && doc()->IsOperation() )
271   {
272     doc()->CommitOperation( HYDROGUI_Tool::ToExtString( getName() ) );
273     myIsTransactionOpened = false;
274   }
275 }
276
277 Handle_HYDROData_Document HYDROGUI_Operation::doc() const
278 {
279   return HYDROData_Document::Document( myModule->getStudyId() );
280 }
281
282 void HYDROGUI_Operation::onApply()
283 {
284   QApplication::setOverrideCursor( Qt::WaitCursor );
285
286   startDocOperation();
287
288   int anUpdateFlags = 0;
289   QString anErrorMsg;
290
291   bool aResult = false;
292   
293   try
294   {
295     aResult = processApply( anUpdateFlags, anErrorMsg );
296   }
297   catch ( Standard_Failure )
298   {
299     Handle(Standard_Failure) aFailure = Standard_Failure::Caught();
300     anErrorMsg = aFailure->GetMessageString();
301     aResult = false;
302   }
303   catch ( ... )
304   {
305     aResult = false;
306   }
307   
308   QApplication::restoreOverrideCursor();
309
310   if ( aResult )
311   {
312     module()->update( anUpdateFlags );
313     commitDocOperation();
314     commit();
315   }
316   else
317   {
318     // Abort document opeartion only if requested
319     if ( isToAbortOnApply() )
320       abortDocOperation();
321
322     printErrorMessage( anErrorMsg );
323  
324     // If the operation has no input panel - do abort
325     if ( !inputPanel() ) {
326       abort();
327     }
328   }
329 }
330
331 void HYDROGUI_Operation::setPrintErrorMessage( const bool theIsPrint )
332 {
333   myIsPrintErrorMessage = theIsPrint;
334 }
335
336 void HYDROGUI_Operation::printErrorMessage( const QString& theErrorMsg )
337 {
338   if ( myIsPrintErrorMessage )
339   {
340     QString aMsg = tr( "INPUT_VALID_DATA" );
341     if( !theErrorMsg.isEmpty() )
342       aMsg.prepend( theErrorMsg + "\n" );
343     SUIT_MessageBox::critical( module()->getApp()->desktop(),
344                                tr( "INSUFFICIENT_INPUT_DATA" ),
345                                aMsg );
346
347   }
348   
349   myIsPrintErrorMessage = true;
350 }
351
352 void HYDROGUI_Operation::onCancel()
353 {
354   processCancel();
355   abort();
356 }
357
358 void HYDROGUI_Operation::onHelp()
359 {
360    emit helpContextModule( getHelpComponent(), getHelpFile(), getHelpContext() );
361 }
362
363 QString HYDROGUI_Operation::getHelpComponent() const
364 {
365    return module()->moduleName();
366 }
367
368 QString HYDROGUI_Operation::getHelpFile() const
369 {
370    QString aFileName = ((myName.isEmpty())? operationName() : myName);
371    return aFileName.replace(QRegExp("\\s"), "_").append(".html");
372 }
373
374 QString HYDROGUI_Operation::getHelpContext() const
375 {
376    return QString();
377 }
378
379