1 // Copyright (C) 2007-2014 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 // File: LightApp_Module.cxx
24 // Created: 6/20/2005 16:30:56 AM
27 #include "LightApp_Module.h"
29 #include "CAM_Application.h"
31 #include "LightApp_Application.h"
32 #include "LightApp_DataModel.h"
33 #include "LightApp_DataObject.h"
34 #include "LightApp_Study.h"
35 #include "LightApp_Preferences.h"
36 #include "LightApp_Selection.h"
37 #include "LightApp_Operation.h"
38 #include "LightApp_SwitchOp.h"
39 #include "LightApp_UpdateFlags.h"
40 #include "LightApp_ShowHideOp.h"
41 #include "LightApp_SelectionMgr.h"
43 #include <SUIT_Study.h>
44 #include <SUIT_DataObject.h>
45 #include <SUIT_DataBrowser.h>
46 #include <SUIT_Operation.h>
47 #include <SUIT_ViewManager.h>
48 #include <SUIT_ResourceMgr.h>
49 #include <SUIT_ShortcutMgr.h>
50 #include <SUIT_Desktop.h>
51 #include <SUIT_TreeModel.h>
53 #ifndef DISABLE_SALOMEOBJECT
54 #include <SALOME_ListIO.hxx>
55 #include <SALOME_ListIteratorOfListIO.hxx>
58 #ifndef DISABLE_VTKVIEWER
59 #ifndef DISABLE_SALOMEOBJECT
60 #include <SVTK_ViewWindow.h>
61 #include <SVTK_ViewModel.h>
63 #include <VTKViewer_ViewWindow.h>
65 #include <VTKViewer_ViewModel.h>
67 #ifndef DISABLE_OCCVIEWER
68 #include <OCCViewer_ViewWindow.h>
69 #include <OCCViewer_ViewPort3d.h>
70 #ifndef DISABLE_SALOMEOBJECT
71 #include <SOCC_ViewModel.h>
73 #include <OCCViewer_ViewModel.h>
76 #ifndef DISABLE_GLVIEWER
77 #include <GLViewer_ViewFrame.h>
78 #include <GLViewer_ViewPort.h>
80 #ifndef DISABLE_PLOT2DVIEWER
81 #include <Plot2d_ViewWindow.h>
82 #include <Plot2d_ViewFrame.h>
83 #ifndef DISABLE_SALOMEOBJECT
84 #include <SPlot2d_ViewModel.h>
86 #include <Plot2d_ViewModel.h>
90 #include <QtxPopupMgr.h>
94 #include <QStringList>
100 LightApp_Module::LightApp_Module( const QString& name )
101 : CAM_Module( name ),
112 LightApp_Module::~LightApp_Module()
118 /*!Initialize module.*/
119 void LightApp_Module::initialize( CAM_Application* app )
121 CAM_Module::initialize( app );
123 SUIT_ResourceMgr* resMgr = app ? app->resourceMgr() : 0;
125 resMgr->raiseTranslators( name() );
129 void LightApp_Module::windows( QMap<int, int>& ) const
134 void LightApp_Module::viewManagers( QStringList& ) const
138 /*!Context menu popup.*/
139 void LightApp_Module::contextMenuPopup( const QString& client, QMenu* menu, QString& /*title*/ )
141 LightApp_Selection* sel = createSelection();
142 sel->init( client, getApp()->selectionMgr() );
144 popupMgr()->setSelection( sel );
145 popupMgr()->setMenu( menu );
146 popupMgr()->updateMenu();
149 /*!Update object browser.
150 * For updating model or whole object browser use update() method can be used.
152 void LightApp_Module::updateObjBrowser( bool theIsUpdateDataModel,
153 SUIT_DataObject* theDataObject )
155 bool upd = getApp()->objectBrowser()->autoUpdate();
156 getApp()->objectBrowser()->setAutoUpdate( false );
158 if( theIsUpdateDataModel ){
159 if( CAM_DataModel* aDataModel = dataModel() ){
160 if ( LightApp_DataModel* aModel = dynamic_cast<LightApp_DataModel*>( aDataModel ) ) {
161 //SUIT_DataObject* aParent = NULL;
162 //if(theDataObject && theDataObject != aDataModel->root())
163 // aParent = theDataObject->parent();
165 LightApp_DataObject* anObject = dynamic_cast<LightApp_DataObject*>(theDataObject);
166 LightApp_Study* aStudy = dynamic_cast<LightApp_Study*>(getApp()->activeStudy());
167 aModel->update( anObject, aStudy );
172 getApp()->objectBrowser()->setAutoUpdate( upd );
173 getApp()->objectBrowser()->updateTree( 0, false );
177 void LightApp_Module::selectionChanged()
181 /*! \brief If return false, selection will be cleared at module activation
183 bool LightApp_Module::isSelectionCompatible()
185 // return true if selected objects belong to this module
186 bool isCompatible = true;
187 #ifndef DISABLE_SALOMEOBJECT
188 SALOME_ListIO selected;
189 if ( LightApp_SelectionMgr *Sel = getApp()->selectionMgr() )
190 Sel->selectedObjects( selected );
192 LightApp_Study* aStudy = dynamic_cast<LightApp_Study*>( getApp()->activeStudy() );
193 LightApp_DataObject* aRoot = dynamic_cast<LightApp_DataObject*>( dataModel()->root() );
194 if ( aStudy && aRoot ) {
196 QString moduleDataType = aRoot->componentDataType();
197 // check data type of selection
198 SALOME_ListIteratorOfListIO It( selected );
199 for ( ; isCompatible && It.More(); It.Next()) {
200 Handle(SALOME_InteractiveObject)& io = It.Value();
201 isCompatible = ( aStudy->componentDataType( io->getEntry() ) == moduleDataType );
208 /*!Activate module.*/
209 bool LightApp_Module::activateModule( SUIT_Study* study )
211 bool res = CAM_Module::activateModule( study );
213 if ( !isSelectionCompatible() )// PAL19290, PAL18352
214 getApp()->selectionMgr()->clearSelected();
216 if ( res && application() && application()->resourceMgr() )
217 application()->resourceMgr()->raiseTranslators( name() );
219 connect( application(), SIGNAL( viewManagerAdded( SUIT_ViewManager* ) ),
220 this, SLOT( onViewManagerAdded( SUIT_ViewManager* ) ) );
221 connect( application(), SIGNAL( viewManagerRemoved( SUIT_ViewManager* ) ),
222 this, SLOT( onViewManagerRemoved( SUIT_ViewManager* ) ) );
224 if ( mySwitchOp == 0 )
225 mySwitchOp = new LightApp_SwitchOp( this );
227 // Enable Display and Erase actions
228 if ( action(myDisplay) )
229 action(myDisplay)->setEnabled(true);
230 if ( action(myErase) )
231 action(myErase)->setEnabled(true);
233 application()->shortcutMgr()->setSectionEnabled( moduleName() );
235 /* BUG 0020498 : The Entry column is always shown at module activation
236 The registration of column is moved into LightApp_Application
238 QString EntryCol = QObject::tr( "ENTRY_COLUMN" );
239 LightApp_DataModel* m = dynamic_cast<LightApp_DataModel*>( dataModel() );
242 SUIT_AbstractModel* treeModel = dynamic_cast<SUIT_AbstractModel*>( getApp()->objectBrowser()->model() );
243 m->registerColumn( getApp()->objectBrowser(), EntryCol, LightApp_DataObject::EntryId );
244 treeModel->setAppropriate( EntryCol, Qtx::Toggled );
249 /*!Deactivate module.*/
250 bool LightApp_Module::deactivateModule( SUIT_Study* study )
255 disconnect( application(), SIGNAL( viewManagerAdded( SUIT_ViewManager* ) ),
256 this, SLOT( onViewManagerAdded( SUIT_ViewManager* ) ) );
257 disconnect( application(), SIGNAL( viewManagerRemoved( SUIT_ViewManager* ) ),
258 this, SLOT( onViewManagerRemoved( SUIT_ViewManager* ) ) );
260 // abort all operations
261 MapOfOperation::const_iterator anIt;
262 for( anIt = myOperations.begin(); anIt != myOperations.end(); anIt++ ) {
263 anIt.value()->abort();
266 // Disable Display and Erase action
267 if ( action(myDisplay) )
268 action(myDisplay)->setEnabled(false);
269 if ( action(myErase) )
270 action(myErase)->setEnabled(false);
272 application()->shortcutMgr()->setSectionEnabled( moduleName(), false );
274 /* BUG 0020498 : The Entry column is always shown at module activation
275 QString EntryCol = QObject::tr( "ENTRY_COLUMN" );
276 LightApp_DataModel* m = dynamic_cast<LightApp_DataModel*>( dataModel() );
279 SUIT_AbstractModel* treeModel = dynamic_cast<SUIT_AbstractModel*>( getApp()->objectBrowser()->model() );
281 treeModel->setAppropriate( EntryCol, Qtx::Shown );
282 m->unregisterColumn( getApp()->objectBrowser(), EntryCol );
285 return CAM_Module::deactivateModule( study );
289 void LightApp_Module::MenuItem()
294 void LightApp_Module::createPreferences()
299 void LightApp_Module::preferencesChanged( const QString&, const QString& )
304 void LightApp_Module::message( const QString& )
308 /*!Gets application.*/
309 LightApp_Application* LightApp_Module::getApp() const
311 return (LightApp_Application*)application();
315 * \brief Update something in accordance with update flags
316 * \param theFlags - update flags
318 * Update viewer or/and object browser etc. in accordance with update flags ( see
319 * LightApp_UpdateFlags enumeration ). Derived modules can redefine this method for their
322 void LightApp_Module::update( const int theFlags )
324 if ( theFlags & UF_Model )
326 if( CAM_DataModel* aDataModel = dataModel() )
327 if( LightApp_DataModel* aModel = dynamic_cast<LightApp_DataModel*>( aDataModel ) )
328 aModel->update( 0, dynamic_cast<LightApp_Study*>( getApp()->activeStudy() ) );
331 if ( theFlags & UF_ObjBrowser )
332 getApp()->objectBrowser()->updateTree( 0 );
334 if ( theFlags & UF_Controls )
336 if ( theFlags & UF_Viewer )
338 if ( SUIT_ViewManager* viewMgr = getApp()->activeViewManager() )
339 if ( SUIT_ViewWindow* viewWnd = viewMgr->getActiveView() )
341 #ifndef DISABLE_VTKVIEWER
342 #ifndef DISABLE_SALOMEOBJECT
343 if ( viewWnd->inherits( "SVTK_ViewWindow" ) )
344 ( (SVTK_ViewWindow*)viewWnd )->Repaint();
346 if ( viewWnd->inherits( "VTKViewer_ViewWindow" ) )
347 ( (VTKViewer_ViewWindow*)viewWnd )->Repaint();
350 #ifndef DISABLE_OCCVIEWER
351 if ( viewWnd->inherits( "OCCViewer_ViewWindow" ) )
352 ( (OCCViewer_ViewWindow*)viewWnd )->getViewPort()->onUpdate();
354 #ifndef DISABLE_PLOT2DVIEWER
355 if ( viewWnd->inherits( "Plot2d_ViewWindow" ) )
356 ( (Plot2d_ViewWindow*)viewWnd )->getViewFrame()->Repaint();
358 #ifndef DISABLE_GLVIEWER
359 if ( viewWnd->inherits( "GLViewer_ViewFrame" ) )
360 ( (GLViewer_ViewFrame*)viewWnd )->getViewPort()->onUpdate();
366 * \brief Updates controls
368 * Updates (i.e. disable/enable) controls states (menus, tool bars etc.). This method is
369 * called from update( UF_Controls ). You may redefine it in concrete module.
371 void LightApp_Module::updateControls()
375 /*!Create new instance of data model and return it.*/
376 CAM_DataModel* LightApp_Module::createDataModel()
378 return new LightApp_DataModel( this );
381 /*!Create and return instance of LightApp_Selection.*/
382 LightApp_Selection* LightApp_Module::createSelection() const
384 return new LightApp_Selection();
388 void LightApp_Module::onModelOpened()
393 void LightApp_Module::onModelSaved()
398 void LightApp_Module::onModelClosed()
402 /*!Gets popup manager.(create if not exist)*/
403 QtxPopupMgr* LightApp_Module::popupMgr()
407 myPopupMgr = new QtxPopupMgr( 0, this );
410 SUIT_Desktop* d = application()->desktop();
413 *disp = createAction( -1, tr( "TOP_SHOW" ), p, tr( "MEN_SHOW" ), tr( "STB_SHOW" ),
414 0, d, false, this, SLOT( onShowHide() ), QString("General:Show object(s)") ),
415 *erase = createAction( -1, tr( "TOP_HIDE" ), p, tr( "MEN_HIDE" ), tr( "STB_HIDE" ),
416 0, d, false, this, SLOT( onShowHide() ) , QString("General:Hide object(s)") ),
417 *dispOnly = createAction( -1, tr( "TOP_DISPLAY_ONLY" ), p, tr( "MEN_DISPLAY_ONLY" ), tr( "STB_DISPLAY_ONLY" ),
418 0, d, false, this, SLOT( onShowHide() ) ),
419 *eraseAll = createAction( -1, tr( "TOP_ERASE_ALL" ), p, tr( "MEN_ERASE_ALL" ), tr( "STB_ERASE_ALL" ),
420 0, d, false, this, SLOT( onShowHide() ) );
421 myDisplay = actionId( disp );
422 myErase = actionId( erase );
423 myDisplayOnly = actionId( dispOnly );
424 myEraseAll = actionId( eraseAll );
426 myPopupMgr->insert( disp, -1, 0 );
427 myPopupMgr->insert( erase, -1, 0 );
428 myPopupMgr->insert( dispOnly, -1, 0 );
429 myPopupMgr->insert( eraseAll, -1, 0 );
430 myPopupMgr->insert( separator(), -1, 0 );
432 QString oneAndNotActive = "( count( $component ) = 1 ) and ( not( activeModule in $component ) )";
433 QString uniform = "true in $canBeDisplayed and %1 and ( activeModule = '%2' )";
434 uniform = uniform.arg( oneAndNotActive ).arg( name() );
435 myPopupMgr->setRule( disp, /*QString( "( not isVisible ) and " ) + */ uniform, QtxPopupMgr::VisibleRule );
436 myPopupMgr->setRule( erase, /*QString( "( isVisible ) and " ) + */ uniform, QtxPopupMgr::VisibleRule );
437 myPopupMgr->setRule( dispOnly, uniform, QtxPopupMgr::VisibleRule );
441 #ifndef DISABLE_OCCVIEWER
442 #ifndef DISABLE_SALOMEOBJECT
443 viewers.append( SOCC_Viewer::Type() );
445 viewers.append( OCCViewer_Viewer::Type() );
448 #ifndef DISABLE_VTKVIEWER
449 #ifndef DISABLE_SALOMEOBJECT
450 viewers.append( SVTK_Viewer::Type() );
452 viewers.append( VTKViewer_Viewer::Type() );
455 #ifndef DISABLE_PLOT2DVIEWER
456 #ifndef DISABLE_SALOMEOBJECT
457 viewers.append( SPlot2d_Viewer::Type() );
459 viewers.append( Plot2d_Viewer::Type() );
463 if( !viewers.isEmpty() )
465 QString strViewers = "{ ", temp = "'%1' ";
466 QStringList::const_iterator anIt = viewers.begin(), aLast = viewers.end();
467 for( ; anIt!=aLast; anIt++ )
468 strViewers+=temp.arg( *anIt );
470 myPopupMgr->setRule( eraseAll, QString( "client in %1" ).arg( strViewers ), QtxPopupMgr::VisibleRule );
476 /*!Gets preferences.*/
477 LightApp_Preferences* LightApp_Module::preferences() const
479 LightApp_Preferences* pref = 0;
481 pref = getApp()->preferences();
485 /*!Add preference to preferences.*/
486 int LightApp_Module::addPreference( const QString& label )
488 LightApp_Preferences* pref = preferences();
492 int catId = pref->addPreference( moduleName(), -1 );
496 return pref->addPreference( label, catId );
499 /*!Add preference to preferences.*/
500 int LightApp_Module::addPreference( const QString& label, const int pId, const int type,
501 const QString& section, const QString& param )
503 LightApp_Preferences* pref = preferences();
507 return pref->addPreference( moduleName(), label, pId, type, section, param );
510 /*!Gets property of preferences.*/
511 QVariant LightApp_Module::preferenceProperty( const int id, const QString& prop ) const
514 LightApp_Preferences* pref = preferences();
516 var = pref->itemProperty( prop, id );
520 /*!Set property of preferences.*/
521 void LightApp_Module::setPreferenceProperty( const int id, const QString& prop, const QVariant& var )
523 LightApp_Preferences* pref = preferences();
525 pref->setItemProperty( prop, var, id );
529 * \brief Starts operation with given identifier
530 * \param id - identifier of operation to be started
532 * Module stores operations in map. This method starts operation by id.
533 * If operation isn't in map, then it will be created by createOperation method
534 * and will be inserted to map
536 void LightApp_Module::startOperation( const int id )
538 LightApp_Operation* op = 0;
539 if( myOperations.contains( id ) && reusableOperation( id ) )
540 op = myOperations[ id ];
543 op = createOperation( id );
546 myOperations.insert( id, op );
547 op->setModule( this );
548 connect( op, SIGNAL( stopped( SUIT_Operation* ) ), this, SLOT( onOperationStopped( SUIT_Operation* ) ) );
549 connect( op, SIGNAL( destroyed() ), this, SLOT( onOperationDestroyed() ) );
555 // be sure that operation has correct study pointer
556 op->setStudy( application() ? application()->activeStudy() : 0 );
562 * \brief Creates operation with given identifier
563 * \param id - identifier of operation to be started
564 * \return Pointer on created operation or NULL if operation is not created
566 * Creates operation with given id. You should not call this method, it will be called
567 * automatically from startOperation. You may redefine this method in concrete module to
570 LightApp_Operation* LightApp_Module::createOperation( const int id ) const
576 return new LightApp_ShowHideOp( LightApp_ShowHideOp::DISPLAY );
577 else if( id==myErase )
578 return new LightApp_ShowHideOp( LightApp_ShowHideOp::ERASE );
579 else if( id==myDisplayOnly )
580 return new LightApp_ShowHideOp( LightApp_ShowHideOp::DISPLAY_ONLY );
581 else if( id==myEraseAll )
582 return new LightApp_ShowHideOp( LightApp_ShowHideOp::ERASE_ALL );
588 * \brief Virtual protected slot called when operation stopped
589 * \param theOp - stopped operation
591 * Virtual protected slot called when operation stopped. Redefine this slot if you want to
592 * perform actions after stopping operation
594 void LightApp_Module::onOperationStopped( SUIT_Operation* /*theOp*/ )
599 * \brief Virtual protected slot called when operation destroyed
600 * \param theOp - destroyed operation
602 * Virtual protected slot called when operation destroyed. Redefine this slot if you want to
603 * perform actions after destroying operation. Base implementation removes pointer on
604 * destroyed operation from the map of operations
606 void LightApp_Module::onOperationDestroyed()
608 const QObject* s = sender();
609 if( s && s->inherits( "LightApp_Operation" ) )
611 const LightApp_Operation* op = ( LightApp_Operation* )s;
612 MapOfOperation::const_iterator anIt = myOperations.begin(),
613 aLast = myOperations.end();
614 for( ; anIt!=aLast; anIt++ )
615 if( anIt.value()==op )
617 myOperations.remove( anIt.key() );
624 Must be redefined in order to use standard displayer mechanism
625 \return displayer of module
627 LightApp_Displayer* LightApp_Module::displayer()
633 SLOT: called on activating of standard operations show/hide
635 void LightApp_Module::onShowHide()
637 if( !sender()->inherits( "QAction" ) || !popupMgr() )
640 QAction* act = ( QAction* )sender();
641 int id = actionId( act );
643 startOperation( id );
647 virtual SLOT: called on view manager adding
649 void LightApp_Module::onViewManagerAdded( SUIT_ViewManager* )
654 virtual SLOT: called on view manager removing
656 void LightApp_Module::onViewManagerRemoved( SUIT_ViewManager* )
661 \brief Returns instance of operation by its id; if there is no operation
662 corresponding to this id, null pointer is returned
663 \param id - operation id
664 \return operation instance
666 LightApp_Operation* LightApp_Module::operation( const int id ) const
668 return myOperations.contains( id ) ? myOperations[id] : 0;
672 virtual method called to manage the same operations
674 bool LightApp_Module::reusableOperation( const int id )
681 \return true if module can copy the current selection
683 bool LightApp_Module::canCopy() const
690 \return true if module can paste previously copied data
692 bool LightApp_Module::canPaste() const
699 \brief Copies the current selection into clipboard
701 void LightApp_Module::copy()
707 \brief Pastes the current data in the clipboard
709 void LightApp_Module::paste()
715 \return true if module allows dragging the given object
717 bool LightApp_Module::isDraggable( const SUIT_DataObject* /*what*/ ) const
724 \return true if module allows dropping one or more objects (currently selected) on the object \c where
726 bool LightApp_Module::isDropAccepted( const SUIT_DataObject* /*where*/ ) const
733 Complete drag-n-drop operation by processing objects \a what being dragged, dropped to the line \a row
734 within the object \a where. The drop action being performed is specified by \a action.
736 void LightApp_Module::dropObjects( const DataObjectList& /*what*/, SUIT_DataObject* /*where*/,
737 const int /*row*/, Qt::DropAction /*action*/ )
742 \brief Return \c true if object can be renamed
744 bool LightApp_Module::renameAllowed( const QString& /*entry*/ ) const
750 Rename object by entry.
751 \param entry entry of the object
752 \param name new name of the object
753 \brief Return \c true if rename operation finished successfully, \c false otherwise.
755 bool LightApp_Module::renameObject( const QString& /*entry*/, const QString& /*name*/ )