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