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 bool HYDROGUI_Operation::myIsClear = false;
48 HYDROGUI_Operation::HYDROGUI_Operation( HYDROGUI_Module* theModule )
49 : LightApp_Operation(),
50 myModule( theModule ),
52 myIsPrintErrorMessage( true ),
53 myIsTransactionOpened( false ),
54 myPreviewManager( 0 ),
55 myPreviewZLayer( -1 ),
56 myIsApplyAndClose( true )
58 DEBTRACE("HYDROGUI_Operation");
59 connect( this, SIGNAL( helpContextModule( const QString&, const QString&,
61 theModule->application(), SLOT( onHelpContextModule( const QString&,
62 const QString&, const QString& ) ) );
65 HYDROGUI_Operation::~HYDROGUI_Operation()
67 DEBTRACE("~HYDROGUI_Operation");
70 void HYDROGUI_Operation::setName( const QString& theName )
75 const QString& HYDROGUI_Operation::getName() const
80 HYDROGUI_InputPanel* HYDROGUI_Operation::inputPanel() const
82 DEBTRACE("inputPanel");
85 ( ( HYDROGUI_Operation* )this )->myPanel = createInputPanel();
88 connect( myPanel, SIGNAL( panelApplyAndClose() ), this, SLOT( onApplyAndClose() ) );
89 connect( myPanel, SIGNAL( panelApply() ), this, SLOT( onApply() ) );
90 connect( myPanel, SIGNAL( panelCancel() ), this, SLOT( onCancel() ) );
91 connect( myPanel, SIGNAL( panelHelp() ), this, SLOT( onHelp() ) );
97 SUIT_SelectionMgr* HYDROGUI_Operation::selectionMgr() const
99 return myModule->getApp()->selectionMgr();
102 HYDROGUI_Module* HYDROGUI_Operation::module() const
108 * Returns Z layer of the operation preview.
109 \ returns a layer position
111 int HYDROGUI_Operation::getPreviewZLayer() const
113 return myPreviewZLayer;
117 * Update Z layer for the operation preview.
118 \param theLayer a layer position
120 void HYDROGUI_Operation::updatePreviewZLayer( int theLayer )
122 setPreviewZLayer( theLayer );
124 HYDROGUI_Shape* aPreview = getPreviewShape();
126 aPreview->setZLayer( getPreviewZLayer() );
130 * Set Z layer for the operation preview.
131 \param theLayer a layer position
133 void HYDROGUI_Operation::setPreviewZLayer( int theLayer )
135 if ( theLayer != myPreviewZLayer )
136 myPreviewZLayer = theLayer;
140 * Returns a shape preview of the operation
142 HYDROGUI_Shape* HYDROGUI_Operation::getPreviewShape() const
148 * Return the operation preview manager
150 OCCViewer_ViewManager* HYDROGUI_Operation::getPreviewManager()
152 return myPreviewManager;
156 * Set the preview manager
158 void HYDROGUI_Operation::setPreviewManager( OCCViewer_ViewManager* theManager )
160 //No good: preview Z layer could be used by usual presentations
161 //if ( !theManager && myPreviewManager )
162 // module()->getOCCDisplayer()->RemoveZLayer( myPreviewManager, getPreviewZLayer() );
164 myPreviewManager = theManager;
165 // the manager can be null on abort Operation and we need to clear ZLayers on first call, when on new document (second study without SALOME restart)
166 OCCViewer_ViewManager* aViewManager = myPreviewManager;
168 aViewManager = dynamic_cast<OCCViewer_ViewManager*>( module()->getApp()->getViewManager( OCCViewer_Viewer::Type(), true ) );
169 DEBTRACE("aViewManager, myPreviewManager " << aViewManager << " " << myPreviewManager);
173 module()->getOCCDisplayer()->RemoveZLayer(aViewManager, 0, true);
176 if ( myPreviewManager )
177 setPreviewZLayer( module()->getOCCDisplayer()->AddPreviewZLayer( myPreviewManager ) );
180 void HYDROGUI_Operation::setCursor()
182 if ( myPreviewManager )
184 QVector<SUIT_ViewWindow*> winList = myPreviewManager->getViews();
185 for ( QVector<SUIT_ViewWindow*>::iterator it = winList.begin(); it != winList.end(); ++it )
187 OCCViewer_ViewWindow* occWin = ::qobject_cast<OCCViewer_ViewWindow*>( *it );
190 OCCViewer_ViewPort3d* vp = occWin->getViewPort();
194 myCursor = vp->cursor();
195 // Set specific cursor chosen in preferences
196 QCursor aCursor = module()->getPrefEditCursor();
197 vp->setDefaultCursor( aCursor.shape() );
198 vp->setCursor( *vp->getDefaultCursor() );
205 void HYDROGUI_Operation::restoreCursor()
207 if ( myPreviewManager )
209 QVector<SUIT_ViewWindow*> winList = myPreviewManager->getViews();
210 for ( QVector<SUIT_ViewWindow*>::iterator it = winList.begin(); it != winList.end(); ++it )
212 OCCViewer_ViewWindow* occWin = ::qobject_cast<OCCViewer_ViewWindow*>( *it );
215 OCCViewer_ViewPort3d* vp = occWin->getViewPort();
218 // Restore old cursor
219 vp->setDefaultCursor( myCursor.shape() );
220 vp->setCursor( myCursor );
227 void HYDROGUI_Operation::setIsApplyAndClose( const bool theFlag )
229 myIsApplyAndClose = theFlag;
232 bool HYDROGUI_Operation::isApplyAndClose() const
234 return myIsApplyAndClose;
237 void HYDROGUI_Operation::apply()
239 QApplication::setOverrideCursor( Qt::WaitCursor );
243 int anUpdateFlags = 0;
246 bool aResult = false;
247 QStringList aBrowseObjectsEntries;
251 aResult = processApply( anUpdateFlags, anErrorMsg, aBrowseObjectsEntries );
253 catch ( Standard_Failure& e )
256 anErrorMsg = e.GetMessageString();
258 anErrorMsg = "failure unknown: catch ( Standard_Failure ) does not give access to failure!";
266 QApplication::restoreOverrideCursor();
270 module()->update( anUpdateFlags );
271 commitDocOperation();
273 browseObjects( aBrowseObjectsEntries, myIsApplyAndClose );
275 if ( !myIsApplyAndClose && inputPanel() )
278 else if( !anErrorMsg.isEmpty() )
280 // Abort document opeartion only if requested
281 if ( isToAbortOnApply() )
284 printErrorMessage( anErrorMsg );
286 // If the operation has no input panel - do abort
287 if ( !inputPanel() ) {
293 void HYDROGUI_Operation::startOperation()
295 LightApp_Operation::startOperation();
296 myModule->getActiveOperations().push( this );
298 if( myIsApplyAndClose && inputPanel() )
300 myModule->getApp()->desktop()->addDockWidget( Qt::RightDockWidgetArea, inputPanel() );
301 inputPanel()->show();
305 void HYDROGUI_Operation::abortOperation()
307 LightApp_Operation::abortOperation();
311 void HYDROGUI_Operation::commitOperation()
313 LightApp_Operation::commitOperation();
314 if ( myIsApplyAndClose )
318 void HYDROGUI_Operation::stopOperation()
320 LightApp_Operation::stopOperation();
322 // pop the operation from the cached map of active operations
323 QStack<HYDROGUI_Operation*>& anOperations = myModule->getActiveOperations();
324 if ( !anOperations.empty() ) {
325 if ( anOperations.top() == this )
329 // find in the stack the current operation and remove it from the stack
330 QVectorIterator<HYDROGUI_Operation*> aVIt( anOperations );
332 aVIt.previous(); // skip the top show/hide operation
333 while ( aVIt.hasPrevious() )
335 HYDROGUI_Operation* anOp = aVIt.previous();
337 anOperations.remove( anOperations.lastIndexOf( anOp ) );
341 // release the preview manager with removing the added preview Z layer
342 setPreviewManager( 0 );
345 void HYDROGUI_Operation::setDialogActive( const bool active )
347 LightApp_Operation::setDialogActive( active );
357 HYDROGUI_InputPanel* HYDROGUI_Operation::createInputPanel() const
362 void HYDROGUI_Operation::closeInputPanel()
366 myModule->getApp()->desktop()->removeDockWidget( myPanel );
372 bool HYDROGUI_Operation::processApply( int& theUpdateFlags,
373 QString& theErrorMsg,
374 QStringList& theBrowseObjectsEntries )
379 void HYDROGUI_Operation::processCancel()
383 void HYDROGUI_Operation::startDocOperation()
385 // Open transaction in the model document only if it not
386 // already opened by other operation (intended for nested operations)
387 if ( !doc()->IsOperation() )
389 doc()->StartOperation();
390 myIsTransactionOpened = true;
394 void HYDROGUI_Operation::abortDocOperation()
396 // Abort transaction in the model document only if it was
397 // opened by this operation (intended for nested operations)
398 if ( myIsTransactionOpened && doc()->IsOperation() )
400 doc()->AbortOperation();
401 myIsTransactionOpened = false;
405 void HYDROGUI_Operation::commitDocOperation()
407 // Commit transaction in the model document only if it was
408 // opened by this operation (intended for nested operations)
409 if ( myIsTransactionOpened && doc()->IsOperation() )
411 doc()->CommitOperation( HYDROGUI_Tool::ToExtString( getName() ) );
412 myIsTransactionOpened = false;
416 Handle(HYDROData_Document) HYDROGUI_Operation::doc() const
418 return HYDROData_Document::Document();
421 void HYDROGUI_Operation::onApplyAndClose()
423 myIsApplyAndClose = true;
425 restoreOCCViewerSelection();
428 void HYDROGUI_Operation::onApply()
430 myIsApplyAndClose = false;
432 restoreOCCViewerSelection();
435 void HYDROGUI_Operation::setPrintErrorMessage( const bool theIsPrint )
437 myIsPrintErrorMessage = theIsPrint;
440 void HYDROGUI_Operation::printErrorMessage( const QString& theErrorMsg )
442 if ( myIsPrintErrorMessage )
444 QString aMsg = tr( "INPUT_VALID_DATA" );
445 if( !theErrorMsg.isEmpty() )
446 aMsg.prepend( theErrorMsg + "\n" );
447 SUIT_MessageBox::critical( module()->getApp()->desktop(),
448 tr( "INSUFFICIENT_INPUT_DATA" ),
453 myIsPrintErrorMessage = true;
456 void HYDROGUI_Operation::onCancel()
462 myIsApplyAndClose = true;
463 restoreOCCViewerSelection();
466 void HYDROGUI_Operation::restoreOCCViewerSelection(OCCViewer_ViewManager* theViewManager)
468 DEBTRACE("restoreOCCViewerSelection " << theViewManager);
469 LightApp_Application* anApp = module()->getApp();
470 LightApp_SelectionMgr* sm = anApp->selectionMgr();
473 sm->clearFilters(); // see GEOM_Displayer::GlobalSelection
475 OCCViewer_ViewManager* aViewManager = theViewManager;
477 aViewManager = dynamic_cast<OCCViewer_ViewManager*>( anApp->getViewManager( OCCViewer_Viewer::Type(), true ) );
480 if( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
482 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
485 DEBTRACE("aViewManager " << aViewManager << " AIScontext " << aCtx.get());
488 aCtx->SetAutomaticHilight( Standard_True );
489 aCtx->RemoveFilters();
494 void HYDROGUI_Operation::onHelp()
496 emit helpContextModule( getHelpComponent(), getHelpFile(), getHelpContext() );
499 QString HYDROGUI_Operation::getHelpComponent() const
501 return module()->moduleName();
504 QString HYDROGUI_Operation::getHelpFile() const
506 QString aFileName = ((myName.isEmpty())? operationName() : myName);
507 aFileName = aFileName.toLower();
508 aFileName = aFileName.replace(QRegExp("\\s"), "_").append(".html");
509 aFileName.remove( "create_" );
510 aFileName.remove( "edit_" );
514 QString HYDROGUI_Operation::getHelpContext() const
519 void HYDROGUI_Operation::browseObjects( const QStringList& theBrowseObjectsEntries,
520 const bool theIsApplyAndClose/* = true*/ )
522 bool isOptimizedBrowse = true;
523 module()->getApp()->browseObjects( theBrowseObjectsEntries, theIsApplyAndClose, isOptimizedBrowse );