1 // Copyright (C) 2007-2015 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, or (at your option) any later version.
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 <OCCViewer_ViewManager.h>
42 #include <OCCViewer_ViewWindow.h>
43 #include <OCCViewer_ViewPort3d.h>
45 #include <QApplication>
47 HYDROGUI_Operation::HYDROGUI_Operation( HYDROGUI_Module* theModule )
48 : LightApp_Operation(),
49 myModule( theModule ),
51 myIsPrintErrorMessage( true ),
52 myIsTransactionOpened( false ),
53 myPreviewManager( 0 ),
54 myPreviewZLayer( -1 ),
55 myIsApplyAndClose( true )
57 connect( this, SIGNAL( helpContextModule( const QString&, const QString&,
59 theModule->application(), SLOT( onHelpContextModule( const QString&,
60 const QString&, const QString& ) ) );
63 HYDROGUI_Operation::~HYDROGUI_Operation()
67 void HYDROGUI_Operation::setName( const QString& theName )
72 const QString& HYDROGUI_Operation::getName() const
77 HYDROGUI_InputPanel* HYDROGUI_Operation::inputPanel() const
81 ( ( HYDROGUI_Operation* )this )->myPanel = createInputPanel();
82 connect( myPanel, SIGNAL( panelApplyAndClose() ), this, SLOT( onApplyAndClose() ) );
83 connect( myPanel, SIGNAL( panelApply() ), this, SLOT( onApply() ) );
84 connect( myPanel, SIGNAL( panelCancel() ), this, SLOT( onCancel() ) );
85 connect( myPanel, SIGNAL( panelHelp() ), this, SLOT( onHelp() ) );
90 SUIT_SelectionMgr* HYDROGUI_Operation::selectionMgr() const
92 return myModule->getApp()->selectionMgr();
95 HYDROGUI_Module* HYDROGUI_Operation::module() const
101 * Returns Z layer of the operation preview.
102 \ returns a layer position
104 int HYDROGUI_Operation::getPreviewZLayer() const
106 return myPreviewZLayer;
110 * Update Z layer for the operation preview.
111 \param theLayer a layer position
113 void HYDROGUI_Operation::updatePreviewZLayer( int theLayer )
115 setPreviewZLayer( theLayer );
117 HYDROGUI_Shape* aPreview = getPreviewShape();
119 aPreview->setZLayer( getPreviewZLayer() );
123 * Set Z layer for the operation preview.
124 \param theLayer a layer position
126 void HYDROGUI_Operation::setPreviewZLayer( int theLayer )
128 if ( theLayer != myPreviewZLayer )
129 myPreviewZLayer = theLayer;
133 * Returns a shape preview of the operation
135 HYDROGUI_Shape* HYDROGUI_Operation::getPreviewShape() const
141 * Return the operation preview manager
143 OCCViewer_ViewManager* HYDROGUI_Operation::getPreviewManager()
145 return myPreviewManager;
149 * Set the preview manager
151 void HYDROGUI_Operation::setPreviewManager( OCCViewer_ViewManager* theManager )
153 //No good: preview Z layer could be used by usual presentations
154 //if ( !theManager && myPreviewManager )
155 // module()->getOCCDisplayer()->RemoveZLayer( myPreviewManager, getPreviewZLayer() );
157 myPreviewManager = theManager;
159 if ( myPreviewManager )
160 setPreviewZLayer( module()->getOCCDisplayer()->AddPreviewZLayer( myPreviewManager ) );
163 void HYDROGUI_Operation::setCursor()
165 if ( myPreviewManager )
167 QVector<SUIT_ViewWindow*> winList = myPreviewManager->getViews();
168 for ( QVector<SUIT_ViewWindow*>::iterator it = winList.begin(); it != winList.end(); ++it )
170 OCCViewer_ViewWindow* occWin = ::qobject_cast<OCCViewer_ViewWindow*>( *it );
173 OCCViewer_ViewPort3d* vp = occWin->getViewPort();
177 myCursor = vp->cursor();
178 // Set specific cursor chosen in preferences
179 QCursor aCursor = module()->getPrefEditCursor();
180 vp->setDefaultCursor( aCursor.shape() );
181 vp->setCursor( *vp->getDefaultCursor() );
188 void HYDROGUI_Operation::restoreCursor()
190 if ( myPreviewManager )
192 QVector<SUIT_ViewWindow*> winList = myPreviewManager->getViews();
193 for ( QVector<SUIT_ViewWindow*>::iterator it = winList.begin(); it != winList.end(); ++it )
195 OCCViewer_ViewWindow* occWin = ::qobject_cast<OCCViewer_ViewWindow*>( *it );
198 OCCViewer_ViewPort3d* vp = occWin->getViewPort();
201 // Restore old cursor
202 vp->setDefaultCursor( myCursor.shape() );
203 vp->setCursor( myCursor );
210 void HYDROGUI_Operation::setIsApplyAndClose( const bool theFlag )
212 myIsApplyAndClose = theFlag;
215 bool HYDROGUI_Operation::isApplyAndClose() const
217 return myIsApplyAndClose;
220 void HYDROGUI_Operation::apply()
222 QApplication::setOverrideCursor( Qt::WaitCursor );
226 int anUpdateFlags = 0;
229 bool aResult = false;
230 QStringList aBrowseObjectsEntries;
234 aResult = processApply( anUpdateFlags, anErrorMsg, aBrowseObjectsEntries );
236 catch ( Standard_Failure )
238 Handle(Standard_Failure) aFailure = Standard_Failure::Caught();
239 anErrorMsg = aFailure->GetMessageString();
247 QApplication::restoreOverrideCursor();
251 module()->update( anUpdateFlags );
252 commitDocOperation();
254 browseObjects( aBrowseObjectsEntries, myIsApplyAndClose );
256 if ( !myIsApplyAndClose && inputPanel() )
261 // Abort document opeartion only if requested
262 if ( isToAbortOnApply() )
265 printErrorMessage( anErrorMsg );
267 // If the operation has no input panel - do abort
268 if ( !inputPanel() ) {
274 void HYDROGUI_Operation::startOperation()
276 LightApp_Operation::startOperation();
277 myModule->getActiveOperations().push( this );
279 if( myIsApplyAndClose && inputPanel() )
281 myModule->getApp()->desktop()->addDockWidget( Qt::RightDockWidgetArea, inputPanel() );
282 inputPanel()->show();
286 void HYDROGUI_Operation::abortOperation()
288 LightApp_Operation::abortOperation();
292 void HYDROGUI_Operation::commitOperation()
294 LightApp_Operation::commitOperation();
295 if ( myIsApplyAndClose )
299 void HYDROGUI_Operation::stopOperation()
301 LightApp_Operation::stopOperation();
303 // pop the operation from the cached map of active operations
304 QStack<HYDROGUI_Operation*>& anOperations = myModule->getActiveOperations();
305 if ( !anOperations.empty() ) {
306 if ( anOperations.top() == this )
310 // find in the stack the current operation and remove it from the stack
311 QVectorIterator<HYDROGUI_Operation*> aVIt( anOperations );
313 aVIt.previous(); // skip the top show/hide operation
314 while ( aVIt.hasPrevious() )
316 HYDROGUI_Operation* anOp = aVIt.previous();
318 anOperations.remove( anOperations.lastIndexOf( anOp ) );
322 // release the preview manager with removing the added preview Z layer
323 setPreviewManager( 0 );
326 void HYDROGUI_Operation::setDialogActive( const bool active )
328 LightApp_Operation::setDialogActive( active );
338 HYDROGUI_InputPanel* HYDROGUI_Operation::createInputPanel() const
343 void HYDROGUI_Operation::closeInputPanel()
347 myModule->getApp()->desktop()->removeDockWidget( myPanel );
353 bool HYDROGUI_Operation::processApply( int& theUpdateFlags,
354 QString& theErrorMsg,
355 QStringList& theBrowseObjectsEntries )
360 void HYDROGUI_Operation::processCancel()
364 void HYDROGUI_Operation::startDocOperation()
366 // Open transaction in the model document only if it not
367 // already opened by other operation (intended for nested operations)
368 if ( !doc()->IsOperation() )
370 doc()->StartOperation();
371 myIsTransactionOpened = true;
375 void HYDROGUI_Operation::abortDocOperation()
377 // Abort transaction in the model document only if it was
378 // opened by this operation (intended for nested operations)
379 if ( myIsTransactionOpened && doc()->IsOperation() )
381 doc()->AbortOperation();
382 myIsTransactionOpened = false;
386 void HYDROGUI_Operation::commitDocOperation()
388 // Commit transaction in the model document only if it was
389 // opened by this operation (intended for nested operations)
390 if ( myIsTransactionOpened && doc()->IsOperation() )
392 doc()->CommitOperation( HYDROGUI_Tool::ToExtString( getName() ) );
393 myIsTransactionOpened = false;
397 Handle_HYDROData_Document HYDROGUI_Operation::doc() const
399 return HYDROData_Document::Document( myModule->getStudyId() );
402 void HYDROGUI_Operation::onApplyAndClose()
404 myIsApplyAndClose = true;
408 void HYDROGUI_Operation::onApply()
410 myIsApplyAndClose = false;
414 void HYDROGUI_Operation::setPrintErrorMessage( const bool theIsPrint )
416 myIsPrintErrorMessage = theIsPrint;
419 void HYDROGUI_Operation::printErrorMessage( const QString& theErrorMsg )
421 if ( myIsPrintErrorMessage )
423 QString aMsg = tr( "INPUT_VALID_DATA" );
424 if( !theErrorMsg.isEmpty() )
425 aMsg.prepend( theErrorMsg + "\n" );
426 SUIT_MessageBox::critical( module()->getApp()->desktop(),
427 tr( "INSUFFICIENT_INPUT_DATA" ),
432 myIsPrintErrorMessage = true;
435 void HYDROGUI_Operation::onCancel()
441 myIsApplyAndClose = true;
444 void HYDROGUI_Operation::onHelp()
446 emit helpContextModule( getHelpComponent(), getHelpFile(), getHelpContext() );
449 QString HYDROGUI_Operation::getHelpComponent() const
451 return module()->moduleName();
454 QString HYDROGUI_Operation::getHelpFile() const
456 QString aFileName = ((myName.isEmpty())? operationName() : myName);
457 return aFileName.replace(QRegExp("\\s"), "_").append(".html");
460 QString HYDROGUI_Operation::getHelpContext() const
465 void HYDROGUI_Operation::browseObjects( const QStringList& theBrowseObjectsEntries,
466 const bool theIsApplyAndClose/* = true*/ )
468 bool isOptimizedBrowse = true;
469 module()->getApp()->browseObjects( theBrowseObjectsEntries, theIsApplyAndClose, isOptimizedBrowse );