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>
43 HYDROGUI_Operation::HYDROGUI_Operation( HYDROGUI_Module* theModule )
44 : LightApp_Operation(),
45 myModule( theModule ),
47 myIsPrintErrorMessage( true ),
48 myIsTransactionOpened( false ),
49 myPreviewManager( 0 ),
50 myPreviewZLayer( -1 ),
51 myIsApplyAndClose( true )
53 connect( this, SIGNAL( helpContextModule( const QString&, const QString&,
55 theModule->application(), SLOT( onHelpContextModule( const QString&,
56 const QString&, const QString& ) ) );
59 HYDROGUI_Operation::~HYDROGUI_Operation()
63 void HYDROGUI_Operation::setName( const QString& theName )
68 const QString& HYDROGUI_Operation::getName() const
73 HYDROGUI_InputPanel* HYDROGUI_Operation::inputPanel() const
77 ( ( HYDROGUI_Operation* )this )->myPanel = createInputPanel();
78 connect( myPanel, SIGNAL( panelApplyAndClose() ), this, SLOT( onApplyAndClose() ) );
79 connect( myPanel, SIGNAL( panelApply() ), this, SLOT( onApply() ) );
80 connect( myPanel, SIGNAL( panelCancel() ), this, SLOT( onCancel() ) );
81 connect( myPanel, SIGNAL( panelHelp() ), this, SLOT( onHelp() ) );
86 SUIT_SelectionMgr* HYDROGUI_Operation::selectionMgr() const
88 return myModule->getApp()->selectionMgr();
91 HYDROGUI_Module* HYDROGUI_Operation::module() const
97 * Returns Z layer of the operation preview.
98 \ returns a layer position
100 int HYDROGUI_Operation::getPreviewZLayer() const
102 return myPreviewZLayer;
106 * Update Z layer for the operation preview.
107 \param theLayer a layer position
109 void HYDROGUI_Operation::updatePreviewZLayer( int theLayer )
111 setPreviewZLayer( theLayer );
113 HYDROGUI_Shape* aPreview = getPreviewShape();
115 aPreview->setZLayer( getPreviewZLayer() );
119 * Set Z layer for the operation preview.
120 \param theLayer a layer position
122 void HYDROGUI_Operation::setPreviewZLayer( int theLayer )
124 if ( theLayer != myPreviewZLayer )
125 myPreviewZLayer = theLayer;
129 * Returns a shape preview of the operation
131 HYDROGUI_Shape* HYDROGUI_Operation::getPreviewShape() const
137 * Return the operation preview manager
139 OCCViewer_ViewManager* HYDROGUI_Operation::getPreviewManager()
141 return myPreviewManager;
145 * Set the preview manager
147 void HYDROGUI_Operation::setPreviewManager( OCCViewer_ViewManager* theManager )
149 //No good: preview Z layer could be used by usual presentations
150 //if ( !theManager && myPreviewManager )
151 // module()->getOCCDisplayer()->RemoveZLayer( myPreviewManager, getPreviewZLayer() );
153 myPreviewManager = theManager;
155 if ( myPreviewManager )
156 setPreviewZLayer( module()->getOCCDisplayer()->AddPreviewZLayer( myPreviewManager ) );
159 void HYDROGUI_Operation::setCursor()
161 if ( myPreviewManager )
163 QVector<SUIT_ViewWindow*> winList = myPreviewManager->getViews();
164 for ( QVector<SUIT_ViewWindow*>::iterator it = winList.begin(); it != winList.end(); ++it )
166 OCCViewer_ViewWindow* occWin = ::qobject_cast<OCCViewer_ViewWindow*>( *it );
169 OCCViewer_ViewPort3d* vp = occWin->getViewPort();
173 myCursor = vp->cursor();
174 // Set specific cursor chosen in preferences
175 QCursor aCursor = module()->getPrefEditCursor();
176 vp->setDefaultCursor( aCursor.shape() );
177 vp->setCursor( *vp->getDefaultCursor() );
184 void HYDROGUI_Operation::restoreCursor()
186 if ( myPreviewManager )
188 QVector<SUIT_ViewWindow*> winList = myPreviewManager->getViews();
189 for ( QVector<SUIT_ViewWindow*>::iterator it = winList.begin(); it != winList.end(); ++it )
191 OCCViewer_ViewWindow* occWin = ::qobject_cast<OCCViewer_ViewWindow*>( *it );
194 OCCViewer_ViewPort3d* vp = occWin->getViewPort();
197 // Restore old cursor
198 vp->setDefaultCursor( myCursor.shape() );
199 vp->setCursor( myCursor );
206 void HYDROGUI_Operation::setIsApplyAndClose( const bool theFlag )
208 myIsApplyAndClose = theFlag;
211 bool HYDROGUI_Operation::isApplyAndClose() const
213 return myIsApplyAndClose;
216 void HYDROGUI_Operation::apply()
218 QApplication::setOverrideCursor( Qt::WaitCursor );
222 int anUpdateFlags = 0;
225 bool aResult = false;
226 QStringList aBrowseObjectsEntries;
230 aResult = processApply( anUpdateFlags, anErrorMsg, aBrowseObjectsEntries );
232 catch ( Standard_Failure )
234 Handle(Standard_Failure) aFailure = Standard_Failure::Caught();
235 anErrorMsg = aFailure->GetMessageString();
243 QApplication::restoreOverrideCursor();
247 module()->update( anUpdateFlags );
248 commitDocOperation();
250 browseObjects( aBrowseObjectsEntries, myIsApplyAndClose );
252 if ( !myIsApplyAndClose && inputPanel() )
255 else if( !anErrorMsg.isEmpty() )
257 // Abort document opeartion only if requested
258 if ( isToAbortOnApply() )
261 printErrorMessage( anErrorMsg );
263 // If the operation has no input panel - do abort
264 if ( !inputPanel() ) {
270 void HYDROGUI_Operation::startOperation()
272 LightApp_Operation::startOperation();
273 myModule->getActiveOperations().push( this );
275 if( myIsApplyAndClose && inputPanel() )
277 myModule->getApp()->desktop()->addDockWidget( Qt::RightDockWidgetArea, inputPanel() );
278 inputPanel()->show();
282 void HYDROGUI_Operation::abortOperation()
284 LightApp_Operation::abortOperation();
288 void HYDROGUI_Operation::commitOperation()
290 LightApp_Operation::commitOperation();
291 if ( myIsApplyAndClose )
295 void HYDROGUI_Operation::stopOperation()
297 LightApp_Operation::stopOperation();
299 // pop the operation from the cached map of active operations
300 QStack<HYDROGUI_Operation*>& anOperations = myModule->getActiveOperations();
301 if ( !anOperations.empty() ) {
302 if ( anOperations.top() == this )
306 // find in the stack the current operation and remove it from the stack
307 QVectorIterator<HYDROGUI_Operation*> aVIt( anOperations );
309 aVIt.previous(); // skip the top show/hide operation
310 while ( aVIt.hasPrevious() )
312 HYDROGUI_Operation* anOp = aVIt.previous();
314 anOperations.remove( anOperations.lastIndexOf( anOp ) );
318 // release the preview manager with removing the added preview Z layer
319 setPreviewManager( 0 );
322 void HYDROGUI_Operation::setDialogActive( const bool active )
324 LightApp_Operation::setDialogActive( active );
334 HYDROGUI_InputPanel* HYDROGUI_Operation::createInputPanel() const
339 void HYDROGUI_Operation::closeInputPanel()
343 myModule->getApp()->desktop()->removeDockWidget( myPanel );
349 bool HYDROGUI_Operation::processApply( int& theUpdateFlags,
350 QString& theErrorMsg,
351 QStringList& theBrowseObjectsEntries )
356 void HYDROGUI_Operation::processCancel()
360 void HYDROGUI_Operation::startDocOperation()
362 // Open transaction in the model document only if it not
363 // already opened by other operation (intended for nested operations)
364 if ( !doc()->IsOperation() )
366 doc()->StartOperation();
367 myIsTransactionOpened = true;
371 void HYDROGUI_Operation::abortDocOperation()
373 // Abort transaction in the model document only if it was
374 // opened by this operation (intended for nested operations)
375 if ( myIsTransactionOpened && doc()->IsOperation() )
377 doc()->AbortOperation();
378 myIsTransactionOpened = false;
382 void HYDROGUI_Operation::commitDocOperation()
384 // Commit transaction in the model document only if it was
385 // opened by this operation (intended for nested operations)
386 if ( myIsTransactionOpened && doc()->IsOperation() )
388 doc()->CommitOperation( HYDROGUI_Tool::ToExtString( getName() ) );
389 myIsTransactionOpened = false;
393 Handle_HYDROData_Document HYDROGUI_Operation::doc() const
395 return HYDROData_Document::Document( myModule->getStudyId() );
398 void HYDROGUI_Operation::onApplyAndClose()
400 myIsApplyAndClose = true;
404 void HYDROGUI_Operation::onApply()
406 myIsApplyAndClose = false;
410 void HYDROGUI_Operation::setPrintErrorMessage( const bool theIsPrint )
412 myIsPrintErrorMessage = theIsPrint;
415 void HYDROGUI_Operation::printErrorMessage( const QString& theErrorMsg )
417 if ( myIsPrintErrorMessage )
419 QString aMsg = tr( "INPUT_VALID_DATA" );
420 if( !theErrorMsg.isEmpty() )
421 aMsg.prepend( theErrorMsg + "\n" );
422 SUIT_MessageBox::critical( module()->getApp()->desktop(),
423 tr( "INSUFFICIENT_INPUT_DATA" ),
428 myIsPrintErrorMessage = true;
431 void HYDROGUI_Operation::onCancel()
437 myIsApplyAndClose = true;
440 void HYDROGUI_Operation::onHelp()
442 emit helpContextModule( getHelpComponent(), getHelpFile(), getHelpContext() );
445 QString HYDROGUI_Operation::getHelpComponent() const
447 return module()->moduleName();
450 QString HYDROGUI_Operation::getHelpFile() const
452 QString aFileName = ((myName.isEmpty())? operationName() : myName);
453 aFileName = aFileName.toLower();
454 aFileName = aFileName.replace(QRegExp("\\s"), "_").append(".html");
455 aFileName.remove( "create_" );
456 aFileName.remove( "edit_" );
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 );