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 ),
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
80 ( ( HYDROGUI_Operation* )this )->myPanel = createInputPanel();
81 connect( myPanel, SIGNAL( panelApply() ), this, SLOT( onApply() ) );
82 connect( myPanel, SIGNAL( panelCancel() ), this, SLOT( onCancel() ) );
83 connect( myPanel, SIGNAL( panelHelp() ), this, SLOT( onHelp() ) );
88 SUIT_SelectionMgr* HYDROGUI_Operation::selectionMgr() const
90 return myModule->getApp()->selectionMgr();
93 HYDROGUI_Module* HYDROGUI_Operation::module() const
99 * Returns Z layer of the operation preview.
100 \ returns a layer position
102 int HYDROGUI_Operation::getPreviewZLayer() const
104 return myPreviewZLayer;
108 * Update Z layer for the operation preview.
109 \param theLayer a layer position
111 void HYDROGUI_Operation::updatePreviewZLayer( int theLayer )
113 setPreviewZLayer( theLayer );
115 HYDROGUI_Shape* aPreview = getPreviewShape();
117 aPreview->setZLayer( getPreviewZLayer() );
121 * Set Z layer for the operation preview.
122 \param theLayer a layer position
124 void HYDROGUI_Operation::setPreviewZLayer( int theLayer )
126 if ( theLayer != myPreviewZLayer )
127 myPreviewZLayer = theLayer;
131 * Returns a shape preview of the operation
133 HYDROGUI_Shape* HYDROGUI_Operation::getPreviewShape() const
139 * Return the operation preview manager
141 OCCViewer_ViewManager* HYDROGUI_Operation::getPreviewManager()
143 return myPreviewManager;
147 * Set the preview manager
149 void HYDROGUI_Operation::setPreviewManager( OCCViewer_ViewManager* theManager )
151 //No good: preview Z layer could be used by usual presentations
152 //if ( !theManager && myPreviewManager )
153 // module()->getOCCDisplayer()->RemoveZLayer( myPreviewManager, getPreviewZLayer() );
155 myPreviewManager = theManager;
157 if ( myPreviewManager )
158 setPreviewZLayer( module()->getOCCDisplayer()->AddPreviewZLayer( myPreviewManager ) );
161 void HYDROGUI_Operation::setCursor()
163 if ( myPreviewManager )
165 QVector<SUIT_ViewWindow*> winList = myPreviewManager->getViews();
166 for ( QVector<SUIT_ViewWindow*>::iterator it = winList.begin(); it != winList.end(); ++it )
168 OCCViewer_ViewWindow* occWin = ::qobject_cast<OCCViewer_ViewWindow*>( *it );
171 OCCViewer_ViewPort3d* vp = occWin->getViewPort();
175 myCursor = vp->cursor();
176 // Set specific cursor chosen in preferences
177 QCursor aCursor = module()->getPrefEditCursor();
178 vp->setDefaultCursor( aCursor.shape() );
179 vp->setCursor( *vp->getDefaultCursor() );
186 void HYDROGUI_Operation::restoreCursor()
188 if ( myPreviewManager )
190 QVector<SUIT_ViewWindow*> winList = myPreviewManager->getViews();
191 for ( QVector<SUIT_ViewWindow*>::iterator it = winList.begin(); it != winList.end(); ++it )
193 OCCViewer_ViewWindow* occWin = ::qobject_cast<OCCViewer_ViewWindow*>( *it );
196 OCCViewer_ViewPort3d* vp = occWin->getViewPort();
199 // Restore old cursor
200 vp->setDefaultCursor( myCursor.shape() );
201 vp->setCursor( myCursor );
208 void HYDROGUI_Operation::startOperation()
210 LightApp_Operation::startOperation();
211 myModule->getActiveOperations().push( this );
215 myModule->getApp()->desktop()->addDockWidget( Qt::RightDockWidgetArea, inputPanel() );
216 inputPanel()->show();
220 void HYDROGUI_Operation::abortOperation()
222 LightApp_Operation::abortOperation();
226 void HYDROGUI_Operation::commitOperation()
228 LightApp_Operation::commitOperation();
232 void HYDROGUI_Operation::stopOperation()
234 LightApp_Operation::stopOperation();
236 // pop the operation from the cached map of active operations
237 QStack<HYDROGUI_Operation*>& anOperations = myModule->getActiveOperations();
238 if ( !anOperations.empty() ) {
239 if ( anOperations.top() == this )
243 // find in the stack the current operation and remove it from the stack
244 QVectorIterator<HYDROGUI_Operation*> aVIt( anOperations );
246 aVIt.previous(); // skip the top show/hide operation
247 while ( aVIt.hasPrevious() )
249 HYDROGUI_Operation* anOp = aVIt.previous();
251 anOperations.remove( anOperations.lastIndexOf( anOp ) );
255 // release the preview manager with removing the added preview Z layer
256 setPreviewManager( 0 );
259 void HYDROGUI_Operation::setDialogActive( const bool active )
261 LightApp_Operation::setDialogActive( active );
271 HYDROGUI_InputPanel* HYDROGUI_Operation::createInputPanel() const
276 void HYDROGUI_Operation::closeInputPanel()
280 myModule->getApp()->desktop()->removeDockWidget( myPanel );
286 bool HYDROGUI_Operation::processApply( int& theUpdateFlags,
287 QString& theErrorMsg,
288 QStringList& theBrowseObjectsEntries )
293 void HYDROGUI_Operation::processCancel()
297 void HYDROGUI_Operation::startDocOperation()
299 // Open transaction in the model document only if it not
300 // already opened by other operation (intended for nested operations)
301 if ( !doc()->IsOperation() )
303 doc()->StartOperation();
304 myIsTransactionOpened = true;
308 void HYDROGUI_Operation::abortDocOperation()
310 // Abort transaction in the model document only if it was
311 // opened by this operation (intended for nested operations)
312 if ( myIsTransactionOpened && doc()->IsOperation() )
314 doc()->AbortOperation();
315 myIsTransactionOpened = false;
319 void HYDROGUI_Operation::commitDocOperation()
321 // Commit transaction in the model document only if it was
322 // opened by this operation (intended for nested operations)
323 if ( myIsTransactionOpened && doc()->IsOperation() )
325 doc()->CommitOperation( HYDROGUI_Tool::ToExtString( getName() ) );
326 myIsTransactionOpened = false;
330 Handle_HYDROData_Document HYDROGUI_Operation::doc() const
332 return HYDROData_Document::Document( myModule->getStudyId() );
335 void HYDROGUI_Operation::onApply()
337 QApplication::setOverrideCursor( Qt::WaitCursor );
341 int anUpdateFlags = 0;
344 bool aResult = false;
345 QStringList aBrowseObjectsEntries;
349 aResult = processApply( anUpdateFlags, anErrorMsg, aBrowseObjectsEntries );
351 catch ( Standard_Failure )
353 Handle(Standard_Failure) aFailure = Standard_Failure::Caught();
354 anErrorMsg = aFailure->GetMessageString();
362 QApplication::restoreOverrideCursor();
366 module()->update( anUpdateFlags );
367 commitDocOperation();
369 browseObjects( aBrowseObjectsEntries );
373 // Abort document opeartion only if requested
374 if ( isToAbortOnApply() )
377 printErrorMessage( anErrorMsg );
379 // If the operation has no input panel - do abort
380 if ( !inputPanel() ) {
386 void HYDROGUI_Operation::setPrintErrorMessage( const bool theIsPrint )
388 myIsPrintErrorMessage = theIsPrint;
391 void HYDROGUI_Operation::printErrorMessage( const QString& theErrorMsg )
393 if ( myIsPrintErrorMessage )
395 QString aMsg = tr( "INPUT_VALID_DATA" );
396 if( !theErrorMsg.isEmpty() )
397 aMsg.prepend( theErrorMsg + "\n" );
398 SUIT_MessageBox::critical( module()->getApp()->desktop(),
399 tr( "INSUFFICIENT_INPUT_DATA" ),
404 myIsPrintErrorMessage = true;
407 void HYDROGUI_Operation::onCancel()
413 void HYDROGUI_Operation::onHelp()
415 emit helpContextModule( getHelpComponent(), getHelpFile(), getHelpContext() );
418 QString HYDROGUI_Operation::getHelpComponent() const
420 return module()->moduleName();
423 QString HYDROGUI_Operation::getHelpFile() const
425 QString aFileName = ((myName.isEmpty())? operationName() : myName);
426 return aFileName.replace(QRegExp("\\s"), "_").append(".html");
429 QString HYDROGUI_Operation::getHelpContext() const
434 void HYDROGUI_Operation::browseObjects( const QStringList& theBrowseObjectsEntries )
436 bool isApplyAndClose = true;
437 bool isOptimizedBrowse = true;
438 module()->getApp()->browseObjects( theBrowseObjectsEntries, isApplyAndClose, isOptimizedBrowse );