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.
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.
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
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include "HYDROGUI_Operation.h"
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"
27 #include <HYDROData_Document.h>
28 #include <HYDROData_Iterator.h>
30 #include <LightApp_Application.h>
31 #include <LightApp_SelectionMgr.h>
33 #include <SUIT_Desktop.h>
34 #include <SUIT_MessageBox.h>
35 #include <SUIT_Study.h>
37 #include <QApplication>
39 HYDROGUI_Operation::HYDROGUI_Operation( HYDROGUI_Module* theModule )
40 : LightApp_Operation(),
41 myModule( theModule ),
43 myIsPrintErrorMessage( true ),
44 myIsTransactionOpened( false ),
45 myPreviewManager( 0 ),
48 connect( this, SIGNAL( helpContextModule( const QString&, const QString&,
50 theModule->application(), SLOT( onHelpContextModule( const QString&,
51 const QString&, const QString& ) ) );
54 HYDROGUI_Operation::~HYDROGUI_Operation()
58 void HYDROGUI_Operation::setName( const QString& theName )
63 const QString& HYDROGUI_Operation::getName() const
68 HYDROGUI_InputPanel* HYDROGUI_Operation::inputPanel() const
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() ) );
80 SUIT_SelectionMgr* HYDROGUI_Operation::selectionMgr() const
82 return myModule->getApp()->selectionMgr();
85 HYDROGUI_Module* HYDROGUI_Operation::module() const
91 * Returns Z layer of the operation preview.
92 \ returns a layer position
94 int HYDROGUI_Operation::getPreviewZLayer() const
96 return myPreviewZLayer;
100 * Update Z layer for the operation preview.
101 \param theLayer a layer position
103 void HYDROGUI_Operation::updatePreviewZLayer( int theLayer )
105 setPreviewZLayer( theLayer );
107 HYDROGUI_Shape* aPreview = getPreviewShape();
109 aPreview->setZLayer( getPreviewZLayer() );
113 * Set Z layer for the operation preview.
114 \param theLayer a layer position
116 void HYDROGUI_Operation::setPreviewZLayer( int theLayer )
118 if ( theLayer != myPreviewZLayer )
119 myPreviewZLayer = theLayer;
123 * Returns a shape preview of the operation
125 HYDROGUI_Shape* HYDROGUI_Operation::getPreviewShape() const
131 * Return the operation preview manager
133 OCCViewer_ViewManager* HYDROGUI_Operation::getPreviewManager()
135 return myPreviewManager;
139 * Set the preview manager
141 void HYDROGUI_Operation::setPreviewManager( OCCViewer_ViewManager* theManager )
143 //No good: preview Z layer could be used by usual presentations
144 //if ( !theManager && myPreviewManager )
145 // module()->getOCCDisplayer()->RemoveZLayer( myPreviewManager, getPreviewZLayer() );
147 myPreviewManager = theManager;
149 if ( myPreviewManager )
150 setPreviewZLayer( module()->getOCCDisplayer()->AddPreviewZLayer( myPreviewManager ) );
153 void HYDROGUI_Operation::startOperation()
155 LightApp_Operation::startOperation();
156 myModule->getActiveOperations().push( this );
160 myModule->getApp()->desktop()->addDockWidget( Qt::RightDockWidgetArea, inputPanel() );
161 inputPanel()->show();
165 void HYDROGUI_Operation::abortOperation()
167 LightApp_Operation::abortOperation();
171 void HYDROGUI_Operation::commitOperation()
173 LightApp_Operation::commitOperation();
177 void HYDROGUI_Operation::stopOperation()
179 LightApp_Operation::stopOperation();
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 )
188 // find in the stack the current operation and remove it from the stack
189 QVectorIterator<HYDROGUI_Operation*> aVIt( anOperations );
191 aVIt.previous(); // skip the top show/hide operation
192 while ( aVIt.hasPrevious() )
194 HYDROGUI_Operation* anOp = aVIt.previous();
196 anOperations.remove( anOperations.lastIndexOf( anOp ) );
200 // release the preview manager with removing the added preview Z layer
201 setPreviewManager( 0 );
204 void HYDROGUI_Operation::setDialogActive( const bool active )
206 LightApp_Operation::setDialogActive( active );
216 HYDROGUI_InputPanel* HYDROGUI_Operation::createInputPanel() const
221 void HYDROGUI_Operation::closeInputPanel()
225 myModule->getApp()->desktop()->removeDockWidget( myPanel );
231 bool HYDROGUI_Operation::processApply( int& theUpdateFlags,
232 QString& theErrorMsg,
233 QStringList& theBrowseObjectsEntries )
238 void HYDROGUI_Operation::processCancel()
242 void HYDROGUI_Operation::startDocOperation()
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() )
248 doc()->StartOperation();
249 myIsTransactionOpened = true;
253 void HYDROGUI_Operation::abortDocOperation()
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() )
259 doc()->AbortOperation();
260 myIsTransactionOpened = false;
264 void HYDROGUI_Operation::commitDocOperation()
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() )
270 doc()->CommitOperation( HYDROGUI_Tool::ToExtString( getName() ) );
271 myIsTransactionOpened = false;
275 Handle_HYDROData_Document HYDROGUI_Operation::doc() const
277 return HYDROData_Document::Document( myModule->getStudyId() );
280 void HYDROGUI_Operation::onApply()
282 QApplication::setOverrideCursor( Qt::WaitCursor );
286 int anUpdateFlags = 0;
289 bool aResult = false;
290 QStringList aBrowseObjectsEntries;
294 aResult = processApply( anUpdateFlags, anErrorMsg, aBrowseObjectsEntries );
296 catch ( Standard_Failure )
298 Handle(Standard_Failure) aFailure = Standard_Failure::Caught();
299 anErrorMsg = aFailure->GetMessageString();
307 QApplication::restoreOverrideCursor();
311 module()->update( anUpdateFlags );
312 commitDocOperation();
314 browseObjects( aBrowseObjectsEntries );
316 else if( !anErrorMsg.isEmpty() )
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
379 void HYDROGUI_Operation::browseObjects( const QStringList& theBrowseObjectsEntries )
381 bool isApplyAndClose = true;
382 bool isOptimizedBrowse = true;
383 module()->getApp()->browseObjects( theBrowseObjectsEntries, isApplyAndClose, isOptimizedBrowse );