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 <OCCViewer_ViewManager.h>
38 #include <OCCViewer_ViewWindow.h>
39 #include <OCCViewer_ViewPort3d.h>
41 #include <QApplication>
44 #include "HYDRO_trace.hxx"
46 HYDROGUI_Operation::HYDROGUI_Operation( HYDROGUI_Module* theModule )
47 : LightApp_Operation(),
48 myModule( theModule ),
50 myIsPrintErrorMessage( true ),
51 myIsTransactionOpened( false ),
52 myPreviewManager( 0 ),
53 myPreviewZLayer( -1 ),
54 myIsApplyAndClose( true )
56 connect( this, SIGNAL( helpContextModule( const QString&, const QString&,
58 theModule->application(), SLOT( onHelpContextModule( const QString&,
59 const QString&, const QString& ) ) );
62 HYDROGUI_Operation::~HYDROGUI_Operation()
66 void HYDROGUI_Operation::setName( const QString& theName )
71 const QString& HYDROGUI_Operation::getName() const
76 HYDROGUI_InputPanel* HYDROGUI_Operation::inputPanel() const
78 DEBTRACE("inputPanel");
81 ( ( HYDROGUI_Operation* )this )->myPanel = createInputPanel();
84 connect( myPanel, SIGNAL( panelApplyAndClose() ), this, SLOT( onApplyAndClose() ) );
85 connect( myPanel, SIGNAL( panelApply() ), this, SLOT( onApply() ) );
86 connect( myPanel, SIGNAL( panelCancel() ), this, SLOT( onCancel() ) );
87 connect( myPanel, SIGNAL( panelHelp() ), this, SLOT( onHelp() ) );
93 SUIT_SelectionMgr* HYDROGUI_Operation::selectionMgr() const
95 return myModule->getApp()->selectionMgr();
98 HYDROGUI_Module* HYDROGUI_Operation::module() const
104 * Returns Z layer of the operation preview.
105 \ returns a layer position
107 int HYDROGUI_Operation::getPreviewZLayer() const
109 return myPreviewZLayer;
113 * Update Z layer for the operation preview.
114 \param theLayer a layer position
116 void HYDROGUI_Operation::updatePreviewZLayer( int theLayer )
118 setPreviewZLayer( theLayer );
120 HYDROGUI_Shape* aPreview = getPreviewShape();
122 aPreview->setZLayer( getPreviewZLayer() );
126 * Set Z layer for the operation preview.
127 \param theLayer a layer position
129 void HYDROGUI_Operation::setPreviewZLayer( int theLayer )
131 if ( theLayer != myPreviewZLayer )
132 myPreviewZLayer = theLayer;
136 * Returns a shape preview of the operation
138 HYDROGUI_Shape* HYDROGUI_Operation::getPreviewShape() const
144 * Return the operation preview manager
146 OCCViewer_ViewManager* HYDROGUI_Operation::getPreviewManager()
148 return myPreviewManager;
152 * Set the preview manager
154 void HYDROGUI_Operation::setPreviewManager( OCCViewer_ViewManager* theManager )
156 //No good: preview Z layer could be used by usual presentations
157 //if ( !theManager && myPreviewManager )
158 // module()->getOCCDisplayer()->RemoveZLayer( myPreviewManager, getPreviewZLayer() );
160 myPreviewManager = theManager;
162 if ( myPreviewManager )
163 setPreviewZLayer( module()->getOCCDisplayer()->AddPreviewZLayer( myPreviewManager ) );
166 void HYDROGUI_Operation::setCursor()
168 if ( myPreviewManager )
170 QVector<SUIT_ViewWindow*> winList = myPreviewManager->getViews();
171 for ( QVector<SUIT_ViewWindow*>::iterator it = winList.begin(); it != winList.end(); ++it )
173 OCCViewer_ViewWindow* occWin = ::qobject_cast<OCCViewer_ViewWindow*>( *it );
176 OCCViewer_ViewPort3d* vp = occWin->getViewPort();
180 myCursor = vp->cursor();
181 // Set specific cursor chosen in preferences
182 QCursor aCursor = module()->getPrefEditCursor();
183 vp->setDefaultCursor( aCursor.shape() );
184 vp->setCursor( *vp->getDefaultCursor() );
191 void HYDROGUI_Operation::restoreCursor()
193 if ( myPreviewManager )
195 QVector<SUIT_ViewWindow*> winList = myPreviewManager->getViews();
196 for ( QVector<SUIT_ViewWindow*>::iterator it = winList.begin(); it != winList.end(); ++it )
198 OCCViewer_ViewWindow* occWin = ::qobject_cast<OCCViewer_ViewWindow*>( *it );
201 OCCViewer_ViewPort3d* vp = occWin->getViewPort();
204 // Restore old cursor
205 vp->setDefaultCursor( myCursor.shape() );
206 vp->setCursor( myCursor );
213 void HYDROGUI_Operation::setIsApplyAndClose( const bool theFlag )
215 myIsApplyAndClose = theFlag;
218 bool HYDROGUI_Operation::isApplyAndClose() const
220 return myIsApplyAndClose;
223 void HYDROGUI_Operation::apply()
225 QApplication::setOverrideCursor( Qt::WaitCursor );
229 int anUpdateFlags = 0;
232 bool aResult = false;
233 QStringList aBrowseObjectsEntries;
237 aResult = processApply( anUpdateFlags, anErrorMsg, aBrowseObjectsEntries );
239 catch ( Standard_Failure )
241 Handle(Standard_Failure) aFailure = Standard_Failure::Caught();
243 anErrorMsg = aFailure->GetMessageString();
245 anErrorMsg = "failure unknown: catch ( Standard_Failure ) does not give access to failure!";
253 QApplication::restoreOverrideCursor();
257 module()->update( anUpdateFlags );
258 commitDocOperation();
260 browseObjects( aBrowseObjectsEntries, myIsApplyAndClose );
262 if ( !myIsApplyAndClose && inputPanel() )
265 else if( !anErrorMsg.isEmpty() )
267 // Abort document opeartion only if requested
268 if ( isToAbortOnApply() )
271 printErrorMessage( anErrorMsg );
273 // If the operation has no input panel - do abort
274 if ( !inputPanel() ) {
280 void HYDROGUI_Operation::startOperation()
282 LightApp_Operation::startOperation();
283 myModule->getActiveOperations().push( this );
285 if( myIsApplyAndClose && inputPanel() )
287 myModule->getApp()->desktop()->addDockWidget( Qt::RightDockWidgetArea, inputPanel() );
288 inputPanel()->show();
292 void HYDROGUI_Operation::abortOperation()
294 LightApp_Operation::abortOperation();
298 void HYDROGUI_Operation::commitOperation()
300 LightApp_Operation::commitOperation();
301 if ( myIsApplyAndClose )
305 void HYDROGUI_Operation::stopOperation()
307 LightApp_Operation::stopOperation();
309 // pop the operation from the cached map of active operations
310 QStack<HYDROGUI_Operation*>& anOperations = myModule->getActiveOperations();
311 if ( !anOperations.empty() ) {
312 if ( anOperations.top() == this )
316 // find in the stack the current operation and remove it from the stack
317 QVectorIterator<HYDROGUI_Operation*> aVIt( anOperations );
319 aVIt.previous(); // skip the top show/hide operation
320 while ( aVIt.hasPrevious() )
322 HYDROGUI_Operation* anOp = aVIt.previous();
324 anOperations.remove( anOperations.lastIndexOf( anOp ) );
328 // release the preview manager with removing the added preview Z layer
329 setPreviewManager( 0 );
332 void HYDROGUI_Operation::setDialogActive( const bool active )
334 LightApp_Operation::setDialogActive( active );
344 HYDROGUI_InputPanel* HYDROGUI_Operation::createInputPanel() const
349 void HYDROGUI_Operation::closeInputPanel()
353 myModule->getApp()->desktop()->removeDockWidget( myPanel );
359 bool HYDROGUI_Operation::processApply( int& theUpdateFlags,
360 QString& theErrorMsg,
361 QStringList& theBrowseObjectsEntries )
366 void HYDROGUI_Operation::processCancel()
370 void HYDROGUI_Operation::startDocOperation()
372 // Open transaction in the model document only if it not
373 // already opened by other operation (intended for nested operations)
374 if ( !doc()->IsOperation() )
376 doc()->StartOperation();
377 myIsTransactionOpened = true;
381 void HYDROGUI_Operation::abortDocOperation()
383 // Abort transaction in the model document only if it was
384 // opened by this operation (intended for nested operations)
385 if ( myIsTransactionOpened && doc()->IsOperation() )
387 doc()->AbortOperation();
388 myIsTransactionOpened = false;
392 void HYDROGUI_Operation::commitDocOperation()
394 // Commit transaction in the model document only if it was
395 // opened by this operation (intended for nested operations)
396 if ( myIsTransactionOpened && doc()->IsOperation() )
398 doc()->CommitOperation( HYDROGUI_Tool::ToExtString( getName() ) );
399 myIsTransactionOpened = false;
403 Handle(HYDROData_Document) HYDROGUI_Operation::doc() const
405 return HYDROData_Document::Document();
408 void HYDROGUI_Operation::onApplyAndClose()
410 myIsApplyAndClose = true;
412 restoreOCCViewerSelection();
415 void HYDROGUI_Operation::onApply()
417 myIsApplyAndClose = false;
419 restoreOCCViewerSelection();
422 void HYDROGUI_Operation::setPrintErrorMessage( const bool theIsPrint )
424 myIsPrintErrorMessage = theIsPrint;
427 void HYDROGUI_Operation::printErrorMessage( const QString& theErrorMsg )
429 if ( myIsPrintErrorMessage )
431 QString aMsg = tr( "INPUT_VALID_DATA" );
432 if( !theErrorMsg.isEmpty() )
433 aMsg.prepend( theErrorMsg + "\n" );
434 SUIT_MessageBox::critical( module()->getApp()->desktop(),
435 tr( "INSUFFICIENT_INPUT_DATA" ),
440 myIsPrintErrorMessage = true;
443 void HYDROGUI_Operation::onCancel()
449 myIsApplyAndClose = true;
450 restoreOCCViewerSelection();
453 void HYDROGUI_Operation::restoreOCCViewerSelection()
455 DEBTRACE("restoreOCCViewerSelection");
456 LightApp_Application* anApp = module()->getApp();
457 LightApp_SelectionMgr* sm = anApp->selectionMgr();
460 sm->clearFilters(); // see GEOM_Displayer::GlobalSelection
462 OCCViewer_ViewManager* aViewManager =
463 dynamic_cast<OCCViewer_ViewManager*>( anApp->getViewManager( OCCViewer_Viewer::Type(), true ) );
466 if( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
468 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
471 DEBTRACE("AIScontext " << aCtx.get());
474 aCtx->SetAutomaticHilight( Standard_True );
475 aCtx->RemoveFilters();
480 void HYDROGUI_Operation::onHelp()
482 emit helpContextModule( getHelpComponent(), getHelpFile(), getHelpContext() );
485 QString HYDROGUI_Operation::getHelpComponent() const
487 return module()->moduleName();
490 QString HYDROGUI_Operation::getHelpFile() const
492 QString aFileName = ((myName.isEmpty())? operationName() : myName);
493 aFileName = aFileName.toLower();
494 aFileName = aFileName.replace(QRegExp("\\s"), "_").append(".html");
495 aFileName.remove( "create_" );
496 aFileName.remove( "edit_" );
500 QString HYDROGUI_Operation::getHelpContext() const
505 void HYDROGUI_Operation::browseObjects( const QStringList& theBrowseObjectsEntries,
506 const bool theIsApplyAndClose/* = true*/ )
508 bool isOptimizedBrowse = true;
509 module()->getApp()->browseObjects( theBrowseObjectsEntries, theIsApplyAndClose, isOptimizedBrowse );