1 // Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "HYDROGUI_Operation.h"
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"
31 #include <HYDROData_Document.h>
32 #include <HYDROData_Iterator.h>
34 #include <LightApp_Application.h>
35 #include <LightApp_SelectionMgr.h>
37 #include <SUIT_Desktop.h>
38 #include <SUIT_MessageBox.h>
39 #include <SUIT_Study.h>
41 #include <QApplication>
43 HYDROGUI_Operation::HYDROGUI_Operation( HYDROGUI_Module* theModule )
44 : LightApp_Operation(),
45 myModule( theModule ),
47 myIsPrintErrorMessage( true ),
48 myIsTransactionOpened( false ),
49 myPreviewManager( 0 ),
52 connect( this, SIGNAL( helpContextModule( const QString&, const QString&,
54 theModule->application(), SLOT( onHelpContextModule( const QString&,
55 const QString&, const QString& ) ) );
58 HYDROGUI_Operation::~HYDROGUI_Operation()
62 void HYDROGUI_Operation::setName( const QString& theName )
67 const QString& HYDROGUI_Operation::getName() const
72 HYDROGUI_InputPanel* HYDROGUI_Operation::inputPanel() const
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() ) );
84 SUIT_SelectionMgr* HYDROGUI_Operation::selectionMgr() const
86 return myModule->getApp()->selectionMgr();
89 HYDROGUI_Module* HYDROGUI_Operation::module() const
95 * Returns Z layer of the operation preview.
96 \ returns a layer position
98 int HYDROGUI_Operation::getPreviewZLayer() const
100 return myPreviewZLayer;
104 * Update Z layer for the operation preview.
105 \param theLayer a layer position
107 void HYDROGUI_Operation::updatePreviewZLayer( int theLayer )
109 setPreviewZLayer( theLayer );
111 HYDROGUI_Shape* aPreview = getPreviewShape();
113 aPreview->setZLayer( getPreviewZLayer() );
117 * Set Z layer for the operation preview.
118 \param theLayer a layer position
120 void HYDROGUI_Operation::setPreviewZLayer( int theLayer )
122 if ( theLayer != myPreviewZLayer )
123 myPreviewZLayer = theLayer;
127 * Returns a shape preview of the operation
129 HYDROGUI_Shape* HYDROGUI_Operation::getPreviewShape() const
135 * Return the operation preview manager
137 OCCViewer_ViewManager* HYDROGUI_Operation::getPreviewManager()
139 return myPreviewManager;
143 * Set the preview manager
145 void HYDROGUI_Operation::setPreviewManager( OCCViewer_ViewManager* theManager )
147 if ( !theManager && myPreviewManager )
148 module()->getOCCDisplayer()->RemoveZLayer( myPreviewManager, getPreviewZLayer() );
150 myPreviewManager = theManager;
152 if ( myPreviewManager )
153 setPreviewZLayer( module()->getOCCDisplayer()->AddTopZLayer( myPreviewManager ) );
156 void HYDROGUI_Operation::startOperation()
158 LightApp_Operation::startOperation();
159 myModule->getActiveOperations().push( this );
163 myModule->getApp()->desktop()->addDockWidget( Qt::RightDockWidgetArea, inputPanel() );
164 inputPanel()->show();
168 void HYDROGUI_Operation::abortOperation()
170 LightApp_Operation::abortOperation();
174 void HYDROGUI_Operation::commitOperation()
176 LightApp_Operation::commitOperation();
180 void HYDROGUI_Operation::stopOperation()
182 LightApp_Operation::stopOperation();
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 )
191 // find in the stack the current operation and remove it from the stack
192 QVectorIterator<HYDROGUI_Operation*> aVIt( anOperations );
194 aVIt.previous(); // skip the top show/hide operation
195 while ( aVIt.hasPrevious() )
197 HYDROGUI_Operation* anOp = aVIt.previous();
199 anOperations.remove( anOperations.lastIndexOf( anOp ) );
203 // release the preview manager with removing the added preview Z layer
204 setPreviewManager( 0 );
207 void HYDROGUI_Operation::setDialogActive( const bool active )
209 LightApp_Operation::setDialogActive( active );
219 HYDROGUI_InputPanel* HYDROGUI_Operation::createInputPanel() const
224 void HYDROGUI_Operation::closeInputPanel()
228 myModule->getApp()->desktop()->removeDockWidget( myPanel );
234 bool HYDROGUI_Operation::processApply( int& theUpdateFlags,
235 QString& theErrorMsg )
240 void HYDROGUI_Operation::processCancel()
244 void HYDROGUI_Operation::startDocOperation()
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() )
250 doc()->StartOperation();
251 myIsTransactionOpened = true;
255 void HYDROGUI_Operation::abortDocOperation()
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() )
261 doc()->AbortOperation();
262 myIsTransactionOpened = false;
266 void HYDROGUI_Operation::commitDocOperation()
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() )
272 doc()->CommitOperation( HYDROGUI_Tool::ToExtString( getName() ) );
273 myIsTransactionOpened = false;
277 Handle_HYDROData_Document HYDROGUI_Operation::doc() const
279 return HYDROData_Document::Document( myModule->getStudyId() );
282 void HYDROGUI_Operation::onApply()
284 QApplication::setOverrideCursor( Qt::WaitCursor );
288 int anUpdateFlags = 0;
291 bool aResult = false;
295 aResult = processApply( anUpdateFlags, anErrorMsg );
297 catch ( Standard_Failure )
299 Handle(Standard_Failure) aFailure = Standard_Failure::Caught();
300 anErrorMsg = aFailure->GetMessageString();
308 QApplication::restoreOverrideCursor();
312 module()->update( anUpdateFlags );
313 commitDocOperation();
318 // Abort document opeartion only if requested
319 if ( isToAbortOnApply() )
322 printErrorMessage( anErrorMsg );
324 // If the operation has no input panel - do abort
325 if ( !inputPanel() ) {
331 void HYDROGUI_Operation::setPrintErrorMessage( const bool theIsPrint )
333 myIsPrintErrorMessage = theIsPrint;
336 void HYDROGUI_Operation::printErrorMessage( const QString& theErrorMsg )
338 if ( myIsPrintErrorMessage )
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" ),
349 myIsPrintErrorMessage = true;
352 void HYDROGUI_Operation::onCancel()
358 void HYDROGUI_Operation::onHelp()
360 emit helpContextModule( getHelpComponent(), getHelpFile(), getHelpContext() );
363 QString HYDROGUI_Operation::getHelpComponent() const
365 return module()->moduleName();
368 QString HYDROGUI_Operation::getHelpFile() const
370 QString aFileName = ((myName.isEmpty())? operationName() : myName);
371 return aFileName.replace(QRegExp("\\s"), "_").append(".html");
374 QString HYDROGUI_Operation::getHelpContext() const