]> SALOME platform Git repositories - modules/hydro.git/blob - src/HYDROGUI/HYDROGUI_Operation.cxx
Salome HOME
merge master 2015/06/04
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_Operation.cxx
1 // Copyright (C) 2014-2015  EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
6 //
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10 // Lesser General Public License for more details.
11 //
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
19 #include "HYDROGUI_Operation.h"
20
21 #include "HYDROGUI_InputPanel.h"
22 #include "HYDROGUI_Module.h"
23 #include "HYDROGUI_Tool.h"
24 #include "HYDROGUI_OCCDisplayer.h"
25 #include "HYDROGUI_Shape.h"
26
27 #include <HYDROData_Document.h>
28 #include <HYDROData_Iterator.h>
29
30 #include <LightApp_Application.h>
31 #include <LightApp_SelectionMgr.h>
32
33 #include <SUIT_Desktop.h>
34 #include <SUIT_MessageBox.h>
35 #include <SUIT_Study.h>
36
37 #include <QApplication>
38
39 HYDROGUI_Operation::HYDROGUI_Operation( HYDROGUI_Module* theModule )
40 : LightApp_Operation(),
41   myModule( theModule ),
42   myPanel( 0 ),
43   myIsPrintErrorMessage( true ),
44   myIsTransactionOpened( false ),
45   myPreviewManager( 0 ),
46   myPreviewZLayer( -1 )
47 {
48   connect( this, SIGNAL( helpContextModule( const QString&, const QString&,
49                                             const QString& ) ),
50            theModule->application(), SLOT( onHelpContextModule( const QString&,
51                                             const QString&, const QString& ) ) );
52 }
53
54 HYDROGUI_Operation::~HYDROGUI_Operation()
55 {
56 }
57
58 void HYDROGUI_Operation::setName( const QString& theName )
59 {
60   myName = theName;
61 }
62
63 const QString& HYDROGUI_Operation::getName() const
64 {
65   return myName;
66 }
67
68 HYDROGUI_InputPanel* HYDROGUI_Operation::inputPanel() const
69 {
70   if( !myPanel )
71   {
72     ( ( HYDROGUI_Operation* )this )->myPanel = createInputPanel();
73     connect( myPanel, SIGNAL( panelApply() ),  this, SLOT( onApply() ) );
74     connect( myPanel, SIGNAL( panelCancel() ), this, SLOT( onCancel() ) );
75     connect( myPanel, SIGNAL( panelHelp() ), this, SLOT( onHelp() ) );
76   }
77   return myPanel;
78 }
79
80 SUIT_SelectionMgr* HYDROGUI_Operation::selectionMgr() const
81 {
82   return myModule->getApp()->selectionMgr();
83 }
84
85 HYDROGUI_Module* HYDROGUI_Operation::module() const
86 {
87   return myModule;
88 }
89
90 /**
91   * Returns Z layer of the operation preview.
92    \ returns a layer position
93  */
94 int HYDROGUI_Operation::getPreviewZLayer() const
95 {
96   return myPreviewZLayer;
97 }
98
99 /**
100  * Update Z layer for the operation preview.
101    \param theLayer a layer position
102  */
103 void HYDROGUI_Operation::updatePreviewZLayer( int theLayer )
104 {
105   setPreviewZLayer( theLayer );
106
107   HYDROGUI_Shape* aPreview = getPreviewShape();
108   if ( aPreview )
109     aPreview->setZLayer( getPreviewZLayer() );
110 }
111
112 /**
113  * Set Z layer for the operation preview.
114  \param theLayer a layer position
115  */
116 void HYDROGUI_Operation::setPreviewZLayer( int theLayer )
117 {
118   if ( theLayer != myPreviewZLayer )
119     myPreviewZLayer = theLayer;
120 }
121
122 /**
123  * Returns a shape preview of the operation
124  */
125 HYDROGUI_Shape* HYDROGUI_Operation::getPreviewShape() const
126 {
127   return 0;
128 }
129
130 /**
131  * Return the operation preview manager
132  */
133 OCCViewer_ViewManager* HYDROGUI_Operation::getPreviewManager()
134 {
135   return myPreviewManager;
136 }
137
138 /**
139  * Set the preview manager
140  */
141 void HYDROGUI_Operation::setPreviewManager( OCCViewer_ViewManager* theManager )
142 {
143   //No good: preview Z layer could be used by usual presentations
144   //if ( !theManager && myPreviewManager )
145   //  module()->getOCCDisplayer()->RemoveZLayer( myPreviewManager, getPreviewZLayer() );
146
147   myPreviewManager = theManager;
148
149   if ( myPreviewManager )
150     setPreviewZLayer( module()->getOCCDisplayer()->AddPreviewZLayer( myPreviewManager ) );
151 }
152
153 void HYDROGUI_Operation::startOperation()
154 {
155   LightApp_Operation::startOperation();
156   myModule->getActiveOperations().push( this );
157
158   if( inputPanel() )
159   {
160     myModule->getApp()->desktop()->addDockWidget( Qt::RightDockWidgetArea, inputPanel() );
161     inputPanel()->show();
162   }
163 }
164
165 void HYDROGUI_Operation::abortOperation()
166 {
167   LightApp_Operation::abortOperation();
168   closeInputPanel();
169 }
170
171 void HYDROGUI_Operation::commitOperation()
172 {
173   LightApp_Operation::commitOperation();
174   closeInputPanel();
175 }
176
177 void HYDROGUI_Operation::stopOperation()
178 {
179   LightApp_Operation::stopOperation();
180
181   // pop the operation from the cached map of active operations
182   QStack<HYDROGUI_Operation*>& anOperations = myModule->getActiveOperations();
183   if ( !anOperations.empty() ) {
184     if ( anOperations.top() == this )
185       anOperations.pop();
186     else
187     {
188       // find in the stack the current operation and remove it from the stack
189       QVectorIterator<HYDROGUI_Operation*> aVIt( anOperations );
190       aVIt.toBack();
191       aVIt.previous(); // skip the top show/hide operation
192       while ( aVIt.hasPrevious() )
193       {
194         HYDROGUI_Operation* anOp = aVIt.previous();
195         if ( anOp == this )
196           anOperations.remove( anOperations.lastIndexOf( anOp ) );
197       }
198     }
199   }
200   // release the preview manager with removing the added preview Z layer
201   setPreviewManager( 0 );
202 }
203
204 void HYDROGUI_Operation::setDialogActive( const bool active )
205 {
206   LightApp_Operation::setDialogActive( active );
207   if( myPanel )
208   {
209     if( active )
210     {
211       myPanel->show();
212     }
213   }
214 }
215
216 HYDROGUI_InputPanel* HYDROGUI_Operation::createInputPanel() const
217 {
218   return NULL;
219 }
220
221 void HYDROGUI_Operation::closeInputPanel()
222 {
223   if( myPanel )
224   {
225     myModule->getApp()->desktop()->removeDockWidget( myPanel );
226     delete myPanel;
227     myPanel = 0;
228   }
229 }
230
231 bool HYDROGUI_Operation::processApply( int& theUpdateFlags,
232                                        QString& theErrorMsg,
233                                        QStringList& theBrowseObjectsEntries )
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 only if it not
245   // already opened by other operation (intended for nested operations)
246   if ( !doc()->IsOperation() )
247   {
248     doc()->StartOperation();
249     myIsTransactionOpened = true;
250   }
251 }
252
253 void HYDROGUI_Operation::abortDocOperation()
254 {
255   // Abort transaction in the model document only if it was 
256   // opened by this operation (intended for nested operations)
257   if ( myIsTransactionOpened && doc()->IsOperation() )
258   {
259     doc()->AbortOperation();
260     myIsTransactionOpened = false;
261   }
262 }
263
264 void HYDROGUI_Operation::commitDocOperation()
265 {
266   // Commit transaction in the model document only if it was 
267   // opened by this operation (intended for nested operations)
268   if ( myIsTransactionOpened && doc()->IsOperation() )
269   {
270     doc()->CommitOperation( HYDROGUI_Tool::ToExtString( getName() ) );
271     myIsTransactionOpened = false;
272   }
273 }
274
275 Handle_HYDROData_Document HYDROGUI_Operation::doc() const
276 {
277   return HYDROData_Document::Document( myModule->getStudyId() );
278 }
279
280 void HYDROGUI_Operation::onApply()
281 {
282   QApplication::setOverrideCursor( Qt::WaitCursor );
283
284   startDocOperation();
285
286   int anUpdateFlags = 0;
287   QString anErrorMsg;
288
289   bool aResult = false;
290   QStringList aBrowseObjectsEntries;
291
292   try
293   {
294     aResult = processApply( anUpdateFlags, anErrorMsg, aBrowseObjectsEntries );
295   }
296   catch ( Standard_Failure )
297   {
298     Handle(Standard_Failure) aFailure = Standard_Failure::Caught();
299     anErrorMsg = aFailure->GetMessageString();
300     aResult = false;
301   }
302   catch ( ... )
303   {
304     aResult = false;
305   }
306   
307   QApplication::restoreOverrideCursor();
308
309   if ( aResult )
310   {
311     module()->update( anUpdateFlags );
312     commitDocOperation();
313     commit();
314     browseObjects( aBrowseObjectsEntries );
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 void HYDROGUI_Operation::browseObjects( const QStringList& theBrowseObjectsEntries )
380 {
381   bool isApplyAndClose = true;
382   bool isOptimizedBrowse = true;
383   module()->getApp()->browseObjects( theBrowseObjectsEntries, isApplyAndClose, isOptimizedBrowse );
384 }